Shaders: a scalar a per-lane branch writes is lost after the join in the lanes that skipped it (native wave64 on AMD; 73 of Lyrnc's 151 cases) #3562

Open
opened 2026-11-27 17:55:27 +00:00 by github-bot · 1 comment
Owner

@⁠ColorMeBoom on GitHub · 2026-11-27 17:54 UTC · original

On main, a scalar a per-lane branch writes keeps its old value after the join in the lanes that skipped the branch. On the console a scalar register is one value per wave, so every lane reads what the branch wrote. main translates scalar code per lane, and each lane keeps the scalar from its own path. This breaks the console compiler's usual pattern: something computed inside a lane branch (a v_readfirstlane, a v_readlane, a whole-wave reduction's result read into an SGPR) and then used by the whole wave after the join.

This is the per-lane form main uses for wave64 compute in native WaveSize(64), which is what runs on AMD hosts. #2633's halves inherit it on 32-lane hosts.

Evidence

@⁠Lyrnc's 151 hand-written GFX10 wave64 cases (gist; no game data), each compared lane by lane with the same code run natively on a gfx1036 (RDNA2). An independent Python model agrees with that hardware on 151/151.

  • main 63bcca4, native WaveSize(64), RX 5700 XT (RDNA1): 59 exact, 92 differ. Per case kind, that's the same count Lyrnc measured with main on the gfx1036 iGPU (#2633), so the differences don't depend on the GPU.
  • 73 of the 92 are this bug: the only wrong output, or one of them, is the post-join register v11, which every lane writes from the scalar after s_or_b64 exec, exec, s[34:35]:
case kind cases v11 wrong wrong v11 lanes
red_branch (whole-wave reduction in a branch) 40 32 1,624
readlane_branch (v_readlane in a branch) 20 16 832
firstlane_branch (v_readfirstlane in a branch) 10 9 457
red_earlyout (reduction after v_cmpx) 6 5 197
red_loop (reduction in a loop lanes leave) 3 3 144
writelane_branch 18 8 8

The other 19 are in-branch or in-loop values (vote_loop*, break_loop, red_nested's v12), which aren't covered here.

Smallest case: firstlane_branch/lo32

s_mov_b32 s38, 0x5555
s_and_saveexec_b64 s[34:35], vcc      ; vcc = lanes 0-31
s_cbranch_execz .Lskip
v_readfirstlane_b32 s38, v47          ; v47 = 3 * lane + 5: s38 = 5 (lane 0)
v_mov_b32 v10, s38
.Lskip:
s_or_b64 exec, exec, s[34:35]
v_mov_b32 v11, s38                    ; every lane
lane console (gfx1036) v11 main, RX 5700 XT
0-31 0x5 0x5
32-63 0x5 0x5555

v10 (inside the branch) matches in every lane. The full program as words (one 64-thread group, raw UAV in table dword 0 at s0, outputs v10..v25 stored register-major at 64 * register + lane):

words
0xbe810380, 0xf4080100, 0xfa000000, 0xbf8cc07f, 0x7e140280, 0x7e160280, 0x7e180280, 0x7e1a0280, 0x7e1c0280, 0x7e1e0280, 0x7e200280, 0x7e220280, 0x7e240280, 0x7e260280, 0x7e280280, 0x7e2a0280, 0x7e2c0280, 0x7e2e0280, 0x7e300280, 0x7e320280, 0x381400ff, 0xdead0000, 0x381600ff, 0xdead0000, 0x381800ff, 0xdead0000, 0x381a00ff, 0xdead0000, 0x165e0083, 0x4a5e5e85, 0xbe9e03c1, 0xbe9f0380, 0xd700002c, 0x00003d00, 0x36565881, 0x7d8a5680, 0xbea603ff, 0x00005555, 0xbea2246a, 0xbf880002, 0x7e4c052f, 0x7e140226, 0x88fe227e, 0x7e160226, 0x34020082, 0xe0701000, 0x80010a01, 0xe0701100, 0x80010b01, 0xe0701200, 0x80010c01, 0xe0701300, 0x80010d01, 0xe0701400, 0x80010e01, 0xe0701500, 0x80010f01, 0xe0701600, 0x80011001, 0xe0701700, 0x80011101, 0xe0701800, 0x80011201, 0xe0701900, 0x80011301, 0xe0701a00, 0x80011401, 0xe0701b00, 0x80011501, 0xe0701c00, 0x80011601, 0xe0701d00, 0x80011701, 0xe0701e00, 0x80011801, 0xe0701f00, 0x80011901, 0xbf810000

Notes

  • The value is the wave's on the console, whichever lanes compute it. So the fix is about where a scalar's definition reaches, not about the instruction. A scalar written under a lane branch needs to reach the lanes that skipped it at the join.
  • #2920's console mode (branches decided for the wave, vector instructions guarded by EXEC) is a form where scalars stay wave-shared, but it only applies to programs with group barriers.
  • On 32-lane hosts (main refuses WaveSize(64) there) this shows up only through #2633/#439-style halves, which inherit it: firstlane_branch is 508 wrong words there too, matching @⁠pickles976's RTX 4060 count.

AI use: Claude Code (Claude Opus 5.5).

**@⁠ColorMeBoom** on GitHub · 2026-11-27 17:54 UTC · [original](https://github.com/Marvisak/VIctory/issues/3562) **On `main`, a scalar a per-lane branch writes keeps its old value after the join in the lanes that skipped the branch.** On the console a scalar register is one value per wave, so every lane reads what the branch wrote. `main` translates scalar code per lane, and each lane keeps the scalar from its own path. This breaks the console compiler's usual pattern: something computed inside a lane branch (a `v_readfirstlane`, a `v_readlane`, a whole-wave reduction's result read into an SGPR) and then used by the whole wave after the join. This is the per-lane form `main` uses for wave64 compute in native `WaveSize(64)`, which is what runs on AMD hosts. #2633's halves inherit it on 32-lane hosts. ## Evidence @⁠Lyrnc's 151 hand-written GFX10 wave64 cases ([gist](https://gist.github.com/Lyrnc/00c6589f26930d5d07c077f8c4a30131); no game data), each compared lane by lane with the same code run natively on a gfx1036 (RDNA2). An independent Python model agrees with that hardware on 151/151. - `main` `63bcca4`, native `WaveSize(64)`, **RX 5700 XT (RDNA1)**: 59 exact, 92 differ. Per case kind, that's the same count Lyrnc measured with `main` on the gfx1036 iGPU ([#2633](https://git.gtahub.st/github/Marvisak--VIctory/pulls/2633)), so the differences don't depend on the GPU. - **73 of the 92** are this bug: the only wrong output, or one of them, is the post-join register `v11`, which every lane writes from the scalar after `s_or_b64 exec, exec, s[34:35]`: | case kind | cases | `v11` wrong | wrong `v11` lanes | | --- | --- | --- | --- | | `red_branch` (whole-wave reduction in a branch) | 40 | 32 | 1,624 | | `readlane_branch` (`v_readlane` in a branch) | 20 | 16 | 832 | | `firstlane_branch` (`v_readfirstlane` in a branch) | 10 | 9 | 457 | | `red_earlyout` (reduction after `v_cmpx`) | 6 | 5 | 197 | | `red_loop` (reduction in a loop lanes leave) | 3 | 3 | 144 | | `writelane_branch` | 18 | 8 | 8 | The other 19 are in-branch or in-loop values (`vote_loop*`, `break_loop`, `red_nested`'s `v12`), which aren't covered here. ## Smallest case: `firstlane_branch/lo32` ``` s_mov_b32 s38, 0x5555 s_and_saveexec_b64 s[34:35], vcc ; vcc = lanes 0-31 s_cbranch_execz .Lskip v_readfirstlane_b32 s38, v47 ; v47 = 3 * lane + 5: s38 = 5 (lane 0) v_mov_b32 v10, s38 .Lskip: s_or_b64 exec, exec, s[34:35] v_mov_b32 v11, s38 ; every lane ``` | lane | console (gfx1036) `v11` | `main`, RX 5700 XT | | --- | --- | --- | | 0-31 | `0x5` | `0x5` | | 32-63 | `0x5` | **`0x5555`** | `v10` (inside the branch) matches in every lane. The full program as words (one 64-thread group, raw UAV in table dword 0 at `s0`, outputs `v10..v25` stored register-major at `64 * register + lane`): <details><summary>words</summary> ``` 0xbe810380, 0xf4080100, 0xfa000000, 0xbf8cc07f, 0x7e140280, 0x7e160280, 0x7e180280, 0x7e1a0280, 0x7e1c0280, 0x7e1e0280, 0x7e200280, 0x7e220280, 0x7e240280, 0x7e260280, 0x7e280280, 0x7e2a0280, 0x7e2c0280, 0x7e2e0280, 0x7e300280, 0x7e320280, 0x381400ff, 0xdead0000, 0x381600ff, 0xdead0000, 0x381800ff, 0xdead0000, 0x381a00ff, 0xdead0000, 0x165e0083, 0x4a5e5e85, 0xbe9e03c1, 0xbe9f0380, 0xd700002c, 0x00003d00, 0x36565881, 0x7d8a5680, 0xbea603ff, 0x00005555, 0xbea2246a, 0xbf880002, 0x7e4c052f, 0x7e140226, 0x88fe227e, 0x7e160226, 0x34020082, 0xe0701000, 0x80010a01, 0xe0701100, 0x80010b01, 0xe0701200, 0x80010c01, 0xe0701300, 0x80010d01, 0xe0701400, 0x80010e01, 0xe0701500, 0x80010f01, 0xe0701600, 0x80011001, 0xe0701700, 0x80011101, 0xe0701800, 0x80011201, 0xe0701900, 0x80011301, 0xe0701a00, 0x80011401, 0xe0701b00, 0x80011501, 0xe0701c00, 0x80011601, 0xe0701d00, 0x80011701, 0xe0701e00, 0x80011801, 0xe0701f00, 0x80011901, 0xbf810000 ``` </details> ## Notes - The value is the wave's on the console, whichever lanes compute it. So the fix is about where a scalar's definition reaches, not about the instruction. A scalar written under a lane branch needs to reach the lanes that skipped it at the join. - #2920's console mode (branches decided for the wave, vector instructions guarded by EXEC) is a form where scalars stay wave-shared, but it only applies to programs with group barriers. - On 32-lane hosts (`main` refuses `WaveSize(64)` there) this shows up only through #2633/#439-style halves, which inherit it: `firstlane_branch` is 508 wrong words there too, matching @⁠pickles976's RTX 4060 count. AI use: Claude Code (Claude Opus 5.5).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 17:55:27 +00:00
Author
Owner

@⁠ColorMeBoom on GitHub · 2026-11-27 19:41 UTC · original

Fix: #3593 (RX 5700 XT: 59 -> 108 of the 151 cases exact, none worse; regression test included).

AI use: Claude Code (Claude Opus 5.5).

**@⁠ColorMeBoom** on GitHub · 2026-11-27 19:41 UTC · [original](https://github.com/Marvisak/VIctory/issues/3562#issuecomment-7226779270) Fix: #3593 (RX 5700 XT: 59 -> 108 of the 151 cases exact, none worse; regression test included). AI use: Claude Code (Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
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#3562
No description provided.