Skip to content

wave64_halves: a buffer_load with idxen at a wave-uniform address gives a wave-uniform VGPR (new SHADER item) - #4630

Open
shdann wants to merge 2 commits into
ItsNotRexed:mainfrom
shdann:wave64-uniform-buffer-load
Open

shdann wants to merge 2 commits into
ItsNotRexed:mainfrom
shdann:wave64-uniform-buffer-load

Conversation

@shdann

@shdann shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new SHADER item (claim)

Depends on: #4373

The first commit of this branch is #4373 (47b43d81). The change of this pull request is the commit after it.

What and why

tools/wave64_halves.py knows that a VGPR is the same in each lane only when a VALU instruction wrote it from immediates, scalars and other such VGPRs (st["u"]). A VGPR that a buffer_load wrote is never in that set. Thus this code is tier C (cross-lane, or tainted-branch when the scalar goes to a branch), although each lane loads the same word:

v_mov_b32_e32 v2, 3
buffer_load_dword v4, v2, s[0:3], 0 idxen     ; the same address in each lane
v_readfirstlane_b32 s20, v4                   ; tier C today
v_add_nc_u32_e32 v6, s20, v0

The research note for #94 names this as a probable cause of many tier C shaders (not measured).

The rule. Analysis.uniform_load puts the destination of a load into st["u"] when all of these conditions are true:

  1. The instruction is a MUBUF buffer_load_* with idxen, and not a d16 form (those keep half of the destination).
  2. EXEC is the launch mask (st["f"]), so each lane that the program runs does the load.
  3. The index and offset VGPRs are in st["u"]. The descriptor and soffset are scalars that are not a lane mask. No operand has a taint bit.
  4. The shader has no store and no atomic (MEM_WRITES).

After that, the rules that are already there do the work: v_readfirstlane_b32 and v_readlane_b32 of the VGPR are tier B (readfirstlane-uniform, readlane-uniform), and the translator makes a v_readlane_b32 of it read the lane's own copy.

Why it is exact.

  • With conditions 2 and 3, each lane calculates one address, one bounds result and one V# format. Then each lane gets the same word.
  • Condition 1, idxen: the "Swizzled buffer addressing" row of docs/design.md (measured on RDNA2) says that idxen ignores ADD_TID_ENABLE. Without idxen, a V# with ADD_TID_ENABLE adds the lane number to the index, and the V# is a run-time value. Thus a load without idxen is not accepted. The new test shows the two cases with one V#.
  • Condition 4: a 64-lane wave reads the word in all its lanes at the same time. Two 32-lane halves read it one after the other. A store or an atomic of the same shader (in a different wave) can change the word between the two reads.
  • The analyser already assumes that a load is a function of its operands (the result has the taint of its sources). This rule adds no new assumption about other shaders.

Tested

Windows 11 Pro (10.0.26200), Python 3.14.7, CPU only. No GPU, no llvm-mc, no SPIRV-Tools on this machine, so I did not build the GPU fixtures and I did not run spirv-val. No game files.

New test file tests/semantic/test_wave64_uniform_load.py (12 tests). Each program is a hand-written vertex shader. It is translated and then run on spirv_wave_interp.py as one 64-lane wave and as two 32-lane waves. The memory is a structured buffer of 16 records, record i = 1000 + i.

Program Tier 64 lanes against 2 x 32 lanes
v_readfirstlane_b32 of a load at a constant index B equal, 1003 + lane
v_readlane_b32 at lanes 40 and 7 of a load at a scalar index (s4 = 5) B equal, 1005 + lane
idxen and offen (index 3, offset 4, records of 8 bytes) B equal, 1007 + lane
the loaded value (1002 & 15) is the index of a second load B equal, 1010 + lane
a scalar branch on the loaded value, with code on the two sides (two memories, so each side runs) B equal
near miss: index = lane >> 3 C cross-lane different (lane 32: 1032 against 1036)
near miss: the load is under an EXEC of lanes 0 to 31 C cross-lane different (lane 32: 1035 against 32)
near miss: the index is written under an EXEC of lanes 0 to 31 C cross-lane different (lane 32: 1037 against 1035)
near miss: the index is written in part (v_mov_b32_sdwa ... UNUSED_PRESERVE) C cross-lane different (lane 32: 1035 against 32)
near miss: soffset is s_bcnt1_i32_b64 of EXEC C lane-count different (1019 + lane against 1011 + lane)
near miss: the shader has a buffer_store_dword or a buffer_atomic_add C tier only (the interpreter memory is read-only)
near miss: a load without idxen C cross-lane tier only; with ADD_TID_ENABLE in the V#, one 64-lane wave reads record lane in lane lane, and the idxen load with the same V# is tier B and equal

Negative controls.

The wave interpreter. spirv_wave_interp.py had no memory, so it could not run a buffer_load. It now takes memory (byte address to 32-bit word, read-only): OpConvertUToPtr to a PhysicalStorageBuffer pointer, OpAccessChain into it and OpLoad of one 4-byte element. A load from an address that the memory does not hold raises, and so does a store. tests/semantic/test_spirv_wave_interp.py has 4 new tests on a hand-written module (15 tests, all pass).

Other runs.

Check Result
tests/semantic/test_wave64_halves.py, test_wave64_halves_soundness.py, test_wave64_halves_partial_dst.py 73, 30 and 13 tests, all pass
python -I -m unittest discover -s tests/semantic 7,485 tests (16 more than the base), 7 failures and 3 errors. The base #4373 47b43d81 gives 7,469 tests with the same 7 failures and 3 errors on this machine, compared by test name: test_loop_ballot_barrier (5), test_cmp_class_f64 (2), test_mutations (2), test_vcmp16_opsel (1). None of them is in a file that this pull request changes
pytest on tests/test_wave64_halves_fixtures.py, test_w64_graphics_args.py, test_w64_graphics_reference.py, test_wave64_graphics_scan.py, the docs row tests, test_semantic_translator_use.py, test_text_write_line_endings.py, test_resource_hygiene.py 53 passed, 1 skipped (it needs llvm-mc)
python -I -m compileall -q make_game.py tools tests, git diff --check, scripts/check_no_game_data.py pass
9 more hand-written listings that try to break the rule (64 lanes against 2 x 32 lanes) see below

The 9 more listings: an index from an SGPR that is set in a skip region (tier C, different); a descriptor word from v_readfirstlane_b32 of a varying value (tier C, different); a load whose destination is its index register (tier B, equal); a load in a loop with a scalar trip count (tier B, equal); v_readfirstlane_b32 of the loaded VGPR under a narrowed EXEC (tier B, equal, as for a VGPR written from a scalar). One listing is tier B and the two runs differ: the load is in the run side of an EXEC vote that restores the launch mask before the join. That is the defect that #4541 corrects; the same listing with v_mov_b32_e32 v4, s4 in the place of the load is tier B on main and differs also. In a local merge of this branch with #4541, the two listings are tier C unconfined-vote-side and all tests of the two pull requests pass.

Effect on the emitted SPIR-V. The rule changes the tier metadata, and it adds instructions to the set whose v_readlane_b32 reads the lane's own copy (the rewrite that the readlane-uniform idiom already uses). No other emitted code changes.

Not measured: the corpus

I have no copy of the game, so I cannot say how many shaders this rule moves from tier C. If you have your own copy, please run the census before and after, counts only:

python tools/wave64_halves.py --census <spirv>/index.json

on an index translated with #4373 47b43d81 and on one translated with this branch. The numbers of interest are tier A/B/C for each stage, the reasons cross-lane, tainted-branch, tainted-exec and waterfall-scalar, and the idiom tags readfirstlane-uniform and readlane-uniform. A shader that has other reasons too stays tier C, so the reason counts can move more than the tiers.

Limits

  • Only MUBUF buffer_load_* with idxen. tbuffer_load_format_*, global_load_* and the image loads with an explicit level are possible later, each with its own test.
  • A load without idxen (a raw buffer) stays out because of ADD_TID_ENABLE. If the census shows that such loads are frequent, that needs a decision about run-time V#s.
  • A load under a narrowed EXEC stays out. wave64_halves: park pure SGPR spills (lanes 0..63) and widen sound Tier B idioms #2469 (st["ua"]) is the work for values that are uniform in the lanes that are on.

Overlap

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

AI-assisted (Claude Code): it wrote the change and ran all the checks above on this machine.

🤖 Generated with Claude Code

kotekompotek and others added 2 commits November 23, 2026 05:53
…t asymmetric DPP row_mask

