Shaders: a lane mask counted after a loop its lanes leave at different iterations is each lane's own, not the wave's (wrong count, no error) #1498

Open
opened 2026-11-26 04:34:02 +00:00 by github-bot · 3 comments
Owner

@⁠r4tedx on GitHub · 2026-11-24 18:31 UTC · original

A compute program that counts (or otherwise reads as a number) a lane mask after control flow its lanes took differently gets a wrong value in the per-lane form, without any error. The commonest shape is the loop compilers emit: lanes that are done are added to an exit mask, EXEC loses them, and after the loop the mask is counted.

Reproduction (hand-assembled GFX10, no game data)

A wave32 compute program, 32 threads per group, v0 = the thread's index in the group, result in v2:

v_add_nc_u32   v1, 1, v0            ; lane i loops i + 1 times
s_mov_b32      s10, 0               ; exit mask
loop:
v_subrev_nc_u32 v1, 1, v1
v_cmp_eq_u32   vcc_lo, 0, v1
s_or_b32       s10, vcc_lo, s10     ; lanes that are done
s_andn2_b32    exec_lo, exec_lo, s10
s_cbranch_execnz loop
s_or_b32       exec_lo, exec_lo, s10
s_bcnt1_i32_b32 s9, s10             ; all 32 lanes are in the mask
v_mov_b32      v2, s9

Words: 4A020081 BE8A0380 4E020281 7D840280 880A0A6A 8A7E0A7E BF89FFFB 887E0A7E BE890F0A 7E040209. The wave64 form is the same with _b64 operations (BE8A0480, 888A0A6A, 8AFE0A7E, 88FE0A7E, BE89100A) and 64 threads.

On the console every lane ends with v2 = 32 (64 for the wave64 form): one wave, one mask.

What happens

program host result in every lane expected
wave32 RTX 5070 (32-lane waves) 1 32
wave32 Radeon iGPU (32-64) 1 32
wave64 Radeon iGPU (32-64) 1 64

Measured with the readback harness of #233 at its head ededb1b, where lane masks are handled as on main (the per-lane form). On main ffe310e itself both programs lower without a refusal (checked with a unit test through lower); I have not run them on a GPU from main, which has no multi-thread readback harness, so the table is #233's.

A second shape, a mask grown inside a branch only some lanes take and counted after it, is refused on a 64-lane host with a lane mask holding lanes elsewhere read as data, which is at least not silent.

Why

In the per-lane form each lane knows of a mask only what its own path saw. A mask joined across paths the lanes took differently, when read as a number, is then each lane's own view of it (here: its own bit), not the wave's.

How common

Offline over the title's shader pack (local title data, CPU only, counts only): about 400 compute programs read a lane mask as a number after such a join, about 300 of them wave32. That is a count of the shape, not of programs known to misbehave, and I have no title measurement of the effect. Counts feeding compaction and culling are the obvious candidates.

A fix, and where it is

The console flow of #320 (every lane runs every block, branches are the wave's) gives the wave's mask. A rule that sends such programs there on a host that runs their wave size, keeping the per-lane form where the console flow cannot lower them, makes all three rows exact and changes no program's outcome over the pack (no program gained or lost for a 32-lane or a 32-64 host). It sits on the fork branch after #439 (r4tedx/VIctory shader/wave-per-group), because it needs the console flow, which is not on main. Two of the fixtures and the detection of joins lanes reach by different paths come from @⁠mohammadallami75-ops's patch on #439.

Related: #225 (named lane reads where lanes branch differently, the same family), #269.

AI-assisted (Claude Code).

