Skip to content

D3D12.X: a buffer's last release does not wait for the sparse state's lock - #1981

Merged
raylitebull merged 1 commit into
xcl-dev:mainfrom
Eterst:fix/sparse-forget-buffer-deadlock
Nov 25, 2026
Merged

raylitebull merged 1 commit into
xcl-dev:mainfrom
Eterst:fix/sparse-forget-buffer-deadlock

Conversation

@Eterst

@Eterst Eterst commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

forget_buffer no longer waits for the sparse state's lock. before_execute holds STATE around D3D12 calls, and D3D12 can make a placed buffer's last Release inside a call of its own on another thread; that Release reaches forget_buffer, which took STATE. The D3D12 call under the lock then waits for D3D12's lock and the Release inside D3D12 for STATE. On a retail title this stopped presents after the third frame on every start (black screen), with both threads in those waits in a thread dump. #968 fixed the other half of the same cycle (queue()); this is the remaining half.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs: forget_buffer queues the resource in FORGOTTEN and takes STATE only if it is free (try_lock); every other user of the state takes it through lock_state, which first lets the queued buffers go (forget_released → forget_in, the old body). Test a_buffer_released_while_the_state_is_held_does_not_wait_for_it.

Tested

  • NVIDIA GeForce RTX 4070 SUPER 12 GB (driver 616.92, 32-lane waves), Ryzen 7 5700X, 32 GB; Windows 10 22H2 (19045); Rust 1.100.0 MSVC; debug for the suite, release for the smoke run.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4 on 892177b: 976 passed, 0 failed, 45 ignored. SKIPPED on this adapter (no wave64 / refused forms): the gpu::recomp::lower and lower_dxil GPU comparisons, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, heap_window_out_of_memory_falls_back_to_the_switch_form.
  • The new test fails on main 689f2db (forget_buffer waited for the sparse state's lock, after its 5 s timeout) and passes here.
  • On this machine hle::d3d12x::vram::tests::d32s8_targets_take_their_own_size_of_video_memory and three list_x GPU tests (0x887A0005) fail now and then in parallel runs, on main as well (3 of 8 parallel main runs); they passed in the run above.

Regression run

main:

xcl-smoke v1
commit: 689f2db477c3c062cf162649781640ef8a977c18
gpu: NVIDIA GeForce RTX 4070 SUPER
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1515 (PresentX 1515)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16570 MB

this PR:

xcl-smoke v1
commit: 892177b682542c574eafbda3b8ea5d57725c88c5
gpu: NVIDIA GeForce RTX 4070 SUPER
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1595 (PresentX 1596)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15939 MB

Not tested

AI use

  • Claude Code (Claude Opus 5.5): found the cycle from thread dumps, wrote the fix, the test and this description, and ran the tests and smoke runs.

🤖 Generated with Claude Code

… lock

`before_execute` holds the sparse state's lock (`STATE`) around D3D12 calls. D3D12 can make
a placed buffer's last Release inside one of its own calls on another thread; that Release
reaches `forget_buffer`, which took `STATE` too: the D3D12 call under the lock waited for
D3D12's lock, the Release inside D3D12 for `STATE`. A retail title's presents stopped after
its third frame on every start (black screen) with both threads in those waits.

`forget_buffer` now queues the resource in `FORGOTTEN` and only takes `STATE` if it is free
(`try_lock`); every other user of the state takes it through `lock_state`, which first lets
the queued buffers go. xcl-dev#968 fixed the other half of the same cycle (`queue()`).

Test: a_buffer_released_while_the_state_is_held_does_not_wait_for_it (fails on main after
its 5 s timeout: "forget_buffer waited for the sparse state's lock").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@futurepro62

futurepro62 commented Nov 25, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 892177b (on its base 689f2db).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::dxil::fuzz_tests: 966 passed, 0 failed, 43 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: a_buffer_released_while_the_state_is_held_does_not_wait_for_it.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 975 passed, 0 failed, 45 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main 7994cd1 too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651), the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests and const_and_null_mask_operands_agree_with_hlsl_on_the_gpu (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

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

@raylitebull

Copy link
Copy Markdown
Member

Code review on 892177b:

  • Avoiding taking sparse::STATE synchronously on forget_buffer prevents late_release::drain_known deadlocks when STATE is held across GPU resource/allocator work, and RTX 4070 SUPER + RTX 5070 Ti runs pass cleanly.
  • Diff audited and CI workflow approved (currently running).

Remaining before merge: Build and test (Windows, no GPU) finishing green plus an AMD verification pass on 892177b.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

Ahmet-Dedeler added a commit to Ahmet-Dedeler/xcl that referenced this pull request Nov 25, 2026
Apple's D3DMetal (CrossOver, the Game Porting Toolkit) calls Release on threads of its own (its
queue thread at submit, for a barrier's resource) and from inside calls XCL makes while XCL
holds a lock of its own. When that Release is the title's last one and late release keeps the
object, res_release took the maps' locks, the sparse state's and the texture table's, and
late_release::hold signalled the queues and waited. Whatever the host's thread waits for there,
its queue waits for too. (The one such wait seen in a retail run on the Mac port, sparse's
forget_buffer, no longer waits since xcl-dev#1981; the others are the same shape and are not waited
for here either.)

On a host whose D3D12 is in no PE module (class_for: module_of of the table's first method is
0, noted once when the first such table is adopted) a kept last Release now writes the resource
into a ring of atomics (late_release::hand_over) and returns: no lock, no fence, no allocation,
no thread start, no environment read (xcl-dev#1789 reads XCL_LATE_RELEASE at adoption). The next
drain_known, on the late-release thread (started at adoption on such a host) or a submit,
forgets what XCL keeps by the address (real::forget_released, the old body of res_release) and
holds the object like any other; the address cannot come again before that, the object is still
there. With the ring full (16,384 slots) the reference is kept for good and counted.

D3D12 on Windows and vkd3d-proton are PE modules: there the flag is never set, the ring stays
empty (drain_known reads two atomics) and a Release does what it did.

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

4 participants