From 4d3badfa4e8cf7d299015595109e150ed3351fa1 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:39:18 -0600 Subject: [PATCH] vulkan_replay: a dropped indirect dispatch prepares and emits no clamp pass From kevincommits' finding (item 3) in #1608. prepare() set up the clamp pass (vulkan_indirect_clamp.h) of an ExecuteIndirect(DISPATCH) before it resolved the dispatch's compute pipeline. When that pipeline was dropped (skipped on request, or async_build and still building), the command was left out after its clamp was prepared and linked into the list's shared work budget, and emit() then recorded the clamp pass before skipping the dispatch. The pass claimed scratch and counter slots and counted the dropped call's records against the list budget that every later clamped draw and dispatch of the list shares. The pipeline is now resolved first, so a dropped dispatch leaves prepare() before any clamp exists. A pipeline left to PipelineCompiles is not dropped later: a failed deferred build fails the list. New CPU test replay_dropped_indirect_clamp follows the repro: a dropped dispatch alone, before and between built ones, for skipped and async_build pipelines. 10 checks fail on main; all pass with the change. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 7 + runtime/src/vulkan_replay.h | 29 ++-- .../replay_dropped_indirect_clamp_test.cpp | 145 ++++++++++++++++++ 3 files changed, 167 insertions(+), 14 deletions(-) create mode 100644 runtime/test/replay_dropped_indirect_clamp_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3c0112060b..f627a8eda6 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4659,6 +4659,13 @@ if(TARGET Vulkan::Headers) add_test(NAME replay_pipeline_compiles COMMAND replay_pipeline_compiles_test) add_test(NAME replay_pipeline_compiles_strict COMMAND replay_pipeline_compiles_test) set_tests_properties(replay_pipeline_compiles_strict PROPERTIES ENVIRONMENT "LEONIDA_STRICT_REPLAY=1") + # A dropped ExecuteIndirect(DISPATCH) prepares and emits no clamp pass, and owns no list budget (no GPU). + add_executable(replay_dropped_indirect_clamp_test test/replay_dropped_indirect_clamp_test.cpp) + target_link_libraries(replay_dropped_indirect_clamp_test PRIVATE Vulkan::Headers) + if(WIN32) + target_compile_definitions(replay_dropped_indirect_clamp_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + endif() + add_test(NAME replay_dropped_indirect_clamp COMMAND replay_dropped_indirect_clamp_test) # The request hook of ComputePipeline / GraphicsPipeline that pipeline_order.h's recorder hangs on (no GPU). add_executable(replay_pipeline_order_hook_test test/replay_pipeline_order_hook_test.cpp) target_link_libraries(replay_pipeline_order_hook_test PRIVATE Vulkan::Headers) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 4f3c476541..5b9ce29dca 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -4109,6 +4109,21 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun p.source = *cnt; } if (ind->kind != IndirectKind::dispatch && !vk.external_graphics_scope) continue; + // A DISPATCH's pipeline first: one that is dropped (skipped, or still building) leaves the command here, + // before its clamp is prepared. A clamp prepared for it would be emitted with no dispatch after it, and + // when it was the list's first clamp it owns the list's work budget (share_list_budget), so the budget + // word every later clamp of the list reads would be set up by a pass that never runs. + if (ind->kind == IndirectKind::dispatch && ind->pipeline) { // the pipeline and root arguments it was recorded + if (!vk.bind_pipeline || !vk.push_constants || !vk.compute_pipeline) return unsupported_at(__LINE__); + const ComputePipeline* pipeline = vk.compute_pipeline(ind->pipeline); + if (!pipeline) return unresolved_at(__LINE__); + p.pipeline = *pipeline; + const bool found = detail::first_use(*pipeline, PipelineCompiles::User::indirect_dispatch, vk, p); + if (!found && p.pipeline.skipped) continue; // dropped on request, strict or not + if (!found && p.pipeline.async_build && !strict_replay()) continue; + if (!found) return unresolved_at(__LINE__); + if (pipeline->bindless_set && !vk.bind_descriptor_sets) return unsupported_at(__LINE__); + } // The clamp pass (vulkan_indirect_clamp.h): the arguments are copied on the GPU into scratch with their // values bounded before the indirect command reads them, so a stale or garbage record left by a skipped or // mistranslated compute pass cannot run for seconds and reset the display driver. A counted call has its @@ -4157,20 +4172,6 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun return unsupported_at(__LINE__); } } - // Legacy graphics IR carries no graphics state of its own: the recorded, stateful graphics indirects are - // Draw commands (IndirectRasterDraw) and open their own rendering scope. A legacy one is emitted as it is - // - the clamp pass above is what keeps its arguments safe - and its dispatch-side work ends here. - if (ind->kind == IndirectKind::dispatch && ind->pipeline) { // the pipeline and root arguments it was recorded - if (!vk.bind_pipeline || !vk.push_constants || !vk.compute_pipeline) return unsupported_at(__LINE__); - const ComputePipeline* pipeline = vk.compute_pipeline(ind->pipeline); - if (!pipeline) return unresolved_at(__LINE__); - p.pipeline = *pipeline; - const bool found = detail::first_use(*pipeline, PipelineCompiles::User::indirect_dispatch, vk, p); - if (!found && p.pipeline.skipped) continue; // dropped on request, strict or not - if (!found && p.pipeline.async_build && !strict_replay()) continue; - if (!found) return unresolved_at(__LINE__); - if (pipeline->bindless_set && !vk.bind_descriptor_sets) return unsupported_at(__LINE__); - } } else if (const auto* build = std::get_if(&command)) { p.khr_instances = 0; if (vk.xdxr_check) { diff --git a/runtime/test/replay_dropped_indirect_clamp_test.cpp b/runtime/test/replay_dropped_indirect_clamp_test.cpp new file mode 100644 index 0000000000..d6876228d1 --- /dev/null +++ b/runtime/test/replay_dropped_indirect_clamp_test.cpp @@ -0,0 +1,145 @@ +// An ExecuteIndirect(DISPATCH) whose compute pipeline is dropped (skipped on request, or still building with +// async_build) prepares and emits no clamp pass (vulkan_indirect_clamp.h): before, its clamp was prepared and emitted +// although no dispatch followed, and as the list's first clamp it owned the list's shared work budget, which every +// later clamp of the list then read. No GPU: pipelines, buffers and the clamp pass are fakes. +#include "../src/vulkan_replay.h" + +#include +#include +#include +#include + +namespace { +using namespace leonida; + +int failures = 0; +void check(bool ok, const std::string& what) { + if (ok) return; + std::fprintf(stderr, "FAIL: %s\n", what.c_str()); + ++failures; +} + +template +T handle(std::uintptr_t n) { + return reinterpret_cast(n); +} + +constexpr ResourceId arguments_id = 3; +constexpr std::uintptr_t good = 1, skipped = 2, building = 3; + +struct Counts { + int clamps_prepared = 0; + int clamps_emitted = 0; + int dispatches = 0; + std::vector emitted_budgets; // list_budget of each emitted clamp + std::vector emitted_stats; // stats of each emitted clamp +}; +Counts counts; + +std::vector pipelines() { + std::vector out(4); + out[good].pipeline = handle(0x1000); + out[good].layout = handle(7); + out[skipped].layout = handle(7); + out[skipped].skipped = true; + out[building].layout = handle(7); + out[building].async_build = true; + return out; +} +const std::vector registry = pipelines(); + +ReplayFunctions functions() { + ReplayFunctions vk{}; + vk.bind_pipeline = +[](VkCommandBuffer, VkPipelineBindPoint, VkPipeline) {}; + vk.push_constants = +[](VkCommandBuffer, VkPipelineLayout, VkShaderStageFlags, std::uint32_t, std::uint32_t, + const void*) {}; + vk.dispatch = +[](VkCommandBuffer, std::uint32_t, std::uint32_t, std::uint32_t) {}; + vk.dispatch_indirect = +[](VkCommandBuffer, VkBuffer, VkDeviceSize) { ++counts.dispatches; }; + vk.compute_pipeline = [](std::uintptr_t id) -> const ComputePipeline* { + return id < registry.size() && (registry[id].pipeline || registry[id].skipped || registry[id].async_build) + ? ®istry[id] + : nullptr; + }; + // Each clamp gets scratch of its own; its stats block holds the list budget word it owns (stats + 16). + vk.prepare_indirect_clamp = [](VkBuffer, VkDeviceSize, VkBuffer, VkDeviceSize, std::uint32_t, std::uint32_t stride, + std::uint32_t max_count, ClampedIndirect& out) { + const int n = ++counts.clamps_prepared; + out.buffer = handle(0x100 + std::uintptr_t(n)); + out.offset = 0; + out.stride = stride; + out.records = max_count; + out.stats = VkDeviceAddress(0x10000) * VkDeviceAddress(n); + out.list_budget = out.stats + 16; + return true; + }; + vk.emit_indirect_clamp = [](VkCommandBuffer, const ClampedIndirect& clamp) { + ++counts.clamps_emitted; + counts.emitted_budgets.push_back(clamp.list_budget); + counts.emitted_stats.push_back(clamp.stats); + }; + return vk; +} + +const ResourceBinding* resolve(ResourceId id) { + static const ResourceBinding arguments = [] { + ResourceBinding b{}; + b.buffer = handle(12); + b.size = 4096; + return b; + }(); + return id == arguments_id ? &arguments : nullptr; +} + +ExecuteIndirect dispatch_with(std::uintptr_t pipeline) { + ExecuteIndirect e{}; + e.kind = IndirectKind::dispatch; + e.stride = 12; + e.max_count = 1; + e.argument_buffer = arguments_id; + e.pipeline = pipeline; + return e; +} + +// Prepares and emits `pipelines`, one indirect dispatch each, as one list. +void run(const char* what, const std::vector& order, int want_clamps, int want_dispatches) { + counts = {}; + CommandList list; + for (const auto p : order) list.execute_indirect(dispatch_with(p)); + list.close(); + const ReplayFunctions vk = functions(); + PreparedList prepared; + const ReplayResult r = prepare(list, resolve, vk, prepared); + check(r == ReplayResult::success, std::string(what) + ": prepare succeeds"); + if (r != ReplayResult::success) return; + emit(prepared, handle(5), vk); + const std::string where = std::string(what) + ": "; + check(counts.clamps_prepared == want_clamps, + where + std::to_string(counts.clamps_prepared) + " clamps prepared, want " + std::to_string(want_clamps)); + check(counts.clamps_emitted == want_clamps, + where + std::to_string(counts.clamps_emitted) + " clamps emitted, want " + std::to_string(want_clamps)); + check(counts.dispatches == want_dispatches, + where + std::to_string(counts.dispatches) + " dispatches, want " + std::to_string(want_dispatches)); + // The list's budget word belongs to the first clamp emitted (its pass zeroes it), and every clamp shares it. + for (std::size_t i = 0; i < counts.emitted_budgets.size(); ++i) + check(counts.emitted_budgets[i] == counts.emitted_stats.front() + 16, + where + "clamp " + std::to_string(i) + " reads a budget word no emitted clamp owns"); +} +} // namespace + +int main() { + // kevincommits' repro: the dropped dispatch alone. + run("a skipped pipeline", {skipped}, 0, 0); + run("a pipeline still building (async_build)", {building}, 0, 0); + // The dropped dispatch first: the one that runs owns the budget it reads. + run("skipped, then a built one", {skipped, good}, 1, 1); + run("building, then a built one", {building, good}, 1, 1); + run("built, skipped, built", {good, skipped, good}, 2, 2); + // The control: two built dispatches share the first one's budget. + run("two built", {good, good}, 2, 2); + if (failures) { + std::fprintf(stderr, "%d failure(s)\n", failures); + return 1; + } + std::printf("replay_dropped_indirect_clamp: ok\n"); + return 0; +}