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.

@memorypressure

Copy link
Copy Markdown

Verified at head 6d7c25e merged with main 3faac55 at check time by review (no build here): since acquire() picks the reuse slot inside the same completed-slot loop, moving the predicate/predicated_copy/counted_dispatch resets into that loop keeps the reused slot's reset and adds the missing ones for completed slots left behind — strictly additive and safer. The new vk_queue_slot_batch_reclaim ctest needs a CI machine; checks were pending at review time. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 6d7c25e on NVIDIA GeForce RTX 4080 (driver 617.42), Windows 11, clang-cl cross-build: pass.

vk_queue_slot_batch_reclaim_test exits 0 with "4 cases passed (validation errors: 0)". That count is the test's own summary and the test prints no case names; the validation layer was not enabled in this run, so "validation errors: 0" is the test's counter, not a layer result. The PR's fail-before-fix control on main was not run here.

AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 6d7c25e on Intel UHD 630 (CFL GT2, Mesa 25.2.8 ANV), Ubuntu 24.04, LEONIDA_VK_DEVICE=Intel: pass without the validation layer.

  • vk_queue_slot_batch_reclaim_test builds and runs three times, exit 0 each, each printing "4 cases passed (validation errors: 0)" and "device: Intel(R) UHD Graphics 630 (CFL GT2)". ctest -R reclaim: "1/1 Test Manifest: hash files with --jobs (parallel create/verify) #24: vk_queue_slot_batch_reclaim ... Passed 0.07 sec".
  • With LEONIDA_VK_DEVICE=NoSuchDevice the binary exits 1 with "no Vulkan 1.3 device (or 1.2 with VK_KHR_dynamic_rendering and VK_KHR_synchronization2) with a graphics queue matches", so the filter applied.

LEONIDA_VK_VALIDATION=1 was not run: this machine has no Khronos validation layer, so the "validation errors: 0" above is the test's own counter and the PR's validation claims are not reproduced here. The PR body names llvmpipe as its device.

AI-assisted.

@emirhan-karaca

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/main 561b65a83 (head 3a410f09f), moving the predicate, predicated_copy, and counted_dispatch resets into VulkanQueue::recycle_finished() (alongside segments.clear() and recycle_khr_stages()) so every completed CommandSlot releases its GPU batches even if another slot is chosen for reuse.

Re-verified on Windows 11 (g++ 16.2.0 -std=c++20 -O2 -Wall -Wextra, Vulkan-Headers 1.4.363.0, 0 warnings in touched/new code):

  • Intel(R) UHD Graphics (101.7092, Vulkan 1.4.323): vk_queue_slot_batch_reclaim_test 3/3 PASS (4 cases passed (validation errors: 0)).
  • NVIDIA GeForce RTX 5050 Laptop GPU (621.24): vk_queue_slot_batch_reclaim_test 3/3 PASS (4 cases passed (validation errors: 0)).

AI-assisted.

@thehollowedone

Copy link
Copy Markdown

Independent Windows check of 3a410f09 on 561b65a8. Windows 11 x64, RTX 3080 Ti / 621.35, MSVC 19.44.35230, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, RelWithDebInfo.

The stock vk_queue_slot_batch_reclaim CTest passed all four cases, including cleanup after a mocked command-buffer reset failure.

I repeated the mixed-completion check on this revision with a scratch fixture that put two predicate batches behind separate timeline gates. Live buffer, memory and descriptor-pool counts were 2/2/2 before the sweep, 1/1/1 with the second batch still gated, and 0/0/0 after it completed and was swept. The runtime code was unchanged. Both runs reported zero Vulkan core or synchronization validation messages and no skips. Synthetic inputs only. AI-assisted.

@speed-y

speed-y commented Nov 23, 2026

Copy link
Copy Markdown

Tested 3a410f09fa03fe3cea0a74ca32ccd991cad4df09 on NVIDIA GeForce RTX 5080, driver 621.24, Windows 11, VS 2022 / Ninja RelWithDebInfo: pass

  • ctest -R 'vk_queue_slot_batch_reclaim|vk_predicate_capture|vk_predicated_copy': 3/3 passed
  • LEONIDA_VK_VALIDATION=1 + vk_queue_slot_batch_reclaim: passed (no Validation Error / VUID- lines in the log)

No game files. AI-assisted.

@LooperSalty

Copy link
Copy Markdown

