Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3606,6 +3606,16 @@ if(TARGET Vulkan::Headers)
target_compile_options(d3d12_compute_journal_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_compute_journal COMMAND d3d12_compute_journal_test)
set_tests_properties(d3d12_compute_journal PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
# A seeded miss whose cache kept its data as loaded (NVIDIA 610.88): the journal file is rewritten from a
# retry against an empty cache, also when the data came from the seed folder, and a failed retry leaves the
# file alone. Separate processes, so a hit is the journal file's and not the driver's memory.
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_FOUND)
add_test(NAME d3d12_compute_journal_seeded_miss COMMAND "${Python3_EXECUTABLE}" -I
"${CMAKE_CURRENT_SOURCE_DIR}/../scripts/check_compute_journal_seeded_miss.py"
$<TARGET_FILE:d3d12_compute_journal_test>)
set_tests_properties(d3d12_compute_journal_seeded_miss PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
endif()
endif()

# The cache vk_pipeline_check saves has to hit on the runtime's device (docs/pipeline_check.md): the test
Expand Down
43 changes: 42 additions & 1 deletion runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17492,6 +17492,7 @@ class JournalCache {
file_.data.empty() ? nullptr : file_.data.data()};
if (!file_.data.empty() && vk.vkCreatePipelineCache(device_.info.device, &info, nullptr, &cache) == VK_SUCCESS) {
seeded = true;
seeded_bytes_ = info.initialDataSize; // the payload the driver took: may be the seed's, or stamp-stripped
} else {
cache = VK_NULL_HANDLE; // no file, or one the driver declined: start empty, and the build rewrites it
info.initialDataSize = 0;
Expand All @@ -17501,6 +17502,25 @@ class JournalCache {
std::vector<char>().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 != seeded_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) {
Expand Down Expand Up @@ -17578,6 +17598,7 @@ class JournalCache {
leonida::SubmitMutex* submits_ = nullptr; // null: compute_driver_turn, for a build outside the submit lock
const char* trigger_ = "";
const char* name_;
std::size_t seeded_bytes_ = 0; // create(): bytes the seeded cache was made from (file_.bytes is the local file)
bool enabled_ = false;
bool built_ = false;
bool write_ = false;
Expand Down Expand Up @@ -17768,6 +17789,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
int hit_reported = -1; // what the journal sees as its hit, printed below to explain a missing write
ULONGLONG probe_ms = 0, driver_ms = 0;
{
Expand Down Expand Up @@ -17812,11 +17834,30 @@ 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<unsigned long long>(driver_ms),
static_cast<unsigned long long>(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
hit_reported = trace.hit(); // what finish() sees; decides whether the file is written
journal.finish(!from_shared && created == VK_SUCCESS && built, hit_reported, driver_ms);
journal.finish(!from_shared && !journal_retry_failed && created == VK_SUCCESS && built, hit_reported,
driver_ms);
}
std::vector<std::uint32_t>().swap(words);
profile.finish(int(created));
Expand Down
143 changes: 139 additions & 4 deletions runtime/test/d3d12_compute_journal_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,132 @@
#include "../shims/d3d12_com.h"
#include "../src/shader_library.h"

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <map>
#include <mutex>
#include <vector>

using namespace d3d12test;

#define CHECK(expr) do { if (!(expr)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #expr); return 1; } } while (0)

namespace {
// --keep-seeded-cache-data: a test-only layer between the runtime and the driver that makes every pipeline cache
// created from initial data answer vkGetPipelineCacheData with exactly that data, whatever was built against it.
// This is what NVIDIA 610.88 did on an RTX 3080 after a seeded miss (the cache kept its data as loaded, without the
// pipeline just compiled), so the journal file was rewritten unchanged and missed again on every boot. Other drivers
// grow the cache instead, so the layer makes that case reproducible on any GPU. Pipelines, dispatches and readbacks
// stay the real driver's. --fail-cache-retry also fails the first create against an empty cache made after a seeded
// cache was destroyed (the journal's retry against an empty cache) with VK_ERROR_OUT_OF_HOST_MEMORY.
struct SeededCacheLayer {
std::mutex mutex;
bool fail_retry = false;
PFN_vkGetInstanceProcAddr real_instance_proc = nullptr;
PFN_vkGetDeviceProcAddr real_device_proc = nullptr;
PFN_vkCreatePipelineCache real_create_cache = nullptr;
PFN_vkDestroyPipelineCache real_destroy_cache = nullptr;
PFN_vkGetPipelineCacheData real_cache_data = nullptr;
PFN_vkCreateComputePipelines real_create_compute = nullptr;
std::map<VkPipelineCache, std::vector<char>> loaded; // live caches made from initial data
bool seeded_destroyed = false;
VkPipelineCache retry_cache = VK_NULL_HANDLE; // the first empty cache made after a seeded one was destroyed
unsigned seeded_caches = 0, kept_answers = 0, failed_retries = 0;
};
SeededCacheLayer seeded_layer;

VKAPI_ATTR VkResult VKAPI_CALL layer_create_cache(VkDevice device, const VkPipelineCacheCreateInfo* info,
const VkAllocationCallbacks* allocator, VkPipelineCache* cache) {
const VkResult result = seeded_layer.real_create_cache(device, info, allocator, cache);
if (result != VK_SUCCESS) return result;
const std::lock_guard lock(seeded_layer.mutex);
if (info->initialDataSize && info->pInitialData) {
const auto* first = static_cast<const char*>(info->pInitialData);
seeded_layer.loaded[*cache].assign(first, first + info->initialDataSize);
++seeded_layer.seeded_caches;
} else if (seeded_layer.seeded_destroyed && seeded_layer.retry_cache == VK_NULL_HANDLE) {
seeded_layer.retry_cache = *cache;
}
return result;
}

VKAPI_ATTR void VKAPI_CALL layer_destroy_cache(VkDevice device, VkPipelineCache cache,
const VkAllocationCallbacks* allocator) {
{
const std::lock_guard lock(seeded_layer.mutex);
if (seeded_layer.loaded.erase(cache)) seeded_layer.seeded_destroyed = true;
}
seeded_layer.real_destroy_cache(device, cache, allocator);
}

VKAPI_ATTR VkResult VKAPI_CALL layer_cache_data(VkDevice device, VkPipelineCache cache, size_t* size, void* data) {
std::unique_lock lock(seeded_layer.mutex);
const auto it = seeded_layer.loaded.find(cache);
if (it == seeded_layer.loaded.end()) {
lock.unlock();
return seeded_layer.real_cache_data(device, cache, size, data);
}
++seeded_layer.kept_answers;
const auto& bytes = it->second;
if (!data) {
*size = bytes.size();
return VK_SUCCESS;
}
const size_t copied = std::min(*size, bytes.size());
std::memcpy(data, bytes.data(), copied);
*size = copied;
return copied < bytes.size() ? VK_INCOMPLETE : VK_SUCCESS;
}

VKAPI_ATTR VkResult VKAPI_CALL layer_create_compute(VkDevice device, VkPipelineCache cache, uint32_t count,
const VkComputePipelineCreateInfo* infos,
const VkAllocationCallbacks* allocator, VkPipeline* pipelines) {
{
const std::lock_guard lock(seeded_layer.mutex);
if (seeded_layer.fail_retry && cache != VK_NULL_HANDLE && cache == seeded_layer.retry_cache) {
std::fill(pipelines, pipelines + count, VkPipeline(VK_NULL_HANDLE));
++seeded_layer.failed_retries;
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
}
return seeded_layer.real_create_compute(device, cache, count, infos, allocator, pipelines);
}

PFN_vkVoidFunction layer_function(const char* name) {
if (std::strcmp(name, "vkCreatePipelineCache") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&layer_create_cache);
if (std::strcmp(name, "vkDestroyPipelineCache") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&layer_destroy_cache);
if (std::strcmp(name, "vkGetPipelineCacheData") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&layer_cache_data);
if (std::strcmp(name, "vkCreateComputePipelines") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&layer_create_compute);
return nullptr;
}

VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL layer_device_proc(VkDevice device, const char* name) {
if (const auto own = layer_function(name)) return own;
return seeded_layer.real_device_proc(device, name);
}

VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL layer_instance_proc(VkInstance instance, const char* name) {
if (std::strcmp(name, "vkGetDeviceProcAddr") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&layer_device_proc);
if (const auto own = layer_function(name)) return own;
return seeded_layer.real_instance_proc(instance, name);
}

// The real entry points, looked up once before the runtime sees the device.
bool install_layer(PFN_vkGetInstanceProcAddr instance_proc, VkInstance instance, VkDevice device) {
seeded_layer.real_instance_proc = instance_proc;
seeded_layer.real_device_proc = reinterpret_cast<PFN_vkGetDeviceProcAddr>(instance_proc(instance, "vkGetDeviceProcAddr"));
if (!seeded_layer.real_device_proc) return false;
const auto get = [&](const char* name) { return seeded_layer.real_device_proc(device, name); };
seeded_layer.real_create_cache = reinterpret_cast<PFN_vkCreatePipelineCache>(get("vkCreatePipelineCache"));
seeded_layer.real_destroy_cache = reinterpret_cast<PFN_vkDestroyPipelineCache>(get("vkDestroyPipelineCache"));
seeded_layer.real_cache_data = reinterpret_cast<PFN_vkGetPipelineCacheData>(get("vkGetPipelineCacheData"));
seeded_layer.real_create_compute = reinterpret_cast<PFN_vkCreateComputePipelines>(get("vkCreateComputePipelines"));
return seeded_layer.real_create_cache && seeded_layer.real_destroy_cache && seeded_layer.real_cache_data && seeded_layer.real_create_compute;
}

int dispatch_once(ID3D12Device* dev, const std::vector<std::uint8_t>& container, UINT expected_sum) {
D3D12_ROOT_PARAMETER param{};
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;
Expand Down Expand Up @@ -96,10 +212,20 @@ int dispatch_once(ID3D12Device* dev, const std::vector<std::uint8_t>& container,
} // namespace

int main(int argc, char** argv) {
const bool second = argc == 2 && std::strcmp(argv[1], "--second-shader") == 0;
if (argc != 1 && !second) {
std::fputs("usage: d3d12_compute_journal_test [--second-shader]\n", stderr);
return 2;
bool second = false, keep_seeded = false;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--second-shader") == 0) {
second = true;
} else if (std::strcmp(argv[i], "--keep-seeded-cache-data") == 0) {
keep_seeded = true;
} else if (std::strcmp(argv[i], "--fail-cache-retry") == 0) {
keep_seeded = true;
seeded_layer.fail_retry = true;
} else {
std::fputs("usage: d3d12_compute_journal_test [--second-shader] [--keep-seeded-cache-data] "
"[--fail-cache-retry]\n", stderr);
return 2;
}
}
// One additional constant per group in the second module: (1+2+3+4) versus (2+3+4+5).
const UINT expected_sum = second ? 14u : 10u;
Expand Down Expand Up @@ -131,6 +257,10 @@ int main(int argc, char** argv) {
CHECK(index);
rt::d3d12::VulkanDeviceInfo info{};
info.get_instance_proc = vk.instance_proc;
if (keep_seeded) {
CHECK(install_layer(vk.instance_proc, vk.instance, vk.device));
info.get_instance_proc = &layer_instance_proc;
}
info.instance = vk.instance;
info.physical = vk.physical;
info.device = vk.device;
Expand All @@ -148,6 +278,11 @@ int main(int argc, char** argv) {
CHECK(dispatch_once(dev.p, container, expected_sum) == 0);
}
CHECK(vktest::validation_error_count() == 0); // includes device teardown
if (keep_seeded) {
// Read by scripts/check_compute_journal_seeded_miss.py: proof that the layer was in the path.
std::printf("seeded cache layer: %u seeded cache(s), %u kept-data answer(s), %u failed retry create(s)\n",
seeded_layer.seeded_caches, seeded_layer.kept_answers, seeded_layer.failed_retries);
}
std::printf("compute journal: direct Dispatch(4,1,1) copied both expected words (4,%u)\n", expected_sum);
return 0;
}
Loading
Loading