Keep folded Wave64 branches converged across EXEC restores #2265

Open
FIocker wants to merge 1 commit from upstream-pr/2265 into main
FIocker commented 2026-11-29 11:11:19 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by FIocker on 2026-11-29T11:11:19Z.


A folded Wave64 shader can branch on EXEC and later restore lanes from a saved scalar mask. Treat scalar writes that can reactivate EXEC lanes as requiring wave-wide branch control. The two save forms that only intersect the current mask still qualify for the existing path.

The synthetic regression branches on EXEC and restores it inside the branch. The offline regression passed with this patch and failed on 829188e9. A separate GPU fixture covers masks with 0, 1, 33, and 64 active guest lanes. Gameplay validation on this branch is pending.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5550134450 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2265). Originally posted by [FIocker](https://github.com/FIocker) on 2026-11-29T11:11:19Z. --- A folded Wave64 shader can branch on EXEC and later restore lanes from a saved scalar mask. Treat scalar writes that can reactivate EXEC lanes as requiring wave-wide branch control. The two save forms that only intersect the current mask still qualify for the existing path. The synthetic regression branches on EXEC and restores it inside the branch. The offline regression passed with this patch and failed on `829188e9`. A separate GPU fixture covers masks with 0, 1, 33, and 64 active guest lanes. Gameplay validation on this branch is pending.
Lyrnc commented 2026-11-30 06:11:18 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Lyrnc on 2026-11-30T06:11:18Z.


Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively in wave64, and ran the translator's output 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 (7230502) merged locally, no conflict. The victory-shader test binary was built from that merged tree, one tree and one target directory per PR.
  • Kernels: hand-written GFX10 code, nothing from a title. One 64-thread group; per-lane inputs come from tables on the thread id; outputs are v10.. stored per lane.
  • Hardware rows: the same machine code dispatched natively on the gfx1036 as one wave64 (MODE: IEEE on, f32 denormals kept).
  • Host runs: super::folded (the two-lanes-per-host-lane rewrite) and super::emit with a 32-lane host forced, each through dxil.dll validation, dispatched on the 5080 and on the Radeon (VICTORY_ADAPTER=radeon). Also super::emit with 64 host lanes on the Radeon.

Cases

  • restore_exec/<k>: s_mov_b64 s[42:43], exec, v_cmpx_gt_u32 k, v0, s_cbranch_execz over a block that restores the saved mask (s_mov_b64 exec, s[42:43]) and writes v40 = 200, then the join restores EXEC again. On the gfx1036 every one of the 64 lanes holds 200 for k = 1, 31, 32, 33, 64 (the restore inside the branch brings the lanes back) and 100 for k = 0 (the branch is skipped).
  • restore_exec_or/<k>: the same with s_or_b64 exec, exec, s[42:43] in the branch, then exec_lo = 0 / exec_lo = s42, exec_hi = 0 around one more write.
  • waterfall/<mixed|upper_only|sparse>: a v_readfirstlane_b32 waterfall (v_cmp_eq_u32, s_and_saveexec_b64, s_xor_b64 exec, s[42:43], exec, s_cbranch_execnz) with a scalar counter that is read after the loop. On the hardware the counter is the same in every lane (5, 3, 5 rounds).
  • Also run: join_leader*/* (EXEC set to one half, branches on SCC and EXEC at a join), counter_loop*/* (a loop with an if inside, exiting on a v_readfirstlane_b32 counter), permute_cmpx, saveexec_forms.

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

case main, folded, 5080 this PR, folded, 5080 main, folded, Radeon this PR, folded, Radeon
restore_exec/0 exact exact exact exact
restore_exec/1 62 (v10, lanes 1-31, 33-63 keep 100) exact 62 exact
restore_exec/31 2 (v10, lanes 31 and 63) exact 2 exact
restore_exec/32, /33, /64 exact exact exact exact
restore_exec_or/1 124 (v10, v11) exact 124 exact
restore_exec_or/40 exact exact exact exact
waterfall/mixed 32 (v11: the counter reads 4 in the even lanes) exact 32 exact
waterfall/sparse 40 (v11) exact 40 exact
waterfall/upper_only 44 (v11) exact 44 exact
join_leader/* (4 cases) exact exact exact exact
  • With super::emit and a 32-lane host, restore_exec_or/1 goes from 124 differing values to exact on both cards (it now ends in the folded form, PSV0 wave 32). Every other case gives the same values as main there, and with 64 host lanes on the Radeon nothing changes.
  • No case changed for the worse: the 15 other wave64 cases give the same values as main in every host form, and so do the 26 wave32 cases on both cards. (join_leader2/hi0_scc1, the case #3097 fixes, still differs in the folded form here.)

What still differs (not this PR's change)

  • super::emit keeps the per-lane form for restore_exec/1..33: the lanes outside the v_cmpx mask keep 100 (63, 33, 32, 31 values), on both cards and with 64 host lanes too. The waterfall cases differ there as well.

A question on the scope

widens_exec now holds for any SOP instruction that writes EXEC except s_and_saveexec_b64 and s_andn1_saveexec_b64. That includes s_and_b64 exec, exec, vcc, s_andn2_b64 exec, exec, s[..] and the s_or_b64 exec, exec, s[..] / s_mov_b64 exec, s[..] at the end of every divergent if, so nearly every folded program with a divergent branch now takes the wave-wide branches (before, only the forms with an inline -1 did). The exempted two are the right ones on the hardware (saveexec table below). Do you have a count of the folded programs whose form changes, and their card time? The PR's own numbers cover the synthetic shaders only.

saveexec_forms on the gfx1036 (wave64): EXEC = lanes 0..31, S0 = lanes 16..47, then each save form; the new EXEC:

form EXEC lo EXEC hi lanes outside the old EXEC
s_and_saveexec_b64 0xFFFF0000 0x00000000 none
s_andn1_saveexec_b64 0x0000FFFF 0x00000000 none
s_andn2_saveexec_b64 0x00000000 0x0000FFFF 32..47
s_or_saveexec_b64 0xFFFFFFFF 0x0000FFFF 32..47
s_orn1_saveexec_b64 0xFFFFFFFF 0xFFFF0000 48..63
s_orn2_saveexec_b64 0xFFFF0000 0xFFFFFFFF 32..63
s_xor_saveexec_b64 0x0000FFFF 0x0000FFFF 32..47
s_nand_saveexec_b64 0x0000FFFF 0xFFFFFFFF 32..63

So s_andn1_saveexec_b64 is ~S0 & EXEC and s_andn2_saveexec_b64 is S0 & ~EXEC on this hardware: the two the PR exempts are the only ones that stay inside the old mask.

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

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7270392428 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2265#issuecomment-7270392428). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-30T06:11:18Z. --- Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively in wave64, and ran the translator's output 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 (7230502) merged locally, no conflict. The victory-shader test binary was built from that merged tree, one tree and one target directory per PR. - Kernels: hand-written GFX10 code, nothing from a title. One 64-thread group; per-lane inputs come from tables on the thread id; outputs are v10.. stored per lane. - Hardware rows: the same machine code dispatched natively on the gfx1036 as one wave64 (MODE: IEEE on, f32 denormals kept). - Host runs: `super::folded` (the two-lanes-per-host-lane rewrite) and `super::emit` with a 32-lane host forced, each through `dxil.dll` validation, dispatched on the 5080 and on the Radeon (`VICTORY_ADAPTER=radeon`). Also `super::emit` with 64 host lanes on the Radeon. **Cases** - `restore_exec/<k>`: `s_mov_b64 s[42:43], exec`, `v_cmpx_gt_u32 k, v0`, `s_cbranch_execz` over a block that restores the saved mask (`s_mov_b64 exec, s[42:43]`) and writes v40 = 200, then the join restores EXEC again. On the gfx1036 every one of the 64 lanes holds 200 for k = 1, 31, 32, 33, 64 (the restore inside the branch brings the lanes back) and 100 for k = 0 (the branch is skipped). - `restore_exec_or/<k>`: the same with `s_or_b64 exec, exec, s[42:43]` in the branch, then `exec_lo = 0` / `exec_lo = s42`, `exec_hi = 0` around one more write. - `waterfall/<mixed|upper_only|sparse>`: a `v_readfirstlane_b32` waterfall (`v_cmp_eq_u32`, `s_and_saveexec_b64`, `s_xor_b64 exec, s[42:43], exec`, `s_cbranch_execnz`) with a scalar counter that is read after the loop. On the hardware the counter is the same in every lane (5, 3, 5 rounds). - Also run: `join_leader*/*` (EXEC set to one half, branches on SCC and EXEC at a join), `counter_loop*/*` (a loop with an if inside, exiting on a `v_readfirstlane_b32` counter), `permute_cmpx`, `saveexec_forms`. **Results** (values that differ from the gfx1036, out of 64 lanes x outputs) | case | main, folded, 5080 | this PR, folded, 5080 | main, folded, Radeon | this PR, folded, Radeon | |---|---|---|---|---| | `restore_exec/0` | exact | exact | exact | exact | | `restore_exec/1` | 62 (v10, lanes 1-31, 33-63 keep 100) | exact | 62 | exact | | `restore_exec/31` | 2 (v10, lanes 31 and 63) | exact | 2 | exact | | `restore_exec/32`, `/33`, `/64` | exact | exact | exact | exact | | `restore_exec_or/1` | 124 (v10, v11) | exact | 124 | exact | | `restore_exec_or/40` | exact | exact | exact | exact | | `waterfall/mixed` | 32 (v11: the counter reads 4 in the even lanes) | exact | 32 | exact | | `waterfall/sparse` | 40 (v11) | exact | 40 | exact | | `waterfall/upper_only` | 44 (v11) | exact | 44 | exact | | `join_leader/*` (4 cases) | exact | exact | exact | exact | - With `super::emit` and a 32-lane host, `restore_exec_or/1` goes from 124 differing values to exact on both cards (it now ends in the folded form, PSV0 wave 32). Every other case gives the same values as main there, and with 64 host lanes on the Radeon nothing changes. - No case changed for the worse: the 15 other wave64 cases give the same values as main in every host form, and so do the 26 wave32 cases on both cards. (`join_leader2/hi0_scc1`, the case #3097 fixes, still differs in the folded form here.) **What still differs (not this PR's change)** - `super::emit` keeps the per-lane form for `restore_exec/1..33`: the lanes outside the `v_cmpx` mask keep 100 (63, 33, 32, 31 values), on both cards and with 64 host lanes too. The waterfall cases differ there as well. **A question on the scope** `widens_exec` now holds for any SOP instruction that writes EXEC except `s_and_saveexec_b64` and `s_andn1_saveexec_b64`. That includes `s_and_b64 exec, exec, vcc`, `s_andn2_b64 exec, exec, s[..]` and the `s_or_b64 exec, exec, s[..]` / `s_mov_b64 exec, s[..]` at the end of every divergent if, so nearly every folded program with a divergent branch now takes the wave-wide branches (before, only the forms with an inline -1 did). The exempted two are the right ones on the hardware (saveexec table below). Do you have a count of the folded programs whose form changes, and their card time? The PR's own numbers cover the synthetic shaders only. `saveexec_forms` on the gfx1036 (wave64): EXEC = lanes 0..31, S0 = lanes 16..47, then each save form; the new EXEC: | form | EXEC lo | EXEC hi | lanes outside the old EXEC | |---|---|---|---| | `s_and_saveexec_b64` | 0xFFFF0000 | 0x00000000 | none | | `s_andn1_saveexec_b64` | 0x0000FFFF | 0x00000000 | none | | `s_andn2_saveexec_b64` | 0x00000000 | 0x0000FFFF | 32..47 | | `s_or_saveexec_b64` | 0xFFFFFFFF | 0x0000FFFF | 32..47 | | `s_orn1_saveexec_b64` | 0xFFFFFFFF | 0xFFFF0000 | 48..63 | | `s_orn2_saveexec_b64` | 0xFFFF0000 | 0xFFFFFFFF | 32..63 | | `s_xor_saveexec_b64` | 0x0000FFFF | 0x0000FFFF | 32..47 | | `s_nand_saveexec_b64` | 0x0000FFFF | 0xFFFFFFFF | 32..63 | So `s_andn1_saveexec_b64` is ~S0 & EXEC and `s_andn2_saveexec_b64` is S0 & ~EXEC on this hardware: the two the PR exempts are the only ones that stay inside the old mask. AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2265:upstream-pr/2265'
git switch 'upstream-pr/2265'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2265'
git switch 'upstream-pr/2265'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2265'
git switch 'upstream-pr/2265'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2265'
git switch 'main'
git merge --squash 'upstream-pr/2265'
git switch 'main'
git merge --ff-only 'upstream-pr/2265'
git switch 'main'
git merge 'upstream-pr/2265'
git push origin 'main'
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
trustno1-coder/VIctory-NVIDIA!2265
No description provided.