Skip to content

d3d12: a pool of workers for the pipeline compiler (RT-ASYNC-COMPILE-POOL) - #2077

Closed
MoonEyes wants to merge 7 commits into
ProjectLeonida:mainfrom
MoonEyes:feat/async-compile-pool
Closed

MoonEyes wants to merge 7 commits into
ProjectLeonida:mainfrom
MoonEyes:feat/async-compile-pool

Conversation

@MoonEyes

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: RT-ASYNC-COMPILE-POOL (#1608, comment 7169077699)

Depends on: #1889 (the memory gate for large pipelines), #1756 (conflicts: both change has_compile_waiter; whichever lands second rebases). This branch is #1889 plus the pool commit and a doc fix, with main merged in; review the commits after #1889's head (aa8018b).

What and why

rt::d3d12::PipelineCompiler (runtime/shims/d3d12_pipeline_compile.h) runs every LEONIDA_ASYNC_PIPELINES=1 build, the compute warm-up and the cache checkpoint on one below-normal thread. One long driver compile holds every pipeline a submission asked for behind it, and their draws and dispatches are skipped meanwhile.

In-game observations (reported by OreoFR on the project Discord, quoted with his permission)

Numbers as OreoFR reported them; I did not measure them and they are not reproduced here.

  • At each device loss (4 traced runs, DadaCap's mesh branch, asynchronous compilation) the only pipeline being compiled was a mesh pipeline a draw asked for.
  • With synchronous compilation (replay_skips=0) the device holds after the world starts: 6,191 indirect draws recorded at 176 s, then 11,211 later in the same run.
  • The three ScreenProbes compute modules (ActivateRays, ActivateProbes, ActivateProbesTarget) take the NVIDIA driver 30 to 32 minutes each, and compiling all three in parallel makes NVVM fail.
  • Early in the world, compilation is only 11% of frame time (8 s of 72 s).

Hypothesis, which OreoFR is checking and which is not confirmed: a dispatch skipped because its pipeline is not ready may leave a later compute looping, waiting for a value that never comes.

This pool reduces skips and compile walls. It does not reduce the per-frame cost in steady state.

Change

  • LEONIDA_COMPILE_THREADS workers: unset, half the logical processors, at least 2 and at most 8. 1 is the one thread there was, with the same order of jobs.
  • At most N-1 workers are in heavy builds at a time (the warm-up's, and demanded builds of large modules: compute over 128 KiB, anything of 1 MiB or more), so one worker is always left for a build that is not heavy. Graphics pipelines under 1 MiB (mesh pipelines of a few hundred KiB, for one) are not heavy: N of them can still hold every worker.
  • One very large build (1 MiB of SPIR-V or more) at a time, demanded or speculative: LEONIDA_COMPILE_HEAVY_MAX (default 1, 0 for no limit of its own). This is on top of the memory gate, which admits what fits the free memory and would let the three ScreenProbes modules in together.
  • One job of a PSO at a time, which keeps after_running, delete this and the lifetime rules of Integrate post-story-load memory, queue and Vulkan fixes (RT-POST-STORY-RUNTIME) #1723 as they are.
  • A checkpoint starts only when no worker is in a build, and has_compile_waiter() now counts a build that is running on another worker.
  • A running checkpoint takes a worker too.
  • Large builds wait at the gate of d3d12: large pipelines compile side by side within a memory budget (RT-BIG-COMPUTE-BUDGET) #1889 inside their job, unchanged, and count as heavy while they wait.

It continues the idea of #1577 by madhyn (closed by its author) on the current lifetime model, with the priority-aware gate that #1577 was missing.

Beyond the claim

The claim (#1608, comment 7169077699) said "half the logical processors, at most 8". This sets a minimum of 2, so that 2 or 3 logical processors still leave a worker for demanded builds; LEONIDA_COMPILE_THREADS=1 gives the one thread. LEONIDA_COMPILE_HEAVY_MAX (one build of 1 MiB of SPIR-V or more at a time) was not in the claim: it was added after OreoFR's report of NVVM failing when the three ScreenProbes modules compile together.

Limits

  • The pool makes skips fewer and shorter. It does not remove them: a draw or dispatch is still skipped for as long as its own pipeline takes to compile, so the device loss above can still happen. Not tested in the game.
  • The very large limit is tested with made-up jobs only, not against the NVVM failure.
  • A long module still takes as long. With the limit of one, very large builds queue behind each other.
  • With several workers the pipelines become ready in an order that differs from run to run.
  • The driver holds about 50 MiB for each thread that compiles.
  • With N builds in flight, pipeline_cache.session.bin is more often skipped at device teardown (~Device does not write it while a build is active); the exit write of d3d12: write the session pipeline cache when the run ends with the device alive (RT-SESSION-CACHE-AT-EXIT) #1950 waits 1 s, then writes the pipelines finished so far. stop_for_exit may detach N threads instead of one when its wait runs out.
  • The checkpoint waits for an empty queue and for every worker to be out of its build (as on main, where one thread made these the same thing); d3d12: finish CompileActivity before checkpointing and retry deferred cache snapshots #1756 changes that.
  • The "heavy" threshold (compute over 128 KiB, 1 MiB or more for the rest) is a setting that can be tuned later: graphics pipelines between 128 KiB and 1 MiB are not heavy and can occupy all N workers.
  • LEONIDA_COMPILE_THREADS=1 on this branch stands for main in the measurements; main itself was not measured.

Credit: OreoFR for the in-game measurements, DadaCap for the mesh branch used in the traced runs, madhyn for the idea of #1577.

Tested

  • pipeline_compiler_pool (new, no GPU): the rules with made-up jobs. 17 mutants, each removing or weakening one rule (reserved worker, one job per PSO, checkpoint at rest, the very large limit, the wake-up, the settings), all fail it.
  • d3d12_compile_pool (new, GPU; default, 1 and 4 workers): a large module's build held in flight; with several workers 64 small pipelines are built and their dispatches store the right values meanwhile, with one worker none is.
  • d3d12_compile_teardown with the default, 1 and 4 workers: its Gate now holds every worker.
  • d3d12_big_compute_warmup is d3d12: large pipelines compile side by side within a memory budget (RT-BIG-COMPUTE-BUDGET) #1889's test (TheGeogeo), changed here because it assumed one warm-up thread and failed with the pool (once in ctest, then 3 of 6 runs): the warm-up threads are now the compiler's threads, taken from the job probe; "module 0 enters first" is checked only with one worker; and the "place is taken" check counts large creations only, since the warm-up's small ones may start on the other workers. The order check (the submission's four large builds between two of the warm-up's) is unchanged. If d3d12: large pipelines compile side by side within a memory budget (RT-BIG-COMPUTE-BUDGET) #1889 changes this test, this commit rebases on it.
  • ctest -R "compile|pipeline|big_compute|d3d12_compute|session", with main e18b59a5b merged in: 31 of 31 (before the pool, on d3d12: large pipelines compile side by side within a memory budget (RT-BIG-COMPUTE-BUDGET) #1889 alone with -R "compile|pipeline|big_compute|d3d12_compute", 24 of 25: big_compile_gate timed out on a loaded machine and passes alone). tests/test_docs_*.py: 50 passed.
  • Synthetic run, d3d12_compile_pool_test measure 4194304 200 <seed> (one 4 MiB module the driver takes 92 to 173 s over, then 200 modules of 12 KiB, two alternating runs each, on a machine that was building other things): the 200 are ready after 1.3 and 6.9 s with 6 workers, after 99 and 152 s with one thread. Peak commit 714 and 720 MiB against 645 and 681.

GPUs tested: NVIDIA GeForce RTX 4060, driver 610.88, Windows 11

AI-assisted: yes (Claude Code wrote the code, the tests and this text; the game measurements are OreoFR's).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)

🤖 Generated with Claude Code

TheGeogeo and others added 7 commits November 25, 2026 15:11
…re speculative (RT-BIG-COMPUTE-BUDGET)

LargeModuleGate moves to runtime/src (a forwarding header keeps vk_pipeline_check and its test as they are) and gets a second constructor, whose budget is read whenever a place is given or returned. Its modules get in in the order they came, a speculative one only while no demand waits and within a quarter of the budget. large_compile_budget_bytes() is three quarters of the smaller of the free RAM and the room under the commit limit. CPU test: the bound, one always, order, priority, every wake-up, the budget of the moment and the setting.
…T-BIG-COMPUTE-BUDGET)

Compute modules over 128 KiB and graphics pipelines of 1 MiB of SPIR-V or more wait at one gate for the process until their estimates fit the memory that is free, instead of one at a time. PipelineCompiles::build gives a submission as many lanes for them as can be in at once, largest first, and fills the rest with small ones. A thread that waits for its place holds no PSO's lock, and one that has a place waits for none. The warm-up's compiles are speculative. LEONIDA_SERIAL_BIG_COMPILES=1 keeps one at a time, 0 has no limit, LEONIDA_BIG_COMPILE_BUDGET_MB fixes the budget.
…his change)

Local merge of pull/1889/head (aa8018b) so that the async compile pool can build on its memory gate.
This commit is not part of the pull request: the pull request depends on ProjectLeonida#1889.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…POOL)

PipelineCompiler ran every LEONIDA_ASYNC_PIPELINES=1 build, the compute warm-up and the cache checkpoint on one
below-normal thread, so one long driver compile held every pipeline a submission asked for behind it, and their
draws and dispatches were skipped meanwhile.

- LEONIDA_COMPILE_THREADS workers: unset, half the logical processors, at least 2 and at most 8; 1 is the one
  thread there was, with the same order of jobs.
- At most N-1 workers in heavy builds (the warm-up's, and demanded builds of large modules), so one is left for
  a small build a submission asked for.
- One very large build (1 MiB of SPIR-V or more) at a time, demanded or not: LEONIDA_COMPILE_HEAVY_MAX.
- One job of a PSO at a time, which keeps after_running and the lifetime rules as they are.
- A checkpoint starts only when no worker is in a build; has_compile_waiter counts a build that is running.
- Large builds wait at the memory gate of ProjectLeonida#1889 inside their job, as before.

Tests: pipeline_compiler_pool (the rules, with made-up jobs, no GPU), d3d12_compile_pool (a GPU, the large build
held in flight) and d3d12_compile_teardown, the last two with the default, 1 and 4 workers. d3d12_big_compute's
warm-up case no longer assumes one warm-up thread.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…t heavy, not for any small one

Light builds are not limited, so graphics pipelines below the large size can
still take every worker at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@OreoFR

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Numbers from my side for why the pool matters on NVIDIA (RTX 4070 Laptop 8 GB, 621.40, retail, traced with LEONIDA_TRACE_PIPELINE_COMPILE=1):

  • Single large compute modules take 75-116 s each to compile here, for example lib_cs_01646 in 115 s and lib_cs_01631 in 116 s. Every run hit a different set of them at the world start: 17 distinct library compute modules over 60 s so far, between 3.2 and 5.9 MB of SPIR-V.
  • The translated library has 674 compute modules over 3.5 MB, plus three of about 13 MB. On one thread, building all 674 once would take over 16 hours. The game only asks for part of them per scene, but each one it asks for blocks everything behind it for 1.5-2 minutes.
  • The time is spent in compiles: with DadaCap's frame stats, compiles take about 55% of the world-start frame time on the current single worker.
  • Caches: they sit next to gta6rt.exe by default, so every new build dir started cold here. With d3d12: a pipeline cache for the offline cache's misses, written while the game runs (RT-HEAVY-PIPELINE-CACHE) #1914 (pipeline_cache.heavy.bin, written while the game runs) and LEONIDA_PIPELINE_CACHE_DIR pointing every build at one folder, a module is compiled once per machine: a second run reloaded 20 MB of heavy cache and did not rebuild any of the four slow builds the first run had done.

So pool + #1914 + one shared cache dir covers it: the first boot compiles side by side, and the boots after it pay nothing.

This branch (044d9ba) on the same machine: d3d12_compile_pool (default, one thread, four threads), d3d12_compile_teardown (default, one thread, four threads) and pipeline_compiler_pool all pass, 7 of 7. A retail boot with it follows.

Counts and program names only. AI-assisted (Claude Code).

@DadaCap

DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Three game runs with this PR (head 044d9ba), asked for on Discord. One run each.

Build: main db1a22d29 + our local tree (including #429, #1592, #1756, #1957, #1972) + this PR + #1914, same executable for all three. Machine: RTX 5080 16 GB, NVIDIA 621.24, Windows 11, 64 GB. LEONIDA_SERIAL_BIG_COMPILES=0, warm session cache (1.87 GB), mesh modules from #2102.

Run LEONIDA_ASYNC_PIPELINES LEONIDA_COMPILE_THREADS Draws / dispatches skipped Demand builds that missed the cache Result
m76 1 1 5,307 / 2,323 0 of 460 device lost, process ended 131 s after its start
m78 1 unset (full pool) 7,224 / 2,424 7 of 831 device lost, process ended 150 s after its start
m77 unset 1 none 10 of 992 no loss, stopped at 600 s by the time limit

What the two losses share: a READ_INVALID (type 1, precision 4096, pages 0x4a4a05000 and 0x4bdb7d000) and the same faulting instruction address, 0x200024e20 (type 6).

  • In m76 every demand build was a cache hit, so in that run a long background compile was not needed for the loss.
  • One compile thread lost the device as well as the full pool, so parallel builds were not needed either.
  • Not settled: whether the cause is the skipped draws and dispatches, or pipeline creation beside rendering. Building the mesh pipelines before the world removes both, so "it holds" would not separate them. Keeping the builds on the workers and making the draw wait instead of skip would.
  • The warm cache means this says little about the pool's own aim, shorter compile walls.
  • A sync run of our own tree without this PR (old mesh modules) also lost the device with a READ_INVALID, at 700 s, so there may be more than one cause.

AI-assisted (Claude Code, Claude Opus 5.5).

@DadaCap

DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Addition to my table above: a later sync run (m79, LEONIDA_ASYNC_PIPELINES unset, our tree without this PR and without #1914, same shaders) also lost the device, at about 230 s in the world, with a READ_INVALID (page 0x4b531e000) and the same faulting instruction address 0x200024e20 (plus 0x200025dd0). No draw or dispatch is skipped there. So that fault needs neither skips nor the pool; with one run each, the table does not show that async causes it, only that it came earlier in the two async runs. I have not mapped the instruction address to a program. AI-assisted (Claude Code, Claude Opus 5.5).

@MoonEyes

Copy link
Copy Markdown
Collaborator Author

In-game A/B on one binary, from my own box (RTX 4060 desktop, 12 logical processors, driver 610.88, Windows 11). Same executable (this branch at 044d9ba), two 300 s boots with scripts/windows-boot/boot.ps1, LEONIDA_ASYNC_PIPELINES=1, LEONIDA_DEVICE_TYPE=7, a fresh pipeline-cache folder per run. Only LEONIDA_COMPILE_THREADS differs:

LEONIDA_COMPILE_THREADS=1 default (6 workers here)
End ran its full time (307 s) ran its full time (303 s)
PresentX ok 2,660 2,656
session=1 reached 175 s 125 s
game=3 session=3 reached 190 s 140 s
Pipeline compiles finished (trace lines) 906 1,099
Builds that took 2 s or more 33 164
Threads that compiled pipelines 1 6
Device faults none none

So on this machine the two runs present the same number of frames, and the pool run reaches game=3 session=3 (the landing screen) about 50 s sooner (states are sampled every 5 s). Almost every compile in these runs is the warm-up's (1,093 of 1,099 in the pool run, 900 of 906 in the 1-thread run; 5 from draws and 1 from a dispatch in each), so this boot barely exercises the reserved lane for demanded builds.

Limits: one run of each, one machine. The 1-thread run went first, and a ctest run of another checkout was going during its first 190 s or so, not during the pool run, so part of the 50 s may be that load. An earlier pool run of the same executable, made before the 1-thread run and while a build was running, reached the same state at 145 s, so run order alone does not explain the gap; that run presented 2,340 frames, so frame counts move by about 12% between runs. The driver's shader cache was already warm from earlier boots. The graphics pipeline library (#1592) is not built here (0 fspx_gfx): the game's mesh pipeline streams (12,244 per run) are accepted but none shows up among the traced compiles, and every build of 2 s or more is a compute pipeline. Counts only, no logs.

AI-assisted: yes (Claude Code ran the boots and read the digests; I reviewed it)

@MoonEyes

Copy link
Copy Markdown
Collaborator Author

Closing this one. #2098 changed the same scheduler another way: a graphics worker, the compile helper and the compile gate. This branch is built on the single thread from before and conflicts with main in 12 places of d3d12_pipeline_compile.h.

My later runs did not show the 50 s gain again: run-to-run noise on my machine (RTX 4060) is about 50 s. As I read the compile gate, more builds at once also means a submission waits for the longest of them.

If demanded builds still queue on main with the helper on, I will claim a small follow-up on top of #2098. Thanks to everyone who tested this.

AI-assisted: yes (Claude Code, Claude Fable 5.2, read both trees and wrote this)

@MoonEyes MoonEyes closed this Nov 26, 2026
aslan2142 pushed a commit that referenced this pull request Nov 27, 2026
…COMPILE-POOL)

With LEONIDA_ASYNC_PIPELINES=1 every demanded build, the compute warm-up and
the cache checkpoint ran on one general worker (plus a graphics-only one), so
one long driver compile held every pipeline a submission asked for behind it,
and their draws and dispatches were skipped meanwhile.

PipelineCompiler now runs LEONIDA_COMPILE_THREADS workers (half the logical
processors, 2 to 8). A worker takes the most urgent job it may:
- demanded builds first, in the order they came, then warm-ups by size, then
  the checkpoint, which waits until no worker compiles;
- large builds (compute over 128 KiB, graphics of 1 MiB or more) and warm-ups
  together take at most threads-1 workers, so one stays free for a small
  demanded build; at most LEONIDA_COMPILE_HEAVY_MAX large builds at once
  (default threads-1, 1 with LEONIDA_SERIAL_BIG_COMPILES=1), compute and
  graphics alike, each still within the memory gate;
- warm-ups run one at a time, and none starts while a demanded build runs:
  each holds the compile gate for its whole create, and a submission waits
  for every create in flight;
- one build of a PSO at a time, so after_running and the teardown rules hold.

LEONIDA_COMPILE_THREADS=1 keeps the layout before the pool. The design is
MoonEyes' (#2077), ported onto #2098's scheduler.

Tests: d3d12_compile_pool (CPU, jobs held at gates: settings, a long build
does not hold up a small demanded one, heavy count, free worker, warm-ups one
at a time and after demanded builds, order, one job per PSO, release/cancel/
stop_for_exit with jobs in flight), and d3d12_compile_teardown also with one
worker.

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

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants