Skip to content
Merged
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
133 changes: 90 additions & 43 deletions crates/xcl/src/gpu/dxil/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,49 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac
}
}
let in_base: Vec<Option<u32>> = inputs.iter().map(|e| e.at.map(|(r, c)| r * 4 + c as u32)).collect();
let mut memo: HashMap<Value, BTreeSet<u32>> = HashMap::new();
let mut ctrl_memo: HashMap<usize, BTreeSet<u32>> = HashMap::new();
/// What is known of a value's (or a block's) inputs: `pass` is the pass that worked it
/// out last, `open` while that is going on.
#[derive(Default)]
struct Known {
set: BTreeSet<u32>,
pass: u32,
open: bool,
}
/// The sets so far. A loop makes a value depend on itself: a value reached again while it
/// is `open` answers with what earlier passes know of it (`cut`), so what is derived from
/// that answer may miss inputs – the outputs are worked out again until no set grows.
#[derive(Default)]
struct Sets {
values: HashMap<Value, Known>,
blocks: HashMap<usize, Known>,
pass: u32,
cut: bool,
grew: bool,
}
impl Sets {
/// The answer for `key` without working it out: it is done in this pass, or open.
fn known<K: std::hash::Hash + Eq>(map: &HashMap<K, Known>, key: &K, pass: u32, cut: &mut bool) -> Option<BTreeSet<u32>> {
let k = map.get(key)?;
if k.open {
*cut = true;
} else if k.pass != pass {
return None;
}
Some(k.set.clone())
}
fn open<K: std::hash::Hash + Eq>(map: &mut HashMap<K, Known>, key: K) {
map.entry(key).or_default().open = true;
}
/// `d` (with what was known before) is `key`'s set of this pass.
fn close<K: std::hash::Hash + Eq>(map: &mut HashMap<K, Known>, key: K, mut d: BTreeSet<u32>, pass: u32, grew: &mut bool) -> BTreeSet<u32> {
let k = map.entry(key).or_default();
d.extend(k.set.iter().copied());
*grew |= d.len() > k.set.len();
*k = Known { set: d.clone(), pass, open: false };
d
}
}
let mut sets = Sets::default();

