Skip to content

D3D12.X: defer pred_buf drops until GPU fence completion - #2145

Open
Simon-CR wants to merge 1 commit into
xcl-dev:mainfrom
Simon-CR:pr/defer-pred-buf-release
Open

Simon-CR wants to merge 1 commit into
xcl-dev:mainfrom
Simon-CR:pr/defer-pred-buf-release

Conversation

@Simon-CR

@Simon-CR Simon-CR commented Nov 25, 2026 •

Copy link
Copy Markdown

When a console command list calls SetPredicationBufferX, XCL creates a dedicated PC buffer (pred_buf) on the fly to hold the predicate value during recording. Previously, when that command list was released (on_release) or another command list was created at the same memory address (on_create), the old tracking entry and its pred_buf COM reference were dropped synchronously on the CPU — even if earlier GPU submissions referencing that buffer were still in flight.

This change defers dropping those PC-owned pred_buf resources behind GPU completion fences using late_release, matching the lifetime guarantee already enforced for title-owned resources: once retired outside the command-list tracking lock, pred_buf stays alive until every known GPU queue has passed a signalled batch fence, and then drops outside the HELD lock (dropping immediately when no queues are active or when XCL_LATE_RELEASE=0).

What is in it

  • crates/xcl/src/hle/d3d12x/late_release.rs: extend Held / Batch with open_pc / dead_pc (Vec<ID3D12Resource>), add Held::drain_all (sealing immediately without waiting on SEAL_EVERY when queues.is_empty()), extract ensure_thread, add pub(super) unsafe fn hold_pc_resources, release gone_pc outside HELD in drain_known, and add GPU/unit tests held_pc_resources_wait_until_the_queue_is_past_their_batch and without_queues_held_pc_resources_go_at_once
  • crates/xcl/src/hle/d3d12x/list_x.rs: add retire_pred_buf and pass old.pred_buf.take() from on_create and on_release outside the LISTS lock, update set_predication_buffer_state_transitions_and_lifecycle step (h) to drain late_release until fake_res reaches 0 (with AcqRel/Acquire ordering and safe timeout handling under parallel queue tests), and add GPU test on_release_and_on_create_retire_pred_buf_until_queue_passes

Tested

Regression run

xcl-smoke v1
commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324
gpu: AMD Radeon RX 9070 XT
title: 5ec87c56
seconds: 60
exit: 0x0
frames shown: 1373 (PresentX 1374)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19392 MB
xcl-smoke v1
commit: 1f8b821e4afdd28bd845888d4a5ac21a9dbf9201
gpu: AMD Radeon RX 9070 XT
title: 5ec87c56
seconds: 60
exit: 0x0
frames shown: 1557 (PresentX 1557)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 20705 MB

Not tested

  • D3D12 debug-layer validation run during heavy world streaming with SetPredicationBufferX, NVIDIA GPUs, Intel Arc GPUs, wave32-only GPUs, and Linux/Wine

AI use

  • Antigravity (frontier model; exact model identifier not exposed to agent) assisted with extending late_release for owned PC resources, wiring list_x::on_create/on_release retirement outside the lock, diagnosing and fixing the parallel-test drain/lifetime race, running the test suites and smoke runs, and drafting this description. All builds, tests, and regression runs were executed locally on the hardware above.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@raylitebull

Copy link
Copy Markdown
Member

Code review on 4fb33c7:

  • Deferring ListState::pred_buf drops through late_release::hold_pc_resources on both list_x::on_release and on_create address reuse (dropping outside LISTS and HELD locks) cleanly prevents premature COM Release while GPU fences are still pending.
  • Diff audited and CI workflow approved (running).

Remaining before merge: Build and test finishing green plus dual-vendor (AMD + NVIDIA) verification on 4fb33c7.

@upipibi

upipibi commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

NVIDIA half of the dual-vendor check, exact head 4fb33c7, tested as-is on its base 6020a33b. It conflicts with main now, so this isn't a merged tree. RTX 3070 8 GB, driver 616.56, Windows 11, debug, full workspace build.

The lib suite doesn't finish. cargo test -p xcl --lib -- --test-threads=4:

  • list_x::tests::set_predication_buffer_state_transitions_and_lifecycle fails.
  • A few tests later, the process dies with a segfault (exit 139, no result line). The last test that printed was null::tests::device_queue_fence_resource_roundtrip ... ok.

This PR's own CI run (Windows, no GPU) shows the same thing: that test fails, then the process exits without finishing. main's CI passed at the base 6020a33b. So the GPU isn't needed to hit it.

A likely cause, from reading the code (not isolated yet): step (h) of that test calls on_release(list_key) and then asserts fake_res.refs == 0. With this PR, on_release hands pred_buf to late_release::hold_pc_resources. When late release is on and a known queue has work in flight, that keeps the resource (late_release.rs:133-137) instead of dropping it. In a parallel run, other tests (this PR's new one included) register real queues, so the reference count stays 1 and the assert fails. Then the test frees the FakeComRes box while HELD still points at it. The next drain would Release freed memory, which fits a segfault a few tests later. If so, the old test needs to wait for or drain the held list before asserting, and to keep fake_res alive until it's released. Alternatively, on_release could drop it at once when the queue has nothing in flight.

Other runs on this head:

run result
gpu_ with XCL_DIRECT_DXIL=1, single-threaded 134 passed, 1 failed: gpu_image_get_resinfo_1d_array, which comes from the base; #2046 fixed it in main (5ebaf3eb)
cargo test -p xcl --test e2e 3 passed
System log nvlddmkm 153 events 0

(Edited: I've dropped the A/B I had queued here. tiagonleal's run on an RTX 5070 Ti covers it: 8 of 10 parallel runs abort at this head, 7 of 7 pass at 6020a33, and the test passes alone and single-threaded.)

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

@tiagonleal

Copy link
Copy Markdown
Contributor

Verification on NVIDIA RTX 5070 Ti (Windows 11), head 4fb33c7e37127a465645044e4c338aa98a022545: hle::d3d12x aborts under parallel scheduling here, passes single-threaded

Tested

  • Head 4fb33c7e37127a465645044e4c338aa98a022545 (git rev-parse HEAD of a clean worktree, no local changes; merge-base with main 6020a33b16ba2de25b3aef5947552f2dc1831f09). The PR was open and the head unchanged when I started and again before writing this.
  • Windows 11 Pro 10.0.26300 (Developer Mode off), NVIDIA GeForce RTX 5070 Ti 16 GB (driver 32.0.16.1742), AMD Ryzen 7 9800X3D, 32 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09), cargo 1.100.0, debug profile (the cargo test default).
  • cargo test -p xcl --lib hle::d3d12x -- --test-threads=4 --nocapture, run through cargo 3 times: all 3 aborted with 0xc0000005 STATUS_ACCESS_VIOLATION before the summary line (182 tests selected).
  • The same test executable run directly (hle::d3d12x --test-threads=4) 4 more times at this head: 3 aborted the same way, 1 passed (180 passed, 0 failed, 2 ignored); a fifth start failed to launch and is not counted. A further 3 runs: 2 aborted, 1 passed. In total 8 aborts in 10 complete parallel runs.
  • In the aborting runs the output shows hle::d3d12x::list_x::tests::set_predication_buffer_state_transitions_and_lifecycle ... FAILED (an existing test, not one this PR adds), followed by a panic in thread xcl-late-release at windows-core-0.62.2\src\interface.rs:41:18: misaligned pointer dereference: address must be a multiple of 0x8 but is 0x9e06fe001, then thread caused non-unwinding panic. aborting.. Because the process aborts, I do not have that test's assertion message.
  • Not an effect of running in parallel on this machine alone: on the merge-base 6020a33 the same command (hle::d3d12x, --test-threads=4, 179 tests selected) passed 7 times out of 7 (177 passed, 0 failed, 2 ignored each time; 2 through cargo, 5 through the test executable).
  • Passing at this head: hle::d3d12x -- --test-threads=1 --nocapture: 180 passed, 0 failed, 2 ignored (8 s). set_predication_buffer_state_transitions_and_lifecycle alone: 1 passed. hle::d3d12x::list_x::tests (30 tests) and hle::d3d12x::late_release (4 tests, including the PR's two new ones), each at 4 threads: passed in all 3 repeats, 0 failed. The PR's own new tests (held_pc_resources_wait_until_the_queue_is_past_their_batch, without_queues_held_pc_resources_go_at_once, on_release_and_on_create_retire_pred_buf_until_queue_passes) passed in the runs where the process survived.
  • With --skip on_release_and_on_create_retire_pred_buf_until_queue_passes at 4 threads the abort still happened in 1 of 5 runs (4 passed, 179 tests), against 8 of 10 without the skip. So that new test makes it more likely but is not the only way to reach it.
  • This is the same failure @upipibi reports above on an RTX 3070, seen independently on a second NVIDIA card; the numbers here add the A/B against the merge-base (7 of 7 pass there), the single-thread pass, and the --skip result. The cause he describes fits what I see (the abort follows that test failing, on the xcl-late-release thread); I did not verify it either.
  • SKIPPED: none seen in the hle::d3d12x runs I looked at.

Not tested

  • AMD (the other half of the requested dual-vendor check).
  • The whole-lib run and cargo test --workspace at this head; other test targets; release profile.
  • The retail game and any xcl-smoke run; Developer Mode on (off here).
  • Whether the abort is also possible outside the unit tests.
  • Another job was using the same machine in bursts. I started each run when no game process above 2 GB was running; I did not see any run abort or pass differently depending on that.

AI use

  • Claude Code created the clean worktrees, built and ran the tests above at this head and at the merge-base, and wrote this report. No code was changed.

Run and written with Claude Code (Claude Fable 5.2 and Claude Sonnet 5.5).

Command lists that call SetPredicationBufferX lazily allocate a PC ID3D12Resource predication buffer. When list_x::on_release removed a released list or list_x::on_create overwrote an entry at a reused command-list address, dropping that pred_buf synchronously inside COM Release could destroy the resource while previously submitted GPU command lists still referenced it. Extend late_release with hold_pc_resources (sealing immediately when queues is empty) so list_x::on_release and on_create defer pred_buf drops outside the LISTS lock until every known queue has passed the batch fence, and update the synthetic pred_buf lifecycle unit test to drain late_release before checking the final refcount.
@Simon-CR
Simon-CR force-pushed the pr/defer-pred-buf-release branch from 4fb33c7 to 1f8b821 Compare November 25, 2026 14:39
@Simon-CR

Copy link
Copy Markdown
Author

Rebased onto current main (06c9f550 -> 1f8b821e) and fixed the CI / parallel-test failure! Thank you @raylitebull, @upipibi, and @tiagonleal for catching and pinpointing both causes:

  1. Zero-queue throttle fix (late_release::Held::drain_all): When queues.is_empty() is true (such as on GPU-less CI), drain_all no longer waits on the 10 ms SEAL_EVERY window, so hold_pc_resources seals and drops synchronously before returning.
  2. Parallel-test drain & unwind safety (set_predication_buffer_state_transitions_and_lifecycle step (h)): When parallel tests register live queues, on_release defers pred_buf through late_release as intended. Step (h) now drains late_release::drain_known(true) until fake_res.refs == 0 (using AcqRel/Acquire on FakeComRes and pinning fake_res + boxed res_vt on timeout so xcl-late-release can never hit freed memory).

Re-tested on 1f8b821e (RX 9070 XT, Win10):

  • 10/10 consecutive parallel runs of cargo test -p xcl --lib hle::d3d12x -- --test-threads=4: 220 passed, 0 failed, 2 ignored each (0 aborts)
  • Full workspace (--no-fail-fast -- --test-threads=4): 1266 passed, 2 pre-existing #2115 fuzz failures, 46 ignored
  • Paired 60 s smoke against 06c9f550: 1557 frames vs 1373 on main, 0 device removals, 0 crashes.

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.

5 participants