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);