Repository navigation
fix(shader): keep lanes that break out of a counted loop in it, in halves - #655
Merged
pragyaangaur merged 2 commits intoNov 28, 2026
Merged
Conversation
…lves A first-lane read the 32-lane halves of a console wave cannot pass between them is refused: one in a block the lanes of the group do not run together. 9991e29feaa26e3e has been refused so since pragyaangaur#522, for a read behind two shapes predication did not take. A counted loop lanes break out of one by one (its code at 0x1f08): loop: ... v_cmpx_lt_f32 exec, 0, v28 ; the lanes that break s_andn2_b64 s[26:27], s[26:27], exec ; the lanes left s_cbranch_scc0 past ; none left: leave s_and_b64 exec, s[10:11], s[26:27] s_add_i32 s30, s30, 1 s_cmp_gt_i32 s30, 2 s_cbranch_scc0 loop The loop's own end is one every lane takes together, but the break put it, and all the code after it, out of step. The break is now predicated away like an early-out (shape 6, match_break_loop): the lanes that break stay in the loop, inactive, to its own end. Each vector write being guarded by the lanes still active, a lane that broke holds past the loop what it held when it broke. Nothing else the loop makes may be seen past it: no lane mask is joined at the exit, no scalar or lane mask written in the loop is read after it, and its body holds no scalar store, atomic or message (the console wave leaves once its every lane broke, and here it runs on). The block taken after the break must dominate the latch and the loop's own end, so the lanes active there are among those that stayed. Only where the loop and its exit are in step afterwards. A guard in a guard, both skipping to one join (0x2158): s_and_saveexec_b64 s[28:29], s[12:13] s_cbranch_execz join ... s_and_saveexec_b64 s[12:13], s[12:13] s_cbranch_execz join v_readfirstlane_b32 s39, v13 The outer one became a skip guard, whose join the inner one could then not share, so what the inner one guards stayed out of step. Where that holds a block that needs to be in step and the whole region can be laid out flat (shape 3), that is done instead. No new lowering. 9991e29feaa26e3e translates again, with 9 group barriers. Over the 2,734 compute programs our runs passed, none is newly refused; 134 others translate differently, 125 of them by the flat layout alone, with 556 fewer group barriers in all. Tests: a loop of three passes lanes break out of (by lane, all of them early, and by half) with a first-lane read in it, and a first-lane read under two guards in a counted loop, give the console's lanes on hardware in groups of 64 and 128; both translate in halves where they were refused; the break stays where the loop's count is read after it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was refused
9991e29feaa26e3e(reported on #552 as CS_ShadowTileClassify) has been refused since #522, and #591 (merged) did not change that:The read is
v_readfirstlane_b32 s39, v13at 0x21e4. Halves can pass it between them only in a block every lane of the group runs together. Two shapes before it kept its block out of step.1. A counted loop lanes break out of one by one (0x1f08).
The loop's own end is one every lane takes together. The break is not, and it put the loop and everything after it out of step.
2. A guard in a guard, both skipping to one join (0x2158).
The outer guard became a skip guard (shape 2). The inner one could then not be one: its join was already the outer one's. So what it guards stayed out of step.
The change
Shape 6,
match_break_loop. The break is predicated away like an early-out:Each vector write is guarded by the lanes still active. So a lane that broke holds past the loop what it held when it broke, which is what the edge it would have taken carries. Nothing else the loop makes may be seen past it. The shape is taken only where:
in_step_with).Nested guards. Where a skip guard's body holds a guard that skips to the same join, around a block that needs to be in step, and the whole region can be laid out flat (shape 3,
match_sese_diamond), the flat layout is used instead. There is no new lowering: both shapes exist.Effect
Over the 2,734 compute programs our runs passed (survey on #591's head before it was squashed;
9991e29feaa26e3ere-measured on current main 06a9ea2):9991e29feaa26e3eEach rule alone (measured with a local switch, not in the branch):
9991e29feaa26e3estays refused;9991e29feaa26e3estays refused.Barriers over the 134: 33 programs have fewer, 100 the same, 1 more (
71883e40620a567a, 24 → 26). The largest:eb9a057a81d18c4241aa4d3d51094531049459e0c78e0d507ddd1b511732d145d2b169b82f67ec5b03546d9c50d82518Cost of
9991e29feaa26e3e(not measured in game separately). 9 group barriers in its code. Its group is 8x6 (48 threads: one full host wave and one of 16 lanes); #591's tests run a lockstep loop in that group shape against the console. The loop at 0x1f08 now runs its three passes for every lane; the console leaves it once no lane is left.Tests
break_loop: a loop of three passes counted ins30that lanes break out of, withv_readfirstlane_b32 s6, v0in it. Three ways to break: by lane (the lanes with 3 never do), every lane by the second pass (the console wave leaves early), and by half (from the second pass on the console wave's first lane left is in its upper half).nested_guards_first_lane: a counted loop whose body is a guard in a guard, both skipping to the loop's end, with the read in the inner one; the outer guard takes the odd lanes or each wave's upper half.first_lane_reads_past_a_break_and_under_nested_guards_match_the_console_on_every_gpu: both, in groups of 64 and 128, each lane against the console's value.a_first_lane_read_in_a_loop_lanes_break_out_of_translates_in_halves,a_first_lane_read_under_nested_guards_translates_in_halves: groups of 64, 128 and 256. With the two rules switched off (a local build) all three of these fail with the refusal above.a_loop_lanes_break_out_of_whose_count_is_read_after_it_is_refused: withs30stored after the loop, the read stays refused.the_break_loop_programs_are_what_their_comments_say.halvestests (release): 90 passed, 0 failed; nativecargo test -p victory-shader: 276 passed, 0 failed, 2 ignored. Clippy (x86_64-pc-windows-gnullvm,--all-targets,-D warnings) passes for victory-shader, andcargo xtask policypasses.Not in this branch
eeed82b877b44b5f: it declares all 32 KiB of group-shared memory as LDS; shader: pass values between halves through borrowed LDS when the program fills it #648 (borrowed LDS, merged) may now cover it, not checked here.7b2c3c1db2298f44: seedesign-7b2c.md.🤖 Generated with Claude Code