Shaders: collapse duplicate branch targets and dedup phi predecessors #1460

Open
github-bot wants to merge 1 commit from github-bot/gh-1460 into main AGit
Owner

@⁠piyonchu on GitHub · 2026-11-24 18:12 UTC · original
piyonchu:fix/lower-duplicate-critical-edge → main · +132 −12 · 2 files · 1 commits · open

Summary

Fixes invalid DXIL phi nodes when a structured branch (LaneExit::Branch) has identical taken and not_taken targets (VIC-BT-007).

Cause

When a degenerate conditional branch had taken == not_taken, Lower::exit lowered it as a conditional branch to the same target and created duplicate outgoing control-flow edges, causing DXIL phi nodes in the successor to repeat the same predecessor block and fail DXIL validation (PHI node entries do not match predecessors!).

Change

  • crates/victory-shader/src/emit/lower.rs:
    • Collapse LaneExit::Branch { taken, not_taken, .. } if taken == not_taken in Lower::exit through the same jump path as LaneExit::Jump (handling both counted back-edges via CondBr and forward jumps via Br).
    • Deduplicate repeated incoming blocks when building DXIL phi entries in Source::Phi (immediate samplers), uniform_heap_index, Source::LanePhi, data_phis, and pending_masks, matching the single collapsed Br.
    • Add duplicate_branch_target_back_edge_produces_valid_dxil testing a counted back-edge whose degenerate Branch targets the loop header twice with duplicate IR predecessors.
  • crates/victory-shader/src/emit/valu_tests.rs:
    • Add duplicate_branch_target_critical_edge_produces_valid_dxil verifying DXIL validation and execution on hardware/WARP when a branch's taken and fallthrough target the same join block.

Verification

Rebased onto main (13f91ca, commit 954ef58):

  • cargo clippy -p victory-shader --all-targets -- -D warnings -A clippy::needless_range_loop -A deprecated: pass.
  • cargo test -p victory-shader --lib duplicate_branch_target: 2 passed, 0 failed.
  • cargo test -p xtask: 6 passed, 0 failed.
  • git diff --check upstream/main..HEAD: clean.

AI-assisted; no proprietary game files used.

**@⁠piyonchu** on GitHub · 2026-11-24 18:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460) `piyonchu:fix/lower-duplicate-critical-edge` → `main` · +132 −12 · 2 files · 1 commits · open ### Summary Fixes invalid DXIL `phi` nodes when a structured branch (`LaneExit::Branch`) has identical `taken` and `not_taken` targets (`VIC-BT-007`). ### Cause When a degenerate conditional branch had `taken == not_taken`, `Lower::exit` lowered it as a conditional branch to the same target and created duplicate outgoing control-flow edges, causing DXIL `phi` nodes in the successor to repeat the same predecessor block and fail DXIL validation (`PHI node entries do not match predecessors!`). ### Change - **`crates/victory-shader/src/emit/lower.rs`**: - Collapse `LaneExit::Branch { taken, not_taken, .. } if taken == not_taken` in `Lower::exit` through the same `jump` path as `LaneExit::Jump` (handling both counted back-edges via `CondBr` and forward jumps via `Br`). - Deduplicate repeated incoming blocks when building DXIL `phi` entries in `Source::Phi` (immediate samplers), `uniform_heap_index`, `Source::LanePhi`, `data_phis`, and `pending_masks`, matching the single collapsed `Br`. - Add `duplicate_branch_target_back_edge_produces_valid_dxil` testing a counted back-edge whose degenerate `Branch` targets the loop header twice with duplicate IR predecessors. - **`crates/victory-shader/src/emit/valu_tests.rs`**: - Add `duplicate_branch_target_critical_edge_produces_valid_dxil` verifying DXIL validation and execution on hardware/WARP when a branch's `taken` and fallthrough target the same join block. ### Verification Rebased onto `main` (`13f91ca`, commit `954ef58`): - `cargo clippy -p victory-shader --all-targets -- -D warnings -A clippy::needless_range_loop -A deprecated`: pass. - `cargo test -p victory-shader --lib duplicate_branch_target`: **2 passed, 0 failed**. - `cargo test -p xtask`: **6 passed, 0 failed**. - `git diff --check upstream/main..HEAD`: clean. AI-assisted; no proprietary game files used.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:32:43 +00:00
github-bot changed title from Shaders: keep distinct critical-edge blocks for duplicate branch targets to Shaders: collapse duplicate branch targets and dedup phi predecessors 2026-11-26 06:29:31 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 18:21 UTC · original

Tested commit \ f443c2\ on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, SM 6.6) + WARP:

  • \cargo test -p victory-shader --lib valu_tests: 14/14 passed (including \duplicate_branch_target_critical_edge_produces_valid_dxil).

One edge case noticed during code audit:

  • In \Lower::exit\ (\crates/victory-shader/src/emit/lower.rs:6778-6805), if a degenerate \LaneExit::Branch { taken, not_taken, .. }\ with \ aken == not_taken\ ever targets a loop back-edge (\self.rpo[to] <= self.rpo[from]), \self.edge\ returns \ arget\ directly (without inserting a critical-edge block), emitting \Term::CondBr(cond, target, target)\ (two edges from \self.cur) while \count_iteration\ only executes in the first \if back(self, taken)\ branch and pushes one entry into \self.counters[&taken]. Collapsing \ aken == not_taken\ to \LaneExit::Jump(taken)\ (and deduplicating duplicate predecessor blocks in \ ill_phis\ / \Source::Phi\ / \Source::LanePhi) would handle both forward critical edges and counted back-edges without extra edge blocks.

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 18:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#issuecomment-7140522567) Tested commit \ f443c2\ on **Windows 11 / AMD Radeon RX 9060 XT (RDNA4, SM 6.6)** + **WARP**: - \cargo test -p victory-shader --lib valu_tests\: **14/14 passed** (including \duplicate_branch_target_critical_edge_produces_valid_dxil\). One edge case noticed during code audit: - In \Lower::exit\ (\crates/victory-shader/src/emit/lower.rs:6778-6805\), if a degenerate \LaneExit::Branch { taken, not_taken, .. }\ with \ aken == not_taken\ ever targets a loop back-edge (\self.rpo[to] <= self.rpo[from]\), \self.edge\ returns \ arget\ directly (without inserting a critical-edge block), emitting \Term::CondBr(cond, target, target)\ (two edges from \self.cur\) while \count_iteration\ only executes in the first \if back(self, taken)\ branch and pushes one entry into \self.counters[&taken]\. Collapsing \ aken == not_taken\ to \LaneExit::Jump(taken)\ (and deduplicating duplicate predecessor blocks in \ ill_phis\ / \Source::Phi\ / \Source::LanePhi\) would handle both forward critical edges and counted back-edges without extra edge blocks. *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠piyonchu on GitHub · 2026-11-24 19:00 UTC · original · edited 2026-11-24 19:45 UTC

Thanks for catching the counted back-edge case, @⁠geiyiliyabumo. Collapsed LaneExit::Branch { taken, not_taken, .. } if taken == not_taken to the Jump path in Lower::exit (with incoming DXIL block deduplication across Source::Phi, Source::LanePhi, data_phis, and pending_masks), restored single-entry edges, and added duplicate_branch_target_back_edge_produces_valid_dxil covering a counted back-edge with taken == not_taken. Re-ran victory-shader (121 passed, 0 failed, 1 ignored), workspace clippy (default and --all-features), and xtask policy checks.

**@⁠piyonchu** on GitHub · 2026-11-24 19:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#issuecomment-7141456304) · edited 2026-11-24 19:45 UTC Thanks for catching the counted back-edge case, @⁠geiyiliyabumo. Collapsed `LaneExit::Branch { taken, not_taken, .. } if taken == not_taken` to the `Jump` path in `Lower::exit` (with incoming DXIL block deduplication across `Source::Phi`, `Source::LanePhi`, `data_phis`, and `pending_masks`), restored single-entry `edges`, and added `duplicate_branch_target_back_edge_produces_valid_dxil` covering a counted back-edge with `taken == not_taken`. Re-ran `victory-shader` (121 passed, 0 failed, 1 ignored), workspace `clippy` (default and `--all-features`), and `xtask` policy checks.
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-24 20:43 UTC · original

Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head 667de6e onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a 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 --lib passed 122, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-24 20:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#issuecomment-7143980217) ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head 667de6e onto `main` ffe310e (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 21,724 on `main`, 21,724 with this PR. No program changes its failure reason either. That is the expected result for a 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 --lib` passed 122, failed 0. The bitcode benchmark is ignored, as on `main`, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with `VICTORY_WARP` unset. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:28 UTC · original · edited 2026-11-26 03:25 UTC

Automated triage for the maintainers.

Verdict: merge candidate (as before the rebase). Thanks for rebasing; the branch merges cleanly into current main again.

crates/victory-shader/src/emit/lower.rs: Lower::exit now lowers a LaneExit::Branch whose taken == not_taken through the same path as LaneExit::Jump (counted back-edge CondBr or plain Br), and the five phi builders (Source::Phi, uniform_heap_index, Source::LanePhi, data_phis, pending_masks) skip a repeated incoming block, so the single collapsed edge gets one phi entry. That matches DXIL's one-entry-per-predecessor rule, and the duplicated IR predecessor carries the same register state on both edges, so dropping the second entry loses nothing. Both tests (forward join in valu_tests.rs, counted back-edge in lower.rs) cover the two shapes. main has no equivalent handling.

Minor: the title still says "keep distinct critical-edge blocks", but the change now collapses the branch; worth retitling.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#issuecomment-7151946128) · edited 2026-11-26 03:25 UTC <!-- victory-triage v1 head=67c70115414ad26ae1790af97edb01a6b1d81ebe verdict=merge-candidate at=2026-11-26T03:25:19Z --> Automated triage for the maintainers. **Verdict: merge candidate** (as before the rebase). Thanks for rebasing; the branch merges cleanly into current `main` again. `crates/victory-shader/src/emit/lower.rs`: `Lower::exit` now lowers a `LaneExit::Branch` whose `taken == not_taken` through the same path as `LaneExit::Jump` (counted back-edge `CondBr` or plain `Br`), and the five phi builders (`Source::Phi`, `uniform_heap_index`, `Source::LanePhi`, `data_phis`, `pending_masks`) skip a repeated incoming block, so the single collapsed edge gets one phi entry. That matches DXIL's one-entry-per-predecessor rule, and the duplicated IR predecessor carries the same register state on both edges, so dropping the second entry loses nothing. Both tests (forward join in `valu_tests.rs`, counted back-edge in `lower.rs`) cover the two shapes. `main` has no equivalent handling. Minor: the title still says "keep distinct critical-edge blocks", but the change now collapses the branch; worth retitling. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠piyonchu on GitHub · 2026-11-25 04:00 UTC · original

Updated PR #1460 (2c0aeeb):

  • Rewrote the PR description to match the current diff (Lower::exit collapsing LaneExit::Branch with taken == not_taken to jump + predecessor deduplication in Source::Phi, Source::LanePhi, data_phis, and pending_masks, with edges: HashMap / Lower::edge_block references removed).
  • Rebased onto main (940d32f, adding view_marks: false in duplicate_branch_target_back_edge_produces_valid_dxil) so CI runs cleanly on the latest head.
  • Re-verified locally (cargo clippy, cargo test -p victory-shader --lib duplicate_branch_target 2/2 passed, cargo test -p xtask 6/6 passed).
**@⁠piyonchu** on GitHub · 2026-11-25 04:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#issuecomment-7152440875) Updated PR `#1460` (`2c0aeeb`): - Rewrote the PR description to match the current diff (`Lower::exit` collapsing `LaneExit::Branch` with `taken == not_taken` to `jump` + predecessor deduplication in `Source::Phi`, `Source::LanePhi`, `data_phis`, and `pending_masks`, with `edges: HashMap` / `Lower::edge_block` references removed). - Rebased onto `main` (`940d32f`, adding `view_marks: false` in `duplicate_branch_target_back_edge_produces_valid_dxil`) so CI runs cleanly on the latest head. - Re-verified locally (`cargo clippy`, `cargo test -p victory-shader --lib duplicate_branch_target` 2/2 passed, `cargo test -p xtask` 6/6 passed).
Author
Owner

@⁠piyonchu on GitHub · 2026-11-25 19:48 UTC · original

Rebased onto main (13f91ca, commit 954ef58), keeping predecessor-block deduplication across Source::Phi / uniform_heap_index / Source::LanePhi / data_phis / pending_masks alongside join_handles and no_mask_lane_on_edge, updated the PR description, and re-verified (cargo clippy, cargo test -p victory-shader --lib duplicate_branch_target 2/2 passed, cargo test -p xtask 6/6 passed).

**@⁠piyonchu** on GitHub · 2026-11-25 19:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#issuecomment-7172794806) Rebased onto `main` (`13f91ca`, commit `954ef58`), keeping predecessor-block deduplication across `Source::Phi` / `uniform_heap_index` / `Source::LanePhi` / `data_phis` / `pending_masks` alongside `join_handles` and `no_mask_lane_on_edge`, updated the PR description, and re-verified (`cargo clippy`, `cargo test -p victory-shader --lib duplicate_branch_target` 2/2 passed, `cargo test -p xtask` 6/6 passed).
Author
Owner

@⁠piyonchu on GitHub · 2026-11-26 02:45 UTC · original

Rebased onto latest main (63bcca4, head commit 67c7011) to resolve the EOF test conflicts in crates/victory-shader/src/emit/lower.rs and crates/victory-shader/src/emit/valu_tests.rs.

Verified locally after rebase:

  • cargo clippy -p victory-shader --all-targets -- -D warnings -A clippy::needless_range_loop -A deprecated
  • cargo test -p victory-shader --lib duplicate_branch_target (2 passed, 0 failed)
  • cargo test -p xtask (6 passed, 0 failed)
**@⁠piyonchu** on GitHub · 2026-11-26 02:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#issuecomment-7181097990) Rebased onto latest `main` (`63bcca4`, head commit `67c7011`) to resolve the EOF test conflicts in `crates/victory-shader/src/emit/lower.rs` and `crates/victory-shader/src/emit/valu_tests.rs`. Verified locally after rebase: - `cargo clippy -p victory-shader --all-targets -- -D warnings -A clippy::needless_range_loop -A deprecated` - `cargo test -p victory-shader --lib duplicate_branch_target` (2 passed, 0 failed) - `cargo test -p xtask` (6 passed, 0 failed)
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 18:42 UTC · original · approved these changes

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and were not run here; lowering/structure coverage only.

Edge-splitting blocks are now kept per predecessor occurrence (HashMap<(from,to), Vec<usize>>) so duplicate targets from one branch (e.g. s_cbranch with taken == not_taken) each get their own edge block; edge_block picks the occurrence index consistently in fill_phis and both descriptor-join paths. The valu test duplicate_branch_target_critical_edge_produces_valid_dxil covers the case (Windows-only). 96 passed on macOS.

