Skip to content

D3D12.X: sweep completed PSO generation pins when nothing new is pinned - #3185

Open
afpruivo wants to merge 1 commit into
xcl-dev:mainfrom
afpruivo:pso-pin-sweep
Open

afpruivo wants to merge 1 commit into
xcl-dev:mainfrom
afpruivo:pso-pin-sweep

Conversation

@afpruivo

@afpruivo afpruivo commented Nov 28, 2026 •

Copy link
Copy Markdown

pso_async::retire_native_generations keeps each pinned NativeGeneration (an extra reference on the real PSO) until its submission's receipt completes. Completed batches were swept only when a new pending batch was queued. Retiring an already-complete receipt returned early, and released() didn't sweep at all. A batch queued behind a pending fence therefore leaked its pins, and the pipeline's TWO_PASS entry with them, whenever every later submission had already completed or no later submission came. Introduced in 11613fb (#2959). Fixes #3441.

What is in it (one commit, on main c53e9696)

  • pso_async.rs: sweep completed batches when retiring an already-complete receipt, and at the start of released(). Queuing an in-flight pinned receipt starts the late-release worker.
  • late_release.rs: the worker sweeps the completed pins on each tick, so a fence that completes after the title's last submission and release is still looked at (thehollowedone's case and fix from the review below).
  • list_x.rs tests: a_completed_later_submission_retires_earlier_generations, releasing_a_standin_after_its_submission_completed_releases_the_pipeline, and a_submission_completed_after_the_last_release_is_swept_while_idle (thehollowedone's test). Each fails without its part of the fix. The tests that use the retirement queue take a shared lock (RETIRING_TESTS); its comment now says which other sweeps run without it.

Tested

Regression run

main:

xcl-smoke v1
commit: c53e96960265a82477a9c98677afb0402e98d486
gpu: NVIDIA GeForce RTX 3090
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1573 (PresentX 1573)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15794 MB

this PR:

xcl-smoke v1
commit: 6f263066986fb0212b9e41b40c92c201d7a71449
gpu: NVIDIA GeForce RTX 3090
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1575 (PresentX 1575)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 16796 MB

Not tested

  • AMD and wave64 hardware here (YousefElghanam's results on a discrete AMD card for the earlier head are on Tests: two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs fails on an AMD RDNA2 iGPU since #2959: one two-pass state outlives its PSOs #3441).
  • No measured gain in play: a running title keeps submitting, so pins usually clear at the next pending submission.
  • The cost of the worker's sweep: one GetCompletedValue per distinct fence among the pending batches on every 4 ms tick while any are pending (a lock and an empty check when none are). And of the sweep in released() at a level change that releases many pipelines. Not measured.
  • The smoke runs stay on "press A to start" (smoke.ps1 sends no input).
  • Nothing in HANDOFF.md or docs/specs becomes wrong, as far as I can tell.

AI use

  • Claude Code, Claude Opus 5.5: found the cause, wrote the fix and tests, ran the tests and smoke runs, and drafted this description. The idle-completion fix and its test are thehollowedone's (posted in review here). The rebase, that addition and this update were made and posted by Claude Code while I was away, under the standing approval I gave it for this session.

🤖 Generated with Claude Code

@JJAutoCompare

Copy link
Copy Markdown

Tested

Independently reran the pinned XCL library test binary for 0cd75eae5062102abdb35fc2984c1d9f40a4ac73 on Windows 11 x64 build 26200.9974 with a Ryzen 7 3800X. The binary was built with Rust 1.100.1 (x86_64 MSVC) from the exact-head source plus one private fixture in the existing test module.

xcl-8941870b96d3eddf.exe hle::d3d12x::list_x::tests::a_retirement_sweep_releases_only_completed_receipts --exact --nocapture --test-threads=1
xcl-8941870b96d3eddf.exe hle::d3d12x::list_x::tests::releasing_a_standin_after_its_submission_completed_releases_the_pipeline --exact --nocapture --test-threads=1

Both native processes exited 0. Total was 2 passed, 0 failed, 0 ignored, 0 skipped. The fixture asserted that three synthetic pipelines went from refcounts [2,2,2] to [1,1,1] after dropping the title owners. An already-complete submission carrying no new pin swept only the completed middle receipt, leaving [1,0,1]. After the other two receipts completed, calling released() on a distinct absent stand-in swept the remaining pins, leaving [0,0,0]. Updating the completion counters alone left refcounts unchanged before each sweep. The stock control passed separately.

Not tested

Real D3D12 queues and fences, live COM objects, GPU, retail titles, leak rates, and the workspace suite.

AI use

T3 Code through Codex (gpt-6.2-sol, high) prepared the fixture, verified the source and binary, independently reran the tests, and drafted this report.

@thehollowedone

Copy link
Copy Markdown
Contributor

Follow-up on PR #3185 at head 0cd75eae5062102abdb35fc2984c1d9f40a4ac73, merged cleanly onto current main ff0c364b3de011c13ed45865480c21918a79ec89 (merge commit 7e58b565d5503f6db3ddee50fa3f7223ebc11596).

One lifecycle ordering still retains a pipeline: the title releases its last stand-in while the submission receipt is pending, then the fence completes after the PR's release-time sweep. If no later submission or PSO release occurs, nothing is guaranteed to sweep the receipt again. The native pipeline reference therefore remains retained after GPU use has ended. The related observed failure is tracked in #3441.

I added this schedule as a synthetic regression. With the regression test added but before the runtime candidate, it failed after the fake fence advanced (left: 1, expected 0). With the candidate below, it passes, along with the three existing retirement tests: 4 passed, 0 failed, 0 ignored, 0 skipped, run separately with XCL_LATE_RELEASE=0. git diff --check passed.

The candidate starts XCL's existing late-release worker after queuing an incomplete pinned receipt, then sweeps these receipts on worker ticks. This uses one existing worker and does not poll fences synchronously on each submission. This is focused CPU evidence only; I did not run the workspace suite, live D3D12 fence checks, GPU tests, or title smoke. The PR remains a draft, so this is not a merge check.

Candidate fix and regression test
diff --git a/crates/xcl/src/hle/d3d12x/late_release.rs b/crates/xcl/src/hle/d3d12x/late_release.rs
@@ -254,11 +254,16 @@ fn start_thread() {
         let _ = std::thread::Builder::new().name("xcl-late-release".into()).spawn(|| loop {
             std::thread::sleep(TICK);
+            super::pso_async::sweep_native_generations();
             unsafe { drain_known(false) };
         });
     });
 }
 
+pub(super) fn start_native_generation_worker() {
+    start_thread();
+}
+
 /// A pipeline state XCL owns (raw pointer, 0 = none) is let go: the compute pass of a two-pass
diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs
@@ -3758,7 +3758,7 @@ fn dedup_generations<G>(pins: impl Iterator<Item = G>, identity: impl Fn(&G) -> usize) -> Vec<G> {
-fn sweep_native_generations() {
+pub(super) fn sweep_native_generations() {
@@ -3788,6 +3788,7 @@ pub(super) fn retire_native_generations<'a>(
     }
     RETIRING_GENERATIONS.lock().unwrap().push(receipt, pins);
+    super::late_release::start_native_generation_worker();
     sweep_native_generations();
 }
diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs
@@ -7563,6 +7563,24 @@ fn releasing_a_standin_after_its_submission_completed_releases_the_pipeline() {
     assert_eq!(native.refs.load(Ordering::Relaxed), 0, "a completed submission kept the released pipeline");
 }
 
+/// A completion after the title's last release needs a background sweep even when no later
+/// submission or PSO release arrives to trigger one.
+#[test]
+fn release_before_receipt_completion_is_reaped_while_idle() {
+    let _serial = RETIRING_TESTS.lock().unwrap_or_else(|e| e.into_inner());
+    let standin = 0x7E57_B02Busize;
+    let (native, completed) = pinned_in_flight(standin);
+    super::super::pso_async::release(super::super::pso_async::released(standin));
+    assert_eq!(native.refs.load(Ordering::Relaxed), 1, "in-flight receipt released its generation");
+
+    completed.store(1, Ordering::Release);
+    let deadline = Instant::now() + Duration::from_secs(1);
+    while native.refs.load(Ordering::Relaxed) != 0 && Instant::now() < deadline {
+        std::thread::sleep(Duration::from_millis(1));
+    }
+    assert_eq!(native.refs.load(Ordering::Relaxed), 0, "idle completion worker did not retire the generation");
+}

AI use: Codex (GPT-6.2 Sol) prepared and tested the candidate; GPT-6.2 Astra checked the fence-completion lifecycle.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Triage (#1607) – head 0cd75ea

No open or merged PR makes the same change.
retire_native_generations now sweeps on an already-complete receipt and released sweeps before removing the pending entry; the two new tests cover both paths. An incomplete receipt with no pins still returns before sweeping, so that case keeps the old behaviour despite the title ("when nothing new is pinned"). released now polls the retiring fences on every title PSO release; cheap when the list is empty, but not measured.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Review (head 0cd75ea, diff read, not a merge-check; draft):

  • No blocking finding. released() (pso_async.rs ~10979) now sweeps before every stand-in release. A sweep reads GetCompletedValue once per distinct fence among the pending batches, so a title that releases thousands of pipelines at a level change pays that many sweeps. Small, but unmeasured.
  • The tests take RETIRING_TESTS. Other tests that reach sweep_native_generations through released() without that lock (any test releasing a stand-in) can still drop another test's pin early. The description's 30/30 suggests it's fine in practice; it's worth a word in the lock's doc comment.

Fine:

  • Sweeping when an already-complete receipt is retired fixes the case where every later submission had completed and earlier pins stayed for good (11613fb / Tests: compare benchmark regression thresholds #2959).
  • sweep_retirements drops the retired generations outside its lock (~3744), so a Release that comes back through standin_released -> released() -> sweep doesn't deadlock.
  • Both new tests fail without the fix, and the flaky two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs loop (30 % -> 30/30) points at the same leak.

Once it's out of draft, a suite run and smoke pair on current main would make it ready.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@cj-tomlin

cj-tomlin commented Dec 1, 2026 •

Copy link
Copy Markdown

Test report, head 0cd75eae5062102abdb35fc2984c1d9f40a4ac73 merged onto main f07e426dade72aa7112cf6bfe08ca6f6ff5a9d5c (clean, merge ad3d78b5).

Edit: the lib test binary aborts in about 3 of 10 runs with this PR; see below. This report does not count as a clean run.

New abort. A second full-suite run, on this merge plus #1249 (which only touches xaudio2.rs), lost the whole lib test binary: exit code: 0xc0000409, after thread 'xcl-pso-ahead-2' panicked at windows-core-0.62.2\src\interface.rs:41:18: misaligned pointer dereference: address must be a multiple of 0x8 but is 0x4332. cargo test -p xcl --lib -- --test-threads=4 hle::d3d12x::pso_async, alternating between the trees: main f07e426d 10 of 10 ok, the merged tree aborted 3 of 10, the same way each time. Backtrace: pso_async::release ← pso_async::complete ← run_build ← spawn_backlog_worker.

0x4332 is the fake pipeline of graphics_first_use_carries_the_exact_serialized_recipe_identity: it submit_lazys a job returning 0x4332, then calls released(standin) without waiting. When a backlog worker has already started that job, the build is orphaned and complete hands 0x4332 to release, which drops it as a real ID3D12PipelineState. On main released usually drops the unstarted job first. My guess is that the sweep_native_generations() call this PR adds at the top of released widens that window, but I have not proven it. The race is in the test, so the fix may belong in the test (for example a job returning 0, or waiting for the build before released). Either way, with this PR the lib binary aborts about 30 % of the time on this machine.

Not run: a title (no smoke run), AMD / wave64.

Tool and model: Claude Code (Claude Opus 5.5) merged, built, ran the tests and drafted this; I checked it.

retire_native_generations swept completed batches only when a new
in-flight batch arrived. A submission whose receipt was already complete
returned before the sweep, and the title's release of a stand-in did not
sweep at all. A pipeline pinned by a submission still running when it was
retired then kept its last reference for good when every later submission
had finished first, or when none followed: its TWO_PASS entry stayed too.

That made two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs
fail about 3 in 10 runs on an RTX 3090 ("releasing the title PSOs releases
their two-pass state", left 1; xcl-dev#3441). A trace showed the leaked
pipeline's pin queued behind a pending fence, then only complete
submissions.

Sweep before the early return for a complete receipt, at the start of
pso_async::released, and on each tick of the late-release worker, which
a pinned in-flight submission now starts: a fence that completes after
the title's last submission and release is otherwise never looked at
again (the case and the worker sweep are thehollowedone's, on xcl-dev#3185).
Three unit tests cover the cases and fail without the fix; the
generation retirement tests run one at a time, since a concurrent sweep
may still hold another test's retired pin.

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@afpruivo

afpruivo commented Dec 2, 2026

Copy link
Copy Markdown
Author

Rebased onto main c53e9696 and updated (one commit, now 6f263066, force-pushed; description updated). Thanks to everyone who tested and reviewed this:

#3441's test alone on the 3090: main c53e9696 failed 3 of 6 (left: 1), 6f263066 passed 6 of 6. Full suite and smoke pair are in the description. Marking ready for review.

AI use: Claude Code (Claude Opus 5.5) rebased, added the fix, ran the tests and wrote this while I was away; posted with my standing approval for this session.

@afpruivo
afpruivo marked this pull request as ready for review December 2, 2026 23:15
@Foniass

Foniass commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up review of 6f263066, since 0cd75ea (code reading only, not a merge-check) · no new findings

In short: This push adds a third sweep point. The late-release worker now sweeps completed pipeline pins on each tick, and it is started when a pinned submission is still in flight. A fence that completes after the title's last submission and release is now seen too. The lock doc comment now says which other sweeps run without it, and a third unit test covers the idle case. Nothing blocks it.

Checked and fine
  • Earlier finding fixed: the RETIRING_TESTS doc comment now says that released() and the worker also sweep without the lock, and why the tests stay correct.
  • Earlier point, still unmeasured but small: a sweep per stand-in release reads GetCompletedValue once per distinct pending fence. The worker's sweep adds the same every 4 ms (TICK). With nothing pending, it takes the lock twice and builds an empty map.
  • retire_native_generations starts the worker only when it pushes a pending batch, so a title with no pinned in-flight work starts nothing new. start_thread is a call_once.
  • sweep_retirements drops retired generations outside its lock, so a Release that comes back through released() cannot deadlock the worker.
  • a_submission_completed_after_the_last_release_is_swept_while_idle completes the fence after the release and waits up to 1 s for the pin to go, with no other submission or release.
  • The commit message credits the case and the worker sweep to the reviewer on D3D12.X: sweep completed PSO generation pins when nothing new is pinned #3185, and links Tests: two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs fails on an AMD RDNA2 iGPU since #2959: one two-pass state outlives its PSOs #3441.

Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 2, 2026
@afpruivo

afpruivo commented Dec 3, 2026

Copy link
Copy Markdown
Author

The flaky two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (#3441) on current main, and with this PR, same machine and command (cargo test -p xcl --test gpu_two_pass_cache_e2e, run 15 times in a row, RTX 3090, inbox D3D12):

  • main 395405e: 3 of 15 runs failed (releasing the title PSOs releases their two-pass state, left 1, right 0)
  • 6f26306 merged onto 395405e (clean merge): 0 of 15 failed

With a temporary trace on main I saw the cause this PR fixes. In each failing run, the stand-in of the leaked pipeline was released while its real pipeline still had 2 references (the others had 1). Late release then dropped its reference, and the count never reached 0. Which pipeline leaked changed from run to run (the first MS, the second MS, or the GS). It also failed in the disabled case, so the disk cache is not involved.

Other PRs' test reports count this test's two failures against the PR (for example on #3676), so this PR also cleans up their counts.

AI use: Claude Code (Claude Opus 5.5) ran the tests and traced the references; I posted it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests: two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs fails on an AMD RDNA2 iGPU since #2959: one two-pass state outlives its PSOs

5 participants