From 2d3e3fe111598096165b7ec2a41e1fcc788cc91d Mon Sep 17 00:00:00 2001 From: KDenKC <125705260+KDenKC@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:52:13 -0700 Subject: [PATCH] Recompiler: flat heap reads outside the windows take the null view XCL_HEAP_FLAT=1 reads a compute program's heap memory windows without a branch (2f523330): the access runs for every lane, and a lane outside the windows or under a false guard (a buffer index past num_records, an EXEC- masked lane) had its value set to zero afterwards. But its access went to window 0's descriptor, at the address it was given, so it read window 0's memory at an address the program never reaches: the guarded form branches around it, and the HLSL accessors (409cf071) send it to the null view MEM_NULL_LOAD. The direct form now selects MEM_NULL_LOAD for it as well, so it touches no window's memory. On the RTX 5090, the retail title with XCL_HEAP_FLAT=1 hung on its first world frame (DRED: a page fault in a compute dispatch); with it off it ran past 200 s. Test: raw_loads_in_root_windows_are_branchless checks that the flat form selects the null-load view. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/core_tests.rs | 3 +++ crates/xcl/src/gpu/dxil/lower_mem.rs | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index e1421c69bc..07f15fe4c7 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -1458,6 +1458,9 @@ fn raw_loads_in_root_windows_are_branchless() { if let (Ok(heap), Ok(flat)) = (super::validate::disassemble(&heap_dxil), super::validate::disassemble(&flat_dxil)) { let branches = |s: &str| s.lines().filter(|line| line.trim_start().starts_with("br i1 ")).count(); assert!(branches(&flat) < branches(&heap), "flat heap reads must remove guarded-read branches: guarded {}, flat {}", branches(&heap), branches(&flat)); + // a flat read outside the windows or under a false guard takes the null view, not window 0 + let null = format!(", i32 {}", crate::gpu::recomp::MEM_NULL_LOAD); + assert!(flat.lines().any(|l| l.contains("select i1") && l.contains(&null)), "flat heap reads must select the null-load view (descriptor {}):\n{flat}", crate::gpu::recomp::MEM_NULL_LOAD); } let writes = prog(&[ diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index dbe002c217..18507e7a92 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -416,11 +416,15 @@ impl Lw { let carry_case = high.filter(|&(_, hi)| matches!(hi, Value::Inst(_))).map(|(_, hi)| (hi, self.high_one_slot())); let inside = self.cmpc(Pred::ULt, w, self.mems.len() as u32); let ok = if let Some(g) = guard { self.and(g, inside) } else { inside }; - let safe = self.sel(ok, w, z); - let i = self.addc(safe, crate::gpu::recomp::MEM_HEAP_BASE); + // (an access outside the windows or under a false guard still runs here, without a + // branch: it reads the null view, as the HLSL accessors do, never a window's memory. + // The optimizer's carry rewrite of `inside` and of this add keeps that: `FlatRead`) + let i = self.addc(w, crate::gpu::recomp::MEM_HEAP_BASE); if let (Some((hi, (valid1, slot1))), Value::Inst(test), Value::Inst(slot)) = (carry_case, inside, i) { self.s.m.flat_reads.push(super::module::FlatRead { hi, test, slot, valid1, slot1 }); } + let null = self.c(crate::gpu::recomp::MEM_NULL_LOAD); + let i = self.sel(ok, i, null); let m = self.s.heap_raw_buffer_uav(i, coherent); let vals = f(self, m); debug_assert_eq!(vals.len(), n);