vulkan_memory_budget: saturate multi-heap uint64_t sums in sum_vram_budget #2333

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

@⁠narekshaghoyandev on GitHub · 2026-11-26 14:00 UTC · original
narekshaghoyandev:ubiq-vulkan-memory-budget-saturate → main · +60 −3 · 2 files · 1 commits · open

Prevents uint64_t wraparound when summing multiple Vulkan memory heaps in leonida::sum_vram_budget() (runtime/src/vulkan_memory_budget.h):

  • Previously, out.local_usage += ..., out.local_budget += ..., and out.shared_usage += ... could wrap around modulo 2^{64} when summing large heap figures, even wrapping local_budget to 0 and falsely marking out.available = false ("VRAM n/a").
  • Use saturating addition (UINT64_MAX cap) for local_usage, local_budget, and shared_usage.
  • Added standalone unit test runtime/test/vulkan_memory_budget_test.cpp.

Verification

  • g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp passed
  • g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp passed
  • Negative control: new test failed on line 44 (saturated.available, exit 1) against unmodified ubiq/main
  • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_memory_budget.h runtime/test/vulkan_memory_budget_test.cpp clean

Claimed in the coordination issue as: RT-VULKAN-MEMORY-BUDGET-SATURATE (#1608)
AI-assisted: yes (Gemini)

**@⁠narekshaghoyandev** on GitHub · 2026-11-26 14:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2333) `narekshaghoyandev:ubiq-vulkan-memory-budget-saturate` → `main` · +60 −3 · 2 files · 1 commits · open Prevents `uint64_t` wraparound when summing multiple Vulkan memory heaps in `leonida::sum_vram_budget()` (`runtime/src/vulkan_memory_budget.h`): - Previously, `out.local_usage += ...`, `out.local_budget += ...`, and `out.shared_usage += ...` could wrap around modulo $2^{64}$ when summing large heap figures, even wrapping `local_budget` to `0` and falsely marking `out.available = false` (`"VRAM n/a"`). - Use saturating addition (`UINT64_MAX` cap) for `local_usage`, `local_budget`, and `shared_usage`. - Added standalone unit test `runtime/test/vulkan_memory_budget_test.cpp`. ### Verification - `g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp` passed - `g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Wno-missing-field-initializers -Iruntime/src runtime/test/vulkan_memory_budget_test.cpp` passed - Negative control: new test failed on line 44 (`saturated.available`, exit 1) against unmodified `ubiq/main` - `python3 -I scripts/check_no_game_data.py runtime/src/vulkan_memory_budget.h runtime/test/vulkan_memory_budget_test.cpp` clean Claimed in the coordination issue as: RT-VULKAN-MEMORY-BUDGET-SATURATE (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) AI-assisted: yes (Gemini)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:01:36 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 15:40 UTC · original · reviewed

Ran vulkan_memory_budget_test.cpp directly with GCC 11 and UBSan on Ubuntu/WSL. All cases, including saturated multi-heap totals, passed with no sanitizer report. Could this executable be registered in CTest? I found no current test target for the new source.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 15:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2333#pullrequestreview-6076920276) · **reviewed** Ran `vulkan_memory_budget_test.cpp` directly with GCC 11 and UBSan on Ubuntu/WSL. All cases, including saturated multi-heap totals, passed with no sanitizer report. Could this executable be registered in CTest? I found no current test target for the new source. AI-assisted.
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 19:40 UTC · original · reviewed

macOS arm64 check of #2333 (vulkan_memory_budget saturation). A two-file change with no device dependency; I built its new test by hand because the build system does not - see the finding.

Tested head 6376bbc20 merged onto main bdbc7e923 as merge f7ba677a4 (clean, no conflicts).

Environment: Apple M3 Pro arm64, macOS 27.2.1, Apple clang 21.0.0, CMake 4.5.0, CPython 3.15.2, SPIRV-Tools v2026.4, Vulkan headers from /opt/homebrew/include (Vulkan-Headers 1.4.363.0).

The fix and its test

  • runtime/src/vulkan_memory_budget.h:19-37: add_sat clamps each heap's heapUsage/heapBudget contribution at UINT64_MAX instead of wrapping, and available still keys off local_budget != 0, so wide multi-heap devices keep reporting a figure. That is the right shape: the only consumers are the HUD's display (a ratio) and the null-query guard, and a saturated numerator/denominator is a saner answer than a wrapped one.
  • I compiled and ran the new test directly (clang++ -std=c++17 -I runtime -I /opt/homebrew/include runtime/test/vulkan_memory_budget_test.cpp): prints vulkan_memory_budget_test: ok, exit 0.
  • NC (keep the new test, revert only runtime/src/vulkan_memory_budget.h to main): compile OK, run fails at line 44: saturated.available - the wrapped local_budget becomes 0 and the code reports "VRAM n/a". The regression is real and the test detects it.

Finding: the new test is never built or run - it is not in runtime/CMakeLists.txt
runtime/test/vulkan_memory_budget_test.cpp is a new file, but runtime/CMakeLists.txt has no add_executable(vulkan_memory_budget_test ...) or add_test for it (I grepped the source list and the test list; ctest --test-dir build2304 -N shows 112 tests and none mentions memory_budget). Every other test in runtime/test is registered explicitly (title_hud_test at :852-859, list_value_wait_budget_test at :1508-1509), and there is no glob, so this file is dead in every build: ctest, the Windows runtime job and Wine will all skip it, which is exactly the regression coverage the PR is adding. The existing guard tests/test_ctest_registration.py cannot see it - its test_all_test_executables_have_an_add_test compares targets that exist against add_test commands, so a file with no target at all is invisible (it passes on this merge; I ran it).

Two things I would ask for in the same PR:

  1. Register the target. It needs only the Vulkan headers (target_link_libraries(vulkan_memory_budget_test PRIVATE Vulkan::Headers)), so no WIN32/MSVC guard is needed and it runs on macOS, Linux and Windows alike.
  2. Close the hole in tests/test_ctest_registration.py: compare runtime/test/*_test.cpp against the target names in the CMake source, and fail on a file with no add_executable. Otherwise this class of "test that never runs" will keep getting through, and the PR's own body can say "tested" while CI tests nothing.

Not run: the Windows-only title HUD test (title_hud_test links user32/psapi) and the full ctest suite, neither of which is affected by this header. No GPU work here.

python -I scripts/check_no_game_data.py on both changed files: clean.

No game files. AI-assisted.

**@⁠wavehs** on GitHub · 2026-11-26 19:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2333#pullrequestreview-6079663749) · **reviewed** **macOS arm64 check of #2333** (vulkan_memory_budget saturation). A two-file change with no device dependency; I built its new test by hand because the build system does not - see the finding. Tested head `6376bbc20` merged onto `main` `bdbc7e923` as merge `f7ba677a4` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, Apple clang 21.0.0, CMake 4.5.0, CPython 3.15.2, SPIRV-Tools v2026.4, Vulkan headers from `/opt/homebrew/include` (Vulkan-Headers 1.4.363.0). **The fix and its test** - `runtime/src/vulkan_memory_budget.h:19-37`: `add_sat` clamps each heap's `heapUsage`/`heapBudget` contribution at `UINT64_MAX` instead of wrapping, and `available` still keys off `local_budget != 0`, so wide multi-heap devices keep reporting a figure. That is the right shape: the only consumers are the HUD's display (a ratio) and the null-query guard, and a saturated numerator/denominator is a saner answer than a wrapped one. - I compiled and ran the new test directly (`clang++ -std=c++17 -I runtime -I /opt/homebrew/include runtime/test/vulkan_memory_budget_test.cpp`): prints `vulkan_memory_budget_test: ok`, exit 0. - NC (keep the new test, revert only `runtime/src/vulkan_memory_budget.h` to `main`): compile OK, run fails at `line 44: saturated.available` - the wrapped `local_budget` becomes 0 and the code reports "VRAM n/a". The regression is real and the test detects it. **Finding: the new test is never built or run - it is not in `runtime/CMakeLists.txt`** `runtime/test/vulkan_memory_budget_test.cpp` is a new file, but `runtime/CMakeLists.txt` has no `add_executable(vulkan_memory_budget_test ...)` or `add_test` for it (I grepped the source list and the test list; `ctest --test-dir build2304 -N` shows 112 tests and none mentions `memory_budget`). Every other test in `runtime/test` is registered explicitly (`title_hud_test` at `:852-859`, `list_value_wait_budget_test` at `:1508-1509`), and there is no glob, so this file is dead in every build: ctest, the Windows runtime job and Wine will all skip it, which is exactly the regression coverage the PR is adding. The existing guard `tests/test_ctest_registration.py` cannot see it - its `test_all_test_executables_have_an_add_test` compares *targets that exist* against `add_test` commands, so a file with no target at all is invisible (it passes on this merge; I ran it). Two things I would ask for in the same PR: 1. Register the target. It needs only the Vulkan headers (`target_link_libraries(vulkan_memory_budget_test PRIVATE Vulkan::Headers)`), so no `WIN32`/`MSVC` guard is needed and it runs on macOS, Linux and Windows alike. 2. Close the hole in `tests/test_ctest_registration.py`: compare `runtime/test/*_test.cpp` against the target names in the CMake source, and fail on a file with no `add_executable`. Otherwise this class of "test that never runs" will keep getting through, and the PR's own body can say "tested" while CI tests nothing. **Not run**: the Windows-only title HUD test (`title_hud_test` links `user32`/`psapi`) and the full ctest suite, neither of which is affected by this header. No GPU work here. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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