d3d12: a pool of compile workers for the pipeline compiler (RT-ASYNC-COMPILE-POOL) #2233
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2233"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-26T09:21:39Z.
Claimed in the coordination issue as: RT-ASYNC-COMPILE-POOL (claim; released by MoonEyes after #2077 was closed)
What and why
With
LEONIDA_ASYNC_PIPELINES=1, every demanded build, the compute warm-up and the startup cache checkpoint run onPipelineCompiler's one general worker (plus #2098's graphics-only worker). One long driver compile therefore holds every pipeline a submission asked for behind it, and their draws and dispatches are skipped meanwhile. This ports MoonEyes' worker-pool design from #2077 onto #2098's scheduler (runtime/shims/d3d12_pipeline_compile.h), keeping its lifetime rules (after_running,cancel,running_here,release,stop_for_exit).PipelineCompilernow runsLEONIDA_COMPILE_THREADSworkers (default half the logical processors, 2 to 8). A worker takes the most urgent job it may:LEONIDA_COMPILE_HEAVY_MAXlarge builds at once (default threads-1; 1 withLEONIDA_SERIAL_BIG_COMPILES=1), compute and graphics alike. Each still takes its place at the memory gate (LEONIDA_BIG_COMPILE_BUDGET_MB). Graphics builds are counted by size even whenLEONIDA_SERIAL_BIG_COMPILES=0clearsgraphics_.large, so the async path follows the same knobs for both.runtime/src/vulkan_device_fault.h) makes every submission wait for each create in flight. With the gate on, their boot's submissions waited 142 s in all behind warm-ups of about 800 KB (5-13.5 s each). WithLEONIDA_COMPILE_GATE=0the same boot reached game state 3 at 130 s and ran 240 s with no fault. Running several warm-ups at once would lengthen those waits; so while the title asks for builds, only those builds are started.after_runningand the teardown rules hold as they were.vkGetPipelineCacheDatasize query.has_compile_waiter(), which the checkpoint asks before the query and again after it, now also answers true while a demanded build runs, not only while one is queued, so the checkpoint stops before the blob read. No warm-up can start in that window: the running checkpoint is the one speculative job allowed.LEONIDA_COMPILE_THREADS=1is the layout before the pool: one general worker and the graphics-only one (LEONIDA_GRAPHICS_COMPILE_WORKER), with the old job order.Not changed here, and still open: a submission still waits at the compile gate for every create in flight, including a warm-up already running. The pool shortens how long a demanded build waits behind another build. It does not stop the gate holding the title's thread, which is what ends brandostrong's synchronous runs at the 30 s watchdog. MoonEyes' two ideas (warm-ups not holding the gate; a cap on the wait) touch the gate itself and belong in a separate item.
Synchronous mode (
LEONIDA_ASYNC_PIPELINES=0, the default) and MoonEyes' finding of 10:31 in #1608 (large compute builds one after another, although the driver compiles about 3.7 times faster on 6 threads): there a build a dispatch asks for runs inside the submission, throughPipelineCompiles::build(runtime/src/vulkan_replay.h) and its memory budget, not on these workers. Only the compute warm-up and the checkpoint go throughPipelineCompilerin that mode, and warm-ups stay one at a time. So this pull request does not change that case; letting more large builds run at once there is a change toPipelineCompiles(or to the warm-up's one-at-a-time rule, which the compile-gate finding above argues against while the title submits) and belongs in its own item.A note on azsekazvamdimitar's report (#1608, 06:42) that graphics jobs of 1 MiB or more are always serial in
PipelineCompiles. On currentmain,graphics_.largeis false withLEONIDA_SERIAL_BIG_COMPILES=0, and otherwise the large ones share the memory budget. The serial builds they saw in the asynchronous path came from the single graphics worker, which this pool replaces.One build per PSO with
LEONIDA_COMPILE_THREADS=1(the general worker plus the graphics-only one): two variants of one graphics PSO could build at once there, one on each worker, and a last Release then deleted the PSO under the second (found by qhukz13). This is the same race qhukz13 claimed as RT-COMPILE-OWNER-RACE and fixes formainin #2361, which I missed. The single-worker branch ofnext_lockedhere now uses #2361's code line for line, so whichever of the two lands second merges trivially; if #2361 lands first, this pull request takes its version.Tested
New CPU test
d3d12_compile_pool(no device). Its jobs wait at gates the test opens, so each rule is checked while the jobs it is about are still running. It covers:release,cancelandstop_for_exitwith jobs running and queued.It passed 8 runs out of 8. Each rule was broken by hand in turn (7 mutants, before the checkpoint fix: checkpoint-when-idle, one job per PSO, the free worker, the heavy count, one warm-up, warm-ups behind demanded builds, waking idle workers), and the test failed for every one.
d3d12_compile_teardownruns with the pool, and as newd3d12_compile_teardown_one_workerwithLEONIDA_COMPILE_THREADS=1. Both pass, as dod3d12_big_compute(all 7 settings),d3d12_m4_compute,big_compile_gate,compile_helper,replay_pipeline_compiles*,pipeline_workersandd3d12_pipeline_workers*.Full runtime CTest,
-j 4(a boot was running on this machine). Everything passes except four:xs_proxy_mockandd3d12_multithreadfailed under that load and pass when rerun alone;xbox_present(presentVkResult -11) andd3d12_set_descriptor_heaps(line 353) fail the same way onupstream/maina73a57e13without this change.python -I tests/test_docs_env_vars.py,test_ctest_registration.py,test_docs_repo_paths.py,test_text_write_line_endings.py,scripts/check_no_game_data.pyon the changed files: pass.Test-merged with the open PRs on the same code: #2198, #2220 and #2208 merge cleanly. #1756 conflicts in
d3d12_vulkan_device.cpp, but it already conflicts withmainby itself. It also moves the checkpoint to priority 1, so whichever lands second must keep "checkpoint only when idle" keyed on the checkpoint, not onSIZE_MAX.No real boot was run with this change; the boot session will test it.
GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11
AI-assisted: yes (Claude Code)
Checklist
mainand merges cleanly.docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T10:37:12Z.
Review: COMMENTED
The native Windows build passed
d3d12_compile_pool,d3d12_compile_teardownandd3d12_compile_teardown_one_worker. I also checked the optional startup checkpoint with gated jobs in the real scheduler. No retail workload. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T10:37:12Z.
Code review location:
runtime/shims/d3d12_pipeline_compile.h; line 539[P3] Keep the checkpoint's second pressure check aware of running demands. Checkpoint admission is idle-only, but a priority-0 job can start on a second worker during the checkpoint's
vkGetPipelineCacheDatasize query. I reproduced the transition with gated jobs: for the same demand,has_compile_waiter()is true with one worker and false with two once the second worker takes it. The second check insave_session_pipeline_cachethen proceeds to the blob read even though the demand is active, which can contend again for the driver cache. Could the checkpoint's post-query check also recognize a running demand? The probe checks scheduling, without measuring driver latency.Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-26T10:53:41Z.
Code review location:
runtime/shims/d3d12_pipeline_compile.h; line 539Thanks @lixonik, good catch, and thanks for the gated repro. Fixed in
2d56b685a.has_compile_waiter()now also returns true while a demanded build is running, not only while one is queued. The checkpoint asks it before and after the size query, so the second look now stops before the blob read when a demand started on another worker meanwhile. Only demanded builds needed adding: no warm-up can start in that window, since the running checkpoint is the one speculative job the pool allows.Test:
test_a_checkpoint_sees_a_demand_that_started_during_itinruntime/test/d3d12_compile_pool_test.cpp. Two workers; the checkpoint job checks the first look, then waits at a gate standing in forvkGetPipelineCacheData's size query; a demanded job starts on the second worker; the gate opens and the second look must be true. Before the fix it fails (second_look == 1), after it passes (9 runs of 9).d3d12_compile_teardown(pool and one worker),d3d12_big_compute*,d3d12_m4_computeandd3d12_pipeline_workers*still pass.With
LEONIDA_COMPILE_THREADS=1the same now also holds for a demanded graphics build on the graphics-only worker, which the old check missed as well.AI-assisted (Claude Code)
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-26T10:57:09Z.
Test of
a83f38508on a world-reaching recipe: RTX 4070 SUPER, NVIDIA 621.01, Windows 11, 64 GB. Counts only.Tree:
maina73a57e13+ #1851 #1905 #1957 #1886 #1894 #1914 #1882 #2195 #2113 #1592 #2097. The pool runs add this PR (clean merge).d3d12_compile_pool_test: PASS.Recipe (as in my #1608 comment of 09:00):
LEONIDA_COMPILE_GATE=0, a local skip list of the 724 library compute programs of 2 MiB or more,LEONIDA_RT_AS=off,LEONIDA_DEVICE_FAULT=0, the watchdog kicked,LEONIDA_COMPILE_WORKERS=6, warm offline and heavy caches, 1,800 s, the same 21-press script.LEONIDA_COMPILE_THREADSleft at its default.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by HasiKe on 2026-11-26T11:37:43Z.
Test report for this PR at head
2d56b685a. I merged it locally onmaina73a57e13together with #2195, becausemainalone aborts at startup on this machine. AI-assisted (Claude Code).Setup: RTX 2070 SUPER, NVIDIA 616.56, Windows 11 Pro 26300, MSVC RelWithDebInfo via
scripts/build_runtime.ps1 -FetchSdl3, my own 1.0.2.1 package. Every boot ran 300 s withLEONIDA_ASYNC_PIPELINES=1, the keyboard pad,LEONIDA_TITLE_WATCHDOG=0with the kick RVAs,LEONIDA_CONSOLE_FIX_US=0and 4096 MB title windows and arena.d3d12_compile_pool_testpasses.The cold boots have no session or seed cache and an empty journal. Each also has its own empty
__GL_SHADER_DISK_CACHE_PATH, so the driver cache does not warm the second run.main+#2195main+#2195LEONIDA_COMPILE_GATE=0What this shows on this GPU:
submit-handofflines andwait 0.00in the submit phases. So that cause is my reading, not a measurement.LEONIDA_COMPILE_GATE=0the title moves on to about 200 submissions/s and 100 frames/s from 65 s on. It then demands 98 builds, and the pool runs them in parallel: 49 dispatch, up to 30.7 s each; 49 draw, up to 16.8 s each. At 249.7 s the device is lost.vkGetSemaphoreCounterValuereturnsVK_ERROR_DEVICE_LOST, andnvlddmkmlogs event 153 (TDR) in the same minute. Right before the loss, fourdraw-workercreates were in flight on four threads while the title kept submitting. This is the case the gate was added for, and it matches the device losses fiestorra reports in #1608 on an RTX 4090. One boot only, so it is not proof that parallel creates cause the TDR.So on this machine the pool is safe to merge as it is. The win MoonEyes measured with the gate off comes with a device loss here. I would rather not run with
LEONIDA_COMPILE_GATE=0until the "warm-ups do not hold the gate" follow-up exists. That follow-up looks like the change that would actually let a cold boot get going.I'm happy to run that follow-up, or a
LEONIDA_COMPILE_THREADS/LEONIDA_COMPILE_HEAVY_MAXsweep with the gate off, on this card. Logs stay local: they name game shaders.Imported from ubiquitarianism/leonida. Originally posted by HasiKe on 2026-11-26T11:47:58Z.
Correction to my report above, on the device loss with
LEONIDA_COMPILE_GATE=0. AI-assisted (Claude Code).I linked it to the four parallel creates, and that is probably wrong. fiestorra's newest finding in #1608 gives a better explanation. It describes a world-load submission that never completes, a TDR at about 2 s, with no pipeline build anywhere in the run. azsekazvamdimitar avoids it on a 3090 by skipping the ray-hit shading programs.
My gated boots never got far enough to hit it: about 700 submissions in 300 s. The ungated one got there: about 24,000 submissions, with the frame rate dropping from 165 s on. So the loss at 249.7 s is most likely the same GPU hang. What made the difference is how far the title got, not the pool.
That changes the conclusion. The data does not show that parallel creates are unsafe on this card. The pool still looks safe to merge. Whether the gate can be dropped stays open until the hang is fixed. I'm trying a repeat with
LEONIDA_DEVICE_FAULT=2next, to see whether NVIDIA's checkpoints name the submission and pipeline. I'll post the result in #1608.Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-26T12:06:40Z.
@HasiKe thank you for the thorough cold/warm comparison and the correction. The "warm-ups do not hold the gate" follow-up is up as #2275 (stacked on this PR).
With the gate on, a submission now waits only for demanded creates. A warm-up's create does not hold it, but still never enters the driver while a submission is in it or waiting, and at most one warm-up is in the driver at once (
LEONIDA_COMPILE_GATE_WARMUP_MAX, default 1).LEONIDA_COMPILE_GATE_WARMUPS=1gives the old behaviour for an A/B;LEONIDA_COMPILE_GATE=0is unchanged.If you can repeat your cold boot on the RTX 2070 SUPER with the gate on, submissions per second while the warm-ups run would show whether it helps, and whether the device stays. CPU tests only on our side so far.
AI-assisted (Claude Code)
Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-26T14:29:33Z.
Code reading of
2d56b685a(no build here yet). One finding I think matters beyond this PR: the pool closes a race thatmainhas by default, but theLEONIDA_COMPILE_THREADS=1fallback keeps it.Two builds of one graphics PSO on two workers, and its last Release. With one general worker plus the graphics worker (main's default,
LEONIDA_GRAPHICS_COMPILE_WORKERon),next_lockeddoes this:jobs_.begin(), which can be a graphics job;Neither looks at
owner(d3d12_pipeline_compile.h:531-534here, the same lines on main). So two variants of one graphics PSO can be built at once, one on each worker. If the title releases that PSO meanwhile,PipelineState::last_releasecallsafter_running(compile_owner(), [this] { delete this; ... }).after_runningattaches the deletion to the firstRunningentry of that owner and returns (d3d12_pipeline_compile.h:387-399). That worker deletes the PSO as soon as its own job returns. The other worker is still insidebuild()on the same object, or waiting on itsvariants_mutex_.In pool mode this cannot happen:
next_lockedskips any job whose owner already has a running job (548-549). WithLEONIDA_COMPILE_THREADS=1the old selection, and the race with it, come back. Either of these would remove it:after_runningattachinglastto the last running job of the owner instead of the first.What the pool still serializes, as I read it. These are by design, and listed so the next reader doesn't have to find them:
threads - 1workers, so one stays free for small demanded builds;variants_mutex_, the large-module memory budget, andDriverTurn/CompileTurnfor compute.So a demanded build can still wait behind a running warm-up of its own PSO, or behind one that holds the large-module budget.
Docs:
runtime/README.md:331saysLEONIDA_SERIAL_BIG_COMPILES=0lets all large compiles run side by side. The pool still caps them atLEONIDA_COMPILE_HEAVY_MAX(d3d12_pipeline_compile.h:551).DriverTurn; graphicsbuild()does not.The code reading was done with a second agent and checked against the source before writing this.
AI-assisted (Claude Code, with Antigravity / Gemini 4 Neon).
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-27T10:37:58Z.
@qhukz13 thank you for the careful reading. The race is real, and
mainhas it too. Fixed here in9b8760d32;mainwas merged in ate5401bd62.Fix:
next_locked's single-worker branch (LEONIDA_COMPILE_THREADS=1: one general worker plus the graphics-only one) now skips a job whose PSO has a build running, as the pool branch already did; both use one helper,pso_busy. Soafter_running, which hands the deletion to the build of that PSO it finds running, can no longer leave a second build of the same PSO inbuild()on the other worker. I took this route rather than attachinglastto the last running job, because it also keeps the two variants from contending for the PSO'svariants_mutex_.Test:
test_one_worker_and_the_graphics_worker_build_one_job_of_a_psoind3d12_compile_pool_test, with one worker plus the graphics worker, gated jobs:after_runningruns after variant 1, with variant 2 dropped and nothing running.Without the fix it fails 3 checks (variant 2 starts on the graphics worker). With it, it passes 8 of 8 repeats. After the merge of
main,d3d12_compile_pool,d3d12_compile_teardown(pool and one worker),d3d12_big_compute(7 settings),d3d12_m4_compute,d3d12_pipeline_workers*andbig_compile_gatepass, 14 of 14.Docs, both points taken:
LEONIDA_SERIAL_BIG_COMPILESrow now says0is still bounded byLEONIDA_COMPILE_HEAVY_MAXon the workers.Your list of what the pool still serializes matches my reading.
mainwithout this PR:next_lockedreturnsjobs_.begin()for the general worker and the first graphics job for the graphics worker, with no owner check (d3d12_pipeline_compile.h:423-425on2859d0c9b). Asynchronous graphics builds passgraphics=true(d3d12_vulkan_device.cpp:12616), and the graphics worker is on by default. So withLEONIDA_ASYNC_PIPELINES=1two variants of one PSO can build at once there, andlast_release(:11344) can delete the PSO under the second. I'll post that as afinding:.AI-assisted (Claude Code)
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-27T12:56:06Z.
Reviewed at
78a5eab40, then pushed merge commit8bab5f281ontoclaude/rt-async-compile-poolto merge currentmain(690dea519), resolve theruntime/README.mdconflict with#2220, and refresh the compile-gate note indocs/d3d12_vulkan.mdto reflect#2412(LEONIDA_COMPILE_GATE=adaptive/1also gating graphics creates).Verified locally on
8bab5f281:scripts/check_no_game_data.pyon all 6 changed files: pass.tests/test_docs_env_vars.py(5/5),tests/test_docs_repo_paths.py(3/3),tests/test_ctest_registration.py(6/6),tests/test_ci_registration.py(18/18),tests/test_text_write_line_endings.py(3/3): pass.d3d12_compile_pool,d3d12_compile_teardown, andd3d12_compile_teardown_one_workerpass; 0 regressions vsmain-e5a7c8942(431 pass, 40 skip, 23 pre-existing Wine-iGPU failures, 2 disabled).Merging.
AI-assisted review (Antigravity, Gemini 4 Neon).