Skip to content

d3d12_xs: a proxied call writes no shared atomic after the first call of its slot (RT-295) - #4500

Open
shdann wants to merge 3 commits into
ItsNotRexed:mainfrom
shdann:shdann/xs-proxy-note-slot-read-first
Open

shdann wants to merge 3 commits into
ItsNotRexed:mainfrom
shdann:shdann/xs-proxy-note-slot-read-first

Conversation

@shdann

@shdann shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-295 (claim). Finding: item 2 of the runtime audit.

What and why

note_slot, note_heap_slot and note_child_slot in runtime/shims/d3d12_xs_device.cpp run for each call through an Xbox device, heap, queue, fence or allocator proxy. Each call did two writes to shared memory:

  1. fetch_add on the hit counter of the slot.
  2. compare_exchange_strong on the first-call flag of the slot. This operation takes the cache line for writing also when it fails. g_first is std::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_call reads the flag first. It does the compare-exchange only when the flag is not set.
  • count_hit counts only when g_count_hits is set. The hit counters are test helpers: only the tests read them. The first call of a *_slot_hits function or of reset_device_proxy_stats sets g_count_hits. A title does not call these functions, thus its calls write no shared counter.
  • The first-call log line and the XS_TRACE line do not change.

I examined each reader of the counters: device_proxy_test, queue_proxy_test, allocator_proxy_test, bvh_builder_test and fsr_dispatch_test. Each of them reads a counter or calls reset_device_proxy_stats before 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.h has a note about this.

The open #2490 does not change these three functions. It adds ListStats with the same two writes for the command-list proxy. That code can use count_hit and first_call after 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_test uses 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:

Check main (03b09a08) This branch
slot 0 first-call flag is set by the creation PASS PASS
slot 0 has no hit before the first read of a counter FAIL PASS
slots 1 and 2 count the calls after the first read FAIL PASS

Measurement 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. Thread t calls slot 9 + t % 2. The time includes the call of the real heap. Median of 3 runs:

Threads main, ns per call This branch, ns per call
1 12.0 11.1
2 19.2 11.1
4 60.0 13.9
8 126.4 13.8

Measurement 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:

Threads Code of main, ns per call Code of this branch (count off), ns per call
1 3.5 1.6
2 37.8 1.7
4 64.1 1.6
8 104.0 4.5

Other tests:

  • device_proxy_test, queue_proxy_test (3 backends), allocator_proxy_test (3 backends), xs_proxy_mock_test and root_sig_test: OK.
  • Full ctest on this branch: 133 of 134 tests pass. xbox_present fails. Full ctest on main: 132 of 134 tests pass. d3d12_present and xbox_present fail. These two tests fail on main on this computer because of the window manager. d3d12_present passes in some runs. 10 tests do not run on the two builds (no game files, no SDL3, no validation layer).
  • In the first full run on this branch, d3d12_xdxr_rt failed one time with the 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_test and fsr_dispatch_test read the counters, but they need game files. I did not run them. I examined their code: they call reset_device_proxy_stats before the calls that they count.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator rule.)

🤖 Generated with Claude Code

… 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>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 01:51

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

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.

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_hits flag enabled only by tests/readers.
  • Avoid compare_exchange on 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

Comment on lines +65 to +79
// 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);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines 1435 to 1439
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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@itsonlysimbo

Copy link
Copy Markdown
Contributor

Tested a1ec0cbc on RTX 4070, NVIDIA 621.14, Windows 11 (10.0.26300), MSVC 19.44, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363: pass.

  • device_proxy passes.
  • ctest: 145 passed, 10 skipped, 0 failed; run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed, same as main 03b09a08.

AI-assisted (Claude Code).

@mrbean56

Copy link
Copy Markdown

Reading review (Windows target: device_proxy_test links D3D12/DXGI, so this is not a pass), and the test change looks like it pins the new contract.

  • The ordering is deliberate and right: first_call reads the flag before the compare-exchange because a failed CAS still takes the cache line for writing, and the per-slot flags share lines. note_slot's old unconditional fetch_add was the write the title complains about.
  • The arming model is coherent: counters are only written once a reader (*_slot_hits) or reset_device_proxy_stats has run, so a title's calls touch no shared counter, and the test asserts exactly that — device_slot_first_call_seen(0) is true from the creation while device_slot_hits(0) == 0 until the first read, then slots 1 and 2 count the addref/release.
  • One consequence worth a sentence in the header or the PR body: a test that triggers calls before reading a counter sees 0 for them, so the reader must be called first. The new test demonstrates it, but the requirement is easy to trip over; naming it where the counters are declared would save the next author a confusing == 0.
  • kMaxSlots/kHeapSlots bounds checks are unchanged, so an out-of-range slot still cannot index the arrays.

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>
@kapitche

Copy link
Copy Markdown
Contributor

Tested a1ec0cb merged with main 03b09a0 (already up to date) on native Windows 11, MSVC 14.51, full runtime/ build, RTX 4080 SUPER: pass.

  • ctest for the tests that use the proxy counters: device_proxy, xs_proxy_mock (+ _no_reserve, _no_protect, _no_handler), queue_proxy (98 s) and allocator_proxy pass (7/7); fsr_dispatch and bvh_builder are skipped here (they need a game copy), so I read them instead: both call reset_device_proxy_stats() before their measurements, which now starts the counting, so the change does not zero what they read.
  • Negative control: d3d12_xs_device.cpp/.h from main with the PR's device_proxy_test.cpp: device_proxy FAILED, slot 0 has no hit before the first read of a counter and slots 1 and 2 count the calls after the first read.
  • Not measured: the claimed reduction in cache-line traffic (no benchmark in the PR).

…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>
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@mrbean56 Thank you for the review. I changed the note at the counter declarations in runtime/shims/d3d12_xs_device.h in eb32459. It now gives the rule directly: a test must read a counter (any *_slot_hits function) or call reset_device_proxy_stats before the calls that it counts. Calls made before that are not counted, and the counter stays 0. The pull request description has the same rule in the section "What and why". The commit changes a comment only, thus the code and the test results do not change. AI-assisted.

@Alpaca-exe

Copy link
Copy Markdown

AI-assisted Windows hardware verification for RT-295 on Windows 11 (AMD Radeon RX 6750 XT, 0x1002:0x73df, AMD driver 26.11.2 / 32.0.21045.29003, Vulkan 1.4.315, MSVC 19.44.35230.0, VK_LOADER_LAYERS_DISABLE=~implicit~):

  • Built device_proxy_test from commit eb324590e (rc=0).
  • Ran device_proxy_test.exe: rc=0 (128 output lines), verifying proxy wrapping over the host PC device, first-call slot logging ([rt] d3d12_xs: device slot ... first call), and the new post-first-read hit-counter checks (device_slot_first_call_seen(0), device_slot_hits(0) == 0, device_slot_hits(1) == 1 && device_slot_hits(2) == 1).

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.

8 participants