struct Cx<'a> {
s: &'a Shader,
Expand All @@ -783,29 +824,28 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac
in_base: &'a [Option<u32>],
inputs: &'a [Placed],
}
fn block_deps(cx: &Cx, b: usize, memo: &mut HashMap<Value, BTreeSet<u32>>, cm: &mut HashMap<usize, BTreeSet<u32>>) -> BTreeSet<u32> {
if let Some(d) = cm.get(&b) {
return d.clone();
fn block_deps(cx: &Cx, b: usize, st: &mut Sets) -> BTreeSet<u32> {
if let Some(d) = Sets::known(&st.blocks, &b, st.pass, &mut st.cut) {
return d;
}
cm.insert(b, BTreeSet::new());
Sets::open(&mut st.blocks, b);
let mut d = BTreeSet::new();
for &a in &cx.ctrl[b] {
if let Some(c) = cx.cond[a] {
d.extend(value_deps(cx, c, memo, cm));
d.extend(value_deps(cx, c, st));
}
if a != b {
d.extend(block_deps(cx, a, memo, cm));
d.extend(block_deps(cx, a, st));
}
}
cm.insert(b, d.clone());
d
Sets::close(&mut st.blocks, b, d, st.pass, &mut st.grew)
}
fn value_deps(cx: &Cx, v: Value, memo: &mut HashMap<Value, BTreeSet<u32>>, cm: &mut HashMap<usize, BTreeSet<u32>>) -> BTreeSet<u32> {
fn value_deps(cx: &Cx, v: Value, st: &mut Sets) -> BTreeSet<u32> {
let Value::Inst(i) = v else { return BTreeSet::new() };
if let Some(d) = memo.get(&v) {
return d.clone();
if let Some(d) = Sets::known(&st.values, &v, st.pass, &mut st.cut) {
return d;
}
memo.insert(v, BTreeSet::new());
Sets::open(&mut st.values, v);
let m = &cx.s.m;
let mut d = BTreeSet::new();
match m.inst_info(i) {
Expand All @@ -821,69 +861,76 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac
for r in 0..cx.inputs[id].e.nrows() {
d.insert(base + r * 4 + col);
}
d.extend(value_deps(cx, args[2], memo, cm));
d.extend(value_deps(cx, args[2], st));
}
}
}
}
InstInfo::Call(_, args) => {
for &a in args {
d.extend(value_deps(cx, a, memo, cm));
d.extend(value_deps(cx, a, st));
}
}
InstInfo::Phi(list) => {
for &(x, from) in list {
d.extend(value_deps(cx, x, memo, cm));
d.extend(block_deps(cx, from.0 as usize, memo, cm));
d.extend(value_deps(cx, x, st));
d.extend(block_deps(cx, from.0 as usize, st));
if let Some(c) = cx.cond[from.0 as usize] {
d.extend(value_deps(cx, c, memo, cm));
d.extend(value_deps(cx, c, st));
}
}
}
InstInfo::Load(p) => {
d.extend(value_deps(cx, p, memo, cm));
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, memo, cm));
d.extend(value_deps(cx, x, st));
}
}
}
InstInfo::Other(ops) => {
for x in ops {
d.extend(value_deps(cx, x, memo, cm));
d.extend(value_deps(cx, x, st));
}
}
InstInfo::Store(..) | InstInfo::Term(..) => {}
}
let _ = cx.block_of;
memo.insert(v, d.clone());
d
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 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));
for b in 0..nb {
for &i in m.block_insts(Block(b as u32)) {
let InstInfo::Call(name, args) = m.inst_info(i) else { continue };
let (dir, base) = if name.starts_with("dx.op.storeOutput.") || name.starts_with("dx.op.storeVertexOutput.") {
(0u8, &ob)
} else if name.starts_with("dx.op.storePrimitiveOutput.") {
(1u8, &pb)
} else {
continue;
};
let id = m.const_u64(args[1]).unwrap() as usize;
let Some(base) = base[id] else { continue };
let col = m.const_u64(args[3]).unwrap() as u32;
let row = m.const_u64(args[2]).unwrap() as u32;
let mut d = value_deps(&cx, args[4], &mut memo, &mut ctrl_memo);
d.extend(block_deps(&cx, b, &mut memo, &mut ctrl_memo));
if args.len() > 5 {
d.extend(value_deps(&cx, args[5], &mut memo, &mut ctrl_memo));
// (code without a loop is done after one pass: nothing was cut)
loop {
sets.pass += 1;
(sets.cut, sets.grew) = (false, false);
for b in 0..nb {
for &i in m.block_insts(Block(b as u32)) {
let InstInfo::Call(name, args) = m.inst_info(i) else { continue };
let (dir, base) = if name.starts_with("dx.op.storeOutput.") || name.starts_with("dx.op.storeVertexOutput.") {
(0u8, &ob)
} else if name.starts_with("dx.op.storePrimitiveOutput.") {
(1u8, &pb)
} else {
continue;
};
let id = m.const_u64(args[1]).unwrap() as usize;
let Some(base) = base[id] else { continue };
let col = m.const_u64(args[3]).unwrap() as u32;
let row = m.const_u64(args[2]).unwrap() as u32;
let mut d = value_deps(&cx, args[4], &mut sets);
d.extend(block_deps(&cx, b, &mut sets));
if args.len() > 5 {
d.extend(value_deps(&cx, args[5], &mut sets));
}
out.entry((dir, base + row * 4 + col)).or_default().extend(d);
}
out.entry((dir, base + row * 4 + col)).or_default().extend(d);
}
if !(sets.cut && sets.grew) {
break;
}
}
Deps { out }
Expand Down
43 changes: 43 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,49 @@ fn vs_control_dependence_matches_dxc() {
assert_valid(&c);
}

/// 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
/// out; `next` must not keep the empty set it saw then.
#[test]
fn a_loop_value_first_reached_through_its_phi_keeps_the_phis_inputs() {
let mut s = Shader::new(Stage::Vertex, (6, 6));
let mut io = GraphicsIo::new(Stage::Vertex);
let x = io.input(SigElement::user("X", 0, CompType::F32, 1));
let oa = io.output(SigElement::user("A", 0, CompType::F32, 1));
let ob = io.output(SigElement::user("B", 0, CompType::F32, 1));
let xv = io.load_input(&mut s, x, 0, 0);
let entry = s.m.current_block();
let (lp, exit) = (s.m.new_block(), s.m.new_block());
s.m.br(lp);
s.m.set_block(lp);
let (f32, i32) = (s.m.f32(), s.m.i32());
let acc = s.m.phi(f32);
let i = s.m.phi(i32);
let one = f(&mut s, 1.0);
let next = s.m.fbin(BinOp::Add, acc, one, 0);
let (c0, c1, c4) = (s.m.i32c(0), s.m.i32c(1), s.m.i32c(4));
let inext = s.m.bin(BinOp::Add, i, c1);
let done = s.m.cmp(Pred::Eq, inext, c4);
s.m.cond_br(done, exit, lp);
s.m.add_incoming(acc, xv, entry);
s.m.add_incoming(acc, next, lp);
s.m.add_incoming(i, c0, entry);
s.m.add_incoming(i, inext, lp);
s.m.set_block(exit);
io.store_output(&mut s, oa, 0, 0, acc);
io.store_output(&mut s, ob, 0, 0, next);
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 x_in = component(&ins, x);
for (name, out) in [("A", oa), ("B", ob)] {
let d = deps.out.get(&(0, component(&outs, out))).cloned().unwrap_or_default();
assert!(d.contains(&x_in), "output {name} depends on input X through the loop; its inputs: {d:?}");
}
}

/// A pixel shader reading a per-primitive attribute: `SV_Target =
/// float4(COLOR.rgb, PRIMK)`, discarding pixels left of x = 4.
fn prim_ps() -> Vec<u8> {
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 @@ -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.
Expand Down
Loading