Skip to content

D3D12.X: the sparse state is replaced outside its lock - #443

Merged
theaustinayers merged 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/sparse-state-replace-outside-lock
Nov 24, 2026
Merged

theaustinayers merged 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/sparse-state-replace-outside-lock

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

d3d12x::sparse::init installed its state with *STATE.lock().unwrap() = Some(s), so the state it replaced was dropped while the guard was held. A state that has been used holds the last references to buffers of its own: readback, staging and zeros, made lazily through the adopted device (sparse.rs 830–896). Their release goes through real::res_release, which calls sparse::forget_buffer, which locks STATE again. The thread waits for itself, and every later user of the sparse windows waits behind it. The old state is now taken out under the lock and dropped after it, as planned in the issue.

Fixes #344

The windows get set up again when two first real::init() calls overlap. real::init returns early only once DEVICE is set, and DEVICE is set after open_uma → sparse::init, so GPU tests that start together can both get there. That is how the hang in the issue came about: the issue's suite run stopped in four GPU tests until it was killed. One correction to the issue: what matters is not placed buffers in general but the state's own last-referenced buffers, which is why the test reads back a block before setting the windows up again.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs, sparse::init: STATE.lock().unwrap().replace(s), and the old state is dropped after the guard.
  • crates/xcl/tests/sparse_reinit.rs, the_sparse_state_is_replaced_outside_its_lock, in its own process:
    • real::init(), then a 64 KiB sparse block, written and read back (readback_now), which gives the state its readback buffer;
    • then sparse::init again with the same device, on a second thread. The test waits 60 s for it, so on main it fails instead of hanging the suite;
    • with no sparse window on the GPU after real::init() (real::uma_gpu_va() == 0, as on WARP and the no-GPU CI runner), it skips (no GPU UMA), as predication_e2e.rs does.

Tested

Regression run

Our own runs, one after the other on the machine under Tested (RTX 5070 Ti), release builds, no uncommitted changes.

main:

xcl-smoke v1
commit: 0e946cfc68244edee1fba6e0425371f687a05ccd
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1482 (PresentX 1482)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14725 MB

PR:

xcl-smoke v1
commit: d96812a91dc968bb9db8707d41339b09931087d6
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1518 (PresentX 1518)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14544 MB

Not tested

  • Whether a title can reach sparse::init twice: not through real::init, which outside tests only backend()'s one-time initializer calls (checked on 2997c9a); other routes are not checked. The smoke runs above are a regression check only.
  • Two real::init() calls that really overlap. The test calls sparse::init a second time directly, with the device it already has.
  • Which state ends up installed when two inits do overlap: unchanged by this PR, the last one wins.
  • Wine.
  • macOS / Linux: not built.
  • Memory: serialize sparse commits and decommits with uploads #236 (open) rewrites the same lines in sparse::init and keeps the drop under the guard (*state = Some(s) while state is the guard), so whichever merges second needs a rebase.

AI use

  • Claude Code, Claude Opus 5.5: wrote the change, the test and this description; the runs above are its own.

🤖 Generated with Claude Code

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 16fc79d51385e52aaa9e14749bb23e8914ce7fb4 on AMD (RDNA4); the PR was tested on NVIDIA only.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; XCL picked it (backend 'real' – AMD Radeon RX 9060 XT (16191 MiB VRAM))
  • Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 677 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 636 passed, 0 failed, 21 ignored
  • 0 SKIPPED on this adapter, so the two wave64 tests skipped on the RTX 5070 Ti ran and passed here
  • the_sparse_state_is_replaced_outside_its_lock passes in the suite (sparse_reinit: 2 passed, 0.17 s)
  • with sparse.rs from main a6f5f7a and the new test kept, cargo test -p xcl --test sparse_reinit: 0 passed, 1 failed after 60.33 s (sparse::init did not return within 60 s: it waits for the lock it holds: Timeout), as the description says (1 run)