Fixes partial-destination writes in tools/wave64_halves.py that dropped prior taint and falsely marked divergent VGPRs uniform (miscompiling v_readlane_b32 in rdna2spirv.py):
- Track destination read in operands() for SDWA dst_unused:UNUSED_PRESERVE with sub-dword dst_sel, single-half 16-bit VALU ops (including 8-byte VOP3 / _nc_16 / v_interp_p2_f16), partial-lane DPP (row_mask/bank_mask != 0xf or bound_ctrl:0), v_fmac_legacy_f32, and partial d16 loads.
- Flag half-asymmetric DPP row_mask ((row_mask & 3) != (row_mask >> 2)) and dpp8 in transfer().

Verified locally (CPU-only):
- tests/semantic/test_wave64_halves_partial_dst.py (13 OK)
- tests/semantic/test_wave64_halves_soundness.py (30 OK)
- tests/semantic/test_wave64_halves.py (73 OK)
- tests/semantic/test_wave64_cross_half.py (9 OK)
- tests/semantic/test_wave64_subgroup_ops.py (6 OK)
- pytest tests/test_wave64_halves_fixtures.py tests/test_wave64_graphics_scan.py (25 passed, 1 skipped)
- docs, line-ending, resource hygiene, tool startup, compileall, git diff --check
…es a wave-uniform VGPR

A v_readfirstlane_b32 / v_readlane_b32 of a VGPR that a buffer_load loaded was tier C (cross-lane,
tainted-branch) even when every lane loaded the same word, because only VALU results of scalars entered
the uniform set st["u"].

Analysis.uniform_load puts the destination of a load into st["u"] when all of these hold:
- a MUBUF buffer_load_* with idxen (idxen ignores ADD_TID_ENABLE; without idxen a run-time V# can add the
  lane number), not a d16 form;
- EXEC is the launch mask;
- the index and offset VGPRs are uniform, the descriptor and soffset are untainted scalars;
- the shader has no store and no atomic (the two halves read the word one after the other).

tests/semantic/spirv_wave_interp.py gets a read-only memory (OpConvertUToPtr, OpAccessChain and OpLoad
through a PhysicalStorageBuffer pointer), so that tests/semantic/test_wave64_uniform_load.py runs each
hand-written vertex shader as one 64-lane wave and as two 32-lane waves: 5 accepted shapes are tier B and
equal, 5 near misses are tier C and differ, 2 are classification only.

Builds on ItsNotRexed#4373: without its partial-write rule an index written in part by an SDWA UNUSED_PRESERVE counts
as uniform (test_c_index_written_in_part).

No game data. The corpus effect is not measured.

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

@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 f99eb65f on Windows 11. All four suites pass:

  • tests/semantic/test_wave64_halves_partial_dst.py -> 13 tests, OK
  • tests/semantic/test_wave64_uniform_load.py -> 12 tests, OK
  • tests/semantic/test_spirv_wave_interp.py -> 15 tests, OK
  • tests/semantic/test_wave64_halves.py -> 73 tests, OK
  • git diff --check clean.

The new _preserves_dst covers the partial-destination forms (RMW ops, SDWA with UNUSED_PRESERVE, asymmetric DPP row/bank masks, _e64/op_sel 16-bit writes, f16/u16/i16 results, d16 loads, image_load/image_sample with an odd dmask) and makes the preserved destination a source; uniform_load recognises a buffer_load with idxen from a wave-uniform address, excluding any memory the shader itself writes (MEM_WRITES). The interpreter is extended to run the partial-dst cases.

Merge check: git merge-tree with #4619, #4624 -> clean; with #4620 -> conflict only in docs/design.md (both edit rows there), no code conflict. No blocking issue.

@tupacfan04

Copy link
Copy Markdown
Contributor

BLOCKED: it needs a rebase and depends on #4373.

The rule itself is sound: with idxen, the ADD_TID lane term is dropped (docs/design.md "Swizzled buffer addressing"), the index/offset must be uniform, and MEM_WRITES excludes it; s_atomic_* being absent from MEM_WRITES:80 is moot because the parser rejects it as Unsupported. 182 tests pass across the new and existing wave64 suites, and the negative control (parent 47b43d81) fails 6 with "waterfall-scalar" instead of readfirstlane-uniform, so the tests pin the rule.

But: it conflicts with current origin/main in four files (docs/design.md:436, tools/wave64_halves.py:301,1034, tests/semantic/spirv_wave_interp.py:329,1040, test_spirv_wave_interp.py:280), and it is stacked on unmerged #4373 (its base is 03b09a08 and the branch carries #4373's partial-dst rule plus a 372-line test), so it must land after that.

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

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested f99eb65 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.

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.

5 participants