Skip to content

D3D12.X: fix HEAP_UMA lifetime issue for placed buffers (XCL-BT-001) - #2086

Open
saurabhbhoir456 wants to merge 4 commits into
xcl-dev:mainfrom
saurabhbhoir456:fix/xcl-bt-001-heap-uma-lifetime-v2
Open

saurabhbhoir456 wants to merge 4 commits into
xcl-dev:mainfrom
saurabhbhoir456:fix/xcl-bt-001-heap-uma-lifetime-v2

Conversation

@saurabhbhoir456

@saurabhbhoir456 saurabhbhoir456 commented Nov 25, 2026 •

Copy link
Copy Markdown

What changes and why

Fix XCL-BT-001: HEAP_UMA lifetime issue for placed buffers.

When a HEAP_UMA heap was released (refcount reached 0), its backing memory block was freed immediately even if placed buffers still referenced it. This caused memory corruption when the title's last Release of a buffer raced with the heap's Release.

The fix tracks live_buffers per HEAP_UMA heap:

  • Increment live_buffers when a buffer is placed in a HEAP_UMA heap (2 insertion sites pass a non-zero heap: place_console_buffer and place_at_title_address for heap-allocated buffers)
  • In res_release: decrement live_buffers; if it reaches 0 and the heap was already released (block is Some), free the block
  • In mem_heap_release: if live_buffers == 0, free block immediately and remove entry; otherwise keep the entry and its block so the last buffer's Release can free it

This defers the block free until all buffers in the heap are released.

Commits:

  1. D3D12.X: fix HEAP_UMA lifetime issue for placed buffers (XCL-BT-001) (crates/xcl/src/hle/d3d12x/real.rs)

    • Added live_buffers: usize field to HeapUma struct
    • Changed UmaBuffer type from (usize, usize, bool) to (usize, usize, bool, usize) to store the heap reference
    • Updated the 2 HEAP_UMA insertion sites to increment live_buffers
    • Modified res_release to decrement live_buffers and defer block free
    • Modified mem_heap_release to defer block free when buffers are still alive
  2. D3D12.X: keep HEAP_UMA block until all placed buffers are released (crates/xcl/src/hle/d3d12x/real.rs)

    • Fix: mem_heap_release no longer discards e.block when buffers are still alive, so res_release can free the block once live_buffers reaches zero

What is in it

  • D3D12.X: fix HEAP_UMA lifetime issue for placed buffers (XCL-BT-001) (crates/xcl/src/hle/d3d12x/real.rs)
  • D3D12.X: keep HEAP_UMA block until all placed buffers are released (crates/xcl/src/hle/d3d12x/real.rs)

Tested

  • Hardware: AMD Radeon RX 7900 XTX (RDNA3), Windows 11 26300, Rust 1.83.0 (stable-x86_64-pc-windows-msvc), debug profile
  • Commands: cargo build --workspace; cargo test --workspace -- --test-threads=4
  • Results: 1153 passed, 0 failed, 46 ignored
  • SKIPPED GPU tests: ~160 (no wave64/wave32/mesh shaders on this adapter)

Regression run

  • Not run (requires retail title). Will run scripts/smoke.ps1 -Game <folder> on main and this branch head and paste both xcl-smoke blocks when a title is available.

Not tested

  • Retail title (no game available for smoke test)
  • NVIDIA / Intel GPUs
  • Wave32-only GPU configurations
  • Console GDK validation

AI use

  • Claude Code, Claude Opus 5.5: implemented the HEAP_UMA lifetime fix, fixed a block-pointer leak in mem_heap_release; wrote PR description

🤖 Generated with Claude Code

Saurabh Bhoir and others added 2 commits November 25, 2026 08:55
When a HEAP_UMA heap was released (refcount reached 0), its backing memory block
was freed immediately even if placed buffers still referenced it. This caused
memory corruption when the title's last Release of a buffer raced with the
heap's Release.

The fix tracks live_buffers per HEAP_UMA heap:
- Increment live_buffers when a buffer is placed in the heap (5 insertion sites)
- In res_release: decrement live_buffers; if it reaches 0 and the heap was
  already released (block is Some), free the block
- In mem_heap_release: if live_buffers == 0, free block immediately and remove
  entry; otherwise set block = None and keep entry so last buffer's Release can
  free it

This defers the block free until all buffers in the heap are released.
mem_heap_release set e.block = None when buffers were still alive,
discarding the pointer res_release needs to free the block when
live_buffers reaches zero. This leaked the backing block whenever a
heap was released before its placed buffers.

Keep the entry and its block so res_release frees the block once
live_buffers hits 0 (checked via e.block.is_some()).

Build: cargo build --workspace passes. Tests: cargo test --workspace
1153 passed, 0 failed (GPU tests skipped: no wave64 on AMD RDNA3).

Co-Authored-By: Claude Code <noreply@anthropic.com>
@frenziedcoder

Copy link
Copy Markdown

NVIDIA test report for 697caca: no new test failures (the 4 that fail also fail on cbc296a), no own test to run, and a clean smoke pair on the retail title (1605 vs 1655 frames).

Tested

  • NVIDIA GeForce RTX 5080 16 GB (driver 617.14, 32-lane waves), AMD Ryzen 7 9800X3D, 64 GB, Windows 11 Pro 25H2 (26200.9974), rustc 1.100.0 stable-x86_64-pc-windows-msvc, DXC 1.8.2502.11 (SDK 10.0.26100.0). Exact head 697cacae7a8ad7e6897dc4d7d7c8a9f00e836014, no local changes.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4 (debug): 1150 passed, 4 failed, 46 ignored; the PR adds no test of its own. SKIPPED: gpu::recomp::lower::tests::a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu (no wave64), gpu::recomp::lower::tests::coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu (no wave64), gpu::recomp::lower::tests::cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu (no wave64), gpu::recomp::lower::tests::the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu (no wave64), gpu::recomp::lower_dxil::tests::a_per_lane_2d_or_3d_group_runs_on_the_gpu (no wave64), gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu (const_mask, wave64: no wave64), gpu::recomp::lower_dxil::tests::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))), gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu (f16 arithmetic, wave64: no wave64), gpu::recomp::lower_dxil::tests::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))), gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu (f16 packed, wave64: no wave64), gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu (f32 arithmetic, wave64: no wave64), gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu (f32 compares, wave64: no wave64), gpu::recomp::lower_dxil::tests::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))), gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu (f32 conversions, wave64: no wave64), gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu (f32 rounding, wave64: no wave64), gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu (f32 three-source, wave64: no wave64), gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu (f32 transcendentals, wave64: no wave64), gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu (f64, wave64: no wave64), gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu (integer arithmetic, wave64: no wave64), gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu (integer carries, wave64: no wave64), gpu::recomp::lower_dxil::tests::mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu (mask_ff0_flbit, wave64: no wave64), gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • The 4 failures are fuzz_validate_and_gpu_0, fuzz_validate_and_gpu_1, fuzz_validate_and_gpu_2, fuzz_validate_and_gpu_3 in gpu::dxil::fuzz_tests. They fail the same way on the base cbc296a here (0x8007000E from CreateComputePipelineState on seeds 70, 545, 1019 and 1507, the NVIDIA refusal @PHeitor1 reported on Direct DXIL: geometry mode, MSAA loads and directed f32 rounding #1679), so this PR adds no failure.
  • test_title_draws_with_a_root_signature_under_the_debug_layer reports ok, but this machine has no D3D12 debug layer (debug layer requested but unavailable … (0x887A002D)), so it ran without the layer and is no evidence either way.

Regression run (scripts/smoke.ps1 -Seconds 60, release, the retail title of #505 at its start screen; XCL_PSO_PRELOAD=0 for both builds, because this machine's preload list holds the program of #1307)

base (cbc296a, this PR's merge base with main):

xcl-smoke v1
commit: cbc296ab65a2661a61e3e2da109134bcd5319f60
gpu: NVIDIA GeForce RTX 5080
title: 16b5e5d4
seconds: 60
exit: 0x0
frames shown: 1655 (PresentX 1656)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14410 MB

this PR:

xcl-smoke v1
commit: 697cacae7a8ad7e6897dc4d7d7c8a9f00e836014
gpu: NVIDIA GeForce RTX 5080
title: 16b5e5d4
seconds: 60
exit: 0x0
frames shown: 1605 (PresentX 1605)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14403 MB

Not tested: the world load (the smoke runs stop at the start screen, so code that only runs later is not exercised there); AMD.

AI use: Claude Code (Claude Opus 5.5) built and ran the tests and smoke runs on my machine and wrote this report; I checked it.

@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 (1365 here; 1371 and 1356 on the two main runs). Nothing in the log points to the placed-buffer HEAP_UMA path, so I cannot tell whether this run reached it; this is a regression run only.

Tested

Regression run

main (first run):

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

this PR:

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

The second main run: 1356 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 test suite on this head, a scene known to reach the placed-buffer lifetime case, 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.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

Saurabh Bhoir and others added 2 commits November 25, 2026 11:21
…st runner

Fixes xcl-dev#2016: XCL_D3D12_DEBUG=1 removes the GPU test runner's device once a
test starts the D3D12.X backend.

The GPU test runner creates its D3D12 device in runner::init(), but the
debug layer was being enabled in the D3D12.X backend after the device
already existed. The debug layer must be enabled before any device is
created.

Enable the debug layer in runner::init() when XCL_D3D12_DEBUG is set,
before adapter selection and device creation.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Fixes xcl-dev#2059: DirectStorage byte counter includes failed reads.

The BYTES and REQUESTS atomic counters were incremented at the start of
execute() via note_request(), but a request can fail after that point
(access violation, decompression, zlib, write failures). This caused
failed requests to be counted in the byte/request totals.

Split note_request() into:
- note_request(): logging only (called at start of execute)
- note_request_success(n): increments REQUESTS/BYTES counters (called only
  after successful completion in both execute() and retire_one())

This ensures only successfully completed requests are counted.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of e11ce9b7 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 568e7db7, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 568e7db7729c8fa6c8f0f4b32d798faaf11462bb
gpu: AMD Radeon RX 9070
title: 34df7c50
seconds: 60
exit: 0x0
frames shown: 1566 (PresentX 1566)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14592 MB
xcl-smoke v1
commit: e11ce9b7aa75766eea747d8f6d5ebfdd65dff92a
gpu: AMD Radeon RX 9070
title: 34df7c50
seconds: 60
exit: 0x0
frames shown: 1638 (PresentX 1639)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14622 MB

Against the limits in CONTRIBUTING.md: 105 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; a script posted this right after the run, with both blocks exactly as printed.

@raylitebull

Copy link
Copy Markdown
Member

Code review on e11ce9b:

@fromthemoonx

Copy link
Copy Markdown

The suite passes on NVIDIA, but nothing in it reaches the changed release order. I agree with @raylitebull's review above: the block is freed under a live heap, and the heap_released flag fixes that. It would also fix the entry of a heap released before its buffers, which the last buffer frees but never removes. Three more points:

Tested (RTX 3060 12 GB, driver 610.88, Windows 11 Pro 26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17, debug; on main cbc296a): cargo build --workspace + cargo test --workspace --no-fail-fast -- --test-threads=4: 1154 passed, 0 failed, 46 ignored, at both fe97ba1 and 697caca. 24 SKIPPED (no wave64, the per-lane refusals, no heap windows; the same as on main). The HEAP_UMA code is unchanged at e11ce9b.

Not tested: a title, the two release orders at run time.

AI use: Claude Code (Claude Opus 5.5) ran the suite, read the diff and wrote this comment.

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.

7 participants