Shaders: a lane mask read as a number keeps the console's bits for the lanes outside a partial group #3917

Closed
github-bot wants to merge 2 commits from github-bot/gh-3917 into main AGit
Owner

@⁠dyshay on GitHub · 2026-11-30 11:30 UTC · original
fix/mask-not-popcount → main · +402 −9 · 2 files · 2 commits · closed without merging

Fixes #3916.

What was wrong

On a 32-lane host, the plain wave64 lowering (the default path since folding is off, 5c262d7b) reads a lane mask as a number by taking a ballot of the lanes' booleans (Lower::mask_dword). In a group that is not a multiple of 64 threads (a group of 96: the second console wave has lanes 0..31 only), the lanes that do not exist had no bit, whatever the mask:

  • s_not_b64 of a compare has those bits set on the console (the compare writes 0 for lanes outside EXEC, the complement sets them). s_bcnt1_i32_b64 of it gave 32 instead of 64 for s0 = 0 in the program below, 26 instead of 58 for s0 = 70. The structure takes EXEC on entry as every lane, so the complement looked like a mask set only where lanes run, and s_andn2_b64 exec, vcc (0 there on the console) is the same lane boolean to it: the lane boolean alone cannot say.
  • The second console wave of such a group has no upper host wave. The exchange read that half's LDS dword, which nothing writes: whatever LDS held.

What this does

crates/victory-shader/src/emit/lower.rs only; no switch (a correctness fix checked against the console's semantics).

  • Lower::outside_pair: for a compute program with console lanes not in the group (a partial console wave, or lanes past the dispatched count under partial_active), the two dwords of the mask as the console has them in those lanes, followed back through the instructions that made it: EXEC on entry and VALU compare/carry masks are 0 there; s_mov, s_not, s_and, s_or, s_xor, s_andn2, s_orn2, s_nand, s_nor, s_xnor, s_cselect and the _saveexec forms work on those bits as on the others; inline constants and literals are what they are. A mask made another way (a join, a load) keeps the host's ballot, 0 there, as before. Folded programs and local waves are left as they are.
  • Lower::mask_dword: a lane past the dispatched count takes its bit from that value before the ballot; after the exchange, the bits of the console lanes past the group take it too (fill_outside_lanes).
  • Lower::exchange_halves: a console wave with no upper host wave reads 0 for that half, as its comment already said.

Masks with no lanes outside the group, and groups that are a multiple of 64 without a thread count, are lowered as before.

Test

emit::folded_tests::a_mask_complement_counts_the_lanes_outside_the_group_by_default: the program of a_mask_with_lanes_outside_exec_is_read_as_the_consoles_number through emit() with the default switches (folding off) on a 32-lane host, group of 96, s0 in 0, 20, 70, 96:

v_cmp_gt_u32    vcc, s0, v0
s_not_b64       s[12:13], vcc
s_bcnt1_i32_b64 s14, s[12:13]      ; console: 64 in wave 1 for s0 = 0
s_ff1_i32_b64   s15, s[12:13]
s_andn2_b64     s[16:17], exec, vcc
s_bcnt1_i32_b64 s18, s[16:17]      ; console: 32 in wave 1 for s0 = 0

It fails before the change (lane 64, bcnt of the complement: left 32, right 64) and passes after, on an RTX 4090. It also tries WARP, which does not take a WaveSize(32) program here, so on a machine without a 32-lane card it compiles and validates only.

Checks: victory-shader tests named fold (24), wave (81, 1 ignored), mask (26), all ok, including threads_past_a_dispatch_x_count_do_nothing (s_bcnt1 of EXEC past the count stays 0). cargo clippy -p victory-shader --all-targets: no new warning. Diagnostics build of victory ok.

Credit: found while testing the fold default; ISA semantics from AMD RDNA2.

🤖 Generated with Claude Code

**@⁠dyshay** on GitHub · 2026-11-30 11:30 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3917) `fix/mask-not-popcount` → `main` · +402 −9 · 2 files · 2 commits · closed without merging Fixes #3916. ## What was wrong On a 32-lane host, the plain wave64 lowering (the default path since folding is off, 5c262d7b) reads a lane mask as a number by taking a ballot of the lanes' booleans (`Lower::mask_dword`). In a group that is not a multiple of 64 threads (a group of 96: the second console wave has lanes 0..31 only), the lanes that do not exist had no bit, whatever the mask: - `s_not_b64` of a compare has those bits set on the console (the compare writes 0 for lanes outside EXEC, the complement sets them). `s_bcnt1_i32_b64` of it gave 32 instead of 64 for `s0 = 0` in the program below, 26 instead of 58 for `s0 = 70`. The structure takes EXEC on entry as every lane, so the complement looked like a mask set only where lanes run, and `s_andn2_b64 exec, vcc` (0 there on the console) is the same lane boolean to it: the lane boolean alone cannot say. - The second console wave of such a group has no upper host wave. The exchange read that half's LDS dword, which nothing writes: whatever LDS held. ## What this does `crates/victory-shader/src/emit/lower.rs` only; no switch (a correctness fix checked against the console's semantics). - `Lower::outside_pair`: for a compute program with console lanes not in the group (a partial console wave, or lanes past the dispatched count under `partial_active`), the two dwords of the mask as the console has them in those lanes, followed back through the instructions that made it: EXEC on entry and VALU compare/carry masks are 0 there; `s_mov`, `s_not`, `s_and`, `s_or`, `s_xor`, `s_andn2`, `s_orn2`, `s_nand`, `s_nor`, `s_xnor`, `s_cselect` and the `_saveexec` forms work on those bits as on the others; inline constants and literals are what they are. A mask made another way (a join, a load) keeps the host's ballot, 0 there, as before. Folded programs and local waves are left as they are. - `Lower::mask_dword`: a lane past the dispatched count takes its bit from that value before the ballot; after the exchange, the bits of the console lanes past the group take it too (`fill_outside_lanes`). - `Lower::exchange_halves`: a console wave with no upper host wave reads 0 for that half, as its comment already said. Masks with no lanes outside the group, and groups that are a multiple of 64 without a thread count, are lowered as before. ## Test `emit::folded_tests::a_mask_complement_counts_the_lanes_outside_the_group_by_default`: the program of `a_mask_with_lanes_outside_exec_is_read_as_the_consoles_number` through `emit()` with the default switches (folding off) on a 32-lane host, group of 96, `s0` in 0, 20, 70, 96: ``` v_cmp_gt_u32 vcc, s0, v0 s_not_b64 s[12:13], vcc s_bcnt1_i32_b64 s14, s[12:13] ; console: 64 in wave 1 for s0 = 0 s_ff1_i32_b64 s15, s[12:13] s_andn2_b64 s[16:17], exec, vcc s_bcnt1_i32_b64 s18, s[16:17] ; console: 32 in wave 1 for s0 = 0 ``` It fails before the change (`lane 64, bcnt of the complement: left 32, right 64`) and passes after, on an RTX 4090. It also tries WARP, which does not take a `WaveSize(32)` program here, so on a machine without a 32-lane card it compiles and validates only. Checks: `victory-shader` tests named `fold` (24), `wave` (81, 1 ignored), `mask` (26), all ok, including `threads_past_a_dispatch_x_count_do_nothing` (`s_bcnt1` of EXEC past the count stays 0). `cargo clippy -p victory-shader --all-targets`: no new warning. Diagnostics build of `victory` ok. Credit: found while testing the fold default; ISA semantics from AMD RDNA2. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On a 32-lane host the plain wave64 lowering (the default since folding is
off, 5c262d7b) reads a lane mask as a number by a ballot of the lanes'
booleans. In a group that is not a multiple of 64 threads (96: the second
console wave has lanes 0..31 only), the lanes that do not exist had no
bit, whatever the mask. On the console the SGPR pair holds all 64 bits:
those lanes are outside EXEC, a compare writes 0 there and s_not_b64 sets
them, so s_bcnt1_i32_b64 of the complement of a compare is 64 for s0 = 0
in wave 1, not 32. The structure takes EXEC on entry as every lane, so
the complement and s_andn2_b64 exec, vcc (0 there) are the same lane
boolean to it.

- outside_pair: the mask's two dwords in the lanes not in the group
  (partial console wave, or past the dispatched count), followed back
  through EXEC on entry and compare/carry masks (0), s_mov/not/and/or/
  xor/andn2/orn2/nand/nor/xnor/cselect and the saveexec forms, constants.
  A join or a load keeps the host's ballot (0 there), as before. Folded
  programs and local waves unchanged.
- mask_dword: lanes past the count take their bit from it before the
  ballot; the console lanes past the group take theirs after the exchange.
- exchange_halves: a console wave with no upper host wave reads 0 for
  that half (its LDS dword was never written).

Test: folded_tests::a_mask_complement_counts_the_lanes_outside_the_group_by_default
(emit() with default switches, 32-lane host, group of 96; also tries WARP,
which refuses WaveSize(32) here): fails before (lane 64: 32 vs 64), passes
on the card after.

Found while testing the fold default; ISA semantics from AMD RDNA2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PVi6XmTj4ohPL7QwUeFgV4
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 11:35:11 +00:00
Author
Owner

@⁠mstrmoos on GitHub · 2026-11-30 19:22 UTC · original

Picked onto our branch together with #3896, #3918 and #3894. The merge applied cleanly, and all of victory-gpu's and victory-shader's tests passed (gpu 947, shader 469). Over the following benches on the RTX 5080 16 GB, Ryzen 7 9800X3D, Windows 11, Series X profile, same save and 90 s bench route each run, I saw no regressions or visual problems. I didn't measure this PR on its own.

**@⁠mstrmoos** on GitHub · 2026-11-30 19:22 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3917#issuecomment-7289288275) Picked onto our branch together with #3896, #3918 and #3894. The merge applied cleanly, and all of victory-gpu's and victory-shader's tests passed (gpu 947, shader 469). Over the following benches on the RTX 5080 16 GB, Ryzen 7 9800X3D, Windows 11, Series X profile, same save and 90 s bench route each run, I saw no regressions or visual problems. I didn't measure this PR on its own.
Author
Owner

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

@⁠mstrmoos thanks for the clean merge and the test counts. That fits: the fix only changes what a lane mask read as a number gives for lanes outside a partial group, so it shows only in programs that count a complemented mask, and no visible regression is the expected result elsewhere.

**@⁠dyshay** on GitHub · 2026-11-30 19:39 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3917#issuecomment-7289743852) @⁠mstrmoos thanks for the clean merge and the test counts. That fits: the fix only changes what a lane mask read as a number gives for lanes outside a partial group, so it shows only in programs that count a complemented mask, and no visible regression is the expected result elsewhere.
Author
Owner

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

A small gap here, found while building #4400 on top of this PR: outside_dword strips only _saveexec_b64 and _b64, so a wave32 console's mask operations (s_not_b32 vcc_lo, s_and_saveexec_b32, ...) return None and keep the host's zeros. In a wave32 group of 48 threads, s_bcnt1_i32_b32 of s_not_b32 of a compare gave 16 where the console gives 32 (RTX 4060, 32-lane host waves, default switches). A _b32 operation works on its dword as the _b64 one does on each of its two, so following both suffixes is enough. #4400 carries that fix as 684da387, with a test (a_wave32_mask_complement_counts_the_lanes_outside_the_group_by_default), in case you'd rather fold it in here.

AI-assisted with Claude Code.

**@⁠tyr0nsur** on GitHub · 2026-11-30 20:06 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3917#issuecomment-7290413562) A small gap here, found while building #4400 on top of this PR: `outside_dword` strips only `_saveexec_b64` and `_b64`, so a **wave32** console's mask operations (`s_not_b32 vcc_lo`, `s_and_saveexec_b32`, ...) return `None` and keep the host's zeros. In a wave32 group of 48 threads, `s_bcnt1_i32_b32` of `s_not_b32` of a compare gave 16 where the console gives 32 (RTX 4060, 32-lane host waves, default switches). A `_b32` operation works on its dword as the `_b64` one does on each of its two, so following both suffixes is enough. #4400 carries that fix as 684da387, with a test (`a_wave32_mask_complement_counts_the_lanes_outside_the_group_by_default`), in case you'd rather fold it in here. AI-assisted with Claude Code.
Author
Owner

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

@⁠tyr0nsur good catch, thanks. Folding 684da387 into this PR (your authorship kept, test included); I'll push once the tests pass here. #4400 can then stack on the updated head.

**@⁠dyshay** on GitHub · 2026-11-30 20:39 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3917#issuecomment-7291278850) @⁠tyr0nsur good catch, thanks. Folding 684da387 into this PR (your authorship kept, test included); I'll push once the tests pass here. #4400 can then stack on the updated head.
outside_dword followed only the _b64 operations, so a wave32 console's
mask (s_not_b32 vcc_lo, s_and_saveexec_b32, ...) gave up and kept the
host's zeros: in a group of 48, s_bcnt1_i32_b32 of the complement of a
compare counted 16 where the console counts 32. A _b32 operation works
on its dword as a _b64 one on each of its two, so both are now followed.

Test: the wave32 group of 48 (s_bcnt1_i32_b32 of s_not_b32 vcc_lo).
Found in a review by GPT-6.2.

Folded into #3917 at the author's suggestion (#3917 comment)

(cherry picked from commit 684da38785)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PVi6XmTj4ohPL7QwUeFgV4
Author
Owner

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

Folded in as 628f25de (authored by @⁠tyr0nsur), with the wave32 s_bcnt1_i32_b32 test. One note: in the test I kept only the popcount check. The SCC check of s_not_b32 depends on your scc_some work in #4400, so it belongs there (it fails on this PR alone, lane 32 with s0=48: 0 instead of 1). Mask/fold/wave tests: 117 passed on an RTX 4090. Clippy shows 4 lints in files this PR doesn't touch (lower/analysis.rs, native.rs), coming from the base.

**@⁠dyshay** on GitHub · 2026-11-30 20:47 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3917#issuecomment-7291472472) Folded in as 628f25de (authored by @⁠tyr0nsur), with the wave32 `s_bcnt1_i32_b32` test. One note: in the test I kept only the popcount check. The SCC check of `s_not_b32` depends on your `scc_some` work in #4400, so it belongs there (it fails on this PR alone, lane 32 with s0=48: 0 instead of 1). Mask/fold/wave tests: 117 passed on an RTX 4090. Clippy shows 4 lints in files this PR doesn't touch (`lower/analysis.rs`, `native.rs`), coming from the base.
Author
Owner

@⁠VibeCodeGuy on GitHub · 2026-11-30 23:24 UTC · original

Offline corpus check (every shader container in a v1.0.2.1 dump: 25,699 unique programs), this PR at 628f25de against its merge-base with main, 41adb551 (it does not merge cleanly onto main e1251c31).

  • Newly failing: 0. Newly passing: 0.
  • DXIL output changed: 6 programs (6 Cs); 6 of them pass the validator.
The 6 changed programs
  • 25328ea424d08c00 Cs ShallowWaterSim:CS_GenerateDisturbOverlappingPairs
  • 53c21e950d143cc7 Cs avgLuminanceCompute:LuminanceFromHDRBinkCS
  • e18931c72af42e46 Cs deferred_lighting:CS_ComputeInfiniteRRIntensity
  • 49460c1d1b5fafa0 Cs subsurfacescatter:CS_SSSTileClassification
  • 665b261b36552c65 Cs water_shading:CS_FoamTestDecal
  • e81fd01512ba1002 Cs weatherParticle:CS_UpdateDecalArgs

How: victory translate <dump> --validate --list built for each side (release, cross-built with cargo-xwin, run under Wine), VICTORY_HOST_WAVE=32, plus the four shader-cache-key settings play starts with (VICTORY_EARLY_DEPTH=1, VICTORY_NAN_SCRUB=1, VICTORY_BVH_MISS=1, VICTORY_SAMPLED_MARKS=1). Validator: dxil.dll from DXC v1.9.2609. "Changed" compares translate's emitted hash of each module and its bindings. There was no GPU on this side, so no host pipelines were made and nothing was run on a card. At the merge-base the same run has the same 72 failures: 71 mesh programs over the 32 KiB output limit (the known shortcut in mesh_prologue) and CS_TraceShadowRays_FullAdaptive (#2993).

**@⁠VibeCodeGuy** on GitHub · 2026-11-30 23:24 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3917#issuecomment-7295091015) **Offline corpus check** (every shader container in a v1.0.2.1 dump: 25,699 unique programs), this PR at 628f25de against its merge-base with `main`, 41adb551 (it does not merge cleanly onto `main` e1251c31). - Newly failing: **0**. Newly passing: **0**. - DXIL output changed: **6** programs (6 Cs); 6 of them pass the validator. <details><summary>The 6 changed programs</summary> - `25328ea424d08c00` Cs `ShallowWaterSim:CS_GenerateDisturbOverlappingPairs` - `53c21e950d143cc7` Cs `avgLuminanceCompute:LuminanceFromHDRBinkCS` - `e18931c72af42e46` Cs `deferred_lighting:CS_ComputeInfiniteRRIntensity` - `49460c1d1b5fafa0` Cs `subsurfacescatter:CS_SSSTileClassification` - `665b261b36552c65` Cs `water_shading:CS_FoamTestDecal` - `e81fd01512ba1002` Cs `weatherParticle:CS_UpdateDecalArgs` </details> How: `victory translate <dump> --validate --list` built for each side (release, cross-built with cargo-xwin, run under Wine), `VICTORY_HOST_WAVE=32`, plus the four shader-cache-key settings `play` starts with (`VICTORY_EARLY_DEPTH=1`, `VICTORY_NAN_SCRUB=1`, `VICTORY_BVH_MISS=1`, `VICTORY_SAMPLED_MARKS=1`). Validator: `dxil.dll` from DXC v1.9.2609. "Changed" compares translate's `emitted` hash of each module and its bindings. There was no GPU on this side, so no host pipelines were made and nothing was run on a card. At the merge-base the same run has the same 72 failures: 71 mesh programs over the 32 KiB output limit (the known shortcut in `mesh_prologue`) and `CS_TraceShadowRays_FullAdaptive` (#2993).
github-bot closed this pull request 2026-12-01 12:37:38 +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!3917
No description provided.