Skip to content

D3D12.X: boost low-priority preload PSO workers on demand and priorit… - #1007

Closed
zondy1i wants to merge 1 commit into
xcl-dev:mainfrom
zondy1i:d3d12x/pso-priority-inheritance
Closed

zondy1i wants to merge 1 commit into
xcl-dev:mainfrom
zondy1i:d3d12x/pso-priority-inheritance

Conversation

@zondy1i

@zondy1i zondy1i commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes a priority-inversion bottleneck in pso_async + BIG_COMPILES (related to #675 / #505):

  • Preload workers (xcl-pso-pre-0..3) run at THREAD_PRIORITY_LOWEST on native Windows so speculative ahead-of-use compiles from used-pipelines.txt stay out of the title's way.
  • However, once a preload worker takes a pipeline's job, a later demand bind (resolve / resolve_wait) cannot run the job itself and must wait on that THREAD_PRIORITY_LOWEST worker. Worse, translated_compute_pso routes both demand (xcl-pso-*) and preload (xcl-pso-pre-*) big compiles (>= 256 KiB DXIL) through the single BIG_COMPILES slot (most = 1) in FIFO order: a THREAD_PRIORITY_LOWEST preload worker holding BIG_COMPILES under world-load CPU contention can stretch a ~25 s driver compile to ~90–100 s while starving demand-bound compute compiles behind it.

This adds refcounted priority inheritance (BoostToken) for preload workers and demand-first admission on BIG_COMPILES without skipping any compute dispatches:

  • When resolve, resolve_wait, or resolve_within needs a pipeline already owned by a xcl-pso-pre-* worker, that worker is boosted to THREAD_PRIORITY_NORMAL until the build completes (and woken if queued on BIG_COMPILES).
  • In BIG_COMPILES, demand compiles (xcl-pso-* or a demand-promoted preload worker) always enter ahead of queued speculative preload workers, and any THREAD_PRIORITY_LOWEST preload worker currently holding a slot is boosted to THREAD_PRIORITY_NORMAL until it releases the slot.
  • Dropping the last BoostGuard restores THREAD_PRIORITY_LOWEST.
  • Wine / Linux: Under Wine (crate::util::under_wine()), lowering priority to THREAD_PRIORITY_LOWEST maps to raising Linux nice (always allowed), while raising it back to NORMAL requires RLIMIT_NICE / CAP_SYS_NICE and fails for a normal user. When running under Wine, preload workers stay at THREAD_PRIORITY_NORMAL and SetThreadPriority calls are skipped, while BoostToken refcounting and BIG_COMPILES demand-first admission remain active (thank you @sander110419 for testing and reporting this on GE-Proton).

What is in it

  • crates/xcl/src/hle/d3d12x/pso_async.rs: add can_lower_preload_priority() (!crate::util::under_wine()), BoostToken / BoostGuard (refcounted OpenThread(THREAD_SET_LIMITED_INFORMATION) + SetThreadPriority), thread-local preload token registration, Pending::promote_preload() in resolve, resolve_wait, and resolve_within, and unit tests (boost_token_refcounts_nested_guards, resolve_wait_boosts_preload_worker_priority_and_restores_it).
  • crates/xcl/src/hle/d3d12x/real.rs: update Slots (BIG_COMPILES) to track demand_waiting and active preload_holders, boost holders on demand arrival, wake all waiters on release (notify_all), expose notify_big_compiles() for promoted waiters, and add unit test big_compile_slots_boost_preload_holder_and_run_demand_first.

Tested

  • Hardware: NVIDIA GeForce RTX 3060 Ti 8 GB (driver 32.0.16.2101), Ryzen 5 7500F, 64 GB RAM, Windows 11 Build 26300, DXC 1.9.2609.5, Rust 1.100.0 (x86_64-pc-windows-msvc), debug + release.
  • cargo test --workspace -- --test-threads=1: 740 passed, 0 failed, 41 ignored (1 SKIPPED GPU test: gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64)). All three new tests ran and passed (boost_token_refcounts_nested_guards, resolve_wait_boosts_preload_worker_priority_and_restores_it, big_compile_slots_boost_preload_holder_and_run_demand_first).

Regression run
(local folder name redacted to retail-title in both blocks; all metrics are exact output of the 60 s runs with XCL_NETWORK=0)

main:

xcl-smoke v1
commit: 384f7f7f8c79fa4d4dee59336d650a4021af4c7e
gpu: NVIDIA GeForce RTX 3060 Ti
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1631 (PresentX 1631)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14885 MB

PR:

xcl-smoke v1
commit: 4b3f4405d167ddbe592e1903d3c58303eae5ad17
gpu: NVIDIA GeForce RTX 3060 Ti
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1569 (PresentX 1569)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16061 MB

Not tested

  • AMD native wave64 GPUs.
  • Linux / Wine runtime execution of the updated under_wine() path (verified by code inspection and native Windows test run; awaiting re-test on Wine/Proton).
  • Multi-slot BIG_COMPILES (most > 1 is supported structurally by preload_holders: Vec<_>, though BIG_COMPILES stays most = 1 here).

AI use

  • Antigravity (Gemini 4 Neon): identified the preload priority-inversion path in pso_async.rs / real.rs, wrote the implementation, Wine fallback, unit tests, and PR description; all tests and paired smoke runs were executed locally on the hardware above and the diff was reviewed line by line.

@sander110419

Copy link
Copy Markdown

A note from Linux (Wine), in case it matters for the design. The boost does not take effect there.

cargo test of this PR merged into a local Wine branch (GE-Proton11-11, NVIDIA RTX 5070 Laptop, release toolchain x86_64-pc-windows-gnu): 5 of 7 pso_async tests pass. The two new priority tests fail:

boost_token_refcounts_nested_guards: pso_async.rs:538  left: -2  right: 0   (LOWEST after the boost)
resolve_wait_boosts_preload_worker_priority_and_restores_it: fails as well

Wine applies thread priorities as Linux nice values. Lowering a priority (THREAD_PRIORITY_LOWEST) is always allowed. Raising it again needs RLIMIT_NICE / CAP_SYS_NICE, which a normal desktop user does not have (ulimit -e is 0 here). So SetThreadPriority(NORMAL) on a worker that went to LOWEST fails, and the worker stays at the low priority for the rest of the run. The demand-first ordering in BIG_COMPILES should still work, since it does not depend on priorities. Only the inheritance part is lost.

Two possible ways around it, untested: under Wine (wine_get_version present in ntdll), leave the preload workers at NORMAL and rely on the admission order; or let a demand bind take the job itself when the preload worker has not started the driver compile yet.

The game was not run with this PR yet; only the unit tests above.

AI use: Hermes agent with Claude Opus 5.5 (claude-opus-5-5) ran the build and the tests, and wrote this note; I checked it.

@zondy1i
zondy1i force-pushed the d3d12x/pso-priority-inheritance branch from aceb50e to 4b3f440 Compare November 24, 2026 11:01
@zondy1i

zondy1i commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks for testing this on Wine/GE-Proton and catching the SetThreadPriority / RLIMIT_NICE issue!

Updated the PR (rebased on 384f7f7) using crate::util::under_wine():

  • Under Wine, xcl-pso-pre-* workers stay at THREAD_PRIORITY_NORMAL and SetThreadPriority calls are skipped, while BoostToken refcounting (is_boosted()) and BIG_COMPILES demand-first admission remain active.
  • Also hooked promote_preload() into the new resolve_within() path from D3D12.X: XCL_PSO_WAIT_MS bounds how long a bind waits for a pipeline being built (retail world renders) #891.
  • Updated all three unit tests (boost_token_refcounts_nested_guards, resolve_wait_boosts_preload_worker_priority_and_restores_it, and big_compile_slots_boost_preload_holder_and_run_demand_first) to always verify is_boosted() and demand-first ordering while only asserting OS LOWEST/NORMAL transitions outside Wine.

@SlopTart

Copy link
Copy Markdown

Linux / AMD RDNA3 run: this PR's three new boost tests fail deterministically under Wine.

Merged state: head ee2e530 onto current main, clean merge, full suite 726 passed / 14 failed / 41 ignored; the failures are my known Wine-lane set from #42 plus this rig's mirror flake, and 3 extras that are exactly this PR's new tests, each failing 2/2 in isolation with --test-threads=1:

  • hle::d3d12x::pso_async::used_tests::boost_token_refcounts_nested_guards - assertion left == right failed, left: -2, right: 0 at pso_async.rs:500
  • hle::d3d12x::pso_async::used_tests::resolve_wait_boosts_preload_worker_priority_and_restores_it - worker thread asserts at pso_async.rs:527 ("preload worker must be boosted while resolve_wait waits", left: -2), then the test fails at pso_async.rs:539 "preload worker was not boosted to NORMAL" after the 5 s wait
  • hle::d3d12x::real::console_format_query_tests::big_compile_slots_boost_preload_holder_and_run_demand_first - same 5 s shape

Diagnosis lead, not a verdict: under Wine the boosted priority lands at -2 instead of NORMAL/0, i.e. the raise looks like it does not map through Wine's SetThreadPriority/GetThreadPriority emulation. Worth checking whether the boost is a relative +n from the current priority (Wine's notion of current differs from Windows). On native Windows CI these would presumably pass - but heads-up that CI here is description-check-only, so nothing compiles or tests these lanes before merge.

Everything else on the merge is green, including the rest of pso_async.

Counts only, no game data.

Not tested: retail titles (none here), Windows, RDNA4.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. My Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@zondy1i

zondy1i commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Thanks for testing on Linux/Wine! From the line numbers (pso_async.rs:500/527/539) it looks like that run merged the earlier commit (aceb50e) before the Wine fix landed in 4b3f440.

In 4b3f440 (current PR head), can_lower_preload_priority() checks crate::util::under_wine() so preload workers stay at THREAD_PRIORITY_NORMAL under Wine and skip SetThreadPriority, while all three unit tests (boost_token_refcounts_nested_guards, resolve_wait_boosts_preload_worker_priority_and_restores_it, big_compile_slots_boost_preload_holder_and_run_demand_first) assert is_boosted() and BIG_COMPILES demand-first ordering unconditionally and only assert OS LOWEST/NORMAL transitions outside Wine. Feel free to re-run against 4b3f440 if you get a chance!

@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification, merged onto current main 384f7f7 locally (clean merge).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib pso: 22 passed, 0 failed, 1 ignored (compile-time bench) — includes the new resolve_wait_boosts_preload_worker_priority_and_restores_it and boost_token_refcounts_nested_guards, covering the on-demand worker boost and its restoration.
  • Scope note: ~421 changed lines, slightly over the ~400-line guideline.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@SlopTart

Copy link
Copy Markdown

Fix verification from the Wine/RDNA3 lane: re-merged the updated head 4b3f440 onto current main and re-ran the three tests that failed 2/2 for me at ee2e530 (my 12:04 comment, the GetThreadPriority returns -2 ones).

All three now pass in isolation (--test-threads=1):

  • boost_token_refcounts_nested_guards - ok
  • resolve_wait_boosts_preload_worker_priority_and_restores_it - ok
  • big_compile_slots_boost_preload_holder_and_run_demand_first - ok

The under_wine() guard (preload workers stay NORMAL and SetThreadPriority is skipped under Wine, while BoostToken refcounting and demand-first admission still get asserted) resolves the failures on this rig exactly as the diagnosis predicted. Verified merged state: lane merge at 4fab701, release profile.

Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile. My Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@zondy1i

zondy1i commented Nov 24, 2026

Copy link
Copy Markdown
Author

Superseded by commit 25c6f7e on main (D3D12.X: a big pipeline build the title waits for does not wait for its turn), which addresses both preload worker priority elevation on demand and bypassing the BIG_COMPILES slot / commit wait.

Closing this PR — thanks to everyone who tested and reviewed!

@Royalflamejlh

Copy link
Copy Markdown
Contributor

Linux (Wine + vkd3d-proton) + NVIDIA test of 4b3f440 and of the head before it, aceb50e, 3 runs of 100 s each: under Wine neither changes the crawl that the preload of #690 causes here. The title's burst of pipeline creation after its loading screen starts stayed slow in 6 of 6 runs, as on main, and with 4b3f440 it was slower than on main.

Setup: Fedora 44 (kernel 7.2.9), Wine 11.21 Staging (Wayland driver), vkd3d-proton 3.1 with the relaxed-vtable patch from #175 and a local dxil-spirv patch, VKD3D_CONFIG=ignore_shared_fence. RTX 4070 12 GB (driver 615.71.09, 32-lane waves), Ryzen 9 9950X3D, 64 GB RAM. Release, cross-compiled to x86_64-pc-windows-gnu (rustc 1.98.0), DXC 1.9.2609.5, XCL_NETWORK=0, every other setting at its default. Retail title 1.0.2.1, the first-run screens completed once and no story progress, one A press at 44.8 s through XCL_PRESS_AT. used-pipelines.txt held 1,131 shader sets from earlier runs that had reached the world load. aceb50e was merged onto main 384f7f7 (no conflicts); 4b3f440 is on 384f7f7 itself. Since 25c6f7e ("a big pipeline build the title waits for does not wait for its turn") went into main, this pull request conflicts with it in pso_async.rs and real.rs.

Tested

  • cargo test --release -p xcl --lib for boost and big_compile_slots under Wine, on 4b3f440: 3 passed, 0 failed. These are the tests a Linux tester reported failing on aceb50e.
main 67a3735 / 384f7f7 / 384f7f7 + aceb50e, run 1 / 2 / 3 4b3f440, run 1 / 2 / 3 for comparison: 67a3735 + #688, run 1 / 2 / 3
[SHADER] lines from 60 s to 80 s 414 / 274 / 386 533 / 452 / 265 2 / 2 / 2 10,466 / 10,465 / 10,465
DirectStorage requests from 70 s to 100 s 1,063 / 1,072 / 1,065 1,062 / 1,065 / 1,064 1,064 / 3,849 / 1,072 51,221 / 55,027 / 50,986
frames presented in 100 s 2700 / 2696 / 2252 2711 / 2514 / 2701 2739 / 2298 / 2595 2002 / 2145 / 1809

A slow run presents more frames because it stays on the loading pictures at 29 fps. A normal run makes about 10,465 of those lines in that window and then thins out as the world loads.

Read

  • What holds the title's threads in these runs is not a priority. In thread dumps of a slow run (main beaa245 + XG: fix CopyFromSubresource 7-argument ABI and guard destination span #684) five title threads in dev_create_pipeline_state and the DXC thread compiling for a preload worker all wait in RtlEnterCriticalSection under RtlAllocateHeap: Wine's process heap has one lock for these sizes, and DXC allocates from it for the 23 to 50 s a big compute program takes. Shaders: isolate DXC compiler heaps for parallel AOT #688 gives DXC heaps of its own, and with it the same runs are normal (last column).
  • pso_async.rs:30 (can_lower_preload_priority): under Wine 4b3f440 keeps the preload workers at normal priority. That matches the middle columns: with the workers no longer lowered the burst made 2 lines in the window in each run, against 265 to 533 with aceb50e. So on Wine the lowering was doing a little good, and the reason given for dropping it (the priority cannot be raised back) does not seem to cost anything here, because nothing in these runs was waiting on a boost. Would it be worth keeping the workers lowered under Wine and skipping only the boost?

Not tested: the demand-first order of BIG_COMPILES (no run got to a big compile wanted by a bind); past 100 s; native Windows, where the priorities do what this change expects; the rest of the test suite; AMD and Intel.

AI use: Claude Code (Claude Fable 5.2) read both diffs, built the trees, ran the title with XCL_PRESS_AT, took the thread dumps, compared the logs and wrote this report.

wondercrash added a commit to wondercrash/xcl that referenced this pull request Nov 24, 2026
…e guard only

integ already has upstream 25c6f7e: a bind that waits marks the pipeline
wanted (Pending::want); a preload worker building it is raised to normal
priority until the build is done (build / want, by thread id), and a
wanted build skips the big-compile slot and the commit wait
(real::big_compile_turn). That covers both parts of xcl-dev#1007: the boost of a
preload worker a bind waits for, and demand builds going ahead of builds
made ahead (they no longer queue for the slot at all). xcl-dev#1007's BoostToken /
BoostGuard, its Slots rewrite, notify_big_compiles and their tests are not
taken: a second priority mechanism and a second slot admission rule.

Taken: the Wine guard (preload_runs_low). Under Wine a thread priority is
a Linux nice value; raising it again needs RLIMIT_NICE / CAP_SYS_NICE, so
a preload worker lowered once stayed at the lowest priority even while a
bind waited for it (reported on the PR from GE-Proton). Under Wine the
preload workers now stay at normal priority and build() neither raises
nor lowers them.
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.

5 participants