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.)


Update 16:33Z, new head 1f8b821e: repeat runs, which pvfreis's one-run RTX 3060 report below lists as not done. RTX 3070 (616.56), Windows 11, debug: the crash we reported at 4fb33c7 is gone. Both trees were built first, then hle::d3d12x --test-threads=4 ran 10 times on each, alternating the PR and its base 06c9f550 on an otherwise idle machine:

tree runs aborts / segfaults runs with a failed test
base 06c9f550 10 0 0 (217 passed each)
this PR 10 0 1 (220 passed in the other 9)

The one failing run is a test this PR doesn't add, title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels:

panicked at crates\xcl\src\hle\d3d12x\title_textures.rs:2082:13:
assertion `left == right` failed: superseded placements must be retired from active tracking
  left: 14
 right: 16

The other two failures in that run (transitions_apply_when_their_list_is_submitted, vram::tests::d32s8_targets_take_their_own_size_of_video_memory) are PoisonErrors that follow from it. I saw the same assertion once in an earlier set of 5 runs on this head, but those overlapped other work on the machine. Counting both sets: 2 of 15 runs with the PR, 0 of 15 on the base. If the failures were spread at random over the runs, the chance of both landing on the PR is about 24% (computed by script), so this doesn't show the PR causes it.

My guess, not checked: the test counts active placements in shared state while other hle::d3d12x tests run in parallel. This PR holds pred_buf releases in late_release longer, which could change when a superseded placement is retired. If you want to rule it out, run that test alone, or the module with --test-threads=1, a few times.

AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only.

@tiagonleal

tiagonleal commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Verification on NVIDIA RTX 5070 Ti (Windows 11), head 1f8b821e4afdd28bd845888d4a5ac21a9dbf9201: the parallel hle::d3d12x abort I reported is gone

This replaces my earlier report on this PR. That report was for head 4fb33c7e37127a465645044e4c338aa98a022545, where hle::d3d12x aborted with 0xc0000005 in 8 of 10 parallel runs. This one is for the rebased and fixed head 1f8b821e4afdd28bd845888d4a5ac21a9dbf9201 (pushed 14:39 UTC). Short answer: the failure no longer reproduces here.

Tested

  • Head 1f8b821e4afdd28bd845888d4a5ac21a9dbf9201 (git rev-parse HEAD of a clean worktree, no local changes; merge-base with main 06c9f5505f63f10c5ec49ef932d75b2c28177324). The PR was open and this was the latest head when I started and again before writing.
  • Windows 11 Pro 10.0.26300 (Developer Mode on), NVIDIA GeForce RTX 5070 Ti 16 GB (driver 617.42, 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 command that failed before, cargo test -p xcl --lib hle::d3d12x -- --test-threads=4 --nocapture: 3 runs through cargo, all passed (220 passed, 0 failed, 2 ignored each).
  • The same test executable run directly (hle::d3d12x --test-threads=4): 10 runs, all passed (220 passed, 0 failed, 2 ignored each). No abort and no STATUS_ACCESS_VIOLATION. Together that is 0 failures in 13 parallel runs, against 8 aborts in 10 at the old head.
  • hle::d3d12x -- --test-threads=1: 220 passed, 0 failed, 2 ignored (8 s).
  • The PR's named tests, each alone at 4 threads: 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 and set_predication_buffer_state_transitions_and_lifecycle: 1 passed, 0 failed each.
  • SKIPPED: none in any of the hle::d3d12x runs above.
  • Whole lib, cargo test -p xcl --lib -- --test-threads=4, run through the test executable 3 times: the process now finishes every time (no abort, which is what @upipibi saw), 1195 passed; 4 failed; 26 ignored, 24 SKIPPED lines (no wave64 on this card). The same 4 tests fail in all 3 runs: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0 to _3, with CreateComputePipelineState: Not enough memory resources are available to complete this operation. (0x8007000E). They fail the same way alone at 1 and 4 threads (gpu::dxil::fuzz_tests: 1 passed, 4 failed, 2 ignored). They are in gpu::dxil, not in code this PR touches, and I did not run them on main, so I cannot say whether main shows the same here.

Not tested

  • AMD (the other half of the dual-vendor check).
  • cargo test --workspace at this head; other test targets; release profile.
  • Whether the 4 gpu::dxil::fuzz_tests failures also happen on main on this machine.
  • The retail game and any xcl-smoke run.

Run and written with Claude Code (Claude Fable 5.2 and Claude Sonnet 5.5). No code was changed.

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.

@pvfreis

pvfreis commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

NVIDIA run of 1f8b821e (this PR's head) merged cleanly onto main 08294413, with the old head 4fb33c7 as a control. RTX 3060, one run per tree, so not a rate. The test @upipibi and @tiagonleal saw fail at the old head fails there here too and passes at the new one, and the new head has no failure main lacks.

tree lib binary whole workspace set_predication_buffer_state_transitions_and_lifecycle
main 08294413 1203 passed, 0 failed, 26 ignored 1271 passed, 1 failed, 46 ignored ok
1f8b821e merged onto it 1206 passed, 0 failed, 26 ignored 1274 passed, 1 failed, 46 ignored ok
4fb33c7 as is, on its own base 6020a33b exits with 0xc0000005, no result line (1127 tests selected) no total FAILED
  • The three extra passes are the PR's new tests, none SKIPPED: 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. Test by test, nothing else differs from main.
  • The one failure in both workspace runs is the e2e title test's presents assertion (e2e.rs:249: frame_boundaries 1, presents 0): CreateSwapChainForHwnd returns 0x887A0022 in this session. The control fails the same assertion (e2e.rs:225 there).
  • In the control the named test failed and the process died about 70 tests later. Output was captured (no --nocapture), so this run does not show where the access violation was; tiagonleal's runs above do. Test suite: parallel cargo test intermittently dies with 0xc0000005 (hle::d3d12x queue_tests × list_x race) #1978 reports the same exit code on main, so I count only the failed test, not the abort, as this run's evidence against the old head. The named test itself also changed between the heads (it now drains late_release before checking the count).

Tested

  • RTX 3060 12 GB (driver 617.14), Ryzen 9 5900XT, 32 GB, Windows 11 (10.0.26300), Rust 1.100.0 MSVC, debug.
  • cargo test --workspace --locked --offline --no-fail-fast -- --test-threads=4, as a separate sandbox user: non-interactive session, Low integrity, NVIDIA 32-lane waves only, no display, no audio device, network off (XCL_NETWORK=0).
  • Three tests that turn on the D3D12 debug layer passed with it on, on main and the merge: set_predication_buffer_state_and_kickoff, test_title_draws_with_a_root_signature_under_the_debug_layer, uploads_start_from_each_subresources_state (the author's AMD run SKIPPED the last two).
  • SKIPPED: the same 25 lines on main and the merge (21 "no wave64", 3 f16 / f32 per-lane forms the direct path refuses, heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows).

Not tested

  • How often the new head passes: the earlier failure is intermittent, and one green run at 4 threads cannot show it is gone. Repeats, other thread counts and a filtered hle::d3d12x run were not done.
  • AMD, Intel, wave64 hardware, a retail title and any smoke run, release, the debug layer beyond those three tests, XCL_LATE_RELEASE=0, presentation and audio output.

AI use: Claude Code. Claude Opus 5.5 staged and ran the three jobs, compared the logs and wrote this; a second Claude Opus 5.5 agent checked it against the logs and the live PR; Claude Fable 5.2 reviewed the text and spot-checked the counts against the logs.

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11): still a merge conflict with current main on the new head — needs another rebase before it can be tested.

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.

6 participants