Shaders: prune unreachable blocks before SSA construction #1484

Closed
github-bot wants to merge 1 commit from gh-pr/1484/head into gh-pr/1484/base
Owner

@⁠chiperandrei42 on GitHub · 2026-11-24 18:21 UTC · original
chiperandrei42:fix/ir-prune-unreachable-blocks → main · +177 −10 · 1 files · 1 commits · closed without merging

Summary: Fixes VIC-BT-002 from #1242:

Prunes blocks unreachable from entry block 0 (remapping Exit::Jump and Exit::Branch targets in code order) before link(&mut blocks) and after split_irreducible if any blocks become unreachable.
Ensures Block::preds, reverse_postorder, liveness, and SSA phi argument construction never observe unreachable predecessors, preventing NONE (Value(u32::MAX)) from entering phi.args and panicking remove_trivial_phis in release builds.
Factors SSA construction into build_ssa and adds synthetic unit tests (prunes_unreachable_predecessor_before_ssa, prunes_unreachable_edge_into_entry_and_merge).

Verification:

cargo test --workspace (122 passed, 1 ignored in victory-shader)
cargo test --release -p victory-shader (122 passed, 1 ignored)
cargo clippy --workspace --all-targets -- -D warnings
cargo xtask policy
victory translate --validate on local dump (26,148/26,148 translated; zero regressions)

**@⁠chiperandrei42** on GitHub · 2026-11-24 18:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/1484) `chiperandrei42:fix/ir-prune-unreachable-blocks` → `main` · +177 −10 · 1 files · 1 commits · closed without merging Summary: Fixes VIC-BT-002 from #1242: Prunes blocks unreachable from entry block 0 (remapping Exit::Jump and Exit::Branch targets in code order) before link(&mut blocks) and after split_irreducible if any blocks become unreachable. Ensures Block::preds, reverse_postorder, liveness, and SSA phi argument construction never observe unreachable predecessors, preventing NONE (Value(u32::MAX)) from entering phi.args and panicking remove_trivial_phis in release builds. Factors SSA construction into build_ssa and adds synthetic unit tests (prunes_unreachable_predecessor_before_ssa, prunes_unreachable_edge_into_entry_and_merge). Verification: cargo test --workspace (122 passed, 1 ignored in victory-shader) cargo test --release -p victory-shader (122 passed, 1 ignored) cargo clippy --workspace --all-targets -- -D warnings cargo xtask policy victory translate --validate on local dump (26,148/26,148 translated; zero regressions)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:33:35 +00:00
github-bot closed this pull request 2026-11-26 04:33:35 +00:00
Author
Owner

@⁠wbkepler on GitHub · 2026-11-24 20:59 UTC · original

A note to read alongside #1619, which pins the same VIC-BT-002 invariants from the other side.

From reading ir.rs and gfx10/mod.rs at ffe310e: build() can't produce an unreachable block today. gfx10::decode only decodes instructions it reaches from offset 0, and build() turns those same Flow edges into Exits. The one place they can differ is far_jump. decode resolves it against the instructions decoded so far, and build() resolves it against the full list. That can only add an edge in build(), never remove one. So every leader is reachable, every outs[p] is filled before the phi arguments are read, and lookup can't return NONE. #1619's a_branch_skips_its_dead_code_before_the_ir_is_built shows this for the VIC-BT-002 reproducer: the dead code at 4 and 8 is never decoded.

The second prune in build_ssa also can't fire. split_irreducible only moves edges into a multi-entry SCC, and its header keeps an outside predecessor, so the original entry and every copied block stay reachable.

So prune_unreachable changes nothing for any program that goes through build(), and I see no correctness problem with keeping it as a guard. The two new tests build their Blocks by hand and skip decode, so they show the guard working but not a program that reached the release panic. If you have an input that hits it through build(), a build_words test would pin the real trigger. If not, it may be worth calling this defensive in the description. It is compatible with #1619's assert_well_formed (both only add tests at the end of ir::tests, so the conflict is textual).

AI-assisted review (Claude Code); checked on Windows 11 / RTX 5090.

**@⁠wbkepler** on GitHub · 2026-11-24 20:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/1484#issuecomment-7144330948) A note to read alongside #1619, which pins the same VIC-BT-002 invariants from the other side. From reading `ir.rs` and `gfx10/mod.rs` at `ffe310e`: `build()` can't produce an unreachable block today. `gfx10::decode` only decodes instructions it reaches from offset 0, and `build()` turns those same `Flow` edges into `Exit`s. The one place they can differ is `far_jump`. `decode` resolves it against the instructions decoded so far, and `build()` resolves it against the full list. That can only add an edge in `build()`, never remove one. So every leader is reachable, every `outs[p]` is filled before the phi arguments are read, and `lookup` can't return `NONE`. #1619's `a_branch_skips_its_dead_code_before_the_ir_is_built` shows this for the VIC-BT-002 reproducer: the dead code at 4 and 8 is never decoded. The second prune in `build_ssa` also can't fire. `split_irreducible` only moves edges into a multi-entry SCC, and its header keeps an outside predecessor, so the original entry and every copied block stay reachable. So `prune_unreachable` changes nothing for any program that goes through `build()`, and I see no correctness problem with keeping it as a guard. The two new tests build their `Block`s by hand and skip `decode`, so they show the guard working but not a program that reached the release panic. If you have an input that hits it through `build()`, a `build_words` test would pin the real trigger. If not, it may be worth calling this defensive in the description. It is compatible with #1619's `assert_well_formed` (both only add tests at the end of `ir::tests`, so the conflict is textual). _AI-assisted review (Claude Code); checked on Windows 11 / RTX 5090._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:41 UTC · original

Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current).

Prunes unreachable blocks before SSA, a state ir::build cannot produce: the decoder only yields instructions reachable from offset 0 and node splitting keeps the originals reachable (#1619 shows this with tests). Its own tests feed hand-built blocks; 177 lines of hardening for nothing.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/1484#issuecomment-7148768868) Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current). Prunes unreachable blocks before SSA, a state `ir::build` cannot produce: the decoder only yields instructions reachable from offset 0 and node splitting keeps the originals reachable (#1619 shows this with tests). Its own tests feed hand-built blocks; 177 lines of hardening for nothing. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1484
No description provided.