**@⁠r4tedx** on GitHub · 2026-11-24 18:31 UTC · [original](https://github.com/Marvisak/VIctory/issues/1498) A compute program that counts (or otherwise reads as a number) a lane mask after control flow its lanes took differently gets a wrong value in the per-lane form, without any error. The commonest shape is the loop compilers emit: lanes that are done are added to an exit mask, EXEC loses them, and after the loop the mask is counted. ## Reproduction (hand-assembled GFX10, no game data) A wave32 compute program, 32 threads per group, `v0` = the thread's index in the group, result in `v2`: ``` v_add_nc_u32 v1, 1, v0 ; lane i loops i + 1 times s_mov_b32 s10, 0 ; exit mask loop: v_subrev_nc_u32 v1, 1, v1 v_cmp_eq_u32 vcc_lo, 0, v1 s_or_b32 s10, vcc_lo, s10 ; lanes that are done s_andn2_b32 exec_lo, exec_lo, s10 s_cbranch_execnz loop s_or_b32 exec_lo, exec_lo, s10 s_bcnt1_i32_b32 s9, s10 ; all 32 lanes are in the mask v_mov_b32 v2, s9 ``` Words: `4A020081 BE8A0380 4E020281 7D840280 880A0A6A 8A7E0A7E BF89FFFB 887E0A7E BE890F0A 7E040209`. The wave64 form is the same with `_b64` operations (`BE8A0480`, `888A0A6A`, `8AFE0A7E`, `88FE0A7E`, `BE89100A`) and 64 threads. On the console every lane ends with `v2 = 32` (64 for the wave64 form): one wave, one mask. ## What happens | program | host | result in every lane | expected | |---|---|---|---| | wave32 | RTX 5070 (32-lane waves) | **1** | 32 | | wave32 | Radeon iGPU (32-64) | **1** | 32 | | wave64 | Radeon iGPU (32-64) | **1** | 64 | Measured with the readback harness of #233 at its head `ededb1b`, where lane masks are handled as on `main` (the per-lane form). On `main` `ffe310e` itself both programs lower without a refusal (checked with a unit test through `lower`); I have not run them on a GPU from `main`, which has no multi-thread readback harness, so the table is #233's. A second shape, a mask grown inside a branch only some lanes take and counted after it, is refused on a 64-lane host with `a lane mask holding lanes elsewhere read as data`, which is at least not silent. ## Why In the per-lane form each lane knows of a mask only what its own path saw. A mask joined across paths the lanes took differently, when read as a number, is then each lane's own view of it (here: its own bit), not the wave's. ## How common Offline over the title's shader pack (local title data, CPU only, counts only): about 400 compute programs read a lane mask as a number after such a join, about 300 of them wave32. That is a count of the shape, not of programs known to misbehave, and I have no title measurement of the effect. Counts feeding compaction and culling are the obvious candidates. ## A fix, and where it is The console flow of #320 (every lane runs every block, branches are the wave's) gives the wave's mask. A rule that sends such programs there on a host that runs their wave size, keeping the per-lane form where the console flow cannot lower them, makes all three rows exact and changes no program's outcome over the pack (no program gained or lost for a 32-lane or a 32-64 host). It sits on the fork branch after #439 ([r4tedx/VIctory `shader/wave-per-group`](https://github.com/r4tedx/VIctory/tree/shader/wave-per-group)), because it needs the console flow, which is not on `main`. Two of the fixtures and the detection of joins lanes reach by different paths come from @⁠mohammadallami75-ops's patch on #439. Related: #225 (named lane reads where lanes branch differently, the same family), #269. AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:34:02 +00:00
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-24 21:20 UTC · original

I ran the issue's wave32 loop on WARP 1.0.22 through the fork's normal shader emitter and dispatch/readback harness.

  • Before the mask-join change, 05c7cbb161f320e2fc25918a234c5978f86ed23a returned counts 1 through 8 in batches of four lanes. All 96 outputs across three 32-thread groups disagreed with 32.
  • At 88b38e8262c4b0242d84d640990e1e10565b78f9, all 96 outputs from the 32-thread groups and all 192 outputs from three 64-thread groups were 32. DXIL validation passed and the shader suite reported 165 passed, 1 ignored.

Partial-wave results on this WARP version are inconclusive. An independent bounded [WaveSize(32)] HLSL control compiled with DXC also sometimes counts more than 16 active lanes in the trailing wave of a 48-thread group. A useful hardware check would run the translated 48-thread case beside the DXC control on a wave32 adapter and compare each lane against 32 for the first wave and 16 for the trailing wave.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-24 21:20 UTC · [original](https://github.com/Marvisak/VIctory/issues/1498#issuecomment-7144801384) I ran the issue's wave32 loop on WARP 1.0.22 through the fork's normal shader emitter and dispatch/readback harness. - Before the mask-join change, `05c7cbb161f320e2fc25918a234c5978f86ed23a` returned counts 1 through 8 in batches of four lanes. All 96 outputs across three 32-thread groups disagreed with 32. - At `88b38e8262c4b0242d84d640990e1e10565b78f9`, all 96 outputs from the 32-thread groups and all 192 outputs from three 64-thread groups were 32. DXIL validation passed and the shader suite reported 165 passed, 1 ignored. Partial-wave results on this WARP version are inconclusive. An independent bounded `[WaveSize(32)]` HLSL control compiled with DXC also sometimes counts more than 16 active lanes in the trailing wave of a 48-thread group. A useful hardware check would run the translated 48-thread case beside the DXC control on a wave32 adapter and compare each lane against 32 for the first wave and 16 for the trailing wave.
Author
Owner

@⁠mohammadallami75-ops on GitHub · 2026-11-25 09:09 UTC · original

I ran the requested 48-thread hardware check with the translated fixture and a separate DXC control on a physical RTX 2080 SUPER (driver 621.01, Wave32). Across three groups, the translator returned 1 for all 144 reads at 05c7cbb. At c734600, all 144 were correct: 32 for every thread in the first wave and 16 for every thread in the trailing wave. The independently compiled DXC control returned the same 32/16 results at both revisions. The 32- and 64-thread cases also went from 0/288 correct to 288/288. DXIL validation passed on both revisions.

I submitted the 48-thread regression with the full results in r4tedx/VIctory#17. This was a synthetic test. I did not test the game or frame rate.

AI-assisted.

**@⁠mohammadallami75-ops** on GitHub · 2026-11-25 09:09 UTC · [original](https://github.com/Marvisak/VIctory/issues/1498#issuecomment-7157956395) I ran the requested 48-thread hardware check with the translated fixture and a separate DXC control on a physical RTX 2080 SUPER (driver 621.01, Wave32). Across three groups, the translator returned 1 for all 144 reads at `05c7cbb`. At `c734600`, all 144 were correct: 32 for every thread in the first wave and 16 for every thread in the trailing wave. The independently compiled DXC control returned the same 32/16 results at both revisions. The 32- and 64-thread cases also went from 0/288 correct to 288/288. DXIL validation passed on both revisions. I submitted the 48-thread regression with the full results in r4tedx/VIctory#17. This was a synthetic test. I did not test the game or frame rate. AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 10:22 UTC · original

Automated triage for the maintainers.

Confirmed by reading main. mask_dword in crates/victory-shader/src/emit/lower.rs turns a lane mask read as a number into a waveActiveBallot taken in the block that writes the mask. In a loop that lanes leave at different iterations, lanes that already left are not active when the last write runs. Each lane's count then holds only the lanes that left with it, and nothing is refused. The WARP and RTX 2080 SUPER readbacks in the comments match this (1 instead of 32 or 64). Same family as #225.

Open PR #1587 ("ballot divergent-loop masks at reconverged reads") targets this issue on main. The fork branch shader/wave-per-group fixes it through the console flow instead and depends on #439. Pull requests are open to everyone. If the branch should be weighed against #1587, please open it as a pull request stacked on #439, with the 48-thread regression from the comments.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 10:22 UTC · [original](https://github.com/Marvisak/VIctory/issues/1498#issuecomment-7159615543) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-25T09:09:43Z at=2026-11-25T09:50:38Z --> Automated triage for the maintainers. Confirmed by reading `main`. `mask_dword` in `crates/victory-shader/src/emit/lower.rs` turns a lane mask read as a number into a `waveActiveBallot` taken in the block that writes the mask. In a loop that lanes leave at different iterations, lanes that already left are not active when the last write runs. Each lane's count then holds only the lanes that left with it, and nothing is refused. The WARP and RTX 2080 SUPER readbacks in the comments match this (1 instead of 32 or 64). Same family as #225. Open PR #1587 ("ballot divergent-loop masks at reconverged reads") targets this issue on `main`. The fork branch `shader/wave-per-group` fixes it through the console flow instead and depends on #439. Pull requests are open to everyone. If the branch should be weighed against #1587, please open it as a pull request stacked on #439, with the 48-thread regression from the comments. --- _Generated by [Claude Code](https://claude.ai/code)_
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#1498
No description provided.