Skip to content

kernelx: keep AllocateTitlePhysicalPages out of coherent UMA when sparse is on - #2135

Open
Simon-CR wants to merge 1 commit into
xcl-dev:mainfrom
Simon-CR:pr/kernelx-no-coherent-spill
Open

Simon-CR wants to merge 1 commit into
xcl-dev:mainfrom
Simon-CR:pr/kernelx-no-coherent-spill

Conversation

@Simon-CR

Copy link
Copy Markdown

When AllocateTitlePhysicalPages cannot fit a new physical-page backing allocation inside the sparse windows, it previously fell back to u.alloc in the fixed coherent unified-memory window before falling back to plain MEM_RESERVE virtual memory. Since physical-page homes only need virtual address space, spilling failed sparse allocations into coherent RAM needlessly consumes scarce GPU-visible coherent memory needed by command recordings and readback buffers. Part of #1566.

What is in it

  • Factor alloc_physical_home_block in crates/xcl/src/hle/kernelx.rs to allocate physical page homes from sparse windows when enabled, skipping coherent fallback directly to MEM_RESERVE
  • Add unit test physical_home_block_does_not_spill_into_coherent_when_sparse_enabled verifying coherent UMA remains untouched when sparse space is exhausted

Tested

Regression run

xcl-smoke v1
commit: 6020a33b16ba2de25b3aef5947552f2dc1831f09
gpu: AMD Radeon RX 9070 XT
title: 5ec87c56
seconds: 60
exit: 0x0
frames shown: 1483 (PresentX 1483)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19090 MB
xcl-smoke v1
commit: 8a3e42cec3bb0a583c0abab85b872143f6462406
gpu: AMD Radeon RX 9070 XT
title: 5ec87c56
seconds: 60
exit: 0x0
frames shown: 1578 (PresentX 1578)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 20933 MB

Not tested

  • Full retail sparse-exhaustion soak on main (verified during an extended local 240 s soak along with allocator reclamation), NVIDIA GPUs, Intel Arc GPUs, wave32-only GPUs, and Linux/Wine

AI use

  • Antigravity (frontier model; exact model identifier not exposed to agent) assisted with factoring the helper, writing the unit test, running the local test suite and smoke runs, and drafting this description. All builds, tests, and regression runs were executed locally on the hardware above.

…rse is on

When sparse windows are exhausted during AllocateTitlePhysicalPages,
spilling physical page home reservations into the fixed coherent UMA
window consumes scarce GPU-visible coherent RAM even though physical
page homes only need virtual address space (already noted for the plain
MEM_RESERVE fallback). Skip the coherent fallback when sparse windows are
enabled and fall back directly to plain MEM_RESERVE.

Part of xcl-dev#1566.

Assisted-by: Antigravity
@luckyteagirl

Copy link
Copy Markdown

Independent native Windows check of exact PR head 8a3e42cec3bb0a583c0abab85b872143f6462406 against main 6020a33b16ba2de25b3aef5947552f2dc1831f09.

Hardware/toolchain: Windows 11 build 26200, Ryzen 9 5980HX, Radeon RX 6800M driver 30.0.13002.13003, Rust 1.99.0 MSVC, DXC/DXIL 1.9.2609.5.

Tested

  • cargo test --locked -p xcl --lib -j 2 kernelx -- --test-threads=1 --show-output: 41 passed, 0 failed, 1 ignored on the exact PR head. This includes physical_home_block_does_not_spill_into_coherent_when_sparse_enabled, which passed separately as a one-test filter too.
  • cargo build --locked --workspace --release -j 2: passed.
  • Paired 7-second launches of my owned 1.0.2.1 title with isolated data and shader caches and a commit-memory guard. The guard did not fire:
xcl-smoke v1
commit: 6020a33b16ba2de25b3aef5947552f2dc1831f09
gpu: AMD Radeon RX 6800M
title: 2ef5fd64
seconds: 7
exit: 0x0
frames shown: 1 (PresentX 1)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 6969 MB
xcl-smoke v1
commit: 8a3e42cec3bb0a583c0abab85b872143f6462406
gpu: AMD Radeon RX 6800M
title: 2ef5fd64
seconds: 7
exit: 0x0
frames shown: 1 (PresentX 1)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 6981 MB

The short title runs reached only the first frame, with zero draws. They check startup, not a retail sparse-exhaustion path or sustained memory behavior. I did not run the full workspace suite on this PR head; my current-main GPU suite on this older driver has unrelated device-suspension failures tracked separately. No game data was uploaded.

AI-assisted testing and report drafting with OpenAI Codex (GPT-6); I checked the exact revisions and local logs before posting.

@fromthemoonx

Copy link
Copy Markdown

Passes on NVIDIA (RTX 3060, which has sparse windows here), at the head and merged onto current main. The new test fails when the coherent fallback is put back.

  • Exact head 8a3e42c (base 6020a33): cargo build --workspace + cargo test --workspace --no-fail-fast -- --test-threads=4: 1155 passed, 0 failed, 46 ignored. Merged onto main 6e1e2be (no conflicts): 1155 passed, 0 failed, 46 ignored. 24 SKIPPED on both, the same as on main: no wave64, the per-lane refusals and heap_window_out_of_memory_falls_back_to_the_switch_form. The new test does not skip.
  • physical_home_block_does_not_spill_into_coherent_when_sparse_enabled: 10 of 10 runs. cargo test -p xcl --lib kernelx -- --test-threads=4: 41 passed, 5 of 5 runs.
  • Mutation: with .or_else(|| u.alloc(bytes, PHYS_PAGE)…) put back after sparse_alloc in alloc_physical_home_block, the new test fails (kernelx.rs:3111).

Hardware: RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17, debug.

Not tested: a title (no smoke run here), so not whether a title that fills its sparse windows still runs with plain-memory homes instead of coherent ones.

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

@JoaoPauluu

Copy link
Copy Markdown

A regression run under Wine on Linux on the retail title: no device removal, no crash, frames level with main (1376 here; 1368 and 1370 on the two main runs). The title's GPU heaps stay outside unified memory here (UMA: 13507 MB of commit free, under 17408 MB), and no AllocateTitlePhysicalPages line appears, so the changed path is not reached; this is a regression run only.

Tested

Regression run

main (first run):

xcl-smoke v1
commit: 6e1e2befc7430126b1e7b98b5c3fe93813c866b6
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1368 (PresentX 1368)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 0 MB

this PR:

xcl-smoke v1
commit: 8a3e42cec3bb0a583c0abab85b872143f6462406
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1376 (PresentX 1376)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 0 MB

The second main run: 1370 frames, exit: 0x0, no device removal, no crash. Under Wine, peak commit reads 0 MB, so these blocks do not check memory.

Not tested

  • the whole test suite on this head, a machine with enough free commit for the coherent window, native Windows.

AI use

  • Claude Code (Claude Opus 5.5) built both commits, ran the runs and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

@wish131400

Copy link
Copy Markdown

Independent real physical-page API regression check for this PR, in addition to its existing local-UMA helper test.

Compared target main 6e1e2befc7430126b1e7b98b5c3fe93813c866b6 with PR head 8a3e42cec3bb0a583c0abab85b872143f6462406. Both were fixed source archives plus the same test-only module appended to kernelx.rs; production source was left unchanged. The PR head's actual parent is 6020a33b, while its current target base is 6e1e2be. The two archive trees differ only in README and kernelx.rs.

Tested

  • Windows 10 Pro x64 build 19045, Ryzen 7 7700 (8C/16T), rustc 1.98.1 (48a229cea 2026-09-01) MSVC; debug test profile. This test executes Win32 allocation APIs only; it does not initialize a GPU or DXC.
  • Each source: cargo +stable test --locked --no-run -j1 -p xcl --lib --message-format=json-render-diagnostics, followed by its pinned standalone libtest binary in a fresh process:
    hle::kernelx::physical_home_api_regression::physical_page_api_does_not_spill_into_coherent_when_sparse_exhausted --exact --test-threads=1 --nocapture.
  • Cleared inherited XCL_*; set XCL_UMA_MB=1, XCL_COHERENT_MB=1, XCL_UMA_WINDOWS=4, XCL_HEAP_WINDOWS=0. The fixture asserts the actual coherent size is 1 MiB. Approximately 12 GiB is sparse VA reservation, not RAM/commit.
  • Baseline: 0 passed / 1 failed / 0 ignored / 0 SKIPPED, failing only the final API_POLICY assertion. PR head: 1 passed / 0 failed / 0 ignored / 0 SKIPPED. These are one case per binary, not five separate tests.

The case uses the actual AllocateTitlePhysicalPages / FreeTitlePhysicalPages, production registry/page_home, and VirtualQuery. It performs three available-sparse controls and two zero-commit sparse-exhaustion rounds; it cleans up each round before the final policy assertion.

After sparse exhaustion, one physical home main PR head
Increase in coherent allocator u.used() 65536 B 0 B
Home inside UMA true false
VirtualQuery state before Free MEM_COMMIT (4096) MEM_RESERVE (8192)
API Free removes registry, restores coherent usage yes yes
Ordinary fallback reservation becomes MEM_FREE n/a yes

Both exhaustion rounds give those same results; all three available-sparse controls still select uncommitted sparse homes and free them successfully. The 65536 B is consumption of the already committed coherent allocator's budget, not a measurement of extra process commit or an FPS gain.

Not tested

