Skip to content

D3D12.X: use per-queue cpu_writes workers and atomic 64-bit stores - #1819

Open
n-romanovich wants to merge 1 commit into
xcl-dev:mainfrom
n-romanovich:d3d12x/cpu-writes-per-queue
Open

n-romanovich wants to merge 1 commit into
xcl-dev:mainfrom
n-romanovich:d3d12x/cpu-writes-per-queue

Conversation

@n-romanovich

@n-romanovich n-romanovich commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Addresses #1488 (XCL-BT-003).

Previously, cpu_writes used a single global xcl-cpu-writes FIFO worker thread for all command queues and wrote Write::Immediate dwords with a loop of 32-bit AtomicU32 stores:

  1. A stalled or blocked queue waiting up to WAIT_MS (10,000 ms) head-of-line blocked or deadlocked independent queues' bottom-of-pipe writes.
  2. write_now tore 8-byte aligned 64-bit Write::Immediate stores into two separate 32-bit writes, allowing spinning CPU readers to observe a half-written u64.
  3. FENCES had no cleanup on queue release.

What is in it

  • crates/xcl/src/hle/d3d12x/cpu_writes.rs:
    • Replace the single global worker() with a dedicated FIFO worker thread per queue inside QueueFence (spawn_worker), checking CreateEventW for NULL, calling ResetEvent before SetEventOnCompletion to avoid stale timeout signals, and advancing entry.value only after q.Signal succeeds.
    • Add on_queue_release(queue) to remove released queues from FENCES and shut down their worker thread cleanly.
    • Share is_region_writable across write_now, write_u64_now, and writable, and store 8-byte aligned 64-bit Write::Immediate values atomically via write_u64_now.
    • Add independent_queues_do_not_hol_block_each_other and write_now_stores_64bit_immediates_atomically unit tests (with QueueCleanup guards).
  • crates/xcl/src/hle/d3d12x/real.rs: call super::cpu_writes::on_queue_release(this as usize) in q_release when refcount reaches 0.

Tested
Hardware: Windows 11 x64, NVIDIA GeForce RTX 5060 Ti.

  • cargo test -p xcl --lib cpu_writes::tests -- --test-threads=4: 4 passed; 0 failed; 0 ignored
  • cargo test --workspace -- --test-threads=4: 893 passed; 0 failed; 42 ignored
    • SKIPPED: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64), shaders::tests::pso_measurement_compiles_an_exact_synthetic_record (Windows Developer Mode is required), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows)

Regression run
Main (4178e888c0b5be50a2887eda45cdb832763b68a4):

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: NVIDIA GeForce RTX 5060 Ti
title: FILES
seconds: 60
exit: 0x0
frames shown: 1538 (PresentX 1538)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14871 MB

PR (ef0b30b1535e20c66eef5cd1991e6a6c1635ff8a):

xcl-smoke v1
commit: 43a01309cbc57da6e96ef5bf93ce8ad321bbfd68
gpu: NVIDIA GeForce RTX 5060 Ti
title: FILES
seconds: 60
exit: 0x0
frames shown: 1476 (PresentX 1476)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17294 MB

Not tested

  • AMD GPU.
  • macOS / Linux (crate targets Windows only).

AI use

  • Antigravity (Claude Sonnet 5.5): implemented the cpu_writes.rs / real.rs changes and unit tests, ran the test suites and smoke.ps1, and verified the diff with an independent adversarial reviewer subagent.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Checked on Ampere: RTX 3070 8 GB, driver 616.56, Windows 11, debug, 74ad74e merged onto main 1d027e5.

  • hle::d3d12x at --test-threads=8: 157 passed, 0 failed, 2 ignored, in 3 of 3 runs.
  • cpu_writes::tests at 4 threads: 34 of 35 runs passed. Run directly from the test binary, it passed 15 of 15 alone and 15 of 15 with the module. Through cargo test it passed 4 of 5.

The one failure was independent_queues_do_not_hol_block_each_other at cpu_writes.rs:323: left: 1, right: 0, so queue A's write had landed while A still waited on blocker. That run took 10.21 s, about WAIT_MS. It looks like the test's thread was delayed about 10 s after after(q_a), A's worker timed out and wrote anyway (as designed), and B's loop then passed at once without reaching its 3 s check. If so, it is the test's timing, not the change. One way to make the test robust: take the time right after after(q_a) and check va_a == 0 only while less than WAIT_MS has passed. I didn't find what stalled the thread.

The release key matches: q_release passes the same raw queue pointer that list_x hands to after, as list_x::on_queue_release already does.

AI-assisted (Claude Code). Synthetic tests only.

@n-romanovich

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough stress run and for catching that timing edge case!

Updated independent_queues_do_not_hol_block_each_other (ef0b30b) as suggested: recorded t_a = Instant::now() right before after(q_a, ...) and guarded the va_a == 0 check with if t_a.elapsed() < Duration::from_millis(WAIT_MS as u64) so a 10 s OS scheduling delay under heavy parallel load won't falsely fail after queue A's worker intentionally times out.

