Skip to content

Runtime: reclaim GPU batches on all completed CommandSlots in VulkanQueue::acquire - #4787

Open
emirhan-karaca wants to merge 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/vk-queue-slot-batch-reclaim
Open

emirhan-karaca wants to merge 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/vk-queue-slot-batch-reclaim

Conversation

@emirhan-karaca

@emirhan-karaca emirhan-karaca commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claim: #4683

Problem

VulkanQueue::acquire() (runtime/src/vulkan_backend.h:2280-2295) sweeps all completed CommandSlots (slot.ticket <= done) to clear slot.segments and recycle slot.events, but only resets predicate, predicated_copy, and counted_dispatch on the single reuse slot (and only after vkResetCommandBuffer succeeds). After a multi-submission burst, completed slots beyond slot 0 retain their VkBuffer, VkDeviceMemory, and VkDescriptorPool handles indefinitely during steady-state traffic.

Fix

  • Move slot.predicate.reset(), slot.predicated_copy.reset(), and slot.counted_dispatch.reset() into the completed-slot sweep alongside slot.segments.clear().
  • Add cross-platform Vulkan regression test vk_queue_slot_batch_reclaim_test covering PredicateReducer::Batch, PredicatedBufferCopier::Batch, IndirectDispatchCounter::Batch, and failed vkResetCommandBuffer. (Note: adjacent open PR runtime: cap the command buffers a Vulkan queue keeps in flight at 64 #4191 also refactors VulkanQueue::acquire(), but leaves these three batch resets outside the completed-slot sweep.)

Verification

  • AI-assisted.
  • GPU / driver: Mesa 25.2.8 llvmpipe (Vulkan 1.4.318), VK_LAYER_KHRONOS_validation 1.3.275, Ubuntu 24.04.5, GCC 13.3.0.
  • Negative control on unmodified main (98fa503f): vk_queue_slot_batch_reclaim_test fails deterministically on all 3 batch types (buf=3 mem=3 pool=3, buf=3 mem=3 pool=6, buf=3 mem=3 pool=0).
  • After fix: vk_queue_slot_batch_reclaim_test 3x plain + 3x LEONIDA_VK_VALIDATION=1 (0 validation errors); vk_predicate_capture_test, vk_predicated_copy_test, vk_queue_query_sync_test under LEONIDA_VK_VALIDATION=1 (0 validation errors); ctest (4/4) and python3 -I tests/test_ctest_registration.py (4/4) pass.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran vk_queue_slot_batch_reclaim_test at 6d7c25e62 on Windows 11, RTX 5070 Ti: 4 cases passed (validation errors: 0), exit 0.

VulkanQueue::acquire now reclaims GPU batches on every completed CommandSlot, not just the current one, so a queue with several completed slots does not hold their batches; the 419-line test covers the multi-slot completion, partial completion and the already-reclaimed case. git diff --check clean.

Merge check: clean with #4557, #4663, #4740 and #4756 (independent of the fence/semaphore changes). No blocking issue.

@emirhan-karaca emirhan-karaca changed the title Runtime: reclaim GPU batches on all completed CommandSlots in VulkanQ… Runtime: reclaim GPU batches on all completed CommandSlots in VulkanQueue::acquire Nov 23, 2026
@Kev-Ball

Copy link
Copy Markdown

Verified the completed-slot sweep with two completed and two still pending on a physical RTX 3080 Ti, NVIDIA 620.73.04, CachyOS Linux, at 6d7c25e6. The checked-in test passed all four cases with Khronos validation enabled.

For the mixed case I modified only the fixture locally. I put a queue timeline signal between submissions 2 and 3, blocked the last two submissions on a second gate value, then released the first pair and waited on the midpoint signal. One plain submission triggered acquire() while the second pair was still blocked. The batch hooks measured:

Batch type At peak (buffers / memory / pools) After midpoint acquire After releasing the second pair and sweeping
Predicate 4 / 4 / 4 2 / 2 / 2 0 / 0 / 0
Predicated copy 4 / 4 / 8 2 / 2 / 4 0 / 0 / 0
Counted dispatch 4 / 4 / 0 2 / 2 / 0 0 / 0 / 0

Nine runs produced identical counts and zero validation errors. This directly checks both lifetimes on one sweep: completed batches released their handles while in-flight batches retained theirs. AI-assisted, synthetic commands only.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 6d7c25e merged with main 98fa503 on Linux (CMake 4.4.5/Ninja, GCC, Vulkan headers only) and Windows (MSVC 14.51, /W4 /permissive-): concern (runtime test could not be run)

  • vk_queue_slot_batch_reclaim_test builds clean on both (no warnings). On Linux ctest reports it Skipped (exit 77, no Vulkan loader/device).
  • Not run: it needs a real Vulkan 1.3 device (only the two physical GPUs are available here, no software ICD), so the live-handle assertions are unverified by me.
  • Negative control: not possible without running the test.
  • Diff read in full: no issue found; the three batch resets now run for every completed slot before the vkResetCommandBuffer early return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants