Skip to content

fix(shader): let lanes outside a loop's entry predicate leave it, in halves - #709

Open
Scarlet-Noble wants to merge 1 commit into
pragyaangaur:mainfrom
Scarlet-Noble:fix/loop-entry-predicate
Open

Scarlet-Noble wants to merge 1 commit into
pragyaangaur:mainfrom
Scarlet-Noble:fix/loop-entry-predicate

Conversation

@Scarlet-Noble

Copy link
Copy Markdown

What was wrong

In halves, a skip-guard body (if (cond) { ... } → v_cmpx + s_cbranch_execz) that has to be predicated (every lane runs it, under the predicate) can contain a counted per-lane loop:

      v_and_b32 v1, 15, v0          ; trip count per lane
      v_cmpx_ne_u32 exec, 0, v1     ; lanes with no trips skip the region
      s_cbranch_execz past
      ...                           ; something halves must exchange (a first-lane read)
loop: ...
      s_add_i32 s14, s14, 1
      v_cmpx_eq_u32 exec, s14, v1   ; a lane is done when count == trip
      s_andn2_b64 s[10:11], s[10:11], exec
      s_cbranch_scc1 loop
past: s_mov_b64 exec, s[0:1]
      buffer_store_dword ...

On the console, lanes outside the predicate (here, trip 0) never enter the loop. After predication they run it anyway, and they run its exit test too, which a trip-0 lane never meets. They spin to the iteration limit and leave the program, so their stores never happen: those lanes write nothing.

The fix

Each counted loop records the predicate it's entered under (enter_loop, single entry only). At the loop's exit branch, a lane outside that entry predicate takes the successor that leaves the loop. A lane that leaves the predicate inside the loop (a break) still stays in it, as #655 needs for its lockstep exchanges. first_lane_reads_past_a_break_and_under_nested_guards_match_the_console_on_every_gpu passes.

How it was found and tested

Found by differential testing: random compute shaders, each with an exact CPU model of its expected output, run through the translator on an M3 Pro. Five of 400 random shaders came out wrong on main (a08d94b); all five are right with this change.

  • cargo test --workspace: 279 passed (victory-shader).
  • GPU tests under Wine/D3DMetal: 544 passed, 0 failed.
  • In our own fork with this fix, 700 random shaders give 0 wrong outputs.

A hand-written regression test in halves_tests.rs is still to come: the minimal shape above didn't reproduce on its own (main already handles it), so the test needs one of the fuzz cases reduced to GFX10 words. I'll add it to this PR.

🤖 Generated with Claude Code

…halves

A skip-guard body that halves predication runs in every lane can hold a
counted per-lane loop. Lanes outside the predicate never entered that loop
on the console, but they ran its exit test, which they need never meet:
they spun to the iteration limit and left the program, skipping their
stores.

Each counted loop now records the predicate it is entered under (one entry
only), and at its exit branch a lane outside that entry predicate takes
the successor that leaves. A lane that left the predicate inside the loop
(a break) stays in it, as pragyaangaur#655's lockstep exchanges need.

Co-Authored-By: Claude Opus 5.5 <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.

1 participant