Re-verified on Windows 11 (cargo test --workspace -- --test-threads=4: 893 passed, 0 failed, 42 ignored) and updated the xcl-smoke block in the PR description for ef0b30b.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Re-ran at ef0b30b (on main 689f2db, RTX 3070, driver 616.56). My timing explanation above was wrong. The new t_a guard didn't catch it: line 325 still saw A's write (1 of 20 direct runs, 0 of 5 through cargo test). hle::d3d12x at 8 threads: 157 passed.

What actually happens: the device is lost. I logged the worker locally. In a failing run, A's worker wakes from WaitForSingleObject after 680 ms, and the fence then reads 18446744073709551615 (u64::MAX, a removed device). A second worker sees the same at the same moment. Each failing run has an nvlddmkm 153 cluster in the System log (15:51:37, 16:09:07, 16:10:34). Splitting the module:

Run Runs Device lost
independent_queues_do_not_hol_block_each_other alone 60 0
cpu_writes::tests without it 60 0
cpu_writes::tests with it (4 threads) 85 3

So the reset only comes when this test's queue sits on q_a.Wait(&blocker, 1) while the other cpu_writes tests submit work. D3D12CreateDevice(None, ..) hands every test the same device, so the loss also fails every other GPU test in the process. That is the #1727 pattern again. Since the test needs no real GPU work, creating its device on the WARP adapter (EnumWarpAdapter, already the fallback here) would keep the blocked queue off the shared hardware device. I didn't find why a GPU-side wait of under a second ends in a reset on this driver.

AI-assisted (Claude Code). Synthetic tests only.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Heads-up for the q_release hook here: #1691 adds a second path where a released queue's count reaches 0, after present::forget_queue, and with it that is the usual one. A hook only in the first if n == 0 branch would not run there. Details in #1899 (comment), which adds a vram hook at the same spot.

Replace the single global xcl-cpu-writes worker with a dedicated FIFO worker per queue (cleaned up on q_release) so one stalled queue cannot HOL-block or deadlock independent queues, reset the wait event before SetEventOnCompletion, and store 8-byte aligned 64-bit Write::Immediate values atomically.

Assisted-by: Antigravity (Claude Sonnet 5.5)
@n-romanovich
n-romanovich force-pushed the d3d12x/cpu-writes-per-queue branch from ef0b30b to 43a0130 Compare November 25, 2026 00:22
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@JJAutoCompare

Copy link
Copy Markdown

PR #1819 | STOR-WINLAB compatibility test

Tested commit 43a01309cbc57da6e96ef5bf93ce8ad321bbfd68 (clean detached checkout). The PR gives each D3D12.X queue a worker for fence-delayed CPU writes and uses a single atomic store for aligned 64-bit immediates.

Environment STOR-WINLAB, Windows 11 Enterprise Evaluation build 26200.9974, AMD Ryzen 7 3800X, Intel Arc A770 16 GiB with driver 32.0.101.9175. The adapter reports DoublePrecisionFloatShaderOps=0 and wave sizes 8–32. d3d12SDKLayers.dll is absent. Rust 1.100.0 (0ef1bc386, 2026-11-09), DXC/DXIL 1.9.2609.5, unoptimized profiles with debug info disabled.

Build cargo build --workspace and cargo test --workspace --no-run both exited 0 outside the GPU lock.

Focused tests cargo test -p xcl --lib hle::d3d12x::cpu_writes::tests -- --test-threads=1 returned 4 passed, 0 failed, 0 ignored, 0 skip lines. The new tests confirmed that the compute queue's write lands while the direct queue waits on a fence and observed no torn values during 20,000 aligned 64-bit stores. The existing immediate-write and readback ordering tests also passed. The queue tests request the default D3D12 device with a WARP fallback and do not identify the selected adapter in their output.

Workspace cargo test --workspace --no-fail-fast -- --test-threads=4 ran once. Both test runs used the shared Arc lock and XCL_ADAPTER=A770. The 20 result summaries, including doc tests, sum to 969 passed, 26 failed, 45 ignored. Cargo exited 101. The four focused tests passed here too.

  • Failures (26) 21 compute PSO creation errors (0x80070057), four GPU result mismatches (fused FMA, mip sampling, the high half of an f16 result, and pixel-shader wave reduction), and one test-title failure. The title exited 1 after its dynamic depth bias check got blue where it expected green.
  • Skip lines (23) 18 due to missing wave64 support, three refused wave32 lowering cases, one driver-cache control skip, and one missing heap-window skip. These lines include partial skips and represent unexercised coverage.

Not tested A baseline on main, retail-title smoke runs, other GPU/driver combinations, or optional game-data fixtures (XCL_SAMPLE_* unset). Without a baseline, the run does not establish whether the workspace failures are regressions.

T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high) ran the build and verification. Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr1819-suite.log and published this report.

@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
# Conflicts:
#	crates/xcl/src/hle/d3d12x/cpu_writes.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants