Skip to content

wave64_halves: a vote on a mask ends when the mask register is rewritten (SHADER-820) - #4620

Open
LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:w64-halves-stale-vote
Open

LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:w64-halves-stale-vote

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

SHADER-820, from @shdann's finding on #4571.

Bug. In tools/wave64_halves.py, put() dropped the destination's own tag but kept a ("vote", r) tag on SCC that names it. v_cmp into an SGPR pair or into VCC, s_mov_b64 and s_cselect_b64 don't write SCC. So after one of them rewrites the voted mask, s_cbranch_scc0 is a vote on the old mask, while the run side narrows EXEC to the new one. Analysis.narrows compares register names only, so the shader came out tier A.

v_cmp_gt_u32_e32 vcc, 20, v0
s_and_b64 s[20:21], exec, vcc
s_cmp_lg_u64 s[20:21], 0
v_cmp_gt_u32_e64 s[20:21], 64, v0     ; SCC unchanged
s_cbranch_scc0 3
s_and_b64 exec, exec, s[20:21]
v_add_nc_u32_e32 v6, 3, v0
s_mov_b64 exec, s[10:11]

On main this is tier A. Run as one 64-lane wave it writes v6 in lanes 20…63; as two 32-lane waves, the upper half votes no and skips, so 32 lanes differ. The same happens with s_mov_b64 s[20:21], exec as the rewrite, and with VCC as the voted register.

Fix. When put() writes the voted register, or the second SGPR of a voted pair, it drops the SCC vote. The branch is then judged as a plain SCC branch: here, tier C tainted-branch. An op that votes on its own destination (s_and_b64 s[20:21], exec, vcc) is unaffected, because it sets the new tag after its put(). EXEC writes are left alone; put() already skips them.

Tests, in tests/semantic/test_wave64_halves_soundness.py:

  • all three listings (v_cmp into the pair, s_mov_b64 into the pair, VCC) are tier C, and their 1×64 and 2×32 runs on spirv_wave_interp really diverge
  • controls: the same vote with no rewrite, and with a re-vote by s_and_b64 on the new mask, stay tier A and invariant

test_wave64_halves_soundness.py passes 33/33; on main the two regression tests fail and the controls pass. tests/test_wave64_halves_fixtures.py passes 9/9. python -I -m unittest discover -s tests/semantic: only main's known failures.

docs/design.md: the SHADER-10 row's vote rule now says a rewrite of the voted register ends the vote. That text appears twice in the row, and both copies are updated.

Neighbours on the same file: SHADER-819 (put() under a narrowed EXEC, @vibryxcx) and SHADER-821 (stale_uses, unclaimed) are separate. This PR only adds the SCC check at the top of put().

CPU only. No game files. Claimed on #4571 before starting.

AI-assisted (Claude Code): I reviewed and ran the change and the tests.

@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 7708d08f on Windows 11: tests/semantic/test_wave64_halves_soundness.py -> 33 tests, OK; tests/semantic/test_wave64_halves.py -> 73 tests, OK; git diff --check clean.

The change drops the ("vote", mask) SCC tag when either register of the voted pair is rewritten, so a later s_cbranch_scc is not treated as a vote on the new mask; the reduced repro and the tier-C expectation are in the added case.

Duplicate/overlap: #4619 covers the same rewritten-vote-mask issue as part of a larger wave64_halves change (narrowed-EXEC off-lane taints plus this vote fix). The two branches conflict in tools/wave64_halves.py (git merge-tree refs/remotes/pr/4619 refs/remotes/pr/4620 -> conflict). You claimed SHADER-820 first (03:10), so the coordinator may prefer this focused fix and have #4619 drop its vote-mask hunk on rebase; either order leaves one redundant hunk. Flagged on both.

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @Kam471 for the run and for spotting the overlap with #4619. This PR is only the SCC-vote check at the top of put() (SHADER-820, claimed at 03:10), and #4619 is SHADER-819's narrowed-EXEC taint plus the same vote fix. Whichever lands first, the other drops that hunk on rebase. I'll do it here if #4619 goes first. The two sets of regression tests don't overlap, so both can stay.

@theswiss24

Copy link
Copy Markdown

Tested 7708d08f (contains main d0835dd9) on two GPUs in a Dell Latitude 5501 (Linux Mint 22.3), test programs built from main d0835dd9 (this PR changes no C++), llvm-mc 20.1.2, LEONIDA_W64_EMULATE=1: pass. Every test has the same result as main on each device:

GPU driver run_gpu_tests.py
NVIDIA MX150 (Pascal) 580.178.04 185/210 passed, 25 skipped, 0 failed
Intel UHD 630 (Gen9.5, subgroup 8..32) Mesa ANV 25.2.8 181/210 passed, 27 skipped, 2 failed, the same 2 as main (cs_fmt_int_narrow, my finding in #4571, and cs_signed_zero_nan, the known Mesa 25.2 NIR issue)

No game files.

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

@Atomic258

Copy link
Copy Markdown

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

  • python -m unittest discover -s tests/semantic -p "test_wave64_halves_soundness.py" -> 33/33 OK.
  • Control (main's tools/wave64_halves.py + this test file): 2 failures — the rewritten-vote-mask cases are not handled on main, as intended.
  • git diff --check and scripts/check_no_game_data.py clean.
  • Heads-up: this overlaps Tools: wave64_halves, a VGPR written under a narrowed EXEC is tier C once EXEC widens #4619 — both patch the same vote-tag tracking in tools/wave64_halves.py for the mask-register-rewrite case (that one tagged SHADER-820 too). Whichever lands first will conflict with the other.

AI-assisted (Devin).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 7708d08 merged with main d0835dd on WSL2 Ubuntu (Python 3.12.16) and native Windows 11 (Python 3.13.1): pass.

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested 7708d08 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.

No game files. AI-assisted.

@tupacfan04

Copy link
Copy Markdown
Contributor

Approved with nits. The fix at tools/wave64_halves.py:380-387 is sound; 131 passed / 1 skipped, compileall clean, merge clean. Negative control: the 3 new tests fail on main's tool ('A' != 'C') and pass on the PR.

Two branches it names or adds are untested: s_cselect_b64 (:381) and a 32-bit write to the pair's second register (+1 at :386). It also overlaps #4619 in the same functions (no textual conflict), so whichever lands second should keep both behaviours.

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

@465d6a0eac

Copy link
Copy Markdown
Contributor

Census (TEST-1006) of 7708d08f against its merge base main d0835dd9, from my own 1.0.2.1 copy. Counts only.

Setup. Native mode (halves_analysis returns early under --wave64-emulate). The run covers the 13,405 wave64 VS/PS/GS, picked by the FSSM wave-size flag and the carve-index stage; no other shader reaches wave64_halves. WSL Ubuntu, Python 3.14.4, llvm-mc 21.1.8, SPIRV-Tools v2026.1.

Result: no tier moves.

d0835dd9 7708d08f
translated 13,405 13,405
pass spirv-val 13,401 13,401
tier A / B / C (--census) 4,033 / 85 / 9,283 4,033 / 85 / 9,283
.spv byte-identical 13,401 of 13,401
  • One reason change. One PS loses unsafe-vote-branch. It was already tier C and keeps tainted-branch, tainted-exec and first-lane. That's the PR's rule working: once the mask is rewritten, the branch is judged as a plain SCC branch, which this shader already failed. So the corpus has exactly one vote whose mask is rewritten before its branch, and that shader was already refused.
  • 4 PS fail spirv-val on both sides. The same 4 fail at both commits, with Operand ... requires a previous definition. The undefined operand is %fmt_size_arr's length constant, so this is SHADER-718 (Translator: constants assemble asks for late are defined too #3902, rdna2spirv: register the buffer-format table constants before the dump #4466), not this PR.

Tested on CPU only (no GPU run), under WSL on Windows 11.

…ten (SHADER-820)

@shdann: put() dropped the destination's own tag but not a ('vote', r) tag on
SCC naming it. v_cmp into the pair or VCC, s_mov_b64 and s_cselect_b64 don't
write SCC, so s_cbranch_scc0 voted on the old mask while the run side
narrowed EXEC to the new one: false tier A (one 64-lane wave writes v6 in 32
lanes two 32-lane waves skip). put() now drops the SCC vote when it writes
the voted register (or the pair's second SGPR); ops that vote on their own
destination set the new tag after it.

Tests in test_wave64_halves_soundness.py: shdann's three listings are tier
C and diverge 1x64 vs 2x32; an unchanged and a re-voted mask stay tier A
and invariant. docs/design.md SHADER-10 row extended.
@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 703dfc93 on Linux (x86_64, Python 3.13.5, SPIRV-Tools on PATH): 1 failure in tests/semantic/test_wave64_halves.py (passes on main 2d3369a4).

What I ran:

  • git diff --check main...HEAD: clean.
  • PYTHONPATH=tests/semantic python3 -m unittest tests/semantic/test_wave64_halves_soundness.py tests/semantic/test_wave64_halves.py: 133 passed, 1 failed:
    FAIL: test_c_mask_redefined_or_not_the_vote (tests.semantic.test_wave64_halves.KillExit.test_c_mask_redefined_or_not_the_vote)
    tests/semantic/test_wave64_halves.py:881:
      self.tier("ps_kill_redef", kill(KILL + ["s_mov_b64 s[20:21], exec"], RUN_WQM, EXIT_PS),
                "C", contains=["unsafe-vote-branch"])
    AssertionError: False is not true : ps_kill_redef: reasons ['tainted-branch'] lack ['unsafe-vote-branch']
    
  • Control on main (2d3369a4): test_c_mask_redefined_or_not_the_vote passes.

Cause:

  • The new invalidation of st["g"]["scc"] when s_mov_b64 s[20:21], exec overwrites the voted pair works as intended (still Tier C), so the branch now falls through to the tainted-branch reason rather than unsafe-vote-branch. KillExit.test_c_mask_redefined_or_not_the_vote in tests/semantic/test_wave64_halves.py just needs its expected reason updated (or checked) to match the new invalidation behavior.

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.

9 participants