Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2956,13 +2956,16 @@ submitting thread; at the maintainers' decision it was replaced by this one when
targets give (`request_pipeline_build`). A compute build the compile helper is still making (`LEONIDA_COMPILE_HELPER`)
publishes nothing, so its next use asks again.
- **Compile workers** (RT-ASYNC-COMPILE-POOL, after MoonEyes' design in #2077). The jobs above, the compute warm-up
and the cache checkpoint run on `LEONIDA_COMPILE_THREADS` workers (half the logical processors, 2 to 8). With one
and the cache checkpoint run on `LEONIDA_COMPILE_THREADS` workers (half the processors in the process's affinity mask, 2 to 8). With one
worker a driver compile of minutes held every pipeline a submission asked for behind it, and their draws and
dispatches were skipped meanwhile. A worker now takes the most urgent job it may: demanded builds first, in the
order they came, then warm-ups by module 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 is always free for a small demanded build; at most `LEONIDA_COMPILE_HEAVY_MAX` large builds run at once (default
threads-1, 1 with `LEONIDA_SERIAL_BIG_COMPILES=1`), each still within the memory gate; warm-ups run one at a time,
2, the most the general and graphics-only workers could run before the pool, 1 with two workers or
`LEONIDA_SERIAL_BIG_COMPILES=1`), each still within the memory gate. The default was threads-1: with 8 workers
under Wine the story load stalled at about 5,100 submissions until the title's 300 s guard fired, while one worker
lived (z3r0rc, #1608), and native boots showed the same knee with troughs of 15-60 s; warm-ups run one at a time,
as before, and none starts while a demanded build is queued or running, since each holds the compile gate for its
whole create (MoonEyes measured in #1608 that with the gate on, submissions waited 142 s in all behind warm-ups of
about 800 KB, and that with `LEONIDA_COMPILE_GATE=0` the same boot reached game state 3); and a PSO has one build
Expand Down
4 changes: 2 additions & 2 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 with the adaptive gate (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | `<ms>`: the adaptive gate's probe budget. Until it knows whether the driver holds a submission behind a create, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its own included, add up to this; a single wait reaches it at 2.5 s. On NVIDIA 621.40 waits of 3-7 s, none near the 15 s limit, came back to back and kept the title's main loop from reporting for 30 s; on an RTX 4060 with the watchdog kept kicked, 459 waits of up to 2.9 s took 155 s by t=202 s. The cost on a driver that does hold submissions (630.20): the first probe enters the driver after 2.5 s of waiting and loses the device if the create stays 10 s more; the held verdict then stops the early probes. `0`: probes only at `LEONIDA_COMPILE_GATE_WAIT_MS`. Used only by the adaptive gate. |
| `LEONIDA_COMPILE_GATE_RECALIBRATE` | unset | `1`: with `LEONIDA_COMPILE_GATE` unset or `auto`, the device measures the driver again instead of reading the verdict kept in `compile_gate_calibration.txt`, and writes the new one there (after a driver update the key changes and it measures anyway). |
| `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. |
| `LEONIDA_COMPILE_THREADS` | half the logical processors, 2-8 | `<n>`: the compile workers of the asynchronous builds (`LEONIDA_ASYNC_PIPELINES=1`), the compute warm-up and the cache checkpoint (`shims/d3d12_pipeline_compile.h`, `CompilePool`). A demanded build goes before warm-ups; large and speculative builds take at most n-1 workers, so one stays free for a small build a submission waits for; one build of a PSO at a time; warm-ups one at a time, and none while a demanded build is queued or running. `1`: one general worker plus the graphics-only one (`LEONIDA_GRAPHICS_COMPILE_WORKER`), the layout before the pool. |
| `LEONIDA_COMPILE_HEAVY_MAX` | threads-1 (`1` with `LEONIDA_SERIAL_BIG_COMPILES=1`) | `<n>`: at most n builds of large modules (compute over 128 KiB, graphics of 1 MiB of SPIR-V or more) on the compile workers at once, 1 to threads-1. They also keep within the memory budget of `LEONIDA_BIG_COMPILE_BUDGET_MB`. |
| `LEONIDA_COMPILE_THREADS` | half the processors in the process's affinity mask, 2-8 | `<n>`: the compile workers of the asynchronous builds (`LEONIDA_ASYNC_PIPELINES=1`), the compute warm-up and the cache checkpoint (`shims/d3d12_pipeline_compile.h`, `CompilePool`). A demanded build goes before warm-ups; large and speculative builds take at most n-1 workers, so one stays free for a small build a submission waits for; one build of a PSO at a time; warm-ups one at a time, and none while a demanded build is queued or running. `1`: one general worker plus the graphics-only one (`LEONIDA_GRAPHICS_COMPILE_WORKER`), the layout before the pool. |
| `LEONIDA_COMPILE_HEAVY_MAX` | 2 (`1` with two workers or `LEONIDA_SERIAL_BIG_COMPILES=1`) | `<n>`: at most n builds of large modules (compute over 128 KiB, graphics of 1 MiB of SPIR-V or more) on the compile workers at once, 1 to threads-1. They also keep within the memory budget of `LEONIDA_BIG_COMPILE_BUDGET_MB`. |
| `LEONIDA_GRAPHICS_COMPILE_WORKER` | on | With `LEONIDA_COMPILE_THREADS=1`: a second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. With more workers it is not used. |
| `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. |
| `LEONIDA_CONSOLE_HEAP_ARENA` | unset | With `LEONIDA_CONSOLE_HEAP_GPU=1`, `1` puts a console heap's GPU copy and its guard in the arena (device-local where the device has ReBAR) instead of an import of host memory. No room in the arena: the import. |
Expand Down
41 changes: 33 additions & 8 deletions runtime/shims/d3d12_pipeline_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,23 @@ class DriverTurn {


// How many compile workers PipelineCompiler runs, and how many of them may build a large module at once.
// LEONIDA_COMPILE_THREADS=<n>: n workers (1..64). Unset: half the logical processors, 2 at least and 8 at most. 1: one
// worker for every job plus the graphics-only one (LEONIDA_GRAPHICS_COMPILE_WORKER), the layout before the pool.
// LEONIDA_COMPILE_THREADS=<n>: n workers (1..64). Unset: half the processors this process may run on (its affinity
// mask, not the machine's count), 2 at least and 8 at most. 1: one worker for every job plus the graphics-only one
// (LEONIDA_GRAPHICS_COMPILE_WORKER), the layout before the pool.
// LEONIDA_COMPILE_HEAVY_MAX=<n>: at most n large builds (compute modules over 128 KiB, graphics pipelines of
// PipelineCompiles::large_spirv_bytes or more) at once, 1..threads-1. Unset: threads-1, or 1 with
// LEONIDA_SERIAL_BIG_COMPILES=1. Large builds still take their place at the memory gate (big_compile_gate) too.
// PipelineCompiles::large_spirv_bytes or more) at once, 1..threads-1. Unset: 2 (1 with two workers), the most the
// general and graphics-only workers could run before the pool, or 1 with LEONIDA_SERIAL_BIG_COMPILES=1. Large builds
// still take their place at the memory gate (big_compile_gate) too. The default was threads-1 (7 with 8 workers):
// under Wine, the story load then stalled at about 5,100 submissions until the title's 300 s guard fired, and one
// worker lived (z3r0rc, #1608); native Windows boots showed the same knee, with troughs of 15-60 s.
// With two workers or more, large and speculative builds (warm-ups) together take at most threads-1 of them, so one
// worker is always free for a small build a submission waits for. Warm-ups run one at a time, as on the single worker,
// and none starts while a demanded build is queued or running: each holds the compile gate for its whole create, and a
// submission waits there for every create in flight (MoonEyes, #1608: with the gate on, submissions waited 142 s in all
// behind warm-ups of about 800 KB, 5-13.5 s each).
// The default heavy count (CompilePool::heavy without LEONIDA_COMPILE_HEAVY_MAX).
inline constexpr unsigned kDefaultHeavyCompiles = 2;

struct CompilePool {
unsigned threads = 1;
unsigned heavy = 1; // large builds at once
Expand Down Expand Up @@ -275,14 +282,31 @@ inline CompilePool compile_pool(const wchar_t* threads_text, const wchar_t* heav
else pool.threads_valid = false;
}
const unsigned most = pool.threads > 1 ? pool.threads - 1 : 1;
pool.heavy = serial_big ? 1 : most;
pool.heavy = serial_big ? 1 : (std::min)(kDefaultHeavyCompiles, most);
if (heavy_text && *heavy_text) {
if (parse_compile_count(heavy_text, value)) pool.heavy = std::clamp(value, 1u, most);
else pool.heavy_valid = false;
}
return pool;
}

// The processors in an affinity mask; `fallback` (the machine's count) when the mask is empty, as when it could not be
// read.
inline unsigned affinity_processors(std::uint64_t mask, unsigned fallback) {
unsigned count = 0;
for (; mask; mask &= mask - 1) ++count;
return count ? count : fallback;
}

// The processors this process may run on: a process limited to some of them (start /affinity, a job object, or a
// container's CPU set as Wine reports it) sizes the pool from those, not from the machine's count.
inline unsigned process_processors() {
DWORD_PTR process = 0, system = 0;
const unsigned machine = std::thread::hardware_concurrency();
if (!GetProcessAffinityMask(GetCurrentProcess(), &process, &system)) return machine;
return affinity_processors(static_cast<std::uint64_t>(process), machine);
}

inline CompilePool compile_pool_from_environment() {
wchar_t threads[16]{}, heavy[16]{}, serial[2]{};
const DWORD t = GetEnvironmentVariableW(L"LEONIDA_COMPILE_THREADS", threads, 16);
Expand All @@ -292,7 +316,7 @@ inline CompilePool compile_pool_from_environment() {
if (h >= 16) heavy[0] = L'x';
const bool serial_big = s == 1 && serial[0] != L'0';
const CompilePool pool = compile_pool(t ? threads : nullptr, h ? heavy : nullptr,
std::thread::hardware_concurrency(), serial_big);
process_processors(), serial_big);
if (!pool.threads_valid) std::fprintf(stderr, "[rt] d3d12: LEONIDA_COMPILE_THREADS is not a number; not used\n");
if (!pool.heavy_valid) std::fprintf(stderr, "[rt] d3d12: LEONIDA_COMPILE_HEAVY_MAX is not a number; not used\n");
return pool;
Expand Down Expand Up @@ -367,8 +391,9 @@ class PipelineCompiler {
pool_ = compile_pool_from_environment();
if (pool_->threads > 1)
std::fprintf(stderr, "[rt] d3d12: %u pipeline compile workers, at most %u large builds at once "
"(LEONIDA_COMPILE_THREADS, LEONIDA_COMPILE_HEAVY_MAX)\n",
pool_->threads, pool_->heavy);
"(LEONIDA_COMPILE_THREADS, LEONIDA_COMPILE_HEAVY_MAX; %u processors for this "
"process)\n",
pool_->threads, pool_->heavy, process_processors());
}
stats_.threads = pool_->threads;
stats_.heavy = pool_->heavy;
Expand Down
19 changes: 17 additions & 2 deletions runtime/test/d3d12_compile_pool_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ void submit(PipelineCompiler& pool, Log& log, Gate* gate, const void* owner, con
}

void test_pool_settings() {
// Large builds: 2 at once by default, the most before the pool (7 at once stalled a story load under Wine, #1608).
auto pool = rt::d3d12::compile_pool(nullptr, nullptr, 16, false);
EXPECT(pool.threads == 8 && pool.heavy == 7);
EXPECT(pool.threads == 8 && pool.heavy == 2);
pool = rt::d3d12::compile_pool(nullptr, nullptr, 32, false);
EXPECT(pool.threads == 8);
pool = rt::d3d12::compile_pool(nullptr, nullptr, 6, false);
Expand All @@ -150,7 +151,21 @@ void test_pool_settings() {
pool = rt::d3d12::compile_pool(L"6", L"3", 16, true); // an explicit count wins
EXPECT(pool.heavy == 3);
pool = rt::d3d12::compile_pool(L"six", L"x", 16, false);
EXPECT(!pool.threads_valid && !pool.heavy_valid && pool.threads == 8 && pool.heavy == 7);
EXPECT(!pool.threads_valid && !pool.heavy_valid && pool.threads == 8 && pool.heavy == 2);
pool = rt::d3d12::compile_pool(L"8", L"7", 16, false); // the old default, asked for
EXPECT(pool.threads == 8 && pool.heavy == 7);
pool = rt::d3d12::compile_pool(L"8", nullptr, 16, true);
EXPECT(pool.heavy == 1);
// The pool follows the processors the process may run on, not the machine's count.
EXPECT(rt::d3d12::affinity_processors(0xFFFFull, 32) == 16);
EXPECT(rt::d3d12::affinity_processors(0x0Full, 32) == 4);
EXPECT(rt::d3d12::affinity_processors(~0ull, 128) == 64);
EXPECT(rt::d3d12::affinity_processors(0, 12) == 12); // not read: the machine's count
pool = rt::d3d12::compile_pool(nullptr, nullptr, rt::d3d12::affinity_processors(0x0Full, 32), false);
EXPECT(pool.threads == 2 && pool.heavy == 1); // four processors of 32: two workers, not eight
pool = rt::d3d12::compile_pool(nullptr, nullptr, rt::d3d12::affinity_processors(0x3Full, 32), false);
EXPECT(pool.threads == 3 && pool.heavy == 2);
EXPECT(rt::d3d12::process_processors() >= 1);
}

void test_long_build_does_not_hold_up_a_small_demanded_one() {
Expand Down
Loading