From 6f8cb8b321fb27b9bd0082486c6750b77f657448 Mon Sep 17 00:00:00 2001 From: josh-wirick <48888341+josh-wirick@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:15:17 -0600 Subject: [PATCH] Recompiler: direct DXIL's signature dependency sets are bit masks The direct translator recomputes a vertex or pixel shader's input -> output dependency tables (PSV0, dx.viewIdState) from the code it emitted. compute_deps kept the set of input components of every value as a BTreeSet, cloned and merged at each operand of each instruction, in hash maps keyed by value. On a retail pack that was 39 % of the time its vertex and pixel shaders took to translate. - the sets are bit masks (a u128 for components 0..128, words after that); - the two memo tables are indexed by instruction and by block; - the base of a pointer chain is found once per instruction, not walked again at every instruction with two operands or more; - the instruction -> block map, which nothing read, is gone. The traversal and what it memoises are unchanged, so the tables are too. Measured on a retail pack's 26 698 programs (xcl shaders --direct --sample 0 --max-bytes 0, 8 threads, Ryzen 7 9700X), two runs each: - compute_deps: 156 / 154 s -> 37.5 / 38.0 s summed over the threads; - vertex and pixel shaders (14 337 programs): 399 / 392 s -> 284 / 288 s; - the whole sweep: 103.6 / 102.7 s -> 89.1 / 89.9 s. The per-phase times come from timers added for the measurement, not part of this commit. With --dump, all 52 168 files of the two builds are identical (25 615 DXIL containers). Part of #1269. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/dxil/graphics.rs | 130 +++++++++++++++++----- crates/xcl/src/gpu/dxil/graphics_tests.rs | 45 ++++++++ 2 files changed, 145 insertions(+), 30 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/graphics.rs b/crates/xcl/src/gpu/dxil/graphics.rs index 0668b80431..547340e5da 100644 --- a/crates/xcl/src/gpu/dxil/graphics.rs +++ b/crates/xcl/src/gpu/dxil/graphics.rs @@ -9,7 +9,7 @@ //! packing is DXC's default prefix-stable first fit, the PSV0 dependency //! tables are recomputed from the emitted code the way the validator does. -use std::collections::{BTreeSet, HashMap}; +use std::collections::HashMap; use super::container::{self, PsvFull, PsvSigElement, SigPartElement, Stage}; use super::module::{Block, FnAttrs, InstInfo, Md, Ty, Value}; @@ -634,10 +634,53 @@ fn psv_elems(p: &[Placed]) -> Vec { .collect() } +/// A set of signature components (`row * 4 + col`), one bit each. `compute_deps` copies +/// and merges one at every operand of every instruction: as `BTreeSet`s that was two fifths +/// of the time a retail pack's vertex and pixel shaders took to translate. +#[derive(Clone, Default)] +struct Comps { + /// components 0..128 (rows 0..32): no allocation for a signature of the usual size + low: u128, + /// components from 128 on, 64 a word + high: Vec, +} + +impl Comps { + fn insert(&mut self, c: u32) { + if c < 128 { + self.low |= 1 << c; + return; + } + let w = ((c - 128) / 64) as usize; + if w >= self.high.len() { + self.high.resize(w + 1, 0); + } + self.high[w] |= 1 << (c % 64); + } + + /// Adds the components of `o`. + fn extend(&mut self, o: Comps) { + self.low |= o.low; + if o.high.len() > self.high.len() { + self.high.resize(o.high.len(), 0); + } + for (a, b) in self.high.iter_mut().zip(&o.high) { + *a |= b; + } + } + + /// The components, ascending. + fn iter(&self) -> impl Iterator + '_ { + let low = (0..128u32).filter(|&c| self.low >> c & 1 != 0); + let high = self.high.iter().enumerate().flat_map(|(w, &bits)| (0..64u32).filter(move |&b| bits >> b & 1 != 0).map(move |b| 128 + w as u32 * 64 + b)); + low.chain(high) + } +} + /// Input component → set of output components (`row * 4 + col`), from the code. struct Deps { /// For each output component, the input components it depends on. - out: HashMap<(u8, u32), BTreeSet>, + out: HashMap<(u8, u32), Comps>, } /// Input components each value depends on, through data flow, memory and @@ -751,44 +794,68 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac ctrl[v].push(a); } } - // Instruction → block. - let mut block_of: HashMap = HashMap::new(); + // The base of each instruction's pointer chain (`root`), found once: the walk below + // asks for it at every instruction with two operands or more. + let mut roots: Vec> = Vec::new(); + let mut root_of = |p: Value| -> Value { + let mut chain: Vec = Vec::new(); + let mut p = p; + let r = loop { + let Value::Inst(i) = p else { break p }; + if let Some(Some(r)) = roots.get(i as usize) { + break *r; + } + match m.inst_info(i) { + InstInfo::Other(ops) if !ops.is_empty() => { + chain.push(i); + p = ops[0]; + } + _ => break p, + } + }; + for i in chain { + if roots.len() <= i as usize { + roots.resize(i as usize + 1, None); + } + roots[i as usize] = Some(r); + } + r + }; 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); - } - if let InstInfo::Other(ops) = m.inst_info(i) { + match m.inst_info(i) { + InstInfo::Store(p, v) => root_stores.entry(root_of(p)).or_default().push(v), // 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..]); + InstInfo::Other(ops) if ops.len() >= 2 => { + let r = root_of(ops[0]); + if let Value::Global(_) = r { + root_stores.entry(r).or_default().extend_from_slice(&ops[1..]); } } + _ => {} } } } 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(); + // by instruction and by block + let mut memo: Vec> = Vec::new(); + let mut ctrl_memo: Vec> = vec![None; nb]; struct Cx<'a> { s: &'a Shader, - block_of: &'a HashMap, ctrl: &'a Vec>, cond: &'a Vec>, root_stores: &'a HashMap>, 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) { + fn block_deps(cx: &Cx, b: usize, memo: &mut Vec>, cm: &mut Vec>) -> Comps { + if let Some(d) = &cm[b] { return d.clone(); } - cm.insert(b, BTreeSet::new()); - let mut d = BTreeSet::new(); + cm[b] = Some(Comps::default()); + let mut d = Comps::default(); for &a in &cx.ctrl[b] { if let Some(c) = cx.cond[a] { d.extend(value_deps(cx, c, memo, cm)); @@ -797,17 +864,21 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac d.extend(block_deps(cx, a, memo, cm)); } } - cm.insert(b, d.clone()); + cm[b] = Some(d.clone()); d } - fn value_deps(cx: &Cx, v: Value, memo: &mut HashMap>, cm: &mut HashMap>) -> BTreeSet { - let Value::Inst(i) = v else { return BTreeSet::new() }; - if let Some(d) = memo.get(&v) { + fn value_deps(cx: &Cx, v: Value, memo: &mut Vec>, cm: &mut Vec>) -> Comps { + let Value::Inst(i) = v else { return Comps::default() }; + let at = i as usize; + if memo.len() <= at { + memo.resize(at + 1, None); + } + if let Some(d) = &memo[at] { return d.clone(); } - memo.insert(v, BTreeSet::new()); + memo[at] = Some(Comps::default()); let m = &cx.s.m; - let mut d = BTreeSet::new(); + let mut d = Comps::default(); match m.inst_info(i) { InstInfo::Call(name, args) if name.starts_with("dx.op.loadInput.") => { let id = m.const_u64(args[1]).unwrap() as usize; @@ -855,13 +926,12 @@ fn compute_deps(s: &Shader, inputs: &[Placed], outputs: &[Placed], prims: &[Plac } InstInfo::Store(..) | InstInfo::Term(..) => {} } - let _ = cx.block_of; - memo.insert(v, d.clone()); + memo[at] = Some(d.clone()); d } - 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 cx = Cx { s, ctrl: &ctrl, cond: &cond, root_stores: &root_stores, in_base: &in_base, inputs }; + let mut out: HashMap<(u8, u32), Comps> = 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 { @@ -912,7 +982,7 @@ fn dep_table(deps: &Deps, dir: u8, n_in: u32, n_out: u32) -> Vec { if d != dir || o >= n_out { continue; } - for &i in ins { + for i in ins.iter() { if i < n_in { t[i as usize * words + (o / 32) as usize] |= 1 << (o % 32); } diff --git a/crates/xcl/src/gpu/dxil/graphics_tests.rs b/crates/xcl/src/gpu/dxil/graphics_tests.rs index d2d8f8876f..ded7da9249 100644 --- a/crates/xcl/src/gpu/dxil/graphics_tests.rs +++ b/crates/xcl/src/gpu/dxil/graphics_tests.rs @@ -1193,3 +1193,48 @@ fn ps_static_resources_match_dxc() { assert_eq!(part(&c, b"SFI0"), part(DXC_PS_RESOURCES, b"SFI0")); assert_valid(&c); } + +/// The dependency sets of `compute_deps`: components on both sides of the 128 held inline. +#[test] +fn component_sets_insert_merge_and_list_in_order() { + let mut a = Comps::default(); + for c in [5, 0, 127, 128, 191, 192, 300, 5] { + a.insert(c); + } + assert_eq!(a.iter().collect::>(), [0, 5, 127, 128, 191, 192, 300]); + let mut b = Comps::default(); + b.insert(64); + b.insert(130); + // the longer set into the shorter one, and the other way round + let (mut ab, mut ba) = (b.clone(), a.clone()); + ab.extend(a); + ba.extend(b); + let all = [0, 5, 64, 127, 128, 130, 191, 192, 300]; + assert_eq!(ab.iter().collect::>(), all); + assert_eq!(ba.iter().collect::>(), all); + assert_eq!(Comps::default().iter().count(), 0); +} + +/// An input placed past row 32 (components from 128 on) reaches the dependency table like +/// any other. +#[test] +fn dependencies_on_input_rows_past_32_reach_the_table() { + let mut s = Shader::new(Stage::Vertex, (6, 6)); + let mut io = GraphicsIo::new(Stage::Vertex); + let lo = io.input(SigElement::user("LO", 0, CompType::F32, 1)); + let hi = io.input(SigElement::user("HI", 0, CompType::F32, 4).at(40, 0)); + let o = io.output(SigElement::user("O", 0, CompType::F32, 2)); + let a = io.load_input(&mut s, lo, 0, 0); + let b = io.load_input(&mut s, hi, 0, 3); + let sum = s.m.fbin(BinOp::Add, a, b, 0); + io.store_output(&mut s, o, 0, 0, sum); + io.store_output(&mut s, o, 0, 1, b); + s.m.ret(); + let (ins, outs, prims) = (io.place(Dir::In), io.place(Dir::Out), io.place(Dir::Prim)); + let (n_in, n_out) = (components(&ins), components(&outs)); + assert_eq!((n_in, n_out), (164, 2)); + let deps = compute_deps(&s, &ins, &outs, &prims); + // a word per input component: bit k is set when output component k depends on it + let want: Vec = (0..n_in).map(|i| [(0, 0b01), (163, 0b11)].iter().find(|w| w.0 == i).map_or(0, |w| w.1)).collect(); + assert_eq!(dep_table(&deps, 0, n_in, n_out), want); +}