Repository navigation
Conversation
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>
1393186 to
39f4df5
Compare
|
I reran the 512-byte LDS boundary case against head A native wave32 kernel declares 128 LDS dwords (512 bytes), sets M0 to The source cause is 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, 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. |
bmjask
left a comment
There was a problem hiding this comment.
Review of 39f4df54: 2 finding(s). 2 are inline.
These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
| self.xw = Some((g, words - 1)); | ||
| } else if fits { | ||
| // `lds_index` clamps below the translator's words (`lds_top` < 8191) | ||
| self.lds_top = declared - 1; |
There was a problem hiding this comment.
In the native (non-split) declared layout, lds_top = declared - 1 makes lds_index clamp any out-of-range LDS access onto the program's last declared dword. Out-of-range writes now overwrite real data there and out-of-range reads return it. On RDNA such writes are dropped and reads return 0, and on main they landed in unused space of the 8,192-dword block. Take a wave32 program that declares 1 granule (128 dwords) and keeps a live value in dword 127. A lane writing past the end at dword 256 is clamped to 127 and replaces it, and a ds_read_b64 at dword 127 returns d127 twice instead of (d127, 0). Guarding the access (skip the store, return 0) instead of clamping would match the hardware. The PR lists this under Not tested.
| self.xw = Some((g, words - 1)); | ||
| } | ||
| } else { | ||
| self.lds_top = if safe_bar { 8188 } else { 8191 }; |
There was a problem hiding this comment.
When the declared LDS plus the translator's words don't fit (e.g. a full 64-granule / 8,192-dword declaration), the code falls back to 8,192 dwords with lds_top = 8188 (safe barrier) or the split base, so the program's own top declared dwords clamp onto one shared index or overlap XCL_WT. A native wave32 group of 256 threads with s_barrier and LDS_SIZE=64 gets fits false and lds_top = 8188. Its writes to dwords 8189/8190/8191 all go to 8188, and a later read of 8190 returns whichever was written last. These lines are older, but the new fits logic detects the condition and keeps the aliasing layout. Moving the translator's words to a separate small groupshared array, rather than the program's own LDS, would avoid it.
The declared-LDS layout clamped a program's LDS index to its last declared dword (umin(i, declared - 1)), so a store past the declared size overwrote that dword and a read past it returned it. RDNA2 drops such a store and reads 0 (thehollowedone's 512-byte case on xcl-dev#3478: [22, 22] here, [11, 22] on main, [11, 0] on the console GPU). The block now has a sink word right after the declared LDS (the translator's words move up by one; a declaration with no room for it keeps the 8,192-dword layout, as one with no room for those words did). lds_index takes the byte address's dword index before any 32 KB wrap and sends one at or past the declared size to the sink, with a flag: a store or atomic there has no effect on the program's LDS, and a read, a returning atomic and each dword of a wide or paired read give 0 for it. Each address of a paired instruction is checked on its own. The HLSL path keeps the 8,192-dword block and is unchanged. Tests (GPU, one wave32 thread, 128 declared dwords): thehollowedone's regression, a ds_write2_b32 / ds_read2_b32 pair across the end, and reads past it (the second dword of a ds_read_b64, an address that wraps onto the last dword in 32 KB, a ds_add_rtn_u32's return). Without the change they give [22, 22], [22, 22, 22, 22] and [11, 11, 11, 11, 22]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Fixed in f33cb35, for the out-of-bounds finding above. The declared layout now has a sink word right after the program's LDS, and the translator's words move up by one. New tests in
Full suite: 2896 passed, 0 failed, 51 ignored, 27 SKIPPED (23 of them AI-assisted: Claude Code (Claude Opus 5.5), posted by Foniass. |
bmjask
left a comment
There was a problem hiding this comment.
Review of f33cb350: 2 finding(s).
Re-review at f33cb350. Accesses past the declared LDS now go to a sink (reads 0, writes dropped), which fixes the clamp-onto-the-last-dword problem from my earlier review. The reviewer traced the layout for split/native, with and without the safe barrier, and found it consistent. Two new points: 2 are inline.
These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
| /// 0 (`lds_in`), as on RDNA, which drops such accesses. | ||
| fn lds_index(&mut self, at: Value) -> (Value, Option<Value>) { | ||
| let i = self.lshrc(at, 2); | ||
| if let Some(sink) = self.lds_sink { |
There was a problem hiding this comment.
Out-of-bounds LDS behaviour now depends on the translator. The direct path sends accesses past the declared LDS to the sink (reads give 0), while the HLSL fallback still wraps into the full 8,192-dword block and returns real data. A program that reads or writes past its declared LDS reads 0 when translated directly (the default), but reads stored data when it falls back to HLSL (an unsupported instruction, XCL_DIRECT_DXIL=0, or split real LDS with XCL_X_LDS_ORDER=0). The same shader gives different results by route, which is hard to debug. Applying the same bound in emit.rs would make them agree.
| tgid: [(r >> 7) & 1 == 1, (r >> 8) & 1 == 1, (r >> 9) & 1 == 1], | ||
| tg_size: (r >> 10) & 1 == 1, | ||
| tid_components: ((r >> 11) & 3) + 1, | ||
| lds_dwords: ((r >> 15) & 0x1FF) * 128, |
There was a problem hiding this comment.
Not certain about this one: on by default, the static LDS_SIZE from the FSSM/FSPX descriptor's COMPUTE_PGM_RSRC2 is taken as the program's whole LDS, with nothing to check that the console didn't allocate more at dispatch (dynamic LDS, or driver FSPX containers whose RSRC2 undercounts). If a title's real allocation is larger than the header value, accesses above it now silently store nothing and read 0, where before they used the 32 KB block. That gives wrong compute results with no diagnostic, and the PR says it doesn't know how often this happens. A once-per-program log when an access hits the sink would make it visible.
|
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any. |
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:
A sink word sits between the program's LDS and the translator's words: a program access past the declared size goes there, so a store has no effect on the program's LDS and a read gives 0, as on RDNA2 (second commit, after thehollowedone's out-of-bounds finding). A header with no size, or one too large to leave room for the sink and those words, keeps the 8,192-dword layout. Mesh, geometry and the HLSL path are unchanged. On by default;
XCL_DECLARED_LDS=0brings back the 8,192-dword layout. The disk caches andComputePolicyKeykeep the two apart.This combines two earlier versions:
CsInterface::lds_dwords, the declared layout, theComputePolicyKeyfield, the clamp condition.5e488781(on by default there): the safe barrier's placement and the GPU test.The first commit credits both as co-authors.
Kalmarv's numbers on #2937:
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 clampcrates/xcl/src/gpu/recomp/pso.rs,hle/d3d12x/real/compute_build_key.rs: cache identities (xcl-declared-lds-0when off) andComputePolicyKey.declared_ldsdeclared_lds_accesses_stop_below_the_translators_words(disassembly: block size, accesses past the end sent to the sink word 128, translator words at 129..131 / split 153..155);Recompiler: drop direct compute LDS accesses past the declared block(lanes.rs,lower.rs,lower_mem.rs,kernel.rs): the first commit clamped an access past the declared LDS onto its last dword, so an out-of-range store overwrote it. Nowsetup_storageputs a sink word after the declared LDS, andlds_indexsends any dword index at or past the declared size to the sink (checked before the 32 KB wrap) with an in-bounds flag. Stores and atomics there leave the program's LDS alone. Reads, returning atomics and each dword of a wide or paired access give 0. Each address of a paired instruction is checked on its own. New GPU tests incore_tests.rs, one wave32 thread with 128 declared dwords:gpu_compute_declared_lds_drops_out_of_bounds_store;gpu_compute_declared_lds_checks_each_address_of_a_pair(ds_write2_b32/ds_read2_b32across the end);gpu_compute_declared_lds_reads_zero_past_its_end(ds_read_b64at the last dword, an address that wraps onto it in 32 KB, ads_add_rtn_u32past the end).Tested
cargo test --workspace --no-fail-fast -- --test-threads=4: 2896 passed, 0 failed, 51 ignored. SKIPPED: 27 notices (23no wave64, 2 no heap windows, 1 no D3D12 debug layer, 1 driver cache needs Developer Mode). The two runs before it ended early when the lib test binary crashed ingpu::agility::tests::create_device_sets_dred_first, inside the NVIDIA driver. That is a known intermittent crash on this machine, and the test is unrelated to this change.[22, 22]instead of[11, 0],[22, 22, 22, 22]instead of[11, 0, 0, 11],[11, 11, 11, 11, 22]instead of[11, 0, 0, 0, 11]. With it, all three pass.XCL_ADAPTER=radeon), AMD Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug tests, release smokecargo test -p xcl --lib gpu::dxil: 283 passed, 0 failed, 3 ignored, on each of:XCL_SPLIT_WAVE64=1.gpu::recomp+compute_build_key: 508 passed, 0 failed, 21 ignored on each of the same three. WithXCL_DECLARED_LDS=0(5070 Ti),gpu::dxil+declared_lds: 284 passed, 0 failed.cargo test --workspace --no-fail-fast -- --test-threads=4on 39f4df5, rebased on main 86e86fc (5070 Ti): 2893 passed, 0 failed, 51 ignored. SKIPPED: 29 notices, almost allno 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.Regression run
main:
this PR:
Not tested
HANDOFF.mdline 106 ("groupshared, 32 KB = the D3D12 limit") is no longer true for direct computeAI use
5e488781on Recompiler: direct DXIL LDS accesses stop below the safe barrier's words #3468, wrote the extra tests, ran the tests on both GPUs and the smoke runs, wrote this description.5e488781.🤖 Generated with Claude Code