From 76c69bd718fef3e723c462a1d4762bb7642b23cc Mon Sep 17 00:00:00 2001 From: qhukz13 Date: Thu, 26 Nov 2026 16:38:24 +0200 Subject: [PATCH] d3d12: one compile job per pipeline at a time, so a released PSO is not deleted under its other build PipelineCompiler runs a general worker and a graphics-only worker, and next_locked previously ignored Job::owner, allowing two jobs for the same graphics PSO to run concurrently. PipelineState::last_release calls after_running(owner, ...) to defer deletion, which attaches to the first Running entry of that owner; when that worker finishes, it can delete the PSO while the other worker is still building another variant on the same object. Skip queued jobs in next_locked when their owner already has an unfinished Running entry (for both workers), and call wake_.notify_all() in run() after erasing a finished job from running_ so held-back jobs can start. Add standalone CPU test d3d12_compile_owner_test. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 9 + runtime/shims/d3d12_pipeline_compile.h | 15 +- runtime/test/d3d12_compile_owner_test.cpp | 347 ++++++++++++++++++++++ 3 files changed, 367 insertions(+), 4 deletions(-) create mode 100644 runtime/test/d3d12_compile_owner_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 10ee582b12..720bf87b23 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4633,6 +4633,15 @@ if(TARGET Vulkan::Headers) target_compile_definitions(geometry_views_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) target_compile_options(geometry_views_test PRIVATE /W4 /permissive- /EHsc /utf-8) add_test(NAME geometry_views COMMAND geometry_views_test) + # PipelineCompiler (shims/d3d12_pipeline_compile.h): one running job per owner across both workers. + add_executable(d3d12_compile_owner_test test/d3d12_compile_owner_test.cpp) + target_link_libraries(d3d12_compile_owner_test PRIVATE Vulkan::Headers) + target_compile_definitions(d3d12_compile_owner_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + if(MSVC) + target_compile_options(d3d12_compile_owner_test PRIVATE /W4 /permissive- /EHsc /utf-8) + endif() + add_test(NAME d3d12_compile_owner COMMAND d3d12_compile_owner_test) + set_tests_properties(d3d12_compile_owner PROPERTIES TIMEOUT 60) endif() endif() else() diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index 2889d7fd36..b929e1544b 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -199,7 +199,8 @@ class DriverTurn { // VkDevice crashed). // - A PSO's last Release does not wait for its running build, which can be a dispatch-worker build of a module the // driver takes minutes over: after_running() drops its queued jobs and hands the PSO's deletion to the worker, -// to run when that build returns (PipelineState::last_release). The PSO lets its device reference go at once. +// to run when that build returns (PipelineState::last_release). One job per owner runs at a time, so that +// deletion never overlaps another build of the same PSO. The PSO lets its device reference go at once. // - A device's teardown waits: cancel() drops its queued jobs (by owner or device) and waits for its running one // and what runs after it, so an in-flight vkCreate*Pipelines finishes before the device's objects go. // - The worker thread runs while a device is alive (retain/release): the last device's release stops and joins it. @@ -419,10 +420,15 @@ class PipelineCompiler { } // The first job this worker may take, most urgent first: any job for the general worker, a graphics job for the - // graphics one. + // graphics one, skipping any job whose owner already has a running build. One job per owner runs at a time, so a + // PSO's deletion after its running build (after_running) never overlaps another build of it. std::multimap::iterator next_locked(bool graphics_only) { - if (!graphics_only) return jobs_.begin(); - return std::find_if(jobs_.begin(), jobs_.end(), [](const auto& entry) { return entry.second.graphics; }); + return std::find_if(jobs_.begin(), jobs_.end(), [this, graphics_only](const auto& entry) { + if (graphics_only && !entry.second.graphics) return false; + const void* owner = entry.second.owner; + // A finished entry may still be running what comes after its job (the PSO's deletion): it counts too. + return std::none_of(running_.begin(), running_.end(), [owner](const Running& r) { return r.owner == owner; }); + }); } void run(std::uint64_t generation, bool graphics_only) { @@ -462,6 +468,7 @@ class PipelineCompiler { running_.erase(std::find_if(running_.begin(), running_.end(), [me](const Running& r) { return r.thread == me; })); + wake_.notify_all(); done_.notify_all(); } } diff --git a/runtime/test/d3d12_compile_owner_test.cpp b/runtime/test/d3d12_compile_owner_test.cpp new file mode 100644 index 0000000000..80df8fa0f4 --- /dev/null +++ b/runtime/test/d3d12_compile_owner_test.cpp @@ -0,0 +1,347 @@ +// PipelineCompiler (shims/d3d12_pipeline_compile.h): one job per owner runs at a time across the general and +// graphics-only workers, so after_running() never deletes a PSO while another build of it runs on the second worker. +// Windows only, no GPU, no Vulkan device, no game data. +#include +#include "../shims/d3d12_pipeline_compile.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ +} while (0) + +namespace { + +using rt::d3d12::PipelineCompiler; + +constexpr auto kTimeout = std::chrono::seconds(5); + +struct Event { + std::mutex mutex; + std::condition_variable cv; + bool signaled = false; + + void set() { + { + std::lock_guard lock(mutex); + signaled = true; + } + cv.notify_all(); + } + + bool wait_for(std::chrono::milliseconds timeout) { + std::unique_lock lock(mutex); + return cv.wait_for(lock, timeout, [this] { return signaled; }); + } +}; + +// Two graphics jobs for owner A: while the first is running, the second stays queued (the second worker skips it) +// and after_running drops the queued second job and runs its callback once after the first finishes, with no job of +// A running concurrently. +int same_owner_graphics_after_running(PipelineCompiler& compiler) { + struct State { + Event job1_started; + Event job1_release; + Event job1_done; + Event job2_started_ev; + Event last_done; + std::atomic active_a{0}; + std::atomic job2_started{false}; + std::atomic job2_running{false}; + std::atomic last_runs{0}; + std::atomic last_saw_job2_started{false}; + std::atomic last_saw_job2_running{false}; + std::atomic last_saw_active_a{false}; + }; + + auto s = std::make_shared(); + struct Guard { + std::shared_ptr state; + ~Guard() { state->job1_release.set(); } + } guard{s}; + + int owner_a = 0; + int device = 0; + + EXPECT(compiler.submit( + &owner_a, &device, 0, + [s] { + s->active_a.fetch_add(1, std::memory_order_acq_rel); + s->job1_started.set(); + s->job1_release.wait_for(kTimeout); + s->active_a.fetch_sub(1, std::memory_order_acq_rel); + s->job1_done.set(); + }, + true)); + + EXPECT(s->job1_started.wait_for(kTimeout)); + const std::uint64_t dropped_before = compiler.stats().dropped; + + EXPECT(compiler.submit( + &owner_a, &device, 1, + [s] { + s->active_a.fetch_add(1, std::memory_order_acq_rel); + s->job2_running.store(true, std::memory_order_release); + s->job2_started.store(true, std::memory_order_release); + s->job2_started_ev.set(); + s->job1_release.wait_for(std::chrono::milliseconds(50)); + s->job2_running.store(false, std::memory_order_release); + s->active_a.fetch_sub(1, std::memory_order_acq_rel); + }, + true)); + + // Hold job 1 for 200 ms: job 2 must not start on the other worker. + const bool job2_started_early = s->job2_started_ev.wait_for(std::chrono::milliseconds(200)); + EXPECT(!job2_started_early); + EXPECT(!s->job2_started.load(std::memory_order_acquire)); + + const bool deferred = compiler.after_running(&owner_a, [s] { + s->last_runs.fetch_add(1, std::memory_order_acq_rel); + if (s->job2_started.load(std::memory_order_acquire)) + s->last_saw_job2_started.store(true, std::memory_order_release); + if (s->job2_running.load(std::memory_order_acquire)) + s->last_saw_job2_running.store(true, std::memory_order_release); + if (s->active_a.load(std::memory_order_acquire) != 0) + s->last_saw_active_a.store(true, std::memory_order_release); + s->last_done.set(); + }); + EXPECT(deferred); + + s->job1_release.set(); + EXPECT(s->job1_done.wait_for(kTimeout)); + EXPECT(s->last_done.wait_for(kTimeout)); + compiler.wait_idle(); + + EXPECT(s->last_runs.load(std::memory_order_acquire) == 1); + EXPECT(!s->last_saw_active_a.load(std::memory_order_acquire)); + EXPECT(!s->last_saw_job2_started.load(std::memory_order_acquire)); + EXPECT(!s->last_saw_job2_running.load(std::memory_order_acquire)); + EXPECT(!s->job2_started.load(std::memory_order_acquire)); + EXPECT(compiler.stats().dropped == dropped_before + 1); + std::printf("same_owner_graphics_after_running: passed\n"); + return 0; +} + +// Graphics jobs of different owners still run concurrently across both workers. +int different_owners_graphics_concurrent(PipelineCompiler& compiler) { + struct State { + Event a_started; + Event a_release; + Event a_done; + Event b_started; + Event b_done; + std::atomic a_released{false}; + std::atomic a_held_when_b_started{false}; + }; + + auto s = std::make_shared(); + struct Guard { + std::shared_ptr state; + ~Guard() { state->a_release.set(); } + } guard{s}; + + int owner_a = 0; + int owner_b = 0; + int device = 0; + + EXPECT(compiler.submit( + &owner_a, &device, 0, + [s] { + s->a_started.set(); + s->a_release.wait_for(kTimeout); + s->a_done.set(); + }, + true)); + + EXPECT(s->a_started.wait_for(kTimeout)); + + EXPECT(compiler.submit( + &owner_b, &device, 0, + [s] { + s->a_held_when_b_started.store(!s->a_released.load(std::memory_order_acquire), std::memory_order_release); + s->b_started.set(); + s->b_done.set(); + }, + true)); + + EXPECT(s->b_started.wait_for(kTimeout)); + EXPECT(s->a_held_when_b_started.load(std::memory_order_acquire)); + + s->a_released.store(true, std::memory_order_release); + s->a_release.set(); + EXPECT(s->a_done.wait_for(kTimeout)); + EXPECT(s->b_done.wait_for(kTimeout)); + compiler.wait_idle(); + std::printf("different_owners_graphics_concurrent: passed\n"); + return 0; +} + +// Two compute (non-graphics) jobs of the same owner run in priority order without overlap. +int same_owner_compute_serial(PipelineCompiler& compiler) { + struct State { + Event job1_started; + Event job1_release; + Event job1_done; + Event job2_started; + Event job2_done; + std::mutex order_mutex; + std::vector order; + std::atomic active{0}; + std::atomic overlapped{false}; + }; + + auto s = std::make_shared(); + struct Guard { + std::shared_ptr state; + ~Guard() { state->job1_release.set(); } + } guard{s}; + + int owner_c = 0; + int device = 0; + + EXPECT(compiler.submit( + &owner_c, &device, 0, + [s] { + if (s->active.fetch_add(1, std::memory_order_acq_rel) != 0) + s->overlapped.store(true, std::memory_order_release); + { + std::lock_guard lock(s->order_mutex); + s->order.push_back(1); + } + s->job1_started.set(); + s->job1_release.wait_for(kTimeout); + s->active.fetch_sub(1, std::memory_order_acq_rel); + s->job1_done.set(); + }, + false)); + + EXPECT(s->job1_started.wait_for(kTimeout)); + + EXPECT(compiler.submit( + &owner_c, &device, 1, + [s] { + if (s->active.fetch_add(1, std::memory_order_acq_rel) != 0) + s->overlapped.store(true, std::memory_order_release); + { + std::lock_guard lock(s->order_mutex); + s->order.push_back(2); + } + s->job2_started.set(); + s->active.fetch_sub(1, std::memory_order_acq_rel); + s->job2_done.set(); + }, + false)); + + EXPECT(!s->job2_started.wait_for(std::chrono::milliseconds(50))); + + s->job1_release.set(); + EXPECT(s->job1_done.wait_for(kTimeout)); + EXPECT(s->job2_done.wait_for(kTimeout)); + compiler.wait_idle(); + + EXPECT(!s->overlapped.load(std::memory_order_acquire)); + EXPECT(s->order.size() == 2 && s->order[0] == 1 && s->order[1] == 2); + std::printf("same_owner_compute_serial: passed\n"); + return 0; +} + +// A non-graphics job held back by a same-owner graphics job on the graphics worker wakes and runs once that finishes. +int held_back_job_wakes_on_finish(PipelineCompiler& compiler) { + struct State { + Event x_started; + Event x_release; + Event x_done; + Event d_gfx_started; + Event d_gfx_release; + Event d_gfx_done; + Event d_cs_started; + Event d_cs_done; + std::atomic active_d{0}; + std::atomic overlapped{false}; + }; + + auto s = std::make_shared(); + struct Guard { + std::shared_ptr state; + ~Guard() { + state->x_release.set(); + state->d_gfx_release.set(); + } + } guard{s}; + + int owner_x = 0; + int owner_d = 0; + int device = 0; + + // Occupy the general worker so D's graphics job runs on the graphics-only worker. + EXPECT(compiler.submit( + &owner_x, &device, 0, + [s] { + s->x_started.set(); + s->x_release.wait_for(kTimeout); + s->x_done.set(); + }, + false)); + EXPECT(s->x_started.wait_for(kTimeout)); + + EXPECT(compiler.submit( + &owner_d, &device, 0, + [s] { + if (s->active_d.fetch_add(1, std::memory_order_acq_rel) != 0) + s->overlapped.store(true, std::memory_order_release); + s->d_gfx_started.set(); + s->d_gfx_release.wait_for(kTimeout); + s->active_d.fetch_sub(1, std::memory_order_acq_rel); + s->d_gfx_done.set(); + }, + true)); + EXPECT(s->d_gfx_started.wait_for(kTimeout)); + + // Free the general worker, then queue a compute job for D that only the general worker can take. + s->x_release.set(); + EXPECT(s->x_done.wait_for(kTimeout)); + + EXPECT(compiler.submit( + &owner_d, &device, 1, + [s] { + if (s->active_d.fetch_add(1, std::memory_order_acq_rel) != 0) + s->overlapped.store(true, std::memory_order_release); + s->d_cs_started.set(); + s->active_d.fetch_sub(1, std::memory_order_acq_rel); + s->d_cs_done.set(); + }, + false)); + EXPECT(!s->d_cs_started.wait_for(std::chrono::milliseconds(50))); + + s->d_gfx_release.set(); + EXPECT(s->d_gfx_done.wait_for(kTimeout)); + EXPECT(s->d_cs_done.wait_for(kTimeout)); + compiler.wait_idle(); + + EXPECT(!s->overlapped.load(std::memory_order_acquire)); + std::printf("held_back_job_wakes_on_finish: passed\n"); + return 0; +} + +} // namespace + +int main() { + std::setvbuf(stdout, nullptr, _IONBF, 0); + auto& compiler = PipelineCompiler::instance(); + compiler.retain(); + if (same_owner_graphics_after_running(compiler) || different_owners_graphics_concurrent(compiler) || + same_owner_compute_serial(compiler) || held_back_job_wakes_on_finish(compiler)) { + compiler.stop_for_exit(kTimeout); + return 1; + } + compiler.release(); + std::printf("d3d12_compile_owner_test: all passed\n"); + return 0; +}