diff --git a/docs/pipeline_check.md b/docs/pipeline_check.md index d3d7490ec..5f39223f2 100644 --- a/docs/pipeline_check.md +++ b/docs/pipeline_check.md @@ -434,6 +434,24 @@ On Windows with an RTX 5090 (driver 621.14), a synthetic three-compute, one-vert sample reported zero application-cache hits on its first run and five on the seeded run. On Mesa llvmpipe the same five pipelines succeeded both times and neither run reported a hit. +### The cache and the runtime's device + +The runtime seeds its `VkPipelineCache` from the cache this tool saves, so a pipeline hits there only when the +driver gives it the same cache key in both programs. Measured on Windows with an RX 9070 XT (driver +32.0.32029.2010) and one synthetic compute shader, reading the application-cache-hit bit: + +- `robustBufferAccess`, `scalarBlockLayout` and `robustImageAccess` are each part of the key. The runtime's + device enables none of them, so the checker's does not either. Nothing else that differs between the two + devices changed the key: the checker's other features, the runtime's extensions, the application name. +- The executable's file name is part of the key; its folder is not. A copy of the checker under another name + misses the cache the original wrote, and the runtime's device hits the checker's cache only from a program + with the checker's file name. So on this driver a cache written by `vk_pipeline_check.exe` does not hit in + `gta6rt.exe`, and one written by a copy of the checker named `gta6rt.exe` does. + +The `pipeline_cache_seed` test runs the checker, then creates the same pipeline on the runtime's device from +the saved cache; the checker runs as a copy with the test program's file name. It is skipped where the driver +reports no hit even for a cache the device wrote itself, as llvmpipe does. + ## Wave64-emulated corpus (`--wave64-emulate`) A GPU that only runs 32-wide subgroups (NVIDIA, Intel) needs the corpus translated with diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index b80132074..36dd3b51b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1857,6 +1857,20 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_indirect_dispatch_count_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_indirect_dispatch_count COMMAND d3d12_indirect_dispatch_count_test) set_tests_properties(d3d12_indirect_dispatch_count PROPERTIES SKIP_RETURN_CODE 77) + # The cache vk_pipeline_check saves has to hit on the runtime's device (docs/pipeline_check.md): the checker + # compiles that shader, then the test creates the same pipeline on the runtime's device from the cache. + add_executable(pipeline_cache_seed_test test/pipeline_cache_seed_test.cpp) + target_link_libraries(pipeline_cache_seed_test PRIVATE gta6rt_d3d12_com) + target_compile_definitions(pipeline_cache_seed_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(pipeline_cache_seed_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME pipeline_cache_seed COMMAND "${CMAKE_COMMAND}" + "-DCHECKER=$" + "-DTEST=$" + "-DSHADER=${_indirect_count_spv}" + "-DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/pipeline_cache_seed" + "-DRUNNER=${CMAKE_CROSSCOMPILING_EMULATOR}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/test/check_pipeline_cache_seed.cmake") + set_tests_properties(pipeline_cache_seed PROPERTIES SKIP_REGULAR_EXPRESSION "SKIP: ") endif() # What Close returns after one recorded call, against WARP; today's differences are listed in the test. diff --git a/runtime/test/check_pipeline_cache_seed.cmake b/runtime/test/check_pipeline_cache_seed.cmake new file mode 100644 index 000000000..89a3c2ab6 --- /dev/null +++ b/runtime/test/check_pipeline_cache_seed.cmake @@ -0,0 +1,33 @@ +# The cache vk_pipeline_check saves must hit on the runtime's device: the checker compiles one synthetic compute +# shader into a cache, then pipeline_cache_seed_test creates the same pipeline on the runtime's device from it. +# +# The checker runs as a copy with the test program's file name: a driver can make the executable's file name part of +# the cache key (docs/pipeline_check.md, "The cache and the runtime's device"), and this test is about the device. +file(REMOVE_RECURSE "${TEST_DIR}") +file(MAKE_DIRECTORY "${TEST_DIR}/checker") +file(COPY_FILE "${SHADER}" "${TEST_DIR}/seed.spv") +file(WRITE "${TEST_DIR}/index.json" "{\"seed\":{\"stage\":\"cs\",\"bindings\":[],\"push_constant_bytes\":144," + "\"local_size\":[1,1,1],\"wave64\":false}}\n") +get_filename_component(test_name "${TEST}" NAME) +set(checker "${TEST_DIR}/checker/${test_name}") +file(COPY_FILE "${CHECKER}" "${checker}") + +execute_process(COMMAND ${RUNNER} "${checker}" "${TEST_DIR}" "${TEST_DIR}/pipeline_cache.bin" --jobs 1 + RESULT_VARIABLE code OUTPUT_VARIABLE log ERROR_VARIABLE log TIMEOUT 120) +if(NOT log MATCHES "cs pipelines: 1 ok, 0 failed, 0 skipped" OR NOT EXISTS "${TEST_DIR}/pipeline_cache.bin") + if(log MATCHES "no Vulkan physical device|vulkan-1|libvulkan|cs pipelines: 0 ok, 0 failed, 1 skipped") + message(STATUS "SKIP: vk_pipeline_check built no pipeline here\n${log}") + return() + endif() + message(FATAL_ERROR "vk_pipeline_check did not build the pipeline (${code})\n${log}") +endif() + +execute_process(COMMAND ${RUNNER} "${TEST}" "${TEST_DIR}/seed.spv" "${TEST_DIR}/pipeline_cache.bin" + RESULT_VARIABLE code OUTPUT_VARIABLE log ERROR_VARIABLE log TIMEOUT 120) +message(STATUS "${log}") +if(code STREQUAL "77") + return() # the test printed its own SKIP line +endif() +if(NOT code STREQUAL "0") + message(FATAL_ERROR "pipeline_cache_seed_test failed (${code})") +endif() diff --git a/runtime/test/pipeline_cache_seed_test.cpp b/runtime/test/pipeline_cache_seed_test.cpp new file mode 100644 index 000000000..2254257b7 --- /dev/null +++ b/runtime/test/pipeline_cache_seed_test.cpp @@ -0,0 +1,170 @@ +// The cache vk_pipeline_check saves has to hit on the runtime's device: +// pipeline_cache_seed_test +// +// Creates the runtime's own device (create_vulkan_device_standalone), seeds a VkPipelineCache from the file and +// creates the compute pipeline of the way PipelineState::create_compute does for a shader without set-0 +// bindings: no set layouts, one 144-byte compute push-constant range and the subgroup request of src/subgroup_size.h. +// It passes when the driver reports VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT for that creation. +// +// Exit 77 (skipped): no device, a cache written for another GPU or driver, or a driver that reports no hit even for +// a cache this device wrote itself a moment before. Run by test/check_pipeline_cache_seed.cmake after the checker. +#include "../shims/d3d12_com.h" +#include "../src/shader_library.h" +#include +#include +#include +#include +#include +#include + +namespace { + +std::vector read_file(const char* path) { + std::ifstream file(path, std::ios::binary); + return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; +} + +struct Created { + bool ok = false; + bool hit = false; // VALID and APPLICATION_PIPELINE_CACHE_HIT +}; + +} // namespace + +int main(int argc, char** argv) { + if (argc != 3) { + std::fprintf(stderr, "usage: pipeline_cache_seed_test \n"); + return 2; + } + const std::vector code = read_file(argv[1]); + const std::vector seed = read_file(argv[2]); + if (code.empty() || code.size() % 4) { + std::fprintf(stderr, "%s: not a SPIR-V module\n", argv[1]); + return 1; + } + if (seed.size() < sizeof(VkPipelineCacheHeaderVersionOne)) { + std::fprintf(stderr, "%s: not a pipeline cache\n", argv[2]); + return 1; + } + + Microsoft::WRL::ComPtr d3d; + if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&d3d))) || !d3d) { + std::printf("SKIP: no Vulkan device for the runtime\n"); + return 77; + } + const rt::d3d12::VulkanDeviceInfo* info = rt::d3d12::vulkan_device_info(d3d.Get()); + if (!info || !info->get_instance_proc) return 1; + const VkDevice device = info->device; + const auto device_proc = reinterpret_cast( + info->get_instance_proc(info->instance, "vkGetDeviceProcAddr")); + const auto physical_properties = reinterpret_cast( + info->get_instance_proc(info->instance, "vkGetPhysicalDeviceProperties")); + if (!device_proc || !physical_properties) return 1; +#define LOAD(name) const auto name = reinterpret_cast(device_proc(device, #name)) + LOAD(vkCreateShaderModule); + LOAD(vkDestroyShaderModule); + LOAD(vkCreatePipelineLayout); + LOAD(vkDestroyPipelineLayout); + LOAD(vkCreatePipelineCache); + LOAD(vkDestroyPipelineCache); + LOAD(vkGetPipelineCacheData); + LOAD(vkCreateComputePipelines); + LOAD(vkDestroyPipeline); +#undef LOAD + + VkPhysicalDeviceProperties properties{}; + physical_properties(info->physical, &properties); + VkPipelineCacheHeaderVersionOne header{}; + std::memcpy(&header, seed.data(), sizeof(header)); + if (header.vendorID != properties.vendorID || header.deviceID != properties.deviceID || + std::memcmp(header.pipelineCacheUUID, properties.pipelineCacheUUID, VK_UUID_SIZE) != 0) { + std::printf("SKIP: %s was written for another GPU or driver than the runtime's %s\n", argv[2], + properties.deviceName); + return 77; + } + + VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; + module_info.codeSize = code.size(); + module_info.pCode = reinterpret_cast(code.data()); + VkShaderModule module = VK_NULL_HANDLE; + if (vkCreateShaderModule(device, &module_info, nullptr, &module) != VK_SUCCESS) return 1; + const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(leonida::ShaderUserData)}; + VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; + layout_info.pushConstantRangeCount = 1; + layout_info.pPushConstantRanges = &push; + VkPipelineLayout layout = VK_NULL_HANDLE; + if (vkCreatePipelineLayout(device, &layout_info, nullptr, &layout) != VK_SUCCESS) return 1; + + const auto make_cache = [&](const void* data, std::size_t size) { + VkPipelineCacheCreateInfo cache_info{VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO}; + cache_info.initialDataSize = size; + cache_info.pInitialData = data; + VkPipelineCache cache = VK_NULL_HANDLE; + if (vkCreatePipelineCache(device, &cache_info, nullptr, &cache) != VK_SUCCESS) return VkPipelineCache{}; + return cache; + }; + // The index entry check_pipeline_cache_seed.cmake writes: both programs take the subgroup request from it. + leonida::ShaderEntry entry; + entry.stage = "cs"; + entry.local_size = {1, 1, 1}; + const auto create = [&](VkPipelineCache cache) { + VkPipelineCreationFeedback feedback{}; + const VkPipelineCreationFeedbackCreateInfo feedback_info{ + VK_STRUCTURE_TYPE_PIPELINE_CREATION_FEEDBACK_CREATE_INFO, nullptr, &feedback, 0, nullptr}; + VkComputePipelineCreateInfo pipeline_info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, &feedback_info}; + pipeline_info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}; + pipeline_info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; + pipeline_info.stage.module = module; + pipeline_info.stage.pName = "main"; + VkPipelineShaderStageRequiredSubgroupSizeCreateInfo required_size{}; + leonida::apply_subgroup_request(leonida::decide_subgroup_size(entry, info->subgroup), required_size, + pipeline_info.stage); + pipeline_info.layout = layout; + VkPipeline pipeline = VK_NULL_HANDLE; + Created result; + if (vkCreateComputePipelines(device, cache, 1, &pipeline_info, nullptr, &pipeline) != VK_SUCCESS) return result; + vkDestroyPipeline(device, pipeline, nullptr); + result.ok = true; + result.hit = (feedback.flags & VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT) && + (feedback.flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT); + return result; + }; + + int code_out = 1; + const VkPipelineCache seeded = make_cache(seed.data(), seed.size()); + const Created from_seed = seeded ? create(seeded) : Created{}; + if (!from_seed.ok) { + std::fprintf(stderr, "FAIL: the pipeline was not created from the seeded cache\n"); + } else if (from_seed.hit) { + std::printf("ok: %s hits on the runtime's device (%s)\n", argv[2], properties.deviceName); + code_out = 0; + } else { + // Does this driver report a hit at all? Write a cache on this device and read it back. + const VkPipelineCache first = make_cache(nullptr, 0); + std::vector own; + std::size_t size = 0; + if (first && create(first).ok && vkGetPipelineCacheData(device, first, &size, nullptr) == VK_SUCCESS) { + own.resize(size); + if (vkGetPipelineCacheData(device, first, &size, own.data()) != VK_SUCCESS) own.clear(); + own.resize(own.empty() ? 0 : size); + } + const VkPipelineCache second = own.empty() ? VkPipelineCache{} : make_cache(own.data(), own.size()); + const bool reports_hits = second && create(second).hit; + if (first) vkDestroyPipelineCache(device, first, nullptr); + if (second) vkDestroyPipelineCache(device, second, nullptr); + if (reports_hits) { + std::fprintf(stderr, + "FAIL: %s does not hit on the runtime's device (%s), which does hit a cache it wrote itself: " + "vk_pipeline_check's device differs from the runtime's in something the driver makes part " + "of a pipeline's cache key\n", + argv[2], properties.deviceName); + } else { + std::printf("SKIP: %s reports no cache hit even for a cache it wrote itself\n", properties.deviceName); + code_out = 77; + } + } + if (seeded) vkDestroyPipelineCache(device, seeded, nullptr); + vkDestroyPipelineLayout(device, layout, nullptr); + vkDestroyShaderModule(device, module, nullptr); + return code_out; +} diff --git a/runtime/test/vk_pipeline_check.cpp b/runtime/test/vk_pipeline_check.cpp index 00b58901b..5227719f3 100644 --- a/runtime/test/vk_pipeline_check.cpp +++ b/runtime/test/vk_pipeline_check.cpp @@ -1172,6 +1172,12 @@ int main(int argc, char** argv) { VkPhysicalDeviceFeatures2 f2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; f2.pNext = chain; vkGetPhysicalDeviceFeatures2(pd, &f2); // fills every linked struct with what's supported + // The runtime's device (create_vulkan_device_standalone) never enables these three, and a driver makes them part + // of a pipeline's cache key: with any of them on here, the cache this tool saves does not hit in gta6rt + // (pipeline_cache_seed_test; docs/pipeline_check.md, "The cache and the runtime's device"). + f2.features.robustBufferAccess = VK_FALSE; + f12.scalarBlockLayout = VK_FALSE; + f13.robustImageAccess = VK_FALSE; VkPhysicalDeviceMeshShaderPropertiesEXT mesh_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_PROPERTIES_EXT}; if (ext_mesh && mesh.meshShader) { VkPhysicalDeviceProperties2 mesh_device_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2};