Skip to content

Recompiler: direct compute LDS is the block the program declares - #3478

Open
Foniass wants to merge 1 commit into
xcl-dev:mainfrom
Foniass:dxil-declared-lds
Open

Foniass wants to merge 1 commit into
xcl-dev:mainfrom
Foniass:dxil-declared-lds

Conversation

@Foniass

@Foniass Foniass commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Rebased directly onto main: #3468 is closed, because main's ada50ec (#3497) already clamps LDS accesses whenever lds_top < 8191.

The direct DXIL translator gives every compute program that uses LDS the whole 8,192-dword groupshared block, so a one-wave group holds 32 KB and few groups stay resident at once. With this PR the block is:

Program LDS accesses are clamped below the translator's words (main's lds_top < 8191, ada50ec). A header with no size, or one too large to leave room for those words, keeps the 8,192-dword layout. Mesh, geometry and the HLSL path are unchanged. On by default; XCL_DECLARED_LDS=0 brings back the 8,192-dword layout. The disk caches and ComputePolicyKey keep the two apart.

This combines two earlier versions:

The commit credits both as co-authors.

Kalmarv's numbers on #2937:

  • RTX 5070 Ti, the retail title: the late frame's compute went from about 89 to about 78 ms per present (per-program GPU timestamps over 187–229 s).
  • 491 of the 497 compute programs that use LDS shrink below 8,192 dwords.
  • amd-dev measured offline that the title's top open-world program goes from 32,768 to 5,008 bytes of LDS (Radeon GPU Analyzer, gfx1201).

What is in it

  • crates/xcl/src/gpu/recomp/mod.rs, recomp/lower.rs: CsInterface::lds_dwords (LDS_SIZE × 128)
  • crates/xcl/src/gpu/dxil/lanes.rs (setup_storage), kernel.rs (declared_lds_enabled, the test switch), lower.rs, lower_mem.rs: the layout and the clamp
  • crates/xcl/src/gpu/recomp/pso.rs, hle/d3d12x/real/compute_build_key.rs: cache identities (xcl-declared-lds-0 when off) and ComputePolicyKey.declared_lds
  • Tests:
    • the declared sizes (native wave32 / wave64, 1 and 2 waves, split with a barrier, size 0, too big);
    • declared_lds_accesses_stop_below_the_translators_words (disassembly: block size, clamp to 127, translator words at 128..130 / split 152..154);
    • a GPU test of a 16×8 group in native wave32, native wave64 and split wave64, on and off, writing past the declared LDS;
    • the build-key test.

Tested

  • NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2114) and AMD Radeon RDNA2 iGPU (driver 32.0.21030, XCL_ADAPTER=radeon), AMD Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug tests, release smoke
  • cargo test -p xcl --lib gpu::dxil: 283 passed, 0 failed, 3 ignored, on each of:
    • the 5070 Ti;
    • the iGPU;
    • the iGPU with XCL_SPLIT_WAVE64=1.
  • gpu::recomp + compute_build_key: 508 passed, 0 failed, 21 ignored on each of the same three. With XCL_DECLARED_LDS=0 (5070 Ti), gpu::dxil + declared_lds: 284 passed, 0 failed.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 39f4df5, rebased on main 86e86fc (5070 Ti): 2893 passed, 0 failed, 51 ignored. SKIPPED: 29 notices, almost all no wave64 (the others: no D3D12 debug layer, heap windows off, driver cache needs Developer Mode). The narrower runs listed here are from before the rebase.
  • Retail title: 60 s smoke run below.

Regression run

main:

xcl-smoke v1
commit: 86e86fcb33f94fa5d0c809d9b50af7b84bf49cce
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1608 (PresentX 1608)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15766 MB

this PR:

xcl-smoke v1
commit: 39f4df5416a4bb108ac1fb1fe708b292f590ac4d
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1600 (PresentX 1600)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16528 MB

Not tested

  • The frame-time gain was not measured here (Recompiler: XCL_DECLARED_LDS=1 sizes direct compute LDS from the header (~11 ms less GPU per frame) #2937's numbers above are Kalmarv's); RDNA3 / RDNA4
  • The GPU test cannot see a missing clamp (both drivers tolerate the stray writes); only the disassembly test fails without it
  • A program that accesses more LDS than it declares now has those accesses land on the last declared dword; before, they reached the rest of the 32 KB block. On the console they are out of range. How often the title does that is not known.
  • HANDOFF.md line 106 ("groupshared, 32 KB = the D3D12 limit") is no longer true for direct compute

AI use

🤖 Generated with Claude Code

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 30, 2026
The direct DXIL translator gave every compute program that uses LDS the
whole 8,192-dword groupshared block D3D12 allows. A one-wave group then
holds 32 KB, which caps how many groups a GPU keeps resident when its LDS
is shared by them (an RDNA WGP's 128 KB: four groups, one wave a SIMD).

The block is now the LDS COMPUTE_PGM_RSRC2 declares (CsInterface::lds_dwords,
LDS_SIZE in 128-dword granules) followed by the translator's own words: a
native 2-D / 3-D group's arrival counter, a multi-wave safe barrier's two
words (xcl-dev#3264; right after the program's LDS in a native group, below the
arrival word in the split form, as at the top of the 8,192-dword block),
and the split form's exchange slots and counters. A header with no size,
or one too large to leave room for those words, keeps the 8,192-dword
layout. Program LDS indices are clamped below the translator's words
(lds_index's `split || lds_top < 8191`, the parent commit), so an access
past the declared size stays at the last declared dword. Mesh and geometry
programs keep the 8,192-dword block. The HLSL path is unchanged.

On by default; XCL_DECLARED_LDS=0 restores the 8,192-dword layout. The
direct translator's disk-cache identities and ComputePolicyKey (the
driver pipeline cache's key) key the switch apart.

From Kalmarv's xcl-dev#2937 ("XCL_DECLARED_LDS=1 sizes direct compute
LDS from the header", opt-in there; written with Claude Code (Claude Opus
5.5), ComputePolicyKey part with Antigravity (Gemini 4 Neon)):
CsInterface::lds_dwords, the declared layout, the ComputePolicyKey field
and its build-key test, the `lds_top < 8191` clamp. From raylitebull's
amd-dev 5e48878 (xcl-dev#2937 adapted to the safe-barrier words, default on):
the safe barrier's placement, the test switch, the declared-size test and
the GPU test, extended here to native wave64 and the split form. New here:
a disassembly test of the clamp and of the translator's words.

Measured by xcl-dev#2937 (RTX 5070 Ti, the title, per-program GPU timestamps over
187-229 s): the late frame's compute from about 89 to about 78 ms per
present. Measured by 5e48878 offline (Radeon GPU Analyzer, gfx1201): the
title's top open-world program from 32,768 to 5,008 bytes of LDS.

Co-authored-by: Kalmarv <59708937+Kalmarv@users.noreply.github.com>
Co-authored-by: raylitebull <287713799+raylitebull@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thehollowedone

thehollowedone commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

I reran the 512-byte LDS boundary case against head 39f4df54 and its clean merge with main at d0832e6. This is now a measured blocking regression, not just a source prediction.

A native wave32 kernel declares 128 LDS dwords (512 bytes), sets M0 to 0xffff, writes 11 at byte 508 and 22 at byte 516, then reads both. AMD's RDNA2 ISA guide, §3.6.1 gives [11, 0]: the second write is out of range and discarded, and its read returns zero. On the PR head and clean merge, the GPU produced [22, 22]. On the same current main control, it produced [11, 22]: the old path already exposes an out-of-range read, while this PR additionally corrupts the final valid word. Each identical focused run failed 0 passed / 1 failed, with 0 ignored and 0 GPU skips.

The source cause is lds_index: umin(i, lds_top) maps both word 127 and word 129 to word 127. Keep the fitted guest byte limit separately from the allocated storage top. Check each original guest byte address before masking or mapping it. Out-of-range stores and atomics must have no memory effect; returning reads/atomics must yield zero while preserving inactive destination lanes and the untouched D16 half. Check both addresses of a paired DS instruction independently. Keep the existing fallback mapping when the declaration does not fit, and apply the guest bound even when the declaration fills all 8,192 words. Please add the boundary regression below and cover those other DS forms before merging; the scalar case alone does not verify wide or paired behavior.

Measured locally: Windows 11 build 26300, RTX 3080 Ti (driver 621.40), Rust 1.99.1 MSVC, DXC 1.8.2502.11. In three isolated worktrees (head, main, clean merge), cargo test --offline --locked -p xcl --lib gpu_probe_3478_last_lds_word -- --nocapture --test-threads=1 gave 0 passed / 1 failed / 0 ignored / 0 skipped in each. I did not run the full workspace suite or a title smoke on this test-only probe.

Minimal failing GPU regression (test-only candidate)
diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs
@@
+#[test]
+fn gpu_compute_declared_lds_drops_out_of_bounds_store() {
+    use crate::gpu::recomp::{runner, CsInterface, KERNEL_SGPRS};
+    assert_eq!(runner::wave32_supported(), Ok(true));
+    let code = prog(&[
+        ("S_MOV_B32", "ENC_SOP1", &[("SDST", 124), ("SSRC0", 255)], Some(0xffff)),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 255)], Some(508)),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 255)], Some(516)),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 139)], None),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 150)], None),
+        ("DS_WRITE_B32", "ENC_DS", &[("ADDR", 1), ("DATA0", 3)], None),
+        ("DS_WRITE_B32", "ENC_DS", &[("ADDR", 2), ("DATA0", 4)], None),
+        ("S_WAITCNT", "ENC_SOPP", &[("SIMM16", 0)], None),
+        ("DS_READ_B32", "ENC_DS", &[("ADDR", 1), ("VDST", 5)], None),
+        ("DS_READ_B32", "ENC_DS", &[("ADDR", 2), ("VDST", 6)], None),
+        ("S_WAITCNT", "ENC_SOPP", &[("SIMM16", 0)], None),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 8), ("SRC0", 128)], None),
+        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 9), ("SRC0", 128)], None),
+        ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 8), ("DATA", 5), ("SADDR", 0x7d)], None),
+        ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 8), ("DATA", 6), ("SADDR", 0x7d), ("OFFSET", 4)], None),
+    ]);
+    let mut descriptor = [0u8; 0x48];
+    for (i, v) in [(1u32 << 15) | (1 << 11), 1, 1, 1].into_iter().enumerate() {
+        descriptor[0x38 + 4*i..0x3c + 4*i].copy_from_slice(&v.to_le_bytes());
+    }
+    let cs = CsInterface::from_descriptor(&descriptor, 0).unwrap();
+    let opts = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 };
+    let kernel = super::kernel::translate_compute(&code, opts, &cs).unwrap();
+    let output = runner::dispatch(&kernel.dxil, [1, 1, 1], &vec![0; KERNEL_SGPRS], &[u32::MAX; 2]).unwrap();
+    assert_eq!(&output[..2], &[11, 0]);
+}

AI use: Codex (GPT-6.2 Sol) wrote and ran the GPU regression; Codex (GPT-6.2 Astra) independently checked the boundary semantics and source paths.

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:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants