From b9ce8f26c16a7848b06eb7357085c53f4071cec9 Mon Sep 17 00:00:00 2001 From: Madhyn Date: Wed, 25 Nov 2026 01:50:06 -0300 Subject: [PATCH] d3d12: parallel PipelineCompiler workers with reserved demand lane (RT-PIPELINE-COMPILER-WORKERS) Upgrade rt::d3d12::PipelineCompiler from a single below-normal worker to a bounded worker pool (LEONIDA_COMPILE_THREADS, default 2..6 by hardware concurrency; 1 preserves the single-worker path). With two or more workers, speculative compute warmup is capped at N-1 concurrent jobs so at least one worker stays reserved for demanded priority-0 builds, and SIZE_MAX idle checkpoints wait until no compile jobs are running. Also count in-flight compile jobs in has_compile_waiter(). Add unit tests in runtime/test/pipeline_compiler_test.cpp covering parallel demanded builds, demand-lane reservation, idle checkpoint ordering, single-worker priority order, and LEONIDA_COMPILE_THREADS parsing. --- runtime/CMakeLists.txt | 9 ++ runtime/shims/d3d12_pipeline_compile.h | 71 +++++++-- runtime/test/pipeline_compiler_test.cpp | 197 ++++++++++++++++++++++++ 3 files changed, 268 insertions(+), 9 deletions(-) create mode 100644 runtime/test/pipeline_compiler_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 48c8af360..1b1028984 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1760,6 +1760,15 @@ if(TARGET Vulkan::Headers) target_link_libraries(vulkan_device_fault_test PRIVATE Vulkan::Headers) add_test(NAME vulkan_device_fault COMMAND vulkan_device_fault_test) + if(WIN32) + add_executable(pipeline_compiler_test test/pipeline_compiler_test.cpp) + target_include_directories(pipeline_compiler_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims) + target_link_libraries(pipeline_compiler_test PRIVATE Vulkan::Headers) + target_compile_definitions(pipeline_compiler_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(pipeline_compiler_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME pipeline_compiler COMMAND pipeline_compiler_test) + endif() + foreach(tool vk_harness vk_ps_sweep vk_texture_test vk_sampler_test vk_compute_test vk_store_test vk_pipeline_check vk_command_replay_test vk_bindless_test vk_image_fuzz vk_descriptor_bench depth24_convert_test vk_predicate_capture_test vk_predicated_copy_test vk_xdxr_build_test vk_queue_test vk_sandbox) diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index 6ef257f0b..8fcc911eb 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -6,11 +6,13 @@ #include #include #include +#include #include #include #include #include #include +#include namespace rt::d3d12 { inline bool trace_pipeline_compile() { @@ -84,18 +86,49 @@ struct CompileTrace { // Demanded work has priority zero, speculative work is ordered by module size, and -// an idle checkpoint runs last. Jobs own their object until they finish. No Vulkan -// call runs with the queue lock held. The worker outlives individual D3D12 devices. +// an idle checkpoint runs last. With two or more workers (LEONIDA_COMPILE_THREADS, default +// 2..6 by hardware concurrency; 1 keeps the single-worker path), at least one worker is +// reserved so demanded builds never stall behind speculative warmup compiles, and an idle +// checkpoint waits until no compile jobs are running. Jobs own their object until they +// finish. No Vulkan call runs with the queue lock held. The singleton outlives individual +// D3D12 devices. class PipelineCompiler { public: static PipelineCompiler& instance() { static auto* pool = new PipelineCompiler; return *pool; } + static unsigned configured_worker_count(unsigned hw = std::thread::hardware_concurrency()) { + char text[16]{}; + const DWORD n = GetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", text, sizeof(text)); + if (n > 0 && n < sizeof(text)) { + char* end = nullptr; + const unsigned long parsed = std::strtoul(text, &end, 10); + if (end != text && *end == '\0' && parsed >= 1 && parsed <= 32) return static_cast(parsed); + } + return hw >= 18 ? 6u : hw >= 12 ? 4u : hw >= 6 ? 3u : 2u; + } + explicit PipelineCompiler(unsigned workers = 0) + : workers_(workers ? workers : configured_worker_count()) {} + ~PipelineCompiler() { + { + std::lock_guard lock(mutex_); + stopping_ = true; + } + wake_.notify_all(); + for (auto& t : threads_) + if (t.joinable()) t.join(); + } + PipelineCompiler(const PipelineCompiler&) = delete; + PipelineCompiler& operator=(const PipelineCompiler&) = delete; + + unsigned worker_count() const noexcept { return workers_; } + void submit(std::size_t priority, std::function work) { std::lock_guard lock(mutex_); if (!started_) { - std::thread([this] { run(); }).detach(); + threads_.reserve(workers_); + for (unsigned i = 0; i < workers_; ++i) threads_.emplace_back([this] { run(); }); started_ = true; } jobs_.emplace(priority, std::move(work)); @@ -103,41 +136,61 @@ class PipelineCompiler { } bool has_compile_waiter() { std::lock_guard lock(mutex_); - return !jobs_.empty() && jobs_.begin()->first != SIZE_MAX; + return (!jobs_.empty() && jobs_.begin()->first != SIZE_MAX) || compile_running_ > 0; } // Returns when no job is queued or running. A job owns its pipeline state, and through it the device, until it // finishes; a caller that counts references (rt::d3d12_xs::device_proxy_ref_count) waits here first. void wait_idle() { std::unique_lock wait(mutex_); - idle_.wait(wait, [this] { return jobs_.empty() && !running_; }); + idle_.wait(wait, [this] { return jobs_.empty() && running_ == 0; }); } private: + bool can_take_locked(std::size_t priority) const noexcept { + if (priority == 0) return true; + if (priority == SIZE_MAX) return running_ == 0; + return workers_ <= 1 || speculative_running_ + 1 < workers_; + } void run() { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL); for (;;) { + std::size_t priority = 0; std::function job; { std::unique_lock wait(mutex_); - wake_.wait(wait, [this] { return !jobs_.empty(); }); + wake_.wait(wait, [this] { + return stopping_ || (!jobs_.empty() && can_take_locked(jobs_.begin()->first)); + }); + if (stopping_) return; auto first = jobs_.begin(); + priority = first->first; job = std::move(first->second); jobs_.erase(first); - running_ = true; + ++running_; + if (priority != SIZE_MAX) ++compile_running_; + if (priority != 0 && priority != SIZE_MAX) ++speculative_running_; } job(); job = nullptr; // what the job captured goes before the pool reports itself idle { std::lock_guard done(mutex_); - running_ = false; + --running_; + if (priority != SIZE_MAX) --compile_running_; + if (priority != 0 && priority != SIZE_MAX) --speculative_running_; } + wake_.notify_all(); idle_.notify_all(); } } + unsigned workers_ = 1; std::mutex mutex_; std::condition_variable wake_; std::condition_variable idle_; - bool running_ = false; + std::size_t running_ = 0; + std::size_t compile_running_ = 0; + std::size_t speculative_running_ = 0; std::multimap> jobs_; + std::vector threads_; bool started_ = false; + bool stopping_ = false; }; } // namespace rt::d3d12 diff --git a/runtime/test/pipeline_compiler_test.cpp b/runtime/test/pipeline_compiler_test.cpp new file mode 100644 index 000000000..645caa767 --- /dev/null +++ b/runtime/test/pipeline_compiler_test.cpp @@ -0,0 +1,197 @@ +// Unit tests for rt::d3d12::PipelineCompiler (shims/d3d12_pipeline_compile.h): +// parallel demanded builds, demand-lane reservation against speculative warmup, +// idle checkpoint ordering, single-worker priority order, and LEONIDA_COMPILE_THREADS parsing. +#include "d3d12_pipeline_compile.h" + +#include +#include +#include +#include +#include +#include + +namespace { + +int g_failed = 0; + +void check(bool ok, const char* what) { + std::printf("%s %s\n", ok ? "ok " : "FAIL ", what); + if (!ok) ++g_failed; +} + +void test_parallel_demand() { + rt::d3d12::PipelineCompiler pool(2); + std::mutex m; + std::condition_variable cv; + int arrived = 0; + std::atomic completed{0}; + auto barrier_job = [&] { + std::unique_lock lock(m); + ++arrived; + cv.notify_all(); + cv.wait(lock, [&] { return arrived == 2; }); + completed.fetch_add(1, std::memory_order_relaxed); + }; + pool.submit(0, barrier_job); + pool.submit(0, barrier_job); + pool.wait_idle(); + check(completed.load() == 2, "two demanded jobs run concurrently on a 2-worker pool"); +} + +void test_demand_lane_reserved() { + rt::d3d12::PipelineCompiler pool(3); + std::mutex m; + std::condition_variable cv; + int warmup_started = 0; + bool release_warmup = false; + std::atomic third_warmup_started{false}; + std::atomic demand_finished{false}; + + auto blocking_warmup = [&] { + std::unique_lock lock(m); + ++warmup_started; + cv.notify_all(); + cv.wait(lock, [&] { return release_warmup; }); + }; + pool.submit(1024, blocking_warmup); + pool.submit(2048, blocking_warmup); + pool.submit(4096, [&] { third_warmup_started.store(true, std::memory_order_release); }); + + { + std::unique_lock lock(m); + cv.wait(lock, [&] { return warmup_started == 2; }); + } + std::mutex dm; + std::condition_variable dcv; + bool demand_done = false; + pool.submit(0, [&] { + demand_finished.store(true, std::memory_order_release); + std::lock_guard lock(dm); + demand_done = true; + dcv.notify_all(); + }); + { + std::unique_lock lock(dm); + dcv.wait(lock, [&] { return demand_done; }); + } + check(demand_finished.load(std::memory_order_acquire), + "demanded job completes immediately on the reserved lane while warmup fills N-1 workers"); + check(!third_warmup_started.load(std::memory_order_acquire), + "speculative warmup stays capped at N-1 concurrent jobs"); + + { + std::lock_guard lock(m); + release_warmup = true; + } + cv.notify_all(); + pool.wait_idle(); + check(third_warmup_started.load(std::memory_order_acquire), + "queued speculative warmup finishes once earlier warmup jobs release"); +} + +void test_checkpoint_waits_for_idle() { + rt::d3d12::PipelineCompiler pool(2); + std::mutex m; + std::condition_variable cv; + bool compile_started = false; + bool allow_compile_exit = false; + std::atomic compile_done{false}; + std::atomic checkpoint_saw_compile_done{false}; + + pool.submit(0, [&] { + { + std::lock_guard lock(m); + compile_started = true; + } + cv.notify_all(); + std::unique_lock lock(m); + cv.wait(lock, [&] { return allow_compile_exit; }); + compile_done.store(true, std::memory_order_release); + }); + + { + std::unique_lock lock(m); + cv.wait(lock, [&] { return compile_started; }); + } + check(pool.has_compile_waiter(), "has_compile_waiter is true while a compile job is running"); + pool.submit(SIZE_MAX, [&] { + checkpoint_saw_compile_done.store(compile_done.load(std::memory_order_acquire), std::memory_order_release); + }); + { + std::lock_guard lock(m); + allow_compile_exit = true; + } + cv.notify_all(); + pool.wait_idle(); + check(checkpoint_saw_compile_done.load(std::memory_order_acquire), + "idle checkpoint (SIZE_MAX) runs only after active compile jobs finish"); + check(!pool.has_compile_waiter(), "has_compile_waiter is false once the pool is idle"); +} + +void test_single_worker_strict_order() { + rt::d3d12::PipelineCompiler pool(1); + std::mutex m; + std::condition_variable cv; + bool gate_entered = false; + bool release_gate = false; + std::vector order; + + pool.submit(0, [&] { + { + std::lock_guard lock(m); + gate_entered = true; + } + cv.notify_all(); + std::unique_lock lock(m); + cv.wait(lock, [&] { return release_gate; }); + }); + { + std::unique_lock lock(m); + cv.wait(lock, [&] { return gate_entered; }); + } + pool.submit(SIZE_MAX, [&] { order.push_back(SIZE_MAX); }); + pool.submit(4096, [&] { order.push_back(4096); }); + pool.submit(512, [&] { order.push_back(512); }); + pool.submit(0, [&] { order.push_back(0); }); + { + std::lock_guard lock(m); + release_gate = true; + } + cv.notify_all(); + pool.wait_idle(); + const bool exact = order.size() == 4 && order[0] == 0 && order[1] == 512 && order[2] == 4096 && + order[3] == SIZE_MAX; + check(exact, "single-worker mode drains priority 0, smallest warmup, then SIZE_MAX checkpoint"); +} + +void test_worker_count_config() { + SetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", nullptr); + check(rt::d3d12::PipelineCompiler::configured_worker_count(4) == 2, "hw=4 defaults to 2 workers"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(8) == 3, "hw=8 defaults to 3 workers"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(12) == 4, "hw=12 defaults to 4 workers"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(20) == 6, "hw=20 defaults to 6 workers"); + + SetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", "1"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(20) == 1, "LEONIDA_COMPILE_THREADS=1 selects 1 worker"); + SetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", "8"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(4) == 8, "LEONIDA_COMPILE_THREADS=8 selects 8 workers"); + SetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", "0"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(12) == 4, "LEONIDA_COMPILE_THREADS=0 falls back to default"); + SetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", "99"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(12) == 4, "LEONIDA_COMPILE_THREADS=99 falls back to default"); + SetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", "4abc"); + check(rt::d3d12::PipelineCompiler::configured_worker_count(12) == 4, "LEONIDA_COMPILE_THREADS=4abc falls back to default"); + SetEnvironmentVariableA("LEONIDA_COMPILE_THREADS", nullptr); +} + +} // namespace + +int main() { + test_worker_count_config(); + test_parallel_demand(); + test_demand_lane_reserved(); + test_checkpoint_waits_for_idle(); + test_single_worker_strict_order(); + std::printf("pipeline_compiler: %s\n", g_failed ? "FAILED" : "all checks passed"); + return g_failed ? 1 : 0; +}