Skip to content

kernelx: XCL_COHERENT_HEAPS=sparse:N puts big GPU heaps in video memory - #2009

Open
Ariorh1337 wants to merge 2 commits into
xcl-dev:mainfrom
Ariorh1337:kernelx/coherent-heaps-sparse
Open

Ariorh1337 wants to merge 2 commits into
xcl-dev:mainfrom
Ariorh1337:kernelx/coherent-heaps-sparse

Conversation

@Ariorh1337

Copy link
Copy Markdown

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

An opt-in switch, XCL_COHERENT_HEAPS=sparse[:N], that puts a title's GPU heaps (committed with a PAGE_GRAPHICS_* protection) in the sparse windows (video memory) instead of the coherent window (process memory the GPU reaches over the bus). With sparse every heap moves, with sparse:N those of at least N MB. Part of #505.

Why. In the coherent window, on an RTX 5090:

The retail title of #505 keeps 4.4 GB of heaps there (1172, 2199, 563 and 596 MB), and its world passes take 0.4–2.7 s each.

Keeping the heaps out of the window (XCL_COHERENT_HEAPS=0) removed the GPU stall on an RTX 5090, an RTX 5080 (@kanade333), an RTX 3080 (@not-rex) and RDNA3 / Linux (@lucsoft). But there the shaders read zeros, and the 3D picture is black. This switch keeps them GPU-visible at the same addresses.

What is in it (crates/xcl/src/hle/kernelx.rs, one commit)

Tested

  • RTX 5090 32 GB (driver 32.0.16.1692), Ryzen 7 9800X3D, 64 GB, Windows 11 Pro 26300, rustc 1.99.0, DXC 1.9.2609.5.
  • cargo test --workspace -- --test-threads=4 (debug, --no-fail-fast), this PR (5370f4b): 988 passed, 4 failed, 45 ignored. The 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (0x8007000E at seed 70), the same on main 8e87fc7: 986 passed, 4 failed, 45 ignored. SKIPPED: 24, the same as on main (19 no wave64, 3 refused per-lane forms, no Developer Mode, no heap windows).
  • Retail title (1.0.2.1), on a local tree: main b9b6ea6 + open PRs + D3D12.X: the committed window gets a copy in video memory #1329 + the earlier prototype of D3D12.X: a copy back after a submission does not wait for the title #1993 + this switch, with XCL_COHERENT_HEAPS=sparse:1024 XCL_HEAP_WINDOWS=1. That moves the 1172 and 2199 MB heaps; the 563 and 596 MB ones, which hold the fences, stay in the window.
    • The intro played to the in-game HUD.
    • The title ran 465 s after the first #101 with 0 draws and 0 dispatches skipped.
    • 125 123 ops of console code took 21.1 s of GPU time. With the heaps in the window it was 30.9 s for 23 452 ops, and the title stopped ~45 s after #101.
    • One run each. A run on main + D3D12.X: a copy back after a submission does not wait for the title #1993 + this PR alone needs the whole shader cache rebuilt for main's HLSL; not done.
  • Cost seen in that run: the write-watch scan of the sparse windows took 88 s of the first 137 s (7.9 M dirty pages). That is the main thing between this and a default.

Regression run
scripts/smoke.ps1 -Game <retail title> -Seconds 60 (start screen, no input, switch not set, so this checks the default path), XCL_HEAP_WINDOWS=1, same machine. The main block is the one from #1993.

xcl-smoke v1
commit: 8e87fc7b62a87ea589ccb9ea6182f514016cf7db
gpu: NVIDIA GeForce RTX 5090
title: a155fb60
seconds: 60
exit: 0x0
frames shown: 1172 (PresentX 1173)
presents per 2 s (last 3): 0 13 51
device removals: 0
crashes: 0
peak commit: 25423 MB
xcl-smoke v1
commit: 5370f4b1738c63301158b683c346abd3f9b35a69
gpu: NVIDIA GeForce RTX 5090
title: a155fb60
seconds: 60
exit: 0x0
frames shown: 1233 (PresentX 1234)
presents per 2 s (last 3): 52 21 24
device removals: 0
crashes: 0
peak commit: 27669 MB

Not tested

AI use

  • Claude Code (Claude Opus 5.5) found the cause with a micro-benchmark and the title runs, wrote the change and the test, ran the suite and the smoke runs on the machine above, and wrote this description. I read the diff and checked the results before opening the PR.

🤖 Generated with Claude Code

Ariorh1337 and others added 2 commits November 25, 2026 01:26
`readback_after_execute` made the sparse queue GPU-wait for the title's
queue and then waited on the CPU for the copy, on the thread that was
submitting. The title's queues can wait for work that same thread submits
next (four compute queues waiting for a Write64 on q1 whose list is
recorded but not submitted yet). So the copy waited its 5 s on every such
submission, and the uploads queued behind it on the sparse queue waited
too (`before_execute` waiting for ring room). In thread dumps of a retail
title on an RTX 5090, the submitting thread is in `readback_ranges` ->
`wait_for` while another title thread waits for the sparse state's lock in
`commit_range`. lucsoft found the same cycle on RDNA3 / Linux (xcl-dev#505).

The copies now run on a queue of their own, which GPU-waits for the
title's queue, and the submitting thread returns. Readback buffers are
sized to the copy (64 KiB and up) and reused. Without a readback queue (it
could not be made), the old path runs.

The data lands in the title's memory once the copy is done, before the
title can see the work it follows is done:
- on the next submit, and from a thread that looks every 0.5 ms;
- before Map's readback and in the CPU memory waits of Wait64BitValueX
  (`settle_readbacks`), which wait for the copy once the title's lists it
  follows are done;
- in the title's fence observations: `GetCompletedValue` settles the copies
  first. `SetEventOnCompletion` hands the fence an event of XCL's own, and a
  thread-pool wait on it settles them, then sets the title's event.

The new test `a_copy_back_does_not_wait_for_the_title_queue` waits 5.0 s
on the old path and under 1 s with this change. Without the fence hooks,
the e2e test title read 0 for its Write32BitValueBottomOfPipeX after its
fence wait. With them, both e2e tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A title's GPU heaps (committed with a PAGE_GRAPHICS_* protection) go to
the coherent window, process memory the GPU reaches over the bus. On an
RTX 5090 a shader's dependent reads there are ~30x slower than from video
memory (no L2 hits, about 2.4 us each). Its store instructions whose lanes
leave gaps (per-lane dwordx2/x4, 8- and 16-byte formats) run at ~0.1 GB/s,
against ~50 GB/s for lane-contiguous ones and >1 TB/s in video memory. The
retail title of xcl-dev#505 keeps 4.4 GB of heaps there (1172, 2199, 563 and
596 MB), and its world passes take 0.4-2.7 s each. Keeping the heaps out
of the window was reproduced as removing the stall on an RTX 5080, an
RTX 3080 and RDNA3 / Linux (xcl-dev#505), but there the shaders read zeros.

With `XCL_COHERENT_HEAPS=sparse` every such heap, with `sparse:N` those of
at least N MB, goes to the sparse windows instead: video memory, with the
CPU's writes uploaded before each submission, like the title's other
graphics memory. A heap that goes there is not counted in the coherent
window's size for the next run (`note_coherent_need`). Off by default: it
costs a second copy in video memory and the write-watch scan of it, and it
needs the asynchronous copies back of the commit below. Without them, the
copies back of the title's lists' writes into these heaps wait for the
submitting thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ariorh1337
Ariorh1337 marked this pull request as ready for review November 25, 2026 00:46
@futurepro62

futurepro62 commented Nov 25, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 5370f4b (on its base 8e87fc7).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::dxil::fuzz_tests: 981 passed, 0 failed, 43 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: a_copy_back_does_not_wait_for_the_title_queue, sparse_heaps_setting.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 991 passed, 0 failed, 45 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main 7994cd1 too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651), the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests and const_and_null_mask_operands_agree_with_hlsl_on_the_gpu (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@raylitebull

Copy link
Copy Markdown
Member

Code review on 5370f4b:

  • In crates/xcl/src/hle/kernelx.rs, sparse_heaps_min() / parse_sparse_heaps (XCL_COHERENT_HEAPS=sparse[:N], overflow-safe checked_mul(1 << 20), and sparse_heaps_setting unit tests) cleanly routes graphics heaps >= N MiB into d3d12x::sparse::alloc.
  • Diff audited and CI workflow approved (currently running).
  • Note: This branch also carries the crates/xcl/src/hle/d3d12x/sparse.rs async-readback changes from #1993 — so please keep the shared monotonic rb_fence multi-queue ordering note from #1993 in mind (or rebase onto #1993 once settled).

Remaining before merge: Settling the #1993 sparse.rs overlap / multi-queue rb_fence note, Build and test finishing green, and an AMD verification pass.

@Filip722

Copy link
Copy Markdown

On the RTX 5060 Laptop GPU (8 GiB), the first sample with zero render-target pages showed 256 MiB of sparse pools on main and 3,584 MiB with #1993 + #2009 (197 and 3,527 MiB mapped). The cost was already visible before render-target streaming.

Requested size Main bf9eebd 5370f4b integrated on the same main
1,172 MiB Coherent Sparse VRAM
2,199 MiB Write-watched fallback Sparse VRAM
563 MiB Coherent Coherent
596 MiB Write-watched fallback Coherent

Both offline Series X runs used fresh copies of the same prefix/cache seeds and XCL_COHERENT_MB=2048 XCL_COHERENT_HEAPS=sparse:1024 XCL_HEAP_WINDOWS=1. I retained main’s internal-wait diagnostics when integrating the stack. The 2 GiB free-VRAM guard stopped each run around 30 s, before account input. I added 100 ms VRAM sampling for the second run after seeing a >1 GiB drop between baseline samples. Each had seven black captures, zero scope swap and zero reported storage failures. World entry remains unverified here.

Bazzite 44, NVIDIA 615.78.08, Proton Experimental 11.0-20261112, local vkd3d-proton 3.1.0 (4522cc1a8b1347f with dxil-spirv compatibility changes). Focused Windows release tests under Proton, including sparse GPU readback, passed 5/5 on main and 7/7 with the stack. No reported skips.

OpenAI Codex (GPT-6.2-Sol) integrated the PRs locally, ran the comparison and wrote this report.

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