wave64 on 32-lane hosts: s_bcnt1 of a mask complement misses the lanes outside a partial group (32 instead of 64) #3916

Open
opened 2026-11-30 11:35:09 +00:00 by github-bot · 4 comments
Owner

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

What is wrong

A console wave64 compute program whose group is not a multiple of 64 threads (a group of 96: the second console wave has lanes 0..31 only) counts the bits of a complemented lane mask wrong when it goes through the plain wave64-on-wave32 lowering, which is the default path since folding was turned off by default (5c262d7b).

On RDNA2 the SGPR pair holds all 64 bits. Lanes that do not exist in a partial wave are outside EXEC: a VALU compare writes 0 there, and s_not_b64 (or s_orn2_b64, s_nand_b64, ...) sets those bits like any other. s_bcnt1_i32_b64 and s_ff1_i32_b64 then count/find them.

The lowering reads a mask as a number by taking a ballot of the lanes' booleans (Lower::mask_dword in crates/victory-shader/src/emit/lower.rs). Two things go wrong for a partial group:

  1. The structure (structure.rs) takes EXEC on entry as every lane (Bool::TRUE), so s_not_b64 of a compare is just !lane and looks like a mask set only where lanes run (Structured::active). The ballot only has the lanes that exist, so the bits of the missing lanes are 0 where the console has 1. (s_andn2_b64 exec, vcc is the same lane boolean to the structure, but its console value has 0 there, so the lane boolean alone cannot give the answer.)
  2. In a group of 96 on a 32-lane host, the second console wave has no upper host wave. The exchange (Lower::exchange_halves) reads that half's dword from LDS though nothing writes it: whatever LDS held. The doc comment says the missing half "gets 0", but nothing makes it so.

Repro

Program (raw GCN, as in folded_tests.rs):

v_cmp_gt_u32   vcc, s0, v0        ; vcc = s0 > t
s_not_b64      s[12:13], vcc
s_bcnt1_i32_b64 s14, s[12:13]
s_ff1_i32_b64  s15, s[12:13]

group of 96 threads, wave64, through emit() with the default switches (folding off), host of 32-lane waves.

s0 console wave console s_bcnt1 translated
0 1 (threads 64..95) 64 32
70 1 58 26

Wave 0 is right. The folded form (VICTORY_WAVE_FOLDED=1) is right (a_mask_with_lanes_outside_exec_is_read_as_the_consoles_number).

Which programs could be affected

Wave64 compute programs translated in the plain form on 32-lane hosts (NVIDIA, Intel) whose group size is not a multiple of 64, or that are dispatched by a thread count with a partial last group (USER_DISPATCH_THREADS_X, lanes kept under partial_active), and that read as a number a lane mask holding lanes outside EXEC: the complement of a compare counted or searched (s_bcnt1, s_ff1, v_mbcnt, scalar compares of the mask). Typically "how many lanes failed the test" in compaction or classification code. Masks that are only ANDed with EXEC are not affected by (1), but every mask read as a number in such a group is exposed to (2).

