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 2 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. |
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 all loops limited to 65,536 passes, that program takes about 255 ms per dispatch. Limiting that one loop to 64 passes brings it to 0.24 ms. This PR is the translator side of that. Whether it ends that runaway loop in the game is not measured yet (see Tested).
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:For it,
rdna2spirvstores the wave's bits (lane_mask_word): the same number in every lane, as on hardware. A later use of that register as a mask stays exact, becausemask_srctakes bit L of a data register for lane L.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.A successor that still reads the register as a mask keeps the per-lane form, as before.
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 case): checks where the instruction goes (before the branch, only for the register that is read again), and that nothing is inserted where a successor still reads a mask or both paths carry one.scripts/run_semantic_tests.py tests/semantic -j 4: 502 files, 6387 tests, 0 failed.Not yet measured: the frame time and image in the game with this branch. I am asking quartz-heron-58 to run it in #1608.
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