From 84943bffde2e8ca072c64857fff4fed014604bf6 Mon Sep 17 00:00:00 2001 From: MacieGx Date: Fri, 27 Nov 2026 23:23:04 +0100 Subject: [PATCH 1/3] Recompiler: heap-window shaders reach all 16 window slots of the heap With XCL_HEAP_WINDOWS=1 a translated program takes memory window k from descriptor MEM_HEAP_BASE + k of the shared heap, and both translators stop at the unified block's own window count (an address past it reads 0). The heap already reserves MAX_HEAP_WINDOWS (16) window slots, but only the layout's windows are written; the others were never read. Both translators now let a heap-window program select any of the 16 slots (HLSL: mem_accessors; direct DXIL: lower_mem::heap_window_bound, translator version 13), and set_window_views gives every slot past the layout's windows a null UAV, which reads 0 and drops writes, as the old bound did. The test runner's heap (run_in_arena) does the same. Nothing changes for a title: an address outside the windows still reads 0. It lets memory XCL maps into those slots later (the title's memory outside the unified block) be read without translating the shaders again. Main already gives a `(0, 0)` window and the slots up to 4 null views; this extends that to the 16 slots (set_window_views writes MAX_HEAP_WINDOWS views, the completeness rule still follows the layout). Tests: the HLSL bound for 2 / 4 / 8 / 16 windows and without heap windows, the direct bound, the null views set_window_views makes for 1, 8 and 16 windows. gpu_heap_windows_reject_out_of_uma_aliasing and gpu_heap_windows_divergent_lanes_match_switch_form read window 4 of a 2-window arena: a null slot now. Ported from the lab tree where MacieGx made the shader side reach the slots past the unified block (HLSL and direct DXIL). Co-Authored-By: CharlyRien <9572419+CharlyRien@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/core_tests.rs | 12 +++++++++ crates/xcl/src/gpu/dxil/kernel.rs | 2 +- crates/xcl/src/gpu/dxil/lower_mem.rs | 12 +++++++-- crates/xcl/src/gpu/recomp/emit.rs | 3 +++ .../xcl/src/gpu/recomp/heap_window_tests.rs | 25 +++++++++++++++++++ crates/xcl/src/gpu/recomp/runner.rs | 11 +++++--- crates/xcl/src/hle/d3d12x/console_heap.rs | 25 +++++++++++++------ 7 files changed, 75 insertions(+), 15 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index ec79716ec9..05f3c5627e 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -1468,3 +1468,15 @@ fn raw_loads_in_root_windows_respect_exec_and_bounds() { } } } + +/// With heap windows a program selects every window descriptor of the shared heap, as the +/// HLSL accessors do (`recomp::heap_window_tests::heap_windows_select_every_heap_slot`): the +/// slots past the unified block's windows hold null views. +#[test] +fn heap_windows_select_every_heap_slot() { + use super::lower_mem::heap_window_bound; + let slots = crate::gpu::uma::MAX_HEAP_WINDOWS as u32; + for mems in [2usize, 3, 4, 8, 16] { + assert_eq!(heap_window_bound(mems), slots, "{mems} unified windows"); + } +} diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 89eeaf1d8d..62f235a3fc 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -38,7 +38,7 @@ pub struct KernelOptions { /// identity (`recomp::pso`): bump it when `pso::direct_compute` / `direct_graphics` change the /// containers. An edit of `gpu/dxil` needs no bump, `XCL_DIRECT_DXIL_SOURCE_ID` is its hash /// (the `gpu/dxil` sources are not in build.rs's translator source hash). -pub const TRANSLATOR_VERSION: u32 = 12; +pub const TRANSLATOR_VERSION: u32 = 13; /// Console compute shaders (and the kernel tests) go through this translator, with the HLSL /// path as the fallback for what it cannot translate yet: the default, and `XCL_DIRECT_DXIL=1`. diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index 1196b9dab0..ce0dc5b286 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -151,6 +151,14 @@ pub struct FmtComp1 { pub dflt: Value, } +/// Windows a program with heap windows (`Lw::heap_mem`) may select: every window descriptor +/// of the shared heap (`MEM_HEAP_BASE`.., `uma::MAX_HEAP_WINDOWS`), the unified block's and the +/// null views or mapped memory after them (`console_heap::set_window_views`) – the same bound as +/// the HLSL path's accessors (`recomp::emit::mem_accessors`). +pub(crate) fn heap_window_bound(mems: usize) -> u32 { + (mems as u32).max(crate::gpu::uma::MAX_HEAP_WINDOWS as u32) +} + impl Lw { /// Runs `f` on the active memory window (`mem`, `mem1`.. at u2.., or descriptor heap /// when `self.heap_mem` is set via `XCL_HEAP_WINDOWS`), returning `out_tys.len()` values @@ -173,7 +181,7 @@ impl Lw { // Descriptor-heap windows (`#1693`, `XCL_HEAP_WINDOWS=1`): single guarded heap lookup // combined with `guard`, stacking cleanly with vector loads/stores & FormatLoad opt. if let Some(coherent) = self.heap_mem { - let windows = self.mems.len() as u32; + let windows = heap_window_bound(self.mems.len()); let w = self.rd(super::lower::VAR_WIN); let inside = self.cmpc(Pred::ULt, w, windows); let cond = match guard { @@ -413,7 +421,7 @@ impl Lw { return vec![z; n]; } let w = self.rd(super::lower::VAR_WIN); - let inside = self.cmpc(Pred::ULt, w, self.mems.len() as u32); + let inside = self.cmpc(Pred::ULt, w, heap_window_bound(self.mems.len())); let ok = match guard { Some(g) if self.s.m.const_u64(g) != Some(1) => self.and(g, inside), _ => inside, diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 19d4ae28be..a5a646ae71 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -6076,6 +6076,9 @@ fn method_switch(windows: u32, w: &str, ret: &str, name: &str, args: &str, def: fn mem_accessors(windows: u32, heap: Option<&str>) -> String { let mut s = String::new(); if let Some(coherent) = heap { + // every window descriptor of the shared heap, not only the unified block's: the slots + // past it hold null views (`console_heap::set_window_views`) or memory XCL maps there + let windows = windows.max(crate::gpu::uma::MAX_HEAP_WINDOWS as u32); // Redirect ordinary out-of-window loads/stores to null raw UAVs without a divergent // branch. Atomics keep the guard because their out parameter must be set to zero. let buf = |nul: u32| format!( diff --git a/crates/xcl/src/gpu/recomp/heap_window_tests.rs b/crates/xcl/src/gpu/recomp/heap_window_tests.rs index 40f856a7af..59e9115a27 100644 --- a/crates/xcl/src/gpu/recomp/heap_window_tests.rs +++ b/crates/xcl/src/gpu/recomp/heap_window_tests.rs @@ -552,3 +552,28 @@ fn heap_windows_graphics_stages_validate() { } } } + +/// With heap windows, the HLSL accessors let a program select every window descriptor of the +/// shared heap (`uma::MAX_HEAP_WINDOWS`), whatever its own window count: the slots past the +/// unified block's windows hold null views (`console_heap::set_window_views`), so an address +/// there reads 0 as before. Without heap windows the bound stays the program's own count. +/// (The direct path's bound: `dxil::core_tests::heap_windows_select_every_heap_slot`.) +#[test] +fn heap_windows_select_every_heap_slot() { + let e = |n: &str, enc: &str, f: &[(&str, u64)]| crate::gpu::rdna2::encode(n, enc, f, None).unwrap(); + let mut code = Vec::new(); + code.extend(e("GLOBAL_LOAD_DWORD", "ENC_FLAT_GLBL", &[("VDST", 4), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 0)])); + code.extend(e("S_ENDPGM", "ENC_SOPP", &[])); + let slots = crate::gpu::uma::MAX_HEAP_WINDOWS as u32; + for windows in [2u32, 4, 8, slots] { + let t = super::translate(&code, &super::Options { mem_windows: windows, heap_windows: true, ..Default::default() }).unwrap(); + // loads and stores select a null view past the bound, atomics branch on it + assert!(t.hlsl.contains(&format!("bool xcl_o = xcl_w >= {slots}u;")), "{windows} windows:\n{}", t.hlsl); + assert!(t.hlsl.contains(&format!("[branch] if (xcl_w >= {slots}u)")), "{windows} windows:\n{}", t.hlsl); + if windows < slots { + assert!(!t.hlsl.contains(&format!("xcl_w >= {windows}u")), "{windows} windows:\n{}", t.hlsl); + } + } + let t = super::translate(&code, &super::Options { mem_windows: 2, heap_windows: false, ..Default::default() }).unwrap(); + assert!(!t.hlsl.contains(&format!("xcl_w >= {slots}u")), "{}", t.hlsl); +} diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs index bdebc51704..8381882062 100644 --- a/crates/xcl/src/gpu/recomp/runner.rs +++ b/crates/xcl/src/gpu/recomp/runner.rs @@ -1579,8 +1579,9 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result Result, Some(&raw_window_uav(size as u64)), h); + match (bufs.get(k), arena.windows.get(k)) { + (Some(b), Some(w)) => dev.CreateUnorderedAccessView(b, None::<&ID3D12Resource>, Some(&raw_window_uav(w.1 as u64)), h), + _ => dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(crate::gpu::uma::GRAIN as u64)), h), + } } for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] { let mut h = dh.GetCPUDescriptorHandleForHeapStart(); diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 3c9dd506b8..1f63cbe026 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -597,11 +597,15 @@ pub fn window_views_failed() -> bool { /// Active windows (`bytes > 0`) in wider layouts (8 / 16) must all have a valid buffer; a slot /// without one (`(0, 0)`: a window with no memory) gets a null raw UAV – shaders read 0 there and /// their stores go nowhere. (It once aliased window 0: what was at such an address read window -/// 0's bytes, and GPU writes there overwrote its descriptor heaps.) +/// 0's bytes, and GPU writes there overwrote its descriptor heaps.) The slots past the layout's +/// windows, up to `MAX_HEAP_WINDOWS`, get null views too: shaders with heap windows may select +/// any of the 16 (`recomp::emit::mem_accessors`, `dxil::lower_mem::heap_window_bound`), and a +/// null view reads 0 and drops writes, as an address outside the windows did before. pub unsafe fn set_window_views(dev: &ID3D12Device, windows: &[(usize, usize)]) -> bool { - let count = windows.len().max(crate::gpu::uma::MAX_WINDOWS); - let complete = count <= crate::gpu::uma::MAX_WINDOWS || windows.iter().all(|&(raw, bytes)| raw != 0 || bytes == 0); - let ok = count <= crate::gpu::uma::MAX_HEAP_WINDOWS && complete && windows.first().is_some_and(|w| w.0 != 0) && shared_heap(dev) && { + let layout = windows.len().max(crate::gpu::uma::MAX_WINDOWS); + let complete = layout <= crate::gpu::uma::MAX_WINDOWS || windows.iter().all(|&(raw, bytes)| raw != 0 || bytes == 0); + let count = crate::gpu::uma::MAX_HEAP_WINDOWS; + let ok = layout <= crate::gpu::uma::MAX_HEAP_WINDOWS && complete && windows.first().is_some_and(|w| w.0 != 0) && shared_heap(dev) && { let (device, cpu, inc) = { let g = GLOBAL.lock().unwrap(); let s = g.as_ref().unwrap(); @@ -626,7 +630,7 @@ pub unsafe fn set_window_views(dev: &ID3D12Device, windows: &[(usize, usize)]) - }; if !ok { WINDOW_VIEWS_FAILED.store(true, std::sync::atomic::Ordering::Relaxed); - crate::xlog_warn!("memory windows: could not bind {count} window(s) in the shared descriptor heap"); + crate::xlog_warn!("memory windows: could not bind {} window(s) in the shared descriptor heap", windows.len()); } ok } @@ -1877,10 +1881,15 @@ mod tests { next: crate::gpu::recomp::MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32, free: Vec::new(), retired: VecDeque::new(), nulls: None, leases: PublishLeases::default(), }); let window = (&mut resource as *mut _ as usize, 0x10000); - // the root-UAV slots past the windows there are: null views, not window 0 again + // the layout's windows, then null views up to MAX_HEAP_WINDOWS (not window 0 again) + let heap_slots = crate::gpu::uma::MAX_HEAP_WINDOWS as u32; + let all = (1u32 << heap_slots) - 1; assert!(set_window_views(&dev, &[window])); - assert_eq!(CALLS.swap(0, Ordering::Relaxed), crate::gpu::uma::MAX_WINDOWS as u32); - assert_eq!(NULLS.swap(0, Ordering::Relaxed), 0b1110); + assert_eq!(CALLS.swap(0, Ordering::Relaxed), heap_slots); + assert_eq!(NULLS.swap(0, Ordering::Relaxed), all & !0b1); + assert!(set_window_views(&dev, &[window; 8])); + assert_eq!(CALLS.swap(0, Ordering::Relaxed), heap_slots); + assert_eq!(NULLS.swap(0, Ordering::Relaxed), all & !0xff); let mut windows = [window; crate::gpu::uma::MAX_HEAP_WINDOWS]; assert!(set_window_views(&dev, &windows)); assert_eq!(CALLS.swap(0, Ordering::Relaxed), windows.len() as u32); From 4dd33fc1f9f4b274aef66d44f2f368af4d732ae4 Mon Sep 17 00:00:00 2001 From: CharlyRien <9572419+CharlyRien@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:35:21 +0100 Subject: [PATCH 2/3] Recompiler: heap window slots - test runner and test follow review - runner::dispatch_with (the backend-hook PSOs of gpu_compute_e2e): heap slots 0..MAX_WINDOWS stay views of `mem`, as the root form's four windows, and the slots past them are null views, as run_in_arena and the runtime. - heap_windows_select_every_heap_slot: the case without heap windows now checks the switch over the program's own windows (case 1 present, case 2 absent, no descriptor heap), instead of an assertion that held by construction. From an independent review of the PR. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/heap_window_tests.rs | 4 +++- crates/xcl/src/gpu/recomp/runner.rs | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/heap_window_tests.rs b/crates/xcl/src/gpu/recomp/heap_window_tests.rs index 59e9115a27..9bbdf0a99a 100644 --- a/crates/xcl/src/gpu/recomp/heap_window_tests.rs +++ b/crates/xcl/src/gpu/recomp/heap_window_tests.rs @@ -574,6 +574,8 @@ fn heap_windows_select_every_heap_slot() { assert!(!t.hlsl.contains(&format!("xcl_w >= {windows}u")), "{windows} windows:\n{}", t.hlsl); } } + // the root-UAV form: a switch over the program's own 2 windows, nothing past them let t = super::translate(&code, &super::Options { mem_windows: 2, heap_windows: false, ..Default::default() }).unwrap(); - assert!(!t.hlsl.contains(&format!("xcl_w >= {slots}u")), "{}", t.hlsl); + assert!(!t.hlsl.contains("ResourceDescriptorHeap[") && t.hlsl.contains("case 1u: return mem1."), "{}", t.hlsl); + assert!(!t.hlsl.contains("case 2u:"), "{}", t.hlsl); } diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs index 8381882062..5aebad0fa2 100644 --- a/crates/xcl/src/gpu/recomp/runner.rs +++ b/crates/xcl/src/gpu/recomp/runner.rs @@ -1856,7 +1856,9 @@ fn dispatch_with( let bound = bind_resources(dev, &list, &[], &[])?; // A PSO built by the backend's hook uses the runtime's window count: with heap windows // (`Options::heap_windows`) its accesses go to ResourceDescriptorHeap[MEM_HEAP_BASE + w], - // so every window slot is a raw view of `mem` (no textures here to share the heap with). + // so the root form's windows (MAX_WINDOWS, all `mem` below) are raw views of `mem` and the + // slots past them null views, as `console_heap::set_window_views` (no textures here to + // share the heap with). let win_count = crate::gpu::uma::MAX_HEAP_WINDOWS; let dh: ID3D12DescriptorHeap = dev .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { @@ -1870,7 +1872,11 @@ fn dispatch_with( for k in 0..win_count { let mut h = dh.GetCPUDescriptorHandleForHeapStart(); h.ptr += (super::MEM_HEAP_BASE as usize + k) * inc; - dev.CreateUnorderedAccessView(&mem_buf, None::<&ID3D12Resource>, Some(&raw_window_uav(mem_bytes)), h); + if k < crate::gpu::uma::MAX_WINDOWS { + dev.CreateUnorderedAccessView(&mem_buf, None::<&ID3D12Resource>, Some(&raw_window_uav(mem_bytes)), h); + } else { + dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(crate::gpu::uma::GRAIN as u64)), h); + } } for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] { let mut h = dh.GetCPUDescriptorHandleForHeapStart(); From 370e1c76f0baf41b02e2147b7a436fe1059e5ec9 Mon Sep 17 00:00:00 2001 From: CharlyRien <9572419+CharlyRien@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:29:42 +0100 Subject: [PATCH 3/3] Tests: the expanded-layout heap-window test checks the 16-slot bound gpu::uma::tests::expanded_heap_window_shaders_translate_and_validate asserted that the HLSL heap accessors range-check against the program's own window count (8); with the previous commits a heap-window program selects every heap window slot, so the bound is 16 for 8 and 16 windows alike. Found by the full suite on this PR's head. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/uma.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/uma.rs b/crates/xcl/src/gpu/uma.rs index e4e34f0166..fc676a91fb 100644 --- a/crates/xcl/src/gpu/uma.rs +++ b/crates/xcl/src/gpu/uma.rs @@ -1692,7 +1692,9 @@ mod tests { // HLSL path accepts 8/16 with heap_windows, rejects without. assert!(recomp::translate(&code, &Options { mem_windows: w, heap_windows: false, ..Default::default() }).is_err()); let t = recomp::translate(&code, &Options { mem_windows: w, heap_windows: true, ..Default::default() }).unwrap(); - assert!(t.hlsl.contains(&format!("xcl_w >= {w}u")), "HLSL must range-check against {w} windows"); + // heap-window programs select every heap window slot (null views past the layout's) + let slots = MAX_HEAP_WINDOWS as u32; + assert!(t.hlsl.contains(&format!("xcl_w >= {slots}u")), "HLSL must range-check against the {slots} heap window slots ({w} windows)"); assert!(!t.hlsl.contains("mem4"), "root signature ABI stays at 4 root UAVs"); // Direct DXIL path validates cleanly with 8/16 | HEAP_WINDOWS. translate(&code, KernelOptions { wave64: true, split: false, mem_windows: w | HEAP_WINDOWS, sampler_base: 0 }).unwrap();