Shaders coalesce conditional branches to their fallthrough #1359

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

@⁠BaggyALPHA on GitHub · 2026-11-24 17:35 UTC · original
BaggyALPHA:fix/ssa-duplicate-branch-edge → main · +41 −4 · 1 files · 1 commits · closed without merging

Addresses item 7 (VIC-BT-007) in #1242. When a GFX10 conditional branch targets its own fallthrough, the IR now treats it as one jump before linking predecessors and constructing SSA. A join still preserves the distinct incoming values from other blocks.

The new synthetic regression failed before the fix and now checks the branch, the two distinct predecessors, and their incoming values. In a separate synthetic compute probe, Microsoft’s DXIL validator rejected upstream output because PHI entries did not match the CFG predecessors. It accepted the same input with this change. The shader crate passed 121 tests with one ignored on Windows WARP. Clippy with all targets and features and the asset-policy hook passed.

Integration with #1282: the current PR heads merge cleanly. The combined Windows shader library suite passed 122 tests with one ignored, including the IR single-edge regression here and the GPU readback regression in #1282. The SOPP fold in #1282 removes the redundant instruction, while this PR coalesces identical successors in the IR.

AI-assisted (Codex).

**@⁠BaggyALPHA** on GitHub · 2026-11-24 17:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1359) `BaggyALPHA:fix/ssa-duplicate-branch-edge` → `main` · +41 −4 · 1 files · 1 commits · closed without merging Addresses item 7 (VIC-BT-007) in #1242. When a GFX10 conditional branch targets its own fallthrough, the IR now treats it as one jump before linking predecessors and constructing SSA. A join still preserves the distinct incoming values from other blocks. The new synthetic regression failed before the fix and now checks the branch, the two distinct predecessors, and their incoming values. In a separate synthetic compute probe, Microsoft’s DXIL validator rejected upstream output because PHI entries did not match the CFG predecessors. It accepted the same input with this change. The shader crate passed 121 tests with one ignored on Windows WARP. Clippy with all targets and features and the asset-policy hook passed. Integration with #1282: the current PR heads merge cleanly. The combined Windows shader library suite passed 122 tests with one ignored, including the IR single-edge regression here and the GPU readback regression in #1282. The SOPP fold in #1282 removes the redundant instruction, while this PR coalesces identical successors in the IR. AI-assisted (Codex).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:28:42 +00:00
github-bot closed this pull request 2026-11-26 04:28:42 +00:00
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 20:50 UTC · original

Reviewed head f2782f3 locally (isolated worktree, fresh CARGO_TARGET_DIR).

What it does: in ir.rs build(), a Flow::Branch(t) whose taken and not-taken targets are the same block is lowered to Exit::Jump(t) instead of Exit::Branch — one SSA edge at the join instead of two, while keeping the branch Inst itself, so it stays encoding-agnostic (works for both SOPP cbranch and any other branch encoding).

Verified locally:

  • cargo test -p victory-shader --lib ir:: — 7 passed / 0 failed, including the new a_conditional_branch_to_its_fallthrough_has_one_edge
  • cargo test -p victory-shader --lib — 121 passed / 0 failed / 1 ignored
  • cargo clippy --all-targets --all-features --locked -- -D warnings — clean
  • cargo build --release -p victory — ok
  • rustfmt --edition 2024 --check crates/victory-shader/src/ir.rs — 0 hunks (same as main)

Findings, ranked:

  1. Sound. The fold is semantics-preserving: taken == not_taken means the branch outcome cannot affect control flow, so a single edge to the join is equivalent. Keeping the Inst means the backend still emits (and can later fold) the branch — no information lost vs. #1282, which instead drops the SOPP cbranch Inst at the encoding layer. The two are complementary, not redundant: #1359 fixes the IR-level edge duplication for every branch form, #1282 removes a dead instruction.
  2. Merge note: both touch ir.rs around the branch handling, so expect a textual conflict with #1282 (and #1359's own sibling branch-to-fallthrough work); semantically they compose in either order — worth a one-line note on both PRs so the second merger rebases cleanly.
  3. No issues found. The guard taken == not_taken is the exact condition under which the branch is a no-op, and the new test covers the single-edge property directly.

LGTM from my side.

**@⁠adamdotpy** on GitHub · 2026-11-24 20:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/1359#issuecomment-7144121599) Reviewed head f2782f3 locally (isolated worktree, fresh CARGO_TARGET_DIR). **What it does:** in `ir.rs` `build()`, a `Flow::Branch(t)` whose taken and not-taken targets are the same block is lowered to `Exit::Jump(t)` instead of `Exit::Branch` — one SSA edge at the join instead of two, while keeping the branch Inst itself, so it stays encoding-agnostic (works for both SOPP cbranch and any other branch encoding). **Verified locally:** - `cargo test -p victory-shader --lib ir::` — 7 passed / 0 failed, including the new `a_conditional_branch_to_its_fallthrough_has_one_edge` - `cargo test -p victory-shader --lib` — 121 passed / 0 failed / 1 ignored - `cargo clippy --all-targets --all-features --locked -- -D warnings` — clean - `cargo build --release -p victory` — ok - `rustfmt --edition 2024 --check crates/victory-shader/src/ir.rs` — 0 hunks (same as main) **Findings, ranked:** 1. Sound. The fold is semantics-preserving: taken == not_taken means the branch outcome cannot affect control flow, so a single edge to the join is equivalent. Keeping the Inst means the backend still emits (and can later fold) the branch — no information lost vs. #1282, which instead drops the SOPP `cbranch` Inst at the encoding layer. The two are complementary, not redundant: #1359 fixes the IR-level edge duplication for every branch form, #1282 removes a dead instruction. 2. Merge note: both touch `ir.rs` around the branch handling, so expect a textual conflict with #1282 (and #1359's own sibling branch-to-fallthrough work); semantically they compose in either order — worth a one-line note on both PRs so the second merger rebases cleanly. 3. No issues found. The guard `taken == not_taken` is the exact condition under which the branch is a no-op, and the new test covers the single-edge property directly. LGTM from my side.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 21:26 UTC · original

@⁠mercuryVM thanks for the approval and especially the validator detail — the duplicate-predecessor phi (two incoming entries from one block, rejected by the DXIL validator) is exactly the failure mode the Exit::Jump fold removes; good to have it confirmed on RTX 5070 with the full workspace suite (638 passed).

**@⁠adamdotpy** on GitHub · 2026-11-24 21:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1359#issuecomment-7144943196) @⁠mercuryVM thanks for the approval and especially the validator detail — the duplicate-predecessor phi (two incoming entries from one block, rejected by the DXIL validator) is exactly the failure mode the `Exit::Jump` fold removes; good to have it confirmed on RTX 5070 with the full workspace suite (638 passed).
Author
Owner

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

Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1282. Same fix as #1282 (a conditional branch to its own fallthrough gets one edge), done by coalescing in the exit match but leaving the branch instruction in a block whose exit is a Jump; #1282 drops the instruction and has the end-to-end GPU test.

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:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1359#issuecomment-7148724167) Automated triage for the maintainers. Closing because another open pull request covers the same change. See #1282. Same fix as #1282 (a conditional branch to its own fallthrough gets one edge), done by coalescing in the exit match but leaving the branch instruction in a block whose exit is a Jump; #1282 drops the instruction and has the end-to-end GPU test. 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.
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 01:48 UTC · original

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

Result: no count change against main.

Offline. I merged head f2782f3 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 121, 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-25 01:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/1359#issuecomment-7149981240) ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head f2782f3 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 121, 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).

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!1359
No description provided.