Repository navigation
wave64_emulate_opt: fold a region at most one lane of the wave runs (SHADER-W64-ONE-LANE-FOLD) - #2447
Conversation
…SHADER-W64-ONE-LANE-FOLD)
A forward must-analysis proves EXEC at most one lane after v_cmp(x)_eq_{i,u}32 <constant>, v0 (v0 the thread id,
not written since entry; the group one row of x or rows of whole waves) and through copies, ANDs and saveexecs of
that mask. Such an s_cbranch_execz region folds with several stores and in a cycle, orders no accesses inside, and
when it writes no EXEC its v_readfirstlane_b32 is the lane's own value. One 1024-thread library compute program:
639 -> 33 OpSwitch cases, spirv-val valid.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5
# Conflicts: # tools/rdna2spirv.py
…ojectLeonida#2102) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
|
Retail test of this exact head (7ccbbd8): the 20 library modules this rule changes, swapped into an otherwise current pack. RTX 4070 Laptop 8 GB (profile M), NVIDIA 621.40, Windows 11,
Caveats:
AI-assisted (Claude Code, Claude Opus 5.5). |
zmasi
left a comment
There was a problem hiding this comment.
Windows x64 / RTX 3090 GPU and semantic check of #2447 (7ccbbd876).
Environment: Windows 11 x64, NVIDIA GeForce RTX 3090 (driver 610.88, Vulkan 1.4.341, compute subgroup 32..32), VK_LAYER_KHRONOS_validation 1.4.363, llvm-mc 21.1.8, SPIRV-Tools 1.4.363.1.
Code & invariant check
- Entry
tiduniqueness (tools/rdna2spirv.py:18873):assemble()unconditionally initializes%vgpr[0](v0) toLocalInvocationId.x(c = 0inrange(gen.cs["tid"] + 1)). With linear invocation indext = x + sx * y + sx * sy * zand 64-lane waves[64w, 64w + 63],sy * sz == 1givesx = t, andsx % 64 == 0keepsy,z, andx // 64constant within each wave, sov0is distinct across all 64 lanes of a wave in both cases (and can collide only whensy * sz > 1andsx % 64 != 0, such as32 x 4 x 1). - Forward must-analysis (
_one_lane_step/_one_lane_inintools/wave64_emulate_opt.py):v_movrel/v_swaprelcleartid,v_swapkills both VGPR operands, and non-v_VGPR-writing ops killn + 1registers sotfedestinations cannot leavev0live intid._TID_EQrejectsmodsand_sdwa(which also excludes_dpp, as DPP ops carry modifier tokens and take VGPR sources), requires an integer immediate paired with a single-VGPRtidoperand, and kills any tracked scalar pair when either half (3 << k) orVCC(1 << _BIT_VCC) may be written.s_andn2_b64ands_andn2_saveexec_b64propagate single-lane status only fromsrc0(src0 & ~src1/ssrc & ~exec), whiles_and_b64accepts either source.
- Memory ordering &
v_readfirstlane_b32(tools/rdna2spirv.py:6139, 8106, 18945):emit_folded_regionstill emitsg.x_order_barrier()in uniform control flow beforelbl_dowheng.x_dirtyconflicts withfd["mem_reads"]/fd["mem_writes"], andx_memory_ordercontinues recording(kind, "r"/"w")ing.x_dirtyinside the fold so subsequent wave accesses afterlbl_endremain ordered.v_readfirstlane_b32is permitted only whenone_lane_in[run]holds and no block inregionwritesEXEC(any(blk_info[u][3] for u in region)is false); becauselbl_doexecutes only when%activeis true,EXECcontains solely the executing lane throughout the fold, makingself.dst(a[0], self.src_u(a[1]))exact for both SGPR andvcc_lo/vcc_hidestinations.
GPU verification on RTX 3090 (subgroup 32, VK_LAYER_KHRONOS_validation)
Adapted the 128-thread two-wave synthetic program from tests/semantic/test_wave64_one_lane_fold.py (3 LDS writes including two to one address, s_barrier, LDS readback, v_readfirstlane_b32 into s12 and vcc_lo, and two buffer stores to s[0:3] offset 0, plus lane 127's control store to offset 4) and ran all four cases through spirv-as, spirv-val --target-env vulkan1.2, and vk_compute_test <spv> 1 dump under VK_LAYER_KHRONOS_validation:
v_cmpx_eq_u32_e32 0, v0(wave 0 lane 0,2one_lanefolds):out[0..1] = 25, 134— PASS (0validation errors).v_cmpx_eq_u32_e32 65, v0(wave 1 lane 1,2one_lanefolds):out[0..1] = 90, 134— PASS (0validation errors).v_cmpx_eq_u32_e32 200, v0(no active lane,2one_lanefolds):out[0..1] = 0xeeeeeeee (untouched sentinel), 134— PASS (0validation errors).v_cmp_eq_u32_e64 s[42:43], 3, v0+s_and_saveexec_b64 s[44:45], s[42:43](2one_lanefolds):out[0..1] = 28, 134— PASS (0validation errors).
Semantic suites, negative controls & SPIR-V size check
python -I tests/semantic/test_wave64_one_lane_fold.py -v: 3/3 OK.python -I tests/semantic/test_wave64_emulate_opt.py: OK.- Negative control: dropping
and (sy * sz == 1 or sx % 64 == 0)intools/rdna2spirv.pymakestest_not_one_lane_keeps_the_rounds(rows of 32 threads) fail immediately (FAILED (failures=1)), confirming the multi-row wave-sharing guard is pinned. Restored. - Assembled SPIR-V size: across two
1.32 MBemulated wave64 compute modules in a local build, assembled SPIR-V drops from1,323,800 Bto850,816 B(-35.7%,235totalOpSwitchcases) and1,320,680 Bto847,712 B(-35.8%,235totalOpSwitchcases), bothspirv-valclean. python -I scripts/check_no_game_data.pyon all 4 changed files: clean.
No game data. AI-assisted.
|
Independently checked head What passed:
Three soundness bugs reproduced live (
Adding these three cases (plus a positive selector from lanes 32–63, e.g. lane 65 in a 2-wave workgroup) to |
|
@timbot103 thank you, these are real gaps in the analysis, and the repros make them easy to pin. I'm fixing all three in the rule itself (a self-edge region is rejected or gets its loop, a compare that writes only |
…mplicit carry VCC Three soundness gaps the review reproduced against the round emulation: - A region that is one block branching back to itself was emitted straight, so it ran once. It now gets a sub-CFG like a multi-block region and is emitted as a loop through cfg_structure.structure. This also covers the read-only self-loop that was already folded before the one-lane rule. - A write of one half of EXEC (v_cmp_eq_u32_e64 exec_lo, 0, v0) marked EXEC at most one lane, though the emitter keeps the other half's lanes. A half write now keeps the fact only when EXEC already had it and the half written is within it (a compare). - The short form v_add_co_ci_u32 vD, src0, src1 (also sub, subrev) wrote VCC only in the emitter. explicit_carry_vcc now gives it its VCC carry-out and carry-in operands when the listing is read, so every analysis sees the write, and the emitter's special case is removed. tests/semantic/test_wave64_one_lane_fold.py adds each case (8 subtests fail before) and picks in lanes 32-63 of both waves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
MSedgeMC
left a comment
There was a problem hiding this comment.
Verified on the PR branch: python3 -I scripts/run_semantic_tests.py tests/semantic -p test_wave64_one_lane_fold.py -> 1 file, 3 tests, 0 files failed (3 s, 6 jobs). Not approving yet: the branch is 596 commits behind current main, so a rebase is needed before the final diff can be reviewed.
|
@timbot103 all three are fixed in a78040b, each with a test that fails on 7ccbbd8 and passes now:
Tested:
Found on the way, not changed here: in wave64 the emitter treats This comment was AI-assisted (Claude Code, Opus 5.5). |
|
Retested the new head
The failures are the same on both sides. Translator suite: 503 files, 6393 tests, 0 failed. MEASURED, 1 run each. This says nothing about the game. AI-assisted (Claude Code, Claude Opus 5.5). |
|
CPU review on Linux, head a78040b merged with main c0201a8:
AI-assisted (Claude Code); I ran the tests and checked the results. Generated by Claude Code |
|
Merge test on an 8 GB laptop (profile M): game runs and this PR's own tests. A second agent on the same machine recomputed the numbers from the raw logs. AI-assisted (Claude Code, Claude Opus 5.5).
|
|
Full-corpus check of the review fixes, asked by @MoonEyes before merging: whole
So the game runs on 7ccbbd8 (OreoFR's, and quartz-heron-58's on the library) also cover this head: the three fixes change no module the game uses. They only matter for programs the corpus does not contain. Windows 11, Vulkan SDK 1.4.363.1, 8 shards. This comment was AI-assisted (Claude Code, Opus 5.5). |
|
Merge-tested a78040b + main 43a9d56 (clean; also merges cleanly onto current main, including #2435 merged just before): AI-assisted (Claude Code, Claude Opus 5.5) |
Claimed in the coordination issue as SHADER-W64-ONE-LANE-FOLD (jointly with @OreoFR): #1608 (comment) (narrowed from SHADER-W64-UNIFORM-EXECZ-BARRIER).
Depends on: none (#2102, which it was built on, is merged).
What and why
Under
--wave64-emulate, a compute program that picks one thread for a tail (clear LDS, then after a barrier sum it up and store) does it withv_cmpx_eq_u32 c, v0orv_cmp_eq_u32 m, c, v0+s_and_saveexec_b64 _, m. Such ans_cbranch_execzregion could not be folded: it has several stores, sometimes a loop, andv_readfirstlane_b32inside, so it went through the emulated rounds. On one 1,024-thread library program this takes the round switch from 639 cases to 33 (spirv-valvalid). A program it changes,lib_cs_00962, was the one active in 3 of 4 compute TDR timeouts @OreoFR saw on NVIDIA.A region whose EXEC holds at most one lane of the workgroup can be folded: no other lane exists to order against, and a
v_readfirstlane_b32in it reads that lane's own value.The rule
_one_lane_in(tools/wave64_emulate_opt.py) is a forward must-analysis that proves EXEC holds at most one lane, intersected at joins:v_cmp(x)_eq_{i,u}32 <constant>, v0, only whilev0(LocalInvocationId.x) has not been written since entry, and only when no two lanes of a wave can sharev0(a one-row group, or rows of whole waves). Compares with modifiers or SDWA are excluded.s_mov/s_and/s_andn2_b64of that mask, saveexecs and later compares.vcc_lodestination counts), any VGPR write tov0(movrel, swaps, memory results), and any EXEC write that is not a recognised narrowing.x_order_barrierbetween its accesses, andv_readfirstlane_b32copies the lane's own value when the region writes no EXEC. Scalar and VCC results of the region must already be dead at its end. The memory kinds it touches still go intox_dirty, so the next conflicting access after the fold still splits.%active.The contract row is in
docs/design.md.Tested
tests/semantic/test_wave64_one_lane_fold.py(hand-written programs, 128 threads in two waves, no game data): the workgroup interpreter oftest_wave64_emulate_opt.py, both invocation orders, compared with the plain round emulation; the constant picks a lane of wave 0, of wave 1, or none, throughv_cmpx_eq_u32and throughv_cmp_eq_u32+s_and_saveexec_b64. Negatives (a compare against another VGPR than the thread id, an overwrittenv0, a compare that is not an equality, rows of 32 threads) must not take the one-lane path and must still match.python -I scripts/run_semantic_tests.py tests/semantic: 501 files, 6,377 tests, 0 failed (351 s, 24 jobs, Windows 11).--no-asm --wave64-emulate, 8 shards, spirv-val on),mainagainst this branch: 2,479 translated andspirv-val-valid both times, the same set; exactly 20 modules change, the other 2,459 are byte-identical.spirv-valon both; 36 one-lane folds in total. A lockstep interpreter run at each program's real workgroup size, comparing memory writes, image texels and LDS betweenmainand this branch, found no divergence in any completed run; 16 of the 20 programs reached a fold (the rest rely on the static argument).TdrDelay=60, GPU timestamps around each dispatch:lib_cs_009621,412 ms mean / 1,754 ms max over 12 dispatches → 212 ms / 231 ms over 9 (about 6.7x). Before this library,lib_cs_00962was the program active in 3 of his 4 device losses (Aftermath timeout). With it: 6 runs, 0 device lost, 0 early crash, the world drawn in 4 of them: Coordinating who works on what (continued from #14) #1608 (comment) and Coordinating who works on what (continued from #14) #1608 (comment)n=1per library; picture correctness was not compared.python -I scripts/check_no_game_data.pyon the changed files: clean.GPUs tested: the measurements above (RTX 4070 Laptop, RTX 4080); my own runs on an RTX 5080 (621.24) used a library with this change. No GPU fixture is added: the rule is checked on the CPU interpreter against the round emulation.
AI-assisted: yes (Claude Code, Opus 5.5)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.🤖 Generated with Claude Code
https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr