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

Open
github-bot wants to merge 1 commit from github-bot/gh-1863 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 13:27 UTC · original
IlyassRS3:fix/rt-queue-recycle-and-stats-bounds → main · +597 −26 · 4 files · 1 commits · open

Item ID: RT-QUEUE-RECYCLE-AND-STATS-BOUNDS
Claim: #1608

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)
**@⁠IlyassRS3** on GitHub · 2026-11-25 13:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1863) `IlyassRS3:fix/rt-queue-recycle-and-stats-bounds` → `main` · +597 −26 · 4 files · 1 commits · open Item ID: `RT-QUEUE-RECYCLE-AND-STATS-BOUNDS` Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ## 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)
Fix three replay/queue bugs in runtime/src/vulkan_backend.h and runtime/src/vulkan_replay.h:
- VulkanQueue::execute_now() (vulkan_backend.h:3151): iterate only the active lists.size() entries of retained prepared_ when populating statistics->mesh, instead of reading stale prepared_[lists.size()..] from earlier larger ExecuteCommandLists batches (which falsely triggered mesh/primitives statistics query pool creation on vertex-only batches).
- VulkanQueue::recycle_finished() (vulkan_backend.h:3842): reset predicate, predicated_copy, counted_dispatch, and indirect_clamp batches for every completed CommandSlot (slot.ticket <= done), not only the single reuse slot returned in acquire(). Otherwise idle slots after a submission burst leak VkBuffer, VkDeviceMemory, VkDescriptorPool, and IndirectArgumentClamp counter slots until queue teardown.
- prepare()/emit_commands() (vulkan_replay.h:3844, 4936): short-circuit ExecuteIndirect(dispatch) with max_count == 0 before prepare_indirect_clamp / prepare_counted_dispatch (which both reject !max_count), matching the zero-count no-op contract at vulkan_replay.h:3821.

Item ID: RT-QUEUE-RECYCLE-AND-STATS-BOUNDS
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163162118
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:14 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 13:27 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 13:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1863#pullrequestreview-6058470329) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 19:14 UTC · original

Thanks. Two of the three fixes hold up:

  • Stats loop: the statistics->mesh loop did read stale prepared_ entries past lists.size(). The fix is right, and the test catches it on main.
  • Recycling: releasing every finished slot's helper batches in recycle_finished() is safe. A slot's ticket is set only after a successful submit, its batches are attached after that, and only slots at or below the timeline value are released.
  • Zero-count dispatch: this case can't happen. CommandList::execute_indirect() returns before append when max_count is 0 (runtime/src/command_list.h:954), so prepare() and emit_commands() never see such a command, and main already treats it as a no-op. Please drop the vulkan_replay.h changes, or show a path that reaches them, with a test that fails on main. If you drop them, please also take "handle zero-count indirect dispatch" out of the title and description.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 19:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1863#issuecomment-7172012096) Thanks. Two of the three fixes hold up: - **Stats loop:** the `statistics->mesh` loop did read stale `prepared_` entries past `lists.size()`. The fix is right, and the test catches it on `main`. - **Recycling:** releasing every finished slot's helper batches in `recycle_finished()` is safe. A slot's ticket is set only after a successful submit, its batches are attached after that, and only slots at or below the timeline value are released. - **Zero-count dispatch:** this case can't happen. `CommandList::execute_indirect()` returns before `append` when `max_count` is 0 (`runtime/src/command_list.h:954`), so `prepare()` and `emit_commands()` never see such a command, and `main` already treats it as a no-op. Please drop the `vulkan_replay.h` changes, or show a path that reaches them, with a test that fails on `main`. If you drop them, please also take "handle zero-count indirect dispatch" out of the title and description. AI-assisted (Claude Code, Claude Opus 5.5).
This pull request has changes conflicting with the target branch.
  • runtime/CMakeLists.txt
  • runtime/src/vulkan_replay.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1863/head:github-bot/gh-1863'
git switch 'github-bot/gh-1863'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1863'
git switch 'github-bot/gh-1863'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1863'
git switch 'github-bot/gh-1863'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1863'
git switch 'main'
git merge --squash 'github-bot/gh-1863'
git switch 'main'
git merge --ff-only 'github-bot/gh-1863'
git switch 'main'
git merge 'github-bot/gh-1863'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.