Review of the change: in let old = STATE.lock().unwrap().replace(s); the guard is a temporary of the let statement, so it is dropped at the end of that statement and drop(old) runs without the lock; forget_buffer can then take it. The caller (real.rs:348, in open_uma) holds no other lock around sparse::init. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the failing run on main was done once, not three times.

Run and report by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@PHeitor1

Copy link
Copy Markdown

Tested 16fc79d merged onto main 2997c9a.

RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 677 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). Build clean.

Run and written with Claude Code (Claude Opus 5.5).

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

crates/xcl/tests/sparse_reinit.rs:33 assumes that the second initialization creates at least one window. On 16fc79d, the Windows CI worker completed the call within the timeout and returned zero windows. The test then failed at this assertion. Please check that the adapter can create the second set of windows before using this integration test as the deadlock check. A separate test with a destructor that takes the same lock can cover the lock order on every worker. Failed CI job. Reviewed the diff and CI log with OpenAI Codex (GPT-6).

@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Test report for 16fc79d merged onto main 2997c9a (clean merge, merge commit 3216209): pass, the new test fails without the fix, and this machine hits the #344 hang in the plain suite often enough to add an independent stack capture.

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 4080 (driver 610.88, 32-lane waves), Intel i9-12900K, 64 GB; Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug; DXC 1.9.2602.17 (Windows SDK 10.0.26100.0); no XCL_SAMPLE_*
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 677 passed, 0 failed, 41 ignored (lib: 636 / 0 / 21; sparse_reinit: 1 / 0 / 0)
  • SKIPPED on this adapter: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)
  • cargo test -p xcl --test sparse_reinit: 3 of 3 runs pass (0.4 s each).
  • Negative control: with init back to *STATE.lock().unwrap() = Some(s);, the same command fails after 60 s (sparse::init did not return within 60 s: it waits for the lock it holds).

The hang without this PR, on this machine

  • cargo test --workspace -- --test-threads=4 on trees without the fix stopped in the lib tests in 8 of the suite runs I made tonight (bases 2b4bb43, a6f5f7a, 2997c9a, 7ebf1b8, each with one unrelated open PR merged), always after ~194 tests, with gpu_uma_import_check_at_init and three other GPU tests reported as running for over 60 seconds until I killed the process. Other runs of the same trees passed, so it depends on test order and timing.
  • Thread stacks of one hung process (a6f5f7a + Recompiler: comparison samples use their supplied pixel gradients #350, taken with cdb):
    • gpu_uma_import_check_at_init: real::init → real::open_uma → sparse::init → real::res_release → sparse::forget_buffer (waiting on the lock) – the chain the description gives;
    • gpu_wave32_and_wave64_src_vccz_execz: runner::run_with_statics → real::res_release → sparse::forget_buffer (waiting);
    • gpu_typed_buffer_loads_and_stores, gpu_valu_16_and_64_bit: waiting in runner::run_with_statics.
  • With this PR merged in, a tree that had stopped in 4 of 4 suite runs (main + Recompiler: pixel comparison samples apply their LOD bias #349) ran through: 679 passed, 0 failed, 41 ignored (one run).

Reviewed (read the whole diff)

Not tested

  • No regression run (scripts/smoke.ps1) on a title, AMD and wave64 hardware, release build.

AI use

  • Claude Code (Claude Fable 5.2) ran the builds, the tests and the controls, read the diff and wrote this report. The runs above are real, on the hardware named.

@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown

Two points not covered above:

  1. tests/sparse_reinit.rs:15/33 – on the WARP CI runner CreateReservedResource fails (E_OUTOFMEMORY) in both inits, so the second init opens 0 windows and line 33 panics instead of reaching the deadlock. crates/xcl/tests/predication_e2e.rs:177 already skips this case (if real::uma_gpu_va() == 0 { runner::skip_test("no GPU UMA"); return; }); the same guard after the first init here should turn the required check green.
  2. Follow-up, not for this PR: the re-init itself comes from real::init having no once-guard (real.rs:456-538), so overlapping first calls run sparse::init twice. The second State starts with an empty mapped table and EARLY is already drained, so blocks committed before it get no tiles in the new windows. Worth a separate issue to serialise real::init?

On an RTX 4070 SUPER (621.24) this tree passed 4 of 4 plain suite runs (677 / 0 / 41), while trees without it hung in the #344 chain in 6 of 11 runs, matching throny's stacks.

AI-assisted review (Claude Code, Claude Opus 5.5).
Discord: bigpeppa.

sparse::init installed its state with *STATE.lock() = Some(s): the
state it replaced was dropped while the guard was held. Once used, that
state holds the last references to buffers of its own (readback,
staging, zeros), all made through the adopted device; their release goes
through real::res_release, which calls sparse::forget_buffer, which
locks STATE again - the thread waited for itself, and every later user
of the sparse windows behind it. The windows are set up again when two
first real::init calls overlap, as GPU tests starting together do: a
suite run hung in four GPU tests until it was killed.

The old state is now taken out under the lock and dropped after it.

Fixes xcl-dev#344

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Kalmarv
Kalmarv force-pushed the fix/sparse-state-replace-outside-lock branch from 16fc79d to d96812a Compare November 24, 2026 01:48
@13hourstilliland

13hourstilliland commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Ampere 8 GB (RTX 3070, driver 610.88) targeted and full workspace run on exact PR head d96812a (updated from earlier 16fc79d run, which also passed with the same counts).

Tested

  • Commit: d96812a91dc968bb9db8707d41339b09931087d6 (PR D3D12.X: the sparse state is replaced outside its lock #443 head), clean worktree
  • Hardware: NVIDIA GeForce RTX 3070 8 GB, driver 610.88; Intel Core i5-12600K, 31.8 GB RAM; Windows 11 Home build 26200.9974
  • Toolchain: Rust 1.100.0 (stable-x86_64-pc-windows-gnu, not MSVC); DXC 1.9.2609.5 (01b62ad4) via XCL_DXC_DIR; debug build; no XCL_SAMPLE_* set
  • Targeted command (3 runs): cargo test -p xcl --test sparse_reinit -- --show-output
    • Result: 3/3 passed (the_sparse_state_is_replaced_outside_its_lock, 0.46 s / 0.45 s / 0.42 s, no skip)
  • Full workspace command: cargo test --workspace --no-fail-fast -- --test-threads=4 --show-output (interactive desktop session)
    • Result: 677 passed, 0 failed, 41 ignored across 17 test binaries (xcl lib: 636 passed, 0 failed, 21 ignored; sparse_reinit: 1/0; e2e: 3/0)
  • SKIPPED (4, not coverage):
    • no wave64 (2): gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware
    • no D3D12 debug layer (2): test_title_draws_with_a_root_signature_under_the_debug_layer, uploads_start_from_each_subresources_state
  • TDR / device removal: 0 Display 4101 events in the System event log

Not tested

  • Retail title / scripts/smoke.ps1 regression run, negative control without the sparse.rs fix, AMD / wave64 hardware, MSVC toolchain, release build, D3D12 debug-layer tests

AI use

  • Hermes Agent, gemini-4-neon: ran the commands and wrote this report from the logs; the human reviewed it.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @QemmHD. Point 1 was right, and it's fixed in d96812a (squashed into the PR's commit).

  1. On the no-GPU runner the first set-up already opens no sparse window ("UMA opened (0 sparse VRAM window(s) …), window 0 buffer at VA 0x0"). The test now skips right after real::init() with the guard predication_e2e.rs uses (if real::uma_gpu_va() == 0 { skip_test("no GPU UMA") }). The 60 s check on the second set-up is unchanged.
    • With XCL_ADAPTER=warp here, the test before this change panicked at line 33 in 0.13 s, as on CI. It now prints SKIPPED … no GPU UMA and passes, also with main's sparse.rs.
    • On the RTX 5070 Ti it passes 3 of 3, and with main's sparse.rs it still fails 3 of 3 after 60 s.
  2. Not changed here. Outside tests, real::init() is only reached through d3d12x::backend(), which is a OnceLock. Only gpu_uma_import_check_at_init, gpu_sparse_vram_descriptors_buffers_and_xmem and this test call it directly, so the double set-up is a test-only race (PHeitor1 found the same on Tests: gpu_sparse_vram_descriptors_buffers_and_xmem fails beside the title-texture tests since #288 #343).

🤖 Claude Opus 5.5

@srcamarda

Copy link
Copy Markdown
Contributor

Regression run on a retail title, as asked under Regression run.

Tested

  • AMD Radeon RX 7800 XT (RDNA3), driver 32.0.32029.2010; Ryzen 5 5600G, 32 GB; Windows 11 Pro 26300; release builds
  • scripts/smoke.ps1 -Seconds 90, no input (the title stays on its account screen at 30 fps). Each binary was built from a clean checkout of its commit and run twice; the first run of each has a cold translation cache. Blocks below are from the second run.
  • main 508a0c7: 2343 / 2361 frames, peak 15764 / 15674 MB. This PR (d96812a): 2318 / 2342 frames, peak 15787 / 15698 MB. No crash or device removal in any run.
  • No difference from main beyond run-to-run spread.

Regression run

main:

xcl-smoke v1
commit: 508a0c74d7c0625d6d2de8d24494e69dc2e1926f
gpu: AMD Radeon RX 7800 XT
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 2361 (PresentX 2361)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15674 MB

this PR's head:

xcl-smoke v1
commit: d96812a91dc968bb9db8707d41339b09931087d6
gpu: AMD Radeon RX 7800 XT
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 2342 (PresentX 2342)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 15698 MB

Not tested

  • This title makes one device, so sparse::init ran once; the replace-while-locked case is not reached here (as your Not tested says).
  • The whole test suite at this head (only the title runs).

AI use

  • Claude Code with Claude Opus 5.5 built both commits, ran the smoke runs and wrote this report; the runs are from my machine.

@futurepro62

Copy link
Copy Markdown

Repeated runs on a machine that hits the #344 hang often, since the check above that the fix removes it was one suite run.

Tested

  • d96812a merged onto main 508a0c7 (clean merge). NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.

  • XCL_REQUIRE_GPU=mesh cargo test -p xcl --lib -- --test-threads=4, 180 s limit per run:

    tree runs passed (0 failed) hung
    main 508a0c7 5 4 1
    main + D3D12.X: a console MS in the AS slot is translated as the mesh shader it is #541 (does not touch sparse.rs) 7 3 4
    main + this PR 10 10 0

    Every hang without the fix stopped at the same four tests reported as running for over 60 seconds, with the process idle: gpu_uma_import_check_at_init, gpu_typed_buffer_loads_and_stores, gpu_valu_16_and_64_bit, gpu_valu_float_and_int. Those are the tests in the stacks above. With this PR, all 10 runs finished: 640 passed, 0 failed, 21 ignored.

  • cargo test -p xcl --test sparse_reinit: 1 passed.

Not tested

  • The whole workspace suite on this merge (only the lib tests and sparse_reinit); no stack dump of my own hangs; scripts/smoke.ps1 (no title on this machine); AMD, wave64 hardware.

AI use

  • Claude Code (Claude Opus 5.5) ran the repeated runs and wrote this report. I checked it before posting.

@theaustinayers
theaustinayers merged commit d852f9a into xcl-dev:main Nov 24, 2026
5 of 6 checks passed
theaustinayers added a commit to theaustinayers/xcl that referenced this pull request Nov 24, 2026
…, xcl-dev#490, xcl-dev#347)

The fork keeps its own pso_async (short waits, all compute pipelines built ahead); xcl-dev#690 upstream is its used-pipelines part.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

D3D12.X: replacing the sparse state deadlocks when the old state still holds placed buffers

10 participants