[Vulkan] Fix incorrect replay of command buffers (#25172) Vulkan command-buffer replay cache reuses a recorded VkCommandBuffer keyed on the HAL command buffer. It re-issues a command-buffer, with new BDA binding table(s). But copy/fill commands and indirect dispatch parameters resolve their binding table slots into VkBuffer handles at command-buffer recording time (versus during execution). Replay with a different binding table does not "refresh" those baked-in buffer handles. Hence, those commands incorrectly used buffer handles from previous submissions, leading to invalid results/Vulkan-UB. --------- Signed-off-by: Tobias Fuchs <fuchs@roofline.ai> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
diff --git a/runtime/src/iree/hal/drivers/vulkan/command_buffer.c b/runtime/src/iree/hal/drivers/vulkan/command_buffer.c index 896b29c..405b385 100644 --- a/runtime/src/iree/hal/drivers/vulkan/command_buffer.c +++ b/runtime/src/iree/hal/drivers/vulkan/command_buffer.c
@@ -169,6 +169,15 @@ // Whether replay embeds binding-table-dependent descriptor dispatch state. bool has_descriptor_dispatches; + // Whether any recorded command resolves a binding table slot into state that + // is baked into the native command buffer at record time and therefore + // cannot be refreshed by republishing BDA binding tables (which only walks + // dispatch commands). Buffer transfer commands record resolved VkBuffer + // handles, and indirect dispatches record the resolved parameter buffer, so + // a command buffer with any of those may not be replayed against a different + // binding table. + bool has_binding_table_dependent_commands; + // Descriptor pool capacity required to replay recorded native commands. iree_hal_vulkan_command_buffer_descriptor_requirements_t descriptor_requirements; @@ -1105,6 +1114,16 @@ return iree_ok_status(); } +// Marks the command buffer as unsafe for cached native replay if |buffer_ref| +// is an indirect reference resolved from the submission binding table. +static void iree_hal_vulkan_command_buffer_note_binding_table_dependency( + iree_hal_vulkan_command_buffer_t* command_buffer, + iree_hal_buffer_ref_t buffer_ref) { + if (!buffer_ref.buffer) { + command_buffer->has_binding_table_dependent_commands = true; + } +} + static iree_status_t iree_hal_vulkan_command_buffer_add_descriptor_count( uint32_t current_count, iree_host_size_t delta_count, uint32_t* out_count) { if (IREE_UNLIKELY(delta_count > UINT32_MAX - current_count)) { @@ -1242,6 +1261,14 @@ return command_buffer->has_descriptor_dispatches; } +bool iree_hal_vulkan_command_buffer_has_binding_table_dependent_commands( + iree_hal_command_buffer_t* base_command_buffer) { + IREE_ASSERT_ARGUMENT(base_command_buffer); + iree_hal_vulkan_command_buffer_t* command_buffer = + iree_hal_vulkan_command_buffer_cast(base_command_buffer); + return command_buffer->has_binding_table_dependent_commands; +} + iree_status_t iree_hal_vulkan_command_buffer_native_bda_publication_length( iree_hal_command_buffer_t* base_command_buffer, iree_device_size_t* out_publication_length) { @@ -2678,6 +2705,8 @@ iree_hal_vulkan_command_buffer_append_command( command_buffer, IREE_HAL_VULKAN_COMMAND_TYPE_FILL_BUFFER, record_length, payload_offset, /*out_command=*/NULL, &payload); + iree_hal_vulkan_command_buffer_note_binding_table_dependency(command_buffer, + target_ref); iree_hal_vulkan_command_fill_buffer_t* fill_buffer = (iree_hal_vulkan_command_fill_buffer_t*)payload; fill_buffer->target_ref = target_ref; @@ -2743,6 +2772,8 @@ iree_hal_vulkan_command_buffer_append_command( command_buffer, IREE_HAL_VULKAN_COMMAND_TYPE_UPDATE_BUFFER, record_length, payload_offset, /*out_command=*/NULL, &payload); + iree_hal_vulkan_command_buffer_note_binding_table_dependency(command_buffer, + target_ref); iree_hal_vulkan_command_update_buffer_t* update_buffer = (iree_hal_vulkan_command_update_buffer_t*)payload; void* source_data = @@ -2791,6 +2822,10 @@ iree_hal_vulkan_command_buffer_append_command( command_buffer, IREE_HAL_VULKAN_COMMAND_TYPE_COPY_BUFFER, record_length, payload_offset, /*out_command=*/NULL, &payload); + iree_hal_vulkan_command_buffer_note_binding_table_dependency(command_buffer, + source_ref); + iree_hal_vulkan_command_buffer_note_binding_table_dependency(command_buffer, + target_ref); iree_hal_vulkan_command_copy_buffer_t* copy_buffer = (iree_hal_vulkan_command_copy_buffer_t*)payload; copy_buffer->source_ref = source_ref; @@ -2955,6 +2990,8 @@ if (iree_hal_dispatch_uses_indirect_parameters(flags)) { IREE_RETURN_IF_ERROR(iree_hal_vulkan_command_buffer_track_buffer_ref( command_buffer, config.workgroup_count_ref)); + iree_hal_vulkan_command_buffer_note_binding_table_dependency( + command_buffer, config.workgroup_count_ref); } iree_host_size_t payload_length = 0;
diff --git a/runtime/src/iree/hal/drivers/vulkan/command_buffer.h b/runtime/src/iree/hal/drivers/vulkan/command_buffer.h index f5c5c2f..0bdd1eb 100644 --- a/runtime/src/iree/hal/drivers/vulkan/command_buffer.h +++ b/runtime/src/iree/hal/drivers/vulkan/command_buffer.h
@@ -110,6 +110,13 @@ iree_hal_vulkan_command_buffer_bda_binding_cache_t* bda_binding_cache, iree_hal_vulkan_command_buffer_bda_binding_slot_t* out_slot); +// Returns true if any recorded command bakes a binding-table-resolved buffer +// into the native command buffer (transfer commands, indirect dispatch +// parameters). Such command buffers cannot be replayed against a different +// binding table because republishing only refreshes dispatch binding tables. +bool iree_hal_vulkan_command_buffer_has_binding_table_dependent_commands( + iree_hal_command_buffer_t* command_buffer); + // Returns host-published BDA table bytes required to replay |command_buffer| // once into a native VkCommandBuffer. iree_status_t iree_hal_vulkan_command_buffer_native_bda_publication_length(
diff --git a/runtime/src/iree/hal/drivers/vulkan/cts/bda_raw_spirv_test.cc b/runtime/src/iree/hal/drivers/vulkan/cts/bda_raw_spirv_test.cc index 76db547..d9c0c3b 100644 --- a/runtime/src/iree/hal/drivers/vulkan/cts/bda_raw_spirv_test.cc +++ b/runtime/src/iree/hal/drivers/vulkan/cts/bda_raw_spirv_test.cc
@@ -1325,7 +1325,28 @@ recommended_page_size)); } -class BdaRawSpirvReplayCacheTest : public BdaRawSpirvTest {}; +class BdaRawSpirvReplayCacheTest : public BdaRawSpirvTest { + protected: + static constexpr iree_device_size_t kParameterByteLength = + 3 * sizeof(uint32_t); + + void CreateIndirectParameterBuffer(uint32_t workgroup_count_x, + Ref<iree_hal_buffer_t>* out_buffer) { + iree_hal_buffer_params_t params = {0}; + params.type = + IREE_HAL_MEMORY_TYPE_DEVICE_LOCAL | IREE_HAL_MEMORY_TYPE_HOST_VISIBLE; + params.usage = IREE_HAL_BUFFER_USAGE_DISPATCH_INDIRECT_PARAMETERS | + IREE_HAL_BUFFER_USAGE_DISPATCH_STORAGE | + IREE_HAL_BUFFER_USAGE_TRANSFER | + IREE_HAL_BUFFER_USAGE_MAPPING; + IREE_ASSERT_OK(iree_hal_allocator_allocate_buffer( + device_allocator_, params, kParameterByteLength, out_buffer->out())); + const uint32_t workgroup_count[3] = {workgroup_count_x, 1, 1}; + IREE_ASSERT_OK(iree_hal_buffer_map_write(out_buffer->get(), /*offset=*/0, + workgroup_count, + sizeof(workgroup_count))); + } +}; TEST_P(BdaRawSpirvReplayCacheTest, TrimDropsIdleCachedNativeReplay) { ASSERT_EQ(0, QueryNativeReplayCache(IREE_SV("retained_instance_count"))); @@ -1543,6 +1564,234 @@ EXPECT_EQ(0, QueryNativeReplayCache(IREE_SV("instance_count"))); } +// Copy commands bake the VkBuffers resolved from the binding table into the +// native command buffer, so a resubmission with a different binding table must +// not reuse the cached replay. +TEST_P(BdaRawSpirvReplayCacheTest, + CommandBufferCopyFromBindingTableBypassesNativeReplay) { + IREE_ASSERT_OK(iree_hal_device_trim(device_)); + ASSERT_EQ(0, QueryNativeReplayCache(IREE_SV("instance_count"))); + + const int32_t source_a_data[kElementCount] = {1, 2, 3, 4}; + const int32_t source_b_data[kElementCount] = {50, 60, 70, 80}; + Ref<iree_hal_buffer_t> source_a_buffer; + IREE_ASSERT_OK(CreateDeviceBufferWithData( + source_a_data, sizeof(source_a_data), source_a_buffer.out())); + Ref<iree_hal_buffer_t> source_b_buffer; + IREE_ASSERT_OK(CreateDeviceBufferWithData( + source_b_data, sizeof(source_b_data), source_b_buffer.out())); + Ref<iree_hal_buffer_t> target_a_buffer; + IREE_ASSERT_OK( + CreateZeroedDeviceBuffer(kDispatchByteLength, target_a_buffer.out())); + Ref<iree_hal_buffer_t> target_b_buffer; + IREE_ASSERT_OK( + CreateZeroedDeviceBuffer(kDispatchByteLength, target_b_buffer.out())); + + Ref<iree_hal_command_buffer_t> command_buffer; + IREE_ASSERT_OK(iree_hal_command_buffer_create( + device_, IREE_HAL_COMMAND_BUFFER_MODE_DEFAULT, + IREE_HAL_COMMAND_CATEGORY_TRANSFER, IREE_HAL_QUEUE_AFFINITY_ANY, + /*binding_capacity=*/2, command_buffer.out())); + IREE_ASSERT_OK(iree_hal_command_buffer_begin(command_buffer.get())); + IREE_ASSERT_OK(iree_hal_command_buffer_copy_buffer( + command_buffer.get(), + iree_hal_make_indirect_buffer_ref(/*buffer_slot=*/0, /*offset=*/0, + kDispatchByteLength), + iree_hal_make_indirect_buffer_ref(/*buffer_slot=*/1, /*offset=*/0, + kDispatchByteLength), + IREE_HAL_COPY_FLAG_NONE)); + IREE_ASSERT_OK(iree_hal_command_buffer_end(command_buffer.get())); + + iree_hal_buffer_binding_t binding_table_a_entries[2]; + iree_hal_buffer_binding_table_t binding_table_a = MakeBindingTable( + source_a_buffer.get(), target_a_buffer.get(), binding_table_a_entries); + iree_hal_buffer_binding_t binding_table_b_entries[2]; + iree_hal_buffer_binding_table_t binding_table_b = MakeBindingTable( + source_b_buffer.get(), target_b_buffer.get(), binding_table_b_entries); + + const int64_t initial_hit_count = + QueryNativeReplayCache(IREE_SV("hit_count")); + const int64_t initial_binding_table_bypass_count = + QueryNativeReplayCache(IREE_SV("binding_table_bypass_count")); + + ExecuteCommandBufferAndWait(command_buffer.get(), binding_table_a); + EXPECT_THAT(ReadBufferData<int32_t>(target_a_buffer.get()), + ContainerEq(std::vector<int32_t>{1, 2, 3, 4})); + // A stale replay would copy source_a into target_a and leave target_b zero. + ExecuteCommandBufferAndWait(command_buffer.get(), binding_table_b); + EXPECT_THAT(ReadBufferData<int32_t>(target_b_buffer.get()), + ContainerEq(std::vector<int32_t>{50, 60, 70, 80})); + + EXPECT_EQ(2, QueryNativeReplayCache(IREE_SV("binding_table_bypass_count")) - + initial_binding_table_bypass_count); + EXPECT_EQ(initial_hit_count, QueryNativeReplayCache(IREE_SV("hit_count"))); +} + +// Indirect dispatches bake the parameter VkBuffer resolved from the binding +// table into vkCmdDispatchIndirect, so a resubmission with a different +// parameter buffer must not reuse the cached replay. +TEST_P(BdaRawSpirvReplayCacheTest, + CommandBufferIndirectParametersFromBindingTableBypassNativeReplay) { + IREE_ASSERT_OK(iree_hal_device_trim(device_)); + ASSERT_EQ(0, QueryNativeReplayCache(IREE_SV("instance_count"))); + + Ref<iree_hal_buffer_t> input_buffer; + Ref<iree_hal_buffer_t> output_a_buffer; + CreateInputOutputBuffers(&input_buffer, &output_a_buffer); + Ref<iree_hal_buffer_t> output_b_buffer; + IREE_ASSERT_OK( + CreateZeroedDeviceBuffer(kDispatchByteLength, output_b_buffer.out())); + Ref<iree_hal_buffer_t> parameters_a_buffer; + CreateIndirectParameterBuffer(/*workgroup_count_x=*/kElementCount, + ¶meters_a_buffer); + Ref<iree_hal_buffer_t> parameters_b_buffer; + CreateIndirectParameterBuffer(/*workgroup_count_x=*/2, ¶meters_b_buffer); + + iree_hal_buffer_ref_t binding_refs[2] = { + iree_hal_make_indirect_buffer_ref(/*buffer_slot=*/0, /*offset=*/0, + kDispatchByteLength), + iree_hal_make_indirect_buffer_ref(/*buffer_slot=*/1, /*offset=*/0, + kDispatchByteLength), + }; + iree_hal_buffer_ref_list_t bindings = { + /*.count=*/IREE_ARRAYSIZE(binding_refs), + /*.values=*/binding_refs, + }; + iree_hal_dispatch_config_t config = + iree_hal_make_static_dispatch_config(0, 0, 0); + config.workgroup_count_ref = iree_hal_make_indirect_buffer_ref( + /*buffer_slot=*/2, /*offset=*/0, kParameterByteLength); + + Ref<iree_hal_command_buffer_t> command_buffer; + IREE_ASSERT_OK(iree_hal_command_buffer_create( + device_, IREE_HAL_COMMAND_BUFFER_MODE_DEFAULT, + IREE_HAL_COMMAND_CATEGORY_DISPATCH, IREE_HAL_QUEUE_AFFINITY_ANY, + /*binding_capacity=*/3, command_buffer.out())); + IREE_ASSERT_OK(iree_hal_command_buffer_begin(command_buffer.get())); + IREE_ASSERT_OK(iree_hal_command_buffer_dispatch( + command_buffer.get(), executable_, + iree_hal_executable_function_from_index(0), config, + iree_const_byte_span_empty(), bindings, + IREE_HAL_DISPATCH_FLAG_STATIC_INDIRECT_PARAMETERS)); + IREE_ASSERT_OK(iree_hal_command_buffer_end(command_buffer.get())); + + iree_hal_buffer_binding_t binding_table_a_entries[3] = { + {input_buffer.get(), 0, kDispatchByteLength}, + {output_a_buffer.get(), 0, kDispatchByteLength}, + {parameters_a_buffer.get(), 0, kParameterByteLength}, + }; + iree_hal_buffer_binding_t binding_table_b_entries[3] = { + {input_buffer.get(), 0, kDispatchByteLength}, + {output_b_buffer.get(), 0, kDispatchByteLength}, + {parameters_b_buffer.get(), 0, kParameterByteLength}, + }; + + const int64_t initial_hit_count = + QueryNativeReplayCache(IREE_SV("hit_count")); + const int64_t initial_binding_table_bypass_count = + QueryNativeReplayCache(IREE_SV("binding_table_bypass_count")); + + ExecuteCommandBufferAndWait( + command_buffer.get(), + {IREE_ARRAYSIZE(binding_table_a_entries), binding_table_a_entries}); + ExpectOutput(output_a_buffer.get()); + // A stale replay would dispatch kElementCount workgroups from + // parameters_a_buffer and write every element of output_b_buffer. + ExecuteCommandBufferAndWait( + command_buffer.get(), + {IREE_ARRAYSIZE(binding_table_b_entries), binding_table_b_entries}); + EXPECT_THAT(ReadBufferData<int32_t>(output_b_buffer.get()), + ContainerEq(std::vector<int32_t>{8, 5, 0, 0})); + + EXPECT_EQ(2, QueryNativeReplayCache(IREE_SV("binding_table_bypass_count")) - + initial_binding_table_bypass_count); + EXPECT_EQ(initial_hit_count, QueryNativeReplayCache(IREE_SV("hit_count"))); +} + +// Direct buffer references are the same for every submission, so copies and +// indirect parameters that only use them keep the cached replay. +TEST_P(BdaRawSpirvReplayCacheTest, + CommandBufferDirectTransferRefsReuseNativeReplay) { + IREE_ASSERT_OK(iree_hal_device_trim(device_)); + ASSERT_EQ(0, QueryNativeReplayCache(IREE_SV("instance_count"))); + + Ref<iree_hal_buffer_t> input_buffer; + Ref<iree_hal_buffer_t> output_a_buffer; + CreateInputOutputBuffers(&input_buffer, &output_a_buffer); + Ref<iree_hal_buffer_t> output_b_buffer; + IREE_ASSERT_OK( + CreateZeroedDeviceBuffer(kDispatchByteLength, output_b_buffer.out())); + Ref<iree_hal_buffer_t> parameters_buffer; + CreateIndirectParameterBuffer(/*workgroup_count_x=*/kElementCount, + ¶meters_buffer); + const int32_t source_data[kElementCount] = {3, 1, 4, 1}; + Ref<iree_hal_buffer_t> source_buffer; + IREE_ASSERT_OK(CreateDeviceBufferWithData(source_data, sizeof(source_data), + source_buffer.out())); + Ref<iree_hal_buffer_t> target_buffer; + IREE_ASSERT_OK( + CreateZeroedDeviceBuffer(kDispatchByteLength, target_buffer.out())); + + iree_hal_buffer_ref_t binding_refs[2] = { + iree_hal_make_indirect_buffer_ref(/*buffer_slot=*/0, /*offset=*/0, + kDispatchByteLength), + iree_hal_make_indirect_buffer_ref(/*buffer_slot=*/1, /*offset=*/0, + kDispatchByteLength), + }; + iree_hal_buffer_ref_list_t bindings = { + /*.count=*/IREE_ARRAYSIZE(binding_refs), + /*.values=*/binding_refs, + }; + iree_hal_dispatch_config_t config = + iree_hal_make_static_dispatch_config(0, 0, 0); + config.workgroup_count_ref = iree_hal_make_buffer_ref( + parameters_buffer.get(), /*offset=*/0, kParameterByteLength); + + Ref<iree_hal_command_buffer_t> command_buffer; + IREE_ASSERT_OK(iree_hal_command_buffer_create( + device_, IREE_HAL_COMMAND_BUFFER_MODE_DEFAULT, + IREE_HAL_COMMAND_CATEGORY_ANY, IREE_HAL_QUEUE_AFFINITY_ANY, + /*binding_capacity=*/2, command_buffer.out())); + IREE_ASSERT_OK(iree_hal_command_buffer_begin(command_buffer.get())); + IREE_ASSERT_OK(iree_hal_command_buffer_dispatch( + command_buffer.get(), executable_, + iree_hal_executable_function_from_index(0), config, + iree_const_byte_span_empty(), bindings, + IREE_HAL_DISPATCH_FLAG_STATIC_INDIRECT_PARAMETERS)); + IREE_ASSERT_OK(iree_hal_command_buffer_copy_buffer( + command_buffer.get(), + iree_hal_make_buffer_ref(source_buffer.get(), /*offset=*/0, + kDispatchByteLength), + iree_hal_make_buffer_ref(target_buffer.get(), /*offset=*/0, + kDispatchByteLength), + IREE_HAL_COPY_FLAG_NONE)); + IREE_ASSERT_OK(iree_hal_command_buffer_end(command_buffer.get())); + + iree_hal_buffer_binding_t binding_table_a_entries[2]; + iree_hal_buffer_binding_table_t binding_table_a = MakeBindingTable( + input_buffer.get(), output_a_buffer.get(), binding_table_a_entries); + iree_hal_buffer_binding_t binding_table_b_entries[2]; + iree_hal_buffer_binding_table_t binding_table_b = MakeBindingTable( + input_buffer.get(), output_b_buffer.get(), binding_table_b_entries); + + const int64_t initial_hit_count = + QueryNativeReplayCache(IREE_SV("hit_count")); + const int64_t initial_binding_table_bypass_count = + QueryNativeReplayCache(IREE_SV("binding_table_bypass_count")); + + ExecuteCommandBufferAndWait(command_buffer.get(), binding_table_a); + ExpectOutput(output_a_buffer.get()); + EXPECT_THAT(ReadBufferData<int32_t>(target_buffer.get()), + ContainerEq(std::vector<int32_t>{3, 1, 4, 1})); + ExecuteCommandBufferAndWait(command_buffer.get(), binding_table_b); + ExpectOutput(output_b_buffer.get()); + + EXPECT_EQ(initial_binding_table_bypass_count, + QueryNativeReplayCache(IREE_SV("binding_table_bypass_count"))); + EXPECT_GE(QueryNativeReplayCache(IREE_SV("hit_count")) - initial_hit_count, + 1); +} + CTS_REGISTER_TEST_SUITE_WITH_TAGS(BdaRawSpirvTest, {"vulkan_bda"}, {"vulkan_bda_replay_cache"}); CTS_REGISTER_TEST_SUITE_WITH_TAGS(BdaRawSpirvReplayCacheTest,
diff --git a/runtime/src/iree/hal/drivers/vulkan/queue.c b/runtime/src/iree/hal/drivers/vulkan/queue.c index 268df93..dcaf8b6 100644 --- a/runtime/src/iree/hal/drivers/vulkan/queue.c +++ b/runtime/src/iree/hal/drivers/vulkan/queue.c
@@ -2284,6 +2284,18 @@ queue->native_replay_cache.one_shot_bypass_count + 1; return iree_ok_status(); } + // Replay only refreshes dispatch BDA binding tables. Copy commands and + // indirect dispatch parameters resolve binding table slots into the native + // command buffer when it is recorded, so replaying one against a different + // binding table would access the buffers of the submission that recorded it. + // Fill and update commands do the same but already bypassed above because + // they require descriptor sets. + if (iree_hal_vulkan_command_buffer_has_binding_table_dependent_commands( + submission->execute.command_buffer)) { + queue->native_replay_cache.binding_table_bypass_count = + queue->native_replay_cache.binding_table_bypass_count + 1; + return iree_ok_status(); + } if (iree_hal_vulkan_queue_profile_requests_queue_device_event(submission) || iree_hal_vulkan_queue_profile_requests_dispatch_events(submission)) { queue->native_replay_cache.profile_bypass_count = @@ -6323,6 +6335,10 @@ } else if (iree_string_view_equal(key, IREE_SV("one_shot_bypass_count"))) { *out_value = iree_hal_vulkan_queue_i64_saturate_u64( queue->native_replay_cache.one_shot_bypass_count); + } else if (iree_string_view_equal(key, + IREE_SV("binding_table_bypass_count"))) { + *out_value = iree_hal_vulkan_queue_i64_saturate_u64( + queue->native_replay_cache.binding_table_bypass_count); } else if (iree_string_view_equal(key, IREE_SV("capacity_bypass_count"))) { *out_value = iree_hal_vulkan_queue_i64_saturate_u64( queue->native_replay_cache.capacity_bypass_count);
diff --git a/runtime/src/iree/hal/drivers/vulkan/queue.h b/runtime/src/iree/hal/drivers/vulkan/queue.h index 3fd771c..5973f6a 100644 --- a/runtime/src/iree/hal/drivers/vulkan/queue.h +++ b/runtime/src/iree/hal/drivers/vulkan/queue.h
@@ -379,6 +379,9 @@ // one-shot. uint64_t one_shot_bypass_count; + // Cached replay bypasses due to binding-table-dependent recorded commands. + uint64_t binding_table_bypass_count; + // Cached replay acquisitions bypassed by configured capacity limits. uint64_t capacity_bypass_count;