Skip to content

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

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-title-gpu-placements-hash
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-title-gpu-placements-hash

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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 AI balanced review requested due to automatic review settings November 25, 2026 18:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2022 | head 6a65fe6 (merged cleanly with main 4a54aed) | 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

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants