Skip to content

rdna2spirv: a lane mask read as a number is the wave's mask word (SHADER-MASK-AS-DATA) - #1043

Open
thesubtleties wants to merge 3 commits into
ProjectLeonida:mainfrom
thesubtleties:mask-as-data
Open

thesubtleties wants to merge 3 commits into
ProjectLeonida:mainfrom
thesubtleties:mask-as-data

Conversation

@thesubtleties

@thesubtleties thesubtleties commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-MASK-AS-DATA

What and why

A lane mask is one word for the wave on hardware; the translator keeps each lane's own 0 / ~0. An op that read the mask as a number therefore got a different wrong value per lane, with no error: with mask 0x3ff, s_add_u32 d, mask, 1 was 0 or 1 and v_mov_b32 v, mask was 0 or ~0 (crosslane_fuzz.py "maskdata", the old #293). s_bcnt / s_ff / s_flbit already rebuild the word. This does the same for:

  • 32-bit integer SALU and VALU ops (lane_masks.NUMBER_READERS);
  • v_mov_b32 of a mask, when no v_readfirstlane_b32 or vector bitwise op reads that value as a mask. Each copy is followed forward, through VGPR-to-VGPR copies (second commit).

The emulation planner counts such an op as an exchange, it never joins an active run, and wave32_pixel_scan.py counts it as mask_word.

