Skip to content

D3D12.X: compute pipelines not used for a while are released on NVIDIA - #2526

Draft
veyn-sudo wants to merge 2 commits into
xcl-dev:mainfrom
veyn-sudo:pr/release-unused-shader-code
Draft

veyn-sudo wants to merge 2 commits into
xcl-dev:mainfrom
veyn-sudo:pr/release-unused-shader-code

Conversation

@veyn-sudo

@veyn-sudo veyn-sudo commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #2422; only the top commit belongs here.

NVIDIA's driver keeps the compiled code of the pipelines in use in a heap of at most 512 MB and removes the device when a submission needs more (#2403). Code counts from a pipeline's first use until it is released, so this releases compute pipelines that are not in use and builds them again at their next bind. Part of #2403.

It is not enough by itself. With the translations of the default path one command allocator's recordings use 289 MB here, and nothing a recording uses can be released: the device was still removed (132 s and 200 s into a run, 134-141 s without). With XCL_DIRECT_DXIL=1 XCL_HEAP_WINDOWS=1 the code is about 40 % smaller. CharlyRien has a version of this of their own and reports no removal in 480 s with it and the direct path (#2403).

What is in it

  • hle/d3d12x/shader_code.rs (new): which pipelines count, what recordings hold, what to release. XCL_SHADER_CODE_MB is the code to keep in use (GetCachedBlob sizes): 320 on NVIDIA, no limit elsewhere, 0 for none.
  • pso_async.rs: a build can run again (Job is FnMut); past the limit the compute pipelines bound longest ago are released, none used in the last second. Their next bind builds them itself: a turn in the workers' queue behind first builds took minutes.
  • A dispatch put off to the submit (list_x::record_deferred) hands its pipelines to its segment's allocator too: its segment is reset without cl_reset (found by KDenKC).
  • What a recording uses stays: resolving gives the thread a reference, a bind hands it to the allocator the list records on, and that allocator's Reset (or the pool knowing that no list is on it and the GPU is done, alloc_pool::when_done, which checks and takes under one lock) gives it back.
  • real/dispatch_chain.rs: all pieces of a split program count, and its recordings let go of them in the same step.
  • Only compute pipelines behind shared stand-ins: XCL owns those, and their bind waits for them. Graphics code counts but stays, as it would flicker.
  • Tests: the choice of what to release, a bound pipeline living until its allocator's reset, release and rebuild with and without a wait budget, when_done taking under the lock, a split program's recordings and pieces, a dispatch put off to the submit keeping its pipeline with its segment.

699 added lines, 58 of them #2422's and 81 the deferred-dispatch fix and its test. I did not find a smaller part that does anything alone: releasing is unsafe without the references, and they do nothing without it. #2417 keeps resolved pipelines alive too; if it goes in, the references here can use it.

Tested

  • RTX 2070 SUPER (driver 617.14), i7-9700K, 32 GB, Windows 11 26300, Rust 1.99.0, debug for the tests, release for the title.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 0fd22d04: 1441 passed, 0 failed, 47 ignored.
  • 25 SKIPPED lines: 21 no wave64, 3 f16 forms refused, 1 no heap windows.
  • In some runs of one test group a_dispatch_with_a_pipeline_still_building_waits_at_the_submit fails; it does on main e1d84b7 too (4 of 12). execute_indirect_reads_the_arguments_written_before_it failed once with out of memory, not in 8 repeats.
  • The title on a local tree with an earlier version of this and other open PRs, XCL_DIRECT_DXIL=1 XCL_HEAP_WINDOWS=1 XCL_DIRECT_SPLIT_INSTRUCTIONS=0: six runs reached the world and were stopped by low memory after 158-170 s, without a removal, the one with XCL_SHADER_CODE_MB=0 too. In the two where it was measured at most 116 and 132 MB of code was in use, far under 320. With XCL_SHADER_CODE_MB=60 45 pipelines were released and 121 MB stayed in use.

Regression run

main:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 404998dc
seconds: 60
exit: 0x0
frames shown: 1485 (PresentX 1485)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14603 MB

this PR:

xcl-smoke v1
commit: 0fd22d04dc7931066a8560fe178119a2040d0da9
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 404998dc
seconds: 60
exit: 0x0
frames shown: 1549 (PresentX 1549)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14416 MB

Not tested

AI use

  • Claude Code with Claude Opus 5.5 wrote the code and the tests, ran everything on my PC and wrote this description.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WStEFektfHt5AEbK6Wupac

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@pvfreis

pvfreis commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

By reading only, at 5b59fe4 (not run). Three points before the long direct-path run:

  1. Split programs (default with XCL_DIRECT_DXIL=1; your run had =0): count sees piece 0 only. dispatch_chain::RECORDINGS keeps piece 0 and the chain until Reset, so after let_go_done a release can free nothing, and the next bind builds a second set.
  2. let_go_done checks, then removes, with no lock across. If another thread records on a in between, its references go too; a pass over a second later can free a pipeline still in flight. Checking and removing under one lock closes it.
  3. Until D3D12.X: keep caller allocator for untracked lists and retain whole PSOs in dispatch_chain #2333 is in, pipelines bound on a fresh split segment are never released. list_x::split without an idle spare makes a list alloc_pool does not know: bound gets 0, which is never reset, so they stay in in_recordings(), also after the title releases them. D3D12.X: keep caller allocator for untracked lists and retain whole PSOs in dispatch_chain #2333 (a7b93a8) registers the segment and merges cleanly with this.

AI use: Claude Code (Claude Opus 5.5) found this; a second Claude Opus 5.5 re-derived it; Claude Fable 5.2 reviewed the text; checked by a person before posting.

@veyn-sudo

Copy link
Copy Markdown
Author

Thanks, checked against the code: all three are right. 2 is a real race, and I'm fixing it here by checking and removing under one lock. 1 and 3 I'm fixing too; until then please don't read results on 5b59fe4 as final, and run it with XCL_DIRECT_SPLIT_INSTRUCTIONS=0. 2 can free a pipeline that is still in flight.

AI use: Claude Code (Claude Opus 5.5) wrote this.

@veyn-sudo
veyn-sudo force-pushed the pr/release-unused-shader-code branch from 5b59fe4 to 6d19dc0 Compare November 25, 2026 22:36
@veyn-sudo

Copy link
Copy Markdown
Author

1 and 2 are fixed in the new head. For 3 I was too quick: #2333 already fixes the split segments, so I'm not duplicating it here, and the two conflict in dispatch_chain.rs. Until it is in, those pipelines stay: in one run here 62 of 524 bound pipelines were only under allocator 0.

AI use: Claude Code (Claude Opus 5.5) wrote this.

@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 164 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of the compute-pipeline lifetime tests (a_pipeline_is_forgotten_before_its_address_is_free, an_unused_compute_pipeline_is_released_and_built_again, the_longest_unused_go_first_and_only_as_many_as_it_takes, a_bound_pipeline_lives_until_its_allocator_is_reset, a_chains_recordings_let_go_of_every_piece): 5 passed.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

@KOLESNiii KOLESNiii mentioned this pull request Nov 25, 2026
@KDenKC

KDenKC commented Nov 26, 2026

Copy link
Copy Markdown

@veyn-sudo, results for this PR at four budgets on the retail title, using its own count line (you asked @Pipodi for counter runs in #2403 (comment); these bracket your NVIDIA default of 320): the removal at world start still comes in every arm, while the count is at or under ~370 MB. Results from one RTX 5090.

Setup

Results

XCL_SHADER_CODE_MB compute PSOs released your count ("MB of shader code left in use") device removed (0x887a0005) peak commit
0 – – 138.0 s 36.7 GB
250 83 (126.5–131.5 s) 250 → 314 135.4 s 35.5 GB
340 19 (134.3–136.5 s) 340 → 368 136.6 s 36.5 GB
370 0 (never reached 370) < 370 134.6 s 35.2 GB

In every arm the world's work starts at ~126 s (200–770 draws and 2,000–8,000 dispatches per 2 s, 0–3 presents), and the device is removed 8–12 s later. No GPU-wait timeouts and no skipped draws in any arm; 0–128 skipped dispatches.

Observations

  • With a 370 MB limit nothing was released, and the device was still removed: the heap filled while the counted code was under 370 MB.
  • With 250 MB, 83 compute pipelines were released within 5 s, and the count still rose from 250 to 314 MB. The removal came at the same point as the other arms. It didn't thrash into waits; it ran out of heap.
  • From the PR's own doc, as we read it: the count is GetCachedBlob sizes, about 1/9 under what the code takes in the heap, and it covers lazily built pipelines. Graphics are counted but never released. Not counted: pipelines built in the call (outside pso_async), stand-ins, and the driver's own code. (@Jonas-automated measured about 80 MB of the heap going to the driver's background work on a 4090: NVIDIA: the device is removed when the pipelines in use need more than 512 MB of shader code (0x887a0005) #2403 (comment).) So 340–370 MB counted is roughly 380–415 MB in the heap before those.
  • The margin is small to begin with: @Kalmarv measured ~321 MiB bound within one second at world start (NVIDIA: the device is removed when the pipelines in use need more than 512 MB of shader code (0x887a0005) #2403 (comment)), so any budget has to sit just under the limit.
  • Graphics are a sizable share. The cache's 276 graphics driver blobs alone are 87 MiB, a lower bound, since most of this title's graphics pipelines came from NVIDIA's own cache.

Possible next steps (not tested):

  • Release graphics pipelines not used lately too.
  • Count pipelines built outside pso_async (and stand-ins) toward the limit.
  • A log of the counted bytes split into graphics and compute at each release would show the split directly. We couldn't separate them from the current line.

Not measured: the graphics/compute split of the count at the removal; how many released pipelines were rebuilt; frame times; a second run of each arm (they ran once, in the order 0, 340, 370, 250, under one exe name, so NVIDIA's own cache warmed across arms); any other GPU or driver; the larger shader-heap driver setting.

Thanks to SlopTart for the RADV/Wine lifetime results (#2526 (comment)), and for your correction on the pool race and split-tail accounting (#2526 (comment)), which made this head the one to run.

AI use: Claude Code (Claude Opus 5.5) built the tree, ran the arms, measured the logs and wrote this comment.

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

@itsSio

itsSio commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Independent dual-PC NVIDIA full-workspace test report on clean head 6d19dc04d80f4f502de54d8aa6c1f714fb8c10d7.

Tested

  • Machine 1: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11 Pro build 26200.9974, rustc 1.97.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug build
    • cargo build --workspace: exit 0 (1 warning)
    • cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture: 1428 passed, 0 failed, 46 ignored (shader_code, pso_async::used_tests, dispatch_chain::tests, and fallback_stream_tests all passed)
    • SKIPPED: 26 (wave64-only GPU tests, #1426 wave32 f16/cvt refusals, no heap windows, pso_measurement driver-cache disable 0x887A002D)
  • Machine 2: NVIDIA GeForce RTX 2080 SUPER (driver 32.0.16.1088), AMD Ryzen 5 5600X, 16 GB RAM, Windows 11 Pro build 26200.8653, rustc 1.99.1 (stable-x86_64-pc-windows-msvc), Windows SDK 10.0.26100 DXC 1.8.2502.11, debug build
    • cargo build --workspace: exit 0 (3 warnings)
    • cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture: 1428 passed, 0 failed, 46 ignored
    • SKIPPED: 29 (same 26 as Machine 1 plus 3 requiring d3d12SDKLayers.dll)

Not tested

  • Retail title / scripts/smoke.ps1 runs
  • Release builds (--release)
  • AMD / Intel GPUs, native wave64, Linux / Wine / vkd3d-proton

AI use
Ran via Samaritan:

  • Google Antigravity CLI (Gemini 4 Hydrogen (High)): built the branch and ran the workspace test suite on both NVIDIA machines.
  • Google Antigravity CLI (Gemini 4 Neon (High)): coordinated the runs and wrote this test report.

@pvfreis

pvfreis commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@veyn-sudo a data point for "Graphics code counts but stays", and for the graphics share KDenKC raised above: on an RTX 3060, graphics and mesh pipelines fill the same shader-code reserve as compute, at about their blob size.

Tested

  • RTX 3060 (617.14), Ryzen 9 5900XT, Windows 11. Sandbox: non-interactive session, Low integrity, debug, --test-threads=4, NVIDIA 32-lane waves only. One run per mode, main b49be183 (this PR's code is not involved).
  • Your shader_code_limit.rs from NVIDIA: the device is removed when the pipelines in use need more than 512 MB of shader code (0x887a0005) #2403 with three modes added, each mode in its own process: unique synthetic programs of 500 statements, each used once and kept until the device is removed. Compute gets one dispatch each. Graphics get a unique VS + PS (the 500 statements are in the PS) and one draw covering a 4x4 offscreen target. Mesh gets the 500 statements in a one-thread mesh shader that outputs a full-screen triangle, a small unique PS, and one DispatchMesh each. Mixed alternates compute and graphics. GetCachedBlob sizes, summed up to the removal (MB = 10^6 bytes):
mode pipelines at removal compute graphics total
compute only 1,563 469.6 MB – 469.6 MB (447.8 MiB)
graphics only 1,498 – 462.7 MB 462.7 MB (441.3 MiB)
mesh only 1,564 – 464.0 MB (mesh) 464.0 MB (442.5 MiB)
mixed 758 compute + 754 graphics 227.8 MB 232.9 MB 460.6 MB (439.3 MiB)
  • All four modes ended in 0x887a0005, and no pipeline failed to create. Compute alone matches NVIDIA: the device is removed when the pipelines in use need more than 512 MB of shader code (0x887a0005) #2403's 446–449 (the test's "MB" is >> 20).
  • In mixed mode the device was removed when the sum of both reached the limit, with compute at 228 MB. Graphics only, mesh only and mixed ended 1.5 %, 1.2 % and 1.9 % under compute only: one run each, so I read that as the same limit, not as a difference.
  • So on this card counting graphics in the one budget (pso_async.rs:1143) is right, and graphics that stay take room from the same reserve: when they hold much of it, releasing compute alone can't keep the sum under it.
  • On the graphics / compute log split KDenKC suggested: the count is printed only when something was released (if n != 0, pso_async.rs:1192-1195), so a run that releases nothing shows no count. Printing the split there also when n == 0 would show in title runs how much of the reserve graphics and mesh hold.

(Posted here only; the limit data itself is the #2403 test's.)

Not tested: whether the title's own mix of programs behaves like these synthetic ones; whether the driver's background code and the heap-to-blob overhead apply to graphics the same way, amplification shaders, other GPUs and drivers, a larger reserve from a driver profile, a title run, this PR's code.

AI use: Claude Code. Claude Opus 5.5 wrote the three modes, ran them and wrote this; Claude Fable 5.2 rechecked it.

@KDenKC

KDenKC commented Nov 26, 2026

Copy link
Copy Markdown

Source review of head 6d19dc04, no run. I found one gap in how deferred dispatches keep their pipeline alive.

list_x::record_deferred (around list_x.rs:2716) records each deferred dispatch on its own segment with bind::dispatch / bind::execute_indirect. Those resolve through pso_async::resolve_wait, which calls Pending::take, which holds a Ref. The segment is then closed, but nothing in that loop calls shader_code::bound/bound_on for the segment's allocator, the way split does. So the reference stays in the thread's HELD:

  • With more than HELD_MAX (4) distinct pipelines in one submission's deferred dispatches, the earlier Refs are popped and dropped right away.
  • Otherwise they are drained into whichever allocator the thread binds on next, and that allocator's Reset drops them.

After that, only Pending.real holds the pipeline, and it is not in in_recordings(). Once used() > limit() and 1 s has passed since its last resolve (note_use), the next release_unused can release it while the segment may still be executing. This is the cold path: a pipeline still building at SetPipelineState (stand-in, nothing held), built by the submit. World load is also when the code budget is over and a submission can take more than a second.

A shader_code::bound_on(<the segment's allocator>) after the dispatch in record_deferred would tie the references to the segment. The inline bind::dispatch / execute_indirect and console_list::close take the same resolve_wait path without a bound. Those are usually drained by the next bind on the same list, so the deferred loop is the main window.

Not tested: we have not run this head. This is from reading the source only.

AI use: Gemini (Antigravity) flagged it in a first-pass review. Claude Code (Claude Opus 5.5) traced the path in the source at 6d19dc04 and wrote this.

veyn-sudo and others added 2 commits November 26, 2026 09:24
pso_release dropped what XCL keeps of a pipeline after the driver had freed it. A pipeline made by
another thread in between could get the same address and lose its own entries: a translated compute
program then counts as untranslated and its dispatches are skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WStEFektfHt5AEbK6Wupac
xcl-dev#2403)

NVIDIA's driver keeps the code of the pipelines in use in a heap of at most 512 MB and removes the
device (0x887a0005, nothing in DRED, no event) when a submission needs more. With more than
XCL_SHADER_CODE_MB of compiled code in use (320 on NVIDIA, no limit elsewhere) the compute
pipelines bound longest ago are released, but none a recording holds; their next bind builds them
again from the driver's cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WStEFektfHt5AEbK6Wupac
@veyn-sudo
veyn-sudo force-pushed the pr/release-unused-shader-code branch from 6d19dc0 to 0fd22d0 Compare November 26, 2026 08:42
@veyn-sudo

Copy link
Copy Markdown
Author

@KDenKC thanks for both.

The review point was right: record_deferred resets each segment with the PC Reset directly, not through cl_reset, so nothing called shader_code::bound for it and the references stayed with the thread. Fixed in 0fd22d04: shader_code::bound(seg) after the dispatch ties them to the segment's allocator. The new test a_deferred_dispatch_keeps_its_pipeline_with_its_segment fails without it ("held by the segment's recording") and passes with it. The head is rebased onto main b49be183; the suite and both smoke runs are in the description.

Your budget runs match what I measured on a 2070 SUPER: in the world the code in use is the working set (~435 MB at the removal, all of it used within the last 2 s), so releasing what is idle cannot keep it under the heap. This PR stays useful for idle code, but on its own it does not prevent the removal at world start.

AI use: Claude Code (Claude Opus 5.5) checked the path, wrote the fix and the test, ran them on my PC and wrote this.

@CryingLaughing

Copy link
Copy Markdown

Focused RTX 4080 readback on exact head 0fd22d04dc7931066a8560fe178119a2040d0da9 with a local test-only overlay on pso_async::used_tests::an_unused_compute_pipeline_is_released_and_built_again.

The first one-group compute dispatch wrote 0x13579bdf to a UAV. After allocator reset released the recording reference, idle reclamation cleared the real PSO. The next dispatch rebuilt it (build count 1 → 2) and wrote 0x89abcdef to the same UAV. Both values were read back after their submission fences. Different values verify that the command recorded after the rebuild ran on the GPU.

Tested

  • RTX 4080 driver 621.01, i5-13600K, Windows 11 25H2 build 26200.9974, Rust 1.100.0 x64 MSVC, DXC 1.8.2502.11. XCL_ADAPTER=4080. Test overlay diff SHA-256 ddf8fee788507a89d8481bbe53638f9bd5d042d9270bfe40a193dcd02e1ebfcb.
  • cargo test -p xcl --lib --locked -j 4 --no-run --message-format=json, then the test executable with hle::d3d12x::pso_async::used_tests::an_unused_compute_pipeline_is_released_and_built_again --exact --test-threads=1 --nocapture. 1 passed, 0 failed, 0 ignored, 0 capability skips.

The synthetic test forces idle reclamation on a small pipeline. Retail workloads, driver shader-code capacity, and the workspace suite with this overlay remain untested.

@KDenKC

KDenKC commented Nov 26, 2026

Copy link
Copy Markdown

Checked at 0fd22d04: record_deferred now calls shader_code::bound(seg) after the dispatch (list_x.rs:2721), so the references go with the segment's allocator. Thanks @veyn-sudo.

AI use: Claude Code (Claude Opus 5.5) checked the source at that head and wrote this.

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.

7 participants