From fb7b2ea96d0201e3ca08b560c4241ba794dec987 Mon Sep 17 00:00:00 2001 From: mokkamokacaffe <329663686+mokkamokacaffe@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:50:54 +0100 Subject: [PATCH] Direct DXIL: fix compute init and metadata --- crates/xcl/src/gpu/dxil/graphics_tests.rs | 7 +++-- crates/xcl/src/gpu/dxil/kernel.rs | 2 +- crates/xcl/src/gpu/dxil/module.rs | 31 +++++++++++++++++++++-- crates/xcl/src/gpu/dxil/ops_tests.rs | 15 +++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/graphics_tests.rs b/crates/xcl/src/gpu/dxil/graphics_tests.rs index d2d8f8876f..91223ad0d6 100644 --- a/crates/xcl/src/gpu/dxil/graphics_tests.rs +++ b/crates/xcl/src/gpu/dxil/graphics_tests.rs @@ -1097,8 +1097,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/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index d28a2ffcb6..fb9d78e1a1 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -37,7 +37,7 @@ pub struct KernelOptions { /// Version of the direct translator's output, part of the on-disk translation cache's /// identity (`recomp::pso`): bump it on any change that alters the emitted containers /// (the `gpu/dxil` sources are not in build.rs's translator source hash). -pub const TRANSLATOR_VERSION: u32 = 7; +pub const TRANSLATOR_VERSION: u32 = 8; /// `XCL_DIRECT_DXIL=1`: console compute shaders (and the kernel tests) go through this /// translator, with the HLSL path as the fallback for what it cannot translate yet. diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index cc37c4fa74..9d9263a831 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -1227,7 +1227,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)]), @@ -1499,6 +1499,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 33965aaf35..5a63b718ed 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() {