From 045a439ab37b27014e5b833a3edf1de3e118134b Mon Sep 17 00:00:00 2001 From: luckyteagirl <290248872+luckyteagirl@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:04:18 -0800 Subject: [PATCH] Direct DXIL: include write address and control in signature dependencies Track each memory write's pointer, value, and control block when computing input-to-output dependencies for dx.viewIdState and PSV0. Preserve the fixed-point traversal merged in #1966. The direct-DXIL source hash from #1947 accounts for the changed output without a manual version bump. Refs #1892 --- crates/xcl/src/gpu/dxil/graphics.rs | 35 +++++----- crates/xcl/src/gpu/dxil/graphics_tests.rs | 80 ++++++++++++++++++++++- crates/xcl/src/gpu/dxil/module.rs | 8 ++- 3 files changed, 99 insertions(+), 24 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/graphics.rs b/crates/xcl/src/gpu/dxil/graphics.rs index 239a923ea9..bb913c80b2 100644 --- a/crates/xcl/src/gpu/dxil/graphics.rs +++ b/crates/xcl/src/gpu/dxil/graphics.rs @@ -751,22 +751,16 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac ctrl[v].push(a); } } - // Instruction → block. - let mut block_of: HashMap = HashMap::new(); - let mut root_stores: HashMap> = HashMap::new(); + // A load depends on every write to its root, including each write's address + // and the branch that controls whether it happens. + let mut root_stores: HashMap, usize)>> = HashMap::new(); for b in 0..nb { for &i in m.block_insts(Block(b as u32)) { - block_of.insert(i, b); if let InstInfo::Store(p, v) = m.inst_info(i) { - root_stores.entry(root(s, p)).or_default().push(v); + root_stores.entry(root(s, p)).or_default().push((vec![p, v], b)); } - if let InstInfo::Other(ops) = m.inst_info(i) { - // atomics write memory too - if ops.len() >= 2 { - if let Value::Global(_) = root(s, ops[0]) { - root_stores.entry(root(s, ops[0])).or_default().extend_from_slice(&ops[1..]); - } - } + if let InstInfo::Atomic(ops) = m.inst_info(i) { + root_stores.entry(root(s, ops[0])).or_default().push((ops, b)); } } } @@ -817,10 +811,9 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac struct Cx<'a> { s: &'a Shader, - block_of: &'a HashMap, ctrl: &'a Vec>, cond: &'a Vec>, - root_stores: &'a HashMap>, + root_stores: &'a HashMap, usize)>>, in_base: &'a [Option], inputs: &'a [Placed], } @@ -882,24 +875,26 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac } InstInfo::Load(p) => { d.extend(value_deps(cx, p, st)); - if let Some(vs) = cx.root_stores.get(&root(cx.s, p)) { - for &x in vs { - d.extend(value_deps(cx, x, st)); + if let Some(writes) = cx.root_stores.get(&root(cx.s, p)) { + for (operands, block) in writes { + for &x in operands { + d.extend(value_deps(cx, x, st)); + } + d.extend(block_deps(cx, *block, st)); } } } - InstInfo::Other(ops) => { + InstInfo::Atomic(ops) | InstInfo::Other(ops) => { for x in ops { d.extend(value_deps(cx, x, st)); } } InstInfo::Store(..) | InstInfo::Term(..) => {} } - let _ = cx.block_of; Sets::close(&mut st.values, v, d, st.pass, &mut st.grew) } - let cx = Cx { s, block_of: &block_of, ctrl: &ctrl, cond: &cond, root_stores: &root_stores, in_base: &in_base, inputs }; + let cx = Cx { s, ctrl: &ctrl, cond: &cond, root_stores: &root_stores, in_base: &in_base, inputs }; let mut out: HashMap<(u8, u32), BTreeSet> = HashMap::new(); let bases = |p: &[Placed]| -> Vec> { p.iter().map(|e| e.at.map(|(r, c)| r * 4 + c as u32)).collect() }; let (ob, pb) = (bases(outputs), bases(prims)); diff --git a/crates/xcl/src/gpu/dxil/graphics_tests.rs b/crates/xcl/src/gpu/dxil/graphics_tests.rs index c8f3802053..492ad83b4c 100644 --- a/crates/xcl/src/gpu/dxil/graphics_tests.rs +++ b/crates/xcl/src/gpu/dxil/graphics_tests.rs @@ -3,7 +3,7 @@ //! validator on every emitted shader, and draws on the GPU. use super::*; -use crate::gpu::dxil::module::{BinOp, CastOp, Pred}; +use crate::gpu::dxil::module::{BinOp, CastOp, Pred, RmwOp}; use crate::gpu::dxil::validate; pub(super) fn part<'a>(c: &'a [u8], cc: &[u8; 4]) -> Option<&'a [u8]> { @@ -828,6 +828,84 @@ fn store_all(io: &mut GraphicsIo, s: &mut Shader, stores: &[(Sig, u8, Value)]) { } } +#[test] +fn store_dependency_includes_the_address_input() { + let mut s = Shader::new(Stage::Vertex, (6, 6)); + let mut io = GraphicsIo::new(Stage::Vertex); + let input = io.input(SigElement::user("INDEX", 0, CompType::U32, 1)); + let output = io.output(SigElement::user("VALUE", 0, CompType::U32, 1)); + let index = io.load_input(&mut s, input, 0, 0); + let i32 = s.m.i32(); + let array = s.m.array(2, i32); + let storage = s.m.alloca(array, 4); + let zero = s.m.i32c(0); + let value = s.m.i32c(7); + let selected = s.m.gep(storage, &[zero, index]); + s.m.store(selected, value, 4); + let first = s.m.gep(storage, &[zero, zero]); + let loaded = s.m.load(first, 4); + io.store_output(&mut s, output, 0, 0, loaded); + + let deps = compute_deps(&s, &io.place(Dir::In), &io.place(Dir::Out), &[]); + assert!(deps.out[&(0, 0)].contains(&0), "output must depend on the store address input"); +} + +#[test] +fn store_dependency_includes_the_store_condition() { + let mut s = Shader::new(Stage::Vertex, (6, 6)); + let mut io = GraphicsIo::new(Stage::Vertex); + let input = io.input(SigElement::user("CONDITION", 0, CompType::U32, 1)); + let output = io.output(SigElement::user("VALUE", 0, CompType::U32, 1)); + let flag = io.load_input(&mut s, input, 0, 0); + let i32 = s.m.i32(); + let storage = s.m.alloca(i32, 4); + let zero = s.m.i32c(0); + let one = s.m.i32c(1); + s.m.store(storage, zero, 4); + let condition = s.m.cmp(Pred::Eq, flag, one); + let (then_block, join) = (s.m.new_block(), s.m.new_block()); + s.m.cond_br(condition, then_block, join); + s.m.set_block(then_block); + s.m.store(storage, one, 4); + s.m.br(join); + s.m.set_block(join); + let loaded = s.m.load(storage, 4); + io.store_output(&mut s, output, 0, 0, loaded); + + let deps = compute_deps(&s, &io.place(Dir::In), &io.place(Dir::Out), &[]); + assert!(deps.out[&(0, 0)].contains(&0), "output must depend on the store condition input"); +} + +#[test] +fn atomic_dependency_includes_address_and_condition_inputs() { + let mut s = Shader::new(Stage::Vertex, (6, 6)); + let mut io = GraphicsIo::new(Stage::Vertex); + let input = io.input(SigElement::user("CONTROL", 0, CompType::U32, 2)); + let output = io.output(SigElement::user("VALUE", 0, CompType::U32, 1)); + let index = io.load_input(&mut s, input, 0, 0); + let gate = io.load_input(&mut s, input, 0, 1); + let i32 = s.m.i32(); + let array = s.m.array(2, i32); + let storage = s.m.global("scratch", array, 3, None, false, 4); + let zero = s.m.i32c(0); + let one = s.m.i32c(1); + let selected = s.m.gep(storage, &[zero, index]); + let condition = s.m.cmp(Pred::Eq, gate, one); + let (then_block, join) = (s.m.new_block(), s.m.new_block()); + s.m.cond_br(condition, then_block, join); + s.m.set_block(then_block); + s.m.atomic_rmw(RmwOp::Add, selected, one); + s.m.br(join); + s.m.set_block(join); + let first = s.m.gep(storage, &[zero, zero]); + let loaded = s.m.load(first, 4); + io.store_output(&mut s, output, 0, 0, loaded); + + let deps = compute_deps(&s, &io.place(Dir::In), &io.place(Dir::Out), &[]); + assert!(deps.out[&(0, 0)].contains(&0), "output must depend on the atomic address input"); + assert!(deps.out[&(0, 0)].contains(&1), "output must depend on the atomic condition input"); +} + /// `tests/data/dxil/dxc_vs_clip.hlsl`: clip and cull distances (two clip elements /// sharing a row), `SV_ViewportArrayIndex`. fn vs_clip() -> Vec { diff --git a/crates/xcl/src/gpu/dxil/module.rs b/crates/xcl/src/gpu/dxil/module.rs index d72ef6fc38..f0292161c0 100644 --- a/crates/xcl/src/gpu/dxil/module.rs +++ b/crates/xcl/src/gpu/dxil/module.rs @@ -1372,6 +1372,8 @@ pub enum InstInfo<'a> { Phi(&'a [(Value, Block)]), Load(Value), Store(Value, Value), + /// An atomic write: pointer and value operands. + Atomic(Vec), /// Any other value-producing instruction with its value operands. Other(Vec), /// A terminator: condition operand (if any) and successors. @@ -1395,8 +1397,8 @@ impl Module { Op::Cast(_, v, _) | Op::Extract(v, _) => InstInfo::Other(vec![*v]), Op::Select(c, t, f) => InstInfo::Other(vec![*c, *t, *f]), Op::Gep(p, idx) => InstInfo::Other(std::iter::once(*p).chain(idx.iter().copied()).collect()), - Op::AtomicRmw(_, p, v) => InstInfo::Other(vec![*p, *v]), - Op::CmpXchg(p, c, n) => InstInfo::Other(vec![*p, *c, *n]), + Op::AtomicRmw(_, p, v) => InstInfo::Atomic(vec![*p, *v]), + Op::CmpXchg(p, c, n) => InstInfo::Atomic(vec![*p, *c, *n]), Op::Alloca(..) => InstInfo::Other(Vec::new()), Op::Br(b) => InstInfo::Term(None, vec![*b]), Op::CondBr(c, t, f) => InstInfo::Term(Some(*c), vec![*t, *f]), @@ -1434,7 +1436,7 @@ impl Module { InstInfo::Phi(l) => l.iter().map(|p| p.0).collect(), InstInfo::Load(p) => vec![p], InstInfo::Store(p, v) => vec![p, v], - InstInfo::Other(v) => v, + InstInfo::Atomic(v) | InstInfo::Other(v) => v, InstInfo::Term(c, _) => c.into_iter().collect(), }; ops.into_iter().any(|v| self.value_ty(v) == t)