From 6f9ec5c4f9f16ac614d08094f8d757943d01ed5e Mon Sep 17 00:00:00 2001 From: Thatters <69167449+Thatters@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:21:49 -0500 Subject: [PATCH] pipeline_cache_snapshot: read into a buffer with headroom so a busy cache doesn't fail every checkpoint vk_pipeline_check saves its pipeline cache every 10 minutes while workers keep compiling. The snapshot queried the size and then read into a buffer of exactly that size, so any pipeline added between the two calls made the read VK_INCOMPLETE. On a large, busy cache that can happen four times in a row, and the checkpoint is lost: periodic pipeline cache save to ...\pipeline_cache.bin failed: vkGetPipelineCacheData returned VK_INCOMPLETE on four attempts (seen during a full `--jobs 12` pipelines stage, ~4 GB cache, RTX 5070 Ti / 621.24). A crash later in the stage, as in #884, then loses more work than it needs to. The read buffer now has room to spare: 1/16 of the queried size on the first attempt, doubling on each retry (1/8, 1/4, 1/2). A larger buffer is valid for vkGetPipelineCacheData: the driver writes the whole cache and reports the bytes written, and the snapshot is trimmed to that. A cache that still outgrows the last attempt ends in the same error as before, and the byte-count checks are unchanged (more than the buffer given is still rejected). pipeline_cache_snapshot_test: the scripted driver is now a cache that grows on every call, as a busy one does. New cases: growth under 1/16 succeeds in one attempt, growth past the first headroom succeeds on the retry, and a cache that doubles between calls still ends in the four-attempt error. With the old header the first case fails (the four VK_INCOMPLETEs this fixes); a failing case is now reported by CHECK instead of an uncaught exception. Tested on Windows 11 Pro 25H2, MSVC 14.51 + Ninja, RTX 5070 Ti / 621.24: pipeline_cache_snapshot_test passes (fails at the new busy-cache case with the old header); vk_pipeline_check on 300 real modules from my own copy's work folder wrote a 28.4 MB cache through the new path, and a second run loaded it (64 s, then 1 s). No warnings. No game data. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- runtime/test/pipeline_cache_snapshot.h | 7 +- runtime/test/pipeline_cache_snapshot_test.cpp | 79 ++++++++++++++++--- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/runtime/test/pipeline_cache_snapshot.h b/runtime/test/pipeline_cache_snapshot.h index 3490e0e3d..c5ef9cb6f 100644 --- a/runtime/test/pipeline_cache_snapshot.h +++ b/runtime/test/pipeline_cache_snapshot.h @@ -17,8 +17,13 @@ std::vector pipeline_cache_snapshot(GetData get) { if (size < sizeof(VkPipelineCacheHeaderVersionOne)) throw std::runtime_error("cache size is smaller than its header"); + // Workers keep adding pipelines between the size query and the read, so read into a buffer with room to + // spare: 1/16 of the queried size on the first attempt, doubling on each retry (1/8, 1/4, 1/2). A buffer + // larger than the data is fine; the driver writes the whole cache and reports the bytes it wrote. + const size_t capacity = size + (size >> (4 - attempt)); // Destroy each incomplete snapshot before allocating its replacement. - std::vector data(size); + std::vector data(capacity); + size = capacity; result = get(&size, data.data()); if (result == VK_INCOMPLETE) continue; if (result != VK_SUCCESS) diff --git a/runtime/test/pipeline_cache_snapshot_test.cpp b/runtime/test/pipeline_cache_snapshot_test.cpp index 72c40ea0c..f81d655b7 100644 --- a/runtime/test/pipeline_cache_snapshot_test.cpp +++ b/runtime/test/pipeline_cache_snapshot_test.cpp @@ -11,24 +11,83 @@ } \ } while (0) -int main() { +namespace { + +// A cache that workers keep growing: every call (size query or read) first adds `growth` bytes, or doubles the +// cache when `growth` is 0. A read into a buffer smaller than the cache writes a prefix and returns VK_INCOMPLETE; +// otherwise it writes the whole cache and reports its size, as vkGetPipelineCacheData does. +struct BusyCache { + size_t bytes, growth; int queries = 0, reads = 0; - const auto growing = [&](size_t* size, void* data) { + + VkResult operator()(size_t* size, void* data) { + bytes += growth ? growth : bytes; if (!data) { - *size = (++queries == 1) ? 32 : 64; + ++queries; + *size = bytes; return VK_SUCCESS; } - if (++reads == 1) { + ++reads; + if (*size < bytes) { std::memset(data, 'p', *size); return VK_INCOMPLETE; } - if (*size != 64) return VK_ERROR_UNKNOWN; - *size = 48; // A successful read can return less than the size query. + *size = bytes; std::memset(data, 's', *size); return VK_SUCCESS; - }; - CHECK(pipeline_cache_snapshot(growing) == std::vector(48, 's')); - CHECK(queries == 2 && reads == 2); + } +}; + +// The snapshot, or an empty vector if it threw, so a failing case is reported by CHECK instead of ending the test. +std::vector snapshot(BusyCache& cache) { + try { + return pipeline_cache_snapshot([&](size_t* size, void* d) { return cache(size, d); }); + } catch (const std::exception&) { + return {}; + } +} + +} // namespace + +int main() { + // A busy cache that grows less than 1/16 between the size query and the read: one attempt is enough. Reading + // into exactly the queried size returned VK_INCOMPLETE here on every attempt. + { + BusyCache cache{4096, 128}; + CHECK(snapshot(cache) == std::vector(4096 + 2 * 128, 's')); + CHECK(cache.queries == 1 && cache.reads == 1); + } + + // Growth past the first attempt's headroom: the retry asks for more room and succeeds. + { + BusyCache cache{4096, 400}; + CHECK(snapshot(cache) == std::vector(4096 + 4 * 400, 's')); + CHECK(cache.queries == 2 && cache.reads == 2); + } + + // A cache that doubles between every call outgrows even the last attempt's 1/2 headroom: the four-attempt error. + { + BusyCache cache{4096, 0}; + CHECK(snapshot(cache).empty() && cache.queries == 4 && cache.reads == 4); + } + + // A successful read can return less than the size query. + { + int queries = 0, reads = 0; + const auto shrinking = [&](size_t* size, void* data) { + if (!data) { + ++queries; + *size = 64; + return VK_SUCCESS; + } + ++reads; + *size = 48; + std::memset(data, 's', *size); + return VK_SUCCESS; + }; + CHECK(pipeline_cache_snapshot(shrinking) == std::vector(48, 's')); + CHECK(queries == 1 && reads == 1); + } auto rejected = [](VkResult query_result, size_t available, VkResult read_result, size_t written) { int calls = 0; @@ -50,7 +109,7 @@ int main() { CHECK(rejected(VK_SUCCESS, 32, VK_ERROR_DEVICE_LOST, 32) == 2); CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 0) == 2); CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 16) == 2); - CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 33) == 2); + CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 32 + 32 / 16 + 1) == 2); // more than the buffer it was given CHECK(rejected(VK_SUCCESS, 32, VK_INCOMPLETE, 32) == 8); return 0; }