Skip to content

D3D12.X: system-memory sparse pools (#2987) give back heaps whose tiles are all free - #3613

Open
throny wants to merge 2 commits into
xcl-dev:mainfrom
throny:sparse-sysmem-retire
Open

throny wants to merge 2 commits into
xcl-dev:mainfrom
throny:sparse-sysmem-retire

Conversation

@throny

@throny throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Carries #2987 (treybertram06's commit unchanged, rebased on main) and adds the system-pool retirement asked for in its review; supersedes #2987 if its author does not return.

The system-memory pools of XCL_SPARSE_SYSMEM=1 only grew: unlike the video pools they had no way to give back a heap whose tiles are all free, so the process kept its peak (about 3.8 GB in our world runs). Review point: #2987 (comment)

What is in it

  • D3D12.X: sparse tiles outside the GPU heaps can live in system memory - treybertram06's commit, unchanged (sparse.rs, kernelx.rs); default off.
  • D3D12.X: system-memory sparse pools give back heaps whose tiles are all free (crates/xcl/src/hle/d3d12x/sparse.rs): SysPools::retire releases a heap whose 64 tiles are all free or cooled (COOL), keeps it in a retired list until the sparse queue's fence is past its mark (as State::retired does), and a retired heap is taken back before a new one is made. Heap slots are Option so the indexes in live tile handles never move; the next heap takes a free slot. State::retire_free_pools_with calls it, and the submit path's retire_due also looks at the system pools' cooling queue. Test: a_system_memory_heap_with_all_tiles_free_is_given_back (checks bytes() drops, live heap keeps its index, retired heap waits for the fence, a later heap reuses the slot); it fails with the retirement disabled.

Tested

  • Hardware: NVIDIA GeForce RTX 4080 16 GB (driver 621.40), Intel i9-12900K, Windows 11 26300, stable-x86_64-pc-windows-msvc, debug.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4 on head a6e50f1b (same tree as the head 24a84ca, only the commit message changed) (no other GPU work running): 2908 passed, 0 failed, 51 ignored (sum over the 43 test binaries; the lib: 2760 passed, 0 failed, 31 ignored).
  • SKIPPED lines in the log: 25 (gpu, hle, shaders and heap_window_out_of_memory_falls_back_to_the_switch_form); not counted as coverage.
  • The new test also ran alone before the full suite and failed with retire disabled (panic at the first bytes() assertion after retirement).

Regression run

  • main:

xcl-smoke v1
commit: 6bfb5c5
gpu: NVIDIA GeForce RTX 4080
title: 21222967
seconds: 120
exit: 0x0
frames shown: 3425 (PresentX 3425)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 23525 MB

  • this PR's head:

xcl-smoke v1
commit: 24a84ca
gpu: NVIDIA GeForce RTX 4080
title: 21222967
seconds: 120
exit: 0x0
frames shown: 3404 (PresentX 3404)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 19704 MB

Not tested

  • Whether the retirement lowers the title's peak in a world run (not measured here; the unit test covers the logic with local heaps, not the GPU mapping path).
  • AMD / NVIDIA specifics of dropping an L0 custom heap while the sparse queue still has work against it: relies on the fence mark, the same as the video pools.

AI use

  • Claude Code (Claude Sonnet 5.5) wrote the retirement and its test; the first commit is treybertram06's.

🤖 Generated with Claude Code

treybertram06 and others added 2 commits December 1, 2026 19:41
With XCL_COHERENT_HEAPS=sparse the title's GPU heaps (committed memory with
a PAGE_GRAPHICS_* protection, ~4.5 GB in a retail title) live in the sparse
windows. Those are the data the title's shaders read and write every frame;
in the coherent window (system memory, read over PCIe) one retail compute
program took 1,364 ms a frame on an RTX 3080, under 33 ms in video memory.
With them in video memory the windows hold ~7.5 GB of tiles and the device
ran out of its budget (19 GB in use against 8.9 GB, removed at 659 s).

XCL_SPARSE_SYSMEM=1 (off by default): kernelx registers a GPU heap's range
as hot before its commit maps its tiles (sparse::alloc_as); every other
sparse-window tile takes a pool tile in system-memory pool heaps (custom
heaps, L0, write-back), which the GPU reads over PCIe. A pool tile of those
heaps has SYS in its pool index; freed ones cool in their own lists, kept by
the heap's number without SYS (the free table is indexed by heap number).

RTX 3080, retail title, 400 s of its world after the menus: heaps in video
memory alone 0.98 presents/s and 19 GB of video memory; with this and the
texture-level counterpart 1.70 presents/s, 12.7 GB, no device removal in
900 s. An RTX 4080 report on xcl-dev#2688 found the same with a port of it.

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

The system-memory pools of XCL_SPARSE_SYSMEM only grew: unlike the video
pools they had no retirement path, so the process kept its peak (about
3.8 GB in our world runs). Review point on xcl-dev#2987:
xcl-dev#2987 (comment)

A heap whose 64 tiles are all free or cooled (COOL) is released, and kept
alive in a retired list until the sparse queue's fence is past the value it
got, as State::retired does for video memory (the tiles' unmaps were already
quarantined behind the fences before they cooled). Heap slots become
Option so the indexes encoded in tile handles of live heaps do not move; the
next heap takes the free slot, and a retired heap is taken back before a new
one is created (xcl-dev#913). State::retire_free_pools_with runs it, and the submit
path's retire_due also looks at the system pools' cooling queue.

Test: a_system_memory_heap_with_all_tiles_free_is_given_back fails with
retirement disabled and passes with it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: 6bfb5c5
head: 24a84ca
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2908 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (+354/-12). The new commit makes the system-memory pools retire heaps the same way the video pools already do, and I found no problem in when a heap is released, but two open points from #2987's review are still unfixed and the new tests skip part of the path. Findings: (1) The mixed hot/cold skip from #2987 is still there. In materialize (sparse.rs ~3978), wait = left > 0 || … means that once one tile in a GPU heap finds no video memory, every later tile in the same call is skipped, including cold tiles that would have got system memory. The carried commit is byte-identical to #2987's head 15b74314 (range-diff =), so the regression reported on that head still applies; nobody answered it. (2) A refused system-memory heap is retried for every cold tile. When CreateHeap fails in take_sys_tile (~3637), it logs, falls back to take_tile and remembers nothing, so the next cold tile asks for a new 64 MiB heap again. This was raised in two #2987 merge-checks (one run showed 15 failed creations in a row), and retirement can make heap creation more frequent. (3) The test leaves out part of the path. a_system_memory_heap_with_all_tiles_free_is_given_back (~11645) calls SysPools directly. It covers the negative cases (a heap with one live tile is kept, a heap still cooling is kept, a heap waits for the fence), but not take_sys_tile taking back a retired heap through retired.pop() (~3624), the call from retire_free_pools_with (~3747), or the sys_waiting term added to retire_due on the submit path (~7953), so broken wiring would still pass. (4) A run can't show that retirement works. sys_pool_bytes() (~583) has no caller, and neither pool_bytes nor memory_bytes_for counts the system-memory heaps. Retirement writes no log line, and bytes() drops as soon as a heap is retired although the heap stays allocated in retired until the fence passes and 2 s have gone by. The growth log (~3647) now counts only live heaps, so it prints again each time the count climbs back to a power of two. A world run therefore can't confirm the 3.8 GB this PR is meant to give back (the description says so under Not tested). (5) SYS_POOLS outlives a replaced state (older than this PR). init swaps STATE (~3554) but leaves the global system-memory pools in place, so tiles the old state had mapped never come back and their heaps are never retired; only matters if init runs twice (unverified that any title does). Fine: a heap is retired only when all 64 of its tiles are free or have cooled for the full COOL time; tiles reach cooling only through quarantine (behind the sparse queue's and the title queues' fences) or when a placed buffer is released, so parked, kept, pinned and quarantined tiles keep their heap. A retired heap stays in retired until the sparse queue's fence reaches self.value and 2 s have passed, and is taken back before a new heap is created, as in the video path. Live heap indexes never move, and FreePools::clear_heap runs before a slot is reused. retire, take_sys_tile, cool and pool_heap all run with the STATE lock held and take SYS_POOLS inside it in the same order, so a map into the same heap can't race a retirement. With the switch off, SYS_POOLS stays None and take_tile_for goes to take_tile; the only additions on the default path are the HOT note/forget once per GPU heap and one uncontended lock per submit and per retire. The kernelx.rs change matches sparse::free.
performance: 60 s smoke of a retail title, main 6bfb5c5 vs this merge (24a84ca): 1646 -> 1670 frames shown (101 %; last presents per 2 s: 60 60 61), peak commit 18685 -> 18803 MB, 0 device removals, 0 crashes. With XCL_SPARSE_SYSMEM=1: 1663 frames, peak commit 18343 MB, 0 device removals, 0 crashes; the system-memory pools were used (up to 512 MB of heaps in the log), but no retirement shows in a 60 s run and nothing logs one (finding 4).
verdict: not yet: two points from #2987's review are still open on the carried commit (finding 1, cold tiles skipped after one tile finds no video memory, and finding 2, a refused system-memory heap retried for every cold tile), and nothing yet shows the 3.8 GB coming back (finding 4). Nothing breaks: the suite passes and both smoke runs, with the switch off and on, are clean
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@vasanex

vasanex commented Dec 2, 2026

Copy link
Copy Markdown

merge-check claim
head: 24a84ca

@vasanex

vasanex commented Dec 2, 2026

Copy link
Copy Markdown

merge-check
base: 1c8b551
head: 24a84ca
merge: clean (merged tree 7cbdd9c3, git diff --check clean)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> two runs on the merge: 2912 passed, 1 failed, 51 ignored, then 2913 passed, 0 failed, 51 ignored; SKIPPED: 25 in both (22 no wave64, 2 no heap windows, 1 cannot disable the driver cache), the same set as on the base; base alone, same machine: 2909 passed, 0 failed, 51 ignored. Failing in the first run only: two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (tests/gpu_two_pass_cache_e2e.rs:584, "releasing the title PSOs releases their two-pass state", left 1, right 0, in its warm case). That is the assertion of the open issue #3441. Alone with --exact --test-threads=1: on the merge 9 of 11 passed (the 2 failures came right after the suite run, the next 8 runs passed), on the base 6 of 6. I do not see how this PR reaches that test (it does not set XCL_SPARSE_SYSMEM, and the change touches no pipeline or late-release code), and the second whole run is clean, so I count it as #3441's flake, not as this PR's; the numbers are here so that others can judge. The PR's four new tests pass in both runs (a_system_memory_heap_with_all_tiles_free_is_given_back, a_lazy_tile_outside_the_gpu_heaps_gets_system_memory, a_parked_system_memory_tile_gives_way_to_a_gpu_heap, the_title_gpu_heaps_are_hot_and_nothing_around_them); the two GPU ones did not skip here, so the L0 custom heap was made and mapped on this card.
review: whole diff read (sparse.rs, kernelx.rs), in the merge result. No blocking finding. Checked: SYS (bit 31 of the pool index) against the LAZY sentinel (all bits set): every path that reaches cool, pool_heap or the quarantine count already leaves LAZY out, as on main. All frees of a pool tile go through State::cool, so no SYS index reaches the video pools' FreePools table (the second GPU test asserts that). Lock order is always the sparse state, then SYS_POOLS / HOT; take_sys_tile drops SYS_POOLS before it falls back to take_tile. A heap slot given back is None and reused by add_heap, so indexes in live tile handles stay valid; a retired heap waits for the sparse queue's fence and 2 s, as the video pools do. Smaller points: (1) XCL_SPARSE_SYSMEM has no line in README.md (the switch comes from #2987's commit); (2) the retirement test uses plain default heaps and no tile mapping, as the description says, so giving back an L0 heap that was mapped is covered only by reasoning (the fence mark); (3) sys_pool_bytes() has no caller, so the memory lines of the log do not show these heaps yet; (4) I did not repeat the "fails with retire disabled" run. Duplicates: it carries #2987 (open, same first commit; #1607 group D262 with #3112) and adds one commit; nothing else found for XCL_SPARSE_SYSMEM.
performance: off by default. With the switch off the cost is one OnceLock read per tile taken, one uncontended SYS_POOLS lock per submit (retire_waiting) and per pool retirement, and a bit test per freed tile: reasoned, not measured. No title run in this check; nothing here says what the switch does to frame time on a 10 GB card.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 3080 10 GB (driver 621.40), AMD Ryzen 9 7950X, Windows 11 Pro 26200, rustc 1.100.0, debug / Claude Code with Claude Fable 5.2. Run and posted without a human check of each point.

@github-actions github-actions Bot added checks-disagree Merge-checks disagree (safe vs not yet); needs maintainer/3rd look needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 2026
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:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA checks-disagree Merge-checks disagree (safe vs not yet); needs maintainer/3rd look gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs:amd-test Needs a test report or merge-check on an AMD GPU

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants