shader(fold): mark EXEC_LO dirty at basic-block leaders #3097

Closed
github-bot wants to merge 1 commit from github-bot/gh-3097 into main AGit
Owner

@⁠RanoGames on GitHub · 2026-11-29 20:00 UTC · original
RanoGames:fix/fold-dirty-exec-at-leaders → main · +44 −0 · 2 files · 1 commits · closed without merging

What it changes

Fixes cross-basic-block EXEC_LO settling in crates/victory-shader/src/fold.rs:

  • Fold::dirty tracks whether EXEC_LO might differ from E.lo | E.hi. Previously, at basic-block leaders (leaders.contains(&inst.offset)), f.flush() returned early when f.run was empty without marking f.dirty = true.
  • If a layout-preceding block ended with dirty == false (such as a !live s_cbranch_execz/nz) while another predecessor jumped to the leader with EXEC_LO holding only one half (for example after a vector flush and a live-SCC branch), the leader inherited dirty == false and skipped s_or_b32 EXEC_LO, e, e + 1 before a subsequent !live s_cbranch_execz/nz.
  • Marks f.dirty = true; at every basic-block leader after f.flush().
  • Added GPU-executed regression test emit::folded_tests::exec_branch_at_join_leader_settles_exec_from_other_predecessors.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): N/A (Wave64→2xWave32 shader folding fix tested via folded_tests on host GPU/WARP)
  • Before: a join leader preceded in layout by dirty == false inherited dirty == false and branched on stale EXEC_LO.
  • After: f.dirty = true at each leader forces settle() / scalar_inst() to restore EXEC_LO = E.lo | E.hi.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): Wave64 folded to 2x Wave32 (emit::folded_tests)
  • cargo test passed for the crates it touches: yes (cargo test --locked -p victory-shader --lib -- emit::folded_tests: 18 passed, 0 failed; cargo xtask policy --staged passed)

Checked for overlap

  • I searched the open and closed pull requests for fold.rs, e_written, and s_cbranch_execnz.
**@⁠RanoGames** on GitHub · 2026-11-29 20:00 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3097) `RanoGames:fix/fold-dirty-exec-at-leaders` → `main` · +44 −0 · 2 files · 1 commits · closed without merging ## What it changes Fixes cross-basic-block `EXEC_LO` settling in `crates/victory-shader/src/fold.rs`: - `Fold::dirty` tracks whether `EXEC_LO` might differ from `E.lo | E.hi`. Previously, at basic-block leaders (`leaders.contains(&inst.offset)`), `f.flush()` returned early when `f.run` was empty without marking `f.dirty = true`. - If a layout-preceding block ended with `dirty == false` (such as a `!live` `s_cbranch_execz`/`nz`) while another predecessor jumped to the leader with `EXEC_LO` holding only one half (for example after a vector flush and a live-`SCC` branch), the leader inherited `dirty == false` and skipped `s_or_b32 EXEC_LO, e, e + 1` before a subsequent `!live` `s_cbranch_execz`/`nz`. - Marks `f.dirty = true;` at every basic-block leader after `f.flush()`. - Added GPU-executed regression test `emit::folded_tests::exec_branch_at_join_leader_settles_exec_from_other_predecessors`. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): N/A (Wave64→2xWave32 shader folding fix tested via `folded_tests` on host GPU/WARP) - Before: a join leader preceded in layout by `dirty == false` inherited `dirty == false` and branched on stale `EXEC_LO`. - After: `f.dirty = true` at each leader forces `settle()` / `scalar_inst()` to restore `EXEC_LO = E.lo | E.hi`. - For shader changes, the wave sizes tested (32 and/or 64 lanes): Wave64 folded to 2x Wave32 (`emit::folded_tests`) - `cargo test` passed for the crates it touches: yes (`cargo test --locked -p victory-shader --lib -- emit::folded_tests`: 18 passed, 0 failed; `cargo xtask policy --staged` passed) ## Checked for overlap - [x] I searched the open and closed pull requests for `fold.rs`, `e_written`, and `s_cbranch_execnz`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 20:06:37 +00:00
Author
Owner

@⁠dyshay on GitHub · 2026-11-29 20:46 UTC · original

Merged into our fork's integration branch for testing (dyshay/VIctory-NVIDIA, branch janus-prs). I read the full diff first: no network access, process spawning, binaries, build or dependency changes, or game data. It builds with --profile diag --features diagnostics, and the CPU tests of the touched crates pass. A run on an RTX 4090 is queued (Series S with ray tracing, DLSS Quality). Frame times will follow here.

**@⁠dyshay** on GitHub · 2026-11-29 20:46 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3097#issuecomment-7262352014) Merged into our fork's integration branch for testing ([dyshay/VIctory-NVIDIA](https://github.com/dyshay/VIctory-NVIDIA/tree/janus-prs), branch `janus-prs`). I read the full diff first: no network access, process spawning, binaries, build or dependency changes, or game data. It builds with `--profile diag --features diagnostics`, and the CPU tests of the touched crates pass. A run on an RTX 4090 is queued (Series S with ray tracing, DLSS Quality). Frame times will follow here.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-30 06:11 UTC · original

Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively in wave64, and ran the folded form for main and for this PR on an RTX 5080 and on that Radeon through D3D12.

Setup

  • main at 41adb551 with this PR's head (4401982) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory).
  • Kernels: hand-written GFX10 code, nothing from a title. One 64-thread group as one wave64; outputs v10.. stored per lane.
  • Hardware rows: the same machine code dispatched natively on the gfx1036 as one wave64.
  • Host runs: super::folded (the two-lanes-per-host-lane rewrite), dxil.dll validation, dispatched on the 5080 and on the Radeon (VICTORY_ADAPTER=radeon). super::emit was run too (32 and 64 host lanes); this PR does not change it.

Cases (the shape of your regression, with my own code)

  s_mov_b64 exec, s[30:31]        ; s30 = -1, s31 = 0 (hi0) or -1 (hi1)
  s_cmp_eq_u32 s40, 0             ; s40 from v_readfirstlane: SCC = 1 (scc1) or 0 (scc0)
  v_mov_b32 v41, 7
  s_cbranch_scc1 .Lt
  s_cbranch_execz .Ld
.Lt:
  s_cbranch_execnz .Lk
  s_branch .Ld
.Lk:
  v_mov_b32 v40, 42
.Ld:
  s_mov_b64 exec, s[36:37]        ; v10 = v40, v11 = v41
  • join_leader2/hi{0,1}_scc{0,1}: as above. Nothing reads SCC after .Lt, so the s_cbranch_execz before it is a !live one.
  • join_leader/hi{0,1}_scc{0,1}: the same with an s_cbranch_scc0 .Ld at .Lt and an s_cselect_b32 after the join, so SCC is live through every branch (your test's s_cbranch_scc0 at target also keeps SCC live at the s_cbranch_execz).

On the gfx1036, v11 = 7 in the lanes EXEC holds (0..31 for hi0, all 64 for hi1) and 0 elsewhere. v10 = 42 in the same lanes for all four join_leader2 cases and for join_leader/*_scc1; join_leader/*_scc0 leave through the s_cbranch_scc0 (v10 = 0).

Results (values that differ from the gfx1036, out of 64 lanes x 2 outputs)

case main, folded, 5080 this PR, folded, 5080 main, folded, Radeon this PR, folded, Radeon
join_leader2/hi0_scc1 32 (v10 = 0 in lanes 0..31, the hardware 42) exact 32 (same) exact
join_leader2/hi0_scc0 exact exact exact exact
join_leader2/hi1_scc1, hi1_scc0 exact exact exact exact
join_leader/* (4 cases, SCC live) exact exact exact exact

hi0_scc1 is the case your description names: the s_cbranch_scc1 arrives at .Lt with EXEC_LO = E.hi = 0 after the run's upper copy, the layout-preceding !live s_cbranch_execz left dirty == false, and the s_cbranch_execnz then reads the stale EXEC_LO and skips .Lk for the lower half. With hi1, E.hi is all ones and the stale value happens to be right; with scc0 the path goes through the s_cbranch_execz, which settles EXEC_LO.

Verdict: correct. The one case that shows the bug on main is exact with this PR on both cards, and no other case changed: the other 26 wave64 cases give the same values as main in the folded form and in super::emit (32 and 64 host lanes), and so do the 24 wave32 cases.

One remark on your test: exec_branch_at_join_leader_settles_exec_from_other_predecessors passes on main without the fix as well (its hunk applied to main alone, run on the 5080: "ran on the hardware device", ok). With the s_cbranch_scc0 done at target, SCC is live at the s_cbranch_execz before it, so that branch takes the live path and sets dirty = true. My join_leader2/hi0_scc1 is the same shape without that s_cbranch_scc0 (SCC from a v_readfirstlane_b32, so nothing is constant): it fails on main and passes with this PR, so I expect your test to fail on main once that one branch is dropped (not run in that form).

Outside this PR: super::emit writes v11 = 7 (and v10 = 42 where the branch is taken) into lanes 32..63 for the four hi0 cases, where EXEC's high dword is 0, on both cards and with 64 host lanes, main and this PR alike: with EXEC from two s_mov_b32 constants, its upper half follows the low dword.

(The upper-half value from EXEC built out of two s_mov_b32 constants looks like what #3402 fixes: "a lane mask written a dword at a time keeps its high dword".)

AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.

**@⁠Lyrnc** on GitHub · 2026-11-30 06:11 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3097#issuecomment-7270397023) Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively in wave64, and ran the folded form for main and for this PR on an RTX 5080 and on that Radeon through D3D12. **Setup** - `main` at 41adb551 with this PR's head (4401982) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory). - Kernels: hand-written GFX10 code, nothing from a title. One 64-thread group as one wave64; outputs v10.. stored per lane. - Hardware rows: the same machine code dispatched natively on the gfx1036 as one wave64. - Host runs: `super::folded` (the two-lanes-per-host-lane rewrite), `dxil.dll` validation, dispatched on the 5080 and on the Radeon (`VICTORY_ADAPTER=radeon`). `super::emit` was run too (32 and 64 host lanes); this PR does not change it. **Cases** (the shape of your regression, with my own code) ``` s_mov_b64 exec, s[30:31] ; s30 = -1, s31 = 0 (hi0) or -1 (hi1) s_cmp_eq_u32 s40, 0 ; s40 from v_readfirstlane: SCC = 1 (scc1) or 0 (scc0) v_mov_b32 v41, 7 s_cbranch_scc1 .Lt s_cbranch_execz .Ld .Lt: s_cbranch_execnz .Lk s_branch .Ld .Lk: v_mov_b32 v40, 42 .Ld: s_mov_b64 exec, s[36:37] ; v10 = v40, v11 = v41 ``` - `join_leader2/hi{0,1}_scc{0,1}`: as above. Nothing reads SCC after `.Lt`, so the `s_cbranch_execz` before it is a `!live` one. - `join_leader/hi{0,1}_scc{0,1}`: the same with an `s_cbranch_scc0 .Ld` at `.Lt` and an `s_cselect_b32` after the join, so SCC is live through every branch (your test's `s_cbranch_scc0` at `target` also keeps SCC live at the `s_cbranch_execz`). On the gfx1036, v11 = 7 in the lanes EXEC holds (0..31 for hi0, all 64 for hi1) and 0 elsewhere. v10 = 42 in the same lanes for all four `join_leader2` cases and for `join_leader/*_scc1`; `join_leader/*_scc0` leave through the `s_cbranch_scc0` (v10 = 0). **Results** (values that differ from the gfx1036, out of 64 lanes x 2 outputs) | case | main, folded, 5080 | this PR, folded, 5080 | main, folded, Radeon | this PR, folded, Radeon | |---|---|---|---|---| | `join_leader2/hi0_scc1` | 32 (v10 = 0 in lanes 0..31, the hardware 42) | exact | 32 (same) | exact | | `join_leader2/hi0_scc0` | exact | exact | exact | exact | | `join_leader2/hi1_scc1`, `hi1_scc0` | exact | exact | exact | exact | | `join_leader/*` (4 cases, SCC live) | exact | exact | exact | exact | `hi0_scc1` is the case your description names: the `s_cbranch_scc1` arrives at `.Lt` with EXEC_LO = E.hi = 0 after the run's upper copy, the layout-preceding `!live` `s_cbranch_execz` left `dirty == false`, and the `s_cbranch_execnz` then reads the stale EXEC_LO and skips `.Lk` for the lower half. With hi1, E.hi is all ones and the stale value happens to be right; with scc0 the path goes through the `s_cbranch_execz`, which settles EXEC_LO. **Verdict: correct.** The one case that shows the bug on main is exact with this PR on both cards, and no other case changed: the other 26 wave64 cases give the same values as main in the folded form and in `super::emit` (32 and 64 host lanes), and so do the 24 wave32 cases. One remark on your test: `exec_branch_at_join_leader_settles_exec_from_other_predecessors` passes on main without the fix as well (its hunk applied to main alone, run on the 5080: "ran on the hardware device", ok). With the `s_cbranch_scc0 done` at `target`, SCC is live at the `s_cbranch_execz` before it, so that branch takes the live path and sets `dirty = true`. My `join_leader2/hi0_scc1` is the same shape without that `s_cbranch_scc0` (SCC from a `v_readfirstlane_b32`, so nothing is constant): it fails on main and passes with this PR, so I expect your test to fail on main once that one branch is dropped (not run in that form). Outside this PR: `super::emit` writes v11 = 7 (and v10 = 42 where the branch is taken) into lanes 32..63 for the four hi0 cases, where EXEC's high dword is 0, on both cards and with 64 host lanes, main and this PR alike: with EXEC from two `s_mov_b32` constants, its upper half follows the low dword. (The upper-half value from EXEC built out of two `s_mov_b32` constants looks like what #3402 fixes: "a lane mask written a dword at a time keeps its high dword".) AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.
github-bot force-pushed github-bot/gh-3097 from 44019829b1 to 3d048536ed 2026-12-01 17:51:54 +00:00 Compare
github-bot closed this pull request 2026-12-01 18:59:07 +00:00

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/trustno1-coder--VIctory-NVIDIA!3097
No description provided.