Skip to content

SHADER-747: wave64_halves refuses a vote whose run side widens EXEC before the join - #4541

Open
tumbione wants to merge 1 commit into
ItsNotRexed:mainfrom
tumbione:fix/wave64-vote-exec-widening
Open

tumbione wants to merge 1 commit into
ItsNotRexed:mainfrom
tumbione:fix/wave64-vote-exec-widening

Conversation

@tumbione

Copy link
Copy Markdown
Contributor

SHADER-747 (claimed in #4084, carried to #4421).

What was wrong

Analysis.vote in tools/wave64_halves.py accepts a lane vote when the side that runs on "yes" starts by narrowing EXEC to the voted mask (narrows) and reaches the join. It did not check that EXEC stays inside the mask until the join. A 32-lane half whose lanes all voted no skips the whole side, so anything the side does outside the mask is done by the 64-lane wave only.

Smallest case, tier A with no reasons on main aa2988cc:

v_cmp_eq_u32_e32 vcc, 2, v0          ; v0 = lane id: only lane 2 votes yes
s_cbranch_vccz 4
s_and_saveexec_b64 s[16:17], vcc
v_add_nc_u32_e32 v6, 9, v6
s_or_b64 exec, exec, s[16:17]        ; EXEC restored inside the run side
v_add_nc_u32_e32 v7, 5, v7           ; runs in every lane of the wave

On spirv_wave_interp, one 64-lane wave exports v7 = 5 in lanes 32-63 and two 32-lane waves export 0 there. The same holds behind s_and_saveexec_b64 + s_cbranch_execz, and in a loop closed by s_cbranch_execnz whose body switches lanes back on.

The rule

confine_votes() runs after the reporting pass, once per accepted vote. It follows EXEC over the run side (its blocks up to the join, or back to the vote for a loop) as one of three states: inside the voted mask, as the skipped path has it, or anything else. It keeps the same facts for the mask registers saved or compared inside the side, through the 64-bit mov/and/or/andn1/andn2/xor, the saveexec/wrexec forms, v_cmpx and s_wqm_b64. The shader is tier C unconfined-vote-side when

  • a VGPR write, store, export or atomic runs while EXEC is not inside the mask;
  • the side reaches the join with EXEC in another state than the skipped path has, unless the join first overwrites EXEC with s_mov_b64 exec, <register>;
  • the side reaches the vote again in another state, or ends the program.

The rule is in the SHADER-10 row of docs/design.md.

Tests

tests/semantic/test_wave64_halves_soundness.py:

  • seven listings that main calls tier A. Six are vertex shaders that the test also runs as one 64-lane wave and as two 32-lane waves, and the outputs differ. The seventh is a pixel shader with a store after the restore (tier only, the interpreter runs vertex shaders).
  • seven confined shapes that must stay tier A with equal outputs: restore as the last instruction, restore from a copy saved before the vote, a join that overwrites EXEC, a nested skip region, s_wqm_b64 inside a skip region, a plain EXEC skip, an execnz loop that only narrows.

What I ran

Windows 11 (10.0.26300), NVIDIA GeForce RTX 5090, driver 616.56, Python 3.12, LLVM 21.1.8, Vulkan SDK 1.4.363.0.

Check Result
python -I -m unittest discover -s tests/semantic 7464 run, 7 failures and 2 errors, all in test_loop_ballot_barrier, test_cmp_class_f64 and test_mutations; the same nine fail on main aa2988cc without this change (see below)
python -m pytest tests/test_wave64_halves_fixtures.py tests/test_w64_graphics_args.py tests/test_w64_graphics_reference.py 13 passed
tests/test_docs_*.py, tests/test_design_contract_rows.py, python -m compileall -q tools tests pass
main's analyzer against this one on every wave64 graphics listing the semantic suite translates 598 analyses, 591 with the same tier and reasons; the 7 that change (A to C unconfined-vote-side) are the new refusal tests
runtime/test/shaders/build.py, with main's analyzer and with this one 234 translated, 234 spirv-val-valid, tiers A 9 / B 7 / C 13 both times, index.json identical
runtime/test/shaders/w64_graphics.py on the RTX 5090 15 ok, 11 skipped (tier C needs a device that can require both sizes), 0 failed
scripts/run_gpu_tests.py with LEONIDA_VK_VALIDATION=1 164 passed, 46 skipped, 0 failed, no validation messages

Failing on main too

Nine checks of the semantic suite fail here on an untouched checkout of aa2988cc and fail the same way with this change: test_loop_ballot_barrier (3 failures, 2 errors: 'emulated wave64 structured' != 'emulated wave64 rounds', Unsupported SPIR-V op OpFunctionCall), test_cmp_class_f64.test_rejected_forms (2: Unsupported not raised) and test_mutations (2: the v_mul_legacy_f32 anchor is not found). None of them reaches wave64_halves.py, and this pull request does not touch rdna2spirv.py.

Not measured

  • The corpus. I have no game files, so I do not know how many shaders get unconfined-vote-side, or whether any of them is a compiler shape the pass should accept. A before/after --census from someone with a work folder would settle it; the design row says the count is not measured.
  • Tier C on hardware. No GPU fixture is added: the RTX 5090 can only require 32 lanes, so it cannot show a tier C difference. The divergence is shown on the CPU wave interpreter.

Overlap

This touches the two self.vote(...) call sites and vote(), as #4138 (@tedin7) does, and sits next to SHADER-739 (if/else on a lane vote) and SHADER-742 (scalar side effects, @olegmkrv). The changes are independent; I will rebase on whichever merges first.

No game data involved. AI-assisted (Claude Code): it wrote the change and ran everything above on this machine.

🤖 Generated with Claude Code

… the voted mask

vote() accepted a lane vote when the side that runs on "yes" starts by
narrowing EXEC to the voted mask and reaches the join. It did not check
that EXEC stays there. After `s_or_b64 exec, exec, <saved>` inside that
side, a VGPR write or a store runs in every lane of a 64-lane wave, while
a 32-lane half whose lanes all voted no skipped the whole side: tier A,
and lanes 32-63 differ.

confine_votes() follows EXEC over the run side of every accepted vote
(inside the mask / as the skipped path has it / other, with the mask
registers saved or compared inside) and gives tier C
`unconfined-vote-side` when a lane effect runs outside the mask, when the
side reaches the join or the vote again in another state than the skipped
path, or when it ends the program.

Tests: seven listings that were tier A and diverge between one 64-lane
wave and two 32-lane waves on spirv_wave_interp, and seven confined
shapes that stay tier A. The corpus count of the new reason is not
measured (no game files here).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Atomic258

Copy link
Copy Markdown
Contributor

Tested 4d179e5535 on Linux, Python 3.10, CPU only, synthetic fixtures, no game files: pass.

  • python -m unittest discover -s tests/semantic -p test_wave64_halves_soundness.py → 38/38.
  • Negative control on main's tools/wave64_halves.py: 7 failures, so the soundness cases discriminate.
  • git diff --check and check_no_game_data.py clean.

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.

Ran this at 4d179e55 on Windows 11: test_wave64_halves_soundness.py -> 38 tests, OK; compileall and git diff --check clean.

The analysis now refuses a vote whose run side widens EXEC before the join, and the soundness suite covers the new refusal plus the existing accepted-vote cases. No blocking issue.

@mrbean56

Copy link
Copy Markdown

Tested 4d179e5535 on Linux (Debian 13), Python 3.13.5: pass, and the new reason is genuinely reachable.

  • pytest tests/semantic/test_wave64_halves_soundness.py -q: 38 passed.
  • Negative control: with 03b09a08's tools/wave64_halves.py and this test file, 7 tests fail, including test_vccz_run_side_widens_exec_before_the_join, so the test drives the analysis into the new unconfined-vote-side path rather than around it.
  • The fix is the conservative direction: recording the accepted vote's (mask key, run side, skipped side, EXEC registers) and rejecting a later widening of EXEC on the run side means the planner says "cannot prove" instead of emitting a half that would execute a lane the vote assumed inactive. For a soundness tool, refusing is always better than a wrong plan.
  • REASONS gains unconfined-vote-side, so reports and any golden files that key on a reason name stay self-describing.

No GPU, no game data. AI-assisted.

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

One side-effect class is not covered by the new confinement check.

Comment thread tools/wave64_halves.py
"""(EXEC state, mask facts, whether the instruction is a lane effect) after one instruction."""
op, a = base_op(ins["op"]), ins["args"]
dests, _, defs, _ = operands(ins)
effect = op.startswith(self.LANE_EFFECTS) or any(

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.

[P2] The effect set here covers lane effects and VGPR writes, but scalar s_sendmsg has no VGPR destination and is not modeled as an unconfined effect. A vote side can therefore remain tier A while issuing s_sendmsg sendmsg(MSG_GS, GS_OP_EMIT). The RDNA2 ISA describes GS/emit as a GS output message (section 12.5.1: https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content); a wave64 can issue one message while two split wave32 halves issue two. Please conservatively refuse side-effecting S_SENDMSG forms in accepted vote sides unless their partition semantics are modeled.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted review (Claude Code). I used no game files. All listings are hand-written.

Head 4d179e55. Windows 11, Python 3.14.7, CPU wave interpreter only (no GPU, no llvm-mc).

What I ran

  • tests/semantic/test_wave64_halves.py (73 tests) and test_wave64_halves_soundness.py (38 tests): all pass.
  • pytest on tests/test_wave64_halves_fixtures.py, the two test_w64_graphics_* files, test_wave64_graphics_scan.py and the docs row tests: 45 passed, 1 skipped (it needs llvm-mc).
  • Negative control: the new soundness tests with the analyser of main d0835dd9 give 7 failures.
  • 20 more hand-written vertex listings. I translated each listing and ran it on spirv_wave_interp.py as one 64-lane wave and as two 32-lane waves.

Result: pass for the rule in the title.

  • The rule refuses these run sides, and the two runs differ for each one: s_not_b64 exec, exec; s_orn2_b64 exec, exec, exec; s_cselect_b64 exec, s[16:17], exec; s_andn2_wrexec_b64 s[22:23], s[16:17]; s_mov_b32 exec_hi, -1; an EXEC vote whose run side restores the saved mask, writes a VGPR and then narrows again.
  • These shapes stay tier A, and the two runs are equal: if/endif (s_and_saveexec_b64, s_cbranch_execz); if/else with s_or_saveexec_b64 and s_xor_b64 exec; if/else with s_andn2_saveexec_b64; a nested if; s_wqm_b64 exec, exec in the run side; s_andn1_saveexec_b64 in the run side; a second v_cmp result that is ORed into EXEC in the run side; a restore with s_or_b64 exec, s[16:17], exec.

One case that the rule does not refuse. It is tier A on main also, so this pull request does not cause it. confine_vote gives the fact "in" to the register of the vote. For an SCC vote, that register can get a new value between the compare and the branch, because v_cmp into an SGPR pair, s_mov_b64 and s_cselect_b64 do not write SCC. Then the run side narrows EXEC to a mask that is not the voted mask.

v_cmp_gt_u32_e32 vcc, 20, v0          ; v0 = lane number: lanes 0..19 vote yes
s_and_b64 s[20:21], exec, vcc
s_cmp_lg_u64 s[20:21], 0              ; the vote
v_cmp_gt_u32_e64 s[20:21], 64, v0     ; all lanes; SCC does not change
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]

Tier A with no reasons on this head and on main. One 64-lane wave exports v6 = 35 in lane 32. Two 32-lane waves export 0 in lanes 32 to 63 (32 lanes differ). The VCC form does the same: s_and_b64 vcc, exec, s[16:17], then v_cmp_gt_u32_e32 vcc, 64, v0, then s_cbranch_scc0 and s_and_saveexec_b64 s[18:19], vcc.

To reproduce, from the repository root at this head:

# python -I repro.py
import sys
sys.path.insert(0, "tests/semantic")
from test_wave64_halves import meta_of, vs, run_cut, out_param
body = ["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", "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]"]
(tier, reasons, _), asm = meta_of("vs_scc_mask_rewritten", vs(body))
print(tier, reasons, out_param(run_cut(asm, 64))[32], out_param(run_cut(asm, 32))[32])   # A [] (35, 0, 0, 0) (0, 0, 0, 0)

Proposal: remove the SCC vote tag when an instruction writes the voted register, or give the fact "in" only when no instruction after the SCC writer writes that register. #2674 has this check for its own rule (kill_vote). It can be a part of this pull request or a separate item.

Two notes for the merge.

labj1987 added a commit to labj1987/Leonida that referenced this pull request Nov 23, 2026
…inned by a test

The kill rule's vote has no join: its run side is the rest of the program and ends it. A check of the votes
whose run side must rejoin the skipped path with EXEC restored and must not end the program (SHADER-747, the
`accepted` table of ItsNotRexed#4541) would make every kill exit tier C `unconfined-vote-side`. vote() now records the
votes the kill rule accepts in `kill_exits` (mask registers, run side, exit), and
KillExit.test_kill_vote_is_not_a_skip_region pins it: a kill with an if/endif on its run side stays tier A,
its vote is the only one in `kill_exits`, its run side never reaches the exit, and it is not in an
`accepted` table if the analysis has one. Whichever of ItsNotRexed#2674 and ItsNotRexed#4541 merges second has to keep the kill
votes out of `accepted` (checked: ItsNotRexed#4541's tool patch on top of this branch passes with them left out and
fails with them added).

Also renames confined/confine_step to kill_confined/kill_confine_step: ItsNotRexed#4541 adds its own confine_step,
which would silently replace ours on merge (every kill became analysis-error).

AI-assisted (Claude Code).

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

Copy link
Copy Markdown
Contributor

Census (TEST-1006) of 4d179e55: no tier moves; 2,271 tier C shaders gain unconfined-vote-side.

Setup. This head conflicts with today's main, so I compared it with its merge base d0835dd9, from my own 1.0.2.1 copy, counts only. 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. WSL Ubuntu, Python 3.14.4, llvm-mc 21.1.8, SPIRV-Tools v2026.1, CPU only. On both sides 13,401 pass spirv-val, and the .spv files are byte-identical. The 4 that fail are SHADER-718's %fmt_size_arr, the same 4 at both commits, and already fixed on main. On d0835dd9, --census gives A 4,033 / B 85 / C 9,283. Today's main has #2674, so a rebase will change the base numbers.

  • Tiers. At the head they are the same A 4,033 / B 85 / C 9,283, with all per-stage counts equal.
  • Reasons. 2,271 shaders that were already tier C (2,270 PS, 1 VS) gain the new unconfined-vote-side. No other
    reason changes. So the corpus does have votes whose run side widens EXEC before the join, but in every case another
    reason already refuses the shader.

AI-assisted (Claude Code). Part of TEST-1006 on the coordination issue.

@memorypressure

Copy link
Copy Markdown

FAIL to merge, verified: git merge-tree shows content conflicts in exactly docs/design.md and tools/wave64_halves.py against current main. At the PR head everything passes here too — soundness 38 OK (re-run), the wave64_halves suite 73, fixtures 9 in the venv, docs guards green. Main's cc4231d, 1222fd5 and 69bd312 cover the early-exit pixel-kill and related soundness corners but not this general run-side confinement, so this wants a rebase rather than closing. Four checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@labj1987

Copy link
Copy Markdown
Contributor

Note

🤖 Claude Opus 5.5 responding on behalf of labj1987

Heads-up for the rebase, @tumbione: main now has the kill rule from #2674 (d42f8b46), and two parts of it collide with this PR.

  1. Name clash. main has a static Analysis.confine_step(ins, inside, sub) (tools/wave64_halves.py:1261, called from confined() at :1252, returning 2 values). This PR adds an instance method confine_step(self, ins, ex, facts) returning 3. Both can merge without a textual conflict, and then the later definition wins: confined() gets a 3-tuple and every kill early exit becomes analysis-error, so the 1,022 pixel shaders measured on SHADER-439: the pixel kill early exit is exact on 32-wide subgroups (+1,022 corpus shaders to tier A) #2674 fall back to tier C. One of the two needs a new name.
  2. Kill votes in accepted. A vote accepted by the kill rule has a run side that ends the program, which is exactly what this PR's check refuses. If those votes go into self.accepted, confine_vote reports unconfined-vote-side for them. Checked by applying this PR's tool diff onto the kill rule: with the accepted record after the kill branch in vote(), 8 kill tests fail with unconfined-vote-side; with kill-accepted votes kept out of accepted, all pass.

A test that pins both: labj1987@aa1446d6 (KillExit.test_kill_vote_is_not_a_skip_region, kill votes recorded separately in kill_exits). Feel free to take it into this PR.

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