From abbc1a4f8276c1dd84b5885ace81aa5e7e577592 Mon Sep 17 00:00:00 2001 From: Krashgamer <64923888+Krashgamer@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:38:01 +0100 Subject: [PATCH 1/4] d3d12: rewrite a missed journal file from an empty cache when the seeded one kept its data (RT-JOURNAL-SEEDED-REWRITE) A compute pipeline that missed its journal file was rewritten from the cache made from that file. On NVIDIA (RTX 3080, 610.88) that cache came back exactly as loaded, without the pipeline just compiled, so the rewrite gave the same file and the same multi-second miss on every boot. After a seeded miss whose cache is still the loaded size, the pipeline is built once more against an empty cache (15-78 ms: the driver still has it) and that is written. A seeded cache that took the pipeline is rewritten as before. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_vulkan_device.cpp | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 94e53cfef8..ae300377c2 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10133,6 +10133,25 @@ class JournalCache { std::vector().swap(file_.data); // the driver has its own copy } + // In the build's driver turn, after a create against a seeded cache that missed. Measured on NVIDIA (RTX 3080, + // 610.88): such a cache can keep its data exactly as loaded, without the pipeline just compiled, so the rewrite + // gave the same file and the same miss on every boot. When the cache's data is still the loaded size, it is + // swapped for an empty one for one more create (the driver still has the pipeline: tens of milliseconds). + bool restart_empty() { + if (!cache || !seeded) return false; + const auto& vk = device_.context->vk; + std::size_t size = 0; + if (vk.vkGetPipelineCacheData(device_.info.device, cache, &size, nullptr) != VK_SUCCESS || size != file_.bytes) + return false; // it took the pipeline (or cannot say): rewritten from it as before + vk.vkDestroyPipelineCache(device_.info.device, cache, nullptr); + cache = VK_NULL_HANDLE; + seeded = false; + const VkPipelineCacheCreateInfo info{VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; + if (vk.vkCreatePipelineCache(device_.info.device, &info, nullptr, &cache) == VK_SUCCESS) return true; + cache = VK_NULL_HANDLE; + return false; + } + // In the build's driver turn, after the create against `cache`. `built`: that create made the pipeline; `hit`: // its creation feedback; `driver_ms`: the vkCreateComputePipelines call alone. void finish(bool built, int hit, std::uint64_t driver_ms) { @@ -10428,6 +10447,23 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn const ULONGLONG at = GetTickCount64(); created = vk.vkCreateComputePipelines(dev, cache, 1, &pipeline_info, nullptr, &built); driver_ms = GetTickCount64() - at; + // A seeded miss the cache did not take: built once more against an empty cache, so the file is rewritten + // with this pipeline instead of as it was read. + if (created == VK_SUCCESS && built && journal.seeded && + leonida::pipeline_journal::should_write(true, trace.hit(), driver_ms) && journal.restart_empty()) { + VkPipeline again = VK_NULL_HANDLE; + const ULONGLONG at_again = GetTickCount64(); + const VkResult again_result = vk.vkCreateComputePipelines(dev, journal.cache, 1, &pipeline_info, nullptr, &again); + const ULONGLONG again_ms = GetTickCount64() - at_again; + if (again_result == VK_SUCCESS && again) { + vk.vkDestroyPipeline(dev, built, nullptr); + built = again; + } + std::fprintf(stderr, "[rt] d3d12: pipeline journal: %s missed its journal file (%llu ms); built again " + "against an empty cache in %llu ms (VkResult %d) to rewrite it\n", + entry->name.c_str(), static_cast(driver_ms), + static_cast(again_ms), int(again_result)); + } } compiling.name.store(nullptr, std::memory_order_release); vk.vkDestroyShaderModule(dev, module, nullptr); // Vulkan lets a module go once the pipeline exists From 4212cf5ab3b133375af0f7baa723c38ea5896b17 Mon Sep 17 00:00:00 2001 From: Krashgamer <64923888+Krashgamer@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:50:34 +0100 Subject: [PATCH 2/4] d3d12: keep the journal file when the create against the empty cache fails Found by ecox982745 in review: a failed second create left an empty cache that finish() still wrote over the old file (36 bytes on NVIDIA). The first pipeline is kept and nothing is published on that path. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_vulkan_device.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index ae300377c2..72cf1f2cb6 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10405,6 +10405,7 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn auto& compiling = leonida::driver_compile; VkResult created = VK_ERROR_UNKNOWN; bool from_shared = false, probe_compiled = false; + bool journal_retry_failed = false; // the create against the empty cache failed: nothing to write ULONGLONG probe_ms = 0, driver_ms = 0; { // Every driver call of the build (shader module, journal cache, pipeline) in one turn through the compile @@ -10455,7 +10456,8 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn const ULONGLONG at_again = GetTickCount64(); const VkResult again_result = vk.vkCreateComputePipelines(dev, journal.cache, 1, &pipeline_info, nullptr, &again); const ULONGLONG again_ms = GetTickCount64() - at_again; - if (again_result == VK_SUCCESS && again) { + journal_retry_failed = again_result != VK_SUCCESS || !again; + if (!journal_retry_failed) { vk.vkDestroyPipeline(dev, built, nullptr); built = again; } @@ -10467,7 +10469,7 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn } compiling.name.store(nullptr, std::memory_order_release); vk.vkDestroyShaderModule(dev, module, nullptr); // Vulkan lets a module go once the pipeline exists - journal.finish(!from_shared && created == VK_SUCCESS && built, trace.hit(), driver_ms); + journal.finish(!from_shared && !journal_retry_failed && created == VK_SUCCESS && built, trace.hit(), driver_ms); } std::vector().swap(words); profile.finish(int(created)); From 807c3a50cd9649d6dd98d83cfa3690426866c63b Mon Sep 17 00:00:00 2001 From: Krashgamer <64923888+Krashgamer@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:12:40 +0100 Subject: [PATCH 3/4] d3d12: a helper-sized module's stale journal file goes to the helper, not a compile here (RT-JOURNAL-STALE-PROBE) The compile helper is asked only when a compute pipeline has no journal file. A file the driver no longer takes was seeded and compiled in the title's process: 55-84 s for library modules of 3.6-4.7 MB on an RTX 3080 (NVIDIA 610.88), with every submission waiting at the compile gate (up to 83 s, 580 s in a 900 s boot). For a module the helper would build (LEONIDA_COMPILE_HELPER, at least LEONIDA_COMPILE_HELPER_MIN_BYTES) with a journal file, the seeded create now carries VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT. On VK_PIPELINE_COMPILE_REQUIRED (0-16 ms) the file is removed and the build is asked once more without the probe: the no-file way, to the helper. A driver that compiles anyway gives the pipeline as before. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_vulkan_device.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 72cf1f2cb6..800ec6741a 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10265,6 +10265,7 @@ struct ComputeBuildOptions { std::uint64_t helper_priority = 0; // the request's urgency: 0 a dispatch, else the warm-up's module size bool mark_building = false; // precompile, helper: claim the entry, skip it when another process has it bool key_only = false; // only name the journal file (journal_path), build nothing + bool probe_journal = true; // a helper-sized module's file is asked without a compile (see below) }; // A compute pipeline from a library entry: what a PSO's first dispatch, the warm-up worker and gta6rt @@ -10386,6 +10387,13 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn big_lock.lock(); const bool ask_shared = journal.enabled() && !journal.has_file_data() && shared && device.pipeline_cache_seeded && device.info.pipeline_cache_control && device.journal_ask_shared.load(); + // A module the helper would build, with a journal file: the create from the file is asked not to compile. A file + // the driver does not take (written by another driver build, or before a rewrite fix) would otherwise be a compile + // here, minutes for the largest modules with every submission of the title waiting at the compile gate. On + // VK_PIPELINE_COMPILE_REQUIRED the file is removed and the build goes the no-file way: to the helper. + const bool probe_file = options.probe_journal && journal.has_file_data() && options.use_helper && + compile_helper::wanted(bytes) && device.info.pipeline_cache_control; + bool stale_file = false; VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; module_info.codeSize = bytes; @@ -10445,9 +10453,16 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn if (!from_shared) built = VK_NULL_HANDLE; } if (!from_shared) { + const bool probing = probe_file && journal.seeded; + if (probing) pipeline_info.flags |= VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT; const ULONGLONG at = GetTickCount64(); created = vk.vkCreateComputePipelines(dev, cache, 1, &pipeline_info, nullptr, &built); driver_ms = GetTickCount64() - at; + if (probing) { + pipeline_info.flags &= ~VkPipelineCreateFlags(VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT); + stale_file = created == VK_PIPELINE_COMPILE_REQUIRED; + if (stale_file) built = VK_NULL_HANDLE; + } // A seeded miss the cache did not take: built once more against an empty cache, so the file is rewritten // with this pipeline instead of as it was read. if (created == VK_SUCCESS && built && journal.seeded && @@ -10475,6 +10490,17 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn profile.finish(int(created)); trace.end(trigger, entry->name.c_str(), 0, bytes, created); if (big_lock.owns_lock()) big_lock.unlock(); + if (stale_file) { + std::error_code removed; + std::filesystem::remove(out.journal_path, removed); + std::fprintf(stderr, "[rt] d3d12: pipeline journal: %s: its journal file would have been a compile here " + "(%llu ms to say so); %s, and the build goes to the compile helper\n", + entry->name.c_str(), static_cast(driver_ms), + removed ? "the file could not be removed (built here as before)" : "the file is removed"); + ComputeBuildOptions again = options; // once: a file left in place is then seeded and compiled as before + again.probe_journal = false; + return build_compute_pipeline(device, compute_entry, layout, subgroup, again); + } if (probe_compiled && device.journal_ask_shared.exchange(false)) std::fprintf(stderr, "[rt] d3d12: pipeline journal: the driver compiled %s for " "FAIL_ON_PIPELINE_COMPILE_REQUIRED (%llu ms, no cache hit); built again for its journal " From 7982880cb15d8ce03743dee6c1ca2d7a303cee54 Mon Sep 17 00:00:00 2001 From: Krashgamer <64923888+Krashgamer@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:38:33 +0100 Subject: [PATCH 4/4] d3d12: a refused seed-overlay file is read as absent on the helper retry The seed overlay is read-only, so removing out.journal_path did nothing when the probed file came from it: the retry loaded the same seed, skipped the helper and compiled in the title's process. The retry now reads the seed as absent and leaves it untouched; a local file is still removed. Found by lixonik in review. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_vulkan_device.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 800ec6741a..0e4b4d4d7f 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10103,17 +10103,20 @@ class JournalCache { bool enabled() const { return enabled_; } bool has_file_data() const { return !file_.data.empty(); } + bool from_seed() const { return file_.from_seed; } // the data came from the read-only seed overlay bool found() const { return enabled_ && built_ && !write_; } // the pipeline came out of the journal file bool written() const { return written_; } // publish() wrote the file - void load(std::filesystem::path path) { + // `use_seed` false: the seed overlay's file is read as absent (one the driver already refused in this build). + void load(std::filesystem::path path, bool use_seed = true) { const auto& vk = device_.context->vk; if (!vk.vkCreatePipelineCache || !vk.vkDestroyPipelineCache || !vk.vkGetPipelineCacheData) return; enabled_ = true; path_ = std::move(path); namespace journal = leonida::pipeline_journal; const auto role = compile_helper::build_host.load() ? journal::ReadRole::materialize_local : journal::ReadRole::runtime; - file_ = journal::read_with_seed(path_, pipeline_journal_roots().seed, device_.pso_identity, role); + file_ = journal::read_with_seed(path_, use_seed ? pipeline_journal_roots().seed : std::filesystem::path{}, + device_.pso_identity, role); } // In the build's driver turn. @@ -10266,6 +10269,7 @@ struct ComputeBuildOptions { bool mark_building = false; // precompile, helper: claim the entry, skip it when another process has it bool key_only = false; // only name the journal file (journal_path), build nothing bool probe_journal = true; // a helper-sized module's file is asked without a compile (see below) + bool skip_seed = false; // the seed overlay's file was refused: read as absent, left untouched }; // A compute pipeline from a library entry: what a PSO's first dispatch, the warm-up worker and gta6rt @@ -10346,7 +10350,7 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn out.skipped = true; return out; } - journal.load(path); + journal.load(path, !options.skip_seed); // LOCAL FIX (compile helper, d3d12_compile_helper.h): no file, so this would be a compile in this process, // where the driver's lock it holds stops the title's submissions. The helper process compiles it into the file // instead, and the create below is a hit. Asked without waiting, the build answers deferred and is asked @@ -10364,7 +10368,7 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn } else { const ULONGLONG asked = GetTickCount64(); if (helper.build(path, entry->name, device.pso_identity, &why)) { - journal.load(path); + journal.load(path, !options.skip_seed); std::fprintf(stderr, "[rt] d3d12: compile helper built %s in %.1f s (%zu B SPIR-V, %s)\n", entry->name.c_str(), double(GetTickCount64() - asked) / 1000.0, bytes, trigger); } else { @@ -10394,6 +10398,7 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn const bool probe_file = options.probe_journal && journal.has_file_data() && options.use_helper && compile_helper::wanted(bytes) && device.info.pipeline_cache_control; bool stale_file = false; + bool stale_seed = false; // the refused file is the seed overlay's: it stays, and the retry reads it as absent VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; module_info.codeSize = bytes; @@ -10461,6 +10466,7 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn if (probing) { pipeline_info.flags &= ~VkPipelineCreateFlags(VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT); stale_file = created == VK_PIPELINE_COMPILE_REQUIRED; + stale_seed = stale_file && journal.from_seed(); if (stale_file) built = VK_NULL_HANDLE; } // A seeded miss the cache did not take: built once more against an empty cache, so the file is rewritten @@ -10491,14 +10497,19 @@ ComputeBuild build_compute_pipeline(DeviceState& device, const leonida::ShaderEn trace.end(trigger, entry->name.c_str(), 0, bytes, created); if (big_lock.owns_lock()) big_lock.unlock(); if (stale_file) { + // A local file is removed. The seed overlay is read-only and stays as it is; the retry reads it as absent, + // so the build goes the no-file way too and the helper's file is written locally. std::error_code removed; - std::filesystem::remove(out.journal_path, removed); + if (!stale_seed) std::filesystem::remove(out.journal_path, removed); std::fprintf(stderr, "[rt] d3d12: pipeline journal: %s: its journal file would have been a compile here " "(%llu ms to say so); %s, and the build goes to the compile helper\n", entry->name.c_str(), static_cast(driver_ms), - removed ? "the file could not be removed (built here as before)" : "the file is removed"); + stale_seed ? "the seed's file is left and read as absent" + : removed ? "the file could not be removed (built here as before)" + : "the file is removed"); ComputeBuildOptions again = options; // once: a file left in place is then seeded and compiled as before again.probe_journal = false; + again.skip_seed = stale_seed; return build_compute_pipeline(device, compute_entry, layout, subgroup, again); } if (probe_compiled && device.journal_ask_shared.exchange(false))