Partial on purpose. Not covered: the values excluded above (the v_mov / v_or3 / v_readfirstlane idiom needs each lane's own bit), compares, SDWA and DPP, and 16-bit, float and 64-bit ops.

Three existing tests changed: test_salu_movrels_lane_mask, test_scselect_b64_data and test_salu64_bitwise_data read a mask back with v_mov_b32 and expected the lane's own 0 / ~0; they now expect the mask word.

Tested

Measured on base 82a2aabc, then rebased; the semantic suite and the touched tests were rerun after the rebase.

  • New tests/semantic/test_mask_as_data.py (wave32, native wave64, partial EXEC, the through-a-VGPR idiom, emulated translation): fails on main, passes here.
  • run_semantic_tests.py: 0 files failed.
  • crosslane_fuzz.py, hard mismatches, main -> this PR; no program fails here that passed on main:
Run main first commit now
wave32 native, seed 880505 (300), 3080 and gfx1036 20 10 0
wave32 native, seed 991201 (250), 3080 13 7 4
wave32 native, seed 991202 (250), 3080 and gfx1036 21 16 4
wave64 emulated --ext, seed 880606 (300), 3080 and gfx1036 6 6 1
wave64 native --ext, seed 880707 (300), gfx1036 7 6 3

The 880xxx main rows were run on 98a977b4. With --no-maskdata there are no hard mismatches there on main. Correction: the first version of this text said the remaining mismatches were v_readfirstlane reads. Minimizing them showed most came from a plain VGPR-to-VGPR copy that the first commit counted as a mask read; the second commit fixes that. What is left on wave32 seeds 991201, 991202 and 993301 (11 mismatches, plus 4 programs with a hypothesis op) is now minimized on the 3080: every reduction contains a v_mov_b32 of a mask into a VGPR that a v_readfirstlane_b32 or a vector bitwise op then reads, the idiom this PR leaves alone. The shortest is v_cmp_ne_u32_e32 vcc_lo, 9, v6 / v_mov_b32_e32 v2, vcc_lo / v_readfirstlane_b32 s8, v2. The wave64 leftovers (seed 880606 emulated on the 3080, seed 880707 native on gfx1036) minimize to the same idiom. Caveat: two hard reductions stayed at 14 and 17 lines, where the pattern is present but not shown to be the cause, and the six programs with a hypothesis op (4 wave32, 2 wave64 native) are not explained.

GPUs tested: NVIDIA RTX 3080 (620.73.04) and AMD RDNA2 iGPU gfx1036 (RADV, Mesa 26.3.1), CachyOS Linux 7.3.5

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@morodirule

Copy link
Copy Markdown

Independent Linux check (stdlib only, no game/GPU): all four files pass on head 02d6293 — test_mask_as_data 9/9, test_salu64_bitwise_data 11/11, test_salu_movrels_lane_mask 3/3, test_scselect_b64_data 12/12. Same files against the unmodified tools fail 172+9+32+2 respectively, so the tests fully exercise the change. Not claiming; review evidence.

@KernelSmoker KernelSmoker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on exact head 02d6293 (base 96e1e22). Focused tests pass: new mask-as-data 9/9; updated SALU64, MOVRELS and SCSELECT regressions 11/11, 3/3 and 12/12; lane-mask, planner and pixel-scan unit suites also pass (including wave64 emulation 31/31). One additional SPIR-V validation test skipped because spirv-as/spirv-val are absent. The full semantic run covered 471 files / 6,174 tests; only test_sopc_validation and test_unary_f16_validation failed, both because those executables are missing, so I do not report the full suite as green. Changed-file no-game-data/diff checks and merge-tree pass. The added docs/design.md contract entry is present. CPU semantic validation only; no local GPU or game-data run.

@thesubtleties

Copy link
Copy Markdown
Contributor Author

Thanks @morodirule and @KernelSmoker. I pushed a second commit, so your checks cover 02d62939f and not the new head. It changes only how lane_masks.number_moves decides which v_mov_b32 copies are read as numbers: a VGPR-to-VGPR copy no longer counts as a mask read. Wave32 fuzz mismatches go from 10 to 0 (seed 880505). My earlier explanation of those 10 was wrong, and the description is corrected. AI-assisted (Claude Code).

@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 68b964e on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, and a GPU before/after with crosslane_fuzz

Runtime built with scripts\build_runtime.ps1 (MSVC 19.51; this PR changes no runtime file); llvm-mc 23.1.6, SPIRV-Tools 2026.4. No game files.

crosslane_fuzz.py --wave both --modes both --programs 300 --seed 20261123 (the same vk_compute_test), on this PR's base d4de3a7d and at this head. "Flagged" = programs reported as mismatch, including the hypothesis-op ones; every flagged program in both columns contains maskdata:

variant base: wave32 native / wave64 emulated mismatch (+hyp) flagged this PR flagged
plain 6 (+2) / 10 (+9) 27 1 (+0) / 6 (+1) 8
--dpp 6 (+3) / 10 (+6) 25 2 (+1) / 1 (+3) 7
--ext 6 (+2) / 3 (+6) 17 1 (+0) / 0 (+1) 2
total 69 17

The base counts are identical to a run of the same seed on main 867d922f the day before, so the drop is this change. 0 rejected and 0 harness errors in all runs. Still flagged at this head (seed 20261123, reproducible with --show):

  • plain: cs_xl32_2026173083373, cs_xl64_2026173083374, …396, …450, …524, …564, …632, …660
  • --dpp: cs_xl32_2026173083409, …425, …639, cs_xl64_2026173083512, …528, …580, …642
  • --ext: cs_xl32_2026173083373, cs_xl64_2026173083562

Also at this head: test_mask_as_data.py 13 OK, test_salu64_bitwise_data.py 11 OK, test_salu_movrels_lane_mask.py 3 OK, test_scselect_b64_data.py 12 OK (unittest discover). GPU suite: 261 shaders, 261 translated, 261 pass spirv-val; native 201/252, emulated 233/252; the only failures are cs_imgload_tfe / cs_imgload_d16_tfe, as on main on this card (#892).

@thesubtleties

Copy link
Copy Markdown
Contributor Author

More evidence on head 68b964e42; no code change.

tools/wave_diff.py --masks-as-data (CPU only, seed 551117, 2,000 programs per wave size), main 3aa97542 against this branch:

main this PR
wave32 104 mismatches 17
wave64 112 mismatches 11

The first remaining failure in each is a v_mov_b32 of a mask followed by a v_readfirstlane_b32 of that VGPR, the idiom this PR leaves alone. I did not check the other 26 one by one (that tool has no minimizer).

The wave64 crosslane_fuzz.py leftovers from the description are minimized now (seed 880606 emulated on the 3080, seed 880707 native on gfx1036): all 4 contain the same idiom, the shortest being v_cmp_ge_u32_e64 vcc, v6, v1 / v_mov_b32_e32 v5, vcc_hi / v_readfirstlane_b32 s8, v5. The description is updated to say so.

Not tested: the game corpus and RDNA2 console hardware. AI-assisted (Claude Code)

@MynFriend

MynFriend commented Nov 24, 2026 •

Copy link
Copy Markdown

Tested 68b964e42 on an RTX 3080 with --wave64-emulate. A synthetic loop counting four set bits should return 4 in every lane. GPU readback returned 32 in four lanes and 2 in the other 60. A local change that converts the lane mask to a scalar word before entering the loop returned 4 in all 64 lanes. Both modules passed Vulkan 1.2 validation. AI-assisted test, no game files used.

Local translator change. On entry to the scalar bit-scan loop, s8 represents the low half of a per-lane mask. On the back edge, s_mov_b32 s8, s14 carries an ordinary scalar after clearing one bit. The analysis drops the mask classification at the join. The first s_ff1_i32_b32 then sees a different value across lanes. The local fix gathers the low 32 predicate bits into one uniform word at the entry zero-test, while the source is still known to be a mask (Gen.lane_mask_word). It stores that word in the SGPR and treats the register as scalar on both outgoing paths and at the loop header. The fix was tested on an experimental translator branch with single-wave, 64-thread compute groups.

Reproduce on the PR head with Python only

From the repo root at 68b964e42, run the following Python. Each of the 64 output words should be 4. The interpreter prints Counter({2: 60, 32: 4}), matching the RTX 3080 readback.

from collections import Counter
from pathlib import Path
import sys

sys.path.insert(0, str(Path("tests/semantic").resolve()))
import test_wave64_emulate_opt as t

insns = [
    ('v_cmp_lt_u32_e64 s[8:9], v0, 4', 8),
    ('s_and_b64 s[8:9], s[8:9], exec', 4),
    ('s_mov_b32 s20, 0', 4),
    ('s_cmp_lg_i32 s8, 0', 4),
    ('s_cbranch_scc0 8', 4),
    ('s_ff1_i32_b32 s12, s8', 4),
    ('s_lshl_b32 s12, 1, s12', 4),
    ('s_xor_b32 s14, s12, s8', 4),
    ('s_add_u32 s20, s20, 1', 4),
    ('s_cmp_eq_i32 s8, s12', 4),
    ('s_cbranch_scc1 2', 4),
    ('s_mov_b32 s8, s14', 4),
    ('s_branch 65528', 4),
    ('v_mov_b32_e32 v6, s20', 4),
    ('v_lshlrev_b32_e32 v1, 2, v0', 4),
    ('buffer_store_dword v6, v1, s[0:3], 0 offen', 8),
    ('s_endpgm', 4),
]
asm, _ = t._translate_encoded("cs_lowword_bitloop", insns, (64, 1, 1), emulate=True)
mem, _ = t.run_emulated_workgroup(asm, (64, 1, 1), push_constants=t.VSHARP + [0] * 28)
print(Counter(mem[4*i] for i in range(64)))

For the GPU check, the same asm was assembled with spirv-as --target-env vulkan1.2 and validated with spirv-val --target-env vulkan1.2. The module was run with vk_compute_test scan.spv 1 inspect and VKC_DUMP set to an output file. All 64 little-endian uint32 values in the dump were counted.

@thesubtleties

Copy link
Copy Markdown
Contributor Author

Thanks @MynFriend, I reproduced this in the CPU interpreters at 68b964e42: wave64 native and emulated, and a wave32 version. Main 82a2aabc gives the same wrong values, so it isn't a regression, but this PR doesn't fix it. It's a mask meeting plain data at a join, where the analysis drops the mask.

While checking I found a straight-line relative: s_xor_b32 of a mask and plain data reads one lane's bit. I have a local prototype for that second case only (semantic suite clean, no GPU run yet). If you'd like to send your entry-test fix as its own PR, please do; I won't start on the loop case.

CPU only. AI-assisted (Claude Code)

@thesubtleties

Copy link
Copy Markdown
Contributor Author

Native RDNA2 (gfx1036, tools/hw_oracle): all 21 programs left over in the description (15 hard, 6 with a hypothesis op) match the fuzzer's model, so those are translator bugs, not model errors. Four short versions of the idiom also match. The cause is still shown only for the short reductions.

GPU suite on this head: the only failures are the two tfe fixtures fixed by #892. AI-assisted (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@thesubtleties

Copy link
Copy Markdown
Contributor Author

Rebased by merging main bfef489c; two conflicts, both independent additions. test_slot67_operand_forms loses an expectedFailure that this fixes.

Semantic suite: 488 files / 6,292 tests, 0 failed. GPU suite on the 3080 and gfx1036, native and emulated: per-test identical to main. Earlier approvals cover older heads. #1397 overlaps; I commented there. AI-assisted (Claude Code)

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
whoalin1 added a commit to whoalin1/leonida-continued that referenced this pull request Nov 25, 2026
…er reads

Review fixes for the mask-as-data change:

- The wave-mask gather is a group op over the lanes EXEC leaves active,
  and drivers differ on whether lanes EXEC has off still vote: NVIDIA,
  AMD and KosmicKrisp return the whole subgroup's bits, MoltenVK's
  ballot/reduce saw only the lanes a narrowed EXEC left on and produced
  the EXEC value instead of the frozen mask word (cs_mask_vgpr_exec).
  mask_word_read_ok now gathers only when a CFG must-analysis
  (mark_exec_state) proves every lane that can hold a set bit of the
  mask is EXEC-active at the read: each EXEC half is tracked as clean,
  a constant mask, or an unknown narrowing (s_mov/s_cselect resolve
  constants, saved-EXEC aliases and unchanged EXEC, so the common
  restore idioms still gather), and mask_exec_sigs records every EXEC
  state seen while each mask register held a mask. Unproven reads keep
  this lane's 0 / ~0 bit, the pre-change answer.
- Scalar number ops read a lane mask operand as data too
  (MASK_NUMBER_READERS, ProjectLeonida#1043's NUMBER_READERS): s_add_u32 s20, s10, K
  after v_cmp wrote s10 yields the wave's mask word, while mask moves
  and mask logic ops keep this lane's bit for the saveexec idioms.
- wave64_emulate_opt treats ins["mask_words"] (a mask read that will
  gather) as a cross-lane exchange for the lane-local fold planner and
  barrier scheduler, covering the scalar readers as well.
- test_slot67_operand_forms' expectedFailure comes off: the packed
  vcc_hi word now reaches the data read.

Co-Authored-By: Devin <noreply@cognition.ai>
whoalin1 added a commit to whoalin1/leonida-continued that referenced this pull request Nov 25, 2026
…er reads

Review fixes for the mask-as-data change:

- The wave-mask gather is a group op over the lanes EXEC leaves active,
  and drivers differ on whether lanes EXEC has off still vote: NVIDIA,
  AMD and KosmicKrisp return the whole subgroup's bits, MoltenVK's
  ballot/reduce saw only the lanes a narrowed EXEC left on and produced
  the EXEC value instead of the frozen mask word (cs_mask_vgpr_exec).
  mask_word_read_ok now gathers only when a CFG must-analysis
  (mark_exec_state) proves every lane that can hold a set bit of the
  mask is EXEC-active at the read: each EXEC half is tracked as clean,
  a constant mask, or an unknown narrowing (s_mov/s_cselect resolve
  constants, saved-EXEC aliases and unchanged EXEC, so the common
  restore idioms still gather), and mask_exec_sigs records every EXEC
  state seen while each mask register held a mask. Unproven reads keep
  this lane's 0 / ~0 bit, the pre-change answer.
- Scalar number ops read a lane mask operand as data too
  (MASK_NUMBER_READERS, ProjectLeonida#1043's NUMBER_READERS): s_add_u32 s20, s10, K
  after v_cmp wrote s10 yields the wave's mask word, while mask moves
  and mask logic ops keep this lane's bit for the saveexec idioms.
- wave64_emulate_opt treats ins["mask_words"] (a mask read that will
  gather) as a cross-lane exchange for the lane-local fold planner and
  barrier scheduler, covering the scalar readers as well.
- test_slot67_operand_forms' expectedFailure comes off: the packed
  vcc_hi word now reaches the data read.

Co-Authored-By: Devin <noreply@cognition.ai>
@ammonium231

Copy link
Copy Markdown

RTX 3090 / 621.40, crosslane fuzz with maskdata, seed 309700: mismatches go 56+29 -> 12+9 (w32+w64) with this PR. Remaining are still mask-as-data, e.g. cs_xl32_30970929176 (native, group 50):

minimized repro
v_cmp_gt_u32_e64 s18, v7, v7
s_mov_b32 exec_lo, 0x110c5
v_mov_b32_e32 v6, s18
v_readfirstlane_b32 s14, v6

s14: expected 0x00000000; threads 0-31 got 0xfffeef3a, threads 32-49 got 0x0002ef3a.

@ammonium231

Copy link
Copy Markdown

Fix for the remaining mismatches: thesubtleties:mask-as-data...ammonium231:fix-1043-number-moves (on top of this PR, 4 files, +123/-10). number_moves dropped a plain v_mov of a mask when a later v_readfirstlane / v_and read the copy, so inactive lanes leaked into the mask. RTX 3090 fuzz, 2000 programs/seed, --dpp --ext: 309700 984/12/4 + 989/9/2 -> 1000/1000 both waves; held-out 309900 (36 failing) and 310100 (29 failing) -> 0, no new failures. Semantic suite 6296/0.

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Checked 4d58a05 merged into main 2859d0c (merges cleanly), by Claude Code (AI-assisted) on my behalf. Linux,
Mesa lavapipe, --wave64-emulate.

Not run: a hardware GPU, native wave32.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants