Repository navigation
rdna2spirv: a constant in an SGPR read as a lane mask gives lane L bit L (SHADER-304) - #1841
etcetera-yt wants to merge 1 commit into
Conversation
…t L (SHADER-304) s_mov_b32 s20, 0x3ffffc00 leaves the raw constant in every invocation, but Gen.mask_src reads an SGPR as this lane's 0/~0 bit, so any non-zero constant was every lane: s_and_saveexec_b32 s6, s20 enabled 32 lanes instead of 10..29 (@LouisRiverstone's finding). The immediate form was already right. lane_masks.constants() follows s_mov_b32/s_mov_b64 of an immediate, and copies of such an SGPR, to each instruction (same value on every path, forgotten on any other write). mask_src and s_mov_b32 exec_lo/exec_hi read such an SGPR as the constant, as for an immediate. The mask analysis itself is unchanged. Tests: tests/semantic/test_sgpr_const_mask.py (saveexec b32/b64, wrexec, s_mov exec_lo, v_cndmask, copies, overwritten constants) and tests/test_lane_masks.py test_constants (joins, loops, overwrites). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Tested 03f93c7 on NVIDIA GeForce RTX 2060 SUPER (Turing), NVIDIA 615.78.08, Vulkan 1.4.351, Fedora 44, native Linux build, llvm-mc 22.1.8: pass.
AI-assisted (Claude Code). |
|
Review finding, not a competing fix: tested On RDNA2 an SGPR is one register for the whole wave, so Setup: AMD Radeon RX 6700 XT (gfx1031, RDNA2, RADV NAVI22, subgroup 32..64), Mesa 26.1.6-1, Debian 13 (kernel 7.3.4); llvm-mc 21.1.8, SPIRV-Tools 2026.4; harness wave32,
wave64, native 64-wide subgroup,
Each word is #1792 takes another route that covers these: cs_w32_sgpr_mask.asm (wave32 fixture); Local review fixture for SHADER-304 (#1841, #1792), not part of either PR: an SGPR holding a lane-mask VALUE
; (not a compare result) read as a lane mask. On RDNA2 an SGPR is one 32-bit register shared by the wave, so
; s_and_saveexec_b32 s7, sN gives EXEC &= sN bit by bit, wherever sN's value came from (ISA: EXEC is one bit per
; lane). s4 is a user SGPR input, 0x3ffffc00 = lanes 10..29.
; Each case marks the lanes it leaves on; with EXEC back to all lanes, lane 0 stores
; word = (set lanes) | (lowest set lane) << 8, from s_bcnt1 and s_ff1 of the marks (no lane: 0xffffff00).
; out[0] s_mov_b32 s20, 0x3ffffc00 (literal); s_and_saveexec_b32 s20 lanes 10..29 20 | 10<<8 = 2580
; out[1] s_and_saveexec_b32 s4 (user SGPR input) lanes 10..29 2580
; out[2] s_mov_b32 exec_lo, s4 lanes 10..29 2580
; out[3] s_mov_b32 s21, s4 (copy of the input); EXEC=0; s_or_saveexec s21 lanes 10..29 2580
; out[4] s_lshl_b32 s22, s4, 1 = 0x7ffff800 (computed); s_and_saveexec lanes 11..30 20 | 11<<8 = 2836
; out[5] s_and_b32 exec_lo, exec_lo, s4 (SOP2 on EXEC) lanes 10..29 2580
; out[6] control: v_cmp_gt_u32 vcc_lo, 16, v0; s_and_saveexec vcc_lo lanes 0..15 16
; out[7] control: s_mov_b32 s23, vcc_lo (a real mask); s_and_saveexec s23 lanes 0..15 16
; "Every lane" (an SGPR read as this lane's 0/~0 bool, any non-zero value = all lanes) gives 32 for a case.
; group: 32,1,1
; rsrc2: 0x8090
; Run: VKC_SUBGROUP_SIZE=32 VKC_SGPRS=0x3ffffc00 vk_compute_test cs_w32_sgpr_mask.spv 1 dump
; -> out[0..7] = 2580 2580 2580 2580 2836 2580 16 16
; mattr: +wavefrontsize32,-wavefrontsize64
v_mov_b32_e32 v10, 0
v_mov_b32_e32 v11, 0
v_mov_b32_e32 v12, 0
v_mov_b32_e32 v13, 0
v_mov_b32_e32 v14, 0
v_mov_b32_e32 v15, 0
v_mov_b32_e32 v16, 0
v_mov_b32_e32 v17, 0
s_mov_b32 exec_lo, -1
s_mov_b32 s20, 0x3ffffc00
s_and_saveexec_b32 s7, s20
v_mov_b32_e32 v10, 1
s_mov_b32 exec_lo, -1
s_and_saveexec_b32 s7, s4
v_mov_b32_e32 v11, 1
s_mov_b32 exec_lo, s4
v_mov_b32_e32 v12, 1
s_mov_b32 s21, s4
s_mov_b32 exec_lo, 0
s_or_saveexec_b32 s7, s21
v_mov_b32_e32 v13, 1
s_mov_b32 exec_lo, -1
s_lshl_b32 s22, s4, 1
s_and_saveexec_b32 s7, s22
v_mov_b32_e32 v14, 1
s_mov_b32 exec_lo, -1
s_and_b32 exec_lo, exec_lo, s4
v_mov_b32_e32 v15, 1
s_mov_b32 exec_lo, -1
v_cmp_gt_u32_e32 vcc_lo, 16, v0
s_and_saveexec_b32 s7, vcc_lo
v_mov_b32_e32 v16, 1
s_mov_b32 exec_lo, -1
v_cmp_gt_u32_e32 vcc_lo, 16, v0
s_mov_b32 s23, vcc_lo
s_and_saveexec_b32 s7, s23
v_mov_b32_e32 v17, 1
s_mov_b32 exec_lo, -1
v_mov_b32_e32 v20, 0
v_cmp_ne_u32_e32 vcc_lo, 0, v10
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v21, s30
v_cmp_ne_u32_e32 vcc_lo, 0, v11
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v22, s30
v_cmp_ne_u32_e32 vcc_lo, 0, v12
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v23, s30
v_cmp_ne_u32_e32 vcc_lo, 0, v13
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v24, s30
v_cmp_ne_u32_e32 vcc_lo, 0, v14
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v25, s30
v_cmp_ne_u32_e32 vcc_lo, 0, v15
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v26, s30
v_cmp_ne_u32_e32 vcc_lo, 0, v16
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v27, s30
v_cmp_ne_u32_e32 vcc_lo, 0, v17
s_bcnt1_i32_b32 s30, vcc_lo
s_ff1_i32_b32 s31, vcc_lo
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v28, s30
v_cmp_eq_u32_e32 vcc_lo, 0, v0
s_and_saveexec_b32 s7, vcc_lo
buffer_store_dword v21, v20, s[0:3], 0 offen
buffer_store_dword v22, v20, s[0:3], 0 offen offset:4
buffer_store_dword v23, v20, s[0:3], 0 offen offset:8
buffer_store_dword v24, v20, s[0:3], 0 offen offset:12
buffer_store_dword v25, v20, s[0:3], 0 offen offset:16
buffer_store_dword v26, v20, s[0:3], 0 offen offset:20
buffer_store_dword v27, v20, s[0:3], 0 offen offset:24
buffer_store_dword v28, v20, s[0:3], 0 offen offset:28
s_endpgmcs_w64_sgpr_mask.asm (wave64 fixture); Local review fixture for SHADER-304 (#1841, #1792), wave64: an SGPR pair holding a lane-mask VALUE read as a
; 64-bit lane mask. Lanes 32..63 take their bit from the high register. s[4:5] is a user SGPR input:
; s4 = 0x3ffffc00 (lanes 10..29), s5 = 0x0000ff00 (lanes 40..47).
; word = (set lanes) | (lowest set lane) << 8 over all 64 lanes (s_bcnt1/s_ff1_i32_b64 of the marks).
; out[0] s_mov_b64 s[24:25], 0x3ffffc00 (literal, high word 0); s_and_saveexec_b64 lanes 10..29 20 | 10<<8 = 2580
; out[1] s_mov_b64 s[20:21], s[4:5] (copy of the input); s_and_saveexec_b64 10..29, 40..47 28 | 10<<8 = 2588
; out[2] s_and_saveexec_b64 s[6:7], s[4:5] (the input) 10..29, 40..47 2588
; out[3] s_mov_b64 exec, s[4:5] 10..29, 40..47 2588
; out[4] s_and_b64 exec, exec, s[4:5] 10..29, 40..47 2588
; out[5] s_mov_b32 exec_lo, s4 (exec_hi stays all) 10..29, 32..63 52 | 10<<8 = 2612
; out[6] control: v_cmp_lt_u32 vcc, 20, v0; s_and_saveexec_b64 vcc lanes 21..63 43 | 21<<8 = 5419
; out[7] control: s_mov_b64 s[22:23], vcc (a real mask); s_and_saveexec_b64 lanes 21..63 5419
; group: 64,1,1
; mattr: -wavefrontsize32,+wavefrontsize64
; rsrc2: 0x8090
; Run: VKC_SGPRS=0x3ffffc00,0x0000ff00 vk_compute_test cs_w64_sgpr_mask.spv 1 dump --require-wave64
; -> out[0..7] = 2580 2588 2588 2588 2588 2612 5419 5419
v_mov_b32_e32 v10, 0
v_mov_b32_e32 v11, 0
v_mov_b32_e32 v12, 0
v_mov_b32_e32 v13, 0
v_mov_b32_e32 v14, 0
v_mov_b32_e32 v15, 0
v_mov_b32_e32 v16, 0
v_mov_b32_e32 v17, 0
s_mov_b64 exec, -1
s_mov_b64 s[24:25], 0x3ffffc00
s_and_saveexec_b64 s[6:7], s[24:25]
v_mov_b32_e32 v10, 1
s_mov_b64 exec, -1
s_mov_b64 s[20:21], s[4:5]
s_and_saveexec_b64 s[6:7], s[20:21]
v_mov_b32_e32 v11, 1
s_mov_b64 exec, -1
s_and_saveexec_b64 s[6:7], s[4:5]
v_mov_b32_e32 v12, 1
s_mov_b64 exec, s[4:5]
v_mov_b32_e32 v13, 1
s_mov_b64 exec, -1
s_and_b64 exec, exec, s[4:5]
v_mov_b32_e32 v14, 1
s_mov_b64 exec, -1
s_mov_b32 exec_lo, s4
v_mov_b32_e32 v15, 1
s_mov_b64 exec, -1
v_cmp_lt_u32_e32 vcc, 20, v0
s_and_saveexec_b64 s[6:7], vcc
v_mov_b32_e32 v16, 1
s_mov_b64 exec, -1
v_cmp_lt_u32_e32 vcc, 20, v0
s_mov_b64 s[22:23], vcc
s_and_saveexec_b64 s[6:7], s[22:23]
v_mov_b32_e32 v17, 1
s_mov_b64 exec, -1
v_mov_b32_e32 v20, 0
v_cmp_ne_u32_e32 vcc, 0, v10
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v21, s30
v_cmp_ne_u32_e32 vcc, 0, v11
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v22, s30
v_cmp_ne_u32_e32 vcc, 0, v12
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v23, s30
v_cmp_ne_u32_e32 vcc, 0, v13
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v24, s30
v_cmp_ne_u32_e32 vcc, 0, v14
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v25, s30
v_cmp_ne_u32_e32 vcc, 0, v15
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v26, s30
v_cmp_ne_u32_e32 vcc, 0, v16
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v27, s30
v_cmp_ne_u32_e32 vcc, 0, v17
s_bcnt1_i32_b64 s30, vcc
s_ff1_i32_b64 s31, vcc
s_lshl_b32 s31, s31, 8
s_or_b32 s30, s30, s31
v_mov_b32_e32 v28, s30
v_cmp_eq_u32_e32 vcc, 0, v0
s_and_saveexec_b64 s[6:7], vcc
buffer_store_dword v21, v20, s[0:3], 0 offen
buffer_store_dword v22, v20, s[0:3], 0 offen offset:4
buffer_store_dword v23, v20, s[0:3], 0 offen offset:8
buffer_store_dword v24, v20, s[0:3], 0 offen offset:12
buffer_store_dword v25, v20, s[0:3], 0 offen offset:16
buffer_store_dword v26, v20, s[0:3], 0 offen offset:20
buffer_store_dword v27, v20, s[0:3], 0 offen offset:24
buffer_store_dword v28, v20, s[0:3], 0 offen offset:28
s_endpgmAI-assisted (Claude): the fixtures were written with an assistant; I ran them on my own card. Synthetic programs only, no game files. |
|
Tested 03f93c7 on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): pass, no regression. This run does not cover the non- Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4.
AI-assisted run (Claude). Synthetic programs only, no game files involved. |
|
Thanks @borislisandro and @Impre-visible for the test runs, and @lyricalfrog3 for the GPU review. The review shows this PR only covers a mask that is a tracked AI-assisted (Claude Code). |
Claimed in the coordination issue as: SHADER-304 (claim). Found by @LouisRiverstone (finding).
What and why
s_mov_b32 s20, 0x3ffffc00leaves the raw constant in every invocation, butGen.mask_srcreads an SGPR as this lane's 0 / ~0 bit. So any non-zero constant counted as every lane:s_and_saveexec_b32 s6, s20enabled 32 lanes instead of 10..29. The immediate form was already right.tools/lane_masks.py: a newconstants(blocks, succ)pass, separate from the mask analysis, which is unchanged.s_mov_b32/s_mov_b64of an integer immediate, and copies of such an SGPR, to each instruction.v_add_co_u32.mask_srcreads them: an inline constant sign-extended, a literal with a zero high word.tools/rdna2spirv.py:mask_srcreads such an SGPR (or pair) as that constant through the existing immediate path. That covers saveexec, wrexec, the 64-bit lane-mask SALU ops andv_cndmask_b32_e64.s_mov_b32 exec_lo/exec_hi, sNtakes the constant path that immediates already use.docs/design.md: the "Constant lane masks" row describes the SGPR case and says it is not GPU-verified.What's left:
llvm-mc, so I can't assemble a GPU test shader here.s_and/or/xor_b32intoexec_lowith any constant still turns on every lane where EXEC is on. That includes the immediate form,s_and_b32 exec_lo, exec_lo, 0x0f0f0f0f. The same path on main does it, and the design row now says so.Tested
NVIDIA GeForce RTX 5060 Ti, driver 621.14, Windows 10 19045, Python 3.14. CPU only: these tests run the translated SPIR-V in the repo's interpreters.
test_sgpr_const_mask.py, lanes active after the op:s_mov_b32 s20, 0x3ffffc00+s_and_saveexec_b32 s6, s20s_mov_b32 s20, 0xff00+s_mov_b32 exec_lo, s20s_mov_b32 s20, 0xf0,s21, 1+s_and_saveexec_b64 s[6:7], s[20:21]v_cndmask_b32_e64 v1, 0x11, 0x22, s20withs20 = 0xff00Negative control: with main's
tools/rdna2spirv.pyback in place, 7 of the new subtests fail. The overwritten-constant cases and theandn2_wrexeccase pass either way, as they should. With the fix restored, all pass.The 3 pytest failures also fail on plain main on this machine, so this change didn't cause them. Two are Windows path separators:
test_make_game_assets_pools.pyexpectsdata/ttdb/...and getsdata\\ttdb\\.... The third istest_world_index.py::test_new_report_paths_that_differ_only_in_case: on NTFS,CAFE+ U+0301 is not refused as a collision.Not tested: a real GPU run (needs
llvm-mcfor a test shader), wave64 emulation, and the game corpus (no game files used).Checklist
docs/design.md, with how it was confirmed (CPU interpreter; marked not GPU-verified).AI-assisted (Claude Code).
🤖 Generated with Claude Code