Skip to content

Tools: wave64_halves, a VGPR written under a narrowed EXEC is tier C once EXEC widens - #4619

Open
fearkov wants to merge 2 commits into
ItsNotRexed:mainfrom
fearkov:fix/wave64-halves-narrowed-writes
Open

fearkov wants to merge 2 commits into
ItsNotRexed:mainfrom
fearkov:fix/wave64-halves-narrowed-writes

Conversation

@fearkov

@fearkov fearkov commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #4571 as: #4571 (comment) (@shdann's finding #4571 (comment))

The other finding, a vote whose mask is rewritten before the branch, is #4620's: it was claimed first. I dropped my commits for it, so the two PRs no longer conflict (git merge-tree exits 0) and the suites below pass on the merge of both.

What and why

tools/wave64_halves.py gave tier A to wave64 programs whose one 64-lane wave and two 32-lane waves export different values, after a VGPR was written under a narrowed EXEC. The lanes that are off still hold what the register held when EXEC narrowed, and they read it again once EXEC widens. put() replaced the register's taint, so a lane id, a readlane or a lane count that a narrowed v_mov covered in one lane was forgotten.

A narrowed write now records the register's taint at the narrowing, per EXEC token (st["p"], carried and joined with the rest of the state). widen() adds that taint back and drops the register's tags, which only described the lanes that were on. It runs where EXEC goes back to an ancestor token and wherever EXEC gets a value of unknown extent.

The finding suggested or-ing the taint at the write. I tried that first, and it breaks the tier B aggregated atomic. Its whole elect / atomic / v_readfirstlane sequence runs under a narrowed EXEC, and its rank and elected-lane tags are right for the lanes that are on. So nothing changes while EXEC is narrowed. The old taint comes back only when the off lanes become readable again, and an elected lane's tag stays, because v_readfirstlane reads that lane.

Listing (from the finding) main this branch 1x64 vs 2x32
lane id (v_mbcnt) covered in lane 1 under a narrowed EXEC A C lane-id 32 lanes differ
v_readlane_b32 s6, v0, 5 covered the same way A C cross-lane 32 differ
s_bcnt1_i32_b64 s6, exec covered the same way A C lane-count 63 differ
a constant covered the same way (control) A A equal

I left out the finding's ds_bpermute_b32 address variant because I didn't rebuild its listing.

Commits:

  1. the narrowed-write rule in tools/wave64_halves.py
  2. NarrowedWrites in tests/semantic/test_wave64_halves_soundness.py: the three listings are tier C and their two runs on the CPU wave interpreter really diverge, plus the control

Tested

Linux, on main d0835dd. Each commit passes tests/semantic/test_wave64_halves*.py and tests/test_wave64_halves_fixtures.py. With main's wave64_halves.py, exactly the 3 bug cases fail. tests/test_wave64_graphics_scan.py and tests/test_make_game_wave64.py are unchanged. Merged with #4620: 110 passed in tests/semantic/test_wave64_halves*.py.

Census needed: the rule can move corpus shaders from tier A or B to C. That's the point of it, but someone with the game should run python tools/wave64_halves.py --census <spirv>/index.json before and after.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.

@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 a09039c2 on Windows 11: tests/semantic/test_wave64_halves_soundness.py -> 38 tests, OK; git diff --check clean.

The PR covers both shdann findings: a VGPR written under a narrowed EXEC keeps the off-lanes' pre-narrowing taint (st["p"], re-applied by widen() when EXEC widens), and a vote tag on a mask register is dropped when that register or its pair partner is rewritten. The new soundness cases pin the tier-C outcomes and the elected-lane exception. No blocking issue in the change itself.

Duplicate/overlap: #4620 fixes the same rewritten-vote-mask issue alone and conflicts with this branch in tools/wave64_halves.py (git merge-tree --write-tree refs/remotes/pr/4619 refs/remotes/pr/4620 -> exit 1, conflict in that file). #4620 was claimed first for SHADER-820 (03:10 vs this PR's 03:16 claim, which names both findings). If this PR lands, #4620's tool hunk is redundant; if #4620 lands first, this PR should drop its vote-mask hunk and rebase, keeping the narrowed-write part.

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested a09039c 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

APPROVE. compileall clean; the new tests/semantic/test_wave64_halves_soundness.py 38/38, test_wave64_halves.py 73/73, plus _fixtures, test_wave64_cross_half, test_wave64_scmp_masks, test_wave64_subgroup_ops and test_wave64_vcc_data_branch all pass. Negative control: with main's tool the 6 new NarrowedWrites/StaleVotes cases fail while the 73 existing ones pass; at the head all pass. Merge clean.

One issue: tools/wave64_halves.py:904-911 keeps st["p"] entries for a still-live token and only drops them on a matching widen, so entries survive regions that end at :894 - bounded, but growing per VGPR x narrowing site. The unconditional tag drop at :918-920 is conservative only (tier C), which is fine.

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

@fearkov
fearkov force-pushed the fix/wave64-halves-narrowed-writes branch from a09039c to 302583e Compare November 23, 2026 03:31
@fearkov fearkov changed the title Tools: wave64_halves, a narrowed write's off lanes and a rewritten vote mask are tier C Tools: wave64_halves, a VGPR written under a narrowed EXEC is tier C once EXEC widens Nov 23, 2026
@fearkov

fearkov commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Kam471. You're right, #4620 claimed the vote part first. I dropped my two vote commits, so this PR is now only the narrowed-write rule (302583e). It merges cleanly with #4620 (git merge-tree exit 0), and tests/semantic/test_wave64_halves*.py passes on the merge of both: 110 passed.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested head 302583ea — 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" -> 34/34 OK.
  • Control (main's tools/wave64_halves.py + this test file): 3 failures — the mask-register-rewrite vote cases are not tier C on main, as intended.
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 302583e merged with main d0835dd on WSL2 Ubuntu (Python 3.12.16) and native Windows 11 (Python 3.13.1): pass. (An earlier head, a09039c, was a larger change; this is the narrowed one, and I tested only this head.)

… lanes

A VGPR written under a narrowed EXEC still holds, in the lanes that are off, what it held when EXEC narrowed, and those lanes read it again once EXEC widens. The write replaced the register's taint, so a lane id, a lane count or a readlane it covered in one lane passed as tier A while one 64-lane wave and two 32-lane ones exported different values (shdann's finding). Each such write now records the old taint per EXEC token, and widening EXEC back adds it and drops the tags, except an elected lane's, which v_readfirstlane reads back.
@fearkov
fearkov force-pushed the fix/wave64-halves-narrowed-writes branch from 302583e to e81227b Compare November 23, 2026 04:16
@fearkov

fearkov commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main 0db7fed (e81227b). Round 43 added ua to the analyser state, and both it and p are kept at each join, block copy and EXEC write. The rule hasn't changed: on main's wave64_halves.py the three NarrowedWrites cases still fail, and here they pass. tests/semantic/test_wave64_halves*.py, tests/test_wave64_halves_fixtures.py, tests/test_wave64_graphics_scan.py and tests/test_make_game_wave64.py: 199 passed.

@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 CPU verification of #4619 e81227bc combined with #4655 f502d1fc in tree 39b664c6. A literal ADD_TID load followed by an overwrite of lanes 0–31 and restoration of launch EXEC still reports tier A when the mask is set with v_cmpx_gt_u32_e32 32, v0. The upper 32 lanes differ between wave64 and two wave32 halves. Replacing just that instruction with v_cmp_gt_u32_e32 vcc, 32, v0 then s_and_b64 exec, exec, vcc gives the same 64 outputs in each configuration and correctly reports tier C (lane-id). Lane 40 exports 1040 versus 1008 with either spelling.

The existing operands() checks v_cmpx through NO_DEST before reaching the branch that adds its implicit EXEC definition. That bypasses #4619’s restore tracking for the original sequence. The explicit EXEC control shows the new state logic can recover the taint. Clear-bit, idxen, and full-EXEC overwrite controls remained A. Six synthetic modules validated for Vulkan 1.2. Windows, uv CPython 3.12.13, CPU interpreter only.

@kapitche

Copy link
Copy Markdown
Contributor

Tested e81227b (already contains main 98fa503) on Linux (Python 3.12.16): pass - test_wave64_halves_soundness.py 34 OK; all tests/semantic/test_wave64*.py 202 OK; negative control: tools/wave64_halves.py from main, PR test kept: 3 failures (the three narrowed-write tests, NarrowedWrites) of 34.