**@⁠dyshay** on GitHub · 2026-11-30 11:30 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/3916) ## What is wrong A console wave64 compute program whose group is not a multiple of 64 threads (a group of 96: the second console wave has lanes 0..31 only) counts the bits of a complemented lane mask wrong when it goes through the plain wave64-on-wave32 lowering, which is the default path since folding was turned off by default (5c262d7b). On RDNA2 the SGPR pair holds all 64 bits. Lanes that do not exist in a partial wave are outside EXEC: a VALU compare writes 0 there, and `s_not_b64` (or `s_orn2_b64`, `s_nand_b64`, ...) sets those bits like any other. `s_bcnt1_i32_b64` and `s_ff1_i32_b64` then count/find them. The lowering reads a mask as a number by taking a ballot of the lanes' booleans (`Lower::mask_dword` in `crates/victory-shader/src/emit/lower.rs`). Two things go wrong for a partial group: 1. The structure (`structure.rs`) takes EXEC on entry as every lane (`Bool::TRUE`), so `s_not_b64` of a compare is just `!lane` and looks like a mask set only where lanes run (`Structured::active`). The ballot only has the lanes that exist, so the bits of the missing lanes are 0 where the console has 1. (`s_andn2_b64 exec, vcc` is the same lane boolean to the structure, but its console value has 0 there, so the lane boolean alone cannot give the answer.) 2. In a group of 96 on a 32-lane host, the second console wave has no upper host wave. The exchange (`Lower::exchange_halves`) reads that half's dword from LDS though nothing writes it: whatever LDS held. The doc comment says the missing half "gets 0", but nothing makes it so. ## Repro Program (raw GCN, as in `folded_tests.rs`): ``` v_cmp_gt_u32 vcc, s0, v0 ; vcc = s0 > t s_not_b64 s[12:13], vcc s_bcnt1_i32_b64 s14, s[12:13] s_ff1_i32_b64 s15, s[12:13] ``` group of 96 threads, wave64, through `emit()` with the default switches (folding off), host of 32-lane waves. | s0 | console wave | console `s_bcnt1` | translated | |---|---|---|---| | 0 | 1 (threads 64..95) | 64 | 32 | | 70 | 1 | 58 | 26 | Wave 0 is right. The folded form (`VICTORY_WAVE_FOLDED=1`) is right (`a_mask_with_lanes_outside_exec_is_read_as_the_consoles_number`). ## Which programs could be affected Wave64 compute programs translated in the plain form on 32-lane hosts (NVIDIA, Intel) whose group size is not a multiple of 64, or that are dispatched by a thread count with a partial last group (`USER_DISPATCH_THREADS_X`, lanes kept under `partial_active`), and that read as a number a lane mask holding lanes outside EXEC: the complement of a compare counted or searched (`s_bcnt1`, `s_ff1`, `v_mbcnt`, scalar compares of the mask). Typically "how many lanes failed the test" in compaction or classification code. Masks that are only ANDed with EXEC are not affected by (1), but every mask read as a number in such a group is exposed to (2).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 11:35:09 +00:00
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-30 11:30 UTC · original

Please attach both logs from the run that went wrong. Without them we can only guess. Paste %LOCALAPPDATA% into the address bar of File Explorer to get there.

  1. The play log, in %LOCALAPPDATA%\victory\logs\: the newest victory-nvidia-play-<date>-<time>-<number>.log (from victory.exe) or victory-nvidia-<date>-<time>.log (from the script launcher). The window shows its path at the start, on the line Log: ....
  2. The crash log: %LOCALAPPDATA%\VIctory\data\7952987A\crashes.log, and, if there is one from that run, the newest victory-<date>-<time>-<number>.crash.log in %LOCALAPPDATA%\VIctory\data\7952987A\temp\.

Also say which GPU, Windows version (10 or 11)

**@⁠github-actions[bot]** on GitHub · 2026-11-30 11:30 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/3916#issuecomment-7277121281) Please attach both logs from the run that went wrong. Without them we can only guess. Paste `%LOCALAPPDATA%` into the address bar of File Explorer to get there. 1. The play log, in `%LOCALAPPDATA%\victory\logs\`: the newest `victory-nvidia-play-<date>-<time>-<number>.log` (from `victory.exe`) or `victory-nvidia-<date>-<time>.log` (from the script launcher). The window shows its path at the start, on the line `Log: ...`. 2. The crash log: `%LOCALAPPDATA%\VIctory\data\7952987A\crashes.log`, and, if there is one from that run, the newest `victory-<date>-<time>-<number>.crash.log` in `%LOCALAPPDATA%\VIctory\data\7952987A\temp\`. Also say which GPU, Windows version (10 or 11)
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-30 11:32 UTC · original

