Shaders: guard both back edges when a conditional branch targets two … #1539
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1540"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-24T18:58:36Z.
Problem
In
crates/victory-shader/src/emit/lower.rs,LaneExit::Branchhandles loop-counter increments withif back(self, taken) { ... } else if back(self, not_taken) { ... }, and the subsequententer_looppass skips any target whereback(self, to)is true.When both arms of a conditional branch are back edges (for example, an inner loop latch placed before an outer loop header where
takencontinues the inner loop andnot_takenfalls straight back into the outer loop header),not_takennever callscount_iteration: itsLOOP_LIMITcounter phi is missing its incoming edge from that block (producing invalid DXIL) and that back edge has no iteration cap.Fix
takenandnot_takenare back edges (and distinct), emit dedicated per-edge guard blocks recorded inself.edges[(b, to)]that each increment and bound their target header's counter before branching to the header orabort_block().taken == not_takenis a back edge, emit a single bounded back-edge branch directly to the header orabort_block().dual_back_edge_branch_counts_both_loop_headersverifying both loop headers receive all counter-phi incoming edges and pass DXIL validation whendxil.dllis present.Verification
windows-latestCI (cargo clippydefault +--all-featuresandcargo test --workspace --locked).Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T19:20:54Z.
Review: APPROVED
Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (
ffe310e),cargo test -p victory-shader --locked→ 97. Execution/valu tests are#[cfg(all(test, windows))]and were not run here.Conditional branches whose BOTH arms are back edges (inner latch returning to the inner or the outer header) previously only counted one side — the other loop header's counter phi never got its incoming edge, leaving that loop unbounded. Now each back edge gets its own guard block with its own
count_iteration_fromand abort, and thetaken == not_takencase folds to a single guarded branch. The macOS unit test asserts both headers' counter phis are populated (counter_edges == 4); DXIL validation is Windows-gated. 97 passed (+1).Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-24T19:33:46Z.
Offline measurement on a retail dump, and the shader tests on Ada
Result: no count changes against
main, and the tests pass on Ada.Offline. I merged head
1ccae2dontomainffe310e(clean merge) and ranvictory translate --validate --listonshaders.rpf(1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result withmainprogram by program (25,889 programs). Validated: 21,724 on both, 0 gained, 0 lost. No program changes its failure reason either.This is the expected result for a correctness fix whose instructions sit behind the earlier failures on
main. The count shows that the change rejects nothing. It does not show whether the output is right.Tests. With the same merge,
cargo test --locked -p victory-shader --libpassed 121, failed 0. The bitcode benchmark is ignored, as onmain, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, withVICTORY_WARPunset.No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:30:18Z.
Automated triage for the maintainers.
Merge candidate. The rebase onto
mainis clean, and the logic is as reviewed before. InLaneExit::Branch(crates/victory-shader/src/emit/lower.rs), when both arms of a conditional branch are back edges, each arm now gets its own guard block. That block counts the iteration for its own header throughcount_iteration_fromand branches to the header or toabort_block(), and it is recorded inself.edgesso the header's other phis see it as the predecessor. When both arms go to the same header, there is a single guarded branch. Previously thenot_takenheader's counter phi was missing an incoming edge, and that edge had no iteration cap.dual_back_edge_branch_counts_both_loop_headerschecks that all four counter-phi edges exist and that the DXIL validates.On your reply: confirmed. It merges cleanly with current
main. One thing for the maintainers: with this PR merged,emit/lower.rsis 524,196 bytes, only 92 bytes under the 524,288-byte limit inxtask/src/policy.rs. #1271 and #1315 also grow that file, so only one of them can land beforelower.rsis split.CI: passing (
check).Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T22:46:33Z.
Rebased cleanly onto
main(f3578e3) inf207efe. The conflicts were only adjacent#[cfg(test)]fields onLowered(uses_lane_index/has_half_wave_mask) and the newds_bpermute_*tests incrates/victory-shader/src/emit/lower.rs; the dual back-edge guard logic anddual_back_edge_branch_counts_both_loop_headerstest are unchanged.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:44:56Z.
Code review and RTX 5090 + WARP + DXIL verification at head
f207efe21429merged cleanly ontomain(63bcca43c6a0):crates/victory-shader/src/emit/lower.rs:10195-10220):LaneExit::Branchare back edges (self.counters.contains_key(&to) && self.rpo[to] <= self.rpo[b]): thetaken == not_takencase guards once directly fromself.cur(avoiding double-increment), whiletaken != not_takenallocates dedicated edge blocks checking each target's loop counter againstLOOP_LIMITand registersself.edges.insert((b, to), edge).self.edgesconsumers inlower.rs(lines 5490, 5631, 5695, 10877, 10940): every one resolves predecessors viaself.edges.get(&(q, b)).copied().unwrap_or(qb), keeping data/mask/descriptor/counter phis at both loop headers consistent without resetting back-edge counters viaenter_loop.lower.rsbudget warning:main(63bcca43),crates/victory-shader/src/emit/lower.rsreaches524,196bytes — only92bytes below the524,288-bytextask policylimit. Worth noting for merge ordering alongside otherlower.rsPRs (or trimming a few lines of comments / extracting a helper if anotherlower.rsPR lands first).dxil.dll10.0.26100.0,VICTORY_D3D12_DEBUG=1, Rust 1.98.1):cargo xtask policy:191 files ok;cargo clippy -p victory-shader --all-targets -- -D warnings(default +--all-features): 0 warningsemit::lower::tests emit::tests structure::tests: 64/64 passed (1 ignored:bench_bools) on both RTX 5090 and WARP.Logic and DXIL validation look solid (just watch the 92 B headroom in
lower.rs); LGTM.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.