runtime: preserve in-flight GPU label targets when expanding a non-heap region (RT-GPU-LABEL-MEMORY-EXPAND) #1788

Merged
IlyassRS3 merged 1 commit from upstream-pr/1788 into main 2026-11-29 03:35:16 +00:00
IlyassRS3 commented 2026-11-25 12:11:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T12:11:27Z.


Summary

Fixes leonida::label::Memory::track() in runtime/src/gpu_label_memory.h so growing a tracked non-heap region at the same base and allocation_base updates Region::end in place instead of overwriting regions_[base] and dropping in-flight GPU completion value writes.

Root cause

In Memory::track(base, bytes, allocation_base, heap):

  • Previously, if a non-heap region at base with the same allocation_base already existed and a new track() call covered a larger range (end > r.end), the early-return check end <= r.end failed and line 41 executed regions_[base] = std::make_shared<Region>(...).
  • Replacing regions_[base] destroyed the previous Region shared_ptr (and caused it->second != region in publish()), silently dropping any in-flight label::Target GPU completion writes captured from [base, old_end) before the expansion.

Changes

  • runtime/src/gpu_label_memory.h:
    • When !heap and an existing non-heap region at the same base and allocation_base is expanded (base == r.begin && end > r.end), update r.end = end in place under mutex_.
    • Preserves existing Region identity and generation (so stale pre-decommit targets remain rejected, live post-decommit targets survive further expansion, adjacent pages stay isolated from each other's partial decommits, and heap regions or distinct allocation_base reservations still allocate a fresh Region).
  • runtime/test/gpu_label_memory_test.cpp & runtime/CMakeLists.txt:
    • Add portable gpu_label_memory CTest covering in-place expansion, post-partial-release generation retention, subrange and adjacent-page isolation, heap and new-reservation invalidation, base-0 GPU arena tracking, alignment/overflow bounds, and concurrent expand/capture/publish under TSan.

Verification

  • AI-assisted: yes
  • GPUs tested: none (portable standard-library C++20 header + test, verified locally on macOS Apple Silicon ARM64 with Clang; Windows MSVC build and Windows-only gpu_label_store / xbox_gpu_label tests rely on Windows CI).
  • Commands run:
    • clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/gpu_label_memory_test.cpp — pass
    • clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=thread -O1 runtime/test/gpu_label_memory_test.cpp — pass (zero data races)
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/gpu_label_memory_test.cpp — pass
    • Mutation test compiling gpu_label_memory_test.cpp against unpatched main:runtime/src/gpu_label_memory.h — fails on test_expand_preserves_inflight_targets, confirming regression coverage
    • python3 -I scripts/check_no_game_data.py runtime/src/gpu_label_memory.h runtime/test/gpu_label_memory_test.cpp runtime/CMakeLists.txt — pass
    • python3 -I tests/test_text_write_line_endings.py — 3/3 pass
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5491072109 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1788). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T12:11:27Z. --- ## Summary Fixes `leonida::label::Memory::track()` in `runtime/src/gpu_label_memory.h` so growing a tracked non-heap region at the same `base` and `allocation_base` updates `Region::end` in place instead of overwriting `regions_[base]` and dropping in-flight GPU completion value writes. - **Coordination item**: `RT-GPU-LABEL-MEMORY-EXPAND` - **Claim**: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162005947 ### Root cause In `Memory::track(base, bytes, allocation_base, heap)`: - Previously, if a non-heap region at `base` with the same `allocation_base` already existed and a new `track()` call covered a larger range (`end > r.end`), the early-return check `end <= r.end` failed and line 41 executed `regions_[base] = std::make_shared<Region>(...)`. - Replacing `regions_[base]` destroyed the previous `Region` `shared_ptr` (and caused `it->second != region` in `publish()`), silently dropping any in-flight `label::Target` GPU completion writes captured from `[base, old_end)` before the expansion. ### Changes - [`runtime/src/gpu_label_memory.h`](runtime/src/gpu_label_memory.h): - When `!heap` and an existing non-heap region at the same `base` and `allocation_base` is expanded (`base == r.begin && end > r.end`), update `r.end = end` in place under `mutex_`. - Preserves existing `Region` identity and `generation` (so stale pre-decommit targets remain rejected, live post-decommit targets survive further expansion, adjacent pages stay isolated from each other's partial decommits, and heap regions or distinct `allocation_base` reservations still allocate a fresh `Region`). - [`runtime/test/gpu_label_memory_test.cpp`](runtime/test/gpu_label_memory_test.cpp) & [`runtime/CMakeLists.txt`](runtime/CMakeLists.txt): - Add portable `gpu_label_memory` CTest covering in-place expansion, post-partial-release generation retention, subrange and adjacent-page isolation, heap and new-reservation invalidation, base-0 GPU arena tracking, alignment/overflow bounds, and concurrent expand/capture/publish under TSan. ## Verification - **AI-assisted**: yes - **GPUs tested**: none (portable standard-library C++20 header + test, verified locally on macOS Apple Silicon ARM64 with Clang; Windows MSVC build and Windows-only `gpu_label_store` / `xbox_gpu_label` tests rely on Windows CI). - Commands run: - `clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined -O1 runtime/test/gpu_label_memory_test.cpp` — pass - `clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=thread -O1 runtime/test/gpu_label_memory_test.cpp` — pass (zero data races) - `clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/gpu_label_memory_test.cpp` — pass - Mutation test compiling `gpu_label_memory_test.cpp` against unpatched `main:runtime/src/gpu_label_memory.h` — fails on `test_expand_preserves_inflight_targets`, confirming regression coverage - `python3 -I scripts/check_no_game_data.py runtime/src/gpu_label_memory.h runtime/test/gpu_label_memory_test.cpp runtime/CMakeLists.txt` — pass - `python3 -I tests/test_text_write_line_endings.py` — 3/3 pass
copilot-pull-request-reviewer[bot] commented 2026-11-25 12:11:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T12:11:39Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6057366929 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1788#pullrequestreview-6057366929). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T12:11:39Z. **Review:** COMMENTED --- 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.
Tyrannys commented 2026-11-25 16:18:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-25T16:18:37Z.


Independent verification of head 0267290f9c7ec35bece4e0b81a132f7d8310e4e6, merged onto current main (b0f3d3998) without conflicts.

Windows 11 Pro, RTX 4070 SUPER (NVIDIA 621.01), MSVC 19.52.37029, RelWithDebInfo.

  • With the PR: gpu_label_memory (new), plus neighbours gpu_label_store and xbox_gpu_label_completion: 3/3 pass.
  • The new test against main's gpu_label_memory.h: fails at line 56: publish_ok(mem, t4, kBase + 0x10, 4, kAlloc) (the region is replaced on growth, so the in-flight target is dropped), which is the bug the PR fixes.

Diff read: Region::end is changed under the same mutex_ that capture/publish take, and only for the same base and allocation base, so targets keep their owner. No problem found.

No game files used. AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167775395 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1788#issuecomment-7167775395). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-25T16:18:37Z. --- **Independent verification** of head `0267290f9c7ec35bece4e0b81a132f7d8310e4e6`, merged onto current `main` (b0f3d3998) without conflicts. Windows 11 Pro, RTX 4070 SUPER (NVIDIA 621.01), MSVC 19.52.37029, RelWithDebInfo. - With the PR: `gpu_label_memory` (new), plus neighbours `gpu_label_store` and `xbox_gpu_label_completion`: **3/3 pass**. - The new test against `main`'s `gpu_label_memory.h`: **fails** at `line 56: publish_ok(mem, t4, kBase + 0x10, 4, kAlloc)` (the region is replaced on growth, so the in-flight target is dropped), which is the bug the PR fixes. Diff read: `Region::end` is changed under the same `mutex_` that `capture`/`publish` take, and only for the same base and allocation base, so targets keep their owner. No problem found. No game files used. AI-assisted (Claude Code).
zmasi commented 2026-11-28 23:06:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T23:06:39Z.


Native Windows 11 + MSVC 19.44 + RTX 3090 Khronos Validation Verification (0267290f9c + origin/main adfb3821a2)

Verdict: PASS (0 blockers, 0 /W4 warnings, 0 Vulkan validation errors on RTX 3090)

Environment & Merge Check

  • Host: Windows 11 (10.0.26200), MSVC 19.44.35228 (x64, /W4 /permissive- /utf-8 /O2 /EHsc), NVIDIA GeForce RTX 3090, Khronos VK_LAYER_KHRONOS_validation 1.4.363.1 (LEONIDA_VK_VALIDATION=1, VK_VALIDATE_SYNC=1)
  • Merge hygiene: Clean merge onto origin/main (adfb3821a2, git merge-tree --write-tree rc=0).

