From 25298fb70155f9b7c724560b7af4455bc9a4a450 Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:55:09 +0000 Subject: [PATCH] Recompiler: direct DXIL's signature dependencies are complete across loops compute_deps (the input-to-output tables of dx.viewIdState and PSV0) breaks the recursion a loop makes by answering an empty set for a value whose own set is still being worked out, and then keeps what it derived from that answer. With acc = phi [X, entry], [next, loop] and next = acc + 1, asking for acc first reaches next while acc is open: next is remembered as depending on nothing, and an output written from next loses input X (#1892, its first point). The sets now say which pass worked them out and whether that is still going on. A pass that had to answer for an open value or block (a cut) is followed by another, each value starting from what the last pass knew of it, until no set grows. Code without a loop cuts nothing and is done after one pass, as before. The direct translator's version goes to 8, since the tables of programs with loops change. Assisted-by: OpenCode (Claude Fable 5.2) --- crates/xcl/src/gpu/dxil/graphics.rs | 133 +++++++++++++++------- crates/xcl/src/gpu/dxil/graphics_tests.rs | 43 +++++++ crates/xcl/src/gpu/dxil/kernel.rs | 2 +- 3 files changed, 134 insertions(+), 44 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/graphics.rs b/crates/xcl/src/gpu/dxil/graphics.rs index 0668b80431..2856283962 100644 --- a/crates/xcl/src/gpu/dxil/graphics.rs +++ b/crates/xcl/src/gpu/dxil/graphics.rs @@ -771,8 +771,49 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac } } let in_base: Vec> = inputs.iter().map(|e| e.at.map(|(r, c)| r * 4 + c as u32)).collect(); - let mut memo: HashMap> = HashMap::new(); - let mut ctrl_memo: HashMap> = 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, + 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, + blocks: HashMap, + 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(map: &HashMap, key: &K, pass: u32, cut: &mut bool) -> Option> { + let k = map.get(key)?; + if k.open { + *cut = true; + } else if k.pass != pass { + return None; + } + Some(k.set.clone()) + } + fn open(map: &mut HashMap, key: K) { + map.entry(key).or_default().open = true; + } + /// `d` (with what was known before) is `key`'s set of this pass. + fn close(map: &mut HashMap, key: K, mut d: BTreeSet, pass: u32, grew: &mut bool) -> BTreeSet { + 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, @@ -783,29 +824,28 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac in_base: &'a [Option], inputs: &'a [Placed], } - fn block_deps(cx: &Cx, b: usize, memo: &mut HashMap>, cm: &mut HashMap>) -> BTreeSet { - if let Some(d) = cm.get(&b) { - return d.clone(); + fn block_deps(cx: &Cx, b: usize, st: &mut Sets) -> BTreeSet { + 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>, cm: &mut HashMap>) -> BTreeSet { + fn value_deps(cx: &Cx, v: Value, st: &mut Sets) -> BTreeSet { 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) { @@ -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> = 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)); - 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 } diff --git a/crates/xcl/src/gpu/dxil/graphics_tests.rs b/crates/xcl/src/gpu/dxil/graphics_tests.rs index d2d8f8876f..c8f3802053 100644 --- a/crates/xcl/src/gpu/dxil/graphics_tests.rs +++ b/crates/xcl/src/gpu/dxil/graphics_tests.rs @@ -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 { diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index d28a2ffcb6..fb9d78e1a1 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -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.