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

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

📋 Pull Request Information

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

Base: main ← Head: perf/rt-title-gpu-placements-hash


📝 Commits (1)

  • 6a65fe6 d3d12: index TitleGpuPlacements in O(1) hash map and fix mip/layer overflow

📊 Changes

2 files changed (+57 additions, -16 deletions)

View changed files

📝 runtime/shims/title_gpu_placements.h (+44 -16)
📝 runtime/test/d3d12_existing_heap_test.cpp (+13 -0)

📄 Description

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.

🔄 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/2022 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-title-gpu-placements-hash` --- ### 📝 Commits (1) - [`6a65fe6`](https://github.com/ubiquitarianism/leonida/commit/6a65fe6a6ad9a963fe6ec95b2e4729c2fef710ab) d3d12: index TitleGpuPlacements in O(1) hash map and fix mip/layer overflow ### 📊 Changes **2 files changed** (+57 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/title_gpu_placements.h` (+44 -16) 📝 `runtime/test/d3d12_existing_heap_test.cpp` (+13 -0) </details> ### 📄 Description 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. --- <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#2027
No description provided.