Tested 3a410f09 on RTX 4070 Super, NVIDIA 610.88, Windows 11 (MSVC, RelWithDebInfo): pass.

  • ctest -C RelWithDebInfo: 246/248; the 2 failures (d3d12_indirect_dispatch_count, d3d12_indirect_rt) fail the same way on main 561b65a8 on this machine.
  • scripts/run_gpu_tests.py (llvm-mc built by scripts/build_llvm_mc.ps1): 183/232 passed, 48 skipped; ms_wave_info_s3 fails as on main.

No new failure compared with main.

@sennecools

Copy link
Copy Markdown

Tested 3a410f09f on Windows 11 Pro 26300, MSVC 2022 (Ninja, RelWithDebInfo), AMD Radeon RX 9070 XT (RDNA4, 32.0.32029.2010) and the Ryzen 9800X3D iGPU (RDNA2 gfx1036, 32.0.21045.29003). First AMD run on this PR. The runtime change is right; the new test fails on the 9070 XT.

What fails. vk_queue_slot_batch_reclaim_test on the RX 9070 XT without validation: 22 of 23 runs fail in one build folder and 6 of 12 in a second, clean one. Each failing case reports the last one or two burst batches as leaked, for example:

predicate: leaked GPU batch handles on non-reused slots (buf=1 mem=1 pool=1)
counted_dispatch: leaked GPU batch handles on non-reused slots (buf=2 mem=2 pool=0)

With LEONIDA_VK_VALIDATION=1 it passes 3/3 there, and on the iGPU it passes 26/26 (plus 6/6 in the second build).

Repro: build vk_queue_slot_batch_reclaim_test, set LEONIDA_VK_DEVICE=9070 and VK_LOADER_LAYERS_DISABLE=~implicit~, leave validation off, run the exe 20 times.

Why. With temporary hooks on vkQueueSubmit, vkQueueWaitIdle and vkGetSemaphoreCounterValue: the burst signals progress 1..4, vkQueueWaitIdle returns VK_SUCCESS, and the queue's progress timeline then reads 3, 3, 4. So on this driver the timeline can lag the idle by a read or two. When acquire() runs in that gap, recycle_finished() correctly leaves the last slots alone, and the test counts them as leaked. queue.idle() is only vkQueueWaitIdle; it does not wait on the timeline.

Fix (test only), in case it helps: wait on the timeline after the idle, in run_burst_and_check and in case 4. It is one commit on top of this head: sennecools@25634c2 (branch sennecools:assist-4787-timeline-wait, cherry-pick or copy as you like).

             const bool idled = queue.idle();
+            // vkQueueWaitIdle can return before the queue's progress timeline reads the last ticket (AMD Windows
+            // 32.0.32029: it read 3 of 4 right after the idle), so wait on that timeline before acquire() reads it.
+            context.wait_retire_ticket(context.submitted_retire_ticket());
             const bool idl = queue.idle();
+            context.wait_retire_ticket(context.submitted_retire_ticket());

With it: 20/20 on each GPU in both build folders, and 3/3 with validation on each.

Negative control. With vulkan_backend.h from 561b65a83, the original test and the fixed one both fail 3/3 on each GPU with the full count (predicate buf=3 mem=3 pool=3, predicated_copy 3/3/6, counted_dispatch 3/3/0), so the test still catches the bug this PR fixes.

Not run: the full ctest at this head. Merges cleanly onto current main.

Synthetic tests only, no game files. AI-assisted.

@SlopTart

Copy link
Copy Markdown

Linux AMD data point at head 3a410f09f - RX 7900 XT (RADV NAVI31, RDNA3), Mesa 26.2.5, kernel 7.2.15, Arch/Omarchy, Ninja/cmake RelWithDebInfo-equivalent: clean.

So the current picture: passes on NVIDIA (3080 Ti, 4070 Super, 5080, 5090 - Windows) and RADV/RDNA3 (Linux, here); fails 22/23 on ADRENALIN-Windows/RDNA4 (9070 XT) and the iGPU (gfx1036). That splits by driver family + generation, not by PR: something in AMD's Windows driver on RDNA4 completes/sequences CommandSlots differently enough that the reclaim assumption bites there. Worth stating explicitly in the PR description so nobody "fixes" the test into a Windows-NVIDIA-only shape.

No game files - ctest fixtures, bare hardware. Posted by Hermes (agent) on my machine (RX 7900 XT, Arch; also the boxb/GTX 1070 reporter).

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.