d3d12: index TitleGpuPlacements in O(1) hash map and fix mip/layer overflow (RT-TITLE-GPU-PLACEMENTS-HASH-AND-OVERFLOW) #2022

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/2022 into main
IlyassRS3 commented 2026-11-25 18:19:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:19:51Z.


Claim: RT-TITLE-GPU-PLACEMENTS-HASH-AND-OVERFLOW (claim comment #7170709644 on #1608).

Problem

In runtime/shims/title_gpu_placements.h (held under descriptor_mutex for every placed-resource descriptor lookup):

  1. O(log N) red-black tree lookup: TitleGpuPlacements stored placements in a std::multimap<std::uint64_t, Resource*> even though every operation (add, remove, find) is an exact-VA lookup (never ordered iteration or range scan).
  2. Duplicate lookup when alias == va: find(va, alias, matches) probed {va, alias} unconditionally even when alias == va.
  3. 32-bit wrap in matches(const gfx10::Image&, const D3D12_RESOURCE_DESC&): image.first_mip + image.mip_count > d.MipLevels and image.first_layer + image.layer_count <= d.DepthOrArraySize wrapped uint32_t near UINT32_MAX (e.g. first_mip = 0xffffffffu, mip_count = 1 wrapped to 0 <= d.MipLevels and falsely matched).

Fix

  • Replace std::multimap with std::unordered_map<std::uint64_t, Bucket> where Bucket stores the single common placement inline (Resource* first) with an overflow std::vector<Resource*> extra for overlapping lifetimes at the same VA — zero extra allocation in the single-resource case, O(1) lookup, and exact newest-first (LIFO) search order preserved.
  • Skip null resources in add() and skip the second probe in find() when alias == va.
  • Replace addition checks in matches() with overflow-free subtraction (image.first_mip > d.MipLevels || image.mip_count > d.MipLevels - image.first_mip, and similarly for first_layer/layer_count).
  • Add regression checks in runtime/test/d3d12_existing_heap_test.cpp (0 CMakeLists.txt edits).

Benchmark (clang++ -std=c++20 -O2, bit-exact A/B, 16,384 placed resources x 500,000 find() calls)

  • Before (std::multimap): 42.60 ms
  • After (std::unordered_map + inline-1 LIFO bucket): 1.49 ms (28.54x faster)

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Compiled and ran runtime/shims/title_gpu_placements.h unit + A/B parity tests (verifying LIFO order, multi-element front/middle/back removal, va == alias single-probe, and wrapping mip/layer rejection vs old code) with minimal D3D12/DXGI stubs under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Ran python3 -I scripts/check_no_game_data.py runtime/shims/title_gpu_placements.h runtime/test/d3d12_existing_heap_test.cpp and python3 -I tests/test_text_write_line_endings.py: all passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496778612 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2022). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T18:19:51Z. --- Claim: `RT-TITLE-GPU-PLACEMENTS-HASH-AND-OVERFLOW` (claim comment `#7170709644` on #1608). ### Problem In `runtime/shims/title_gpu_placements.h` (held under `descriptor_mutex` for every placed-resource descriptor lookup): 1. **O(log N) red-black tree lookup**: `TitleGpuPlacements` stored placements in a `std::multimap<std::uint64_t, Resource*>` even though every operation (`add`, `remove`, `find`) is an exact-VA lookup (never ordered iteration or range scan). 2. **Duplicate lookup when `alias == va`**: `find(va, alias, matches)` probed `{va, alias}` unconditionally even when `alias == va`. 3. **32-bit wrap in `matches(const gfx10::Image&, const D3D12_RESOURCE_DESC&)`**: `image.first_mip + image.mip_count > d.MipLevels` and `image.first_layer + image.layer_count <= d.DepthOrArraySize` wrapped `uint32_t` near `UINT32_MAX` (e.g. `first_mip = 0xffffffffu, mip_count = 1` wrapped to `0 <= d.MipLevels` and falsely matched). ### Fix - Replace `std::multimap` with `std::unordered_map<std::uint64_t, Bucket>` where `Bucket` stores the single common placement inline (`Resource* first`) with an overflow `std::vector<Resource*> extra` for overlapping lifetimes at the same VA — zero extra allocation in the single-resource case, O(1) lookup, and exact newest-first (LIFO) search order preserved. - Skip null resources in `add()` and skip the second probe in `find()` when `alias == va`. - Replace addition checks in `matches()` with overflow-free subtraction (`image.first_mip > d.MipLevels || image.mip_count > d.MipLevels - image.first_mip`, and similarly for `first_layer`/`layer_count`). - Add regression checks in `runtime/test/d3d12_existing_heap_test.cpp` (0 `CMakeLists.txt` edits). ### Benchmark (`clang++ -std=c++20 -O2`, bit-exact A/B, 16,384 placed resources x 500,000 `find()` calls) - Before (`std::multimap`): `42.60 ms` - After (`std::unordered_map` + inline-1 LIFO bucket): `1.49 ms` (**28.54x faster**) ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Compiled and ran `runtime/shims/title_gpu_placements.h` unit + A/B parity tests (verifying LIFO order, multi-element front/middle/back removal, `va == alias` single-probe, and wrapping mip/layer rejection vs old code) with minimal D3D12/DXGI stubs under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Ran `python3 -I scripts/check_no_game_data.py runtime/shims/title_gpu_placements.h runtime/test/d3d12_existing_heap_test.cpp` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 18:20:06 +00:00 (Migrated from github.com)

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

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:6062781162 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2022#pullrequestreview-6062781162). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T18:20:06Z. **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.
zmasi commented 2026-11-28 11:36:21 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:36:21Z.


#2022 | head 6a65fe6a6a (merged cleanly with main 4a54aedcb6) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(d3d12_existing_heap|d3d12_host_prefix|d3d12_mapped_plane_async)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: d3d12_existing_heap fails (exit 8 at the wrapping 0xffffffffu + 1 mip/layer and va == alias single-probe checks in d3d12_existing_heap_test.cpp) | green on PR+main: 3/3 CTests pass (d3d12_existing_heap, d3d12_host_prefix, d3d12_mapped_plane_async) on RTX 3090 + WARP (0 new validation errors; d3d12_existing_heap logs pre-existing teardown VUID-vkDestroyDevice-device-05137 on both main and PR+main, while d3d12_host_prefix and d3d12_mapped_plane_async have 0 validation messages); check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238661987 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2022#issuecomment-7238661987). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T11:36:21Z. --- #2022 | head 6a65fe6a6ad9a963fe6ec95b2e4729c2fef710ab (merged cleanly with main 4a54aedcb6b66525e0c9f72a6ea10263337c9141) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(d3d12_existing_heap|d3d12_host_prefix|d3d12_mapped_plane_async)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `d3d12_existing_heap` fails (exit 8 at the wrapping `0xffffffffu + 1` mip/layer and `va == alias` single-probe checks in `d3d12_existing_heap_test.cpp`) | green on PR+main: 3/3 CTests pass (`d3d12_existing_heap`, `d3d12_host_prefix`, `d3d12_mapped_plane_async`) on RTX 3090 + WARP (`0` new validation errors; `d3d12_existing_heap` logs pre-existing teardown `VUID-vkDestroyDevice-device-05137` on both `main` and `PR+main`, while `d3d12_host_prefix` and `d3d12_mapped_plane_async` have `0` validation messages); `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
OreoFR commented 2026-11-29 04:17:21 +00:00 (Migrated from github.com)

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


Verified by OreoFR

  • Base: main aa918e5d9 + PR head 6a65fe6a6
  • Test: ctest -R ^d3d12_existing_heap$
  • Red on main: fails at line 453: a placement whose 32-bit mip range wraps (first_mip 0xffffffff) still matches
  • Green with the PR: passes; package builds, full ctest shows no new failure (12 CPU/GPU tests failed once under heavy load and pass on a serial rerun; xs_proxy_mock is flaky on main too)
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

Note: shares runtime/test/d3d12_existing_heap_test.cpp with the open #2566; a rebase may be needed if #2566 lands first.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7250562692 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2022#issuecomment-7250562692). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T04:17:21Z. --- **Verified by OreoFR** - Base: main `aa918e5d9` + PR head `6a65fe6a6` - Test: `ctest -R ^d3d12_existing_heap$` - Red on main: fails at line 453: a placement whose 32-bit mip range wraps (`first_mip` 0xffffffff) still matches - Green with the PR: passes; package builds, full ctest shows no new failure (12 CPU/GPU tests failed once under heavy load and pass on a serial rerun; `xs_proxy_mock` is flaky on main too) - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 Note: shares `runtime/test/d3d12_existing_heap_test.cpp` with the open #2566; a rebase may be needed if #2566 lands first. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:41:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:41:17Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318036760 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2022#issuecomment-7318036760). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:41:17Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

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!2022
No description provided.