Skip to content

fix(gpu): count DispatchX's threads as threads, not groups - #527

Merged
pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/dispatch-thread-counts
Nov 27, 2026
Merged

pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/dispatch-thread-counts

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

What. DispatchX(x, y, z, flags) with flag 0x20 (the console's dispatch-initiator bit "use thread dimensions") gives thread counts, not group counts. The host now runs the groups that hold those threads (div_ceil by the program's numthreads, now kept in pipeline::Translated).

When the last group along x is only partly used, the dispatch switches to a second form of the pipeline (pipeline::thread_limited), made the first time such a dispatch arrives. That form translates the shader again with emit::Options::thread_limits, under its own cache key (bit 9 of translation_build, after #511's and #506's bits). In it, a one-dimensional compute program whose lanes never wait for each other reads a user-data word, emit::USER_DISPATCH_THREADS_X, and the lanes past that count leave before doing anything else. "Never wait" means: no s_barrier, no barriers for narrow host waves, not in 32-lane halves that pass values, and not in #435's whole-wave halves, where the group must take one path. The host sets the word after binding.

All other translations stay as they are, so programs that no such dispatch reaches keep their DXIL. Containers of one-dimensional compute programs are kept for the second translation (at most ~22 MB, all compute containers of a world run). The first thread-counted dispatch of each pipeline is logged ("a dispatch counted in threads"), with a warning if a partial group has to run whole. VICTORY_THREAD_DIMENSIONS=0 treats the counts as groups again, as before.

Depends on the lower.rs refactor PR (#R). Main's lower.rs is 523,123 bytes, 1,165 under cargo xtask policy's 512 KiB file limit, and this change adds about 4 KB to it. The first commit here is that PR's (the lowering's entry moved unchanged into lower/retry.rs). The second makes list::tests::allocate_upload_survives_early_reset_and_release_while_held_on_gpu wait for the release thread (host::wait_for_releases) before it checks that the uploads are freed: they are dropped there, behind what other tests retired, and with this PR's dispatch test running beside it the check failed in 5 of 7 Wine runs (2 of 15 without it; 3 of 3 pass with the wait). The third is the fix. The fix's lowering part lives in lower/thread_limits.rs and lower/retry.rs, so lower.rs ends at 519,998 bytes, and at 521,127 together with the early-depth PR.

Why. The host ignored the flags and ran x * y * z groups. A ray-tracing structure-builder stage dispatched this way ran up to 64 times as many threads as it should, and stored over the structures after its own.

Tests

  • halves_tests::the_lanes_past_a_count_of_threads_do_not_run: wave32 and wave64 programs on the hardware device and WARP, counts 0 to 128 over two groups of 64. Only the lanes below the count add to their counter; 0 runs all.
  • halves_tests::only_programs_whose_lanes_never_wait_leave_past_a_count_of_threads: which programs get the word. feat(shader): retry refused 64-thread wave64 compute with whole-wave control flow #435's whole-wave retry is 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 on D3DMetal. Threads 70 to 127 write nothing, and the pipeline's own translation reads no count. The test fails with VICTORY_THREAD_DIMENSIONS=0.
  • On this branch (base d2b4889): cargo test --workspace 722 passed; tools/macos/test-wine.sh --workspace --no-fail-fast 1445 passed, 1 failed before the second commit (the upload test it fixes); with it, three victory-gpu runs failed only descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync and sync_heaps_reads_partial_64k_pages_and_small_heaps, intermittent on plain main too in our runs, which shared the machine with a game run; gnullvm clippy --workspace --all-targets -D warnings, with and without --all-features: clean; cargo xtask policy: ok.
  • In game: it has been in our integration builds (M5 Pro) with the world lit and no new refusals. We have no in-game A/B that isolates its effect on the picture or the frame time yet; VICTORY_THREAD_DIMENSIONS=0 gives the old behaviour for one.

Credits. Ported from DexNom/VIctory bc3cd4b0 (DexNom found the flag, and made the lanes past the count leave 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, so the limited form is made on demand, per pipeline.

Update: rebased onto main f7dddb0 (after #518–#522 and #447); tests re-run there, clippy and policy clean. This change has also been in our play builds on the M5 Pro (integration v8/v9, lit and correct prison scene) for the last day.

🤖 Generated with Claude Code

hackgods and others added 2 commits November 27, 2026 12:43
allocate_upload_survives_early_reset_and_release_while_held_on_gpu
checks, once pump_held returns, that the held lists' uploads are
unregistered. They are dropped with their batch on the release thread
(retire_any, release_later), behind whatever other tests retired, so a
test that releases host objects meanwhile makes the check fail now and
then. Under Wine it failed in 2 of 15 runs of the victory-gpu tests,
and in 5 of 7 with the thread-count dispatch test of the next commit
running beside it. host::wait_for_releases (tests) waits until the
release thread has dropped what was sent to it before; with it, that
test passed in 3 of 3 runs with the dispatch test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
@pragyaangaur
pragyaangaur merged commit f20d00d into pragyaangaur:main Nov 27, 2026
1 check passed
@gpalfercmd

Copy link
Copy Markdown

Follow-up on this merged PR; the code below is now in main.

Possible stall: thread_limited holds a global lock while building

In pipeline.rs, thread_limited locks MADE and keeps the guard while make_thread_limited translates the shader and creates the host pipeline. Cache hits take the same lock, so while one pipeline is being built for the first time, any other thread reaching a partial thread-counted dispatch has to wait, even if its own pipeline is already made.

One option is to hold the map lock only long enough to get a per-shader cell, then build outside it:

type Cell = Arc<OnceLock<Option<&'static host::HostPipeline>>>;
static MADE: Mutex<Option<HashMap<[u8; 32], Cell>>> = Mutex::new(None);

let cell = MADE
    .lock()
    .unwrap_or_else(|e| e.into_inner())
    .get_or_insert_with(HashMap::new)
    .entry(shader.full_hash)
    .or_default()
    .clone();
*cell.get_or_init(|| make_thread_limited(shader))

This still builds each shader only once, and only callers of that same shader wait for it. The timing log would need to move inside the closure.

I found this by reading the code and have not measured a stall. The native tests passed on this PR's head on macOS, but the Windows-only tests, including the new dispatch ones, did not run. The snippet is untested.

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