Shaders: fold conditional branches to their fallthrough #1282

Merged
EatCodeSleep2 merged 1 commit from upstream-pr/1282 into main 2026-11-25 00:21:56 +00:00
EatCodeSleep2 commented 2026-11-24 17:07:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by EatCodeSleep2 on 2026-11-24T17:07:40Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587237786 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1282). Originally posted by [EatCodeSleep2](https://github.com/EatCodeSleep2) on 2026-11-24T17:07:40Z. --- ## 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.
geiyiliyabumo commented 2026-11-24 18:05:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T17:25:40Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139178299 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1282#issuecomment-7139178299). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T17:25:40Z. --- 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.*
adamdotpy commented 2026-11-24 18:05:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T17:45:32Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139662140 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1282#issuecomment-7139662140). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T17:45:32Z. --- 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.
upstream-sync closed this pull request 2026-11-25 01:06:08 +00:00
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!1282
No description provided.