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/2] 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/2] 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));