diff --git a/crates/xcl/src/gpu/dxil/graphics.rs b/crates/xcl/src/gpu/dxil/graphics.rs index 239a923ea9..9f291cb49c 100644 --- a/crates/xcl/src/gpu/dxil/graphics.rs +++ b/crates/xcl/src/gpu/dxil/graphics.rs @@ -751,20 +751,30 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac ctrl[v].push(a); } } + /// A store into a root (a global or groupshared array): the value written, the pointer it + /// went through (an index built from an input) and the block (a store under a branch + /// depends on that branch's inputs) – a later load of the root depends on all three. + struct Stored { + v: Value, + p: Value, + block: usize, + } // Instruction → block. let mut block_of: HashMap = HashMap::new(); - let mut root_stores: HashMap> = HashMap::new(); + let mut root_stores: HashMap> = 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(Stored { v, p, block: 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..]); + for &v in &ops[1..] { + root_stores.entry(root(s, ops[0])).or_default().push(Stored { v, p: ops[0], block: b }); + } } } } @@ -820,7 +830,7 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac block_of: &'a HashMap, ctrl: &'a Vec>, cond: &'a Vec>, - root_stores: &'a HashMap>, + root_stores: &'a HashMap>, in_base: &'a [Option], inputs: &'a [Placed], } @@ -883,8 +893,12 @@ 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)); + for x in vs { + // what was written, the index it was written through (the load may + // read a slot the input chose) and the branch that guarded the store + d.extend(value_deps(cx, x.v, st)); + d.extend(value_deps(cx, x.p, st)); + d.extend(block_deps(cx, x.block, st)); } } } diff --git a/crates/xcl/src/gpu/dxil/graphics_tests.rs b/crates/xcl/src/gpu/dxil/graphics_tests.rs index c8f3802053..499d762278 100644 --- a/crates/xcl/src/gpu/dxil/graphics_tests.rs +++ b/crates/xcl/src/gpu/dxil/graphics_tests.rs @@ -695,6 +695,51 @@ fn vs_control_dependence_matches_dxc() { assert_valid(&c); } +/// A load of a root depends on how the stores into it were reached, not only on what they +/// wrote (#1892, its second point): the branch condition guarding a store and the index the +/// store was made through are inputs the outputs depend on, and `dx.viewIdState` has to say so. +#[test] +fn a_load_depends_on_the_branch_and_the_index_of_the_stores_into_it() { + let mut s = Shader::new(Stage::Vertex, (6, 6)); + let mut io = GraphicsIo::new(Stage::Vertex); + let c = io.input(SigElement::user("C", 0, CompType::U32, 1)); // the branch condition + let i = io.input(SigElement::user("I", 0, CompType::U32, 1)); // the index + let x = io.input(SigElement::user("X", 0, CompType::F32, 1)); // the value stored + let oa = io.output(SigElement::user("A", 0, CompType::F32, 1)); + let f32t = s.m.f32(); + let arr = s.m.array(2, f32t); + let g = s.m.global("g", arr, 3, None, false, 4); + let cv = io.load_input(&mut s, c, 0, 0); + let iv = io.load_input(&mut s, i, 0, 0); + let xv = io.load_input(&mut s, x, 0, 0); + let (c0, c1) = (s.m.i32c(0), s.m.i32c(1)); + let one = f(&mut s, 1.0); + let idx = s.m.bin(BinOp::And, iv, c1); + let p = s.m.gep(g, &[c0, idx]); + // `if (C != 0) g[I & 1] = X;` then `if (C == 0) g[0] = 1.0;` – both stores guarded by C + let nz = s.m.cmp(Pred::Ne, cv, c0); + let (st, else_b, join) = (s.m.new_block(), s.m.new_block(), s.m.new_block()); + s.m.cond_br(nz, st, else_b); + s.m.set_block(st); + s.m.store(p, xv, 4); + s.m.br(join); + s.m.set_block(else_b); + let p0 = s.m.gep(g, &[c0, c0]); + s.m.store(p0, one, 4); + s.m.br(join); + s.m.set_block(join); + let a = s.m.load(p0, 4); + io.store_output(&mut s, oa, 0, 0, a); + s.m.ret(); + let (ins, outs, prims) = (io.place(Dir::In), io.place(Dir::Out), io.place(Dir::Prim)); + let component = |p: &[Placed], sig: Sig| p[sig.id as usize].at.map(|(r, c)| r * 4 + c as u32).expect("a placed element"); + let deps = compute_deps(&s, &ins, &outs, &prims); + let d = deps.out.get(&(0, component(&outs, oa))).cloned().unwrap_or_default(); + for (name, sig) in [("C (the branch that guards the stores)", c), ("I (the index a store was made through)", i), ("X (the value stored)", x)] { + assert!(d.contains(&component(&ins, sig)), "the output depends on {name}; its inputs: {d:?}"); + } +} + /// A value of a loop depends on what the loop's phi depends on, also when it is first reached /// through that phi (#1892): `acc = phi [X, entry], [next, loop]`, `next = acc + 1`, and the /// outputs A = acc, B = next. Asking for `acc` reaches `next` while `acc` is still being worked diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 8687e4807a..6693528687 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -38,7 +38,7 @@ pub struct KernelOptions { /// identity (`recomp::pso`): bump it when `pso::direct_compute` / `direct_graphics` change the /// containers. An edit of `gpu/dxil` needs no bump, `XCL_DIRECT_DXIL_SOURCE_ID` is its hash /// (the `gpu/dxil` sources are not in build.rs's translator source hash). -pub const TRANSLATOR_VERSION: u32 = 9; +pub const TRANSLATOR_VERSION: u32 = 10; /// `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.