Repository navigation
Conversation
… of its slot note_slot, note_heap_slot and note_child_slot did a fetch_add on the slot's hit counter and a compare_exchange_strong on its first-call flag for each proxied call. The compare-exchange takes the cache line for writing also when it fails, and the flags of all slots share one or two lines, so threads that call different slots slowed each other. The first-call flag is now read before the compare-exchange. The hit counters are test helpers: they count from the first call of a *_slot_hits reader or of reset_device_proxy_stats, so a title's calls do not write them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Kam471
left a comment
There was a problem hiding this comment.
Built and ran device_proxy_test at a1ec0cbc on Windows 11: device_proxy_test: OK (0 failures), exit 0; ctest -R device_proxy -> 1/1 passed.
The slot callbacks now write their shared atomic only on the first call, and the proxy/fence/heap/PC-lifetime checks pass. No blocking issue.
Flyander
left a comment
There was a problem hiding this comment.
The counter gate and first-call fast path match the documented test-only semantics. After the first observed slot, later calls avoid the previous contended writes; the new test checks that reads begin counting only after instrumentation is enabled. I found no correctness issue or overlapping scope in this change.
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
2 open issues
What changed in this PR
Reduces contention in d3d12_xs proxy slot accounting by avoiding unnecessary shared atomic writes on hot paths (especially when counters aren’t being observed), while adding a regression test that validates the new “count only after first read/reset” semantics.
Changes:
- Gate per-slot hit counting behind a global
g_count_hitsflag enabled only by tests/readers. - Avoid
compare_exchangeon already-seen first-call flags by adding a read-first fast path. - Add a device proxy test asserting first-call flags are set even when hit counting is still disabled.
| File | Description |
|---|---|
| runtime/test/device_proxy_test.cpp | Adds assertions for the new hit-counter enablement semantics and first-call behavior. |
| runtime/shims/d3d12_xs_device.h | Documents that hit counters only start counting after first read/reset. |
| runtime/shims/d3d12_xs_device.cpp | Implements gated hit counting and read-first first-call detection to reduce contention. |
Review effort: Lite
| // The hit counters are for tests. They count from the first call of a *_slot_hits reader or of | ||
| // reset_device_proxy_stats, so the calls of a title, which reads no counter, write no shared counter. | ||
| std::atomic<bool> g_count_hits{false}; | ||
|
|
||
| void count_hit(std::atomic<std::uint64_t>& hits) { | ||
| if (g_count_hits.load(std::memory_order_relaxed)) hits.fetch_add(1, std::memory_order_relaxed); | ||
| } | ||
|
|
||
| // True for the first call of a slot only. The flag is read first: a compare-exchange takes the cache line for | ||
| // writing also when it fails, and the flags of all slots share one or two lines. | ||
| bool first_call(std::atomic<bool>& seen) { | ||
| if (seen.load(std::memory_order_relaxed)) return false; | ||
| bool expected = false; | ||
| return seen.compare_exchange_strong(expected, true, std::memory_order_relaxed); | ||
| } |
There was a problem hiding this comment.
No change is necessary for this point. g_count_hits, count_hit and first_call are in the unnamed namespace of this file. That namespace starts at line 33 (namespace {) and ends at line 1257 (} // namespace), and the three names are at lines 62 to 79. Thus they have internal linkage already, as g_hits and note_slot have. AI-assisted.
| void reset_device_proxy_stats() { | ||
| g_count_hits.store(true, std::memory_order_relaxed); | ||
| for (unsigned slot = 0; slot < kMaxSlots; ++slot) { | ||
| g_hits[slot].store(0, std::memory_order_relaxed); | ||
| g_first[slot].store(false, std::memory_order_relaxed); |
There was a problem hiding this comment.
Fixed in 9f22844. The readers and reset_device_proxy_stats now call one helper, start_hit_counts. It reads g_count_hits first and writes it only when it is not set. Only the tests call these functions, thus the proxied calls do not change. device_proxy_test, queue_proxy_test (3 backends), allocator_proxy_test (3 backends), xs_proxy_mock_test and root_sig_test pass on Windows 11 with MSVC 14.51. AI-assisted.
|
Tested
AI-assisted (Claude Code). |
|
Reading review (Windows target:
Cannot run the Windows test. No game data. AI-assisted. |
Review of ItsNotRexed#4500: the *_slot_hits readers and reset_device_proxy_stats call start_hit_counts, which writes g_count_hits only when it is not set. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Tested a1ec0cb merged with main 03b09a0 (already up to date) on native Windows 11, MSVC 14.51, full
|
…ounts Review of ItsNotRexed#4500: the note at the counter declarations now gives the rule directly. Comment only. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
@mrbean56 Thank you for the review. I changed the note at the counter declarations in |
|
AI-assisted Windows hardware verification for RT-295 on Windows 11 (
|

Claimed in the coordination issue as: RT-295 (claim). Finding: item 2 of the runtime audit.
What and why
note_slot,note_heap_slotandnote_child_slotinruntime/shims/d3d12_xs_device.cpprun for each call through an Xbox device, heap, queue, fence or allocator proxy. Each call did two writes to shared memory:fetch_addon the hit counter of the slot.compare_exchange_strongon the first-call flag of the slot. This operation takes the cache line for writing also when it fails.g_firstisstd::atomic<bool>[128], thus the flags of 64 slots share one cache line.Thus threads that call different slots slow each other. The descriptor copy calls and the view creation calls go through
note_slot.This change:
first_callreads the flag first. It does the compare-exchange only when the flag is not set.count_hitcounts only wheng_count_hitsis set. The hit counters are test helpers: only the tests read them. The first call of a*_slot_hitsfunction or ofreset_device_proxy_statssetsg_count_hits. A title does not call these functions, thus its calls write no shared counter.XS_TRACEline do not change.I examined each reader of the counters:
device_proxy_test,queue_proxy_test,allocator_proxy_test,bvh_builder_testandfsr_dispatch_test. Each of them reads a counter or callsreset_device_proxy_statsbefore the calls that it counts. These tests do not change. One pattern does not work now: a test that makes calls first, and reads a counter for the first time after them, gets 0.d3d12_xs_device.hhas a note about this.The open #2490 does not change these three functions. It adds
ListStatswith the same two writes for the command-list proxy. That code can usecount_hitandfirst_callafter one of the two pull requests is merged.Tested
Windows 11, MSVC 14.51, CMake + Ninja,
RelWithDebInfo. NVIDIA GeForce RTX 4090, driver 32.0.16.2101.device_proxy_testuses WARP. No game files.The test that fails before the change and passes after it is a new check in
runtime/test/device_proxy_test.cpp:main(03b09a08)slot 0 first-call flag is set by the creationslot 0 has no hit before the first read of a counterslots 1 and 2 count the calls after the first readMeasurement 1, the real shim. A local loop (not in this pull request) calls slots 9 and 10 of one heap proxy (
GetCPUDescriptorHandleForHeapStart,GetGPUDescriptorHandleForHeapStart) on a WARP device. Each thread makes 10 000 000 calls. Threadtcalls slot9 + t % 2. The time includes the call of the real heap. Median of 3 runs:main, ns per callMeasurement 2, the model of the finding. It has only the two lines of
note_slot. Each thread calls one of the slots 17 to 24, 20 000 000 times:main, ns per callOther tests:
device_proxy_test,queue_proxy_test(3 backends),allocator_proxy_test(3 backends),xs_proxy_mock_testandroot_sig_test: OK.cteston this branch: 133 of 134 tests pass.xbox_presentfails. Fullctestonmain: 132 of 134 tests pass.d3d12_presentandxbox_presentfail. These two tests fail onmainon this computer because of the window manager.d3d12_presentpasses in some runs. 10 tests do not run on the two builds (no game files, no SDL3, no validation layer).d3d12_xdxr_rtfailed one time withthe builds did not complete. That is its 10-second wait for the GPU. Other GPU work was in progress on this computer at that time. The test does not use the Xbox proxies. It passed 3 of 3 times alone and in the second full run.bvh_builder_testandfsr_dispatch_testread the counters, but they need game files. I did not run them. I examined their code: they callreset_device_proxy_statsbefore the calls that they count.AI-assisted (Claude Code).
Checklist
docs/design.md, with how it was confirmed. (Not applicable: no translator rule.)🤖 Generated with Claude Code