fix(shader): prune unreachable CFG blocks before SSA #546

Manually merged
github merged 4 commits from github-bot/gh-546 into main 2026-12-05 17:30:41 +00:00 AGit
Owner

@⁠tommiehansen on GitHub · 2026-12-05 02:41 UTC · original
tommiehansen:review-split/404-cfg-fix → main · +338 −13 · 2 files · 4 commits · merged 2026-12-05 17:28 UTC

Latest CFG correctness verification

Exact-head audit at 201e316: debug and release shader suites passed; the deliberately omitted fallthrough-remap negative control failed as intended. This is a correctness fix, not an FPS claim.


Review split from #404, based on current upstream main. The diff contains this one slice.

Linux shader suite: 298 passed and 2 ignored. The audit job failed the inherited launcher-switch documentation check tracked in #528.

Exact pinned CI evidence

No hardware timing or gameplay results. The original PR remains available until maintainers choose the integration order.

Post-review follow-up — 2026-12-05

Head: e869080060209b9eab3d014df25a6335c3d6bad4.

The CFG pruning fix now conservatively bumps TRANSLATOR_VERSION from 20 to 21, since dead blocks in formerly successful translations can affect resource/IO analysis. Reconcile the version with main at merge time. Wrapped the reviewed formatting issues. The version bump does not establish an observed DXIL difference; the prior 840-container parity evidence remains scoped to 31e7e78.

Fresh pinned debug/release CI started; new test and performance results must be reported at this head. Previous validation is retained at its original revision.

Targeted branch-remap regression

Head: 201e3166e905dc5b5ad849673bc5d48463b8a2e5. Added pruning_remaps_both_branch_targets_past_a_dead_block to cover the fallthrough-remap gap identified by Kal0s-design. The test verifies both targets, predecessor lists and reachability after pruning. Production code is unchanged in this commit. Debug/release and deliberate missing-remap negative control pending.

**@⁠tommiehansen** on GitHub · 2026-12-05 02:41 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/546) `tommiehansen:review-split/404-cfg-fix` → `main` · +338 −13 · 2 files · 4 commits · merged 2026-12-05 17:28 UTC ## Latest CFG correctness verification [Exact-head audit](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47503802058/job/145623465963) at `201e316`: debug and release shader suites passed; the deliberately omitted fallthrough-remap negative control failed as intended. This is a correctness fix, not an FPS claim. --- Review split from #404, based on current upstream main. The diff contains this one slice. Linux shader suite: 298 passed and 2 ignored. The audit job failed the inherited launcher-switch documentation check tracked in #528. [Exact pinned CI evidence](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47466412636/job/145504218739) No hardware timing or gameplay results. The original PR remains available until maintainers choose the integration order. ### Post-review follow-up — 2026-12-05 Head: `e869080060209b9eab3d014df25a6335c3d6bad4`. The CFG pruning fix now conservatively bumps TRANSLATOR_VERSION from 20 to 21, since dead blocks in formerly successful translations can affect resource/IO analysis. Reconcile the version with main at merge time. Wrapped the reviewed formatting issues. The version bump does not establish an observed DXIL difference; the prior 840-container parity evidence remains scoped to 31e7e78. [Fresh pinned debug/release CI](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47499397549) started; new test and performance results must be reported at this head. Previous validation is retained at its original revision. ### Targeted branch-remap regression Head: `201e3166e905dc5b5ad849673bc5d48463b8a2e5`. Added `pruning_remaps_both_branch_targets_past_a_dead_block` to cover the fallthrough-remap gap identified by Kal0s-design. The test verifies both targets, predecessor lists and reachability after pruning. Production code is unchanged in this commit. [Debug/release and deliberate missing-remap negative control](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47503802058) pending.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 02:41:32 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 04:28 UTC · original

Checked 31e7e78 on native Windows (one NVIDIA desktop card plus WARP, didn't run the game). Merge base is b581f7e, current main.

The body doesn't say what goes wrong without this, so here is what I found it fixes. On main, thread_edges, without_early_outs and without_skips can leave blocks that nothing reaches. link still counts them as predecessors, rename only walks reverse postorder, so a live join fed by a dead block gets a phi arg from lookup on an empty outs entry, which is NONE. In a debug build that trips the debug_assert at ir.rs:746 on main. In release there's no assert, and remove_trivial_phis then does replaced[NONE] (ir.rs:1562 into 1539 on main), which is an out-of-bounds panic. The translate callers in victory-gpu catch panics, so on main those shaders just come out as not translated. I put the PR's four new tests on main. thread_edges_prunes_orphaned_join_and_keeps_valid_ssa and prune_unreachable_drops_dead_preds_... hit that debug_assert, so they reproduce the bug for real. The two without_* tests only fail on main because of the block count asserts (3 vs 2): main keeps the dead early-exit block, but that block feeds nothing, so it's harmless there.

The pruning looks right to me. decode only follows reachable flow, so build() never has dead blocks in practice. There prune_unreachable (ir.rs:1299) does one RPO and returns it as is. The order is the same one rename computed on main, so for programs with no dead blocks the IR is unchanged. When it does prune, block 0 stays block 0 (the entry is always reachable), the kept blocks keep their relative order, Jump/Branch targets get remapped, and link rebuilds preds. Exits are the only place a block index lives inside Block. Phis are cleared before every rename, and nothing else keyed by block index crosses from one program to another: every form in emit/mod.rs re-runs structure, io and resource on the new program, and widen stays on one program. Indirect has no successors and decode never follows it, so no block is reachable only through an s_setpc. Exec-dependent branches keep both edges, so a "not taken" side is never pruned. In split_irreducible (ir.rs:1418) each iteration now prunes before multi_entry_loop. Everything indexed (order, index, region, copy) is recomputed per iteration, so the renumbering can't leave stale indices behind. It also means dead blocks can't be chosen as part of a loop region any more, which could happen on main because it passes an all-true mask. The order returned at ir.rs:1427 is the one computed right after the last prune, with nothing changed after it, so passing it to rename_with_order is sound.

With cargo test -p victory-shader --lib -- --test-threads=4 (debug) main gave 419 passed, 1 failed, 4 ignored. On the PR, 423 passed, 1 failed, 4 ignored. The failure is the same one on both, size_tests::a_reused_descriptor_register_read_only_behind_its_flag, which already fails on main. The four new tests pass. Clippy with --lib --tests --examples gives the same 12 warnings on both (--all-targets stops on the same pre-existing guard_runs bench error on both). One nit: the file isn't rustfmt-clean after this. The rename_with_order signature and the two assert_ne! lines in prune_unreachable need wrapping, and main's ir.rs is clean.

For a differential I hashed every DXIL container the crate's test suite produces on both trees, and all 840 of 840 came out identical. One of those programs actually goes down the changed path. In a_far_early_out_is_left_out_and_the_gate_counts_the_lanes_of_both_halves, without_early_outs leaves a dead block that feeds a live one on main (no phi needed there, so main didn't panic). That form's DXIL is byte-identical, passes dxil.dll validation and runs on the card in both trees. I also ran a throwaway fuzz, not part of the PR. One part was 200k random CFGs through prune_unreachable, about 166k of them with dead blocks. Every remaining block is reachable, entry is unchanged, edges map to the same original blocks, preds match a fresh link, and the returned order equals main's RPO remapped. The other part was 40k random CFGs with scalar reads and writes through split_irreducible plus rename, then random pred/join/dest triples through thread_edges. On the PR, 0 panics and 0 invalid SSA (no NONE, no dangling args, phi arity equal to preds). On main the same inputs panic about 31k times, plus about 1k panics in thread_edges. For the about 7k graphs with no dead blocks, and the about 1.6k threadings that leave none, a hash over the Debug dump of the resulting Program is the same on both trees.

About TRANSLATOR_VERSION, shaders that panicked before and translate now fall under the "did not translate before" exemption in lib.rs. The case I can't rule out is a form that did translate on main while carrying a dead block whose stale contents mattered. io.rs:373, resource.rs:238/520 and parts of lower.rs walk every block, dead ones included. If such a block held a resource access or a register read, the PR's output for that shader would change. The one such program in the tests came out identical, but that's one program. A victory shaders pass on a real cache would answer it. I couldn't do that without game data, so either run it or bump to be safe. Those forms only run for wave64 compute on 32-lane hosts, plus the threading retry.

As for the other two splits, this merges cleanly with #545 (structure.rs only, and that one already treats unreachable blocks as usize::MAX idoms). It conflicts with #547 only at the end of the ir.rs tests module, where both append tests. The logic hunks auto-merge, and the merged code only calls rename_with_order, with rename used just by the tests, so it's a rebase-order thing, not a real dependency. I didn't check whether the body's Linux count (298 passed, 2 ignored) matches; on Windows the suite has more tests.

I didn't test release-build behaviour (the release panic above is from reading the code, not from running it), real game shaders, timing, gameplay, and AMD or other vendors' drivers.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 04:28 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/546#issuecomment-7431859673) Checked 31e7e78 on native Windows (one NVIDIA desktop card plus WARP, didn't run the game). Merge base is b581f7e, current main. The body doesn't say what goes wrong without this, so here is what I found it fixes. On main, `thread_edges`, `without_early_outs` and `without_skips` can leave blocks that nothing reaches. `link` still counts them as predecessors, `rename` only walks reverse postorder, so a live join fed by a dead block gets a phi arg from `lookup` on an empty `outs` entry, which is NONE. In a debug build that trips the `debug_assert` at ir.rs:746 on main. In release there's no assert, and `remove_trivial_phis` then does `replaced[NONE]` (ir.rs:1562 into 1539 on main), which is an out-of-bounds panic. The translate callers in victory-gpu catch panics, so on main those shaders just come out as not translated. I put the PR's four new tests on main. `thread_edges_prunes_orphaned_join_and_keeps_valid_ssa` and `prune_unreachable_drops_dead_preds_...` hit that debug_assert, so they reproduce the bug for real. The two `without_*` tests only fail on main because of the block count asserts (3 vs 2): main keeps the dead early-exit block, but that block feeds nothing, so it's harmless there. The pruning looks right to me. `decode` only follows reachable flow, so `build()` never has dead blocks in practice. There `prune_unreachable` (ir.rs:1299) does one RPO and returns it as is. The order is the same one `rename` computed on main, so for programs with no dead blocks the IR is unchanged. When it does prune, block 0 stays block 0 (the entry is always reachable), the kept blocks keep their relative order, `Jump`/`Branch` targets get remapped, and `link` rebuilds preds. Exits are the only place a block index lives inside `Block`. Phis are cleared before every rename, and nothing else keyed by block index crosses from one program to another: every form in emit/mod.rs re-runs structure, io and resource on the new program, and `widen` stays on one program. `Indirect` has no successors and `decode` never follows it, so no block is reachable only through an `s_setpc`. Exec-dependent branches keep both edges, so a "not taken" side is never pruned. In `split_irreducible` (ir.rs:1418) each iteration now prunes before `multi_entry_loop`. Everything indexed (order, index, region, copy) is recomputed per iteration, so the renumbering can't leave stale indices behind. It also means dead blocks can't be chosen as part of a loop region any more, which could happen on main because it passes an all-true mask. The order returned at ir.rs:1427 is the one computed right after the last prune, with nothing changed after it, so passing it to `rename_with_order` is sound. With `cargo test -p victory-shader --lib -- --test-threads=4` (debug) main gave 419 passed, 1 failed, 4 ignored. On the PR, 423 passed, 1 failed, 4 ignored. The failure is the same one on both, `size_tests::a_reused_descriptor_register_read_only_behind_its_flag`, which already fails on main. The four new tests pass. Clippy with `--lib --tests --examples` gives the same 12 warnings on both (`--all-targets` stops on the same pre-existing guard_runs bench error on both). One nit: the file isn't rustfmt-clean after this. The `rename_with_order` signature and the two `assert_ne!` lines in `prune_unreachable` need wrapping, and main's ir.rs is clean. For a differential I hashed every DXIL container the crate's test suite produces on both trees, and all 840 of 840 came out identical. One of those programs actually goes down the changed path. In `a_far_early_out_is_left_out_and_the_gate_counts_the_lanes_of_both_halves`, `without_early_outs` leaves a dead block that feeds a live one on main (no phi needed there, so main didn't panic). That form's DXIL is byte-identical, passes dxil.dll validation and runs on the card in both trees. I also ran a throwaway fuzz, not part of the PR. One part was 200k random CFGs through `prune_unreachable`, about 166k of them with dead blocks. Every remaining block is reachable, entry is unchanged, edges map to the same original blocks, preds match a fresh `link`, and the returned order equals main's RPO remapped. The other part was 40k random CFGs with scalar reads and writes through `split_irreducible` plus rename, then random pred/join/dest triples through `thread_edges`. On the PR, 0 panics and 0 invalid SSA (no NONE, no dangling args, phi arity equal to preds). On main the same inputs panic about 31k times, plus about 1k panics in `thread_edges`. For the about 7k graphs with no dead blocks, and the about 1.6k threadings that leave none, a hash over the Debug dump of the resulting Program is the same on both trees. About TRANSLATOR_VERSION, shaders that panicked before and translate now fall under the "did not translate before" exemption in lib.rs. The case I can't rule out is a form that did translate on main while carrying a dead block whose stale contents mattered. io.rs:373, resource.rs:238/520 and parts of lower.rs walk every block, dead ones included. If such a block held a resource access or a register read, the PR's output for that shader would change. The one such program in the tests came out identical, but that's one program. A `victory shaders` pass on a real cache would answer it. I couldn't do that without game data, so either run it or bump to be safe. Those forms only run for wave64 compute on 32-lane hosts, plus the threading retry. As for the other two splits, this merges cleanly with #545 (structure.rs only, and that one already treats unreachable blocks as `usize::MAX` idoms). It conflicts with #547 only at the end of the `ir.rs` tests module, where both append tests. The logic hunks auto-merge, and the merged code only calls `rename_with_order`, with `rename` used just by the tests, so it's a rebase-order thing, not a real dependency. I didn't check whether the body's Linux count (298 passed, 2 ignored) matches; on Windows the suite has more tests. I didn't test release-build behaviour (the release panic above is from reading the code, not from running it), real game shaders, timing, gameplay, and AMD or other vendors' drivers. AI-assisted: yes
Author
Owner

@⁠tommiehansen on GitHub · 2026-12-05 10:18 UTC · original

Thanks @⁠Kal0s-design for the thorough CFG review. The translator version is now 21 to avoid stale cached translations, and the formatting issues are fixed. 201e316 adds coverage for both branch targets after pruning. Debug/release CI and the deliberate missing-remap negative control passed.

**@⁠tommiehansen** on GitHub · 2026-12-05 10:18 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/546#issuecomment-7436352715) Thanks @⁠Kal0s-design for the thorough CFG review. The translator version is now 21 to avoid stale cached translations, and the formatting issues are fixed. [201e316](https://github.com/tommiehansen/VIctory-xmem/commit/201e3166e905dc5b5ad849673bc5d48463b8a2e5) adds coverage for both branch targets after pruning. [Debug/release CI and the deliberate missing-remap negative control](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47503802058/job/145623465963) passed.
github manually merged commit d8a701da87 into main 2026-12-05 17:30:41 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.