Skip to content

D3D12.X: a freed allocator's recordings leave before its address is reused - #2919

Open
b3IKK wants to merge 15 commits into
xcl-dev:mainfrom
b3IKK:review/free-aside
Open

b3IKK wants to merge 15 commits into
xcl-dev:mainfrom
b3IKK:review/free-aside

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Stacked on #2404 (5b9d4ff).

alloc_pool::title_reset puts a PC allocator aside and computes the free backings under STATE, then drops their dispatch_chain::RECORDINGS after unlocking. A backing reused in that window could have its new recording swept away, or keep the old recording's readbacks/COM releases alive; KDenKC's trace and the new test isolate the interleaving (#2404).

This takes each free backing's RECORDINGS entries out while STATE is held (dispatch_chain::take_recordings) and drops them after the unlock, so a backing reused in between keeps the recordings made after its reuse. The lock order stays STATE then RECORDINGS.

What is in it

  • crates/xcl/src/hle/d3d12x/alloc_pool.rs: title_reset_with/take_recordings; the free set is detached under the lock.
  • crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs: take_recordings.
  • test aside_sweep_keeps_recordings_made_after_backing_reuse (pauses the sweep after the unlock, reuses the backing and records on it, then finishes the sweep; fails on 5b9d4ff, passes here).

Measured on the retail title (RTX 5080, 2 GiB shader heap, heap windows, the stack of #2404 + #2519 + #684 + #2877):

without with
presentation 502.2 s 599.2 s
end device removed at 520.1 s exit 0x0, 0 removals

Two 600 s runs with it, both 0 removals and 0 crashes. Royalflamejlh reports 0/2 device losses with it against 2/2 without on Linux + NVIDIA.

Tested

  • hardware: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 1448 passed, 0 failed, 47 ignored
  • the new test alone: 1 passed; KDenKC reports it fails on 5b9d4ff (the earlier sweep erased the reused backing's new recording)
  • SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware, no heap windows, and cannot disable the driver cache (Windows Developer Mode is required)
  • the two 600 s runs above

Regression run

main (426a5a7, clean tree, no local changes):

xcl-smoke v1
commit: 426a5a745db4d5e4b037449ede0b70cb342f5192
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1642 (PresentX 1642)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15158 MB

PR (2b32e81, clean tree, no local changes):

xcl-smoke v1
commit: 2b32e81824e82d21d3754b14804a3a797e3b1905
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1596 (PresentX 1596)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15595 MB

(The main block is 426a5a7, an ancestor of current main, from the same machine and data folder. A fresh launch on 8b957be is blocked on this machine by #2555's startup preparation, reported on that PR.)

Not tested

  • AMD GPUs, other titles; Linux / Wine beyond Royalflamejlh's report.

AI use

Kalmarv and others added 15 commits November 25, 2026 19:49
An ExecuteIndirect of a compute program that the recompiler split into
pieces fell back to the program's whole form, and that form was compiled
on the recording thread when first needed. On an RTX 5070 Ti with
XCL_DIRECT_DXIL=1 a retail title stalls its world load on it: 11
per-material programs, 9-14 pieces each, each dispatched through
ExecuteIndirect, each whole form 690-950 KB of DXIL taking 16-40 s to
compile, one after another from 326 s to past 600 s.

The title's ExecuteIndirects of split programs all have one command
signature: one Dispatch, stride 12, MaxCommandCount 1, no count buffer
(150 of them in 360 s). Their group counts are written on the GPU, so
the CPU cannot slice them when recording.

The ExecuteIndirect now runs the chain on the GPU. A small compute pass
(pso::record_slicer) reads the title's arguments (and count) and writes,
for a fixed number of rounds, one command per piece of each slab of
dispatch_slabs (the root constants of record_dispatch_chain, then the
slab's group counts); unused rounds get empty dispatches. Each piece then
runs one ExecuteIndirect per round with the chain's command signature,
with UAV barriers between them, in the bounded scratch.

The rounds come from the arguments the CPU sees now and the most slabs an
earlier ExecuteIndirect of the program had, plus a quarter (2 to
XCL_SPLIT_INDIRECT_ROUNDS, 16). When the arguments need more slabs and
the whole form is built already, the slicer leaves the rounds empty and
writes the title's Dispatch arguments after them, and the whole form runs
on those through a command signature of one Dispatch; otherwise it leaves
those empty. So one of the two runs, without predication: under one
predicate, two NVIDIA Ampere cards (RTX 3080 Ti, RTX 3070) ran some of
the rounds and not the whole form (xcl-dev#2404). The whole form's
ExecuteIndirect is recorded first, and the rounds' UAV barriers follow
it: on the RTX 3080 Ti it ran right after the slicer alone but not
after the pieces' ExecuteIndirects. If the whole form is not
built, the rounds run, the lost slabs are counted and warned about once
per program, the whole form is built on another thread for the next
ones, and the next rounds are sized from the count. The title's own
predication holds for the slicer too, so the same choice is made under
it. Bundles, lists without scratch and
other command signatures keep the whole form. The 'compute split' line
counts chained, whole and lost ExecuteIndirects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
runner::dispatch_split_indirect records a chain's ExecuteIndirect the way
the runtime does: the slicer, the whole form on the slicer's arguments
when there is one, then the rounds' ExecuteIndirects.
dispatch_chain_divergent_forward_edges_3d runs it with one command,
several with empty commands and a count buffer that leaves the last out,
and a stride of 20 bytes, in slabs of 1 to 12 groups and 1 to 16 rounds,
each without and with a whole form. It checks the memory against the
whole program and the slicer's slab count. With more slabs than rounds
it checks that the slicer says so and that the whole form runs instead
(the memory would not match if the rounds had run too). Without a whole
form it checks that only the rounds' groups run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s whole form

A chained ExecuteIndirect whose arguments have more slabs than its
rounds runs the program's whole form instead, but only when that form is
built already. Until then the slabs past the rounds are lost, counted and
warned about. Across ten title runs of the earlier version, 158 slabs were
lost in six, in 73,714 chained ExecuteIndirects. In each run it was one
program with 17 slabs: 15 at its first ExecuteIndirect, whose arguments
read 0 when it was recorded and which so got 2 rounds, then 1 at each
later one (at most 16 rounds) until its whole form was built.

- An ExecuteIndirect of a program none of whose ExecuteIndirects has been
  read back yet gets the most rounds. After that, the most slabs any of
  them had (not only those that lost some) size the rounds. This costs
  the empty rounds of a program's first ExecuteIndirects: one
  ExecuteIndirect and one UAV barrier per piece and round. The scratch
  does not grow with the rounds; the command buffer does, by 44 bytes a
  piece and round.
- XCL_SPLIT_INDIRECT_ROUNDS defaults to 32, not 16. Rounds are sized from
  the slabs read back, so the cap matters only for programs that need
  more. With both, none of those runs would have lost a slab.
- The whole form is asked for at the program's first chained
  ExecuteIndirect, not after a loss is read back. It is built on an
  xcl-pso-chain-whole thread, so a huge whole form waits for its turn
  (driver_compile) as the workers' builds do. Before, it was built on a
  thread named xcl-chain-whole, which the huge-build gate did not hold
  (pvfreis, xcl-dev#2404). It is bound only when an ExecuteIndirect has more
  slabs than its rounds.

More than 32 slabs before the whole form is built are still lost, counted
and warned about. Waiting for the whole form when the ExecuteIndirect is
recorded would make it exact. But the precompile does not build the whole
forms of split programs, so that wait is a cold compile on the title's
thread: the 16-40 s stalls this chain removes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ot read back

The slicer's words after the rounds size the next ExecuteIndirects of
the program: the most slabs one had, and whether any had been measured
at all. Under the title's predication (SetPredicationBufferX) the slicer's
Dispatch may be skipped, and its words then stay as the buffer was
created, all 0. read_back still took them as a count: the program counted
as measured with 0 slabs, so its next ExecuteIndirect whose arguments read
0 on the CPU got 2 rounds instead of the 32 of one never measured.

A chained ExecuteIndirect recorded while the title's predication is set
is no longer read back, as one whose binding failed. Its slicer still
runs the same choice under the predicate.

Reported by @thehollowedone (xcl-dev#2404).

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

a_chained_execute_indirect_keeps_its_commands_when_binding_fails records
two chained ExecuteIndirects whose binding succeeds, but its bind closure
did not set the chain's root signature, as bind_abi does. The pieces'
ExecuteIndirects then ran under the slicer's root signature: with the D3D12
debug layer and GPU-based validation, 258 messages (953, 744 and 712). The
test still passed; the runtime's path sets the root signature.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Indirect sizes the rounds

A chained ExecuteIndirect records rounds x pieces ExecuteIndirects, each
after a UAV barrier, also when the slicer leaves them empty. On an RTX
5070 Ti (NVIDIA 610.74) such a step takes 14.4 us of GPU time with the
chain's command signature (eight root constants and a Dispatch): 144 us a
round for 10 pieces, 2.3 ms for 16 rounds, 4.6 ms for 32, empty or not.
Without the barrier it is 0.17 us, and with a Dispatch-only signature
0.41 us; a count buffer of 0 makes it 11.5 us. The rounds came from the
most slabs any ExecuteIndirect of the program had, so one large one kept
every later one at that size.

Until the whole form is built that is needed, since the slabs past the
rounds are lost. Once it is built, an ExecuteIndirect with more slabs than
its rounds runs the whole form, so the rounds now follow the last one
read back (and the arguments seen when recording), not the most.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The chain's root signature indexes the descriptor heaps directly, so setting it on a list
without heaps is a debug-layer error (1419, four of them under GPU-based validation at
809da30). A title's list has its heaps set when XCL binds the ABI; the test's `bind` now sets
two shader-visible heaps before the root signature, as that list has.

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

With the allocator pool on, `execute_indirect` kept the recording with the
PC allocator the list records on (`alloc_pool::recording_backing`). For a
list the pool does not track it got 0, so it returned Ok(false) and the
ExecuteIndirect ran its whole form. XCL makes such lists itself: a mid-list
split's next list is created on `alloc_pool::current(alloc)` with no
`created_on`. Under Wine + vkd3d-proton on an RTX 4060 Ti, this branch
chained 2 ExecuteIndirects and ran 26-30 whole in three runs of a title,
and the title presented nothing after its loading burst. In a fourth run
with a log line, all 32 that declined did so for this reason.

Such a list is now taken on by the pool, on the PC allocator the title's
allocator stands for, which is the one the list was made on
(`alloc_pool::recording_backing_or_take_on`), and the recording is kept
there. xcl-dev#2333 keeps these recordings with the title's allocator instead
(its `resolve_allocator`). The pool then knows nothing of the list, so it
can take that allocator for free and release the recordings (scratch and
commands buffer) while the list still records or runs on the GPU. Taken
on, the list keeps its backing busy until it is reset elsewhere or
released, and its submits mark the backing like any other list's.

The title's allocator also gets the list's type, so that a Reset while
the list holds its backing swaps it like the title's own lists.

Reported by @Royalflamejlh (xcl-dev#2404). With xcl-dev#2333 merged under this branch
and its fallback in `execute_indirect`, every ExecuteIndirect chained in
four runs (22 to 70,341 of them, 0 whole).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The chains of one recording share one scratch, which `dispatch_chain`
finds by the list's address (`SHARED`). Only a list's Reset forgot it.
Addresses are reused (xcl-dev#2588). A list created at the address of a
released one, on the same allocator while the old recordings were still
held, found the old list's scratch and shared it with work the GPU may
still be running. A chained ExecuteIndirect takes its scratch the same
way. `list_x` now forgets the entry when a list is created and when it
is released, with a unit test that fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e free

A chained ExecuteIndirect keeps its slicer scratch and commands buffer
with the recording of its PC allocator until that allocator is reset
(dispatch_chain::allocator_reset, from reset_pc_allocator) or released.
When the title resets an allocator whose recording is still held or on
the GPU, alloc_pool puts the PC allocator aside and gives the title
another. The one put aside is reset only when the same title allocator
is reset again and reuses it, which may never happen. Until then its
recordings, and the memory they keep, stay.

dhillonjagdeep650-boop found it on an RTX 3080 Ti (xcl-dev#2404): 1,887
recordings under 48 allocators at 110 s, local and non-local memory
growing about 150 MB/s, then a device removal at 110.8 s. The allocator
log said 119 made and 119 put aside.

Every title Reset now also releases the recordings of each PC allocator
put aside that nothing holds any more: no list records on it and its
submits are done. This happens outside the pool's lock.
allocator_reset of an allocator without recordings does nothing, and
the allocator is reset as before when it is reused.

This relies on every chained recording being filed with the PC
allocator its list records on, and on that list being known to the pool
(an earlier commit here takes on the lists it had not seen). A recording
filed with a title's allocator, as xcl-dev#2333's lines file one, would go when
that allocator is free, while its list still records or runs on the GPU
elsewhere.

The first test fails without the sweep: the free allocator put aside still
holds its recordings after another title allocator's Reset. Two more
show that a taken-on list's recording stays while the list records on
its allocator put aside and while its submit runs, and goes after both;
and that a free title allocator put aside does not take a live list's
recording with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…'s turn

`want_whole` builds a split program's whole form on `xcl-pso-chain-whole`, an `xcl-pso*` thread,
so a huge one waits in `big_compile_turn` for a background slot and 3 GB of free commit, with no
timeout. It waits holding `Chain::build_whole`, and nothing on that thread says a title thread
waits for it (`pso_async::build_is_wanted` reads only a worker's build). So a title thread that
needs the same whole form meanwhile (`bind.rs` for an ExecuteIndirect it cannot chain,
`dispatch` without scratch) blocked on the mutex until the background turn came. It got neither
the wanted cap nor the commit skip of a build the title waits for, where before `want_whole` it
compiled at once (found by @pvfreis in the review of xcl-dev#2404).

A title thread now counts itself in `Chain::waiting` while it waits for the whole form, and
`want_whole`'s build runs in `pso_async::build_waited_on`, so `build_is_wanted` is true on that
thread while one does. The background build then goes as one the title waits for, and the title
thread gets the form when it is built. The test fills the background slots, starts the build and
asks for the form on another thread: without the count it waited until the slots were freed
("the title thread waited for the background build's turn").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A chained dispatch or ExecuteIndirect keeps its scratch and commands until
the PC allocator it recorded on is reset or, put aside, is free. Two runs on
other machines held thousands of them (7,410 under 115 allocators at 270 s,
2,297 of them under ones put aside) while memory grew, and neither log says
why the rest were not let go.

Every 10 s, with the counter line, a second line gives the recordings held
and made, and splits the held ones by their allocator: put aside with a list
still recording on it, with submits on the GPU, or free; in use for a
title's allocator, busy or free; or one the pool does not know.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d each

`want_whole` started a thread for each split program whose whole form it
asked for. A huge whole form on it waits for its turn (`driver_compile`),
so the threads piled up: a run on Linux + NVIDIA had 200 of them at 280 s,
199 waiting.

The whole forms now queue on one thread for each slot set of
`driver_compile` (small, huge, giant), which builds them one after another
and ends when its queue is empty. Small ones are never gated, and at the
default of one huge compile at a time (`XCL_HUGE_PSO_CONCURRENCY`) huge or
giant ones took their turns one by one already. A title thread that needs a
whole form still builds it itself. A program released before its turn is
not built.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ole form is built

Until a split program's whole form is built, slabs past a chained
ExecuteIndirect's rounds are lost. The rounds were sized by the most slabs
one of its ExecuteIndirects had read back, so the 32 of 9125edb held only
until the first read-back: a program whose first ones had a slab got 2
rounds, and a later one with 22 slabs lost 20 (two runs on Linux + NVIDIA).

Before the whole form is built a chained ExecuteIndirect now records the most
rounds (`XCL_SPLIT_INDIRECT_ROUNDS`, 32) whatever was read back; once it is
built, the last one read back sizes them as before. The whole form is asked
for at the program's first chained ExecuteIndirect, so the empty rounds
last until it is built.

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

title_reset() free_aside()s a PC allocator under STATE, unlocks, and only then drops
its RECORDINGS entries. A backing reused in that window could have its new recording
swept away, or keep the old recording's readbacks/COM releases alive (xcl-dev#2404).

The free backings' recordings are taken out while STATE is held and dropped after the
unlock; a backing reused in between keeps the recordings made after its reuse.

Patch from KDenKC's tested diff in xcl-dev#2404. Test:
aside_sweep_keeps_recordings_made_after_backing_reuse fails on 5b9d4ff, passes here.
@DarkPanda69

Copy link
Copy Markdown

AMD run (listed as untested), with an RTX 3060 in the same PC: the 13 new d3d12x tests pass on both, and nothing else changes from main.

Tested: pass, head 2b32e81 applied on main 8b957be, debug, Rust 1.100.0, Windows 11 Pro 26200, Ryzen 7 5800X, DXC 1.9.2602.17, card picked with XCL_ADAPTER; main and this PR one after the other on each card.

  • cargo test -p xcl --lib -- hle::d3d12x:: --test-threads=4 --nocapture:
    • AMD RX 9070 XT (RDNA4), driver 32.0.32029.2010: main 260 passed, 0 failed; this PR 273 passed, 0 failed, 2 ignored, 0 SKIPPED.
    • NVIDIA RTX 3060, driver 32.0.16.1088: the same, 260 → 273 passed, 0 failed, 0 SKIPPED.
    • The only result changes on both cards are the 13 new tests, all ok (from aside_sweep_keeps_recordings_made_after_backing_reuse to a_title_thread_does_not_wait_behind_a_background_whole_build_waiting_for_its_turn).
  • cargo test -p xcl --lib -- gpu:: --test-threads=4 --nocapture (XCL_DIRECT_DXIL unset): RX 9070 XT 708 passed, 2 failed (the DXIL fuzz tests: fuzz_validate_and_gpu_1 fails on AMD (seed 722 and others, RDNA2-4), so the default suite is red there #2115 fuzz seeds, same on main), 6 SKIPPED, on both main and this PR; RTX 3060 710 passed, 0 failed, 24 SKIPPED on both. No result changes.

The diff adds test code that makes its device with D3D12CreateDevice(None, ..), so on the RTX 3060 run those still ran on the first adapter, the RX 9070 XT.

Not tested: the whole workspace suite, a title run, release, Intel, Linux / Wine.

AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026

@luckyteagirl luckyteagirl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the new regression call a helper that bypasses the production environment gate, or explicitly assert that its reuse callback ran? With XCL_ALLOC_SWAP=0, title_reset_with returns None before calling after_unlock. The test already called note(spare), so holds(spare) still reports true for the old entry. Consequently both final assertions pass even though the sweep and reuse never ran. Checking callback execution would prevent this false pass. Moving the env gate to title_reset would also let the test exercise the same sweep regardless of shell settings.

Environment gate and sweep · Regression

AI-assisted review (GPT-6.2 Sol).

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