diff --git a/crates/xcl/src/gpu/dxil/graphics_tests.rs b/crates/xcl/src/gpu/dxil/graphics_tests.rs index c8f3802053..47429aa87d 100644 --- a/crates/xcl/src/gpu/dxil/graphics_tests.rs +++ b/crates/xcl/src/gpu/dxil/graphics_tests.rs @@ -1140,8 +1140,11 @@ fn groupshared_bytes_follow_the_16bit_layout() { let mut s = Shader::new(Stage::Mesh, (6, 6)); let (i16, f16) = (s.m.int(16), s.m.f16()); let (a, b) = (s.m.array(6, i16), s.m.array(3, f16)); - s.m.global("g", a, 3, None, false, 2); - s.m.global("h", b, 3, None, false, 2); + let (g, h) = (s.m.global("g", a, 3, None, false, 2), s.m.global("h", b, 3, None, false, 2)); + let unused = s.m.array(100, i16); + s.m.global("unused", unused, 3, None, false, 2); + s.m.load(g, 2); + s.m.load(h, 2); assert_eq!(s.m.global_bytes_layout(3, true), 18); assert_eq!(s.m.global_bytes(3), 36); } diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index f2930cd7e0..6410cce08c 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -1237,7 +1237,7 @@ impl Module { Op::Alloca(t, n, align) => { // [instty, opty, size (absolute value id), align record (log2 + 1 | explicit type)] let i32t = self.value_ty(*n); - let a = align.trailing_zeros() as u64 + 1; + let a = if *align == 0 { 0 } else { align.trailing_zeros() as u64 + 1 }; w.record(FUNC_CODE_INST_ALLOCA, &[t.0 as u64, i32t.0 as u64, abs(*n), a | 1 << 6]) } Op::Br(b) => w.record(FUNC_CODE_INST_BR, &[bb(*b)]), @@ -1521,6 +1521,33 @@ impl Module { } /// [`Module::global_bytes`] under the native 16-bit layout when `native16`. pub fn global_bytes_layout(&self, addrspace: u32, native16: bool) -> u64 { - self.globals.iter().filter(|g| g.addrspace == addrspace).map(|g| self.alloc_size_layout(g.ty, native16)).sum() + let (order, _) = self.block_layout(); + let mut used = vec![false; self.globals.len()]; + let mut mark = |v: Value| { + if let Value::Global(g) = v { + used[g as usize] = true; + } + }; + for &b in &order { + for &i in &self.blocks[b] { + self.insts[i as usize].op.for_each_operand(&mut mark); + } + } + for c in &self.consts { + if let ConstDef::Aggregate(_, e) = c { + e.iter().copied().for_each(&mut mark); + } + } + for m in &self.md { + if let MdDef::Val(_, v) = m { + mark(*v); + } + } + self.globals + .iter() + .zip(used) + .filter(|(g, used)| *used && g.addrspace == addrspace) + .map(|(g, _)| self.alloc_size_layout(g.ty, native16)) + .sum() } } diff --git a/crates/xcl/src/gpu/dxil/ops_tests.rs b/crates/xcl/src/gpu/dxil/ops_tests.rs index 50145822b7..5a09f3dd39 100644 --- a/crates/xcl/src/gpu/dxil/ops_tests.rs +++ b/crates/xcl/src/gpu/dxil/ops_tests.rs @@ -71,6 +71,21 @@ fn check(c: &[u8]) { } } +#[test] +fn default_aligned_alloca_validates_without_inalloca() { + let mut s = Shader::new(Stage::Compute, (6, 6)); + let out = s.raw_uav(1, 0); + let i32 = s.m.i32(); + let p = s.m.alloca(i32, 0); + let seven = s.m.i32c(7); + s.m.store(p, seven, 0); + let v = s.m.load(p, 0); + let zero = s.m.i32c(0); + s.raw_store(out, zero, &[v]); + let c = s.finish([1, 1, 1], Some(32)); + check(&c); +} + /// Runs a validated kernel; `None` when there is no wave32 GPU. fn gpu(c: &[u8], mem: &[u32], tex: &[runner::TestTexture], smp: &[runner::TestSampler]) -> Option { match runner::wave32_supported() {