Repository navigation
Translator: gather a lane mask's bits where a merge takes it as data (SHADER-WAVE-LOOP-EXEC-VOTE) - #2501
Translator: gather a lane mask's bits where a merge takes it as data (SHADER-WAVE-LOOP-EXEC-VOTE)#2501Tyrannys wants to merge 6 commits into
Conversation
The translator keeps a lane mask as each lane's own bit (0 or ~0) and data as one number in every lane. lane_masks.py takes a register that is a mask on one path to a merge and a number on another as data, but nothing gathered the mask's real bits on the mask path. The compiler makes such a merge when it walks the lanes of a ballot with a scalar loop: `s_and_b64 s[26:27], vcc, exec`, then s_ff1_i32_b32 / s_xor_b32 of s26 and `s_mov_b32 s26, sN` on the back edge. On the first pass each lane read its own 0 or ~0 as the number, found its own first bit and took its own number of passes, where the wave takes one. Natively the subgroup diverged at a ballot; under --wave64-emulate the per-lane counts were wrong. lane_masks.insert_data_merges now puts an s_mask_to_data_b32 (the translator's own instruction) before the branch of a block where a register is a mask and every successor takes it as data, when a successor may still read it. The translator gives it the wave's bits (lane_mask_word). A later mask use of the register stays exact, as mask_src takes bit L of a data register. wave64_emulate_opt counts the new instruction as a mask word reader, so it never sits in a folded lane-local region. Tests: tests/semantic/test_ballot_bit_loop.py (a hand-written wave64 loop of the shape of a library compute program that ran to the loop guard in ProjectLeonida#1608; native wave64 and --wave64-emulate on 32-wide subgroups, structured and rounds) fails on main and passes; tests/test_lane_masks.py covers where the instruction goes and where not. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The game run you asked for, at head 80894fc. It ends the runaway loop. Two things to look at: the picture differs from a narrower variant of the same fix, and the change reaches about half of the compute library. AI-assisted (Claude Code). Setup. RTX 4080 16 GB, NVIDIA 621.24, Windows 11, 32 GB. My earlier recipe: nothing skipped, loop guard at its default for every loop, 600 s, one run each. My library is built with the one-lane-fold branch's tools, so I applied this PR's
So yes: this is what kept loop 2 at the guard, and the run survives. Picture (by eye, screenshots every 60 s, one run each, so weak). No fix and the narrower prototype look alike: a lit scene in every shot from 300 s on. With this PR the scene is much darker with banding on some surfaces, and the shots at 361 s and 481 s are entirely black (420 s is not). It does not wash out to white. I cannot say which of the two is right, only that the two variants of the fix do not give the same picture. A repeat of each would be the next check. Reach. Whole pipeline library, Correction to my #1608 comment 7227273277: with plain liveness my own copy does not make "9 sites" in this program. It makes 23 insertions over the rounds, on seven registers besides the two ballot words, and the module has 73 barriers. I miscounted from a cut-off log. I can run any variant you want on the same recipe: this branch with the narrower condition, or a repeat of either for the picture. |
|
Correction to the picture paragraph above: withdraw it. I repeated both variants once (same recipe, same machine), and the picture is not stable from run to run, so it says nothing about either variant. AI-assisted (Claude Code).
No device loss or exception in any of the four. So the two variants cannot be told apart by the picture on my setup, and my earlier remarks about loop limits changing the picture (near white at 2,048, black at 64) were single runs too and should be read the same way. What makes the exposure swing between runs is a separate question I have not looked into. What stands from the comment above: the pass's time and the frame rate (now two runs each), the barrier counts, and the reach of 1,396 of 2,635 library programs against 2. |
insert_data_merges gathered the wave's bits wherever a lane mask met data at a merge and the register was read again. Most such merges are only read as a mask again (written back to EXEC, ANDed with it, a v_cndmask mask), where each lane's 0 / ~0 is already exact: the translator takes bit L of a data register for lane L. The gather was a barrier round under --wave64-emulate in about half of the pipeline library for nothing. Now the gather goes in only where the register can reach a scan of its lane bits (s_ff0 / s_ff1 / s_bcnt0 / s_bcnt1 / s_flbit) before it is written, also through scalar copies and bitwise ops of it (s_xor_b32 into a register moved back on a loop's back edge). Other number reads of such a merge are left as they were. On a local --wave64-emulate translation of the pipeline library this changes 31 of 2,684 programs (the wider rule changed about half) and none of the other shader folders. Tests: test_ballot_bit_loop gains a merged mask used only as a mask (exact with no gather); test_lane_masks covers a scan through a copy, and an add that gets no gather. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@quartz-heron-58 thank you for both runs, the reach count and the correction. That settled it: the loop ends, and the first head reached far too wide. The head is now 660f609 and uses your narrower condition. It gathers only where the merged register can reach On my local Could you run the new head once with the same recipe? Just the time per dispatch and the frame rate. Since the picture varies run to run, it does not need judging. If you can, also tell me how many programs change on your side. AI-assisted (Claude Code). |
|
Run of head 660f609, same machine and recipe (RTX 4080 16 GB, NVIDIA 621.24, nothing skipped, default loop guard, 600 s). It holds. AI-assisted (Claude Code). Reach on my side: the same as yours. Library with
The other 29: my earlier game runs request 9 of them. I retranslated those 9 the same way (all 9 differ from what I had), precompiled them with the runtime's
None of the 9 was a hot program before or after; each is dispatched about once a frame and costs hundredths of a millisecond either way, so nothing here says whether their results changed, only that they build, run and do not hang. One run; the frame-rate difference is within what I see between repeats. The other 20 changed programs are not requested in my runs. Picture not judged, as agreed. |
|
@quartz-heron-58 thank you, that closes the loop: the same 31 programs on both sides, |
|
Independently checked head Local semantic & dual-GPU verification:
Static code review notes:
Overall looks clean and well-tested. AI-assisted. |
…64-bit scan step() now names s_mask_to_data_b32 explicitly as writing data (it took the default data path before), so a second insert_data_merges finds nothing to insert; the diamond cases assert that. A 64-bit scan of the whole ballot (s_bcnt1_i32_b64 / s_ff1_i32_b64) gathers both s6 and s7. The docstring notes that under --wave64-emulate the gather is an exchange round that relies on its block being reached by the whole wave. Suggested by timbot103 in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@timbot103 thank you for the dual-GPU run and the review. All three items are in 4adc681:
|
|
Full translator semantic suite on 4adc681: 503 files, 6389 tests, 0 failed. AI-assisted (Claude Code). |
|
#2501 | head 4adc681 | python -I -m unittest discover -s tests/semantic -p test_ballot_bit_loop.py & python -I tests/test_lane_masks.py | red: test_ballot_bit_loop Ran 2 tests - FAILED (failures=6, errors=3); test_lane_masks AttributeError: module 'lane_masks' has no attribute 'insert_data_merges' | green: test_ballot_bit_loop Ran 2 tests in 4.253s - OK; test_lane_masks ok | full suite: 506 files, 6399 tests, 0 files failed in 415 s (vs main 02f71cc: 505 files, 6397 tests, 0 failed); stdlib 514/521 passed (0 new failures); compileall=0; node viewer_streaming=0 | Windows 11, Python 3.14.7, no game files | AI-assisted: yes, Antigravity (Gemini)
|
|
Thanks for this! Gathering a lane mask's bits into the wave's mask word where a merge uses it as data fixes ballot bit loops that diverged per lane. During maintainer testing (local, 2026-11-28) (head 4adc681, merged on main b1619e0 / 6dfca64, Windows 11, Python tests via pytest/unittest) we found:
To get this merged:
AI-assisted (Claude Code), posted for DadaCap (maintainer). |
…-LOOP-EXEC-VOTE) Records what s_mask_to_data_b32 does, where lane_masks.insert_data_merges puts it, why only bit scans get the gather, and how it was confirmed: the synthetic repro in tests/semantic/test_ballot_bit_loop.py, quartz-heron-58's game timing, the full semantic suite and timbot103's dual-GPU run. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@DadaCap thank you for the review. Both points:
Which do you prefer? I can move this PR onto that branch now and you review it against #1043's head, but the diff then shows #1043's commits until it merges. Or I keep the PR as it is and push the reworked version here as soon as #1043 merges. I'll go with your choice. AI-assisted (Claude Code). |
lixonik
left a comment
There was a problem hiding this comment.
Reviewed and verified at head 19f6f8e1b (Windows 11, Python 3.12, LLVM 21.1.8 llvm-mc, Vulkan SDK 1.4.363.1 spirv-as / spirv-val).
Verification
python -I tests/test_lane_masks.py -v: passed (ok).python -I tests/semantic/test_ballot_bit_loop.py -v: passed (2/2 tests, includingtest_a_merged_mask_used_as_a_mask_needs_no_gatherandtest_every_lane_walks_the_wave_ballotacrossnative64,emulate_sg32, andemulate_sg32_roundsfor counts 100, 200, and 7).python -I scripts/check_no_game_data.py: clean.
Review notes
lane_masks.insert_data_mergescleanly restrictss_mask_to_data_b32insertion to blocks where every successor drops the SGPR from mask to data and at least one successor has a live word-scan read (_numeric_live_in), leaving mask-only merges untouched.wave64_emulate_opt._MASK_WORD_READERSincludess_mask_to_data_b32so emulated wave64 exchange rounds stay out of lane-local regions, and thedocs/design.mdcontract row matches the implementation.
AI-assisted (Antigravity, Gemini).
|
@lixonik thank you for the review and for checking 19f6f8e, the contract row against the code included. A note for later: if DadaCap wants this moved onto #1043 (see the comment above), the head will change to the merged branch, whose only change in logic is the shared AI-assisted (Claude Code). |
Claimed in the coordination issue as: SHADER-WAVE-LOOP-EXEC-VOTE (#1608 (comment)). It follows up SHADER-LIB-LOOP-AT-GUARD and @quartz-heron-58's readback and per-loop limits (#1608 (comment)). Thanks to quartz-heron-58 for narrowing it to one loop.
What and why
The translator runs one invocation per lane. It keeps a lane mask as that lane's own bit (0 or ~0), and data as the same number in every lane.
tools/lane_masks.pyworks out which registers hold a mask. A register that is a mask on one path into a merge and a number on another path is taken as data from the merge on. But nothing gathered the mask's real bits on the mask path. So after the merge, each lane's 0 or ~0 was read as if it were the number.The compiler makes exactly this merge when it walks the lanes of a ballot with a scalar loop:
On the first pass each lane found its own first bit and took its own number of passes, where the wave takes one:
--wave64-emulate: the work inside the loop ran a wrong number of times for every lane. The exchange rounds also run per lane there.quartz-heron-58's limits per loop point at the loop that holds two such bit loops (one per half of a ballot), in a library compute program. With the guard at its default, that program took about 258 ms per dispatch; capped at 64 passes, 0.24 ms. With this branch quartz-heron-58 measured 0.075 ms per dispatch and 1.44 → about 2.2 frames per second, with no device loss (#2501 (comment)).
Fix.
lane_masks.insert_data_mergesadds a new translator-only instruction,s_mask_to_data_b32 sN, sN. It goes before a block's branch when:s_ff0/s_ff1/s_bcnt0/s_bcnt1/s_flbit, also through scalar copies and bitwise ops of it (thes_xor_b32into a register moved back on the back edge).For it,
rdna2spirvstores the wave's bits (lane_mask_word): the same number in every lane, as on hardware.wave64_emulate_optcounts the new instruction as a mask word reader. So it never ends up inside a folded lane-local region, where its cross-lane gather would run with only some lanes.Why only bit scans. A merged register that is only read as a mask again (written back to EXEC, ANDed with it, a
v_cndmaskmask) is already exact without the gather: the translator takes bit L of a data register for lane L (mask_src), and each lane's 0 or ~0 gives the same bit. The first version of this PR gathered for every read and changed about half the pipeline library (quartz-heron-58 counted 1,396 of 2,635) for no gain. Other number reads of such a merge (an add, a compare) would need the gather too. None is known in a shader, so they are left as before. That keeps the change to the lane loops.Reach. On a local
--wave64-emulatetranslation of the title's shaders, this changes 31 of 2,684 pipeline-library programs and none of the other shader folders (about 47,000 programs in all). The wider first version changed about 8,000.Tested
tests/semantic/test_ballot_bit_loop.py. It is a hand-written wave64 program in the shape above: an outer loop over each lane's work items, 64 apart, that ends whenv_cmpx_le+s_andn2_b64 saved, saved, execleaves no lane. Inside it are one bit loop over each half of a ballot, each with a lane-locals_cbranch_execzregion.--wave64-emulateon 32-wide subgroups (structured), and--wave64-emulatewith the round scheduler.main, all 3 native runs fail withSubgroupDivergenceError, and the emulated runs give wrong per-lane counts (for example 64 visits where 32 are expected).tests/test_lane_masks.py(new cases): checks where the instruction goes (before the branch, only for the register that is scanned, also through a copy). Nothing is inserted where the merge is only read as a mask, where a successor still reads a mask, where both paths carry one, or for an add.scripts/run_semantic_tests.py tests/semantic: 503 files, 6389 tests, 0 failed.GPUs tested: n/a (CPU interpreter).
AI-assisted: yes (Claude Code)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (Not a new instruction rule: a fix to how an existing mask-vs-data merge is lowered. I can add a row if reviewers want one.)🤖 Generated with Claude Code