Skip to content

fix(shader): keep lanes that break out of a counted loop in it, in halves - #655

Merged
pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/halves-counted-loop-break
Nov 28, 2026
Merged

pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/halves-counted-loop-break

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

What was refused

9991e29feaa26e3e (reported on #552 as CS_ShadowTileClassify) has been refused since #522, and #591 (merged) did not change that:

GFX10 code at 0x2880: a first-lane read of a value that may differ between 32-lane halves (wave64 on a 32-lane host) is not supported

The read is v_readfirstlane_b32 s39, v13 at 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).

loop: ...
      v_cmpx_lt_f32 exec, 0, v28            ; the lanes that break
      v_mov_b32 v28, 1
      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
      v_mov_b32 v28, 0
past: s_mov_b64 exec, s[22:23]

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).

      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
join: s_mov_b64 exec, s[28:29]

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:

  • its branch becomes a jump into the loop;
  • the lanes that break stay in the loop, inactive, to its own end (the header's activity phi, as for a lockstep loop);
  • past the loop, every lane that entered is active again.

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:

  • the loop has one latch, one entry from blocks in step, no cycle of its own, and exactly two edges out, both to one block (the loop's own end may go through one block only it enters);
  • the block taken after the break dominates the latch and the loop's own end, so the lanes active there are among those that stayed;
  • no lane mask is joined at the exit: a mask is a function of what each edge carries, and the lanes that broke would read the other edge's;
  • no scalar or lane mask written in the loop is read after it: the console wave leaves once its every lane broke, and here it runs to the loop's own end;
  • the body holds no scalar store, atomic or message: scalar code runs whatever lanes are active;
  • the loop and its exit are in step afterwards (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; 9991e29feaa26e3e re-measured on current main 06a9ea2):

before (#591's head) this branch
refused 62 61
9991e29feaa26e3e refused 80,392 B of DXIL, 9 group barriers
newly refused none
translated differently 134

Each rule alone (measured with a local switch, not in the branch):

  • shape 6 alone: 9 programs change, none in barriers; 9991e29feaa26e3e stays refused;
  • the nested-guard rule alone: 125 programs change, 556 fewer group barriers; 9991e29feaa26e3e stays refused.

Barriers over the 134: 33 programs have fewer, 100 the same, 1 more (71883e40620a567a, 24 → 26). The largest:

program group barriers, before → after
eb9a057a81d18c42 300 → 120
41aa4d3d51094531 139 → 13
049459e0c78e0d50 71 → 19
7ddd1b511732d145 115 → 63
d2b169b82f67ec5b 63 → 19
03546d9c50d82518 35 → 21

Cost 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 in s30 that lanes break out of, with v_readfirstlane_b32 s6, v0 in 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: with s30 stored after the loop, the read stays refused.
  • the_break_loop_programs_are_what_their_comments_say.
  • Under Wine on an M5 Pro (D3DMetal), on the new base, the halves tests (release): 90 passed, 0 failed; native cargo test -p victory-shader: 276 passed, 0 failed, 2 ignored. Clippy (x86_64-pc-windows-gnullvm, --all-targets, -D warnings) passes for victory-shader, and cargo xtask policy passes.

Not in this branch

🤖 Generated with Claude Code

hackgods and others added 2 commits November 28, 2026 04:28
…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>
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