GPU coherence/import/driver behavior, a retail title or smoke run, full workspace suite, multithreaded physical mapping/remapping, whole-system OOM, frame time/FPS. This is a focused allocator-policy/API regression, not a merge-ready runtime PR or a game performance claim. No game data or SDK content is included.

AI use

OpenAI Codex (GPT-6) reviewed the upstream change, wrote the test-only API extension, ran the builds/tests on the machine above, verified the actual logs/source/executable hashes, and drafted this user-authorized report.

Reproducer: append to crates/xcl/src/hle/kernelx.rs in both source snapshots
#[cfg(test)]
mod physical_home_api_regression {
    use super::*;

    /// Run alone in a fresh process: the real API uses the process-global UMA.
    #[test]
    fn physical_page_api_does_not_spill_into_coherent_when_sparse_exhausted() {
        unsafe {
            assert_eq!(std::env::var("XCL_UMA_MB").as_deref(), Ok("1"));
            assert_eq!(std::env::var("XCL_COHERENT_MB").as_deref(), Ok("1"));
            assert_eq!(std::env::var("XCL_UMA_WINDOWS").as_deref(), Ok("4"));
            let u = crate::gpu::uma::get().expect("API fixture requires virtual address space");
            assert!(u.has_sparse(), "the sparse path must actually be available");
            assert_eq!(u.shader_windows(), 4);
            assert_eq!(u.size(), 1 << 20, "both size overrides must take effect");
            assert!(PHYS_BLOCKS.lock().unwrap().is_empty(), "run this exact test alone");
            assert!(AWAY.lock().unwrap().is_none(), "no previous physical mapping state");
            let used_at_start = u.used();
            assert!(used_at_start + PHYS_PAGE <= u.size(), "baseline needs coherent headroom");
            let mut observed = Vec::new();

            // A sparse Allocate/Free control before, between, and after two exhaustions.
            for round in 0..3 {
                let before = u.used();
                let mut n = 1usize;
                let mut pages = [0usize; 1];
                assert_eq!(AllocateTitlePhysicalPages(std::ptr::null_mut(), 0, &mut n, pages.as_mut_ptr()), 1);
                assert_eq!(n, 1);
                let home = page_home(pages[0]).expect("real API must register the physical home");
                assert!(u.is_sparse(home), "available sparse space is used on both snapshots");
                assert_eq!(u.used(), before);
                let mut info: mem::MEMORY_BASIC_INFORMATION = std::mem::zeroed();
                assert_ne!(mem::VirtualQuery(home as *const c_void, &mut info, std::mem::size_of_val(&info)), 0);
                assert_eq!(info.State, mem::MEM_RESERVE, "unused physical homes stay uncommitted");
                assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 1, pages.as_mut_ptr()), 1);
                assert!(page_home(pages[0]).is_none());
                assert!(u.sparse_reservation(home).is_none());
                assert_eq!(u.used(), before);
                println!("API_HOME control={round} home={home:#x} coherent_used={before} state={} freed=true", info.State);
                if round == 2 {
                    break;
                }

                // Same zero-commit exhaustion sequence as the upstream regression.
                let mut reserved = Vec::new();
                while let Some(r) = u.sparse_reserve(1usize << 30) {
                    reserved.push(r);
                }
                while let Some((p, _)) = u.sparse_alloc(PHYS_PAGE, PHYS_PAGE) {
                    reserved.push(p);
                }
                assert!(!reserved.is_empty());
                assert!(u.sparse_alloc(PHYS_PAGE, PHYS_PAGE).is_none());
                assert_eq!(u.used(), before, "exhausting sparse VA must not use coherent bytes");

                let mut n = 1usize;
                let mut pages = [0usize; 1];
                assert_eq!(AllocateTitlePhysicalPages(std::ptr::null_mut(), 0, &mut n, pages.as_mut_ptr()), 1);
                assert_eq!(n, 1);
                let home = page_home(pages[0]).expect("fallback must register its physical home");
                let delta = u.used().checked_sub(before).expect("coherent usage cannot shrink");
                let in_uma = u.contains(home);
                let is_sparse = u.is_sparse(home);
                let is_coherent = u.is_committed(home);
                let mut info: mem::MEMORY_BASIC_INFORMATION = std::mem::zeroed();
                assert_ne!(mem::VirtualQuery(home as *const c_void, &mut info, std::mem::size_of_val(&info)), 0);
                let state = info.State;

                // Complete cleanup even on the expected baseline policy failure below.
                assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 1, pages.as_mut_ptr()), 1);
                assert!(page_home(pages[0]).is_none(), "Free must remove the real registry entry");
                assert_eq!(u.used(), before, "Free must restore coherent usage on either snapshot");
                if !in_uma {
                    let mut after: mem::MEMORY_BASIC_INFORMATION = std::mem::zeroed();
                    assert_ne!(mem::VirtualQuery(home as *const c_void, &mut after, std::mem::size_of_val(&after)), 0);
                    assert_eq!(after.State, mem::MEM_FREE, "ordinary fallback reservation must be released");
                }
                let reservations = reserved.len();
                for r in reserved {
                    assert!(u.sparse_release(r).is_some());
                }
                assert_eq!(u.used(), used_at_start);
                assert!(PHYS_BLOCKS.lock().unwrap().is_empty());
                println!("API_HOME exhausted={round} home={home:#x} reservations={reservations} coherent_delta={delta} in_uma={in_uma} sparse={is_sparse} coherent={is_coherent} state={state} freed=true");
                observed.push((round, delta, in_uma, is_sparse, is_coherent, state));
            }
            assert_eq!(observed.len(), 2);
            assert!(observed.iter().all(|&(_, delta, in_uma, sparse, coherent, state)| {
                delta == 0 && !in_uma && !sparse && !coherent && state == mem::MEM_RESERVE
            }), "API_POLICY: exhausted sparse homes must be plain reserved VA without coherent spill: {observed:?}");
        }
    }
}

Fixture SHA256: 2d4994650f4f6aa106b8337058ce7bd5a7f549643503a291dfc0662a696e8a5b.

Actual test exe SHA256: main 4bd1734e26ccade34e30f91d68409ee07eca29ed60860ed39ed6aecca3601e02; PR d7d04504892bd3829bcaa9f5b291a553fbb7860da443c2c35947bcab46bd8ece. Both successful builds reported fresh=false; sources were hashed before and after. An earlier candidate invocation which reported fresh=true was rejected and not executed as evidence; the candidate was then actually rebuilt.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
@pablogonz12

Copy link
Copy Markdown
Contributor

Full lib suite and integration tests under Wine on RADV, with main as the control. I found no regression from this PR. Head 8a3e42c against main 6e1e2be (the PR's base 6020a33 differs from it only in README.md).

Tested

  • AMD Radeon RX 9070 XT (RADV GFX1201), Mesa 26.3.1-arch3.5, Ryzen 9 9900X, Wine GE-Proton11-14, vkd3d-proton 3.1 with the XCL patch, DXC 1.9.2609.5, rustc 1.100.0, cargo xwin x86_64-pc-windows-msvc, static CRT, release.
  • Sparse windows are on here (UMA: 3 sparse VRAM window(s) (12 GiB VA) + 1 coherent window(s) (512 MB) in the test log). The new test runs, not skipped, and passes. kernelx filter, one thread: main 40 passed, 0 failed, 1 ignored; head 41 passed, 0 failed, 1 ignored.
  • Mutation: with the old .or_else(|| u.alloc(..)) fallback put back in the helper, the new test fails (left: Some(30064771072), right: None).
  • Lib suite, --test-threads=4, skipping integer_ops_match_cpu, uaddc_and_usubb_match_cpu and jumps_to_outer_loop_heads_are_structured (they kill the test binary on this stack). 23 runs per side:
passed / failed / ignored main head
usual result: 1088/7/26 on main, 1089/7/26 on head 20 18
plus 3 title-texture / d32s8 tests: 1085/10 on main, 1086/10 on head 2 1
device removed (0x887A0005), 12 failed: 1083/12 on main, 1084/12 on head 1 3
one extra failure, xapu_connect_returns_oom_and_cleans_up_on_failure: 1088/8 0 1
  • The 7 usual failures are the same on both: fuzz_validate_and_gpu_2, directed_reference_basics, lower_dxil f32_programs_on_the_gpu and f64_programs_on_the_gpu, gpu_nonfused_mad_and_mac_prevent_fma_contraction, gpu_depth_buffer_created_for_unordered_access_is_placed, xma2_source_voice_decodes_and_manages_lifetime.
  • The device removal is intermittent on main too, with the same 12 failing tests. It also happened in 2 of 10 more head runs with the new test left out (--skip physical_home_block), so the new test is not needed to trigger it. With 1 in 23 against 3 in 23, these runs do not show a difference between the two, and do not rule out a small one.
  • The xapu_connect failure happened once in these runs (its assertion: "data buffer must be released when event creation fails", left 8192). I did not investigate it.
  • 7 SKIPPED lines, identical in the first run of each side: six per-lane f16 / f32-conversion refusals and pso_measurement_compiles_an_exact_synthetic_record.
  • Integration tests (17 binaries, one thread, one run per side): 53 passed, 3 failed, 20 ignored on both. Same three: e2e (window not cloaked, XMA2 GetState), pls_quota, predication_e2e.

Not tested

  • native Windows, a game or smoke run, a title that fills its sparse windows, the 3 skipped tests.

AI use

  • Claude Code (Claude Sonnet 5.5) built both commits, ran the tests and the mutation check, and wrote this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants