Shaders: fold conditional branches to their fallthrough #1282

Manually merged
github merged 1 commit from gh-pr/1282/head into gh-pr/1282/base 2026-11-26 04:26:16 +00:00
Owner

@⁠EatCodeSleep2 on GitHub · 2026-11-24 17:07 UTC · original
EatCodeSleep2:fix/shader-branch-to-fallthrough-1242 → main · +31 −1 · 2 files · 1 commits · merged 2026-11-25 00:21 UTC

Summary

A conditional SOPP branch whose target is the following instruction contributes a single unconditional edge before SSA construction. This prevents duplicate predecessor entries when that edge enters a join, so emitted DXIL phi nodes match the control flow. The fold applies only to SOPP branches, since scalar calls use the same internal flow variant and write a return address.

Refs #1242, finding VIC-BT-007.

Verification

  • The new synthetic shader fails DXIL validation on main with PHI node entries do not match predecessors.
  • With the fix it validates and reads back both expected join values on native D3D12 and WARP.
  • Shader library suite had 120 passed, 1 ignored, and the existing adapter-dependent NaN median failure tracked in #726 and #745.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings passed.

AI-assisted. No game files used.

**@⁠EatCodeSleep2** on GitHub · 2026-11-24 17:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1282) `EatCodeSleep2:fix/shader-branch-to-fallthrough-1242` → `main` · +31 −1 · 2 files · 1 commits · merged 2026-11-25 00:21 UTC ## Summary A conditional SOPP branch whose target is the following instruction contributes a single unconditional edge before SSA construction. This prevents duplicate predecessor entries when that edge enters a join, so emitted DXIL phi nodes match the control flow. The fold applies only to SOPP branches, since scalar calls use the same internal flow variant and write a return address. Refs #1242, finding VIC-BT-007. ## Verification - The new synthetic shader fails DXIL validation on main with `PHI node entries do not match predecessors`. - With the fix it validates and reads back both expected join values on native D3D12 and WARP. - Shader library suite had 120 passed, 1 ignored, and the existing adapter-dependent NaN median failure tracked in #726 and #745. - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` passed. AI-assisted. No game files used.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:26:14 +00:00
github manually merged commit 1ed26e50d8 into gh-pr/1282/base 2026-11-26 04:26:16 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 17:25 UTC · original

Independent verification of \1ed26e5\ on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200) / Rust 1.98.1 MSVC:

  • Merge check vs \main\ (\ fe310e): clean
  • \cargo test -p victory-shader --lib valu_tests\ (real RX 9060 XT GPU execution): 14 passed, 0 failed (including \_branch_to_fallthrough_keeps_both_values_at_a_join)
  • \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and --all-features): clean
  • \cargo xtask policy: 154 files ok

Code audit looks clean: gating the fold on \machine.encoding == gfx10::Encoding::Sopp\ excludes \s_swappc_b64\ / calls with register side effects while cleanly collapsing redundant fallthrough conditional branches into a single \Exit::Jump\ edge.

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

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 17:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/1282#issuecomment-7139178299) Independent verification of \1ed26e5\ on **Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200\) / Rust 1.98.1 MSVC**: - Merge check vs \main\ (\ fe310e\): clean - \cargo test -p victory-shader --lib valu_tests\ (real RX 9060 XT GPU execution): **14 passed, 0 failed** (including \_branch_to_fallthrough_keeps_both_values_at_a_join\) - \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and \--all-features\): clean - \cargo xtask policy\: 154 files ok Code audit looks clean: gating the fold on \machine.encoding == gfx10::Encoding::Sopp\ excludes \s_swappc_b64\ / calls with register side effects while cleanly collapsing redundant fallthrough conditional branches into a single \Exit::Jump\ edge. *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 17:45 UTC · original

Reviewed at head 1ed26e5 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The fold is semantically sound. In ir::build (ir.rs:176), a SOPP conditional branch whose target is the next instruction led to the same block either way, so it is a no-op; the PR turns the block's exit into Exit::Jump(fallthrough) and drops the instruction. I verified the guard cannot misfire: Flow::Branch is produced only by s_cbranch_* (Encoding::Sopp, gfx10/mod.rs:144) and s_call_b64 (Encoding::Sopk, line 145) — the encoding check excludes the call, whose return-address write is modelled in fields.rs:216. And all six GFX10 s_cbranch_* opcodes are read-only in effects() (ir.rs Sopp arm: they push Scc/Vcc/Exec into reads only), so dropping the instruction loses no register effect.

2. What the fold fixes: previously such a branch created a Exit::Branch { taken, not_taken } with both targets the same block plus a redundant Inst, i.e. a duplicate self-edge at the join; the fold leaves a single jump edge, which is what the phi construction expects.

3. Hardware evidence on this machine. The new a_branch_to_fallthrough_keeps_both_values_at_a_join runs on the GPU through run_cases: with s_cbranch_scc1 -> +2 around an s2 = 0 / s2 = 1 pair and a trailing s_cbranch_scc1 -> +0 (the folded one), both inputs [7,7] and [7,5] produce the right v0 = (s0 == s1): 1 passed / 0 failed. Full cargo test -p victory-shader --lib: 121 passed / 0 failed / 1 ignored.

4. Checks: cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds; rustfmt --edition 2024 --check — ir.rs clean, one hunk in the new test code at valu_tests.rs:219 (main's file is clean), cosmetic only since CI has no fmt job.

5. One behavioral note, not a defect: the guard fires before effects(), so the four debug-only s_cbranch_cdbg* opcodes (which effects() would refuse as unsupported) are now silently dropped when they target fallthrough instead of being refused. They are condition reads with no register effects, so dropping a branch-to-fallthrough is still a no-op; the only change is that such a shader now translates instead of refusing. Fine for debug branches, just noting the acceptance widened slightly.

Nothing found that would block this.

**@⁠adamdotpy** on GitHub · 2026-11-24 17:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/1282#issuecomment-7139662140) Reviewed at head `1ed26e5` (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked: **1. The fold is semantically sound.** In `ir::build` (`ir.rs:176`), a SOPP conditional branch whose target is the next instruction led to the same block either way, so it is a no-op; the PR turns the block's exit into `Exit::Jump(fallthrough)` and drops the instruction. I verified the guard cannot misfire: `Flow::Branch` is produced only by `s_cbranch_*` (Encoding::Sopp, `gfx10/mod.rs:144`) and `s_call_b64` (Encoding::Sopk, line 145) — the encoding check excludes the call, whose return-address write is modelled in `fields.rs:216`. And all six GFX10 `s_cbranch_*` opcodes are read-only in `effects()` (`ir.rs` Sopp arm: they push `Scc`/`Vcc`/`Exec` into *reads* only), so dropping the instruction loses no register effect. **2. What the fold fixes:** previously such a branch created a `Exit::Branch { taken, not_taken }` with both targets the same block plus a redundant `Inst`, i.e. a duplicate self-edge at the join; the fold leaves a single jump edge, which is what the phi construction expects. **3. Hardware evidence on this machine.** The new `a_branch_to_fallthrough_keeps_both_values_at_a_join` runs on the GPU through `run_cases`: with `s_cbranch_scc1 -> +2` around an `s2 = 0` / `s2 = 1` pair and a trailing `s_cbranch_scc1 -> +0` (the folded one), both inputs `[7,7]` and `[7,5]` produce the right `v0 = (s0 == s1)`: 1 passed / 0 failed. Full `cargo test -p victory-shader --lib`: 121 passed / 0 failed / 1 ignored. **4. Checks:** `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` clean; `cargo build --release -p victory` builds; `rustfmt --edition 2024 --check` — `ir.rs` clean, one hunk in the new test code at `valu_tests.rs:219` (main's file is clean), cosmetic only since CI has no fmt job. **5. One behavioral note, not a defect:** the guard fires before `effects()`, so the four debug-only `s_cbranch_cdbg*` opcodes (which `effects()` would refuse as unsupported) are now silently dropped when they target fallthrough instead of being refused. They are condition reads with no register effects, so dropping a branch-to-fallthrough is still a no-op; the only change is that such a shader now translates instead of refusing. Fine for debug branches, just noting the acceptance widened slightly. Nothing found that would block this.
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!1282
No description provided.