finding: wave64-on-wave32 mask-as-number for a partial group is a real source-level gap and your raw-GCN repro needs no title data, so it is in principle locally testable here. Holding off on an implementation: the fix spans three lowering layers (structure.rs treating entry EXEC as every lane, Lower::mask_dword's whole/ballot path, and Lower::exchange_halves reading an unwritten LDS half), and the right contract for lanes outside EXEC in the plain (non-folded) path is a maintainer design call. I do not want to add a format/behaviour PR that conflicts with the folded path's existing answer. If you can say which layer you intend to own, I can take the remaining one with a synthetic test. No game data used or uploaded. AI-assisted: Hy4 flash.

**@⁠cloph-dsp** on GitHub · 2026-11-30 11:32 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/3916#issuecomment-7277177731) finding: wave64-on-wave32 mask-as-number for a partial group is a real source-level gap and your raw-GCN repro needs no title data, so it is in principle locally testable here. Holding off on an implementation: the fix spans three lowering layers (structure.rs treating entry EXEC as every lane, Lower::mask_dword's whole/ballot path, and Lower::exchange_halves reading an unwritten LDS half), and the right contract for lanes outside EXEC in the plain (non-folded) path is a maintainer design call. I do not want to add a format/behaviour PR that conflicts with the folded path's existing answer. If you can say which layer you intend to own, I can take the remaining one with a synthetic test. No game data used or uploaded. AI-assisted: Hy4 flash.
Author
Owner

@⁠dyshay on GitHub · 2026-11-30 11:39 UTC · original

@⁠cloph-dsp thanks for looking. The PR linked here, #3917, already takes all three layers in one change: lanes outside the group get the console's bits reconstructed from the mask's instructions (entry EXEC and compares/carries 0; s_not/and/or/xor/andn2/orn2/nand/nor/xnor/cselect, saveexec and constants followed), mask_dword applies them before the ballot for lanes past the dispatch and after the exchange for those past the group, and exchange_halves reads 0 for a missing upper half. The folded path isn't touched. A mask from a join or a load keeps the old behaviour (0 there), and the SCC of a mask op read as data (scc_some) is likely wrong the same way. That second one is open if you'd like to take it with a synthetic test. A review of #3917's contract for lanes outside EXEC would also be very welcome.

**@⁠dyshay** on GitHub · 2026-11-30 11:39 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/3916#issuecomment-7277344635) @⁠cloph-dsp thanks for looking. The PR linked here, #3917, already takes all three layers in one change: lanes outside the group get the console's bits reconstructed from the mask's instructions (entry EXEC and compares/carries 0; s_not/and/or/xor/andn2/orn2/nand/nor/xnor/cselect, saveexec and constants followed), `mask_dword` applies them before the ballot for lanes past the dispatch and after the exchange for those past the group, and `exchange_halves` reads 0 for a missing upper half. The folded path isn't touched. A mask from a join or a load keeps the old behaviour (0 there), and the SCC of a mask op read as data (`scc_some`) is likely wrong the same way. That second one is open if you'd like to take it with a synthetic test. A review of #3917's contract for lanes outside EXEC would also be very welcome.
Author
Owner

@⁠tyr0nsur on GitHub · 2026-11-30 19:51 UTC · original

I took the scc_some part: #4400, stacked on #3917. With a group of 96 and every lane passing the compare, SCC of s_not_b64 vcc was 0 in the second console wave where the console has 1. It now takes the lanes outside the group from outside_pair, the same way mask_dword does. The test is in folded_tests.rs. It passes on an RTX 4060 (32-lane host waves, default switches), and all 464 victory-shader lib tests pass. A review of how it picks the mask behind SCC (scc_mask: the lowest scalar destination, EXEC for a saveexec) would be welcome.

AI-assisted with Claude Code.

**@⁠tyr0nsur** on GitHub · 2026-11-30 19:51 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/3916#issuecomment-7290030096) I took the `scc_some` part: #4400, stacked on #3917. With a group of 96 and every lane passing the compare, SCC of `s_not_b64 vcc` was 0 in the second console wave where the console has 1. It now takes the lanes outside the group from `outside_pair`, the same way `mask_dword` does. The test is in `folded_tests.rs`. It passes on an RTX 4060 (32-lane host waves, default switches), and all 464 `victory-shader` lib tests pass. A review of how it picks the mask behind SCC (`scc_mask`: the lowest scalar destination, EXEC for a saveexec) would be welcome. AI-assisted with Claude 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/trustno1-coder--VIctory-NVIDIA#3916
No description provided.