Skip to content

Translator: a lane mask computed in the vector unit is data, written to the active lanes only (SHADER-402) - #3900

Open
simonbronk wants to merge 6 commits into
ItsNotRexed:mainfrom
simonbronk:translator/vector-mask-data
Open

simonbronk wants to merge 6 commits into
ItsNotRexed:mainfrom
simonbronk:translator/vector-mask-data

Conversation

@simonbronk

Copy link
Copy Markdown
Contributor

Depends on: #293

SHADER-402, follow-up to SHADER-21 (#210): a lane mask computed in the vector unit is read as data and written only to the lanes EXEC has on.

#293 gathers the wave's bits when a lane mask is read as data, and its last commit does that at a v_mov_b32 copy (needed with #2290). One gap was left: a mask computed in the vector unit (v_or_b32 vN, vcc_lo, s20, v_or3_b32, v_xor_b32 vN, s20, s21). There, each lane computed its own bit into the VGPR in every lane, active or not. Two things went wrong with that:

  • the lanes EXEC had left off lost their old VGPR;
  • a VGPR mixing the two halves of a wave64 mask got a per-lane bit instead of the word.

Change

  • tools/lane_masks.py: a VGPR never holds a lane mask. A VALU op with a mask source reads it as data, so data_reads gathers each mask source, as it already did for v_mov_b32. The ("v", r) / ("vw", r) state and step's "writes a mask to a VGPR" result are gone. analyse still returns writes (always empty), so callers unpack it as before.
  • tools/rdna2spirv.py: removes the all-lanes VGPR store (write_all_lanes, mask_writes) and v_readfirstlane's per-lane path for a mask VGPR. An SGPR read back with v_readfirstlane is now data; used as a mask, each lane takes its own bit (rdna2spirv: a constant used as a lane mask through an SGPR (or a b32 SOP2 on EXEC) gives each lane its bit #1792), as it already did after a v_mov_b32 copy.
  • docs/design.md: the "Lane mask read as data" row covers this case. Two known gaps are dropped: a wave64 VGPR mixing both halves, and a mask computed in the vector unit under a partial EXEC.

Tests

Two new GPU tests, both failing on #293's head (004e31f):

Shader What #293 This PR (= hardware)
cs_mask_vgpr_or (wave32) vcc_lo = lanes 0-4, s20 = lanes 30-31, v_or_b32_e64 v2, vcc_lo, s20 with EXEC = lanes 0-3, then every lane stores 0xc000001f in all 8 dumped lanes lanes 0-3 0xc000001f, lanes 4-7 keep 0xdeadbeef
cs_wm_vgpr_halves (wave64) s[20:21] = lanes 0-35, v_xor_b32_e64 v10, s20, s21 with EXEC = lanes 0-3 0 in all 8 lanes 0-3 0xfffffff0, lanes 4-7 keep 0xdeadbeef

Windows 11, LLVM 21.1.8 llvm-mc, GPU picked with VK_LOADER_DRIVERS_SELECT:

GPU Driver Mode run_gpu_tests.py
AMD Radeon iGPU (Ryzen 7 9800X3D, RDNA2), subgroup 32..64 AMD 25.10.36 native 183/198 passed, 15 skipped
NVIDIA GeForce RTX 5070, subgroup 32 621.24 LEONIDA_W64_EMULATE=1 183/198 passed, 15 skipped

The skips are the same as on main. Other checks:

Corpus (player's own copy, 1.0.2.1, --wave64-emulate): 26,501 translate and pass spirv-val, the same as main and #293. No SPIR-V binary differs from #293. The game's shaders don't compute a lane mask in the vector unit under these conditions, so this PR changes nothing the game runs today. It fixes the translator's semantics for code that does, and it removes a special path (the all-lanes store) that only this case used.

Remaining gaps, unchanged from #293: SALU bitwise ops that combine wave64 halves (s_xor_b32 s0, s4, s5); a 64-bit EXEC write from a data pair.

AI-assisted (Claude Code). No game data used.

🤖 Generated with Claude Code

simonbronk and others added 6 commits November 22, 2026 22:04
The corpus is disassembled for gfx1013 without +wavefrontsize64, so the 64-bit
SDST of a wave64 v_cmp_*_e64 or carry-out prints as one register
(v_cmp_gt_f32_e64 s0, ...) while the wave writes s[0:1]. lane_masks.py took it
as s0 alone, so s1 was unknown and a later s_and_b64 / s_or_b64 of two compare
results took the 64-bit data path: SCC was each lane's own bit instead of the
wave's vote, and s_bcnt1_i32_b64 of it counted data bits.

lane_masks.step/analyse take the shader's wave size; mask_sdst widens a
one-register VALU mask destination to the pair in wave64. rdna2spirv's
valu_mask_dst writes both registers: since 804f4ae a pair read as a wave64
mask takes lanes 32-63 from the high register, which the one-register write
left stale.

Corpus (26,503 shaders): one-register SDSTs in 18,866 of 23,839 wave64
shaders; 18,868 translate differently, all wave64; the same 26,501 still
translate and pass spirv-val as on main. New GPU test cs_w64_sdst_pair (corpus form) and
tests/semantic/test_w64_sdst_pair.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… part 1, ItsNotRexed#210)

A register holding a lane mask keeps only this lane's bit (0 or ~0). When an
instruction reads it as a plain value (a VALU source, store data, s_lshr /
s_and with a constant, s_lshr_b64, s_add, exec_lo/hi or vcc_lo/hi as a value),
hardware sees the whole wave's mask; the translation saw the lane's bool.

lane_masks.data_reads finds those reads, leaving mask readers (bcnt, ff1,
mbcnt, cmp, bitcmp), copies, mask-to-mask bitwise ops, saveexec, the
v_cndmask selector, carry-ins and EXEC/VCC writes alone. The translator
gathers each half with OpGroupNonUniformBitwiseOr (or the emulated wave64
round) before the instruction and returns it from load_reg / src_u.

New GPU tests cs_wm_as_data, cs_wm_as_data2 (wave64) and cs_wm_as_data32
(wave32), all failing before; data_reads unit tests; design.md row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…exec_lo takes the lane's bit

Follow-up to the lane-mask-as-data change (ItsNotRexed#210), rebased on main's 64-bit
SALU data path (src64_data / dst64_data):
- s_and/or/xor/andn2/orn2_b64 and s_cselect_b64 whose lane-mask sources are
  read as data (mask_data64) take main's data path on both words instead of
  the per-lane mask path.
- mask_src reads the raw register, so the v_cndmask selector stays a mask
  when the same register is also a value operand.
- A register written to exec_lo turns on the lanes 0-31 whose bit is set
  (the same as non-zero for a one-bit-per-lane mask), so a mask combined
  with data and moved to EXEC no longer turns on every lane; in wave64
  lanes 32-63 keep their EXEC (exec_half_write).
- tests/semantic: test_salu64_bitwise_data and test_scselect_b64_data
  expected v_mov_b32 vN, s<mask> to give the lane's bool; it gives the
  wave's bits (ItsNotRexed#210).

New GPU tests cs_wm_as_data64 and cs_wm_as_exec.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… gathered when read as data

A mask copied into a VGPR (v_mov_b32 v7, s16) and then given data in one lane (v_writelane_b32 v7, s15, 27)
was gathered as the wave's bits when read as data, so lane 27's data set bit 27 in every lane. Mark such a
VGPR (and VGPR copies of it) so data_reads reads it lane by lane, as main does (crosslane_fuzz seed 6,
cs_xl32_600042, which passed on main and failed with this PR).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…its at the copy

With ItsNotRexed#2290 merged, a v_mov_b32 copy of a mask into a VGPR is an EXEC-gated
write, so lanes EXEC leaves off keep their old VGPR. Gathering the mask at a
later read (the store) then took those lanes' old words as set bits and
overwrote them: cs_mask_vgpr_exec failed on AMD and on the RTX 5070.

Hardware writes the wave's bits to the active lanes, so the copy now reads its
mask source as data (lane_masks.data_reads) and the VGPR holds data from there
on. cs_mask_vgpr_exec2 uses a mask that differs per lane: lanes 0-3 get 0x1f,
lanes 4-31 keep 0xdeadbeef; main gives 0xffffffff in lanes 0-3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…to the active lanes only

A VALU op that reads a lane mask (v_or_b32 vN, vcc_lo, s20; v_or3_b32; v_xor_b32 vN, s20, s21 with both
wave64 halves) now reads it as data, like the v_mov_b32 copy already does: data_reads gathers each mask
source, the op runs on the words, and the write is EXEC-gated. Before, each lane computed its own bit in
every lane, active or not, so lanes EXEC had left off lost their old VGPR, and a VGPR mixing both wave64
halves gave a per-lane bit instead of the word.

A VGPR now never holds a lane mask. The ("v", r) and ("vw", r) state, the all-lanes store
(write_all_lanes) and v_readfirstlane's per-lane path for such a VGPR are gone. An SGPR read back with
v_readfirstlane is data, and used as a mask each lane takes its own bit (ItsNotRexed#1792).

New GPU tests: cs_mask_vgpr_or (wave32) and cs_wm_vgpr_halves (wave64); both fail on the parent.

SHADER-402, follow-up to SHADER-21 (ItsNotRexed#210).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tupacfan04

Copy link
Copy Markdown
Contributor

Semantically this checks out, but it cannot land as-is: CONFLICTING with 13 conflict blocks in 6 files (design.md 1, check.py 2, run_gpu_tests.py 4, test_lane_masks.py 2, lane_masks.py 3, rdna2spirv.py 1), and it is stacked on open, also-conflicting #293.

What I verified: full tests/semantic 4145 passed / 47,512 subtests; targeted 108 passed + 713 subtests; regen_fixtures --check and compileall clean; reverting lane_masks.py + rdna2spirv.py to HEAD~1 fails 3 tests (tests/test_lane_masks.py:118,145,226). The rule matches docs/design.md: mask readers stay per-lane, VALU mask sources are gathered as data, and the write is ordinary EXEC-gated with inactive lanes keeping their old VGPR; the in-repo spirv_wave_interp reproduces the PR's words exactly (wave32 0xc000001f/0xdeadbeef, wave64 0xfffffff0/0xdeadbeef). Contract rows exist (docs/design.md:371, :282) with the hardware named.

Three fixes while rebasing:

  • docs/design.md:281 still says per-lane computations "store in all lanes", contradicting the removed write_all_lanes.
  • tests/test_shader_checks.py:28 and :32 assign WAVE32_WORDS twice with identical values (line 32 wins).
  • The body lists 2 GPU tests, but the diff against main adds 9 .asm files plus test_w64_sdst_pair.py (inherited from the Translator: a lane mask read as data gives the wave's bits (SHADER-21 part 1, #210) #293 stack) - say so, or split the stack.

AI-assisted review, no game files, no GPU.

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

GPU run for this PR, since the review above had no GPU. I tested head aa8f5383 as it is (base 16282101, not rebased, because of the conflicts @tupacfan04 lists). All 9 new GPU tests pass on both GPUs, and the RDNA2 iGPU runs the wave64 ones on its native 64-wide subgroup. AI-assisted (Claude Code), no game files.

Hardware: AMD Ryzen 5 7600X, Windows 11 Home 26300. GPUs: AMD Radeon(TM) Graphics iGPU (RDNA2 gfx1036, subgroup 32..64), driver 24.10.24 (Vulkan driver 2.0.302); NVIDIA GeForce RTX 5060 Ti, driver 610.88 (subgroup 32..32). MSVC 14.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8, Python 3.12.10. Test programs built at the PR head with scripts\build_runtime.ps1. Implicit layers off.

python -I scripts/run_gpu_tests.py, whole suite:

device mode result
Radeon iGPU native 100/198 passed, 98 skipped, 0 failed
Radeon iGPU LEONIDA_W64_EMULATE=1 100/198 passed, 98 skipped, 0 failed
RTX 5060 Ti native 145/198 passed, 52 skipped (wave64), 1 failed
RTX 5060 Ti LEONIDA_W64_EMULATE=1 182/198 passed, 15 skipped, 1 failed

The one NVIDIA failure is ps_sdwa_f16_const. That's the 610.88 f16 denormal bug fixed in main by #3220, which this base predates, so it isn't caused by this PR.

The new tests, all PASS with the same words on every run that executed them:

test iGPU native iGPU emulate RTX native RTX emulate
cs_mask_vgpr_exec2 PASS PASS PASS PASS
cs_mask_vgpr_or PASS PASS PASS PASS
cs_wm_as_data32 PASS PASS PASS PASS
cs_w64_sdst_pair PASS PASS SKIP (no wave64) PASS
cs_wm_as_data PASS PASS SKIP PASS
cs_wm_as_data2 PASS PASS SKIP PASS
cs_wm_as_data64 PASS PASS SKIP PASS
cs_wm_as_exec PASS PASS SKIP PASS
cs_wm_vgpr_halves PASS PASS SKIP PASS

For example, cs_mask_vgpr_or gives 3221225503 3221225503 3221225503 3221225503 3735928559 3735928559 3735928559 3735928559 (0xc000001f in the active lanes, the old 0xdeadbeef in the rest), and cs_wm_vgpr_halves gives 4294967280 ×4, 3735928559 ×4. Those are the words in the description.

  • python -m unittest discover -s tests/semantic: 4145 tests OK.
  • python -I tests/test_lane_masks.py, python -I tests/test_shader_checks.py: OK.

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested aa8f538 (a re-test at the new head) on AMD Radeon RX 9070 XT (RADV GFX1201, wave32/64) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions against its merge base.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 184/198 passed, 14 skipped, 0 failed on both GPUs. The 9 new tests (cs_mask_vgpr_exec2, cs_mask_vgpr_or, cs_w64_sdst_pair, cs_wm_as_data, _data2, _data32, _data64, cs_wm_as_exec, cs_wm_vgpr_halves) pass on both. Every other test has the same result as at the merge base 1628210.
  • python -m unittest discover -s tests/semantic: 4145 run, OK. tests/test_shader_checks.py: 69 run, OK. My tests/test_lane_masks.py run printed no result line, so I'm not reporting it.
  • Note: the branch's merge base is 2,537 commits behind main, so this says nothing about how it behaves merged onto current main. A rebase would need its own run.

No game files. AI-assisted.

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.

4 participants