@sh1shu1

sh1shu1 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Ran the 6 SHADER-819 cases from #4778 against e81227b: the lane-id, v_readlane and s_bcnt1 listings and Kev-Ball's ds_bpermute rebuild (in #4778) are tier C, the clean-value control stays A, and the interpreter runs diverge as expected (bpermute: lanes 33..63, lane 33 = 32 against 33). On main 98fa503 the 5 bug cases say A. So the bpermute variant you left out is covered by this rule. #4778 is closed as a duplicate; I'll offer these as a tests-only PR after this merges.
AI-assisted (Claude Code, Claude Fable 5.1).

@465d6a0eac

Copy link
Copy Markdown
Contributor

Census (TEST-1006) of the rebased head e81227bc: no tier moves; 279 PS that were already tier C change reasons.

Setup. The head is rebased onto 98fa503f, which has the same tools/ as 0db7fed5. I compared it with 0db7fed5, 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; no other shader reaches wave64_halves. WSL Ubuntu, Python 3.14.4, llvm-mc 21.1.8, SPIRV-Tools v2026.1, CPU only. Both sides translate all 13,405 and pass spirv-val on all 13,405. The .spv files are byte-identical on both sides, so only the tier metadata changes. On 0db7fed5, wave64_halves.py --census gives A 5,054 / B 106 / C 8,245 (VS 1,455 / 79 / 702, PS 3,404 / 27 / 7,543, GS 195 A).

  • Tiers. At the head they are the same A 5,054 / B 106 / C 8,245, with all per-stage counts equal.
  • Reasons, all in PS that were already tier C:
shaders lost gained
230 cross-lane
20 cross-lane
20 stale-scalar
7 stale-scalar cross-lane
2 stale-scalar
  • Totals. Tier C cross-lane goes from 4,612 to 4,829, and stale-scalar from 3,487 to 3,462.
  • The pre-rebase head. 302583ea against d0835dd9, before SHADER-439: the pixel kill early exit is exact on 32-wide subgroups (+1,022 corpus shaders to tier A) #2674, gave the same picture: no tier moves and 261 PS (210 / 22 / 20 / 7 / 2 in the same five groups). The rule only adds taint, so it can only move a shader toward tier C. On the corpus it moves none, because every shader it reaches was already tier C.
  • Question. In 47 shaders a reason disappears: 40 lose one outright and 7 swap stale-scalar for cross-lane. Is that expected, for example because widen() drops a tag that stale_uses() or the cross-lane check classified by? All of them stay tier C, so no decision changes on the corpus.

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

@memorypressure

Copy link
Copy Markdown

Verified on Linux against main 3faac55: merges cleanly, compileall clean, the soundness suite runs 34 tests OK, the full wave64 semantic discovery (202 tests) and tests/test_wave64_halves_fixtures.py pass on the merged tree. The narrowed-EXEC replay rule (widen/join_pre) is new — nothing equivalent on main. One suggestion: the detector now tiers a previously-A shape as C, but the design.md wave64 row was not amended — a one-line contract update would match house practice for rule changes. Merges cleanly alongside #4620. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@AirKyzzZ

Copy link
Copy Markdown
Contributor

TEST-1055: independent before/after census of the wave64 graphics tiers for this PR, over the full corpus (counts only)

Before is index.json from a full make_game.py run at 3faac557 on Windows 10 (RTX 2060). After is 3faac557 with this PR (e81227bc) merged, which merged cleanly, translated on an M3 Pro with the flags translate() passes (wave64 emulation on, no fallbacks).

VS A/B/C PS A/B/C GS A not wave64 translated / spirv-val valid
before 1455 / 79 / 702 3404 / 27 / 7543 195 PS 1127 26,503 / 26,503
after 1455 / 79 / 702 3404 / 27 / 7543 195 PS 1127 26,503 / 26,503
  • No shader changes tier: 0 transitions.
  • 281 entries change only their tier C reason list (279 PS, 2 VS, all tier C before and after). cross-lane goes 4612 → 4829 (+237, −20), stale-scalar 3487 → 3462 (+2, −27), and the waterfall idiom 35 → 33. Every other index field is identical in all 26,503 entries.
  • Host independence: a 501-shader slice (--shard 0/53) at 3faac557 on the Mac matches the Windows entries one for one.
python3 -I tools/rdna2spirv.py <dxbc>/disasm --out <out> --shard i/10 --no-asm --wave64-emulate   # i = 0..9
python3 -I tools/rdna2spirv.py <dxbc>/disasm --out <out> --merge
python3 -I tools/wave64_halves.py --census <index.json>

No game data here, only counts from my own 1.0.2.1 copy. AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted review (Claude Code). No game files; hand-written listings; CPU wave interpreter only (Windows 11, Python 3.14.7).

Head e81227bc, and the same head merged with main 561b65a8 (the merge is clean). I ran 35 hand-written vertex listings as one 64-lane wave and as two 32-lane waves.

What is correct now

  • The four listings of the finding are tier C, and the two runs differ for each: lane id, v_readlane_b32, s_bcnt1_i32_b64, and the lane-id tag of a ds_bpermute_b32 address.
  • The v_cmpx spelling that whattheflop reported is tier C on the merge with main (main has wave64_halves: count v_cmpx as an EXEC write #4585 now).
  • A value copied to a second register after the widening is tier C for these widenings: s_or_b64 exec, exec, s[12:13], s_mov_b64 exec, <saved copy>, s_xor_b64 exec, exec, s[12:13], s_andn2_b64 exec, s[12:13], exec, s_cselect_b64 exec, ..., s_wqm_b64 exec, exec, a second v_cmpx after a restore, a nested region, and the exit of an execnz loop.
  • Controls stay tier A with equal results: a constant under the narrowed write, a read of the register in the same narrowed region, and cross-lane reads in the narrowed region (v_mov_b32_dpp, v_permlane16_b32, ds_bpermute_b32, ds_swizzle_b32).

One defect: a SAVEEXEC form that does not narrow switches lanes on, and widen() does not run.

exec_write() sends each SAVEEXEC form to new_exec(). For the forms that are not a narrowing (s_andn2_saveexec_b64, s_or_saveexec_b64, s_andn2_wrexec_b64, ...) the new EXEC can contain lanes that were off, but none of the three widen() calls is on that path. These are the instructions that a compiler uses to go from the "then" lanes to the "else" lanes.

v_mbcnt_lo_u32_b32 v6, -1, 0
v_mbcnt_hi_u32_b32 v6, -1, v6            ; v6 = lane id
v_cmp_eq_u32_e32 vcc, 1, v0              ; v0 = lane number
s_and_saveexec_b64 s[12:13], vcc         ; EXEC = lane 1
v_mov_b32_e32 v6, 0                      ; the narrowed write
s_andn2_saveexec_b64 s[14:15], s[12:13]  ; EXEC = s[12:13] & ~EXEC: all lanes but lane 1
v_mov_b32_e32 v7, v6                     ; copies the lane id of the lanes that were off
s_mov_b64 exec, s[10:11]
v_mov_b32_e32 v6, 0                      ; v6 is clean at the end; v7 is exported

Tier A with no reasons at this head and on the merge with main. One 64-lane wave exports v7 = 32 in lane 32, two 32-lane waves export 0 (32 lanes differ). s_or_saveexec_b64 s[14:15], s[12:13] and s_andn2_wrexec_b64 s[14:15], s[12:13] in the place of the sixth line give the same result. The taint of v6 comes back at the s_mov_b64 exec, but v7 has its copy from before.

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_mbcnt_lo_u32_b32 v6, -1, 0", "v_mbcnt_hi_u32_b32 v6, -1, v6", "v_cmp_eq_u32_e32 vcc, 1, v0",
        "s_and_saveexec_b64 s[12:13], vcc", "v_mov_b32_e32 v6, 0", "s_andn2_saveexec_b64 s[14:15], s[12:13]",
        "v_mov_b32_e32 v7, v6", "s_mov_b64 exec, s[10:11]", "v_mov_b32_e32 v6, 0"]
(tier, reasons, _), asm = meta_of("vs_narrowed_else", vs(body))
whole, halves = out_param(run_cut(asm, 64)), out_param(run_cut(asm, 32))
print(tier, reasons, whole[32][:2], halves[32][:2])      # A [] (0, 32) (0, 0)

Proposal: in the SAVEEXEC branch of exec_write(), call self.widen(st, None) when the form is not a narrowing (after new_exec()), as the other EXEC writes of unknown extent do.

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.