Skip to content

fix(shader): keep the lanes past a dispatch's thread count outside EXEC where they cannot leave - #667

Merged
pragyaangaur merged 1 commit into
pragyaangaur:mainfrom
hackgods:fix/halves-thread-limits
Nov 28, 2026
Merged

pragyaangaur merged 1 commit into
pragyaangaur:mainfrom
hackgods:fix/halves-thread-limits

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

What was wrong

A dispatch counted in threads (DispatchX with its thread-dimensions flag) may end in a part of a group. The console starts only the threads the dispatch counts. The other lanes of the last wave are there with their EXEC bit clear.

The host runs whole groups. For such a dispatch the pipeline is translated again with Options::thread_limits, and that form branches the lanes past the count to a return at the entry (Lower::leave_past_the_count). It exists only where no lane waits for another: a program with a group barrier, with LDS barriers for narrow waves, or in 32-lane halves that pass values needs every lane at each barrier. Such a program had no form for the dispatch, and the log said:

a dispatch counted in threads whose program cannot leave the lanes past the count: its last group runs whole

A whole last group is wrong for the lanes that do run, not only for stores past the count. EXEC on entry held every lane, so whatever the program computes from EXEC counted lanes the console never starts. Two of the builder's programs are dispatched this way (1,606 and 12 threads in groups of 64). Both begin with s_bcnt1_i32_b64 of EXEC and add the count into every lane's addresses.

The change

Those lanes stay, with EXEC on entry clear, as the console has them.

  • Structure pass. For the translation with thread limits, EXEC on entry is a new atom, "the dispatch runs this lane" (structure::structure_limited, Atom::Dispatched), instead of every lane. Every mask derived from EXEC carries it through the existing mask algebra: guards of loads and stores, ballots and bit counts, sums and prefixes over the wave, first-lane reads, the stay conditions of loops. A lane past the count neither stores nor votes to stay.
  • A mask the program sets to all lanes still holds all of them. The compiler's whole-wave sections (s_or_saveexec_b64 ..., -1, then a select on the saved EXEC) run the lanes not started too, as on the console, and the select gives them the identity.
  • Lowering. The atom's lane bit is thread < count (or count 0: every lane), from the same user-data word the return form reads (USER_DISPATCH_THREADS_X), made once in the entry block (Lower::dispatched). No lane returns, so barriers and exchanges keep every lane.
  • Which form. The return at the entry stays where it is possible. The new form is tried where it is not (lower_limited): one dimension, the per-lane form only. Literal halves take EXEC as the console wave's whole and get no word. Where the new form is refused, the program is as before.

Translations without thread limits do not change: the limited structure is built only for that form.

In game

Nothing to show yet: in our runs only the structure builder dispatches counted in threads, and it is held back by default since #591 (VICTORY_BVH_BUILDER=1 runs it). No play-time program changes.

Effect

before after
Builder programs (52) with a form for a dispatch that ends in a part of a group 37 51
The two dispatched that way in our runs last group whole lanes past the count outside EXEC

The one left has one thread per group. The 14 that gain a form have console barriers or run as wave64 in halves (the three prefix sums among them).

Tests

On hardware (M5 Pro, D3DMetal), each lane against the console's value computed in the test:

Test Cases Differ
lanes_past_a_count_stay_out_of_exec_in_halves_on_every_gpu: wave64, groups of 64, LDS, a group barrier, a lockstep loop with an atomic counter, s_bcnt1 of EXEC, EXEC as two dwords, a first-lane read, the compiler's whole-wave sum 16 counts (0 = all, 1, 2, 7, 12, 31, 32, 33, 40, 63, 64, 65, 74, 96, 127, 128) over two groups; eight dwords per lane, nothing stored past the count, the loop's passes counted 0
the_lanes_past_a_count_of_threads_do_not_run, extended: plain, with s_barrier, with v_mbcnt 3 programs x wave32 and wave64 x 12 counts 0

Without hardware:

  • a_program_with_barriers_keeps_the_lanes_past_a_count_out_of_exec: the program above gets the word only when asked.
  • only_programs_whose_lanes_never_wait_leave_past_a_count_of_threads: a barrier and halves now give a word; two dimensions, one thread per group, not asked for, and literal halves give none.
  • exec_on_entry_holds_the_dispatched_lanes_in_the_limited_form (native): an instruction under EXEC on entry is guarded by the new atom, one under EXEC set to all lanes by nothing, one under the saved EXEC by the atom again.

Whole victory-shader suite under Wine: 537 passed, 0 failed, 5 ignored. Native: 278 passed. Clippy (x86_64-pc-windows-gnullvm, --all-targets, -D warnings) for victory-shader and victory-gpu, and cargo xtask policy, pass.

Not in this branch

  • Dispatches counted in threads in two or three dimensions: no program we have seen is dispatched so.
  • Literal halves: no form for a partial group.

🤖 Generated with Claude Code

…EC where they cannot leave

A dispatch counted in threads (DispatchX with its thread-dimensions flag)
may end in a part of a group. The console starts only the threads the
dispatch counts; the other lanes of the last wave are there with their
EXEC bit clear. Here the host runs whole groups, and the translation asked
for such a dispatch branches the lanes past the count to a return at the
entry (Lower::leave_past_the_count). That is possible only where no lane
waits for another: a program with a group barrier, with LDS barriers for
narrow waves, or in 32-lane halves that pass values needs every lane at
each barrier. Such a program had no form for the dispatch, and its last
group ran whole ("cannot leave the lanes past the count").

That is wrong for the lanes that do run, not only for stores past the
count: EXEC on entry held every lane, so a program that counts its lanes
(s_bcnt1_i32_b64 of EXEC), ballots, sums over the wave or reads the first
lane computed with the lanes the console never starts. GTA VI's
acceleration-structure builder dispatches 28918b7150a7aad5 and
caa78b8ca62bc3d6 that way (1,606 and 12 threads in groups of 64); both
add s_bcnt1 of EXEC into every lane's addresses.

Those lanes now stay, with EXEC on entry clear, as the console has them.
For the translation with thread limits the structure pass takes EXEC on
entry as a new atom, "the dispatch runs this lane"
(structure::structure_limited, Atom::Dispatched), instead of every lane;
its lane bit is thread < count (or count 0) from the same user-data word.
Every mask derived from EXEC carries it through the mask algebra that is
already there: guards of loads and stores, ballots and bit counts, sums
and prefixes over the wave, first-lane reads, and the stay conditions of
loops, so such a lane neither stores nor votes to stay. A mask the
program itself sets to all lanes still holds all of them: the compiler's
whole-wave sections run the lanes not started too and select on the
saved EXEC, as on the console.

The return at the entry stays for the programs that can use it. The new
form is tried where it cannot be (lower_limited), in one dimension, for
the per-lane form only (literal halves take EXEC as the console wave's
whole: no word there); where it is refused the program is as before.
Translations without thread limits do not change.

Of the builder's 52 programs, 37 had a form for such a dispatch; 51 have
one now (the other has one thread per group).

Tests, on hardware (M5 Pro, D3DMetal) against the console's values:
- a wave64 program in groups of 64 with LDS, a group barrier, a loop run
  in lockstep with an atomic counter, s_bcnt1 of EXEC, EXEC as two
  dwords, a first-lane read and the compiler's whole-wave sum, in
  dispatches of 0 (all), 1, 2, 7, 12, 31, 32, 33, 40, 63, 64, 65, 74, 96,
  127 and 128 threads over two groups: every lane that runs stores the
  console's eight dwords, no other lane stores, and the loop's passes are
  counted for the lanes that run only;
- the existing count test (each lane adds 1 to a counter) also with a
  group barrier and with console lanes (v_mbcnt), in wave32 and wave64.
And natively: the limited structure guards an instruction under EXEC on
entry by the new atom and one under EXEC set to all lanes by nothing.

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