Red -> Green & RTX 3090 GPU Results

  • Red (origin/main runtime/src/gpu_label_memory.h + PR #1788 runtime/test/gpu_label_memory_test.cpp):
    • Exited 1 at line 56: publish_ok(mem, t4, kBase + 0x10, 4, kAlloc) because replacing regions_[base] dropped in-flight targets captured prior to non-heap region growth.
  • Green (PR #1788 + origin/main):
    • gpu_label_memory_test.exe: PASS (gpu_label_memory_test: ok, 0.13 s, 0 /W4 warnings).
    • gpu_label_store_test.exe: PASS (gpu_label_store_test: ok (24 checks, 0 failures), 0.41 s).
    • xbox_gpu_label_test.exe (xbox_gpu_label_completion on NVIDIA GeForce RTX 3090 with LEONIDA_VK_VALIDATION=1 + VK_VALIDATE_SYNC=1): PASS (rc=0, 0 VUID- errors — GPU completion values, two real timestamp samples, resubmission and stale allocation guard passed).

Diff Audit Notes

  • Updating r.end = end in place under mutex_ only when !heap && !r.heap && r.allocation_base == allocation_base && base == r.begin && end > r.end preserves existing shared_ptr<Region> ownership and generation counters while leaving heap allocations, new reservations, and adjacent pages isolated.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7247206296 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1788#issuecomment-7247206296). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T23:06:39Z. --- ### Native Windows 11 + MSVC `19.44` + RTX 3090 Khronos Validation Verification (`0267290f9c` + `origin/main` `adfb3821a2`) **Verdict**: **PASS** (0 blockers, 0 `/W4` warnings, `0` Vulkan validation errors on RTX 3090) #### Environment & Merge Check - **Host**: Windows 11 (`10.0.26200`), MSVC `19.44.35228` (`x64`, `/W4 /permissive- /utf-8 /O2 /EHsc`), **NVIDIA GeForce RTX 3090**, Khronos `VK_LAYER_KHRONOS_validation` `1.4.363.1` (`LEONIDA_VK_VALIDATION=1`, `VK_VALIDATE_SYNC=1`) - **Merge hygiene**: Clean merge onto `origin/main` (`adfb3821a2`, `git merge-tree --write-tree` `rc=0`). #### Red -> Green & RTX 3090 GPU Results - **Red** (`origin/main` `runtime/src/gpu_label_memory.h` + PR `#1788` `runtime/test/gpu_label_memory_test.cpp`): - Exited `1` at `line 56: publish_ok(mem, t4, kBase + 0x10, 4, kAlloc)` because replacing `regions_[base]` dropped in-flight targets captured prior to non-heap region growth. - **Green** (`PR #1788 + origin/main`): - `gpu_label_memory_test.exe`: **PASS** (`gpu_label_memory_test: ok`, `0.13 s`, `0` `/W4` warnings). - `gpu_label_store_test.exe`: **PASS** (`gpu_label_store_test: ok (24 checks, 0 failures)`, `0.41 s`). - `xbox_gpu_label_test.exe` (`xbox_gpu_label_completion` on **NVIDIA GeForce RTX 3090** with `LEONIDA_VK_VALIDATION=1` + `VK_VALIDATE_SYNC=1`): **PASS** (`rc=0`, `0` `VUID-` errors — `GPU completion values, two real timestamp samples, resubmission and stale allocation guard passed`). #### Diff Audit Notes - Updating `r.end = end` in place under `mutex_` only when `!heap && !r.heap && r.allocation_base == allocation_base && base == r.begin && end > r.end` preserves existing `shared_ptr<Region>` ownership and `generation` counters while leaving heap allocations, new reservations, and adjacent pages isolated. AI-assisted.
OreoFR commented 2026-11-29 01:17:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T01:17:56Z.


Verified by OreoFR

  • Base: main 48bae6cad + PR head 0267290f9 (tested as a merge)
  • Test: ctest -R ^gpu_label_memory$ (red keeps the PR's CMake registration of the new test)
  • Red on main: gpu_label_memory fails (publish_ok(mem, t4, kBase + 0x10, 4, kAlloc), line 56)
  • Green with the PR: gpu_label_memory passes
  • gta6rt link: OK (gta6rt.exe builds and links; dumpbin /dependents shows the same DLLs as main, no network DLL)
  • Verdict: READY (the PR also merges without conflict on the newer main 5825b02ff)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7248691304 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1788#issuecomment-7248691304). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T01:17:56Z. --- **Verified by OreoFR** - Base: main `48bae6cad` + PR head `0267290f9` (tested as a merge) - Test: `ctest -R ^gpu_label_memory$` (red keeps the PR's CMake registration of the new test) - Red on main: `gpu_label_memory` fails (`publish_ok(mem, t4, kBase + 0x10, 4, kAlloc)`, line 56) - Green with the PR: `gpu_label_memory` passes - gta6rt link: OK (`gta6rt.exe` builds and links; `dumpbin /dependents` shows the same DLLs as main, no network DLL) - Verdict: READY (the PR also merges without conflict on the newer main `5825b02ff`) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
DadaCap commented 2026-11-29 03:35:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-29T03:35:12Z.


Merging, thank you @IlyassRS3! OreoFR's agent verified this head merged with current main: the test fails on main and passes with the PR (https://github.com/ProjectLeonida/mandy/pull/1788#issuecomment-7248691304), and it still merges cleanly.

AI-assisted (Claude Code, Claude Opus 5.5), on behalf of DadaCap.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7250137536 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1788#issuecomment-7250137536). Originally posted by [DadaCap](https://github.com/DadaCap) on 2026-11-29T03:35:12Z. --- Merging, thank you @IlyassRS3! OreoFR's agent verified this head merged with current main: the test fails on main and passes with the PR (https://github.com/ProjectLeonida/mandy/pull/1788#issuecomment-7248691304), and it still merges cleanly. AI-assisted (Claude Code, Claude Opus 5.5), on behalf of DadaCap.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1788
No description provided.