[PR #1863] runtime: bound prepared_ mesh-stats loop, recycle all finished slot batches, handle zero-count indirect dispatch #1875

Open
opened 2026-11-26 21:19:15 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1863
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-queue-recycle-and-stats-bounds


📝 Commits (1)

  • e31049c runtime: bound prepared_ mesh-stats loop, recycle all finished slot batches, handle zero-count indirect dispatch

📊 Changes

4 files changed (+597 additions, -26 deletions)

View changed files

📝 runtime/CMakeLists.txt (+7 -0)
📝 runtime/src/vulkan_backend.h (+7 -7)
📝 runtime/src/vulkan_replay.h (+22 -19)
➕ runtime/test/queue_recycle_and_stats_test.cpp (+561 -0)

📄 Description

Item ID: RT-QUEUE-RECYCLE-AND-STATS-BOUNDS
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163162118

Summary

Fixes three correctness and resource-retention bugs in VulkanQueue (runtime/src/vulkan_backend.h) and command-list replay (runtime/src/vulkan_replay.h):

  1. Bound statistics->mesh collection to lists.size() in VulkanQueue::execute_now() (runtime/src/vulkan_backend.h:3151)
    • prepared_ is intentionally kept at the high-water mark of previous ExecuteCommandLists batch sizes (vulkan_backend.h:2939-2942), and every other loop in execute_now() bounds iteration to lists.size().
    • Line 3151 iterated for (const auto& list : prepared), pulling stale statistics_mesh entries from prepared_[lists.size()..] into statistics->mesh. After a multi-list batch containing a mesh draw inside a pipeline-statistics query, a later smaller vertex-only statistics batch saw the stale 1 entry and needlessly created mesh_pool and primitives_pool (and appended extra entries past statistics_segments).
  2. Release completed helper batches for all finished CommandSlots in VulkanQueue::recycle_finished() (runtime/src/vulkan_backend.h:3835-3853)
    • recycle_finished() freed segments, geometry, khr_instances, khr_as_queries, and events across every completed slot (slot.ticket <= done), but left predicate, predicated_copy, counted_dispatch, and indirect_clamp alive until a slot was specifically chosen as reuse in acquire().
    • Because recycle_finished() always returns the first completed slot, slots buffers_[2..N-1] after a submission burst stayed idle while pinning PredicateReducer::Batch (VkBuffer + VkDeviceMemory + VkDescriptorPool), PredicatedBufferCopier::Batch (VkDescriptorPools), IndirectDispatchCounter::Batch (VkBuffer + VkDeviceMemory), and IndirectArgumentClamp::Batch (counter slots out of the 4,096 cap) until queue destruction.
    • Moving the four .reset() calls into recycle_finished() frees all finished slots' batches immediately on the next acquire().
  3. Short-circuit zero-count ExecuteIndirect(dispatch) before clamp/counted-dispatch helpers (runtime/src/vulkan_replay.h:3844, 4936)
    • prepare() explicitly allows ind->max_count == 0 as a no-op (vulkan_replay.h:3821), but previously fell through to prepare_indirect_clamp / prepare_counted_dispatch which both reject !max_count and failed the entire list. Now returns early (after validating arguments, pipeline, and vk.dispatch_indirect) and skips emit for !ind->max_count.

Tests

Added pure-CPU mock-Vulkan test runtime/test/queue_recycle_and_stats_test.cpp (registered as CTest queue_recycle_and_stats under if(TARGET Vulkan::Headers) in runtime/CMakeLists.txt).

Verified locally on macOS arm64 (Apple Clang) using the pinned Khronos Vulkan-Headers (6802bb4733b63ed5efd3adb308a6c885ef180ea1):

  • queue_recycle_and_stats_test: PASS under ASan+UBSan, PASS under TSan (0 races), PASS under -O2 (3 consecutive runs), -Wall -Wextra -Werror.
  • Existing replay_query_predication_test: PASS under ASan+UBSan.
  • Mutation check against main's vulkan_backend.h + vulkan_replay.h: fails all 7 bug assertions (stale mesh_pool/primitives_pool creation on vertex-only batch, and 3/4 leaked PredicateReducer::Batch and IndirectDispatchCounter::Batch slots).
  • Hygiene: python3 -I scripts/check_no_game_data.py ... and python3 -I tests/test_text_write_line_endings.py pass.

Disclosure

  • AI-assisted: yes
  • GPUs tested: none (macOS arm64 host; CPU mock-Vulkan tests verified under ASan/UBSan/TSan; Windows/MSVC + GPU tests rely on CI / reviewers)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1863 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-queue-recycle-and-stats-bounds` --- ### 📝 Commits (1) - [`e31049c`](https://github.com/ubiquitarianism/leonida/commit/e31049cc7500ba606ab8b3e4fea951172f403b5f) runtime: bound prepared_ mesh-stats loop, recycle all finished slot batches, handle zero-count indirect dispatch ### 📊 Changes **4 files changed** (+597 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+7 -0) 📝 `runtime/src/vulkan_backend.h` (+7 -7) 📝 `runtime/src/vulkan_replay.h` (+22 -19) ➕ `runtime/test/queue_recycle_and_stats_test.cpp` (+561 -0) </details> ### 📄 Description Item ID: `RT-QUEUE-RECYCLE-AND-STATS-BOUNDS` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163162118 ## Summary Fixes three correctness and resource-retention bugs in `VulkanQueue` (`runtime/src/vulkan_backend.h`) and command-list replay (`runtime/src/vulkan_replay.h`): 1. **Bound `statistics->mesh` collection to `lists.size()` in `VulkanQueue::execute_now()` (`runtime/src/vulkan_backend.h:3151`)** - `prepared_` is intentionally kept at the high-water mark of previous `ExecuteCommandLists` batch sizes (`vulkan_backend.h:2939-2942`), and every other loop in `execute_now()` bounds iteration to `lists.size()`. - Line 3151 iterated `for (const auto& list : prepared)`, pulling stale `statistics_mesh` entries from `prepared_[lists.size()..]` into `statistics->mesh`. After a multi-list batch containing a mesh draw inside a pipeline-statistics query, a later smaller vertex-only statistics batch saw the stale `1` entry and needlessly created `mesh_pool` and `primitives_pool` (and appended extra entries past `statistics_segments`). 2. **Release completed helper batches for all finished `CommandSlot`s in `VulkanQueue::recycle_finished()` (`runtime/src/vulkan_backend.h:3835-3853`)** - `recycle_finished()` freed `segments`, `geometry`, `khr_instances`, `khr_as_queries`, and `events` across every completed slot (`slot.ticket <= done`), but left `predicate`, `predicated_copy`, `counted_dispatch`, and `indirect_clamp` alive until a slot was specifically chosen as `reuse` in `acquire()`. - Because `recycle_finished()` always returns the first completed slot, slots `buffers_[2..N-1]` after a submission burst stayed idle while pinning `PredicateReducer::Batch` (`VkBuffer` + `VkDeviceMemory` + `VkDescriptorPool`), `PredicatedBufferCopier::Batch` (`VkDescriptorPool`s), `IndirectDispatchCounter::Batch` (`VkBuffer` + `VkDeviceMemory`), and `IndirectArgumentClamp::Batch` (counter slots out of the 4,096 cap) until queue destruction. - Moving the four `.reset()` calls into `recycle_finished()` frees all finished slots' batches immediately on the next `acquire()`. 3. **Short-circuit zero-count `ExecuteIndirect(dispatch)` before clamp/counted-dispatch helpers (`runtime/src/vulkan_replay.h:3844, 4936`)** - `prepare()` explicitly allows `ind->max_count == 0` as a no-op (`vulkan_replay.h:3821`), but previously fell through to `prepare_indirect_clamp` / `prepare_counted_dispatch` which both reject `!max_count` and failed the entire list. Now returns early (after validating arguments, pipeline, and `vk.dispatch_indirect`) and skips emit for `!ind->max_count`. ## Tests Added pure-CPU mock-Vulkan test `runtime/test/queue_recycle_and_stats_test.cpp` (registered as CTest `queue_recycle_and_stats` under `if(TARGET Vulkan::Headers)` in `runtime/CMakeLists.txt`). Verified locally on macOS arm64 (Apple Clang) using the pinned Khronos `Vulkan-Headers` (`6802bb4733b63ed5efd3adb308a6c885ef180ea1`): - `queue_recycle_and_stats_test`: PASS under ASan+UBSan, PASS under TSan (0 races), PASS under `-O2` (3 consecutive runs), `-Wall -Wextra -Werror`. - Existing `replay_query_predication_test`: PASS under ASan+UBSan. - Mutation check against `main`'s `vulkan_backend.h` + `vulkan_replay.h`: fails all 7 bug assertions (stale `mesh_pool`/`primitives_pool` creation on vertex-only batch, and 3/4 leaked `PredicateReducer::Batch` and `IndirectDispatchCounter::Batch` slots). - Hygiene: `python3 -I scripts/check_no_game_data.py ...` and `python3 -I tests/test_text_write_line_endings.py` pass. ## Disclosure - **AI-assisted**: yes - **GPUs tested**: none (macOS arm64 host; CPU mock-Vulkan tests verified under ASan/UBSan/TSan; Windows/MSVC + GPU tests rely on CI / reviewers) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1875
No description provided.