From 409cf07107d6a281d0415df9a26396daf40738a9 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 22:19:52 -0800 Subject: [PATCH] Recompiler: make heap-window load and store guards branchless Reserve null raw-UAV descriptors after the memory-window slots. Redirect ordinary out-of-layout loads and stores with a descriptor select, while retaining explicit guards for atomics whose return values require defined handling. Keep runtime and GPU-test heaps on the same descriptor ABI and add a deterministic accessor-shape test. Adapted as the behavior-independent half of #2554; its upstream RTX 4090 investigation attributes about six seconds of a large cold compile to the duplicated guard branches. Locally cross-checked with the GNU Windows target; Windows GPU execution remains untested here. Assisted-by: Claude Code (model not disclosed) Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/gpu/recomp/emit.rs | 41 ++++++++++++++++++----- crates/xcl/src/gpu/recomp/mod.rs | 5 +++ crates/xcl/src/gpu/recomp/runner.rs | 14 ++++++-- crates/xcl/src/hle/d3d12x/console_heap.rs | 23 ++++++++++--- 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index bc0a56cf25..8b32f1eb35 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -5832,15 +5832,28 @@ 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 { - // one access: window k is descriptor MEM_HEAP_BASE + k; an `xcl_w` outside the windows - // does what the switch's `default` does + // 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!( + "{{ bool xcl_o = xcl_w >= {windows}u; {coherent}RWByteAddressBuffer m = ResourceDescriptorHeap[NonUniformResourceIndex(select(xcl_o, {nul}u, {}u + xcl_w))]; ", + super::MEM_HEAP_BASE + ); let _ = writeln!(s, "#define XCL_MW {coherent}RWByteAddressBuffer m = ResourceDescriptorHeap[NonUniformResourceIndex({}u + xcl_w)]", super::MEM_HEAP_BASE); - let _ = writeln!(s, "uint4 xcl_loadx4(uint4 a) {{ [branch] if (xcl_w >= {windows}u) {{ return uint4(0u, 0u, 0u, 0u); }} XCL_MW; return uint4(m.Load(a.x), m.Load(a.y), m.Load(a.z), m.Load(a.w)); }}"); + let _ = writeln!(s, "uint4 xcl_loadx4(uint4 a) {}return uint4(m.Load(a.x), m.Load(a.y), m.Load(a.z), m.Load(a.w)); }}", buf(super::MEM_NULL_LOAD)); for (ret, name, params, args, def) in mem_methods() { - let r = if ret == "void" { "" } else { "return " }; - // (the switch's `break` leaves the accessor) - let outside = def.replace("break;", "return;"); - let _ = writeln!(s, "{ret} xcl_m{name}({params}) {{ [branch] if (xcl_w >= {windows}u) {{ {outside} }} XCL_MW; {r}m.{name}({args}); }}"); + if name.starts_with("Load") || name.starts_with("Store") { + let nul = if name.starts_with("Store") { super::MEM_NULL_STORE } else { super::MEM_NULL_LOAD }; + let call = if name.starts_with("Store") { + format!("m.{name}(a, {});", &args[3..]) + } else { + format!("return m.{name}(a);") + }; + let _ = writeln!(s, "{ret} xcl_m{name}({params}) {}{}}}", buf(nul), call); + } else { + let r = if ret == "void" { "" } else { "return " }; + let outside = def.replace("break;", "return;"); + let _ = writeln!(s, "{ret} xcl_m{name}({params}) {{ [branch] if (xcl_w >= {windows}u) {{ {outside} }} XCL_MW; {r}m.{name}({args}); }}"); + } } return s; } @@ -6719,7 +6732,19 @@ uint fclass64(double f) { #[cfg(test)] mod tests { - use super::{PRELUDE, PRELUDE_MEM}; + use super::{mem_accessors, PRELUDE, PRELUDE_MEM}; + + #[test] + fn heap_window_loads_and_stores_use_null_descriptors_without_branches() { + let h = mem_accessors(4, Some("globallycoherent ")); + let load = format!("select(xcl_o, {}u,", super::super::MEM_NULL_LOAD); + let store = format!("select(xcl_o, {}u,", super::super::MEM_NULL_STORE); + assert!(h.contains(&load), "load accessor did not select the null-load UAV:\n{h}"); + assert!(h.contains(&store), "store accessor did not select the null-store UAV:\n{h}"); + assert!(h.contains("xcl_mInterlockedAdd(uint a, uint v, out uint o) { [branch] if"), "atomic guard was removed:\n{h}"); + assert!(!h.contains("xcl_mLoad(uint a) { [branch]"), "ordinary load kept a divergent guard:\n{h}"); + assert!(!h.contains("xcl_mStore(uint a, uint v) { [branch]"), "ordinary store kept a divergent guard:\n{h}"); + } #[test] fn format_helpers_select_uint4_components_without_runtime_indexing() { diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 6f7e1a1440..b5c1a14237 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -81,6 +81,11 @@ mod link_tests; /// (`console_heap::null_views`). pub const MEM_HEAP_BASE: u32 = 4; +/// Null raw-UAV descriptors just after all window slots. Heap-window loads outside the active +/// layout read zero from `MEM_NULL_LOAD`; stores are redirected to `MEM_NULL_STORE` and dropped. +pub const MEM_NULL_LOAD: u32 = MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32; +pub const MEM_NULL_STORE: u32 = MEM_NULL_LOAD + 1; + /// Flag in a memory-windows count (`pso::mem_windows`, the translation caches' keys): /// `Options::heap_windows`. `translate` takes it out of `mem_windows`. pub const HEAP_WINDOWS: u32 = 0x100; diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs index 34b5c65201..f738b41485 100644 --- a/crates/xcl/src/gpu/recomp/runner.rs +++ b/crates/xcl/src/gpu/recomp/runner.rs @@ -1532,7 +1532,7 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result Result, Some(&raw_window_uav(size as u64)), h); } + for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] { + let mut h = dh.GetCPUDescriptorHandleForHeapStart(); + h.ptr += idx as usize * inc; + dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(4)), h); + } list.SetDescriptorHeaps(&[Some(dh.clone())]); list.SetComputeRootSignature(&gpu.rs); list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress()); @@ -1783,7 +1788,7 @@ fn dispatch_with( let dh: ID3D12DescriptorHeap = dev .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, - NumDescriptors: super::MEM_HEAP_BASE + win_count as u32, + NumDescriptors: (super::MEM_HEAP_BASE + win_count as u32).max(super::MEM_NULL_STORE + 1), Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, NodeMask: 0, }) @@ -1794,6 +1799,11 @@ fn dispatch_with( h.ptr += (super::MEM_HEAP_BASE as usize + k) * inc; dev.CreateUnorderedAccessView(&mem_buf, None::<&ID3D12Resource>, Some(&raw_window_uav(mem_bytes)), h); } + for idx in [super::MEM_NULL_LOAD, super::MEM_NULL_STORE] { + let mut h = dh.GetCPUDescriptorHandleForHeapStart(); + h.ptr += idx as usize * inc; + dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(4)), h); + } list.SetDescriptorHeaps(&[Some(dh.clone()), Some(bound._heaps[1].clone())]); list.SetComputeRootSignature(rs); list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress()); diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 81ce117ed0..cd78056644 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -169,16 +169,18 @@ pub unsafe fn shared_heap(dev: &ID3D12Device) -> bool { } }; crate::xlog_info!("console descriptor heaps: one shader-visible PC heap of {cap} view descriptors for all of them"); - null_views(dev, heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)); + let inc = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + null_views(dev, heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, inc); + null_window_views(dev, heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, inc); *g = Some(Global { dev: dev.clone(), cpu: heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, gpu: heap.GetGPUDescriptorHandleForHeapStart().ptr, - inc: dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV), + inc, heap, cap, - // the memory windows' raw UAVs (`set_window_views`) come first - next: crate::gpu::recomp::MEM_HEAP_BASE + crate::gpu::uma::MAX_HEAP_WINDOWS as u32, + // the memory windows and their two out-of-range null UAVs come first + next: crate::gpu::recomp::MEM_NULL_STORE + 1, free: Vec::new(), }); } @@ -210,6 +212,19 @@ unsafe fn null_views(dev: &ID3D12Device, cpu: u64, inc: u32) { } } +/// Null raw UAVs used by heap-window accessors for out-of-layout loads and stores. +unsafe fn null_window_views(dev: &ID3D12Device, cpu: u64, inc: u32) { + for idx in [crate::gpu::recomp::MEM_NULL_LOAD, crate::gpu::recomp::MEM_NULL_STORE] { + let at = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: (cpu + idx as u64 * inc as u64) as usize }; + dev.CreateUnorderedAccessView( + None::<&ID3D12Resource>, + None::<&ID3D12Resource>, + Some(&crate::gpu::recomp::runner::raw_window_uav(4)), + at, + ); + } +} + static WINDOW_VIEWS_FAILED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); /// Whether the memory windows could not be put in the shared heap (`set_window_views`):