**@⁠catminou4** on GitHub · 2026-11-24 18:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#pullrequestreview-6045235110) · **approved these changes** Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), `cargo test -p victory-shader --locked` → 96 passed, 0 failed. Execution/valu tests are `#[cfg(all(test, windows))]` and were not run here; lowering/structure coverage only. Edge-splitting blocks are now kept per predecessor occurrence (`HashMap<(from,to), Vec<usize>>`) so duplicate targets from one branch (e.g. `s_cbranch` with taken == not_taken) each get their own edge block; `edge_block` picks the occurrence index consistently in `fill_phis` and both descriptor-join paths. The valu test `duplicate_branch_target_critical_edge_produces_valid_dxil` covers the case (Windows-only). 96 passed on macOS.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 16:02 UTC · original · approved these changes

Collapsing LaneExit::Branch { taken, not_taken } if taken == not_taken through jump() cleanly preserves loop-counter checks on back edges and avoids duplicate counter entries on forward entries, and deduplicating repeated DXIL predecessors across the five PHI construction paths matches the current SSA invariant (same outgoing register values per (q, block)).

A few non-blocking notes worth considering for follow-up or before stacking with other lower.rs PRs:

  • emit/lower.rs reaches 524,223 B (65 B below the 524,288 B policy cap); moving duplicate_branch_target_back_edge_produces_valid_dxil (~2.8 KB) out of lower.rs into a test module would free up headroom so it does not trip the cap when combined with other shader PRs.
  • In valu_tests.rs, s_cbranch_scc0 +0 is already folded to Exit::Jump in ir.rs before structurization, so only the back-edge unit test exercises the new branch arm.
  • Making the equal-incoming-value invariant explicit at the dedup sites (or normalizing duplicate edges at one CFG boundary) would guard against future per-edge rewrites silently dropping a distinct input.

Local verification at 67c70115 (and merged cleanly onto origin/main 63bcca43): xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets (default and --all-features), emit::lower:: (34/34), emit::valu_tests on RTX 5090 and WARP (36/36), and duplicate_branch_target_* on RTX 5090 and WARP (2/2) all passed.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/1460#pullrequestreview-6077206199) · **approved these changes** Collapsing `LaneExit::Branch { taken, not_taken } if taken == not_taken` through `jump()` cleanly preserves loop-counter checks on back edges and avoids duplicate counter entries on forward entries, and deduplicating repeated DXIL predecessors across the five PHI construction paths matches the current SSA invariant (same outgoing register values per `(q, block)`). A few non-blocking notes worth considering for follow-up or before stacking with other `lower.rs` PRs: - `emit/lower.rs` reaches `524,223` B (`65` B below the `524,288` B policy cap); moving `duplicate_branch_target_back_edge_produces_valid_dxil` (~2.8 KB) out of `lower.rs` into a test module would free up headroom so it does not trip the cap when combined with other shader PRs. - In `valu_tests.rs`, `s_cbranch_scc0 +0` is already folded to `Exit::Jump` in `ir.rs` before structurization, so only the back-edge unit test exercises the new branch arm. - Making the equal-incoming-value invariant explicit at the dedup sites (or normalizing duplicate edges at one CFG boundary) would guard against future per-edge rewrites silently dropping a distinct input. Local verification at `67c70115` (and merged cleanly onto `origin/main` `63bcca43`): `xtask policy` (`191 files ok`), `cargo clippy -p victory-shader --all-targets` (default and `--all-features`), `emit::lower::` (`34/34`), `emit::valu_tests` on RTX 5090 and WARP (`36/36`), and `duplicate_branch_target_*` on RTX 5090 and WARP (`2/2`) all passed.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1460/head:github-bot/gh-1460'
git switch 'github-bot/gh-1460'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1460'
git switch 'github-bot/gh-1460'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1460'
git switch 'github-bot/gh-1460'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1460'
git switch 'main'
git merge --squash 'github-bot/gh-1460'
git switch 'main'
git merge --ff-only 'github-bot/gh-1460'
git switch 'main'
git merge 'github-bot/gh-1460'
git push origin 'main'
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!1460
No description provided.