diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 94e53cfef8..72cf1f2cb6 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) { @@ -10386,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 @@ -10428,10 +10448,28 @@ 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; + journal_retry_failed = again_result != VK_SUCCESS || !again; + if (!journal_retry_failed) { + 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 - 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));