You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
d3d12: index TitleGpuPlacements in O(1) hash map and fix mip/layer overflow (RT-TITLE-GPU-PLACEMENTS-HASH-AND-OVERFLOW) - #2022
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):
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).
Duplicate lookup when alias == va: find(va, alias, matches) probed {va, alias} unconditionally even when alias == va.
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).
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:
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.
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.
#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.py0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
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)
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Claim:
RT-TITLE-GPU-PLACEMENTS-HASH-AND-OVERFLOW(claim comment#7170709644on #1608).Problem
In
runtime/shims/title_gpu_placements.h(held underdescriptor_mutexfor every placed-resource descriptor lookup):TitleGpuPlacementsstored placements in astd::multimap<std::uint64_t, Resource*>even though every operation (add,remove,find) is an exact-VA lookup (never ordered iteration or range scan).alias == va:find(va, alias, matches)probed{va, alias}unconditionally even whenalias == va.matches(const gfx10::Image&, const D3D12_RESOURCE_DESC&):image.first_mip + image.mip_count > d.MipLevelsandimage.first_layer + image.layer_count <= d.DepthOrArraySizewrappeduint32_tnearUINT32_MAX(e.g.first_mip = 0xffffffffu, mip_count = 1wrapped to0 <= d.MipLevelsand falsely matched).Fix
std::multimapwithstd::unordered_map<std::uint64_t, Bucket>whereBucketstores the single common placement inline (Resource* first) with an overflowstd::vector<Resource*> extrafor 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.add()and skip the second probe infind()whenalias == va.matches()with overflow-free subtraction (image.first_mip > d.MipLevels || image.mip_count > d.MipLevels - image.first_mip, and similarly forfirst_layer/layer_count).runtime/test/d3d12_existing_heap_test.cpp(0CMakeLists.txtedits).Benchmark (
clang++ -std=c++20 -O2, bit-exact A/B, 16,384 placed resources x 500,000find()calls)std::multimap):42.60 msstd::unordered_map+ inline-1 LIFO bucket):1.49 ms(28.54x faster)Verification
runtime/shims/title_gpu_placements.hunit + A/B parity tests (verifying LIFO order, multi-element front/middle/back removal,va == aliassingle-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 -Werrorclang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werrorclang++ -std=c++20 -O2 -Wall -Wextra -Werrorpython3 -I scripts/check_no_game_data.py runtime/shims/title_gpu_placements.h runtime/test/d3d12_existing_heap_test.cppandpython3 -I tests/test_text_write_line_endings.py: all passed.