From e4713a4408e7a64e1fcc0d0119d3731f82995c40 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Thu, 26 Nov 2026 06:06:16 -0600 Subject: [PATCH 1/2] d3d12: warm-up creates no longer hold the compile gate (RT-COMPILE-GATE-WARMUP-FREE) A queue submission waited at the compile gate for every pipeline create in the driver, warm-ups included. On a cold boot (HasiKe, RTX 2070 SUPER, #2233) the title crawled at about one submission a second behind 290 s of serial warm-ups; with the gate off it ran at about 100 frames a second. A compile worker now marks its speculative jobs (warm-ups, prewarms, the checkpoint) with the thread-local compile_job_speculative, and DriverTurn passes that to SubmitMutex::CompileTurn. A submission waits only for the demanded creates in the driver. A warm-up still never enters while a submission is in the driver or waiting at the gate, and at most LEONIDA_COMPILE_GATE_WARMUP_MAX (default 1) warm-ups are in the driver at once, so the creates beside a submission stay bounded. LEONIDA_COMPILE_GATE_WARMUPS=1 restores the old behaviour; LEONIDA_COMPILE_GATE=0 still turns the gate off. Tests: submit_mutex_test gets warmups_do_not_hold_the_gate (a submission passes a warm-up, still waits for a demanded create, the warm-up cap, no warm-up during a submission, the old policy, the settings); d3d12_compile_pool_test checks which jobs run as speculative. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 15 ++- runtime/README.md | 2 + runtime/shims/d3d12_pipeline_compile.h | 11 +- runtime/src/vulkan_device_fault.h | 69 ++++++++-- runtime/test/d3d12_compile_pool_test.cpp | 37 +++++ runtime/test/submit_mutex_test.cpp | 165 ++++++++++++++++++++++- 6 files changed, 283 insertions(+), 16 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index a14e10f36f..7e4b7382c4 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2619,8 +2619,19 @@ submitting thread; at the maintainers' decision it was replaced by this one when about 800 KB, and that with `LEONIDA_COMPILE_GATE=0` the same boot reached game state 3); and a PSO has one build running at a time, so `after_running` and the teardown rules hold as they were. `LEONIDA_COMPILE_THREADS=1` is the layout before the pool: - one general worker and the graphics-only one. A submission still waits at the compile gate for every create in - flight, so more workers do not shorten that wait (`SubmitMutex::CompileTurn`; `LEONIDA_COMPILE_GATE=0` turns it off). + one general worker and the graphics-only one. +- **Warm-ups do not hold the compile gate** (RT-COMPILE-GATE-WARMUP-FREE). A submission waits at the compile gate + (`SubmitMutex::CompileTurn`) only for the demanded creates in the driver, not for a warm-up's: a create made by a + compile worker's speculative job (a warm-up, a prewarm, the checkpoint; `compile_job_speculative`) passes the gate + as speculative. Its pipeline is simply not ready yet; whoever needs it builds or waits for it as before. A warm-up + still never enters the driver while a submission is in it or waits at the gate, and at most + `LEONIDA_COMPILE_GATE_WARMUP_MAX` warm-ups (default 1) are in the driver at once, so the creates beside a submission + stay bounded: the pool's demanded builds plus that many warm-ups, and the pool starts no warm-up while a demanded + build runs. Why: on HasiKe's RTX 2070 SUPER (#2233) a cold boot crawled at about one submission a second behind + 290 s of serial warm-ups with the gate on, and ran at about 100 frames a second with `LEONIDA_COMPILE_GATE=0`; + MoonEyes measured 142 s of submission waits behind warm-ups (#1608). The gate stays for demanded creates, which the + title waits for anyway. `LEONIDA_COMPILE_GATE_WARMUPS=1` makes warm-ups hold the gate again, as before; + `LEONIDA_COMPILE_GATE=0` still turns the whole gate off. - **Warm-ups while the title submits.** A compute warm-up holds the compile gate for its whole create, and every submission waits behind it: a native boot's submissions waited 140 s in all at the gate behind compute warm-ups, and past the story load the worker went through 694 warm-ups of modules over 128 KiB (those over 640 KiB take 4-25 s diff --git a/runtime/README.md b/runtime/README.md index ffd166f7d4..31ff126b93 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -416,6 +416,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_SUBMIT_ASYNC` | `0` | ``: each queue's `ExecuteCommandLists`, `Signal`, `Wait` and markers run in call order on a worker of the queue's own (`src/vulkan_backend.h`), at most n batches queued; the caller of the next one waits for room. The replay, the before-submission refreshes and `vkQueueSubmit` then leave the title's thread. A batch that fails is reported as a held one is. `0`: on the caller's thread. | | `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | ``: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. | | `LEONIDA_COMPILE_GATE` | unset | `1`: a compute pipeline build holds the device's submit lock for its driver calls, so no compute compile runs while a queue submits. Unset: the asynchronous workers compile while other queues submit. | +| `LEONIDA_COMPILE_GATE_WARMUPS` | unset (warm-ups free) | A submission waits at the compile gate (`SubmitMutex::CompileTurn`, off with `LEONIDA_COMPILE_GATE=0`) only for demanded pipeline creates; a compile worker's warm-up, prewarm or checkpoint create does not hold it, though it still never enters the driver while a submission is in it or waits. `1`: warm-ups hold the gate as demanded creates do, the behaviour before. See `docs/d3d12_vulkan.md`, "Warm-ups do not hold the compile gate". | +| `LEONIDA_COMPILE_GATE_WARMUP_MAX` | `1` | `` (1-64): at most n warm-up creates in the driver at once while warm-ups do not hold the compile gate. | | `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 | ``: 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`) | ``: 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`. | diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index d1bf35b0cb..04e76763b5 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -138,9 +138,14 @@ inline bool compile_worker_trigger(const char* trigger) { // behind it go first, and does so before its vkCreateShaderModule and vkDestroyShaderModule too; a title thread // waits only for the holder, and only before a pipeline create (it may wait out a vkDeviceWaitIdle or a present: // waits of 50 ms or more are logged). -// - a pipeline create passes the compile gate (SubmitMutex::CompileTurn) for the length of the call. +// - a pipeline create passes the compile gate (SubmitMutex::CompileTurn) for the length of the call; as speculative, +// not holding submissions, when a compile worker runs it for a speculative job (compile_job_speculative). // - the compile worker, which runs below normal priority, runs the call at normal priority, so a preempted worker // does not sit on a driver lock a submit waits for. +// True on a compile worker while it runs a speculative job (a warm-up, a prewarm, the checkpoint; PipelineCompiler::run): +// its pipeline creates pass the compile gate as warm-ups, which do not hold submissions (SubmitMutex::CompileTurn). +inline thread_local bool compile_job_speculative = false; + class DriverTurn { public: DriverTurn(leonida::SubmitMutex& submits, const char* trigger, const char* call, bool pipeline, @@ -160,7 +165,7 @@ class DriverTurn { if (waited >= 50) note("waited for the submit lock", trigger, call, name, waited); } if (pipeline) { - gate_.emplace(submits); + gate_.emplace(submits, compile_job_speculative); if (gate_->waited_ms() >= 50) note("waited at the compile gate", trigger, call, name, gate_->waited_ms()); } } @@ -570,12 +575,14 @@ class PipelineCompiler { stats_.most_running = (std::max)(stats_.most_running, running_.size()); const auto probe = probe_; lock.unlock(); + compile_job_speculative = speculative; // its creates do not hold submissions (DriverTurn) try { if (probe) probe(job.owner); job.work(); } catch (...) { std::fprintf(stderr, "[rt] pipeline compile worker: a job threw\n"); } + compile_job_speculative = false; job.work = nullptr; // its captures go before the job counts as done lock.lock(); ++stats_.completed; diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index b3af188d13..77e0da7fa6 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -399,16 +399,25 @@ class SubmitMutex { // the driver. A long compile then stalls the submitting thread here, in our code, instead of in the driver. // No deadlock: a create holds the gate only across its vkCreate*Pipelines and a submission only across its // vkQueueSubmit, and neither driver call waits for anything of ours. LEONIDA_COMPILE_GATE=0 turns it off. + // A speculative create (a warm-up: nobody waits for its pipeline yet) does not hold submissions: they wait only for + // the demanded creates in the driver. It still never enters while a submission is in the driver or waits there, and + // at most warmup_max of them are in the driver at once, so the creates beside a submission stay bounded (HasiKe and + // MoonEyes, #2233 / #1608: a cold boot crawled at about one submission a second behind 290 s of warm-ups). + // LEONIDA_COMPILE_GATE_WARMUPS=1: warm-ups hold the gate as demanded creates do (the behaviour before). class CompileTurn { // around vkCreate*Pipelines public: - explicit CompileTurn(SubmitMutex& gate) : gate_(gate), waited_(gate.enter_compile()) {} - ~CompileTurn() { gate_.leave_compile(); } + explicit CompileTurn(SubmitMutex& gate, bool speculative = false) + : gate_(gate), speculative_(speculative && gate.warmups_free()), waited_(gate.enter_compile(speculative_)) {} + ~CompileTurn() { gate_.leave_compile(speculative_); } CompileTurn(const CompileTurn&) = delete; CompileTurn& operator=(const CompileTurn&) = delete; std::int64_t waited_ms() const { return waited_; } + bool speculative() const { return speculative_; } + private: SubmitMutex& gate_; + bool speculative_; std::int64_t waited_; }; class SubmitTurn { // around vkQueueSubmit, with the submit lock held @@ -430,6 +439,38 @@ class SubmitMutex { return on; } + // Whether warm-ups pass the gate without holding submissions, and how many may be in the driver at once. + // LEONIDA_COMPILE_GATE_WARMUPS=1: they hold it as demanded creates do. LEONIDA_COMPILE_GATE_WARMUP_MAX=: n at + // once (default 1). Read once; a test sets them per gate (set_warmup_policy). + struct WarmupPolicy { + bool free = true; + unsigned max = 1; + }; + static WarmupPolicy warmup_policy_from_environment() { + WarmupPolicy policy; + const char* hold = std::getenv("LEONIDA_COMPILE_GATE_WARMUPS"); + policy.free = !(hold && !std::strcmp(hold, "1")); + if (const char* max = std::getenv("LEONIDA_COMPILE_GATE_WARMUP_MAX")) { + char* end = nullptr; + const unsigned long n = std::strtoul(max, &end, 10); + if (end != max && !*end && n >= 1 && n <= 64) policy.max = unsigned(n); + } + return policy; + } + void set_warmup_policy(WarmupPolicy policy) { + std::lock_guard lock(gate_mutex_); + policy_ = policy; + } + bool warmups_free() { + std::lock_guard lock(gate_mutex_); + return policy_.free; + } + // Speculative creates in the driver now (they are not counted in what a submission waits for). + unsigned speculative_compiles() { + std::lock_guard lock(gate_mutex_); + return speculative_; + } + // The thread waiting in enter_submit for creates to leave the driver, or 0. std::uint32_t gate_waiter() const { return gate_waiter_.load(std::memory_order_relaxed); } @@ -444,25 +485,33 @@ class SubmitMutex { return stats_; } - std::int64_t enter_compile() { + // `speculative`: a warm-up that does not hold submissions (CompileTurn); it also waits for a free place among the + // warmup_max speculative ones. + std::int64_t enter_compile(bool speculative = false) { if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); std::int64_t waited = 0; - if (submits_) { + const auto blocked = [&] { return submits_ != 0 || (speculative && speculative_ >= policy_.max); }; + if (blocked()) { const std::int64_t start = diagnostic_ms(); - gate_changed_.wait(lock, [&] { return submits_ == 0; }); + gate_changed_.wait(lock, [&] { return !blocked(); }); waited = diagnostic_ms() - start; ++stats_.creates; stats_.create_ms += waited; stats_.create_max_ms = (std::max)(stats_.create_max_ms, waited); } - ++compiles_; + ++(speculative ? speculative_ : compiles_); return waited; } - void leave_compile() { + void leave_compile(bool speculative = false) { if (!compile_gate_enabled()) return; std::lock_guard lock(gate_mutex_); - if (--compiles_ == 0 && submits_) gate_changed_.notify_all(); + if (speculative) { + --speculative_; + gate_changed_.notify_all(); // a warm-up waiting for a place + } else if (--compiles_ == 0 && submits_) { + gate_changed_.notify_all(); + } } std::int64_t enter_submit() { @@ -548,7 +597,9 @@ class SubmitMutex { std::mutex gate_mutex_; // leaf: guards the gate counts and stats std::condition_variable gate_changed_; - std::uint32_t compiles_ = 0; // creates in the driver + std::uint32_t compiles_ = 0; // demanded creates in the driver: what a submission waits for + unsigned speculative_ = 0; // warm-up creates in the driver, while warm-ups are free (policy_) + WarmupPolicy policy_ = warmup_policy_from_environment(); std::uint32_t submits_ = 0; // submissions waiting for them or in the driver std::atomic gate_waiter_{0}; GateStats stats_; diff --git a/runtime/test/d3d12_compile_pool_test.cpp b/runtime/test/d3d12_compile_pool_test.cpp index 752eb438a1..302e60b9c2 100644 --- a/runtime/test/d3d12_compile_pool_test.cpp +++ b/runtime/test/d3d12_compile_pool_test.cpp @@ -6,6 +6,7 @@ // - demanded jobs go first, in the order they came, then warm-ups by size, and a checkpoint only when nothing runs; // - a checkpoint's second look sees a demanded build that started on another worker meanwhile; // - one job of a PSO at a time; +// - a warm-up's and the checkpoint's creates pass the compile gate as speculative, a demanded build's do not; // - the last release with jobs running and queued: the queued ones are dropped, the running ones finish, the // workers are joined; a device's cancel waits for its running jobs; stop_for_exit does not wait past its limit. #include "../shims/d3d12_pipeline_compile.h" @@ -18,6 +19,7 @@ #include #include #include +#include #include #include @@ -351,6 +353,40 @@ void test_a_checkpoint_sees_a_demand_that_started_during_it() { pool.release(); } +// What a job's pipeline creates tell the compile gate (DriverTurn, compile_job_speculative): a warm-up and the checkpoint +// are speculative and do not hold submissions; a demanded build is not, also on a worker that ran a warm-up before. +void test_jobs_tell_the_gate_whether_they_are_speculative() { + for (const unsigned threads : {1u, 3u}) { + auto& pool = make_pool(threads, threads > 1 ? threads - 1 : 1); + Log log; + std::mutex mutex; + std::vector> seen; + Owners owner; + const auto job = [&](const void* who, const std::string& name, std::size_t priority) { + EXPECT(pool.submit(who, device, priority, [&, name] { + std::lock_guard lock(mutex); + seen.emplace_back(name, rt::d3d12::compile_job_speculative); + log.start(name, false); + log.finish(name, false); + })); + }; + job(owner[0], "warm", 100); + EXPECT(log.wait_finished(1)); + job(owner[1], "demand", 0); + EXPECT(log.wait_finished(2)); + job(device, "checkpoint", SIZE_MAX); + EXPECT(log.wait_finished(3)); + job(owner[2], "demand again", 0); + EXPECT(log.wait_finished(4)); + EXPECT(!rt::d3d12::compile_job_speculative); // this thread is no worker + std::lock_guard lock(mutex); + const std::vector> want{ + {"warm", true}, {"demand", false}, {"checkpoint", true}, {"demand again", false}}; + EXPECT(seen == want); + pool.release(); + } +} + void test_one_job_of_a_pso_at_a_time() { auto& pool = make_pool(4, 3); Log log; @@ -486,6 +522,7 @@ int main() { test_order(); test_a_checkpoint_sees_a_demand_that_started_during_it(); test_one_job_of_a_pso_at_a_time(); + test_jobs_tell_the_gate_whether_they_are_speculative(); test_a_finished_job_wakes_the_idle_workers(); test_release_with_jobs_running_and_queued(); test_cancel_waits_for_a_devices_running_jobs(); diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 345f308871..6772eb6730 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -1,7 +1,7 @@ // leonida::SubmitMutex (src/vulkan_device_fault.h), on the CPU: the submit lock's owner and hold tracking, its waiter // count, let_submit_finish (the handoff a pipeline create makes to submits before entering the driver) and the -// compile gate between pipeline creates and queue submissions. Windows only: the owner is a Win32 thread id. No GPU, -// no game data. +// compile gate between pipeline creates and queue submissions, with warm-up creates that do not hold submissions +// (RT-COMPILE-GATE-WARMUP-FREE). Windows only: the owner is a Win32 thread id. No GPU, no game data. #include "../src/vulkan_device_fault.h" #include @@ -9,8 +9,10 @@ #include #include #include +#include #include #include +#include #define EXPECT(condition) do { \ if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ @@ -18,6 +20,12 @@ namespace { +// For checks inside threads, where EXPECT's return would leave the thread, not the test. +std::atomic failed_void{false}; +#define EXPECT_VOID(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); failed_void = true; } \ +} while (0) + using leonida::SubmitMutex; using namespace std::chrono_literals; @@ -197,12 +205,163 @@ int compile_gate() { return 0; } +// Warm-up creates (CompileTurn speculative) do not hold submissions; demanded ones still do; at most warmup_max warm-ups +// are in the driver at once; none enters while a submission waits or is in the driver; with warm-ups held +// (LEONIDA_COMPILE_GATE_WARMUPS=1) a submission waits for them as before. +int warmups_do_not_hold_the_gate() { + if (!SubmitMutex::compile_gate_enabled()) { + std::printf("warm-up gate: compile gate off (LEONIDA_COMPILE_GATE=0): not checked\n"); + return 0; + } + SubmitMutex m; + m.set_warmup_policy({true, 1}); + // A submission is not held by a warm-up in the driver. + { + std::atomic in{false}, release{false}; + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(m, true); + in = true; + while (!release) std::this_thread::sleep_for(1ms); + }); + EXPECT(eventually([&] { return in.load(); })); + EXPECT(m.speculative_compiles() == 1); + const std::int64_t start = now_ms(); + { + const SubmitMutex::SubmitTurn turn(m); + } + EXPECT(now_ms() - start < 50); + release = true; + warm.join(); + EXPECT(m.speculative_compiles() == 0); + } + // A demanded create is still waited for, also beside a warm-up: the submission waits for the demanded one only. + { + std::atomic warm_in{false}, demand_in{false}, release_warm{false}; + std::atomic demand_left{0}, submitted{0}; + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(m, true); + warm_in = true; + while (!release_warm) std::this_thread::sleep_for(1ms); + }); + std::thread demand([&] { + const SubmitMutex::CompileTurn turn(m); + demand_in = true; + std::this_thread::sleep_for(200ms); + demand_left = now_ms(); + }); + EXPECT(eventually([&] { return warm_in.load() && demand_in.load(); })); + std::thread submit([&] { + const SubmitMutex::SubmitTurn turn(m); + submitted = now_ms(); + }); + submit.join(); + demand.join(); + EXPECT(submitted.load() >= demand_left.load()); // waited for the demanded create + EXPECT(warm_in.load() && !release_warm.load()); // and not for the warm-up, still in the driver + release_warm = true; + warm.join(); + } + // At most warmup_max warm-ups in the driver; a demanded create does not wait for them. + for (const unsigned max : {1u, 2u}) { + m.set_warmup_policy({true, max}); + std::atomic inside{0}, most{0}; + std::atomic release{false}; + std::vector warms; + for (int i = 0; i < 4; ++i) + warms.emplace_back([&] { + const SubmitMutex::CompileTurn turn(m, true); + const int now = ++inside; + int seen = most.load(); + while (now > seen && !most.compare_exchange_weak(seen, now)) { + } + while (!release) std::this_thread::sleep_for(1ms); + --inside; + }); + EXPECT(eventually([&] { return inside.load() == int(max); })); + std::this_thread::sleep_for(100ms); + EXPECT(inside.load() == int(max)); // the others wait for a place + std::atomic demand_waited{-1}; + std::thread demand([&] { + const SubmitMutex::CompileTurn turn(m); + demand_waited = turn.waited_ms(); + }); + demand.join(); + EXPECT(demand_waited.load() == 0); + release = true; + for (auto& t : warms) t.join(); + EXPECT(most.load() == int(max)); + EXPECT(m.speculative_compiles() == 0); + } + m.set_warmup_policy({true, 1}); + // A warm-up never enters while a submission is in the driver. + { + std::atomic submitting{false}, release{false}; + std::atomic submit_left{0}, warm_entered{0}; + std::thread submit([&] { + const SubmitMutex::SubmitTurn turn(m); + submitting = true; + while (!release) std::this_thread::sleep_for(1ms); + submit_left = now_ms(); + }); + EXPECT(eventually([&] { return submitting.load(); })); + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(m, true); + warm_entered = now_ms(); + }); + std::this_thread::sleep_for(100ms); + EXPECT(warm_entered.load() == 0); + release = true; + submit.join(); + warm.join(); + EXPECT(warm_entered.load() >= submit_left.load()); + } + // LEONIDA_COMPILE_GATE_WARMUPS=1: a warm-up holds submissions as a demanded create does. + { + m.set_warmup_policy({false, 1}); + std::atomic in{false}; + std::atomic warm_left{0}, submitted{0}; + std::thread warm([&] { + const SubmitMutex::CompileTurn turn(m, true); + EXPECT_VOID(!turn.speculative()); + in = true; + std::this_thread::sleep_for(150ms); + warm_left = now_ms(); + }); + EXPECT(eventually([&] { return in.load(); })); + EXPECT(m.speculative_compiles() == 0); + { + const SubmitMutex::SubmitTurn turn(m); + submitted = now_ms(); + } + warm.join(); + EXPECT(submitted.load() >= warm_left.load()); + } + // The settings. + _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", "1"); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "3"); + auto policy = SubmitMutex::warmup_policy_from_environment(); + EXPECT(!policy.free && policy.max == 3); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", "0"); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "0"); + policy = SubmitMutex::warmup_policy_from_environment(); + EXPECT(policy.free && policy.max == 1); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "x"); + EXPECT(SubmitMutex::warmup_policy_from_environment().max == 1); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", ""); + _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", ""); + policy = SubmitMutex::warmup_policy_from_environment(); + EXPECT(policy.free && policy.max == 1); + if (failed_void) return 1; + std::printf("warm-up gate: submissions pass a warm-up, wait for demanded creates; warm-ups capped\n"); + return 0; +} + } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || - compile_gate()) + compile_gate() || warmups_do_not_hold_the_gate()) return 1; std::printf("submit_mutex_test: all passed\n"); return 0; From 3582924b6e1ac5a43b3c6197a9e4d95f4fd47e76 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 10:17:23 -0600 Subject: [PATCH 2/2] Compile gate: speculative creates pass only as compute ones with the adaptive gate lixonik's review of #2275: with the classic gate and LEONIDA_COMPILE_GATE=1 no verdict is ever reached, so a warm-up passed the gate there too, also where auto's calibration chose classic because the driver holds submissions (630.20). And since #2260, graphics and mesh prewarms are speculative jobs as well, while the calibration measures only a compute create (621.40 held a submission 54 s beside a mesh create). warmup_passes now decides, under gate_mutex_, in enter_compile: a speculative create passes without holding submissions only if it is a compute one and the gate is adaptive, or LEONIDA_COMPILE_GATE_WARMUPS=0 asks for it with every gate. Every other one holds the gate as a demanded create does. set_warmup_policy clamps max to 1-64 (0 deadlocked speculative creates). Tests for each mode, for a graphics prewarm (also through DriverTurn) and for max 0; docs updated. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 31 +++-- runtime/README.md | 4 +- runtime/shims/d3d12_pipeline_compile.h | 12 +- runtime/src/vulkan_device_fault.h | 62 +++++---- runtime/test/d3d12_compile_pool_test.cpp | 20 ++- runtime/test/submit_mutex_test.cpp | 161 +++++++++++++++-------- 6 files changed, 187 insertions(+), 103 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index fe11fe367a..7ff2cd4177 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2938,21 +2938,26 @@ submitting thread; at the maintainers' decision it was replaced by this one when waits at the compile gate for every demanded compute create in flight (and graphics creates too with `LEONIDA_COMPILE_GATE=adaptive` or `1`; the classic default gates compute builds only; warm-ups: next paragraph), so more workers do not shorten that wait (`SubmitMutex::CompileTurn`; `LEONIDA_COMPILE_GATE=0` turns it off). -- **Warm-ups do not hold the compile gate** (RT-COMPILE-GATE-WARMUP-FREE). A submission waits at the compile gate - (`SubmitMutex::CompileTurn`) only for the demanded creates in the driver, not for a warm-up's: a compute create made - by a compile worker's speculative job (`compile_job_speculative`; today the compute warm-up, and a prewarm's once - there is one) passes the gate as speculative. The checkpoint is a speculative job too, but makes no create. A - warm-up's pipeline is simply not ready yet; whoever needs it builds or waits for it as before. A warm-up - still never enters the driver while a submission is in it or waits at the gate, and at most - `LEONIDA_COMPILE_GATE_WARMUP_MAX` warm-ups (default 1) are in the driver at once, so the creates beside a submission - stay bounded: the pool's demanded builds plus that many warm-ups, and the pool starts no warm-up while a demanded - build runs. Why: on HasiKe's RTX 2070 SUPER (#2233) a cold boot crawled at about one submission a second behind - 290 s of serial warm-ups with the gate on, and ran at about 100 frames a second with `LEONIDA_COMPILE_GATE=0`; +- **Warm-ups do not hold the compile gate** (RT-COMPILE-GATE-WARMUP-FREE). With the adaptive gate, a submission + waits at the compile gate (`SubmitMutex::CompileTurn`) only for the demanded creates in the driver, not for a + warm-up's: a compute create made by a compile worker's speculative job (`compile_job_speculative`: the compute + warm-up) passes the gate as speculative. The checkpoint is + a speculative job too, but makes no create. A warm-up's pipeline is simply not ready yet; whoever needs it builds or + waits for it as before. A warm-up still never enters the driver while a submission is in it or waits at the gate, + and at most `LEONIDA_COMPILE_GATE_WARMUP_MAX` of them (default 1) are in the driver at once, so the creates beside a + submission stay bounded: the pool's demanded builds plus that many warm-ups, and the pool starts no warm-up while a + demanded build runs. Why: on HasiKe's RTX 2070 SUPER (#2233) a cold boot crawled at about one submission a second + behind 290 s of serial warm-ups with the gate on, and ran at about 100 frames a second with `LEONIDA_COMPILE_GATE=0`; MoonEyes measured 142 s of submission waits behind warm-ups (#1608). The gate stays for demanded creates, which the - title waits for anyway. With the adaptive gate, a warm-up of a kind whose verdict is that the driver holds - submissions is waited for as a demanded create is: a submission beside it would wait in the driver instead. `LEONIDA_COMPILE_GATE_WARMUPS=1` makes warm-ups hold the gate again, as before; + title waits for anyway. A speculative create holds the gate as a demanded one does (`SubmitMutex::warmup_passes`, + lixonik's review of #2275): with the classic gate and with `LEONIDA_COMPILE_GATE=1`, which are in use where the + driver holds a submission behind a create or that is not known (`auto`'s calibration on 630.20), and where no probe + ever says otherwise; as a graphics or mesh prewarm (`LEONIDA_PIPELINE_PREWARM_ORDER`, also a speculative job), since the calibration measures only a compute create and a + submission was held 54 s beside a mesh create on 621.40; and as a create of a kind the adaptive gate found to hold + submissions, where a submission beside it would wait in the driver instead. `LEONIDA_COMPILE_GATE_WARMUPS=1` makes + every warm-up hold the gate, as before; `=0` lets compute ones pass with every gate, the classic one and `=1` too; `LEONIDA_COMPILE_GATE=0` still turns the whole gate off. The paragraphs above and below that say a warm-up holds the - gate for its whole create describe `LEONIDA_COMPILE_GATE_WARMUPS=1`, the behaviour before. + gate for its whole create describe those cases. - **Only the large ones asynchronous** (`LEONIDA_ASYNC_MIN_BYTES=`, with `LEONIDA_ASYNC_PIPELINES=1`). With every pipeline asynchronous, the small lighting and post programs are skipped with the large ones until the worker gets to them, and on an RTX 4070 Laptop (NVIDIA 621.40) the world phase was reached with the screen black; with none, a diff --git a/runtime/README.md b/runtime/README.md index 71010f239e..bdb3322a2d 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -430,8 +430,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_COMPILE_GATE_HELD_WAIT_MS` | 60000 | ``: with the adaptive gate, how long a submission waits for creates of a kind the gate found to hold submissions (`docs/d3d12_vulkan.md`) before it goes ahead anyway; those creates are then not waited for again. `0`: until they leave. | | `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 with the adaptive gate (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | ``: 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_COMPILE_GATE_WARMUPS` | unset (warm-ups free) | A submission waits at the compile gate (`SubmitMutex::CompileTurn`, off with `LEONIDA_COMPILE_GATE=0`) only for demanded pipeline creates (and, with the adaptive gate, for any create of a kind found to hold submissions); a compute warm-up's create on a compile worker does not hold it (only compute warm-ups exist), though it still never enters the driver while a submission is in it or waits. `1`: warm-ups hold the gate as demanded creates do, the behaviour before. See `docs/d3d12_vulkan.md`, "Warm-ups do not hold the compile gate". | -| `LEONIDA_COMPILE_GATE_WARMUP_MAX` | `1` | `` (1-64): at most n warm-up creates in the driver at once while warm-ups do not hold the compile gate. | +| `LEONIDA_COMPILE_GATE_WARMUPS` | unset (compute warm-ups free with the adaptive gate) | Unset: with the adaptive gate (`LEONIDA_COMPILE_GATE=adaptive`, or `auto` where the calibration chose it), a submission at the compile gate (`SubmitMutex::CompileTurn`) does not wait for a speculative compute create, one a compile worker's speculative job makes (`compile_job_speculative`: the compute warm-up), though such a create still never enters the driver while a submission is in it or waits; it waits for demanded creates as before, and for any create of a kind the adaptive gate found to hold submissions. Graphics and mesh prewarms (`LEONIDA_PIPELINE_PREWARM_ORDER`), speculative jobs too, hold the gate as demanded creates do (the calibration measures only a compute create, and NVIDIA 621.40 held a submission 54 s beside a mesh create), and so do all speculative creates with the classic gate and with `LEONIDA_COMPILE_GATE=1`, which are in use where the driver holds submissions or that is not known. `1`: every speculative create holds the gate as a demanded create does, the behaviour before. `0`: a speculative compute create passes with every gate, the classic one and `1` included (on a driver that holds submissions a submission may then wait in the driver beside it). See `docs/d3d12_vulkan.md`, "Warm-ups do not hold the compile gate". | +| `LEONIDA_COMPILE_GATE_WARMUP_MAX` | `1` | `` (1-64): at most n speculative creates that pass the compile gate without holding submissions (`LEONIDA_COMPILE_GATE_WARMUPS`) in the driver at once. | | `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 | ``: 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`) | ``: 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`. | diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index dd9ab62bd5..52aed79e4c 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -166,13 +166,15 @@ inline bool compile_worker_trigger(const char* trigger) { // waits only for the holder, and only before a pipeline create (it may wait out a vkDeviceWaitIdle or a present: // waits of 50 ms or more are logged). // - a pipeline create passes the compile gate (SubmitMutex::CompileTurn) for the length of the call, as its kind -// (compute, or graphics with mesh): the adaptive gate keeps a verdict per kind. As speculative, not holding -// submissions, when a compile worker runs it for a speculative job (compile_job_speculative). +// (compute, or graphics with mesh): the adaptive gate keeps a verdict per kind. As speculative when a compile worker +// runs it for a speculative job (compile_job_speculative); the gate decides whether that lets it pass without +// holding submissions (SubmitMutex::warmup_passes: a compute create, with the adaptive gate). // - the compile worker, which runs below normal priority, runs the call at normal priority, so a preempted worker // does not sit on a driver lock a submit waits for. // True on a compile worker while it runs a speculative job (any job of a priority other than 0: today the compute warm-up, -// and the checkpoint, which makes no create; PipelineCompiler::run): -// its pipeline creates pass the compile gate as warm-ups, which do not hold submissions (SubmitMutex::CompileTurn). +// the graphics and mesh prewarms of LEONIDA_PIPELINE_PREWARM_ORDER, and the checkpoint, which makes no create; +// PipelineCompiler::run): its pipeline creates ask the compile gate to pass them as speculative, which a compute one +// with the adaptive gate does without holding submissions (SubmitMutex::CompileTurn, warmup_passes). inline thread_local bool compile_job_speculative = false; class DriverTurn { @@ -614,7 +616,7 @@ class PipelineCompiler { stats_.most_running = (std::max)(stats_.most_running, running_.size()); const auto probe = probe_; lock.unlock(); - compile_job_speculative = speculative; // its creates do not hold submissions (DriverTurn) + compile_job_speculative = speculative; // its creates pass the gate as speculative (DriverTurn) try { if (probe) probe(job.owner); job.work(); diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index ec58d9aff3..0c96d28554 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h @@ -422,13 +422,17 @@ class SubmitMutex { // No deadlock: a create holds the gate only across its vkCreate*Pipelines and a submission only across its // vkQueueSubmit, and neither driver call waits for anything of ours. // - // A speculative create (a warm-up: nobody waits for its pipeline yet) does not hold submissions: they wait only for - // the demanded creates in the driver. It still never enters while a submission is in the driver or waits there, and - // at most warmup_max of them are in the driver at once, so the creates beside a submission stay bounded (HasiKe and - // MoonEyes, #2233 / #1608: a cold boot crawled at about one submission a second behind 290 s of warm-ups). One of a - // kind with the driver_holds verdict (adaptive gate) holds submissions as a demanded create does: a submission beside - // it would wait in the driver instead. LEONIDA_COMPILE_GATE_WARMUPS=1: warm-ups hold the gate as demanded creates do - // (the behaviour before). + // A speculative create (a warm-up or a prewarm: nobody waits for its pipeline yet) of a compute pipeline, with the + // adaptive gate, does not hold submissions: they wait only for the demanded creates in the driver. It still never + // enters while a submission is in the driver or waits there, and at most warmup_max of them are in the driver at + // once, so the creates beside a submission stay bounded (HasiKe and MoonEyes, #2233 / #1608: a cold boot crawled at + // about one submission a second behind 290 s of warm-ups). Every other speculative create holds submissions as a + // demanded create does (warmup_passes): with the classic gate or LEONIDA_COMPILE_GATE=1, which are in use where the + // driver holds a submission behind a create or that is not known (auto's calibration, 630.20), and no probe ever + // tells otherwise; a graphics or mesh one, since the calibration measures only a compute create and a submission + // was held 54 s beside a mesh create on 621.40; and one of a kind the adaptive gate found to hold submissions + // (is_blocking). LEONIDA_COMPILE_GATE_WARMUPS=1: speculative creates always hold the gate (the behaviour before); + // =0: a compute one passes with every gate, the classic one and =1 too. // // By default (classic) only compute creates pass the gate and a submission waits for them however long they take, // as before the adaptive gate; LEONIDA_COMPILE_GATE_WAIT_MS bounds that too. With the adaptive gate @@ -510,9 +514,9 @@ class SubmitMutex { using GateAhead = CompileGateAhead; class CompileTurn { // around vkCreate*Pipelines public: + // `speculative`: asked for; speculative() says whether the gate let it pass as one (warmup_passes). explicit CompileTurn(SubmitMutex& gate, const CreateInfo& info = {}, bool speculative = false) - : gate_(gate), speculative_(speculative && gate.warmups_free()), - waited_(gate.enter_compile(seq_, info, speculative_)) {} + : gate_(gate), waited_(gate.enter_compile(seq_, info, speculative, &speculative_)) {} ~CompileTurn() { gate_.leave_compile(seq_); } CompileTurn(const CompileTurn&) = delete; CompileTurn& operator=(const CompileTurn&) = delete; @@ -523,7 +527,7 @@ class SubmitMutex { private: SubmitMutex& gate_; std::uint64_t seq_ = 0; // declared before waited_: enter_compile sets it - bool speculative_; // declared before waited_: enter_compile reads it + bool speculative_ = false; // declared before waited_: enter_compile sets it std::int64_t waited_; }; class SubmitTurn { // around vkQueueSubmit, with the submit lock held @@ -710,32 +714,35 @@ class SubmitMutex { return logged_held_waits_; } - // Whether warm-ups pass the gate without holding submissions, and how many may be in the driver at once. - // LEONIDA_COMPILE_GATE_WARMUPS=1: they hold it as demanded creates do. LEONIDA_COMPILE_GATE_WARMUP_MAX=: n at - // once (default 1). Read once; a test sets them per gate (set_warmup_policy). + // Whether speculative creates may pass the gate without holding submissions (see CompileTurn), and how many may be + // in the driver at once. LEONIDA_COMPILE_GATE_WARMUPS: unset, compute ones with the adaptive gate; 1, none (they + // hold it as demanded creates do); 0, compute ones with every gate (`everywhere`). LEONIDA_COMPILE_GATE_WARMUP_MAX + // =: n at once (1-64, default 1). Read once; a test sets them per gate (set_warmup_policy). + static constexpr unsigned kMaxWarmups = 64; struct WarmupPolicy { bool free = true; unsigned max = 1; + bool everywhere = false; // with the classic gate and LEONIDA_COMPILE_GATE=1 too }; static WarmupPolicy warmup_policy_from_environment() { WarmupPolicy policy; - const char* hold = std::getenv("LEONIDA_COMPILE_GATE_WARMUPS"); - policy.free = !(hold && !std::strcmp(hold, "1")); + if (const char* warmups = std::getenv("LEONIDA_COMPILE_GATE_WARMUPS")) { + policy.free = std::strcmp(warmups, "1") != 0; + policy.everywhere = !std::strcmp(warmups, "0"); + } if (const char* max = std::getenv("LEONIDA_COMPILE_GATE_WARMUP_MAX")) { char* end = nullptr; const unsigned long n = std::strtoul(max, &end, 10); - if (end != max && !*end && n >= 1 && n <= 64) policy.max = unsigned(n); + if (end != max && !*end && n >= 1 && n <= kMaxWarmups) policy.max = unsigned(n); } return policy; } + // max is clamped to 1-64: 0 would keep every speculative create waiting for a place. void set_warmup_policy(WarmupPolicy policy) { + policy.max = (std::clamp)(policy.max, 1u, kMaxWarmups); std::lock_guard lock(gate_mutex_); policy_ = policy; } - bool warmups_free() { - std::lock_guard lock(gate_mutex_); - return policy_.free; - } // Speculative creates in the driver now (they are not counted in what a submission waits for). unsigned speculative_compiles() { std::lock_guard lock(gate_mutex_); @@ -757,9 +764,12 @@ class SubmitMutex { return stats_; } - // `seq` receives the create's number, which leave_compile takes back. `speculative`: a warm-up that does not hold - // submissions (CompileTurn); it also waits for a free place among the warmup_max speculative ones. - std::int64_t enter_compile(std::uint64_t& seq, const CreateInfo& info = {}, bool speculative = false) { + // `seq` receives the create's number, which leave_compile takes back. `speculative`: a warm-up or prewarm, which + // passes without holding submissions where warmup_passes says so (CompileTurn) and then also waits for a free + // place among the warmup_max speculative ones; `passed` (optional) is told whether it did. + std::int64_t enter_compile(std::uint64_t& seq, const CreateInfo& info = {}, bool speculative = false, + bool* passed = nullptr) { + if (passed) *passed = false; if (!compile_gate_enabled()) return 0; std::unique_lock lock(gate_mutex_); std::int64_t waited = 0; @@ -767,6 +777,8 @@ class SubmitMutex { seq = 0; return 0; } + speculative = speculative && warmup_passes(info.kind); + if (passed) *passed = speculative; // Not behind this thread's own submission (it holds the submit lock: none of its submissions is in the // driver now, and waiting for them would never end). const bool own = owner_.load(std::memory_order_relaxed) == diagnostic_thread_id(); @@ -957,6 +969,10 @@ class SubmitMutex { }; // With gate_mutex_ held. + // Whether a speculative create of this kind passes without holding submissions (see CompileTurn). + bool warmup_passes(CreateKind kind) const { + return policy_.free && kind == CreateKind::compute && (adaptive_ || policy_.everywhere); + } bool kind_off(CreateKind kind) const { return verdict_[std::size_t(kind)] == GateVerdict::driver_does_not_hold; } diff --git a/runtime/test/d3d12_compile_pool_test.cpp b/runtime/test/d3d12_compile_pool_test.cpp index 2c4502d4da..a8cc6e842f 100644 --- a/runtime/test/d3d12_compile_pool_test.cpp +++ b/runtime/test/d3d12_compile_pool_test.cpp @@ -433,13 +433,27 @@ void test_jobs_tell_the_gate_whether_they_are_speculative() { void test_a_driver_turn_passes_the_speculative_flag_to_the_gate() { if (!leonida::SubmitMutex::compile_gate_enabled()) return; // LEONIDA_COMPILE_GATE=0: no gate to check leonida::SubmitMutex gate; + gate.set_gate_adaptive(true); // warm-ups pass only the adaptive gate (submit_mutex_test has the other modes) + gate.set_gate_wait_limit_ms(0); + gate.set_gate_probe_budget_ms(0); + gate.set_gate_graphics(true); // graphics creates pass the gate, as with the adaptive gate gate.set_warmup_policy({true, 1}); - for (const bool speculative : {true, false}) { + // A speculative compute create (a warm-up), a demanded one, and a speculative graphics one (a prewarm of + // LEONIDA_PIPELINE_PREWARM_ORDER, which holds submissions: the calibration measures only compute creates). + struct Case { + bool speculative; + leonida::SubmitMutex::CreateKind kind; + bool passes; + }; + for (const Case c : {Case{true, leonida::SubmitMutex::CreateKind::compute, true}, + Case{false, leonida::SubmitMutex::CreateKind::compute, false}, + Case{true, leonida::SubmitMutex::CreateKind::graphics, false}}) { + const bool speculative = c.speculative; std::atomic inside{false}, release{false}, submitted{false}; std::thread create([&] { rt::d3d12::compile_job_speculative = speculative; { - const rt::d3d12::DriverTurn turn(gate, "warmup", "vkCreateComputePipelines", true, nullptr); + const rt::d3d12::DriverTurn turn(gate, "warmup", "vkCreateComputePipelines", true, nullptr, c.kind); inside = true; while (!release) std::this_thread::sleep_for(1ms); } @@ -452,7 +466,7 @@ void test_a_driver_turn_passes_the_speculative_flag_to_the_gate() { submitted = true; }); std::this_thread::sleep_for(150ms); - EXPECT(submitted.load() == speculative); // past a warm-up at once; behind a demanded create + EXPECT(submitted.load() == c.passes); // past a warm-up at once; behind a demanded create or a prewarm release = true; create.join(); submit.join(); diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index d72d5a6084..2cd16e29b8 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp @@ -208,53 +208,117 @@ int compile_gate() { return 0; } -// Warm-up creates (CompileTurn speculative) do not hold submissions; demanded ones still do; at most warmup_max warm-ups -// are in the driver at once; none enters while a submission waits or is in the driver; with warm-ups held -// (LEONIDA_COMPILE_GATE_WARMUPS=1) a submission waits for them as before. +// Speculative creates (CompileTurn speculative: warm-ups and prewarms). With the adaptive gate a compute one does not +// hold submissions; demanded ones still do; at most warmup_max are in the driver at once; none enters while a +// submission waits or is in the driver. They hold submissions as demanded creates do with the classic gate and with +// LEONIDA_COMPILE_GATE=1 (unless LEONIDA_COMPILE_GATE_WARMUPS=0), as graphics or mesh ones, and with +// LEONIDA_COMPILE_GATE_WARMUPS=1. Each case on a gate of its own, so no probe of one case gives a verdict to the next. int warmups_do_not_hold_the_gate() { if (!SubmitMutex::compile_gate_enabled()) { std::printf("warm-up gate: compile gate off (LEONIDA_COMPILE_GATE=0): not checked\n"); return 0; } - SubmitMutex m; - m.set_warmup_policy({true, 1}); - // A submission is not held by a warm-up in the driver. - { - std::atomic in{false}, release{false}; + // A gate as each mode makes it: the adaptive one without early probes or a wait limit (a verdict could turn the + // compute gate off under a later case), the classic one, and LEONIDA_COMPILE_GATE=1 (graphics creates pass it). + enum class Mode { adaptive, classic, always }; + const auto make = [](Mode mode, SubmitMutex::WarmupPolicy policy = {true, 1}) { + auto m = std::make_unique(); + m->set_gate_adaptive(mode == Mode::adaptive); + m->set_gate_graphics(mode != Mode::classic); + m->set_gate_wait_limit_ms(0); + m->set_gate_probe_budget_ms(0); + m->set_warmup_policy(policy); + return m; + }; + // Whether a submission waits for a speculative create of `kind` on gate `m`: the create stays 150 ms. + const auto submission_waits = [](SubmitMutex& m, SubmitMutex::CreateKind kind, bool* passed) { + std::atomic in{false}; + std::atomic left{0}; std::thread warm([&] { - const SubmitMutex::CompileTurn turn(m, {}, true); + const SubmitMutex::CompileTurn turn(m, SubmitMutex::CreateInfo{kind, "warm", "test", 0}, true); + *passed = turn.speculative(); in = true; - while (!release) std::this_thread::sleep_for(1ms); + std::this_thread::sleep_for(150ms); + left = now_ms(); }); - EXPECT(eventually([&] { return in.load(); })); - EXPECT(m.speculative_compiles() == 1); - const std::int64_t start = now_ms(); + bool entered = false; + for (int i = 0; i < 2000 && !in; ++i) std::this_thread::sleep_for(1ms); + entered = in.load(); + std::int64_t submitted = 0; { const SubmitMutex::SubmitTurn turn(m); + submitted = now_ms(); } - EXPECT(now_ms() - start < 50); - release = true; warm.join(); - EXPECT(m.speculative_compiles() == 0); + return entered && submitted >= left.load(); + }; + using Kind = SubmitMutex::CreateKind; + // Adaptive gate: a submission is not held by a compute warm-up in the driver. + { + auto m = make(Mode::adaptive); + bool passed = false; + EXPECT(!submission_waits(*m, Kind::compute, &passed)); + EXPECT(passed && m->speculative_compiles() == 0); + } + // ... but by a graphics or mesh prewarm (the calibration measures compute creates only). + { + auto m = make(Mode::adaptive); + bool passed = true; + EXPECT(submission_waits(*m, Kind::graphics, &passed)); + EXPECT(!passed); + } + // Classic gate (also auto's choice where the driver holds submissions) and LEONIDA_COMPILE_GATE=1: a compute + // warm-up holds submissions; with =1 a graphics prewarm does too. + for (const Mode mode : {Mode::classic, Mode::always}) { + auto m = make(mode); + bool passed = true; + EXPECT(submission_waits(*m, Kind::compute, &passed)); + EXPECT(!passed); + if (mode == Mode::always) { + passed = true; + EXPECT(submission_waits(*m, Kind::graphics, &passed)); + EXPECT(!passed); + } + } + // LEONIDA_COMPILE_GATE_WARMUPS=0: a compute warm-up passes with the classic gate and =1 too; graphics ones never. + for (const Mode mode : {Mode::classic, Mode::always}) { + auto m = make(mode, {true, 1, true}); + bool passed = false; + EXPECT(!submission_waits(*m, Kind::compute, &passed)); + EXPECT(passed); + if (mode == Mode::always) { + passed = true; + EXPECT(submission_waits(*m, Kind::graphics, &passed)); + EXPECT(!passed); + } + } + // LEONIDA_COMPILE_GATE_WARMUPS=1: a warm-up holds submissions with the adaptive gate too. + { + auto m = make(Mode::adaptive, {false, 1}); + bool passed = true; + EXPECT(submission_waits(*m, Kind::compute, &passed)); + EXPECT(!passed && m->speculative_compiles() == 0); } // A demanded create is still waited for, also beside a warm-up: the submission waits for the demanded one only. { + auto m = make(Mode::adaptive); std::atomic warm_in{false}, demand_in{false}, release_warm{false}; std::atomic demand_left{0}, submitted{0}; std::thread warm([&] { - const SubmitMutex::CompileTurn turn(m, {}, true); + const SubmitMutex::CompileTurn turn(*m, {}, true); warm_in = true; while (!release_warm) std::this_thread::sleep_for(1ms); }); std::thread demand([&] { - const SubmitMutex::CompileTurn turn(m); + const SubmitMutex::CompileTurn turn(*m); demand_in = true; std::this_thread::sleep_for(200ms); demand_left = now_ms(); }); EXPECT(eventually([&] { return warm_in.load() && demand_in.load(); })); + EXPECT(m->speculative_compiles() == 1); std::thread submit([&] { - const SubmitMutex::SubmitTurn turn(m); + const SubmitMutex::SubmitTurn turn(*m); submitted = now_ms(); }); submit.join(); @@ -263,16 +327,19 @@ int warmups_do_not_hold_the_gate() { EXPECT(warm_in.load() && !release_warm.load()); // and not for the warm-up, still in the driver release_warm = true; warm.join(); + EXPECT(m->speculative_compiles() == 0); } - // At most warmup_max warm-ups in the driver; a demanded create does not wait for them. - for (const unsigned max : {1u, 2u}) { - m.set_warmup_policy({true, max}); + // At most warmup_max warm-ups in the driver; a demanded create does not wait for them. A max of 0 is taken as 1: + // it would keep every warm-up waiting for a place. + for (const unsigned max : {0u, 1u, 2u}) { + auto m = make(Mode::adaptive, {true, max}); + const int places = int((std::max)(max, 1u)); std::atomic inside{0}, most{0}; std::atomic release{false}; std::vector warms; for (int i = 0; i < 4; ++i) warms.emplace_back([&] { - const SubmitMutex::CompileTurn turn(m, {}, true); + const SubmitMutex::CompileTurn turn(*m, {}, true); const int now = ++inside; int seen = most.load(); while (now > seen && !most.compare_exchange_weak(seen, now)) { @@ -280,35 +347,35 @@ int warmups_do_not_hold_the_gate() { while (!release) std::this_thread::sleep_for(1ms); --inside; }); - EXPECT(eventually([&] { return inside.load() == int(max); })); + EXPECT(eventually([&] { return inside.load() == places; })); std::this_thread::sleep_for(100ms); - EXPECT(inside.load() == int(max)); // the others wait for a place + EXPECT(inside.load() == places); // the others wait for a place std::atomic demand_waited{-1}; std::thread demand([&] { - const SubmitMutex::CompileTurn turn(m); + const SubmitMutex::CompileTurn turn(*m); demand_waited = turn.waited_ms(); }); demand.join(); EXPECT(demand_waited.load() == 0); release = true; for (auto& t : warms) t.join(); - EXPECT(most.load() == int(max)); - EXPECT(m.speculative_compiles() == 0); + EXPECT(most.load() == places); + EXPECT(m->speculative_compiles() == 0); } - m.set_warmup_policy({true, 1}); // A warm-up never enters while a submission is in the driver. { + auto m = make(Mode::adaptive); std::atomic submitting{false}, release{false}; std::atomic submit_left{0}, warm_entered{0}; std::thread submit([&] { - const SubmitMutex::SubmitTurn turn(m); + const SubmitMutex::SubmitTurn turn(*m); submitting = true; while (!release) std::this_thread::sleep_for(1ms); submit_left = now_ms(); }); EXPECT(eventually([&] { return submitting.load(); })); std::thread warm([&] { - const SubmitMutex::CompileTurn turn(m, {}, true); + const SubmitMutex::CompileTurn turn(*m, {}, true); warm_entered = now_ms(); }); std::this_thread::sleep_for(100ms); @@ -318,44 +385,24 @@ int warmups_do_not_hold_the_gate() { warm.join(); EXPECT(warm_entered.load() >= submit_left.load()); } - // LEONIDA_COMPILE_GATE_WARMUPS=1: a warm-up holds submissions as a demanded create does. - { - m.set_warmup_policy({false, 1}); - std::atomic in{false}; - std::atomic warm_left{0}, submitted{0}; - std::thread warm([&] { - const SubmitMutex::CompileTurn turn(m, {}, true); - EXPECT_VOID(!turn.speculative()); - in = true; - std::this_thread::sleep_for(150ms); - warm_left = now_ms(); - }); - EXPECT(eventually([&] { return in.load(); })); - EXPECT(m.speculative_compiles() == 0); - { - const SubmitMutex::SubmitTurn turn(m); - submitted = now_ms(); - } - warm.join(); - EXPECT(submitted.load() >= warm_left.load()); - } // The settings. _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", "1"); _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "3"); auto policy = SubmitMutex::warmup_policy_from_environment(); - EXPECT(!policy.free && policy.max == 3); + EXPECT(!policy.free && !policy.everywhere && policy.max == 3); _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", "0"); _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "0"); policy = SubmitMutex::warmup_policy_from_environment(); - EXPECT(policy.free && policy.max == 1); + EXPECT(policy.free && policy.everywhere && policy.max == 1); _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", "x"); EXPECT(SubmitMutex::warmup_policy_from_environment().max == 1); _putenv_s("LEONIDA_COMPILE_GATE_WARMUPS", ""); _putenv_s("LEONIDA_COMPILE_GATE_WARMUP_MAX", ""); policy = SubmitMutex::warmup_policy_from_environment(); - EXPECT(policy.free && policy.max == 1); - if (failed_void) return 1; - std::printf("warm-up gate: submissions pass a warm-up, wait for demanded creates; warm-ups capped\n"); + EXPECT(policy.free && !policy.everywhere && policy.max == 1); + std::printf("warm-up gate: adaptive gate: compute warm-ups pass, graphics prewarms and demanded creates hold, " + "warm-ups capped (max 0 taken as 1); classic and =1: warm-ups hold unless " + "LEONIDA_COMPILE_GATE_WARMUPS=0\n"); return 0; }