Skip to content

GPU: graphics pipelines are made before compute ones, and compute never takes every worker - #3025

Open
Luislariosss wants to merge 1 commit into
Marvisak:mainfrom
Luislariosss:perf/pipeline-order
Open

Luislariosss wants to merge 1 commit into
Marvisak:mainfrom
Luislariosss:perf/pipeline-order

Conversation

@Luislariosss

Copy link
Copy Markdown
Contributor

Problem

In Grand Theft Auto VI's world about 180 compute pipelines take the host driver 3.7 s each on average (up to 24 s; the other compute pipelines take 0.1 s). Queued strictly by first use, they held every pipeline worker for minutes while the scene's graphics pipelines — which compile in milliseconds — waited behind them, and their draws were dropped until a pipeline was ready.

Change

crates/victory-gpu/src/pipeline/make.rs: the worker queues are split into four, taken in this order — graphics a draw waits for, compute a dispatch waits for, then those started early — and compute never holds more than all workers but one (if only one worker could be spawned, compute takes it, else nothing compute would ever be made). The count of compute compilations lives under the same lock as the queues, and the slot is given back through a guard, so a panic in the compiler cannot keep it and a worker about to sleep cannot miss the wake-up.

Measurement (Ryzen 5 9600X, RX 9060 XT)

One 360 s run before and one after, same build profile and cache state:

  • before: the compile queue sat at ~870, ~500–1000 draws a frame were dropped for want of a pipeline, and characters, faces and light were missing from the world
  • after: the queue went to 7 within three minutes, dropped draws fell to ~370, and the characters, faces and lights appeared

232 tests, clippy clean.

Risk

When no graphics pipeline is queued, peak compute compilation is one worker lower, so a compute-only queue drains a little more slowly.

@pickles976

pickles976 commented Nov 26, 2026 •

Copy link
Copy Markdown

Updated Wine verification: 018b56ac28fa6024eb142921acc03d4e43ccb177

The two previously reported scheduler gates are now covered on this Wine host. Clean detached production head; 6 passed, 0 failed/ignored, 0.00 s after fresh 8.80 s compilation from VIctory-pr3025-evening:

cargo test --locked --target x86_64-pc-windows-msvc -p victory-gpu --features diagnostics --lib pipeline::make::tests:: -- --nocapture --test-threads=1
  • a_pipeline_in_two_queues_takes_one_slot passes: duplicate urgent/early entries reserve only one compute slot. It uses eight available workers, so the cap cannot conceal a second reservation. Source review confirms the claim now happens via taken.swap(true) inside next, under the queue lock, before compute_busy increments; the worker immediately owns ComputeSlot.
  • a_compute_stream_is_queued_as_compute passes for a single compute stage with Some(graphics).
  • Graphics priority/single-worker progress, promotion deduplication, first-use worker completion, and diagnostics skip behavior also pass.

Environment: Linux, Wine 11.0, Rust 1.98.1 Windows MSVC target, 16 GiB host, single test thread, 9 GiB memory / 512 MiB swap cgroup and 180 s timeout. These fixtures exercise CPU scheduling and a no-shader worker path, not real driver compilation or shader execution. No source modifications, no silently skipped fixture, no title launch. The earlier failing heads were not rerun.

This supersedes my earlier duplicate-reservation warning for this head, not the historical results below. It does not prove a loading-time/RAM improvement or a rendered world on this machine. Keep the duplicate and compute-stream regressions when rebasing or combining with slow-pipeline scheduling; #3296's reservation ownership is a separate implementation and needs separate evidence.

Evidence: .compat-runtime/logs/verify-pr3025-fixed-wine-scheduling.log. AI-assisted verification/source review (OpenAI GPT-6 Astra).


Earlier verification, preserved for historical refs:Updated verification, 2026-11-26 — 5abc4cd1fdedc81850a424592c583e38c0330fdd: isolated queue-order test now passes; duplicate-slot race remains.

Same Linux / Wine 11.0 / 16 GiB / RTX 4060 Laptop host (CPU-only tests). Fresh Compiling victory-gpu .../VIctory-pr3025-updated/... provenance for both runs, MemoryMax=9G / MemorySwapMax=512M.

  • Clean head: cargo test -p victory-gpu --lib graphics_go_first_and_compute_leaves_a_worker -- --nocapture --test-threads=1 1 passed, 0 failed, 0 ignored. Source review also confirms the normal compute completion now reaches WAITING.fetch_sub.
  • A temporary deterministic local probe using the real next() and taken operations fails 0/1, with compute_busy = 1, expected 0 after all modeled work completes. Probe restored afterward; no production edits. This is a forced interleaving with local queues and a modeled release branch, not a threaded end-to-end worker/GPU execution.

Remaining interleaving (three workers is enough): enqueue the same Arc<Pending> in wanted-compute queue 1 and early-compute queue 3. A calls next(&mut queues, 3) and unlocks before its taken.swap. B calls next and also sees taken == false, reserving the second slot. A's swap wins; B's swap loses and continues before constructing ComputeSlot. Only A returns its reservation; the duplicate's reservation leaks. Moving the preliminary taken.load into next does not close the gap between dequeue and claim.

Suggested minimal owner fix remains: construct ComputeSlot(compute) immediately after dequeue, before p.taken.swap(...)/continue, so every reserved slot has a guard even when the duplicate loses. Alternatively claim atomically in next before reserving and remove the second claim. Add coverage for the dequeue/dequeue/claim schedule, not only a duplicate whose taken was already set before dequeue. This finding is independent of the compute-only-stream classification concern raised by the other reviewer.

Logs (local): .compat-runtime/logs/verify-pr3025-updated-wine-priority.log, verify-pr3025-updated-wine-duplicate-probe.log. Wrapper supplies --locked --target x86_64-pc-windows-msvc. No title, memory-peak, GPU, or FPS claim. AI-assisted verification/report: omp, OpenAI Codex GPT-6 Astra.


Historical report for the old head (retained for comparison):

Blocking isolated regression on the 16 GiB Wine 11.0 laptop (RTX 4060 Laptop GPU present, but this test does not use the GPU). Clean exact head 47069285a59c58c2ab4a30651b26ea868457dbd1; fresh-compiled Windows target; cargo test -p victory-gpu --lib graphics_go_first_and_compute_leaves_a_worker -- --nocapture --test-threads=1 fails 0/1 at pipeline/make.rs:455: observed graphics a draw waits for, compute a dispatch waits for, early graphics; expected also early compute. In the isolated process WORKERS is set to 2; next reserves the one compute slot, and the test repeatedly calls next without simulating that the previous compute has finished/released its slot. The reported 232-suite pass may depend on a different global WORKERS initialization; the isolated result and source establish the issue. Log: .compat-runtime/logs/verify-pr3025-wine-graphics-priority.log (not attached).

Two additional source-verified runtime accounting errors at make.rs:389-406, independent of the fixture: (1) when a compute item is newly taken, p.make() finishes, then if compute { continue; } skips WAITING.fetch_sub(1), so queued compute increases waiting() permanently and drained() may never run. (2) next() increments compute_busy before the p.taken.swap check, but for an already-taken duplicate compute queue entry no ComputeSlot(compute) is constructed, so its slot is never returned; subsequent compute work can starve with two workers. Suggest constructing the RAII ComputeSlot(compute) unconditionally immediately after dequeue, then checking taken; on newly taken work, call p.make(), drop the slot and run the existing WAITING.fetch_sub/cache save path for both graphics and compute. Update the test to model completion/release of the first compute slot before expecting another compute to be popped, ideally with an explicit worker-count parameter rather than a process-global OnceLock.

No retail title trial: this head currently fails its own targeted test and the two accounting paths risk indefinite preparation. No claim about GPU or frame times here.

@steven-seagull27

Copy link
Copy Markdown

The reserved worker preserved a real cold draw on RTX 5080 and WARP for CreateComputePipelineState. Compute-only pipeline-state streams still occupied all eight workers at 47069285. The difference comes from queue_index using p.graphics.is_none(). The stream constructor sets Some(graphics) even for a stream containing only a compute shader. The host compiler correctly recognized all eight resulting PSOs as compute.

I queued eight valid synthetic compute pipelines as early starts and held their CPU compilation at a test barrier. A guest DrawInstanced(3, 1, 0, 0) then requested a fresh graphics PSO and wrote one D32 pixel. The barrier opened after that first draw returned. Two warm draws used separate pixels, one after resetting the list. Both adapters gave the same results.

First draw under compute load Ordinary compute constructor Compute-only stream
Parent 63bcca43 Dropped, depth 0.50 Dropped, depth 0.50
PR 47069285 Rendered, depth 0.25 Dropped, depth 0.50
With the cherry-pick below Rendered, depth 0.25 Rendered, depth 0.25

Both warm pixels were 0.25 and the untouched control was 0.50 throughout. On the PR, the stream case waited 507–510 ms for the first draw. With stage-based classification it took 11.2 ms on RTX and 10.5 ms on WARP in this setup. These waits include the artificial CPU backlog.

I also measured the accounting paths identified in the Wine review. On the PR, all eight ordinary compute PSOs finished and the graphics PSO rendered. After promoting one queued early start, the idle pool still reported WAITING=8 and compute_busy=1. The cherry-pick gives 0/0 for both constructor paths. It shares the stage check with HostPipeline::new, returns the slot on duplicate entries, and makes the isolated queue test independent of the global worker count. Cherry-pick c24afb9 · Earlier queue review

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 47069285 against main (63bcca43). There are three concrete bugs in crates/victory-gpu/src/pipeline/make.rs:

  1. Promoted duplicate compute queue entries permanently leak a compute_busy slot (make.rs:319-322, 383-387)

    • When a compute pipeline is started early (queues[3]) and later promoted by a waiting dispatch (queues[1]), both entries remain queued.
    • When a worker pops the second (duplicate) entry, next() already incremented queues.compute_busy += 1. Then p.taken.swap(true, Ordering::Relaxed) returns true, so if !taken is skipped and ComputeSlot(compute) is never constructed — compute_busy is never decremented.
    • On a 2-worker pool (compute_busy + 1 < 2), a single promoted compute pipeline permanently locks compute_busy == 1 and blocks all subsequent compute pipeline builds indefinitely.
    • Creating let _slot = ComputeSlot(compute); immediately after dequeue (before checking taken) prevents the leak.
  2. Completed compute pipelines never decrement WAITING or trigger drained() (make.rs:224-226, 388-399)

    • enqueue increments WAITING for both graphics and compute pipelines on first queue, but work() has if compute { continue; } at line 388 before WAITING.fetch_sub(1, Ordering::Relaxed) at line 397.
    • Once any compute pipeline is built, WAITING stays permanently non-zero and drained() can never fire. Removing the early if compute { continue; } (with _slot constructed before if p.taken.swap(true, ...) { continue; }) lets both pipeline kinds share completion bookkeeping cleanly.
  3. Unit test fails --all-features CI and fails in isolation on 2 workers (make.rs:54-58, 407-428)

    • Pending::new(Vec::new(), None) omits the #[cfg(feature = "diagnostics")] skip_hashes: &[String] argument, causing the current --all-features CI failure.
    • graphics_go_first_and_compute_leaves_a_worker initializes the process-global WORKERS OnceLock to 2 and pops from next(&mut queues) without ever releasing compute_busy: after popping "compute a dispatch waits for", compute_busy is 1, so compute_free (1 + 1 < 2) becomes false, "early compute" is never popped, and the iterator yields only 3 items while the assertion expects 4. Passing workers: usize into next(queues, workers) also keeps the test isolated from global WORKERS state and lets you test slot release and duplicate-entry behavior directly.

@Luislariosss

Copy link
Copy Markdown
Contributor Author

Thanks for the review — all three points were real and are fixed in 5abc4cd:

  1. compute_busy leaked on a promoted duplicate. next now skips entries whose taken is already set, so a pipeline queued twice never takes a slot; taken is only swapped once and the slot is released through a guard on the way out, a panic in the compiler included.
  2. WAITING never went down for compute. The decrement (and so drained(), which saves the cache) now runs for every entry a worker really made, compute or graphics; the duplicate entry returns before touching it, since enqueue counted it once.
  3. Test broken with --all-features/two workers. next takes the worker count as an argument instead of reading the global (so the test does not depend on other tests), Pending::new gets its diagnostics argument, and the test now also checks that the early compute entry is left queued while one compute is being made, and that a single worker does take compute.

cargo test -p victory-gpu --locked: 232 passed. --all-features: 247 passed. cargo clippy --all-targets --all-features -- -D warnings: clean.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — verified the queue ordering (gfx-now 0, compute-now 1, gfx-early 2, compute-early 3), compute_free keeps ≥1 slot for graphics without starving compute at 1 worker, taken dedups multi-queue entries, ComputeSlot returns the slot even on a compiler panic. Real fix for the 180×3.7s compute stall.

@steven-seagull27

Copy link
Copy Markdown

Verified your 5abc4cd fixes the accounting on RTX 5080 and WARP. After eight ordinary compute builds and a queued promotion, both WAITING and compute_busy returned to zero. The first graphics draw rendered on both adapters.

The compute-only pipeline-state stream still took all eight workers. With the same controlled CPU backlog, the first draw waited 505 ms on RTX and 506 ms on WARP and left its D32 pixel at the clear value (0.50). Both subsequent warm draws wrote 0.25. The stream constructor supplies Some(graphics) even for a single compute stage, so queue_index puts it in the graphics queue.

Cherry-pick 145b380 directly on your new head makes the queue use the host compiler’s stage check. With that commit, seven workers entered compute compilation and the first draw wrote 0.25 through both constructor paths on both adapters. All five queue tests passed, including the new stream case.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

The four-queue ordering and the cap on compute workers in crates/victory-gpu/src/pipeline/make.rs are a reasonable answer to the measured backlog, and the WAITING accounting now runs for compute too. Two problems remain at this head:

  1. A compute slot can still leak. next() increments compute_busy when it dequeues an entry whose taken is still false, but work() only creates the ComputeSlot guard after p.taken.swap(true). If the same Pending sits in queue 1 and queue 3, two workers can dequeue both entries before either swaps. The loser then hits continue without a guard, and its slot is never given back. With 2 workers that is enough to stop compute compilation entirely. Fix: create ComputeSlot(compute) right after next() returns, before the taken.swap/continue. Or claim taken inside next() under the queue lock and drop the second swap. Please add a test for the dequeue/dequeue/claim order.
  2. Compute-only pipeline-state streams are sorted as graphics. queue_index uses p.graphics.is_none(), but the stream path passes Some(graphics) even when the only shader is compute. HostPipeline::new (pipeline/host.rs) decides compute by stage ([cs] if cs.stage == "compute"). So stream-created compute PSOs bypass the cap, and the problem this PR targets can come back through that path. Please classify by the shader stages, as HostPipeline::new does.

The added doc comment on Queues is long and specific to one measurement. A one-line rule (graphics first, compute keeps one worker free) would fit the repo's short-docs rule better.

CI: check passing.


Generated by Claude Code

…er takes every worker

In Grand Theft Auto VI's world about 180 compute pipelines take the host driver 3.7 s each on average (up to 24 s; the other compute ones 0.1 s). Queued in order of first use they held every worker for minutes while the scene's graphics pipelines, made in milliseconds, waited behind them and their draws were dropped: in one 360 s run the compile queue sat at ~870 and ~500-1000 draws a frame were dropped for want of a pipeline. With graphics first, and compute never holding more than all workers but one (the slot given back on a panic too), the queue went to 7 within three minutes, dropped draws fell to ~370, and characters, faces and lights that had been missing appeared. Measured on a Ryzen 5 9600X; 232 tests, clippy clean.
@Luislariosss

Copy link
Copy Markdown
Contributor Author

Both points of the triage are fixed in the new head:

  1. Compute slot leak. next() now claims the entry with taken.swap(true) under the queues' lock; an entry whose pipeline is already claimed is skipped there, so only the worker that claims it counts a compute slot. work() no longer swaps: it holds the ComputeSlot guard from the start, which gives the slot back on a panic too. New test a_pipeline_in_two_queues_takes_one_slot: one pipeline in queues 1 and 3, the first dequeue claims it and counts one slot, the second finds nothing.
  2. Compute pipeline-state streams. queue_index classifies by stage the way HostPipeline::new does (graphics.is_none(), or a single compute stage), so a stream with Some(graphics) and one compute stage goes to the compute queues. New test a_compute_stream_is_queued_as_compute. Same finding as @steven-seagull27's measurement on RTX and WARP; thanks for it.

The doc comment on Queues is down to one line.

cargo test -p victory-gpu --locked: 234 passed; --all-features: 249 passed; cargo clippy --all-targets -- -D warnings clean with and without --all-features.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

The combined test on #3353 keeps your exact 018b56a head. It checks that the urgent presentation draw is chosen first while a compute slot is occupied, and that the duplicate early start cannot claim the pipeline again. 4a59f2e also makes workers read the published pool size after initialization. A worker can otherwise start before WORKERS.get_or_init returns and keep a local limit based on a pool size of one.

The scheduler/presentation tests passed with both feature configurations. If you take the queue resolution or regression, please preserve its Git author or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> when folding it into another commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants