wave64_emulate_opt: fold a region at most one lane of the wave runs (SHADER-W64-ONE-LANE-FOLD) #2447
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!2447
Loading…
Reference in a new issue
No description provided.
Delete branch "w64-one-lane-fold"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request successfully merged and closed
Claimed in the coordination issue as SHADER-W64-ONE-LANE-FOLD (jointly with @OreoFR): https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7178187388 (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: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7212426301 and https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7212532002n=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
Retail test of this exact head (7ccbbd876): 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,
TdrDelay=60.lib_cs_00962. In earlier runs it was the program active in 3 of 4 compute timeouts.main+ #2418: no device loss, and no early crash (the0xffffffffffffffffread I saw once with an older runtime didn't come back). With #2440, the world draws in these runs.Caveats:
AI-assisted (Claude Code, Claude Opus 5.5).
Independently checked head
7ccbbd876on Windows 11 Pro 26200, Vulkan SDK1.4.363.0, WSLllvm-mc 21.1.8, across NVIDIA GeForce RTX 5080 (616.64) and AMD Radeon(TM) Graphics (25.10.30.02), no game files.What passed:
test_wave64_one_lane_fold.py(3/3),test_wave64_emulate_opt.py(34/34),test_loop_ballot_barrier.py(15/15).run_gpu_tests.py --filter cs_w64(LEONIDA_W64_EMULATE=1,LEONIDA_VK_VALIDATION=1): 25/25spirv-valclean; 24/24 PASS on RTX 5080 and 24/24 PASS on AMD Radeon, 0 validation errors.valu_fuzz.py --family mem --wave 64 --emulate --programs 120 --seed 20261126: 120/120 match on RTX 5080 and 120/120 match on AMD Radeon.Three soundness bugs reproduced live (
spirv_mini_interpat7ccbbd876vsorigin/main50ce8f51a):A one-block self-loop inside a single-lane region loses its back-edge (
tools/wave64_emulate_opt.py:1428-1443, 1457-1471;tools/rdna2spirv.py:18905-18911):v_cmpx_eq_u32_e32 vcc, 0, v0; s_cbranch_execz .join), then in one.loop:block incrementv1(init 10), store tomem[0], decrements10(init 2),s_cmp_lg_u32 s10, 0; s_cbranch_scc1 .loop:origin/main(50ce8f51a):mem[0] = 12(2 iterations)#2447(7ccbbd876):mem[0] = 11(2nd iteration silently dropped)A compare writing
exec_locertifies all 64 lanes as a singleton (tools/wave64_emulate_opt.py:1165-1169, 1176-1202;tools/rdna2spirv.py:6315-6319, 6944-6948, 8106-8109):v_cmp_eq_u32_e64 exec_lo, 0, v0leaves lane 0 and lanes 32–63 active (the emitter preservesexec_hi), but the analysis marks all of EXEC as single-lane so a foldedv_readfirstlane_b32 s14, v0reads each upper lane's ownv0instead of0.s14per lane):origin/main(50ce8f51a):lane0=0, lane1=0xee, lane32=0, lane33=0#2447(7ccbbd876):lane0=0, lane1=0xee, lane32=32, lane33=33exec).Implicit VCC carry writes leave stale singleton VCC facts (
tools/wave64_emulate_opt.py:528-534, 1165-1169, 1189-1191;tools/rdna2spirv.py:10376-10383, 10421-10426):v_cmp_eq_u32_e32 vcc, 0, v0, the accepted shorthandv_add_co_ci_u32 v3, v1, v2(v1=0xffffffff, v2=1) implicitly updates VCC for every carrying lane in the emitter, while the analysis only tracks the VGPR destination; a subsequents_and_saveexec_b64then misclassifies full EXEC as single-lane. (Specific to the accepted shorthand; canonical LLVM disassembly prints explicit VCC operands.)v_readfirstlane_b32 s14, v0stored per lane):origin/main(50ce8f51a):lane0=0, lane1=0, lane32=0#2447(7ccbbd876):lane0=0, lane1=1, lane32=32Adding these three cases (plus a positive selector from lanes 32–63, e.g. lane 65 in a 2-wave workgroup) to
test_wave64_one_lane_fold.pywould make the fold airtight. AI-assisted.@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
exec_lo/exec_hino longer certifies the whole EXEC unless the other half is proven empty, and the shorthand carry ops invalidate VCC facts), with your three cases plus a positive selector in lanes 32-63 added totest_wave64_one_lane_fold.py. I'll also report how many corpus modules each fix changes, so it is clear whether @OreoFR's and @quartz-heron-58's retail numbers still apply to the same 20 modules. @zmasi thanks for the GPU and invariant check. AI-assisted (Claude Code).@timbot103 all three are fixed in a78040bb1, each with a test that fails on 7ccbbd876 and passes now:
cfg_structure.structurelike a multi-block region, so the emitter keeps the back-edge as anOpLoopMergeloop. Before, the back-edge was dropped. That was already true onmainfor a read-only one-block loop, which ran once. Test: self-loop with and without a store, selector lane 0, 40 and 100. Before: a lane got 11 instead of 12, in 6 cases.exec_lo/exec_hi. A half write of EXEC keeps "at most one lane" only if EXEC already was at most one lane and the value written is a compare, which is 0 in inactive lanes. A thread-id compare into one half no longer certifies the other half. Test: lane 40 read its own value (32) where 0 was expected; it now matches the round emulation.v_{add,sub,subrev}_co_ci_u32 vD, src0, src1is now rewritten where the listing is read (explicit_carry_vccintools/rdna2spirv.py, beforewiden_wave64_masks) into the explicit form with VCC as carry-out and carry-in. The emitter's special case is gone, and every analysis sees the VCC write, not only this one. I checked the other implicit-VCC writers the translator accepts; this short form was the only one not modelled. Test: lane 1 read 1 instead of 0 with the short form; the explicit form already passed.docs/design.md's row for SHADER-W64-ONE-LANE-FOLD now states the three rules.Tested:
test_wave64_one_lane_fold.py: 6/6 pass (8 subtests failed on 7ccbbd876). The upper-half selectors and the self-loop with a store still fold as one lane and match.python -I scripts/run_semantic_tests.py tests/semantic): 501 files, 6,380 tests, 0 failed (Windows 11).--no-asm --wave64-emulate,spirv-valon): 2,479 translated and valid onmain, on 7ccbbd876 and on a78040bb1. a78040bb1 against 7ccbbd876: 0 modules differ. None of the corpus programs hits the three cases, so the modules @OreoFR and @quartz-heron-58 measured on retail are byte-identical to what this head produces.main.check_no_game_data.pyon the changed files: clean.spirv-val.Found on the way, not changed here: in wave64 the emitter treats
v_cmp ... exec_loas a half write butvcc_loas a full write of the pair. The analysis matches the emitter; whether the hardware writes the whole EXEC pair for the first form is not checked.This comment was AI-assisted (Claude Code, Opus 5.5).
Retested the new head
a78040bb1(with the three self-loop fixes), merged ontomainec91d695c, on a Mac, where wave64 is always emulated: Apple M3, macOS 26.5.1. No game files.mainmain+ this PRLEONIDA_W64_EMULATE=1The 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 c0201a80:
test_wave64_one_lane_fold.pycopied onto main fails 14 (the one-lane cases,the half-EXEC write and the self-loop), so the tests check the change.
0 failed.
salu_diff --programs 500 --seed 1: 500/500 on main and on that merge.crosslane_fuzz --modes emulate --dpp --ext --programs 300 --seed 1(lavapipe): the same 2 mismatch + 3 hyp_onlyprograms on main and on the merge, so none of them come from these PRs.
_one_lane_stepagainst the ISA and found no soundness problem:s_andn2_saveexec_b64is only trustedthrough its source (EXEC = S0 & ~EXEC), the old EXEC goes on only through
s_and_saveexec_b64, a half-EXEC writeneeds the fact already, and
emit_folded_regionstill orders accesses from before the fold withx_order_barrier, so skippingx_memory_orderinside the fold only drops ordering between the lane's ownaccesses. v0 is
LocalInvocationId.xin the CS prologue, so thetidstart state holds.v_add_co_ci_u32 vD, src0, src1. This PR rewrites itto the explicit form right after the listing is read (
explicit_carry_vcc) and drops the emitter's ownexpansion. #2357 teaches
lane_masks.step,wave64_halves.operands,writes_execandwiden_wave64_masksabout the 3-operand form. With both merged, #2357's branches are not reached in
_translate(the operandsarrive already expanded). They still matter for callers that hand instructions to those helpers directly, so
nothing breaks, but whichever lands second may want to drop the duplicate or say which one is meant to stay. The
two merge together cleanly and pass together (above).
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).
python -m unittest discover -s tests/semantic -p test_wave64_one_lane_fold.py: 6/6 OK.Full-corpus check of the review fixes, asked by @MoonEyes before merging: whole
shaders/dxbccorpus (wave32 and wave64, not only the library),--no-asm --wave64-emulate,spirv-valon, 7ccbbd876 (the head OreoFR ran in the game) against a78040bb1 (this head)..spv), and none is only on one side.MEASURED.So the game runs on 7ccbbd876 (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 a78040bb1 + main 43a9d562d (clean; also merges cleanly onto current main, including #2435 merged just before):
test_wave64_one_lane_fold.py6/6, 14 fail with the code reverted to main; full pytest + semantic suites show no new failure vs main;check_no_game_dataclean. CPU-only tests on Windows 11 / Ryzen 5 3600. In game: OreoFR's runs on an RTX 4070 Laptop 8 GB at 7ccbbd876 (https://github.com/ProjectLeonida/mandy/pull/2447#issuecomment-7225440289), which carry over to this head: 0 modules differ in the full corpus (https://github.com/ProjectLeonida/mandy/pull/2447#issuecomment-7228168140), spot-checked here on two library programs. The short-form carry fix overlaps #2357; whichever lands second should drop the duplicate.AI-assisted (Claude Code, Claude Opus 5.5)