Repository navigation
fix(shader): run while-shaped divergent loops in lockstep in halves - #591
Conversation
A first-lane read in a loop the 32-lane halves of a console wave do not run together is refused (FIRST_ACROSS): the halves cannot pass the console's first lane between them there. Halves make a divergent loop run together, in lockstep, only where its exit branch is at its latch (a do-while, predication shape 4): the latch branches on group_any_true of the lanes staying, so every host wave of the group runs every pass. The console compiler also lays loops out as while loops: the entry jumps to the test, the test branches back into the body while lanes stay, and the body's last block falls into the test, so the exit branch is at a block other than the latch. GTA VI's 23d507cbb6a22e86 and b0095eb50222e808 choose a table entry in such a loop, reading the entry's offset from the first lane in each pass; refused since pragyaangaur#522, their dispatches have been dropped. Predication now takes a single-exit while loop too (shape 5): the exit branch becomes the group-uniform one (FlatExit::LockstepExit), staying while any lane of the group stays, its stay edge carrying the lanes that stay into the rest of the body, and the latch jumps back to the header as before. The same checks as shape 4 hold: one back edge, no nested cycle, no side entry, the exit entered only from the exit branch, and no scalar the body writes read outside it. Of the title's 2,734 compute programs, six translate again (the two above among them) and none is newly refused. 65 others translate differently, with fewer group barriers in all (2,628). Tests: a first-lane read in a while loop gives the console's lanes on hardware in groups of 64 and 128, with lanes leaving the loop one by one (EXEC narrowed) and with a test every lane holds alike; it translates in groups of 64, 128 and 256. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Moving this back to draft: in game it has a performance regression we have to fix first. With this branch, four programs that translate without it ( Claude Code (Claude Opus 5.5). |
…lanes still in it A while loop run in lockstep keeps every lane of the group in its body until no lane of any wave stays: a lane that left runs the body inactive. while_loop_with_a_store has a store in that body, each pass a lane makes storing its count of passes at a dword of its own, lanes leaving after 0 to 7 passes. On hardware, in groups of 64 and 128, each lane's dwords up to its count hold their index and the rest stay 0: a lane that left stores no more. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Update after looking into it: this is not a lowering regression, so I'm marking it ready again, with the cost stated plainly.
The cost on an M5 Pro in the cell: waiting on the GPU ~320 → ~500 ms and copying back ~28 → 90–220 ms. Most of that is the title-memory path's copy-back of what those programs write, which is where we'll look next. The two luminance programs translate too. Claude Code (Claude Opus 5.5). |
|
Offline numbers for this branch, from replaying one captured frame of the cell (M5 Pro; 7 loops each; unchanged vs this branch's translations, in one process):
Submission 2 is almost all one compute program, The capture predates the three prefix-sum programs running, so it does not include the extra title-memory work described above. In game the two effects add up. Claude Code (Claude Opus 5.5). |
|
Why
Replay of submission 2 (7 loops, interleaved): main's translation 89.3 ms; pre-#522 (v9) 7.6; shader left out 7.6; atomics without our locks 87.6. So the cost is the translation, not the console algorithm or the 64-bit locks. This branch's lockstep loop carries the predicate into the loop's activity and leaves when no lane is active, which is why it fixes this program. 59 other programs (14 in this frame) have per-lane loops in predicated blocks that this branch doesn't cover. A separate small fix, ANDing such a loop's continue condition with the block's active predicate, would cover them. It would also stop inactive lanes from returning at the loop limit, where they would miss any in-step barrier after the loop. We are preparing it as its own PR. Claude Code (Claude Opus 5.5). |
|
@hackgods I ran this in game on an M5 Pro (48 GB, CrossOver 26.3), on top of d1609d9 + #589 #593 #605 #606 #608 #592 #595, with no first-lane fallback. Silent scripted opening into the cell, a cold run then a warm run, 420 s each. The good part: 23d507cbb6a22e86 and b0095eb50222e808 now translate and are bound in the world. 7b2c3c1d, 9991e29f and eeed82b8 are still refused at their first-lane reads, as you said. The problem: long host GPU submissions, which I don't see on any other build.
In the warm run, the stalls came before world entry, around overall frame 1,000, while loading. After the 51 s one, the game stopped at its own breakpoint on several threads, so the run ended there. 23d507cb and b0095eb5 aren't in any of the stalled submissions; they're first bound later, at frame 1,602 or 1,674. Seventeen compute programs are in all five stalled submissions: 130103b0f409c622, 186de5778eb555c8, 196bf6114ff3a131, 1e2975e51218868a, 2f03a2df10503cef, 369cbe906ea72764, 3f2cbdc7d80ca3da, 4e7bba07ffbe5625, 660814153af43185, 8317f8682f5b541c, b585907117661f1b, c9da71da40af2e28, cdd3d1fbc03932e7, d03e92d301ccb71e, dc20f390c4354fb4, f1eff11f31566a6e, f8f50ab85e7b10bb So my guess is that the lockstep rule now applies to a while-shaped loop in one of these that already translated before, and some lanes there run until the loop fuse. I'm checking which of the 17 translate differently with this PR and will post it here. Prepared with Claude Code. |
|
Thank you, that's a serious finding. Back to draft until it's explained and fixed. Of your 17 programs, at least Claude Code (Claude Opus 5.5). |
|
Tried this on an M3 Max (48 GB, CrossOver 26.1, Series X 1.0.2.1), on top of d1609d9 + #589 + #605 + #606 + #608, also with #610 and #620 merged. Same silent scripted opening into the cell, 720 s, Good news first: with it the BVH builder runs on this Mac for the first time. But the picture breaks shortly after world entry, in both runs with it:
From +80 on, the interior is dark, the protagonist is a near-black silhouette, and there are black blots along the window edge. It never recovers in the remaining ~9 minutes. The ended submission is the usual "the host GPU ended a submission before it finished, and what it drew is missing", Why I think it's this PR: the #610 switch on or off makes no difference, and #620 is inert in these runs ( |
|
Split run done: same exe with Across the three runs with the builder running, the break follows the ended submission, wherever it lands:
That's one ended submission shortly after entry in 3 of 3 runs with the builder running, and none in the run without this PR. My guess is that the lost submission takes something later frames keep reading (the structures, or a history the traces fill), so the picture never recovers. I haven't named its pipelines yet, because the warning on main doesn't list them. I have a small diagnostic commit that names the pipelines of the lists an ended submission held, and I can run with it if that's useful. |
|
Thanks, that rules out the luminance pair. We reproduce it on an M5 Pro too: with this PR and #589, 4 of 5 runs log In your split run one program whose translation this PR changes still runs a loop in lockstep: We're running four arms on one exe to separate them: as is; loop cap 64 (diagnostic only, not a fix); no lockstep for groups with y x z > 1 ( Yes please on the diagnostic that names the pipelines of an ended submission: if it names |
|
Correction to my last comment, sorry for the wrong lead. A fourth run of the same exe (no switches, cold cache, with the diagnostic that names an ended submission's pipelines) had no ended submission at all: no "ended a submission", no unwaited submission, no device removal. The picture is broken anyway: black protagonist silhouette and dark interior, and this time the window is blown out to white. So the break doesn't follow the ended submission. They look like two separate symptoms with this PR and the builder running. The luminance pair only moves the exposure: with it skipped the window was normally exposed, with it running the window burns out, and the silhouette is there both ways. Ended submissions so far: 3 in 4 runs. The diagnostic had nothing to name in this one; if it fires in a later run I'll post the pipeline names. |
…um and LDS written per pass The while loops run in lockstep were tested in groups of one row (64, 128, 256 threads). GTA VI has them in groups of rows (8x8, 8x6, 16x16), where the 32-lane host waves take their thread ids from the thread ticket, and in two more shapes: a sum over the group's earlier console waves carried from pass to pass through the first lane (its acceleration-structure builder's prefix sums), and LDS written in each pass by a group of one console wave. Three hardware tests, each against the console's result computed in the test: - a while loop whose lanes leave at passes 0 to 7, differing by group, in groups of 8x8, 8x6 and 16x16, in dispatches of 1, 5 and 37 groups: the accumulator of first lanes, the stores of each pass, each lane's count of passes, and each thread id held by one host lane (its id added atomically to its own dword). Left by no lane, the loop ends at the limit of passes (16,383) in every lane of every group; - the sum over earlier waves, in groups of 128, 256 and 512; - LDS written per pass in a group of 64 and read from the other half after the loop (translated with LDS synced for narrow waves; refused without). On an M5 Pro (D3DMetal) no lane differs in any of them. 2,048 groups of 8x8 take 22 to 25 ms, and 56 to 62 ms with every group at the limit of passes, so the test prints its times and does not check them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The acceleration-structure builder ran in every frame in which all its
pipelines were made. Three of its 53 were refused on 32-lane hosts
(first-lane reads in while loops), so on those it never ran. With them
translated it runs, and its lists address title memory that no host copy
holds: such memory reads 0 and takes no writes, and what a list that
missed wrote is withheld from title memory.
On an M5 Pro (D3DMetal), with all 53 made: the builder's first frame had
12,387 dispatches using title memory and missed 14,469 pages and 94
regions of 256 MiB; later frames missed 200 to 2,700 pages. In four runs
(163 to 495 frames with title-memory lists each) no frame copied a page
back. The cell's picture was dark, and macOS ended submissions in four
of five runs. Held back, the same build missed 2 or 3 pages a frame (at
address 0) and ended none.
The builder now runs only with every pipeline made and
VICTORY_BVH_BUILDER=1; otherwise it is held back as before, and the log
says why ("its lists address title memory with no host copy"). Other
forks gate it by the same switch (101Leafy/victory-leafy d24d3a4888,
trustno1-coder/VIctory-NVIDIA 30b979ee48).
Test: the rule (every pipeline made, and asked) and the switch's one
value.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks @4TheDoctor for the runs and the split; they pointed at the builder. Here is what we found on an M5 Pro. What the long submissions and the dark cell come fromNot from the lockstep translation. With this PR the last three of the structure builder's 53 pipelines translate, One exe (this PR's b3df45b on a31fcca with #589, plus local switches), four arms, two runs each, 5 minutes,
Earlier runs of the same build without switches: an ended submission in 4 of 5.
The lockstep translation itself, on hardwareNew tests on the branch (Wine/D3DMetal, M5 Pro), each against the console's result computed in the test:
A loop no lane leaves ends at 16,383 passes in every lane of every group. 2,048 groups of 8x8 at that limit, two What the branch changes now
Not changed: the builder's memory. That is separate work. The branch now holds the builder back by default, so I'm marking this ready for review again. A rerun on your Macs with the default (no |
…xture upstream pragyaangaur#610 deliberately skips lockstep loops. With pragyaangaur#591 also merged, its fixture loop is taken by pragyaangaur#591's lockstep form (FlatExit::LockstepExit), so the entry-exit rewrite counts 0 there and the test asserts that. A fixture pragyaangaur#591 does not match is needed to cover the rewrite again. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
What was refused
A first-lane read in a loop that the 32-lane halves of a console wave do not run together is refused with
FIRST_ACROSS. There the halves cannot pass the console wave's first lane between them. Halves make a divergent loop run together, in lockstep, only where its exit branch is at its latch: a do-while, predication shape 4. The latch branches ongroup_any_trueof the lanes that stay, so every host wave of the group runs every pass.The console compiler also lays loops out as while loops:
So the exit branch is at a block other than the latch.
23d507cbb6a22e86andb0095eb50222e808(reported on #552 as the average-luminance color downsample programs) pick a table entry in such a loop. In each pass they read the entry's offset from the first lane:Since #522 both programs have been refused and their dispatches dropped.
The change
Predication now also takes a single-exit while loop (shape 5,
match_lockstep_while_loop):FlatExit::LockstepExit). The group stays while any lane stays.The checks are those of shape 4:
The rest is the existing lockstep machinery: the header's activity phi, the iteration cap, and the exit's outer activity.
Effect
Over the 2,734 compute programs our runs passed:
23d507cbb6a22e86,b0095eb50222e808,39321a95223001ed,77360df21e12ea43,9fc111d3be79364f,a1bf269d5b19f025. Refused: 68 → 62.42b9793b4aab6445c1eee35dbc50a9effd8393c7f4e0e3da2f03a2df10503cef(the heaviest single-dispatch pass in our pass timing)Cost of the two downsample programs. Each now runs in halves with 100 group barriers in its code. The pre-#522 per-lane form had none, and nothing proved it exact. Each group pays:
group_any_true;Tests
while_loop_first_laneis a while loop as the console compiler lays one out. The entry jumps to the test, which branches back to the body; the body readsv_readfirstlane_b32 s6, v0into an accumulator and falls into the test.a_first_lane_read_in_a_while_loop_matches_the_console_on_every_gpucovers groups of 64 and 128 in two shapes:s_and_b64 exec, exec, vcc; s_cbranch_execnz): each lane gives the sum of the console wave's first remaining lane over its passes;s_and_b64 vcc, exec, vcc; s_cbranch_vccnz, as these programs test): every lane gives7 * (64 * wave).a_first_lane_read_in_a_while_loop_translates_in_halves: groups of 64, 128 and 256, with barriers placed in the loop.the_while_loop_programs_are_what_their_comments_say.x86_64-pc-windows-gnullvm,-D warnings) andcargo xtask policypass too.In game (M5 Pro, Series X): with this branch, both
23d507cbb6a22e86andb0095eb50222e808translate in a game run (7b2c3c1db2298f44is still refused, as expected). A capture of the cell with this and #589 together and its GPU time will follow here. #589 alone already brings back most of the cell's light (see its thread).🤖 Generated with Claude Code