[Vulkan] Keep a rejected queue_dealloca from clearing pending flag (#25003) queue_dealloca cleared back dealloca_queued flag on error, openning the gates for double deallocation. Return early on validation errors. Unfortunately the HAL drivers handle e.g. double deallocation queueing different (silent ignore vs precondition), so the tests are target dependent. Co-Authored-By: Claude Signed-off-by: Tobias Fuchs <fuchs@roofline.ai> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/runtime/src/iree/hal/drivers/vulkan/cts/BUILD.bazel b/runtime/src/iree/hal/drivers/vulkan/cts/BUILD.bazel index ab19a22..1ea62c8 100644 --- a/runtime/src/iree/hal/drivers/vulkan/cts/BUILD.bazel +++ b/runtime/src/iree/hal/drivers/vulkan/cts/BUILD.bazel
@@ -864,6 +864,31 @@ ) iree_runtime_cc_test( + name = "queue_tests", + srcs = [ + "queue_test.cc", + "//runtime/src/iree/hal/cts/util:test_main.cc", + ], + data = ["//build_tools/sanitizer:lsan_suppressions_vulkan.txt"], + env = { + "LSAN_OPTIONS": "suppressions=$(location //build_tools/sanitizer:lsan_suppressions_vulkan.txt)", + }, + tags = [ + "driver=vulkan", + "requires-gpu-vulkan", + ], + deps = [ + ":backends", + "//runtime/src/iree/base/tooling:flags", + "//runtime/src/iree/hal", + "//runtime/src/iree/hal/cts/util:registry", + "//runtime/src/iree/hal/cts/util:test_base", + "//runtime/src/iree/hal/local:transient_buffer", + "//runtime/src/iree/testing:gtest", + ], +) + +iree_runtime_cc_test( name = "queue_host_call_tests", srcs = ["//runtime/src/iree/hal/cts/util:test_main.cc"], data = ["//build_tools/sanitizer:lsan_suppressions_vulkan.txt"],
diff --git a/runtime/src/iree/hal/drivers/vulkan/cts/CMakeLists.txt b/runtime/src/iree/hal/drivers/vulkan/cts/CMakeLists.txt index c7b865d..2c81c3f 100644 --- a/runtime/src/iree/hal/drivers/vulkan/cts/CMakeLists.txt +++ b/runtime/src/iree/hal/drivers/vulkan/cts/CMakeLists.txt
@@ -808,6 +808,25 @@ iree_cc_test( NAME + queue_tests + SRCS + "${PROJECT_SOURCE_DIR}/runtime/src/iree/hal/cts/util/test_main.cc" + "queue_test.cc" + DEPS + ::backends + iree::base::tooling::flags + iree::hal + iree::hal::cts::util::registry + iree::hal::cts::util::test_base + iree::hal::local::transient_buffer + iree::testing::gtest + LABELS + "driver=vulkan" + "requires-gpu-vulkan" +) + +iree_cc_test( + NAME queue_host_call_tests SRCS "${PROJECT_SOURCE_DIR}/runtime/src/iree/hal/cts/util/test_main.cc"
diff --git a/runtime/src/iree/hal/drivers/vulkan/cts/queue_test.cc b/runtime/src/iree/hal/drivers/vulkan/cts/queue_test.cc new file mode 100644 index 0000000..0262479 --- /dev/null +++ b/runtime/src/iree/hal/drivers/vulkan/cts/queue_test.cc
@@ -0,0 +1,163 @@ +// Copyright 2026 The IREE Authors +// +// Licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// Vulkan-specific queue API tests + +#include "iree/hal/cts/util/test_base.h" +#include "iree/hal/local/transient_buffer.h" + +namespace iree::hal::cts { + +namespace { + +constexpr iree_device_size_t kBufferSize = 4096; + +// Issues a queue_dealloca of |buffer| with no waits and a fresh signal +// semaphore and returns its status. +iree_status_t TryQueueDealloca( + iree_hal_device_t* device, iree_hal_buffer_t* buffer, + iree_hal_dealloca_flags_t flags = IREE_HAL_DEALLOCA_FLAG_NONE) { + SemaphoreList empty_wait; + SemaphoreList signal(device, {0}, {1}); + return iree_hal_device_queue_dealloca(device, IREE_HAL_QUEUE_AFFINITY_ANY, + empty_wait, signal, buffer, flags); +} + +} // namespace + +// The Vulkan queue accepts at most one queue_dealloca per transient buffer. +// The first accepted queue_dealloca sets the transient's dealloca_queued flag +// and every later queue_dealloca of the same buffer is rejected. A rejected +// call must not clear the flag: only the call that set it may, and only when +// its own submission fails. +class VulkanQueueDeallocaTest : public CtsTestBase<> { + protected: + void SetUp() override { + CtsTestBase<>::SetUp(); + if (IsSkipped() || HasFatalFailure()) return; + gate_ = SemaphoreList(device_, {0}, {1}); + first_signal_ = SemaphoreList(device_, {0}, {1}); + ASSERT_NO_FATAL_FAILURE(AllocateCommittedTransient()); + } + + void TearDown() override { + buffer_.reset(); + gate_ = SemaphoreList(); + first_signal_ = SemaphoreList(); + CtsTestBase<>::TearDown(); + } + + void AllocateCommittedTransient() { + iree_hal_buffer_params_t params = {0}; + params.type = IREE_HAL_MEMORY_TYPE_DEVICE_LOCAL; + params.usage = IREE_HAL_BUFFER_USAGE_TRANSFER; + SemaphoreList empty_wait; + SemaphoreList alloca_signal(device_, {0}, {1}); + IREE_ASSERT_OK(iree_hal_device_queue_alloca( + device_, IREE_HAL_QUEUE_AFFINITY_ANY, empty_wait, alloca_signal, + /*pool=*/NULL, params, kBufferSize, IREE_HAL_ALLOCA_FLAG_NONE, + buffer_.out())); + ASSERT_TRUE(iree_hal_local_transient_buffer_isa(buffer_)); + IREE_ASSERT_OK(iree_hal_semaphore_list_wait( + alloca_signal, iree_infinite_timeout(), IREE_ASYNC_WAIT_FLAG_NONE)); + ASSERT_TRUE(iree_hal_local_transient_buffer_is_committed(buffer_)); + } + + // Queues the first dealloca behind |gate_| so it stays pending until + // ReleaseFirstDealloca(). + void QueueFirstDealloca() { + IREE_ASSERT_OK(iree_hal_device_queue_dealloca( + device_, IREE_HAL_QUEUE_AFFINITY_ANY, gate_, first_signal_, buffer_, + IREE_HAL_DEALLOCA_FLAG_NONE)); + ASSERT_TRUE(iree_hal_local_transient_buffer_is_dealloca_queued(buffer_)); + } + + // Expects the first dealloca to still be pending: queued but not executed. + void ExpectFirstDeallocaPending() { + EXPECT_TRUE(iree_hal_local_transient_buffer_is_dealloca_queued(buffer_)); + EXPECT_TRUE(iree_hal_local_transient_buffer_is_committed(buffer_)); + } + + // Lets the first dealloca execute and expects it to release the backing. + void ReleaseFirstDealloca() { + IREE_ASSERT_OK(iree_hal_semaphore_list_signal(gate_, /*frontier=*/NULL)); + IREE_ASSERT_OK(iree_hal_semaphore_list_wait( + first_signal_, iree_infinite_timeout(), IREE_ASYNC_WAIT_FLAG_NONE)); + EXPECT_FALSE(iree_hal_local_transient_buffer_is_committed(buffer_)); + EXPECT_EQ(iree_hal_local_transient_buffer_backing_buffer(buffer_), nullptr); + } + + Ref<iree_hal_buffer_t> buffer_; + SemaphoreList gate_; + SemaphoreList first_signal_; +}; + +// A second dealloca while the first is pending is rejected and leaves the +// first one pending. +TEST_P(VulkanQueueDeallocaTest, SecondDeallocaRejectedWhilePending) { + ASSERT_NO_FATAL_FAILURE(QueueFirstDealloca()); + IREE_EXPECT_STATUS_IS(IREE_STATUS_FAILED_PRECONDITION, + TryQueueDealloca(device_, buffer_)); + ExpectFirstDeallocaPending(); + ASSERT_NO_FATAL_FAILURE(ReleaseFirstDealloca()); +} + +// Regression: rejecting the second dealloca used to clear the flag set by the +// first, so a third dealloca was accepted and the buffer was queued for +// release twice. +TEST_P(VulkanQueueDeallocaTest, RejectedDeallocaDoesNotAdmitAnother) { + ASSERT_NO_FATAL_FAILURE(QueueFirstDealloca()); + IREE_EXPECT_STATUS_IS(IREE_STATUS_FAILED_PRECONDITION, + TryQueueDealloca(device_, buffer_)); + IREE_EXPECT_STATUS_IS(IREE_STATUS_FAILED_PRECONDITION, + TryQueueDealloca(device_, buffer_)); + ExpectFirstDeallocaPending(); + ASSERT_NO_FATAL_FAILURE(ReleaseFirstDealloca()); +} + +// Regression: a dealloca rejected for unsupported flags while another is +// pending used to clear the pending one's. +TEST_P(VulkanQueueDeallocaTest, InvalidFlagsDeallocaDoesNotAdmitAnother) { + ASSERT_NO_FATAL_FAILURE(QueueFirstDealloca()); + IREE_EXPECT_STATUS_IS( + IREE_STATUS_INVALID_ARGUMENT, + TryQueueDealloca(device_, buffer_, /*flags=*/1ull << 62)); + EXPECT_TRUE(iree_hal_local_transient_buffer_is_dealloca_queued(buffer_)); + IREE_EXPECT_STATUS_IS(IREE_STATUS_FAILED_PRECONDITION, + TryQueueDealloca(device_, buffer_)); + ExpectFirstDeallocaPending(); + ASSERT_NO_FATAL_FAILURE(ReleaseFirstDealloca()); +} + +// The flag stays set after the dealloca executes, so deallocating an already +// released buffer is rejected. +TEST_P(VulkanQueueDeallocaTest, DeallocaAfterCompletionRejected) { + ASSERT_NO_FATAL_FAILURE(QueueFirstDealloca()); + ASSERT_NO_FATAL_FAILURE(ReleaseFirstDealloca()); + IREE_EXPECT_STATUS_IS(IREE_STATUS_FAILED_PRECONDITION, + TryQueueDealloca(device_, buffer_)); +} + +// A dealloca that sets the flag and then fails to submit must clear it so the +// caller can retry. Here the signal payload is out of range, which is rejected +// after the flag has been set. +TEST_P(VulkanQueueDeallocaTest, FailedSubmissionAllowsRetry) { + SemaphoreList empty_wait; + SemaphoreList bad_signal(device_, {0}, {IREE_HAL_SEMAPHORE_MAX_VALUE + 1}); + IREE_EXPECT_STATUS_IS(IREE_STATUS_OUT_OF_RANGE, + iree_hal_device_queue_dealloca( + device_, IREE_HAL_QUEUE_AFFINITY_ANY, empty_wait, + bad_signal, buffer_, IREE_HAL_DEALLOCA_FLAG_NONE)); + EXPECT_FALSE(iree_hal_local_transient_buffer_is_dealloca_queued(buffer_)); + EXPECT_TRUE(iree_hal_local_transient_buffer_is_committed(buffer_)); + + ASSERT_NO_FATAL_FAILURE(QueueFirstDealloca()); + ASSERT_NO_FATAL_FAILURE(ReleaseFirstDealloca()); +} + +CTS_REGISTER_TEST_SUITE(VulkanQueueDeallocaTest); + +} // namespace iree::hal::cts
diff --git a/runtime/src/iree/hal/drivers/vulkan/queue.c b/runtime/src/iree/hal/drivers/vulkan/queue.c index e79a0e0..2df43da 100644 --- a/runtime/src/iree/hal/drivers/vulkan/queue.c +++ b/runtime/src/iree/hal/drivers/vulkan/queue.c
@@ -6671,28 +6671,27 @@ const iree_hal_dealloca_flags_t known_flags = IREE_HAL_DEALLOCA_FLAG_PREFER_ORIGIN; - iree_status_t status = iree_ok_status(); if (iree_any_bit_set(flags, ~known_flags)) { - status = iree_make_status( + IREE_TRACE_ZONE_END(z0); + return iree_make_status( IREE_STATUS_INVALID_ARGUMENT, "unsupported Vulkan queue dealloca flags: 0x%" PRIx64, flags); } - if (iree_status_is_ok(status) && - !iree_hal_local_transient_buffer_isa(buffer)) { - status = iree_make_status(IREE_STATUS_INVALID_ARGUMENT, - "Vulkan queue_dealloca buffer was not returned " - "by Vulkan queue_alloca"); + if (!iree_hal_local_transient_buffer_isa(buffer)) { + IREE_TRACE_ZONE_END(z0); + return iree_make_status(IREE_STATUS_INVALID_ARGUMENT, + "Vulkan queue_dealloca buffer was not returned " + "by Vulkan queue_alloca"); } - if (iree_status_is_ok(status) && - !iree_hal_local_transient_buffer_begin_dealloca(buffer)) { - status = iree_make_status( + if (!iree_hal_local_transient_buffer_begin_dealloca(buffer)) { + IREE_TRACE_ZONE_END(z0); + return iree_make_status( IREE_STATUS_FAILED_PRECONDITION, "Vulkan transient buffer has already been queued for deallocation"); } - if (iree_status_is_ok(status)) { - status = iree_hal_vulkan_queue_validate_semaphore_list( - queue, wait_semaphore_list, IREE_SV("wait")); - } + + iree_status_t status = iree_hal_vulkan_queue_validate_semaphore_list( + queue, wait_semaphore_list, IREE_SV("wait")); if (iree_status_is_ok(status)) { status = iree_hal_vulkan_queue_validate_semaphore_list( queue, signal_semaphore_list, IREE_SV("signal")); @@ -6722,8 +6721,7 @@ iree_status_clone(status)); } iree_hal_vulkan_queue_pending_submission_destroy(queue, submission); - } else if (!iree_status_is_ok(status) && - iree_hal_local_transient_buffer_isa(buffer)) { + } else if (!iree_status_is_ok(status)) { iree_hal_local_transient_buffer_abort_dealloca(buffer); }