Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions crates/xcl/src/gpu/dxil/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32, usize> = HashMap::new();
let mut root_stores: HashMap<Value, Vec<Value>> = HashMap::new();
let mut root_stores: HashMap<Value, Vec<Stored>> = 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 });
}
}
}
}
Expand Down Expand Up @@ -820,7 +830,7 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac
block_of: &'a HashMap<u32, usize>,
ctrl: &'a Vec<Vec<usize>>,
cond: &'a Vec<Option<Value>>,
root_stores: &'a HashMap<Value, Vec<Value>>,
root_stores: &'a HashMap<Value, Vec<Stored>>,
in_base: &'a [Option<u32>],
inputs: &'a [Placed],
}
Expand Down Expand Up @@ -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));
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions crates/xcl/src/gpu/dxil/graphics_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading