Skip to content

D3D12.X: XCL_ALLOC_TRIM_MB releases grown PC command allocators - #3612

Draft
ArtuArtaza wants to merge 2 commits into
xcl-dev:mainfrom
ArtuArtaza:perf/replace-grown-allocators
Draft

ArtuArtaza wants to merge 2 commits into
xcl-dev:mainfrom
ArtuArtaza:perf/replace-grown-allocators

Conversation

@ArtuArtaza

Copy link
Copy Markdown

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

amdxc64 keeps a command allocator's memory over its Reset, so each PC allocator the pool keeps stays as large as the largest recording it ever held. On an RX 6700 XT, in the retail title's world, that memory (system RAM, Windows' "shared GPU memory") grew by about 60 MB a frame until the device was removed at 5.6–5.8 GB. With XCL_ALLOC_TRIM_MB=<n>, once the process holds more than n MB of non-local video memory besides the coherent windows, idle PC allocators are released and a title Reset goes on with a new one. It is off by default because of its cost (below).

Credit: @iggyvilla found this leak (#3110: amdxc64 keeps memory per command list and per allocator), and @Simon-CR's #2138 (open) releases idle put-aside allocators. This builds on the reset of idle ones that is in main (31930d8 by @bmjask; @Saksukella's #2707 proposed the same). New here: on this driver a Reset does not give the memory back, only Release does, so the allocator in use is replaced too.

What is in it

  • crates/xcl/src/hle/d3d12x/alloc_pool.rs: the switch (trim_limit, trimming: DXGI is asked at most every 250 ms); take_idle_spares / release_idle_spares release the put-aside PC allocators nothing uses, never the title's own or the one in use; in title_reset a free allocator that was recorded on is replaced by a new one (ResetWork::Create { replace }). Unit tests of the switch, of which spares go, and of the replacement's bookkeeping.
  • README.md: the switch.

Tested

  • AMD Radeon RX 6700 XT 12 GB (driver 32.0.21045.29003), Ryzen 5 5600X, Windows 11 26200, rustc 1.100.0, debug
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on ffc43a2: 2903 passed, 6 failed, 51 ignored. The 6 are 5 tests (the two-pass cache e2e test runs itself as a child and prints two results), the same 5 that fail on main 6bfb5c5 on this card (2899 passed, 6 failed, 51 ignored): gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form, f32_programs_on_the_gpu, gpu_f32_compares_with_nan, gpu_float_compares_random, two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs. None is in the code this PR touches.
  • SKIPPED 4: no heap windows (2), no GPU_UPLOAD heaps (1), cannot disable the driver cache without Developer Mode (1).
  • cargo test -p xcl --lib alloc_pool -- --test-threads=4, 30 times: no run with a failure.
  • The driver, with plain D3D12 calls: 20 allocators with 100,000 copies each held 60.6 MB of non-local memory, as much after Reset, and 0.6 MB a few seconds after Release.
  • The retail title's world (Series S profile, XCL_TEXTURE_LEVEL_PAGES=1, no prepared shaders), limit 1024 MB, 3 runs each, 60 s after entering the world, mean (lowest–highest):
without with
commit 29.1 (28.2–29.8) GB 26.7 (25.7–27.3) GB
working set 20.3 (19.9–20.7) GB 17.9 (17.1–18.6) GB
shared GPU memory (Windows) 8.8 (8.3–9.3) GB 6.5 (6.4–6.6) GB
dedicated VRAM (Windows) 10.4 (10.2–10.6) GB 10.5 (9.8–10.8) GB
growth of that memory per frame 59 / 63 / 62 MB 25 / 19 / 25 MB
  • In all world runs of these builds the device was removed (0x887A0020) in 7 of 9 without it, 70–157 s after entering the world, and in 0 of 20 with it. Of those 20, 13 ran their 5 minutes and 7 ended in a driver crash while it compiled a shader (amdxc64.dll+0x790849, a separate problem).
  • The cost, with plain D3D12 calls on this driver: a recording on a new allocator took 1.1–1.2 ms, the same recording after a Reset 1 µs (empty) to 83 µs (4,000 commands). The world went through 70–78 allocators a frame while trimming, so about 80 ms of CPU a frame. Those runs showed 0.3–1 frames a second, where that is small; at 30 frames a second it would not fit.
  • The regression run below, repeated with XCL_ALLOC_TRIM_MB=1 (trimming from the start, on the title's first screens at 60 frames a second): 1466 frames shown, exit 0x0, no device removal, no crash, peak commit 12738 MB; 1024 idle PC allocators were released and 1239 resets went on with a new one in the 60 s.

Regression run

main:

xcl-smoke v1
commit: 6bfb5c5153ccc23517daf30aa1071ddb0fadb53c
gpu: AMD Radeon RX 6700 XT
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1505 (PresentX 1505)
presents per 2 s (last 3): 61 55 60
device removals: 0
crashes: 0
peak commit: 13154 MB

PR:

xcl-smoke v1
commit: ffc43a27997e06b2bfe1f3db530ab9e6010233c2
gpu: AMD Radeon RX 6700 XT
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1513 (PresentX 1513)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 12459 MB

Not tested

AI use

  • Claude Code with Claude Fable 5.2 (earlier parts of the session ran as Claude Opus 5.5) measured the driver and the game runs on my PC, and wrote the code, the tests and this description.

alloc_release dropped what XCL keeps by a command allocator's address
(the pool's namespace and type, the dispatch chain's and list_x's state)
after the Release that frees the object. In between, the driver can give
that address to an allocator another thread creates, and the cleanup then
ran on the new one: alloc_pool::forget took its type and stand-ins, and
marked one that already held a recording for release by the pool.

For an allocator nothing uses, the cleanup now runs while the hook still
holds its probe reference, before the Release that frees it. One the pool
keeps for the GPU, and a Release that did not see the last reference,
are cleaned up after it as before.

sage-sage's xcl-dev#2701 proposed this order for heaps, queues and allocators;
the allocator part is not on main.

Measured on main b2b1f10 (RX 6700 XT, driver 32.0.21045.29003) with a
GPU test that creates allocators between the Release and the cleanup
until one comes back at the freed address: it failed 5 of 5 runs there
and passed 5 of 5 with this order. That test needs a hook in
alloc_release and is not part of this commit.

Assisted-by: Claude Code (Claude Fable 5.2)
amdxc64 keeps a command allocator's memory over its Reset: on an
RX 6700 XT (driver 32.0.21045.29003) 20 allocators with 100,000 copies
each held 60.6 MB of non-local memory, as much after their Reset, and
0.6 MB a few seconds after their Release. The pool resets and keeps its
PC allocators, so each stays as large as the largest recording it ever
held. In a retail title's world that memory grew by about 60 MB a frame
until the device was removed at 5.6-5.8 GB.

With XCL_ALLOC_TRIM_MB=<n>, once the process holds more than n MB of
non-local memory besides the coherent windows, the idle PC allocators
put aside are released instead of kept, and the title's Reset of an
allocator nothing uses goes on with a new PC allocator when the one in
use was recorded on.

Off by default: the first recording on a new allocator took 1.1 ms on
that driver, a recording after a Reset about 1 us, and the title's world
went through about 75 new ones a frame.

Measured with the limit at 1024 MB on main d0832e6 + xcl-dev#3455 + xcl-dev#3473
(this card needs both to reach the world), 3 runs each, 60 s into the
world: commit 29.1 -> 26.7 GB, working set 20.3 -> 17.9 GB, shared GPU
memory 8.8 -> 6.5 GB, growth 59-63 -> 19-25 MB a frame. The device was
removed in 7 of 9 world runs without it and in none of 20 with it.

iggyvilla found the leak (xcl-dev#3110: amdxc64 keeps memory per command list
and per allocator), and Simon-CR's xcl-dev#2138 releases idle put-aside
allocators. This builds on the reset of idle ones that is in main
(31930d8, bmjask; Saksukella's xcl-dev#2707 proposed the same).

Assisted-by: Claude Code (Claude Fable 5.2)
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:docs-ci Documentation, scripts, workflows, repo governance area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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

  • A trimmed Reset can fail where a plain Reset would succeed. In title_reset (alloc_pool.rs ~1568-1572), a free, grown allocator now always goes to ResetWork::Create { replace: true } while trimming. If GetDevice or CreateCommandAllocator then fails (~1642-1645), the title's Reset returns that error even though the allocator could have been reset in place as before. That only happens over the memory limit, which is when E_OUTOFMEMORY is most likely. Falling back to ResetWork::Reset { swap: false, .. } on creation failure would avoid it.
  • No test reaches the trim decision in title_reset. TRIM_IN_TEST (~926) is never set, so under cfg(test) trimming() (~944) always returns (false, false). The new branches in title_reset (the grown replacement ~1568, skipping aside reuse ~1591 and take_shared_aside ~1598) and the release_idle_spares calls are untested; the two new tests call take_idle_spares and commit_reset directly.

Fine: opt-in (unset costs one OnceLock read); the DXGI query runs at most every 250 ms; take_idle_spares skips owners with a reset in progress and revalidates each spare's probe under the lock; spares are released outside STATE in the same order as forget; the title's own allocator is never released; README documents the variable. Stacked on #3611; only the top commit was reviewed here.

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

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:docs-ci Documentation, scripts, workflows, repo governance area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants