Skip to content

Translator: gather a lane mask's bits where a merge takes it as data (SHADER-WAVE-LOOP-EXEC-VOTE) - #2501

Open
Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:claude/wave-loop-exec-vote
Open

Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:claude/wave-loop-exec-vote

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

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.py works 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:

s_and_b64      s[26:27], vcc, exec     ; the ballot: a lane mask
s_cmp_lg_i32   s26, 0
s_cbranch_scc0 done
loop:
s_ff1_i32_b32  s32, s26                ; s26 is a number from here on
s_lshl_b32     s32, 1, s32
s_xor_b32      s35, s32, s26
...                                    ; work for that lane, often a lane-local s_cbranch_execz region
s_cmp_eq_i32   s26, s32
s_cbranch_scc1 done
s_mov_b32      s26, s35                ; the back edge writes a number
s_branch       loop

On the first pass each lane found its own first bit and took its own number of passes, where the wave takes one:

  • Native wave64: the subgroup split up at a ballot.
  • --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_merges adds a new translator-only instruction, s_mask_to_data_b32 sN, sN. It goes before a block's branch when:

  • a register holds a mask at the end of the block,
  • every successor takes it as data, and
  • some successor may still read it (a liveness check, so no gather is paid for a register that is not read again).

For it, rdna2spirv stores 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, because mask_src takes bit L of a data register for lane L. wave64_emulate_opt counts 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

  • New 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 when v_cmpx_le + s_andn2_b64 saved, saved, exec leaves no lane. Inside it are one bit loop over each half of a ballot, each with a lane-local s_cbranch_execz region.
    • It runs on the CPU workgroup interpreter for 3 counts. The modes are native wave64, --wave64-emulate on 32-wide subgroups (structured), and --wave64-emulate with the round scheduler.
    • On main, all 3 native runs fail with SubgroupDivergenceError, and the emulated runs give wrong per-lane counts (for example 64 visits where 32 are expected).
    • With the fix, every lane matches the reference model.
  • 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.
  • Full translator semantic suite, scripts/run_semantic_tests.py tests/semantic -j 4: 502 files, 6387 tests, 0 failed.
  • On a local translation of the program from the readback (not shared), 18 instructions are inserted. Two of them sit exactly on the entries of the two bit loops.

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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. The program above is hand-written.
  • A new translator rule is in the contract table of 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

Tyrannys and others added 2 commits November 27, 2026 14:11
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>
@quartz-heron-58

Copy link
Copy Markdown
Contributor

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 tools/ diff to that copy (it applies cleanly) and retranslated only lib_cs_03010 with --wave64-emulate; everything else in the run is unchanged.

lib_cs_03010 translated with ms per dispatch frames/s, 250 to 410 s loss / exception barriers in the module
no fix 258 (in 46 of 58 profiler windows) 1.44 (1.16 to 1.60) none 50
this PR 0.075 (listed in 1 of 60 windows, under the 120-row cut in the rest) 2.23 (1.95 to 2.60) none 63
my narrower prototype under the cut in all 59 windows 2.13 (1.80 to 2.34) none 52

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, --wave64-emulate --no-asm --no-validate, this branch against main 43a9d56: 2,635 programs translate both ways and 1,396 of them come out different. In a random sample of 40 of those, 5 gained barriers (642 to 700 in the sample) and 35 have the same number; the differing programs grow 0.9% in bytes. The narrower prototype differs in 2 programs (lib_cs_03010, lib_cs_03012). Its condition is the one in my comment on #1608: gather only when the dropped value reaches s_ff1/ff0/bcnt/flbit before it is overwritten. None of the 1,394 others has been run in a game by me with this change.

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.

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.

2 participants