Skip to content

feat(shader): halves meet for lane-mask ballots in divergent code, and keep early-leaving lanes to the end - #511

Merged
pragyaangaur merged 4 commits into
pragyaangaur:mainfrom
ottco-dev:pr/halves-meet-ballots
Nov 27, 2026
Merged

pragyaangaur merged 4 commits into
pragyaangaur:mainfrom
ottco-dev:pr/halves-meet-ballots

Conversation

@ottco-dev

Copy link
Copy Markdown
Contributor

What

Two changes to how wave64 programs run in two 32-lane halves on a 32-lane host (D3DMetal under Wine), so fewer compute programs are refused:

  1. Lanes that leave early stay on to the end (ir::keep_lanes_to_end). A console wave that the host splits into halves needs both halves at every group barrier. A lane that jumps to the end early never arrives there. When a program is refused for that reason, it is rebuilt with those lanes kept on until the end with EXEC off, and translated again. A program without such an early exit stays refused.
  2. The halves meet for lane-mask ballots in divergent code (lower/meet.rs, Options::halves_rendezvous, on by default where programs run in halves; VICTORY_HALVES_RENDEZVOUS=0 turns it off). Until now a lane mask read as data inside a branch was refused, because the halves can only exchange values at a group barrier, and inside divergent code there is none. In each such region (acyclic, entered only from its branch, left to one block or the end) the two halves now publish their ballot word in group-shared memory and wait for each other with a bounded spin on an epoch counter. A half that never enters the region is never waited for.

A third commit moves round16/round16_after and packed_half out of lower.rs unchanged (to lower/round_mode.rs and lower/packed.rs). Without that, the rendezvous puts lower.rs 2.3 KB over the asset policy's 512 KiB limit, as #375 did for the atomic-lock lowering. It comes first, so every commit passes the policy.

Why it is safe to wait in divergent code

Before writing the lowering we measured the assumption it rests on with a standalone probe (compiled with D3DMetal's DXC, run under CrossOver 26.3 + D3DMetal 4.0 beta 3 on an M5 Pro): one lane per half publishes a value and a flag in group-shared memory, then spins with InterlockedOr on the other half's flag, bounded at 2^22. With 64, 4,096 and 65,536 groups, three runs each, there were 0 timeouts and 0 wrong values. A waiting half spun up to about 102,000 times while the other was busy. Host waves are 32 lanes and follow SV_GroupIndex / 32.

Where it matters

In a run on 26 November, the game's remaining wave64 refusals on a Mac all sat in divergent code. The largest group was 49 programs refused with "a lane mask read as data (wave64 on a 32-lane host)". This PR handles that group. The smaller groups (reductions, first-lane and lane reads in divergent code) are not included yet, because main has since reworked those paths (reduce_half, first_lane_across, predication from #440). Since #435's mask_number, main itself translates some of these programs: a ballot inside a loop, which this PR keeps refused, is now translated by main. The test accepts either outcome for that case.

I have not yet measured the refusal count on current main with the game. Numbers from translate --cache before and after would be the next step, and #503 would make translate --wave32 the right tool for that.

Tests

  • New tests in halves_tests: lane_masks_in_divergent_code_meet_between_halves (translation and DXIL validation at 32, 64, 96 and 128 threads, if and if/else, a branch inside a loop; refused without the rendezvous), plus hardware tests that run the divergent ballots on every adapter and compare with the console's masks (lanes of both halves in the branch, only the lower half's, both sides of an if/else, two console waves diverging differently at 128 threads, a group with no upper half at 96, the branch inside a loop).
  • victory-shader under Wine with D3DMetal (Mac mini, 24 GB, CrossOver 26.3 Wine from tools/macos, D3DMetal 4.0 beta 3): 452 passed, 0 failed.
  • victory-gpu under Wine with D3DMetal (--skip profile::sampler): 525 passed, 2 failed (descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync, descriptor::tests::sync_heaps_reads_partial_64k_pages_and_small_heaps). Both fail the same way on main without this change.
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings, with and without --all-features: clean.
  • cargo xtask policy: passes at each of the three commits.
  • Only the changed files are formatted.

The shader cache key includes the switch (no halves rendezvous when off), so cached translations from before stay apart.

🤖 Generated with Claude Code

ottco and others added 4 commits November 27, 2026 07:09
…of lower.rs

The halves rendezvous puts lower.rs 2.3 KB over the asset policy's
512 KiB limit. round16/round16_after and packed_half are free functions
with no state of their own, so they move to lower/round_mode.rs and
lower/packed.rs unchanged, as pragyaangaur#375 did with the atomic-lock lowering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A cross-lane exchange or synced LDS between the two host waves of a
console wave needs both at each group barrier. The common early exit,
s_cbranch_execz straight to s_endpgm (if (id < n) { whole shader }),
makes the lanes that leave skip them, so such programs were refused.
Refused for a halves reason, a compute program is lowered again with
those branches dropped (ir::keep_lanes_to_end): the lanes run on with
EXEC off. Programs with scalar memory writes are left as they were.

Ported from LakshanSivakumar/Native-Victory-macOS 0a8bee7 (GPL-3.0) onto
emit_pruned, keyed on this tree's halves refusal texts. On an M5 Pro the
pack warm-up's compute pipelines that do not translate fell from 247 to
219; victory-shader's suite passes under Wine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A console wave64 program in 32-lane halves passed ballots between the
halves only between group barriers, in blocks every lane of the group
runs together; a lane mask read as data in divergent code was refused
("a lane mask read as data (wave64 on a 32-lane host)").

The two host waves of one console wave now wait for each other there on
group-shared memory (lower::meet): each half publishes its ballot and an
epoch-tagged arrival, and its lanes count every site they will not reach
on the edges they take, so a half waits until the other arrived or all
of its lanes are done with the site. Only in acyclic regions entered from
an in-step branch block and left to one join (a group barrier there when
the branch block loops); the deadlock-freedom argument is in the module
notes. Sites are found in a first pass and laid out in a second
(lower_pruned). Options::halves_rendezvous switches it (victory-gpu: on,
VICTORY_HALVES_RENDEZVOUS=0 off, keyed in the shader cache).

Tests: divergent ballots on the GPU (if, only the lower half entering,
if/else, 128 threads diverging per console wave, 96 threads, the branch
in a loop), validation, and a loop inside the branch staying refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	crates/victory-gpu/src/shader_cache.rs
@pragyaangaur
pragyaangaur merged commit d2b4889 into pragyaangaur:main Nov 27, 2026
1 check passed
hackgods added a commit to hackgods/VIctory-macOS that referenced this pull request Nov 27, 2026
DispatchX(x, y, z, flags) with flag 0x20 (the console's dispatch
initiator bit "use thread dimensions") gives thread counts. The host
ignored the flags and ran x * y * z groups, so a ray-tracing structure
builder stage dispatched this way ran up to 64 times its threads and
stored over the structures after its own.

The host now runs the groups that hold the threads (div_ceil by the
program's numthreads, kept in pipeline::Translated). Where the last
group along x is a part of one, the dispatch takes a second form of the
pipeline (pipeline::thread_limited), made on the first such dispatch:
its shader translated again with emit::Options::thread_limits, under a
cache key of its own (bit 9 of translation_build, after pragyaangaur#511's halves
apart and pragyaangaur#506's cap barriers). In that form a compute program of one dimension whose lanes
never wait for each other (no s_barrier, no barriers for narrow host
waves, not in 32-lane halves that pass values, not in literal halves,
whose group must take one path) reads a user-data word,
emit::USER_DISPATCH_THREADS_X, the threads along x that run, and the
lanes past it leave before anything else, so wave counts, first-lane
reads and stores see only the lanes the console starts. The host sets
the word after binding. Every other translation is as before, so
programs no such dispatch reaches keep their DXIL; containers of
one-dimensional compute programs are kept for the second translation
(at most the ~22 MB all compute containers came to in a world run).
The lowering's part is in lower/thread_limits.rs and lower/retry.rs,
so lower.rs stays under the file limit. Each pipeline's first dispatch
counted in threads is logged ("a dispatch counted in threads"), with a
warning where a part of a group runs whole. VICTORY_THREAD_DIMENSIONS=0
takes the counts for groups again.

Ported from DexNom/VIctory bc3cd4b0 (DexNom: the flag, and the lanes
past the count leaving in two builder programs chosen by digest), as
carried in the cool_fox fork's 810efe5 (RyanLockwoood/VIctory-cool_fox-
LockWood). This tree has no builder detection, hence the form made on
demand.

Tests: halves_tests::the_lanes_past_a_count_of_threads_do_not_run
(wave32 and wave64 on the host, counts 0 to 128 over two groups of 64);
halves_tests::only_programs_whose_lanes_never_wait_leave_past_a_count_
of_threads (pragyaangaur#435's whole-wave retry included: a program it takes reads
no count); thread_dispatch_tests::a_dispatch_counted_in_threads_runs_
only_those_threads, from a console container through State::dispatch_x:
threads 70 to 127 write nothing, the pipeline's own translation reads
no count (fails with VICTORY_THREAD_DIMENSIONS=0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hackgods added a commit to hackgods/VIctory-macOS that referenced this pull request Nov 27, 2026
DispatchX(x, y, z, flags) with flag 0x20 (the console's dispatch
initiator bit "use thread dimensions") gives thread counts. The host
ignored the flags and ran x * y * z groups, so a ray-tracing structure
builder stage dispatched this way ran up to 64 times its threads and
stored over the structures after its own.

The host now runs the groups that hold the threads (div_ceil by the
program's numthreads, kept in pipeline::Translated). Where the last
group along x is a part of one, the dispatch takes a second form of the
pipeline (pipeline::thread_limited), made on the first such dispatch:
its shader translated again with emit::Options::thread_limits, under a
cache key of its own (bit 9 of translation_build, after pragyaangaur#511's halves
apart and pragyaangaur#506's cap barriers). In that form a compute program of one dimension whose lanes
never wait for each other (no s_barrier, no barriers for narrow host
waves, not in 32-lane halves that pass values, not in literal halves,
whose group must take one path) reads a user-data word,
emit::USER_DISPATCH_THREADS_X, the threads along x that run, and the
lanes past it leave before anything else, so wave counts, first-lane
reads and stores see only the lanes the console starts. The host sets
the word after binding. Every other translation is as before, so
programs no such dispatch reaches keep their DXIL; containers of
one-dimensional compute programs are kept for the second translation
(at most the ~22 MB all compute containers came to in a world run).
The lowering's part is in lower/thread_limits.rs and lower/retry.rs,
so lower.rs stays under the file limit. Each pipeline's first dispatch
counted in threads is logged ("a dispatch counted in threads"), with a
warning where a part of a group runs whole. VICTORY_THREAD_DIMENSIONS=0
takes the counts for groups again.

Ported from DexNom/VIctory bc3cd4b0 (DexNom: the flag, and the lanes
past the count leaving in two builder programs chosen by digest), as
carried in the cool_fox fork's 810efe5 (RyanLockwoood/VIctory-cool_fox-
LockWood). This tree has no builder detection, hence the form made on
demand.

Tests: halves_tests::the_lanes_past_a_count_of_threads_do_not_run
(wave32 and wave64 on the host, counts 0 to 128 over two groups of 64);
halves_tests::only_programs_whose_lanes_never_wait_leave_past_a_count_
of_threads (pragyaangaur#435's whole-wave retry included: a program it takes reads
no count); thread_dispatch_tests::a_dispatch_counted_in_threads_runs_
only_those_threads, from a console container through State::dispatch_x:
threads 70 to 127 write nothing, the pipeline's own translation reads
no count (fails with VICTORY_THREAD_DIMENSIONS=0).

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.

3 participants