Skip to content

wave64_halves: allow tainted lane selector on uniform v_readlane_b32 (SHADER-314) - #4601

Open
plentilha wants to merge 1 commit into
ItsNotRexed:mainfrom
plentilha:pr/shader314-readlane-uniform-lane
Open

plentilha wants to merge 1 commit into
ItsNotRexed:mainfrom
plentilha:pr/shader314-readlane-uniform-lane

Conversation

@plentilha

Copy link
Copy Markdown
Contributor

Summary

Bounded SHADER-314 case in tools/wave64_halves.py:

  • In v_readlane_b32 d, v_src, lane, when v_src is proven uniform across all lanes (vr in st["u"]), the read result is identical across 32-lane halves even if the lane selector is tainted (for example, derived from s_ff1_i32_b64, s_bcnt1_i32_b64, or lane-id arithmetic), because every lane holds the same value in v[vr] and tools/rdna2spirv.py rewrites uniform_readlane to read the lane's own copy (OpCopyObject).
  • Updated tools/wave64_halves.py to classify v_readlane_b32 of a uniform VGPR as readlane-uniform (preserving any taint on v_src itself) regardless of lane taint.
  • Added positive (ps_readlane_uniform_tainted_lane -> Tier B, readlane-uniform) and negative-control (ps_readlane_varying_tainted_lane -> Tier C, first-lane) regression tests in tests/semantic/test_wave64_halves_soundness.py.
  • Added GPU fixture runtime/test/shaders/ps_w64_b_readlane_uniform_lane.asm (registered in runtime/test/shaders/w64_graphics.py and tests/test_wave64_halves_fixtures.py).

Verification

Tested on NVIDIA GeForce RTX 3070 (driver 621.24 / 32.0.16.2124, Windows 11, subgroup 32..32, llvm-mc 23.1.6, SPIRV-Tools v2026.4):

  • python -I tests/semantic/test_wave64_halves.py: 73/73 OK
  • python -I tests/semantic/test_wave64_halves_soundness.py: 30/30 OK
  • python -I runtime/test/shaders/build.py <work> vs_ids ps_w64_b_readlane_uniform ps_w64_b_readlane_uniform_lane: 3 shaders: 3 translated, 3 pass spirv-val (wave64 VS/PS/GS on 32-wide subgroups: tier B: 2)
  • python -I runtime/test/shaders/w64_graphics.py <work> runtime/build/vulkan ps_w64_b_readlane_uniform ps_w64_b_readlane_uniform_lane: both B ok
  • LEONIDA_W64_EMULATE=1 LEONIDA_TEXOFF_FALLBACK=1 python -I scripts/run_gpu_tests.py --build runtime/build/vulkan --require-pass: 195/210 passed, 15 skipped, 0 failed

No game data. AI-assisted.

@Kam471 Kam471 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.

Ran this at 9eee64d8 on Windows 11:

  • tests/semantic/test_wave64_halves_readlane_uniform.py -> 2 tests, OK: a uniform VGPR (v_mov_b32_e32 v5, s4) with a tainted selector (s_ff1_i32_b64 s6, exec) is tier B with the readlane-uniform idiom and no reasons; a varying VGPR keeps tier C with first-lane.
  • tests/test_wave64_halves_fixtures.py -> 6 passed, 1 skipped; the new ps_w64_b_readlane_uniform_lane.asm is registered in w64_graphics.py and the fixture list.
  • tests/semantic/test_wave64_halves.py -> 57 tests, OK.
  • git diff --check clean.

The reasoning holds: the check already requires the source VGPR to be in the uniform set, so every lane holds the same value and the selector — even one derived from first-lane/lane-count/lane-id — cannot make the two 32-lane halves differ. Dropping not tl only affects that uniform case, and the varying case stays tier C. No blocking issue.

@whattheflop whattheflop 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.

AI-assisted Windows CPU check at 9eee64d8 against direct parent 6a6704df and GitHub base d0835dd9, uv CPython 3.12.13. Eight synthetic fragment and vertex cases used valid selectors 0, 31, 32, and 63. They changed from Tier C with a shuffle on both comparison commits to Tier B without a shuffle. For each validated module, all 64 outputs matched the hand-derived result in a CPU-modeled wave64 and two wave32 halves. A separate selector probe confirmed the split really changed the selector to 32, 63, 0, and 31 respectively.

The vertex tests also left the uniform source untouched while EXEC narrowed and restored it before the read. An otherwise comparable branch that overwrote the source under narrowed EXEC stayed Tier C with the shuffle. A separate tainted-source control also stayed Tier C. Three stock fixture modules translated and validated with LLVM 21.1.8 and SPIRV-Tools. CPU interpretation only for the independent cases.

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I found two regressions from widening the rewrite at 9eee64d8. Both also reproduce with this change applied to main d0835dd9. The selector can be irrelevant when the source is truly uniform. These cases expose two ways the current analysis can incorrectly mark the source uniform.

Masked write after v_cmpx. In a wave64 vertex shader, seed v4 to 99 and then put 42 in the first 32 lanes.

s_mov_b64 s[10:11], exec
v_mov_b32_e32 v4, 99
v_mbcnt_lo_u32_b32 v10, -1, 0
v_mbcnt_hi_u32_b32 v10, -1, v10
v_cmpx_gt_u32_e32 32, v10
v_mov_b32_e32 v4, 42
s_mov_b64 exec, s[10:11]
s_ff1_i32_b64 s8, exec
v_readlane_b32 s9, v4, s8

Export s9 through a VGPR. On the 64-lane interpreter, the correct result is 42 on every lane. Main gives that result. This head marks the shader tier B, replaces the shuffle, and gives 99 on lanes 32–63. The same regression occurs with e64 v_cmpx and with s_bcnt1_i32_b64 as the selector on a full 64-lane wave. Applying #4585 restores all four cases.

Partial destination write. Replace the write sequence with v_lshlrev_b32_e32 v4, 8, v0, s_mov_b32 s4, 7, then v_mov_b32_sdwa v4, s4 dst_sel:BYTE_0 dst_unused:UNUSED_PRESERVE src0_sel:DWORD. With vertex index in v0, the upper bits still vary. The same first-set selector should read lane zero's 7 on the full 64-lane wave. This head returns 263 on lane 1 and 8199 on lane 32. An analogous WORD_0 case fails too. #4585 alone leaves these two failures. Applying #4373's destination tracking restores both.

I applied both fixes plus this change to main. All six cases retained the shuffle, returned the correct native wave64 words, and passed spirv-val. The PR's two new tests and all 73 existing wave64-halves tests passed with that stack, including the intended uniform-source case.

I also drew a 64×64 fragment variant of the first case on llvmpipe (LLVM 19.1.7, subgroup size 8). Applying this change alone altered 2,000 red-channel values from 2.0 to 1.0. With both fixes, the entire framebuffer matched main byte for byte.

Please stack this change on #4585 and #4373 or add equivalent source-uniformity guards before merging. Synthetic programs only. AI-assisted.

@tupacfan04

Copy link
Copy Markdown
Contributor

PASS. test_wave64_halves.py 57 OK, the new test_wave64_halves_readlane_uniform.py 2 OK, test_wave64_halves_fixtures 6 passed / 1 skipped, pytest tests/semantic -k wave64 127 passed, compileall clean, merge clean. Negative control: main's tools/wave64_halves.py fails with 'C' != 'B', so the rule is pinned.

The reasoning is sound: st["u"] is only reached with zero-taint, full-launch-mask sources (tools/wave64_halves.py:533), source taint is preserved at :448, and the translator drops the lane operand entirely (rdna2spirv.py:4655-4660), so a per-half lane index is never emitted.

Three docs/body fixes: docs/design.md:390 still says "143 of 144 test shaders" and its tier-B fixture list omits the new ps_w64_b_readlane_uniform_lane.asm; the body cites a nonexistent test_wave64_halves_soundness.py (the tests are in ..._readlane_uniform.py) and "73/73"/"30/30" counts that do not match this head (57). The GPU/build.py path is unverified here (no llvm-mc).

Approve with those. AI-assisted review, no game files, no GPU.

@theswiss24

Copy link
Copy Markdown

Tested 9eee64d8 (merged onto main d0835dd9) on two NVIDIA GPUs, with test programs built from main d0835dd9 (this PR changes no C++): pass on both. Every test has the same result as main on each device.

GPU driver, OS run_gpu_tests.py (LEONIDA_W64_EMULATE=1)
RTX 2060 SUPER (Turing) 580.178.04, Ubuntu 24.04.5 195/210 passed, 15 skipped, 0 failed
MX150 (Pascal) 580.178.04, Linux Mint 22.3 185/210 passed, 25 skipped, 0 failed (no mesh shaders or fragment barycentrics)

llvm-mc 20.1.2. No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@Atomic258

Copy link
Copy Markdown

Tested head 9eee64d8 — Windows, CPython 3.15.2, CPU only, synthetic fixtures, no game files: pass.

  • python -m unittest discover -s tests/semantic -p "test_wave64_halves_readlane_uniform.py" -> 2/2 OK.
  • pytest tests/test_wave64_halves_fixtures.py -> 6 passed, 1 skipped (the .asm fixture registers and parses).
  • Control (main's tools/wave64_halves.py + the new semantic file): 1 failure — the tainted lane selector on uniform v_readlane_b32 is not allowed on main, as intended.
  • git diff --check clean. scripts/check_no_game_data.py does not exist at this PR's base (it predates dac28e6), so main's version was run against this tree instead — clean.
  • Note: the branch is based on an older main (6a6704d); it will want a rebase before merge.

AI-assisted (Devin).

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