Skip to content
Open
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
35 changes: 15 additions & 20 deletions crates/xcl/src/gpu/dxil/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32, usize> = HashMap::new();
let mut root_stores: HashMap<Value, Vec<Value>> = 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<Value, Vec<(Vec<Value>, 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));
}
}
}
Expand Down Expand Up @@ -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<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<(Vec<Value>, usize)>>,
in_base: &'a [Option<u32>],
inputs: &'a [Placed],
}
Expand Down Expand Up @@ -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<u32>> = HashMap::new();
let bases = |p: &[Placed]| -> Vec<Option<u32>> { p.iter().map(|e| e.at.map(|(r, c)| r * 4 + c as u32)).collect() };
let (ob, pb) = (bases(outputs), bases(prims));
Expand Down
80 changes: 79 additions & 1 deletion crates/xcl/src/gpu/dxil/graphics_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]> {
Expand Down Expand Up @@ -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<u8> {
Expand Down
8 changes: 5 additions & 3 deletions crates/xcl/src/gpu/dxil/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>),
/// Any other value-producing instruction with its value operands.
Other(Vec<Value>),
/// A terminator: condition operand (if any) and successors.
Expand All @@ -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]),
Expand Down Expand Up @@ -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)
Expand Down
Loading