Skip to content

wave64_halves: allow two-sided EXEC-narrowing vote branches (SHADER-739) - #5747

Open
sb17-stack wants to merge 1 commit into
ItsNotRexed:mainfrom
sb17-stack:fix/shader-739
Open

sb17-stack wants to merge 1 commit into
ItsNotRexed:mainfrom
sb17-stack:fix/shader-739

Conversation

@sb17-stack

Copy link
Copy Markdown

Allows Analysis.vote() in tools/wave64_halves.py to accept structured two-sided vote branches where one branch narrows EXEC to reg and the other narrows EXEC to ~reg (s_andn2_saveexec_b64, s_andn2_wrexec_b64, or s_andn2_b64) before reaching a common join block.

  • Added Analysis.narrows_not() and two-sided narrowing/join check in tools/wave64_halves.py.
  • Added test_vote_if_else_both_sides_narrow_exec in tests/semantic/test_wave64_halves_soundness.py.
  • Verified all 132 test_wave64_halves*.py tests and 3 line-ending tests pass; synthetic inputs only, no game data.

SHADER-739

@SweatierKey

Copy link
Copy Markdown
Contributor

Independent CPU soundness verification on Linux (Python 3.13.5, head a46a6ac7, base main 561b65a8) — soundness bug found (do not merge as-is):

  1. False Tier A on a genuinely divergent 64-wide vs 2×32-wide shader (32/64 lanes wrong):
    A two-sided vote branch where one side narrows to EXEC & reg and the other narrows to EXEC & ~reg is not wave-width invariant when one 32-lane half has reg != 0 and the other half has reg == 0.

    • On native 64-wide hardware, the whole wave sees reg != 0 and takes only the EXEC & reg branch — the EXEC & ~reg branch never executes for any lane.
    • Under 2×32-wide splitting, the half with reg == 0 takes the EXEC & ~reg branch, where EXEC & ~0 == EXEC keeps all 32 lanes of that half active and executes its side effects!

    Reproducible counterexample using s_andn2_b64 exec, exec, vcc (explicitly accepted by narrows_not() at tools/wave64_halves.py:1362):

    body = [
        "v_mov_b32_e32 v6, 77",
        "v_cmp_eq_u32_e32 vcc, 1, v0",     # lane 1 (half 0) has vcc=1; half 1 has vcc=0
        "s_cbranch_vccz 4",                # jumps to s_andn2_b64
        "s_and_saveexec_b64 s[14:15], vcc",
        "v_mov_b32_e32 v6, 3",
        "s_mov_b64 exec, s[10:11]",
        "s_branch 3",
        "s_andn2_b64 exec, exec, vcc",     # EXEC = EXEC & ~vcc -> all 32 lanes of half 1 stay active!
        "v_mov_b32_e32 v6, 9",
        "s_mov_b64 exec, s[10:11]",
        "v_add_nc_u32_e32 v6, 1, v6",
    ]
    • main 561b65a8: classifies as ('C', ['unsafe-vote-branch'], []) (sound).
    • #5747 a46a6ac7: classifies as ('A', [], []), while run_cut(asm, 64) produces 78 on lanes 32..63 and run_cut(asm, 32) produces 10 on lanes 32..63 (32/64 lanes mismatched). The reversed s_cbranch_vccnz (neg_then_pos) form has the exact same 32/64-lane mismatch.
  2. s_andn2_saveexec_b64 / s_andn2_wrexec_b64 semantics reversed in narrows_not():
    On RDNA2, s_andn2_saveexec_b64 D, S computes EXEC = S & ~EXEC (activating previously inactive lanes where S == 1), not EXEC & ~S (s_andn1_saveexec_b64 is ~S & EXEC).

  3. The new test passes on unmodified main and does not exercise narrows_not():
    In test_vote_if_else_both_sides_narrow_exec, s_cbranch_vccz 2 skips only 2 instructions (s_and_saveexec_b64, v_mov_b32) and lands on s_mov_b64 exec, s[10:11] (offset +4 would be s_andn2_saveexec_b64), so main 561b65a8 already classifies the test fixture as ('A', [], []).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested a46a6ac with the Python translator tests only, no GPU: test_wave64_halves.py and test_wave64_halves_soundness.py run 132 tests OK, including test_vote_if_else_both_sides_narrow_exec. With the translator tools from the base commit 3faac55 the new test file also passes ("Ran 31 tests", OK), and a probe of the saveexec-shaped vote on both the base and head tools prints tier A with no reasons, so the new test does not fail without the change. I could not find the 3 line-ending tests the description names.

AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested a46a6ac7 on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_wave64_halves_soundness.py" -> OK

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

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

Head a46a6ac7a277, based on 3faac557b (1,806 commits behind 561b65a). Cherry-pick onto 561b65a is clean.

Commands: python -I -m pytest tests/semantic/test_wave64_halves*.py -q -> 159 passed, 22 subtests on the merged tree (132 on the stale head); python tests/semantic/test_wave64_halves_soundness.py -> 34 OK; git diff --check clean.

Corpus: re-analysed all 14,532 VS/PS/GS listings with wave64_halves.analyse (300/300 sampled tiers equal the stored index): 0 tier moves (0 C->A, 0 C->B, no A/B->C) and 0 reason changes. #5429 is unchanged by this PR: A 5,054 / B 106 / C 8,245. Current 561b65a baseline is A 5,059 / B 101 / C 8,245 (5 A/B drift, no C change).

Verdict: request changes.

  • narrows_not also accepts s_andn2_b64 exec, exec, reg: that is EXEC & ~reg, so a 32-wide all-no half runs the else block (saveexec/wrexec are reg & ~EXEC and zero it). Synthetic if/else: tier A here, but 64-wave output 4/1 vs two 32-wave 4/10; base is tier C unsafe-vote-branch. Drop that arm or prove its else side inert.
  • The added test does not exercise the change: s_cbranch_vccz 2 lands on the s_mov_b64 exec/s_branch before the else block, so main already returns tier A. Offset 4 reaches s_andn2 (base C -> PR A); it still never covers s_andn2_b64.
  • docs/design.md:463 still says the vote's other side must be the join with no code of its own.
  • Merge-tree conflict with #4541 (same vote()/test area): needs rebase/merge order.

@RSK2707X

Copy link
Copy Markdown

Hardware confirmation of @SweatierKey's counterexample, as a wave64 pixel shader on a GPU that can require both 64 and 32 on the fragment stage: AMD Radeon RX 7600 (RDNA3, gfx1102, subgroup 32..64), AMD 26.11.2 (LLPC), Windows 11 10.0.26300.

I wrote the counterexample as a ps_w64_* fixture in the shape of ps_w64_c_vote_else: only pixel (5, 7) votes yes, s_cbranch_vccz 4 goes to an s_andn2_b64 exec, exec, vcc side (v6 = 9), the other side is s_and_saveexec_b64 s[14:15], vcc (v6 = 3), both restore exec from s[10:11], then v_add_nc_u32 v6, 1, v6 (v6 starts at 50). Source below.

Translator tier (build.py index):

  • main 561b65a8: C, ['unsafe-vote-branch']
  • with this PR's tools/wave64_halves.py and tools/rdna2spirv.py: A, no reasons

GPU, w64_graphics.py (main + the fixture, entry ("C", _c_vote_narrow2, {}), r0 = 4 for the pixel, else partition dependent): ps_w64_c_vote_narrow2 C ok. That means the two sizes differ only in the partition-dependent word. r0 counts over the 64x64 image from w64_graphics.draw:

required size r0 = 4 r0 = 51 r0 = 10
64 1 63 4032
32 1 31 4064

At 64 the pixel's whole wave takes the vcc side and its 63 neighbours keep 50 + 1. At 32 the 32 lanes of the half without the pixel take the andn2 side, where exec & ~vcc keeps every lane, and get 9 + 1. So the shader is not partition invariant, and tier A from this PR would be wrong on real hardware, not just in run_cut. Same outcome as ps_w64_c_vote_else on this card, which stays C on both.

tests/test_wave64_halves_fixtures.py with the fixture added to EXPECT as ("C", ["unsafe-vote-branch"], None): 10 passed on main (Linux, llvm-mc 18, SPIRV-Tools from source). I can open it as a separate test-only PR so the case stays pinned, if that's useful.

ps_w64_c_vote_narrow2.asm (hand-written, no game data)
; mattr: +wavefrontsize64,-wavefrontsize32
v_interp_p1_f32_e32 v2, v0, attr0.x
v_interp_p2_f32_e32 v2, v1, attr0.x
v_interp_p1_f32_e32 v3, v0, attr0.y
v_interp_p2_f32_e32 v3, v1, attr0.y
v_mul_f32_e32 v2, 0x42800000, v2
v_mul_f32_e32 v3, 0x42800000, v3
v_cvt_u32_f32_e32 v2, v2
v_cvt_u32_f32_e32 v3, v3
s_mov_b64 s[10:11], exec
v_xor_b32_e32 v4, 5, v2
v_xor_b32_e32 v5, 7, v3
v_or_b32_e32 v4, v4, v5
v_mov_b32_e32 v6, 50
v_cmp_eq_u32_e32 vcc, 0, v4
v_cndmask_b32_e64 v7, 0, 1, vcc
s_cbranch_vccz 4
s_and_saveexec_b64 s[14:15], vcc
v_mov_b32_e32 v6, 3
s_mov_b64 exec, s[10:11]
s_branch 3
s_andn2_b64 exec, exec, vcc
v_mov_b32_e32 v6, 9
s_mov_b64 exec, s[10:11]
v_add_nc_u32_e32 v6, 1, v6
v_mov_b32_e32 v8, 0
exp mrt0 v6, v7, v8, v8 done vm
s_endpgm

Run with an AI assistant (Claude).

@wasonidas

Copy link
Copy Markdown

Verdict: needs changes. Windows 11 and Python 3.15.2, synthetic CPU fixtures. Tested PR head a46a6ac7a27795e90df85019cf16a7d6ec719ace against main 561b65a832218fbb9e6d2effd392a8eef093a3f8.

Three additional counterexamples reproduce beyond the previously reported live else-arm value:

  1. EXEC widens before the join. The no arm narrows with s_andn2_saveexec_b64, restores incoming EXEC, then writes v7 = 99. With only lane 2 voting yes, wave64 exports v7 = 0 in lanes 32–63. The emulated upper half takes the no arm and exports 99. The WREXEC variant also reproduces.
  2. A literal VCC mask is accepted as a lane vote. Replacing the lane comparison with s_mov_b64 vcc, 4 yields v6 = 0 at lane 34 on wave64 and v6 = 7 under 2×32. Both paths restore EXEC and the join overwrites the no-arm value before export.
  3. SCC can refer to an earlier VCC value. Compare lane 2 into VCC, set SCC with s_cmp_eq_u64 vcc, 0, overwrite VCC with a comparison against lane 40, then branch on SCC. Lane 40 exports v6 = 7 on wave64 and v6 = 0 under 2×32.

The PR classifies all three cases as tier A. Current main and the local comparison commit 59c4ffdb classify them as C. The safe control with a join overwrite has equal outputs on all 64 lanes and the PR correctly classifies it as A. The wave64 semantic subset passed 199 tests on the PR. The broader suite had three failures and three errors, all reproduced with the tools from the original base.

AI-assisted.

@vanoizer

Copy link
Copy Markdown
Contributor

Tested a46a6ac on AMD gfx1036 (26.11.2) and RTX 4070 Ti SUPER (621.14), Windows 11: pass.

  • PR tests: test_wave64_halves_soundness.py, 31 passed.
  • GPU: AMD 201/217, NVIDIA 170/217 native and 201/217 emulated, 0 failed. These counts are lower than main's because the branch is 1806 commits behind it.
  • semantic: 3 failures and 2 errors in test_loop_ballot_barrier (emulate modes). They are identical on this branch's base 3faac55 without the PR, and main 561b65a passes all 6150, so they come from the old base and not from this change. A rebase on main should clear them.

Setup: Windows 11. GPUs are an AMD Ryzen 7 9800X3D iGPU (RDNA2 gfx1036, driver 26.11.2, subgroup 32..64) and an RTX 4070 Ti SUPER (NVIDIA 621.14, subgroup 32). Each PR was tested in its own worktree with:

  • the PR's own tests (pytest);
  • python -m unittest discover -s tests/semantic;
  • scripts/run_gpu_tests.py three times: AMD native wave64, NVIDIA native, and NVIDIA with LEONIDA_W64_EMULATE=1.

Baseline is main 561b65a8:

Run Passed
AMD native 215/232, 0 failed
NVIDIA native 183/232, 1 failed
NVIDIA emulated 214/232, 1 failed

The NVIDIA failure is ms_wave_info_s3 (known, #5741).

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.

8 participants