Skip to content

rdna2spirv: a VALU data read of a lane-mask operand yields the wave's mask word - #1397

Open
whoalin1 wants to merge 5 commits into
ProjectLeonida:mainfrom
whoalin1:mask-regs-data-read
Open

whoalin1 wants to merge 5 commits into
ProjectLeonida:mainfrom
whoalin1:mask-regs-data-read

Conversation

@whoalin1

@whoalin1 whoalin1 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #14 (comment)

Depends on: #953

What

A lane mask (EXEC/VCC or a mask-tracked SGPR) that an instruction reads as a 32-bit value — for example v_mov_b32 of it into a plain VGPR, or a scalar number reader such as s_add_u32 s20, s10, K — must produce the wave's mask word. Hardware SGPRs hold that wave-wide word; the translator previously produced each lane's own 0/~0 bit in the VALU path.

This pull request now:

  • materializes the wave mask word for lane-mask data reads (src_u / lane_mask_word), while mask consumers keep extracting the lane bit through mask_src;
  • keeps word-materializing readers out of lane-local if (active) runs, since the gather is a cross-lane operation;
  • handles scalar mask number readers and mask-holding VGPR copies/reads;
  • gathers the word even when EXEC is narrowed. EXEC narrowing is data in the translator's model, not SPIR-V control flow. The earlier EXEC-coverage gate could not prove independent-SGPR restore idioms and still produced the wrong per-lane bit for the narrowed-EXEC fuzz family, so the gate and its CFG provenance analysis are removed;
  • includes the eager wave-mask store from rdna2spirv: the wave mask gather is stored before it is read, so every lane runs it (SHADER-WAVEMASK-EAGER) #953 (authorship preserved) because it is the mechanism that keeps lowering from moving the subgroup gather into an EXEC-selected arm on MoltenVK/SPIRV-Cross.

docs/design.md records the contract. Semantic coverage includes narrowed SGPR/VCC reads, independent all-ones SGPR EXEC restore, EXEC round trips, scalar readers, vector-bitwise mask composition, native/emulated wave64 structure, and the eager-gather shape.

Tested

  • python -I scripts/run_semantic_tests.py tests/semantic — 488 files, 6298 tests, 0 files failed; compileall clean; tests/test_lane_masks.py passed.
  • GPU, NVIDIA RTX 4070 SUPER, Windows 11 (VK_LOADER_DRIVERS_SELECT=*nv-vk*): tools/crosslane_fuzz.py --programs 200 --seed 7 --wave 32 --modes native — 199 match / 1 mismatch / 0 hypothesis-only versus 186 / 9 / 5 at ba315fbf1. The remaining cs_xl32_700032 minimizes to an inactive-EXEC write-predication case (v_cmpx + maskdata v_mov + v_xor), distinct from the narrowed-EXEC mask-word family and already present before this change.
  • NVIDIA seed 102 (--programs 106) � 105 match / 1 mismatch / 0 hypothesis-only versus 100 / 3 / 3 at ba315fbf1. The remaining cs_xl32_10200411 is an EXEC-zero/readfirstlane control-flow case. AMD evidence is reported in the pull-request comments; no game files were used.

AI-assisted (SWE-2.1 Max).

@GeorgiyDemo

Copy link
Copy Markdown
Contributor

Tested 3ec97f53e on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 364ab4eab (translator-only PR; binaries from the build at 66d4f5aa3, GPU test C++ unchanged in between):

  • The four updated semantic files pass at this head; copied onto main they fail: test_lane_masks_vector_bitwise 4, test_salu64_bitwise_data 9, test_salu_movrels_lane_mask 32, test_scselect_b64_data 2 failures — the coverage claim holds.
  • GPU regression, MoltenVK 1.4.2: --filter mask 6/12 passed, 5 skipped, 1 failed; --filter exec 4/8, 3 skipped, 1 failed. The failure is cs_mask_vgpr_exec: 15 15 15 15 3735928559 … where the fixture expects 4294967295 4294967295 … (SHADER-21 part 2 fixture from 72d7e52df). With main's tools/rdna2spirv.py in the same worktree and the same binary it PASSes; reproduced 3/3 runs.
  • KosmicKrisp (Mesa 26.3.1): cs_mask_vgpr_exec PASSes with both this head and main's translator — the same SPIR-V yields 0xFFFFFFFF there and 0xF on MoltenVK.
  • Root cause of the split: the new src_u data-read path calls lane_mask_word → wave_mask, which rebuilds the mask with an OpGroupNonUniformBallot of this lane's bit. The fixture does v_cmp_lt_u32 vcc_lo … (VCC = 0xFFFFFFFF), s_mov_b32 exec_lo, 0xF, then v_mov_b32 v2, vcc_lo; on MoltenVK the ballot only covers the four invocations active under that EXEC and returns 0xF, while the hardware VCC word is 0xFFFFFFFF. KosmicKrisp returns the full subgroup mask and hides it.

So at least on MoltenVK this is a regression against an existing GPU test: a mask word read as data cannot be reconstructed by a ballot that the current EXEC restricts.

AI-assisted; synthetic fixtures only, no game data.

@whoalin1
whoalin1 force-pushed the mask-regs-data-read branch from 3ec97f5 to b9cea5a Compare November 25, 2026 00:15
@ecox982745

Copy link
Copy Markdown

I tested the mask word and the EXEC write mask independently on an RTX 4090, Windows 11, NVIDIA 616.64, with a 32-lane compute subgroup. At head b9cea5af, 25/25 dispatches matched every lane; the same inputs matched in 9/25 at parent 1154976d.

The existing cs_mask_vgpr_exec fixture passed on both revisions here. I added four local synthetic probes, each run with six pairs of masks supplied at dispatch. They read either vcc_lo or a compare-written SGPR, through either v_mov_b32 or v_xor_b32. A distinguishing case was a comparison setting the odd lanes (0xaaaaaaaa) followed by EXEC selecting the even lanes (0x55555555). On the head every even lane received the full mask word on the move, and the odd lanes kept their sentinel. On the parent every even lane received zero.

For comparison with the MoltenVK report, the original fixture produced byte-identical SPIR-V from 3ec97f53 and b9cea5af with my default build (SHA-256 b675f09a98b92c423c6706e5a864dbcd555afe0ae43ad7606aab33a609d899be). My module materializes the mask with two OpGroupNonUniformBitwiseOr … Reduce instructions and has no OpGroupNonUniformBallot. The fixture also passed with LEONIDA_PC_TAIL_UBO=1. Comparing the subgroup operations in the failing MoltenVK module would help narrow down the driver difference; this NVIDIA run leaves that failure open.

Both revisions' five modules passed Vulkan 1.2 SPIR-V validation. I built the compute harness from the PR head. Separately, all 33 tests in the four changed CPU semantic test files passed on the head. AI-assisted verification.

@tiguan26

Copy link
Copy Markdown
Contributor

Merged b9cea5a into current main (bfef489): no conflicts. The four test files this PR touches pass, and the changed expectations look right to me: a mask SGPR read by a VALU op is the 32-bit word on hardware, not the lane's 0 / ~0.

The full translator suite (Linux, Python 3.13) has one new failure, and it's a good one: tests/semantic/test_slot67_operand_forms.py::test_wave32_compare_high_word_preserves_low_and_clears_inactive_bits is an unexpected success. That test is marked @unittest.expectedFailure with a comment saying hardware copies the packed word (s_mov_b32 s8, vcc_hi -> 0x80000004) while main gives each lane its own mask value, which is what this PR fixes. With the marker left in, the suite reports FAILED (unexpected successes=1). Dropping @unittest.expectedFailure and that comment's "fails there" sentence should make it green. Otherwise only test_int32_to_f64 fails, as on main here (a spirv-opt crash in my environment).

AI-assisted (Claude).

@thesubtleties

Copy link
Copy Markdown
Contributor

I'm the author of #1043 (SHADER-MASK-AS-DATA), which overlaps. I ran both on main bfef489c, RTX 3080 and gfx1036.

Your branch (b9cea5afb) handles the copy-then-v_readfirstlane idiom that mine leaves out, and flags fewer fuzz programs: 12 here, 22 with #1043, 97 on main (five crosslane_fuzz runs).

Two gaps:

  1. 6 of those 12 are programs main translates but this branch doesn't under --wave64-emulate, e.g. cs_xl64_2026173083558 (seed 20261123, plain): "OpControlBarrier inside a folded lane-local region". The planner in wave64_emulate_opt.py needs to count these reads as exchanges; rdna2spirv: a lane mask read as a number is the wave's mask word (SHADER-MASK-AS-DATA) #1043 does it at three sites, feel free to reuse it.
  2. Scalar readers (s_add_u32, s_mul_i32, shifts...) still see one lane's bit; test_mask_as_data.py in rdna2spirv: a lane mask read as a number is the wave's mask word (SHADER-MASK-AS-DATA) #1043 covers them.

test_slot67_operand_forms also needs its expectedFailure removed, for both of us. They look complementary; whichever merges first, I'm happy to rebase. AI-assisted (Claude Code)

@levi2m

levi2m commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Another card, native Windows: NVIDIA RTX 4050 Laptop (Ada, 10de:28a1), driver 621.01, Windows 11 build 26200, subgroup 32..32. No game files, nothing online.

tools/crosslane_fuzz.py, 200 programs per run, seeds 101, 102 and 103, on main 3ff2a2ad with vk_compute_test built from it. The "this PR" runs use only the tools/ part of this diff (head b9cea5af) through --translator.

main main + this PR
wave32 native, 600 programs 49 mismatch, 20 hypothesis-only 1 mismatch, 0 hypothesis-only
wave64 --wave64-emulate, 600 programs 27 mismatch, 20 hypothesis-only 0 mismatch, 0 hypothesis-only, 21 not translated

Every mismatching program on main includes the maskdata op. The fix is confirmed on this card, and no harness errors on either side.

Two things this PR leaves, for the record:

  1. 21 of 600 wave64-emulate programs no longer translate (9, 8 and 4 for the three seeds; main translated all of them). I translated one by hand, seed 101 index 316 (cs_xl64_10100316, ops bcnt, branch, cmp, exec, mask_salu, maskdata, mbcnt, mbcnt_data, permlane, readfirstlane, readlane, valu, writelane): AssertionError: wave64 emulation: x_split inside a folded lane-local region, from emit_folded_region. This is the same class as the OpControlBarrier inside a folded lane-local region case in @thesubtleties' comment above, with a different assertion text.
  2. One wave32 mismatch remains, seed 102 index 411 (cs_xl32_10200411, also a mismatch on main): s13 is expected to be the same mask word in every lane (0xb945de14) and comes out per lane (0 / 0xffffffff). It reads a mask held in a scalar register, which may be the scalar follow-up mentioned in the PR notes; I did not trace it.

The other fuzzers on main, same card, three seeds each, show nothing: lds_fuzz (400 native and 300 emulated programs per seed) and valu_fuzz f16 and mem (300 each) have 0 mismatches, and image_fuzz matches 300 of 300 per seed.

AI-assisted: yes

@whoalin1
whoalin1 force-pushed the mask-regs-data-read branch from b9cea5a to 8ece74e Compare November 25, 2026 08:15
@whoalin1

Copy link
Copy Markdown
Contributor Author

Pushed 8ece74e2a addressing the review findings (EXEC-coverage gating for mask-word gathers incl. the MoltenVK cs_mask_vgpr_exec ballot-under-EXEC case, scalar mask-number reads, and the wave64-emulate folded-region gaps).

Independent GPU verification on this head (RTX 4070 SUPER + AMD Radeon gfx1036 iGPU, LEONIDA_VK_VALIDATION=1):

  • cs_mask_vgpr_exec passes on both GPUs; --filter mask/exec curated shaders: AMD 12/12 + 8/8, NVIDIA same modulo wave64 skips; with LEONIDA_W64_EMULATE=1 on NVIDIA all 20 pass.
  • crosslane_fuzz seeds 101-103, 100 programs/seed, wave 64, modes plain / --ext / --ext --dpp: 0 mismatches, 0 rejected on both GPUs (baseline main had ~6-11 mismatches per 100-program run).
  • Semantic suite: 487 files, 6,289 tests, 0 failures.

AI-assisted work.

@ecox982745

Copy link
Copy Markdown

8ece74e2a fixes the reported wave64 translation failure on my RTX 4090. I also found a wave32 regression relative to its immediate parent 142293b30: when EXEC comes from a register, a vector read of a previously computed mask can return just the current lane’s bit.

With all 32 lanes active, v_cmp_ne_u32_e64 s8, v5, 0 sets s8 to 0x05010204 (v5 contains the corresponding bit in each lane). Then set EXEC and run v_mov_b32_e32 v2, s8. Both variants below have identical runtime EXEC and use the same input and harness.

EXEC write on 8ece74e2a Lanes returning 0x05010204 Values read
s_mov_b32 exec_lo, -1 32/32 0x05010204
s_mov_b32 exec_lo, s4, with s4 = 0xffffffff 0/32 0 or 0xffffffff

Across the previous 25-dispatch test, the parent matched 800/800 lane results. The new head had 224 wrong lane results across 16 dispatches. This includes VCC and compare-written s8, with both move and XOR consumers. The unchanged cs_mask_vgpr_exec fixture passed. The literal/register pair above would make a regression test for the fallback in mask_word_read_ok.

I also replayed the two exact programs from the RTX 4050 report against the fuzzer’s CPU reference:

  • cs_xl64_10100316, emulated wave64: parent refused translation. Head translated, passed spirv-val, and matched all 896 checked words.
  • cs_xl32_10200411, native wave32: 64/896 words still differ on both revisions. The first expected 0xb945de14 and read zero. This is program 106 for seed 102 with a single wave size and the default 24 operations, beyond the reported 100-program sweep.

Windows, NVIDIA 616.64, LLVM 23.1.6. I rebuilt vk_compute_test at 8ece74e2a and reran both revisions with it. The 16 tests in the two changed semantic test files passed on CPU. AI-assisted verification using synthetic inputs.

@whoalin1

Copy link
Copy Markdown
Contributor Author

Thanks ecox982745 — confirmed reading. The s_mov_b32 exec_lo, s4 (register-sourced EXEC) vs literal -1 divergence points at the mask_word_read_ok fallback treating a register-written EXEC mask as lane-local; a literal-vs-register EXEC pair is exactly the regression test we'll add. We're reproducing on the RTX 4070 SUPER + AMD gfx1036 iGPU, will fix on this branch, and re-verify both revisions' 16-dispatch sweep. Noted on cs_xl32_10200411 (seed 102 prog 106, native wave32, 64/896 words on both revisions) — tracking separately as it predates this fix. AI-assisted.

@Gefik23

Gefik23 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tested 8ece74e with tools/crosslane_fuzz.py on NVIDIA GeForce RTX 4060 (Ada), driver 617.42, Windows 11 Pro build 26200, native Windows. llvm-mc 21.1.8 (scripts\build_llvm_mc.ps1), vk_compute_test from main 3f81d46, --translator pointed at this PR's tools/rdna2spirv.py.

run main 3f81d46 this PR
--programs 50 --seed 1 45 match, 4 mismatch, 1 hyp_only 49 match, 0 mismatch, 1 hyp_only
--programs 200 --seed 7, wave32 native 89 match, 9 mismatch, 2 hyp_only 91 match, 7 mismatch, 2 hyp_only
--programs 200 --seed 7, wave64 emulate 92 match, 6 mismatch, 2 hyp_only 100 match, 0 mismatch
--programs 50 --seed 1 --no-maskdata (main only) 50 match

So every wave64 mismatch on these seeds is gone. The 7 wave32 mismatches left (cs_xl32_7000{87,135,151,153,185,197,205}) also fail on main, so they are not regressions. With --minimize they all come down to a mask computed under one EXEC and read as data under a narrower one, which looks like the same family as the register-EXEC case above. The two shortest:

; cs_xl32_700087, group 20 (one partial wave32)
v_cmp_le_u32_e64 s18, v5, v7
s_mov_b32 exec_lo, 0x7ec2b
v_mov_b32_e32 v5, s18          ; expected 0x000fffff, got 0xffffffff
; cs_xl32_700152, group 20
v_cmp_ne_u32_e32 vcc_lo, 53, v3
v_cmpx_lt_u32_e32 9, v3
v_mov_b32_e32 v7, vcc_lo       ; expected 0x000fffff, got 0xffffffff

In both cases the bits of lanes 20..31, which are not in the group, read back as set. Full-wave groups fail the same way, for example cs_xl32_700032 (group 64): v_cmp_gt_u32_e64 s16, v2, v4 / v_cmpx_gt_u32_e32 31, v3 / v_mov_b32_e32 v7, s16. To reproduce: python -I tools/crosslane_fuzz.py --programs 200 --seed 7 --wave 32 --translator <this PR>/tools/rdna2spirv.py --minimize.

AI-assisted (Claude Code ran the fuzzer and the reductions). No game files.

whoalin1 added a commit to whoalin1/leonida-continued that referenced this pull request Nov 25, 2026
…R round-trips

Review follow-up on the mask-word gather gate (ProjectLeonida#1397): `s_mov_b32 s4,
exec_lo` under an unknown EXEC narrowing recorded nothing for s4, so the
later `s_mov_b32 exec_lo, s4` restore minted a fresh ("n", id) tag in
_exec_src_ex. mask_exec_sigs coverage then failed and the read kept this
lane's 0 / ~0 bit, while the equivalent literal `s_mov_b32 exec_lo, -1`
resolved to a constant half-state and gathered — register and literal
restores of the same runtime value disagreed.

_mov_sgpr_vals now returns an ("al", halves) alias for an EXEC source
whose half-state is an unknown narrowing, the same scheme saveexec uses:
exec_lo gives (state, None), exec_hi (None, state), exec gives each half
in its own register for b64 moves. _exec_src_ex already resolves those
aliases, so the restore reproduces the original tag and the gather stays
exact; untracked SGPR sources still mint a fresh narrowing and stay
conservative.

Tests: two semantic cases (wave32 and wave64) assert the register
round-trip gathers OpGroupNonUniformBitwiseOr identically to the literal
-1 restore; cs_exec_copy.asm (+ fixture, check.py/run_gpu_tests.py dump
entries) stores the mask word, exec_lo's own word and the literal-restore
word side by side on real hardware — PASS on RTX 4070 and Radeon
(64-wide), wave64 exec filters 9/9 on AMD.

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

Copy link
Copy Markdown
Contributor Author

Pushed ba315fbf1 fixing the wave32 register-EXEC regression @ecox982745 reported.

Root cause: Gen._mov_sgpr_vals handled exec/exec_lo/exec_hi sources only under clean/constant EXEC half-state — under an unknown narrowing (("n", tag)) it recorded nothing, so s_mov_b32 exec_lo, s4 minted a fresh tag and mask_word_read_ok denied the mask-word gather (per-lane 0/0xffffffff). The fix aliases the SGPR to the narrowing tag using the same ("al", ...) scheme s_*_saveexec already uses, which _exec_src_ex resolves — literal and register EXEC restores now behave identically.

New coverage: 2 semantic tests (wave32 + wave64 EXEC round-trip asserting the OpGroupNonUniformBitwiseOr gather) + cs_exec_copy GPU fixture storing mask word / exec_lo word / literal-restore word side by side.

Verification (RTX 4070 SUPER + AMD gfx1036 iGPU, LEONIDA_VK_VALIDATION=1): semantic suite 487 files / 6,291 tests / 0 failed; exec+mask filters and cs_exec_copy PASS on both GPUs with identical words; literal-vs-register now agree (out[0]==out[2]). Independent review APPROVEd (tests confirmed to fail on the parent translator). Re-running the narrowed-EXEC fuzz repros (cs_xl32_700087/700152/700032 family) to confirm coverage — will follow up. AI-assisted.

@ecox982745

Copy link
Copy Markdown

I verified ba315fbf1 against its parent 8ece74e2a on the RTX 4090. The saved-EXEC round trip is fixed: the new cs_exec_copy fixture went from 32 wrong output words on the parent to 128/128 correct on the head. The original case from my earlier report still fails when EXEC is set from an independent SGPR after the comparison.

Here's a small way to reproduce that distinction using the PR's own cs_exec_copy.asm. Keep the expected outputs and the rest of the fixture as written:

 v_mov_b32_e32 v7, -1
 v_readfirstlane_b32 s7, v7
-s_mov_b32 exec_lo, s7
 v_and_b32 v1, 1, v0
 v_cmp_ne_u32_e64 s8, v1, 0
-s_mov_b32 s4, exec_lo
-s_mov_b32 exec_lo, s4
+s_mov_b32 exec_lo, s7
 v_mov_b32_e32 v2, s8

EXEC is full when the comparison defines s8 and is full again when the move reads it. With the edit, all 32 lanes get the first output word wrong: even lanes return zero, odd lanes return 0xffffffff, and all should return 0xaaaaaaaa. The other three fields match in every lane, including the EXEC word and the mask read after the literal restore.

The original five synthetic modules are byte-identical across these two commits. Re-running their 25 dispatches gave the same 224/800 wrong lane results across 16 dispatches. A diagnostic that reads the complete mask into a VGPR immediately after the compare, before changing EXEC, matched all 96 lane results across three full and partial EXEC patterns. This gives a passing control for investigating where the complete mask word can be preserved.

Native wave32, Windows 11, NVIDIA 616.64, LLVM 23.1.6. The edited fixture passed Vulkan 1.2 SPIR-V validation. All 16 CPU tests in test_lane_masks_vector_bitwise.py passed on the head. AI-assisted verification.

@hugo0024

Copy link
Copy Markdown
Contributor

Tested ba315fbf1 merged onto main bcaa13dad on an Apple M5 (MoltenVK 1.4.2, macOS 27.2.1), with the test programs built from that main. AI-assisted.

The fixtures on MoltenVK

run_gpu_tests.py --filter mask: 7/12 passed, 5 skipped. --filter exec: 6/9 passed, 3 skipped. cs_mask_vgpr_exec and the new cs_exec_copy pass; every skip is "no 64-wide compute subgroup on this device".

crosslane_fuzz.py --programs 200 --seed 31008 --wave 32 --dpp --ext (match / mismatch / hypothesis only)

Translator as it is with #953
main 168 / 25 / 7 175 / 20 / 5
b9cea5afb 200 / 0 / 0
8ece74e2a 180 / 16 / 4 188 / 10 / 2
ba315fbf1 180 / 16 / 4 188 / 10 / 2

#953 is the whole-wave mask gather that MoltenVK needs stored, so the right-hand column is the one to read on this driver. The b9cea5afb row was measured earlier today on main 3f81d46ac; the translator on main changed once since then (#1493).

The 12 programs left at ba315fbf1 are the same 12 as at 8ece74e2a, and all are the family @Gefik23 and @ecox982745 describe, a mask read by a VALU op after EXEC changed:

; group 50
v_cmp_ne_u32_e64 s20, v2, v4
s_mov_b32 exec_lo, 0x40f8
v_mov_b32_e32 v3, s20        ; active lanes of wave 1: expected 0x0003ffff, got 0xffffffff

What this run adds: all 12 matched at the first head, b9cea5afb. For the three I looked at (cs_xl32_3100893037, ..125, ..134), b9cea5afb emits two OpGroupNonUniformBitwiseOr and the two later heads emit no subgroup operation at all, as main does. So this family is not a MoltenVK effect, and the first head had it right.

wave64 emulate, --programs 150 --seed 31009 --wave 64 --modes emulate --dpp --ext: main 144 / 5 / 1, ba315fbf1 149 / 1 / 0, nothing rejected. The one left fails on main too, cs_xl64_3100993145 (group 40), minimised:

s_mov_b64 exec, s[20:21]     ; s[20:21] = 0: every lane is off
v_mov_b32_e32 v2, vcc_lo
v_xor_b32_e32 v2, v2, v2     ; v2 expected unchanged, got 0 in all 40 lanes

Not tested: any other GPU, and the game corpus.

@EvgHaze

EvgHaze commented Nov 25, 2026

Copy link
Copy Markdown

Apple check of the new cs_exec_copy fixture, on both drivers.

Machine:  Apple M3, 10-core (MacBook Air 13", Mac15,13), macOS 27.0 (26A5406e), arm64
Drivers:  MoltenVK 1.4.2 (Homebrew), ICD loaded directly, subgroup 4..32
          KosmicKrisp, Mesa 26.3.1 (Homebrew), through the Vulkan loader, subgroup 32..32
Base:     main `0d80bc5` + this diff (clean `git apply`)

scripts/run_gpu_tests.py --filter cs_exec — identical words on both drivers, 3/3 passed:

Test Words
cs_exec64 0 0 0 0 0 0 0 0
cs_exec_const 16 8 2 16 0 16
cs_exec_copy (new) 2863311530 4294967295 2863311530 0 2863311530 4294967295 2863311530 1

python3 -I scripts/run_semantic_tests.py tests/semantic → 489 files, 6300 tests, 0 failed (main is 6288, so this diff adds twelve), 143 s.

Both Apple drivers run these at subgroup size 32 natively, so the lane-mask-as-data read is not being emulated in either run. MoltenVK's SPIRV-Cross path and KosmicKrisp's nir_to_msl path agree word for word.

AI-assisted. No game data: hand-written fixtures, and the numbers above are test output.

@GeorgiyDemo

Copy link
Copy Markdown
Contributor

Follow-up: the regression I reported at 3ec97f53e (cs_mask_vgpr_exec: 15 15 15 15 … on MoltenVK) is gone at ba315fbf1 on the same machine where it reproduced — Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), MoltenVK 1.4.2, binaries from the build at 66d4f5aa3:

  • cs_mask_vgpr_exec PASS: 4294967295 4294967295 4294967295 4294967295 3735928559 3735928559 3735928559 3735928559 on MoltenVK and the same on KosmicKrisp (Mesa 26.3.1).
  • --filter mask: 7/12 passed, 5 skipped, 0 failed (all skips are "no 64-wide compute subgroup on this device").
  • --filter cs_exec: 3/3 PASS, including the new cs_exec_copy (2863311530 4294967295 2863311530 0 …), same words as the M3 run.
  • The four semantic files (test_lane_masks_vector_bitwise, test_salu64_bitwise_data, test_salu_movrels_lane_mask, test_scselect_b64_data): OK.

So this is fixed on macOS 26.7.3 as well, not just on the 27.x runs above.

AI-assisted; synthetic fixtures only, no game data.

@aslan2142

Copy link
Copy Markdown
Collaborator

Thanks, @whoalin1. The saved-EXEC round trip is fixed. Open points:

AI-assisted (Claude Code, Claude Opus 5.5).

@ecox982745

Copy link
Copy Markdown

I compared #1043 (4d58a05d6) and #1397 (ba315fbf1) for the independent-EXEC case in the review above. I merged each PR separately onto main at 9e7720c9e locally and ran the same synthetic wave32 shaders on an RTX 4090, Windows 11, NVIDIA 616.64. Both merges were clean. This is GPU readback using the same executable for all three builds.

GPU check main main + #1043 main + #1397
Saved EXEC, original cs_exec_copy (correct output words / 128) 64 128 128
Independent SGPR, edited fixture (correct first mask word / 32 lanes) 0 32 0
Independent EXEC, v_mov_b32 (dispatches passing / 12) 4 12 4
Independent EXEC, v_xor_b32 (dispatches passing / 12) 4 4 4

The move and XOR rows each cover six runtime mask pairs for each of two sources, VCC and compare-written s8. Adding the stock cs_mask_vgpr_exec control gives 9/25, 17/25 and 9/25 passing dispatches respectively. Across those 25 dispatches, #1043 reduced incorrect lane values from 224 to 112, all in XOR. Every module passed Vulkan 1.2 SPIR-V validation, and every readback guard was intact.

For the overlapping fixes, #1043's handling of v_mov_b32 already covers both EXEC fixtures here. The XOR row gives a separate regression target for the remaining arithmetic use of a mask. This comparison covers native wave32 on this GPU. AI-assisted verification with OpenAI Codex.

@whoalin1

Copy link
Copy Markdown
Contributor Author

Pushed 52fb23042. This reworks the narrowed-EXEC side rather than extending the coverage proof:

  • includes rdna2spirv: the wave mask gather is stored before it is read, so every lane runs it (SHADER-WAVEMASK-EAGER) #953's eager wave-mask gather (hugo0024's authorship preserved), because the gather must not be hidden inside an EXEC-selected arm by lowering;
  • removes the EXEC-coverage gate and its CFG provenance analysis. EXEC narrowing is per-lane data in this model, not SPIR-V control flow; lanes off in EXEC still hold bits of the wave-wide mask register and must contribute them;
  • adds semantic coverage for narrowed SGPR/VCC reads, an independent all-ones SGPR EXEC restore, and vector-bitwise mask composition.

Verified here (no game files):

  • run_semantic_tests.py tests/semantic: 488 files, 6298 tests, 0 files failed; compileall, tests/test_lane_masks.py, and check_no_game_data passed.
  • NVIDIA RTX 4070 SUPER, Windows 11: crosslane_fuzz.py --programs 200 --seed 7 --wave 32 --modes native 199 match / 1 mismatch / 0 hyp-only (was 186 / 9 / 5 at ba315fbf1); seed 102, 106 programs 105 / 1 / 0 (was 100 / 3 / 3).
  • AMD Ryzen 5 7600 RDNA2 iGPU gfx1036, driver 24.10.24, Windows 11, pc-tail UBO harness: seed 7 199 / 1 / 0, exactly the same single residual program as NVIDIA.

The residual is cs_xl32_700032, minimized to v_cmp + v_cmpx + maskdata v_mov + v_xor; it is an inactive-EXEC write-predication case that already mismatched before this work, not the narrowed-but-nonzero EXEC mask-word family. cs_xl32_10200411 also remains in the seed-102 stream and starts with exec_lo = 0, a separate no-active-lane/readfirstlane control-flow case.

On overlap: #1043 currently fixes the independent-EXEC v_mov cases but not arithmetic v_xor in ecox982745's comparison. This head is intended to cover mask data reads uniformly; I have not merged or duplicated #1043's follow-forward VGPR-copy analysis.

AI-assisted (SWE-2.1 Max).

@EvgHaze

EvgHaze commented Nov 25, 2026

Copy link
Copy Markdown

Apple run of the new head 52fb23042, on both drivers.

Machine:  Apple M3, 10-core, macOS 27.0 (26A5406e), arm64
Drivers:  MoltenVK 1.4.2 (driver id 14, subgroup 4..32)
          KosmicKrisp, Mesa 26.3.1 (driver id 28, subgroup 32..32)
Base:     main `0d80bc5` + this head, clean `git apply`
Check MoltenVK KosmicKrisp
--filter cs_exec 3/3 PASS 3/3 PASS
cs_exec_copy words 2863311530 4294967295 2863311530 0 2863311530 4294967295 2863311530 1 identical
--filter mask (non-64-wide) all PASS —
cs_mask_vgpr_exec 4294967295 4294967295 4294967295 4294967295 3735928559 3735928559 3735928559 3735928559 —
semantic suite 490 files, 6307 tests, 0 failed —

Two things worth stating plainly:

  1. The rework did not move the saved-EXEC words on Apple — they are the same eight that @ecox982745 used as the correct row in the rdna2spirv: a lane mask read as a number is the wave's mask word (SHADER-MASK-AS-DATA) #1043 comparison, so this head still passes that case on a 32-wide host.
  2. The independent-SGPR and independent-EXEC cases in that table are not repo fixtures — they come from an edited cs_exec_copy, so my run does not cover them and I am not claiming it does. If you paste that fixture or point at a branch with it, I will run it on this machine in the same two drivers and add the row.

AI-assisted. No game data.

@ecox982745

Copy link
Copy Markdown

@EvgHaze, here are the synthetic fixtures for the Apple run. On the RTX 4090, 52fb23042 passes 25/25 dispatches (800/800 lane results), up from 9/25 on ba315fbf1. The independent-EXEC move and XOR rows both pass 12/12. The separate saved-EXEC and independent-SGPR fixtures each pass 128/128 output words. All readback guards were intact. This closes the two failing rows from my earlier comparison on this GPU.

I ran the PR head directly with native wave32 on Windows 11, NVIDIA 616.64. I also translated all seven programs after merging this head onto the same main (9e7720c9e) used in the comparison. All seven SPIR-V modules matched the ones dispatched byte for byte and passed Vulkan 1.2 validation.

Independent SGPR fixture and all-lane check

Save as runtime/test/shaders/cs_exec_copy_independent.asm.

; group: 32,1,1
; mattr: +wavefrontsize32,-wavefrontsize64
v_mov_b32_e32 v7, -1
v_readfirstlane_b32 s7, v7
v_and_b32 v1, 1, v0
v_cmp_ne_u32_e64 s8, v1, 0
s_mov_b32 exec_lo, s7
v_mov_b32_e32 v2, s8
v_mov_b32_e32 v4, exec_lo
s_mov_b32 exec_lo, -1
v_mov_b32_e32 v5, s8
v_lshlrev_b32_e32 v3, 4, v0
v_mov_b32_e32 v20, v2
v_mov_b32_e32 v21, v3
buffer_store_dword v20, v21, s[0:3], 0 offen
v_mov_b32_e32 v20, v4
v_add_nc_u32_e32 v21, 4, v3
buffer_store_dword v20, v21, s[0:3], 0 offen
v_mov_b32_e32 v20, v5
v_add_nc_u32_e32 v21, 8, v3
buffer_store_dword v20, v21, s[0:3], 0 offen
v_mov_b32_e32 v20, v0
v_add_nc_u32_e32 v21, 12, v3
buffer_store_dword v20, v21, s[0:3], 0 offen
s_endpgm

From the repo root, with your usual driver and push-constant settings and vk_compute_test on PATH, run the following for each driver. The last command checks the full result, including 16 guard words.

mask_dir=$(mktemp -d)
python3 -I runtime/test/shaders/build.py "$mask_dir" cs_exec_copy_independent &&
VKC_SUBGROUP_SIZE=32 VKC_OUT_WORDS=144 VKC_DUMP="$mask_dir/independent.raw" vk_compute_test "$mask_dir/spirv/cs_exec_copy_independent.spv" 1 dump &&
python3 - "$mask_dir/independent.raw" <<'PY'
from pathlib import Path
import struct
import sys
actual = struct.unpack("<144I", Path(sys.argv[1]).read_bytes())
expected = tuple(w for lane in range(32) for w in (0xaaaaaaaa, 0xffffffff, 0xaaaaaaaa, lane))
assert actual == expected + (0xeeeeeeee,) * 16
print("128/128 output words and all guard words match")
PY
Independent-EXEC move and XOR rows

This is the exact s8 XOR shader. For the move variant, replace only v_xor_b32_e32 v2, s8, v7 with v_mov_b32_e32 v2, s8. For the VCC pair, replace s8 with vcc_lo in the compare and the consumer. Build each name with the same build.py command as above.

; group: 32,1,1
; rsrc2: 0x8090
v_mbcnt_lo_u32_b32 v1, -1, 0
v_mov_b32_e32 v2, 0xdeadbeef
v_mov_b32_e32 v5, s5
v_lshrrev_b32_e32 v5, v1, v5
v_and_b32_e32 v5, 1, v5
v_mov_b32_e32 v7, 0x12345678
v_cmp_ne_u32_e64 s8, v5, 0
s_mov_b32 exec_lo, s4
v_xor_b32_e32 v2, s8, v7
s_mov_b32 exec_lo, -1
v_lshlrev_b32_e32 v3, 2, v1
buffer_store_dword v2, v3, s[0:3], 0 offen
s_endpgm

Run each variant once per pair below, setting VKC_SGPRS=s4,s5 to the two literal hex values, VKC_SUBGROUP_SIZE=32 and VKC_OUT_WORDS=64. For lane i, when bit i of s4 is set, expect s5 for the move or s5 ^ 0x12345678 for XOR. Otherwise expect 0xdeadbeef. Words 32–63 keep the harness fill 0xeeeeeeee.

s4           s5
0x0000000f   0xffffffff
0x0000000f   0xaaaaaaaa
0xf0000000   0x80000001
0x55555555   0xaaaaaaaa
0xffffffff   0x05010204
0x00000000   0x05010204

AI-assisted verification with OpenAI Codex.

@mrgenty

mrgenty commented Nov 25, 2026

Copy link
Copy Markdown

Tested 52fb23042 with crosslane_fuzz.py on an NVIDIA GeForce RTX 3060 (NVIDIA 621.24, Windows 11 build 26300, subgroup 32..32). One program is left, reduced to four instructions below.

Setup: vk_compute_test and the fuzzer (with its model) from main b0f3d399, --translator pointed at each tree's tools/rdna2spirv.py, llvm-mc 21.1.8 (scripts\build_llvm_mc.ps1), Vulkan SDK 1.4.363.0. No game files.

python -I tools/crosslane_fuzz.py --programs 200 --seed 25112501 --wave both --dpp --ext --translator <tree>/tools/rdna2spirv.py
tree wave32 native (100) wave64 emulate (100)
main b0f3d399 8 mismatches, 3 hyp_only 2 mismatches, 4 hyp_only
#1043 4d58a05d6 3 mismatches 1 mismatch
this PR 52fb23042 1 mismatch 0

With --no-maskdata, main matches 400/400 in both modes on this card (seed 73190611), so the masks-as-data programs are the only ones that differ here.

The one left here: cs_xl32_2511325437669, wave32, group 20. --minimize gives:

v_cmp_eq_u32_e64 s20, v1, v4      ; no lane is equal: s20 = 0
s_and_saveexec_b32 s24, s20       ; EXEC = 0
v_mov_b32_e32 v3, s20             ; no lane is active, so v3 keeps its old data
v_readfirstlane_b32 s14, v3       ; EXEC = 0: lane 0's v3

v3 was seeded as lane + 0xd4718f66. The model expects s14 = 0xd4718f66 in every lane, lane 0's untouched v3. This head returns 0x00018f66 in all 20 lanes, which is that value ANDed with 0xfffff, the 20 lanes of the group. So it looks as if the v_readfirstlane reads v3 as a mask word because a v_mov from a mask reaches it, although that v_mov wrote no lane. I did not trace it in the translator, so take the cause as a guess; the numbers are measured.

It is not a regression: main fails the same program differently (each lane gets its own v3, so only lane 0 is right), and #1043 returns 0x000fffff.

For comparison, the four programs #1043 leaves on this seed reduce to a v_mov_b32 of a mask followed by v_readfirstlane_b32 (three wave32 cases, one with EXEC set to 0 first) and to v_mov_b32 v3, s21 of the high half of a s[20:21] mask under --wave64-emulate. This PR matches all four.

Not tested: native wave64 (this card has no 64-wide subgroup), other seeds on this head, and the semantic suite.

AI-assisted (Claude Code).

@ecox982745

Copy link
Copy Markdown

I narrowed down the RTX 3060 result on an RTX 4090. It also happens when both the move and the read have active lanes. This gives a regression test independent of the semantics of a read with empty EXEC.

On 52fb23042, seed v3 with 0xd4718f66 + lane, let only lane 7 overwrite its own v3, then restore full EXEC before reading lane 0. The shader stores both v3 directly and the word returned through s14. Lane 0's direct word is correct in all three group sizes.

Threads in the group Direct lane 0 word Word broadcast through s14
8 0xd4718f66 0x00000066
20 0xd4718f66 0x00018f66
32 0xd4718f66 0xd4718f66
v_add_nc_u32_e32 v3, 0xd4718f66, v0   ; v0 is the local lane index
v_cmp_eq_u32_e64 s20, s4, v0          ; dispatch input s4 = 7
s_and_saveexec_b32 s24, s20
v_mov_b32_e32 v3, s20                 ; only lane 7 writes
s_mov_b32 exec_lo, s5                 ; dispatch input s5 = 0xffffffff
v_readfirstlane_b32 s14, v3           ; lane 0 supplies its original word
s_mov_b32 exec_lo, -1
v_mov_b32_e32 v4, s14                 ; store v3 and v4 side by side

Removing only the predicated v_mov gave the full expected broadcast in all 12 control dispatches. Selecting lane 7 as the reader after only lane 3 wrote gave the same truncation at group sizes 8 and 20. Reading lane 7 after lane 7 itself wrote gave the expected 0x80 in all three sizes.

In the 20-thread module, OpGroupNonUniformShuffle broadcasts the word, then a shift and AND extract one bit per lane. The s14 consumer rebuilds the word with OpGroupNonUniformBitwiseOr … Reduce. A 20-thread group can supply only bits 0–19 to that reduction. This points to preserving the full scalar value through this read when the selected lane retains ordinary data. Read-first-lane lowering on the tested head

Native wave32, Windows 11, NVIDIA 616.64. Six synthetic modules per revision passed Vulkan 1.2 validation, with all readback guards intact. Baseline 9e7720c9e also fails the 20-thread live-move case, returning different words across lanes. AI-assisted verification with OpenAI Codex.

@whoalin1

Copy link
Copy Markdown
Contributor Author

Thanks for narrowing this down. I agree with the interpretation that this is a mixed-provenance case rather than a regression introduced by 52fb23042: your baseline control also fails the live-move case.

The current model has one static provenance for the whole VGPR. lane_masks.py already special-cases the copy shape: a v_mov_b32 of a mask into a VGPR is an ordinary EXEC-gated VALU write, not an all-lanes mask computation, so inactive lanes keep their old data. But after that write the register can contain the mask word in some lanes and ordinary data in others. When v_readfirstlane later selects a lane, the correct result depends on which kind of value that selected lane retained. The present translator cannot represent that lane-dependent provenance, so it chooses the mask interpretation for the whole register and rebuilds only the mask bits — exactly the 8/20-thread truncation you measured.

I do not see a small sound fix inside this PR without tracking which lanes of the VGPR received the mask word (or otherwise carrying per-lane provenance) through the copy and the first-lane selection. Since the case exists on main as well, my preference is to treat it as a follow-up rather than block the lane-mask data-read improvement here. If others prefer, I can write the finding up separately with your reduced shader as the reproducer.

AI-assisted analysis with Devin.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
whoalin1 and others added 5 commits November 26, 2026 00:25
… mask word

A lane mask (EXEC/VCC or a mask-tracked SGPR) that a VALU op reads as a
32-bit value — v_mov_b32 of it into a plain VGPR, or any VALU source
where the mask is data rather than a mask operand — materialized each
lane's own 0 / ~0 bit. Hardware holds the whole wave's mask word there
(tools/crosslane_fuzz.py's op_maskdata model: the uniform word written
to the active lanes), so every lane gets the word, the same in each.

- src_u materializes the wave mask word (lane_mask_word) for SGPR and
  EXEC/VCC operands when the instruction's VGPR destination makes the
  read a data read (mask_read_is_data); SGPR/EXEC/VCC destinations keep
  this lane's bit, which mask_src readers rely on. Mask-holding VGPRs
  already store the word, so their reads are plain loads.
- mask_src extracts this lane's bit back out of a mask-holding VGPR
  (_vgpr_mask_lane_bit), half-aware in wave64 via mask_half.
- mask_data_src keeps instructions that materialize the word out of
  EXEC-gated active runs (a subgroup op can't sit inside if (active)).
- v_readfirstlane_b32 of a mask VGPR broadcasts the first active lane's
  copy — the only lane guaranteed fresh after an EXEC-gated v_mov_b32 —
  as the word itself into a VGPR destination (VGPR writes are never
  mask-tracked), or this lane's bit for a mask-tracked SGPR/special
  one; fixes cs_wavemask lanes 48-63.

Found by tools/crosslane_fuzz.py (ProjectLeonida#293). docs/design.md's whole-wave
mask contract updated.

Co-Authored-By: Devin <noreply@cognition.ai>
…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>
…R round-trips

Review follow-up on the mask-word gather gate (ProjectLeonida#1397): `s_mov_b32 s4,
exec_lo` under an unknown EXEC narrowing recorded nothing for s4, so the
later `s_mov_b32 exec_lo, s4` restore minted a fresh ("n", id) tag in
_exec_src_ex. mask_exec_sigs coverage then failed and the read kept this
lane's 0 / ~0 bit, while the equivalent literal `s_mov_b32 exec_lo, -1`
resolved to a constant half-state and gathered — register and literal
restores of the same runtime value disagreed.

_mov_sgpr_vals now returns an ("al", halves) alias for an EXEC source
whose half-state is an unknown narrowing, the same scheme saveexec uses:
exec_lo gives (state, None), exec_hi (None, state), exec gives each half
in its own register for b64 moves. _exec_src_ex already resolves those
aliases, so the restore reproduces the original tag and the gather stays
exact; untracked SGPR sources still mint a fresh narrowing and stay
conservative.

Tests: two semantic cases (wave32 and wave64) assert the register
round-trip gathers OpGroupNonUniformBitwiseOr identically to the literal
-1 restore; cs_exec_copy.asm (+ fixture, check.py/run_gpu_tests.py dump
entries) stores the mask word, exec_lo's own word and the literal-restore
word side by side on real hardware — PASS on RTX 4070 and Radeon
(64-wide), wave64 exec filters 9/9 on AMD.

Co-Authored-By: Devin <noreply@cognition.ai>
…y lane runs it (SHADER-WAVEMASK-EAGER)

A register holds this lane's bit of a lane mask; v_mbcnt, s_bcnt1 and
s_ff1 gather the other lanes' bits with a subgroup operation
(Gen.wave_mask). v_mbcnt writes a VGPR, so its result is selected on the
lane's EXEC, and MoltenVK's SPIRV-Cross inlined the single-use gather
into the taken arm of that ternary:

    v6 = active ? popcount(simd_or(mine ? bit : 0) & below) + acc : v6;

A lane that is off never ran the gather, so its bit was missing from
every other lane's count. Gen.wave_mask now stores the gather to a
variable of its own with Gen.eager, as SHADER-529 did for DPP's ballot
and shuffle.

On an Apple M5 (MoltenVK 1.4.2), tools/crosslane_fuzz.py with wave 32,
native and no maskdata goes from 92 of 100 programs matching to 100 of
100, and from 193 to 200 of 200 with DPP at seed 7.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Co-Authored-By: SWE-2.1 Max <noreply@google.com>
…EXEC

A lane mask in an SGPR, EXEC or VCC is one wave-wide word on hardware.
The EXEC-coverage gate added earlier in this pull request could not prove
the independent-SGPR restore idioms crosslane_fuzz uses, so it fell back
to this lane's 0 / ~0 bit for exactly the programs the read is meant to
fix. It also encoded the wrong control-flow model: EXEC narrowing is a
per-lane value here, not a SPIR-V edge. Mask-data readers are already
kept out of lane-local `if (active)` runs, and the eager wave-mask store
keeps lowering from moving the subgroup gather into an EXEC-selected
arm, so lanes EXEC has off still contribute their mask bits.

Remove the gate and the CFG EXEC provenance analysis it needed, gather
the mask word for every lane-mask data read, and cover narrowed SGPR and
VCC reads, an independent all-ones SGPR EXEC restore, and the composed
vector-bitwise mask words. The vector-bitwise expectations now check the
uniform mask words and the resulting per-lane cndmask bits and popcounts
rather than the old fallback artifact.

Co-Authored-By: SWE-2.1 Max <noreply@google.com>
@whoalin1
whoalin1 force-pushed the mask-regs-data-read branch from 52fb230 to e3e119a Compare November 25, 2026 22:35
@whoalin1

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (581ddba5d) and pushed as e3e119a46; GitHub now reports the PR mergeable again. The only conflicts were the fixture-list intersections with the newly merged cs_w32_wrxchg2* / cs_w64x_partial_swizzle entries; resolution keeps both those and cs_exec_copy.

Post-rebase verification on this machine:

  • python -m compileall -q make_game.py tools tests scripts/run_gpu_tests.py runtime/test/shaders/check.py — pass.
  • python -I scripts/run_semantic_tests.py tests/semantic — 493 files, 6,334 tests, 0 files failed in 345 s (Python 3.14).
  • python -I scripts/run_gpu_tests.py --filter cs_exec_copy --require-pass — pass on both GPUs, with LEONIDA_VK_VALIDATION=1 and spirv-val clean:
    • AMD Radeon Graphics (Ryzen 5 7600 RDNA2 iGPU, 128-byte push constants, AMD Windows driver 24.10.24): cs_exec_copy PASS, exact words.
    • NVIDIA RTX 4070 SUPER (driver 610.88): cs_exec_copy PASS, identical exact words.

No game files used. AI-assisted with Devin.

@whoalin1

Copy link
Copy Markdown
Contributor Author

Additional AMD evidence on the current head e3e119a. Same synthetic crosslane slice before/after, no game files:

  • Environment: Windows, AMD RDNA2 Radeon iGPU (128-byte push-constant device), validation on, VK_LOADER_DRIVERS_SELECT selecting the AMD Vulkan driver, LEONIDA_PC_TAIL_UBO=1.
  • Current main a73a57e, seed 14 wave64-emulate slice (26 programs, --dpp --ext): 23 match, 1 non-hypothesis mismatch (cs_xl64_1400067), 1 hypothesis-only mismatch (cs_xl64_1400059). The companion wave32-native half was 25/25 clean.
  • With this PR's translator on the identical generated wave64 slice: 26/26 match, 0 mismatch, 0 hypothesis-only, 0 harness errors.

So this PR fixes both seed-14 wave64-emulate divergences on AMD, including the non-hypothesis cs_xl64_1400067 case.

@EvgHaze

EvgHaze commented Nov 26, 2026

Copy link
Copy Markdown

@ecox982745 ran your five fixtures on the Apple side, at this head (e3e119a46), on both drivers.

Machine:  Apple M3, 10-core, macOS 27.0 (26A5406e), arm64
Drivers:  MoltenVK 1.4.2 (subgroup 4..32) and KosmicKrisp, Mesa 26.3.1 (32..32)
Tree:     whoalin1/leonida-continued `mask-regs-data-read` @ e3e119a46, `runtime/build`, 33 `vk_*` tools
Fixtures: your `cs_exec_copy_independent`, plus the XOR and move shaders with `s8` and with `vcc_lo`, built by `runtime/test/shaders/build.py`, all five `spirv-val` clean
Run:      `VKC_SUBGROUP_SIZE=32`, `VKC_OUT_WORDS=144` for the independent fixture and 64 for the pairs, `VKC_DUMP` readback, your expected words and guard words
Check MoltenVK 1.4.2 KosmicKrisp 26.3.1
independent SGPR fixture PASS, 128/128 output words + 16 guard words PASS, same
move, s8 (6 SGPR pairs) PASS, 6/6 PASS, 6/6
XOR, s8 (6 SGPR pairs) PASS, 6/6 PASS, 6/6
move, vcc_lo (6 SGPR pairs) PASS, 6/6 PASS, 6/6
XOR, vcc_lo (6 SGPR pairs) PASS, 6/6 PASS, 6/6
total 25/25 25/25

Every lane value and 0xeeeeeeee fill word matches, and both drivers agree word for word — a 32-wide host runs these natively, so nothing here goes through wave64 emulation.

Two notes on reproducing it:

  • v_cmp_ne_u32_e64 vcc_lo, v5, 0 (your VCC variant) assembles, but the move variant has to drop the v7 operand — v_mov_b32_e32 v2, s8, not v_mov_b32_e32 v2, s8, v7; with three operands llvm-mc rejects it.
  • I did not run the baseline on this machine, so I am not claiming the main-side numbers; on Apple both drivers pass the head, which is what the row was for.

AI-assisted. No game data: hand-written shaders, and every number above is a readback comparison.

@ecox982745

Copy link
Copy Markdown

@sdelectron, I checked the six seed-1 cases you reported on the RTX 5080 against this PR on a physical RTX 4090. All six also differ from the CPU reference on current main. All six match after merging this PR locally. That includes the three read-first-lane cases (100023, 100075, 100014), parked lane access (100026), the prefix count (100034), and the 18-lane tail mask (100077). For the last case main returned 0xffffffff where the reference has 0x0003ffff. Seed-1 coordination reports

Unchanged seed-1 inputs Main a73a57e13 Main + PR head e3e119a46
Native wave32 programs matching 45/50 50/50
Emulated wave64 programs matching 45/50 50/50
Output words differing from the CPU model 422/103,180 0/103,180

I used main's crosslane_fuzz.py generator and reference (--seed 1 --programs 100 --wave both, default 24 ops), assembled the inputs once and ran both sets with the same vk_compute_test built from main. Six of the ten main-side mismatches were ordinary model comparisons. The other four programs contain an operation the fuzzer marks as a hypothesis. All 200 dispatched modules passed Vulkan 1.2 SPIR-V validation and ran with Khronos validation enabled. Windows 11, NVIDIA 616.64. The clean merge tree was dd50d3bb2bb09f7d406a90cc0c534a5af48f6300.

I also retested the separate lane-7 fixture on that merge tree to check what remains for the mixed-provenance claim. After only lane 7 writes a mask to its vector register and full EXEC is restored, the direct lane values are all correct. The broadcast of lane 0 should give 0xd4718f66; it still gives 0x00000066 for 8 threads and 0x00018f66 for 20. The 32-thread case and all three controls without the predicated move pass; all guard words stayed intact. This smaller fixture still exercises the outstanding bug even though the full seed-1 corpus now passes.

AI-assisted verification (OpenAI Codex). All shaders and inputs were synthetic.

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026
@Christopher-Sundeby

Copy link
Copy Markdown

Verified on NVIDIA: this pull request clears 19 of the 20 crosslane_fuzz mismatches that main gives on four seeds, with no new ones. The one left is the known cs_xl64_200065.

Setup: RTX 3060 Ti (subgroup 32), NVIDIA 621.24, Windows 11, MSVC vk_compute_test. The head e3e119a merges cleanly onto main a73a57e. llvm-mc 21.1.8 (AMDGPU), --programs 100 --wave both (50 wave32 native + 50 wave64 emulate per seed), translator oracle.

seed main mismatches (w32 / w64 emu) main hypothesis-only this PR mismatches (w32 / w64 emu) this PR hypothesis-only
1 3 / 3 2 / 2 0 / 0 0 / 0
2 2 / 5 2 / 1 0 / 1 0 / 0
3 3 / 1 2 / 1 0 / 0 0 / 0
7 1 / 1 2 / 2 0 / 0 0 / 0
  • The remaining one is cs_xl64_200065 (seed 2, wave64 emulate, s10 expected 0x0000008c, got 0x00000000). That is sdelectron's repro of the outstanding SHADER-READFIRSTLANE-MIXED-PROVENANCE part, so it is not new.
  • A two-instruction repro from seed 7 (cs_xl64_700024, wave64 emulate) that this pull request fixes:
    v_cmp_ge_u32_e32 vcc, 8, v1
    v_mov_b32_e32 v2, vcc_hi      ; main: 0xffffffff, model: 0x00000000
    
  • python -I scripts/run_semantic_tests.py tests/semantic on the merged tree: 499 files, 6,370 tests, 0 files failed (262 s, 32 jobs).
  • The other families on main with the same setup (seed 41, 100 programs, native and emulated) are clean: valu_fuzz f16 and mem, lds_fuzz, image_fuzz (seed 42), f32_fuzz (seed 42, 200 programs).

No game files. AI-assisted (Claude Code).

@qhukz13

qhukz13 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

I checked e3e119a46 merged onto main a73a57e13 (no conflicts), CPU only, on Linux with Python 3.14. The GPU side is well covered above, but every run there is crosslane_fuzz compute. This adds the mesh stage under --wave64-emulate: 128-thread NGG mesh shaders (two waves) are the "VS as MS" path the world-load draws take. I ran them in spirv_mini_interp.run_workgroup on 32-wide subgroups and compared each thread's exported position with a hand model.

The bug is there on the mesh path too, and this fixes it. Take v_cmp_gt_u32 vcc, 40, v0, then v_mov_b32 v6, vcc_lo:

  • on main, lanes 40-63 of wave 0 get 0 instead of the word 0xffffffff;
  • v_cmp_gt_u32_e64 s[10:11], 40, v0 followed by s_add_u32 s20, s10, 1 is wrong on the same 24 lanes.

With this PR both are right, in the structured form and in the forced round-loop form.

No regressions on the mesh path:

  • 17 hand-written mesh programs, 34 runs, give the same correct words as on main, in both forms:
    • waterfall v_readfirstlane / s_and_saveexec loops;
    • execnz, v_cmpx and VCC-vote latches;
    • loops whose trip count differs between the two waves;
    • s_barrier + LDS inside a loop;
    • a wave skipping to s_endpgm.
  • s_cmp_eq/lg/lt_u32 and s_bitcmp1_b32 on a mask word, into s_cselect_b32 or a branch, are right on both trees. I had wondered whether MASK_NUMBER_READERS needs the compares, and it does not.
  • tools/cf_diff.py's programs as 2-wave mesh shaders: 300 programs (seeds 21-22, depth 3), each in the default form, the forced round-loop form and without the extra s_barrier. That is 900 runs, 350 of them in the round-loop form: 900 match cf_ref, with no non-uniform barrier and no runaway loop.
  • The full translator suite, on the PR merged onto main 73516a290 (Windows 10, Python, 24 jobs): 499 files, 6,370 tests, 0 files failed.

Cost, for the record. Each mask-as-data read site adds one exchange segment under --wave64-emulate. The scheduler switch goes 2 → 3 cases for a single v_mov_b32 v, vcc_lo, and 5 → 6 for one inside a loop, plus 2 workgroup atomics. The static OpControlBarrier count is unchanged, but in the round-loop form an extra segment is an extra round at run time. That includes v_mov_b32 v6, exec_lo with EXEC still full, which main already got right. If a census ever shows these reads are common in the mesh corpus, the gather could be skipped where lane_masks proves the mask is all ones or zero. That is not a reason to hold this.

On #1043: plamea's seed-1 sweep (#1608) and the runs above have this PR covering everything #1043 does, plus the v_readfirstlane cases, and #1043 is 553 commits behind. I would merge this one.

AI-assisted (Claude Code).

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

Copy link
Copy Markdown
Contributor

Independent check of e3e119a under --wave64-emulate, by Claude Code (AI-assisted) on my behalf. Linux, Mesa
lavapipe (llvmpipe, LLVM 19.1.7, 8-lane subgroups; emulated wave64 uses no subgroup operations, so it is judged
there, docs/shader_validation.md "Subgroup size").

tools/crosslane_fuzz.py --wave 64 --modes emulate --dpp --ext (main's fuzzer, --translator pointed at this
head):

seed, programs main d1dff79 this head
205, 150 143 match, 5 mismatch, 2 hyp_only 150 match
311, 300 286 match, 10 mismatch, 4 hyp_only 300 match

Every one of main's 21 failing programs has a mask read as data (maskdata); none is left here, and nothing is
rejected. Minimized on main, the shortest are:

  • s_bfm_b64 s[18:19], 47, 49 / v_mov_b32_e32 v3, s19 (group 192): expected 0xfffe0000 in every lane, main
    gives 0 or 0xffffffff per lane. s_bfm_b64 is not mentioned in this thread, so this may be a useful fixture.
  • v_cmp_gt_u32_e32 vcc, 51, v1 / v_mov_b32_e32 v3, vcc_hi (group 64): expected 0x0007ffff.
  • v_cmp_ne_u32_e64 s[18:19], v7, v4 / s_and_b64 s[20:21], ... / v_mov_b32_e32 v5, s21 in a group of 100
    (a partial second wave): expected 0x0000000f in wave 1.

No regressions seen on this head (each its own seed): tests/semantic 493 files / 6,334 tests, 0 failed;
lds_fuzz.py --wave 64 --emulate 150/150; valu_fuzz.py --family mem --wave 64 --emulate 120/120;
cf_diff.py --wave 64 300/300. Native wave32 was not run (lavapipe can't pin 32-lane subgroups), and no hardware
GPU. The branch now conflicts with main.


Generated by Claude Code

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.