Shaders: prune unreachable blocks before SSA construction #1483

Closed
chiperandrei42 wants to merge 1 commit from upstream-pr/1484 into main
chiperandrei42 commented 2026-11-24 18:21:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by chiperandrei42 on 2026-11-24T18:21:16Z.


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)

<!-- greyfinch-sync:Marvisak/VIctory:issue:6588543430 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1484). Originally posted by [chiperandrei42](https://github.com/chiperandrei42) on 2026-11-24T18:21:16Z. --- 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)
DenRakEiw commented 2026-11-24 19:03:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T18:28:10Z.

Review: APPROVED


Verified locally merged onto origin/main (Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib ir:: — 8 passed, 0 failed (including prunes_unreachable_predecessor_before_ssa and prunes_unreachable_edge_into_entry_and_merge)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok

Clean reachability pruning and exit-target remapping prior to linking and SSA construction. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6045045582 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1484#pullrequestreview-6045045582). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T18:28:10Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-shader --lib ir::` — 8 passed, 0 failed (including `prunes_unreachable_predecessor_before_ssa` and `prunes_unreachable_edge_into_entry_and_merge`) - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok Clean reachability pruning and exit-target remapping prior to linking and SSA construction. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-24 19:03:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T18:42:08Z.

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 → 98 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and were not run here; lowering/structure coverage only.

Pruning unreachable blocks before SSA construction (VIC-BT-002): prune_unreachable remaps Exit::Jump/Branch targets before retain, keeps original order, and build_ssa re-prunes + re-links if split_irreducible leaves the RPO incomplete. Entry block 0 is always kept; Exit::Indirect/End untouched. Both synthetic unit tests (unreachable predecessor, edge into entry + merge phi) run on macOS — 98 passed (+2).

<!-- greyfinch-sync:Marvisak/VIctory:review:6045232784 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1484#pullrequestreview-6045232784). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T18:42:08Z. **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` → 98 passed, 0 failed. Execution/valu tests are `#[cfg(all(test, windows))]` and were not run here; lowering/structure coverage only. Pruning unreachable blocks before SSA construction (VIC-BT-002): `prune_unreachable` remaps `Exit::Jump`/`Branch` targets before `retain`, keeps original order, and `build_ssa` re-prunes + re-links if `split_irreducible` leaves the RPO incomplete. Entry block 0 is always kept; `Exit::Indirect`/`End` untouched. Both synthetic unit tests (unreachable predecessor, edge into entry + merge phi) run on macOS — 98 passed (+2).
wbkepler commented 2026-11-24 21:03:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T20:59:36Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144330948 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1484#issuecomment-7144330948). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T20:59:36Z. --- 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._
upstream-sync closed this pull request 2026-11-25 01:06:40 +00:00
Marvisak commented 2026-11-25 01:08:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:41:06Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148768868 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1484#issuecomment-7148768868). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:41:06Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1483
No description provided.