diff --git a/crates/metal2vulkan/src/pipeline/compile/stage_bound.rs b/crates/metal2vulkan/src/pipeline/compile/stage_bound.rs index 9f660171e..cdd91a505 100644 --- a/crates/metal2vulkan/src/pipeline/compile/stage_bound.rs +++ b/crates/metal2vulkan/src/pipeline/compile/stage_bound.rs @@ -8,7 +8,8 @@ //! //! Every module's entry is weighed (`spirv::roll::entry_weight`: the entry's instructions with each //! call counted as its callee's body, as the driver inlines every call) after the repeated regions -//! are rolled, and the weight is the module's ([`ModuleStats::entry_weight`]). A kernel past the +//! are rolled and written as kept routines (`spirv::outline`), and the weight is the module's +//! ([`ModuleStats::entry_weight`]). A kernel past the //! bound that took the `Emulated` simdgroup tier only in place of the `Direct` tier's pin to the //! device's widest mode (`lower::simd::widest_pin`) is planned again keeping the pin //! (`lower::simd::WidestPin::Kept`), its emulated module dropped and what its plan and emission @@ -85,6 +86,11 @@ pub(super) fn emit( m.weighted(crate::pipeline::compile::weights::HOIST, |m| { crate::spirv::hoist::hoist_uniform_loads(&mut emitted.module, p.robustness(), m) })?; + // A driver that keeps a routine compiles it once: repeated regions are written once, as + // kept routines. + m.weighted(crate::pipeline::compile::weights::ROLL, |m| { + crate::spirv::outline::outline_entry(&mut emitted.module, m) + })?; let weight = crate::spirv::roll::entry_weight(&emitted.module, m)?; if weight <= bounds.compiled || widest == WidestPin::Kept || !plan.families.simd.widest_pin { diff --git a/crates/metal2vulkan/src/pipeline/compile/weights.rs b/crates/metal2vulkan/src/pipeline/compile/weights.rs index 1fbb5e65f..b10f9e20e 100644 --- a/crates/metal2vulkan/src/pipeline/compile/weights.rs +++ b/crates/metal2vulkan/src/pipeline/compile/weights.rs @@ -39,7 +39,9 @@ pub(crate) const SELECT: u32 = 6; pub(crate) const STRUCTURIZE: u32 = 5; /// Emission ([`emit_module`](crate::lower::emit::emit_module)), the structurizer inside it aside. pub(crate) const EMIT: u32 = 9; -/// Rolling repeated regions ([`roll_entry`](crate::spirv::roll::roll_entry)). +/// Rolling repeated regions ([`roll_entry`](crate::spirv::roll::roll_entry)), and writing them as +/// kept routines ([`outline_entry`](crate::spirv::outline::outline_entry)): the same spine, keys +/// and exact comparisons, so rolling's weight (the routines' work is not measured apart). pub(crate) const ROLL: u32 = 1; /// Hoisting uniform loads ([`hoist_uniform_loads`](crate::spirv::hoist::hoist_uniform_loads)). pub(crate) const HOIST: u32 = 2; diff --git a/crates/metal2vulkan/src/pipeline/testing.rs b/crates/metal2vulkan/src/pipeline/testing.rs index 3c6bea804..7242e6770 100644 --- a/crates/metal2vulkan/src/pipeline/testing.rs +++ b/crates/metal2vulkan/src/pipeline/testing.rs @@ -414,6 +414,7 @@ fn typed_module_of( crate::lower::emit::emit_module(&spec, &plan, m)?; crate::spirv::roll::roll_entry(&mut module, m)?; crate::spirv::hoist::hoist_uniform_loads(&mut module, p.robustness(), m)?; + crate::spirv::outline::outline_entry(&mut module, m)?; crate::spirv::verify::verify(&module, p, m)?; Ok((plan, module)) } diff --git a/crates/metal2vulkan/src/spirv/builder/module.rs b/crates/metal2vulkan/src/spirv/builder/module.rs index 711aad72e..487ae612e 100644 --- a/crates/metal2vulkan/src/spirv/builder/module.rs +++ b/crates/metal2vulkan/src/spirv/builder/module.rs @@ -993,7 +993,8 @@ impl ModuleBuilder { /// every site that needs it, declared `DontInline` (`OpFunction`'s function control) on every /// serialization (`crate::spirv::serialize::serialize`), the standard SPIR-V statement that its /// calls stay calls. A linear filter emulation's sum (a texel routine) and one combined - /// binding's sample emulation (a sampling routine) are kept routines. A driver that honours the + /// binding's sample emulation (a sampling routine) are kept routines, as is a region a function + /// holds more than once, which `spirv::outline` writes in this form. A driver that honours the /// control (NVIDIA's) compiles it once where it would otherwise compile it again inside every /// sample that calls it, which made a fragment of a few hundred emulated samples take tens of /// seconds to create on an RTX 3090; one that inlines every function (Mesa's) ignores it. Its diff --git a/crates/metal2vulkan/src/spirv/ir/module.rs b/crates/metal2vulkan/src/spirv/ir/module.rs index 5e49e81b4..1f444e058 100644 --- a/crates/metal2vulkan/src/spirv/ir/module.rs +++ b/crates/metal2vulkan/src/spirv/ir/module.rs @@ -1206,9 +1206,9 @@ pub struct Function { /// `Function`-storage variables, declared at the top of the entry block. pub vars: Vec, /// Whether the function is a kept routine (`ModuleBuilder::kept_routine`): a linear filter - /// emulation's sum or one combined binding's sample emulation, which `serialize` declares - /// `DontInline`, so a driver that honours the control compiles it once and calls it from each - /// site. + /// emulation's sum, one combined binding's sample emulation, or a region the function held + /// more than once (`spirv::outline`), which `serialize` declares `DontInline`, so a driver that + /// honours the control compiles it once and calls it from each site. pub kept: bool, } diff --git a/crates/metal2vulkan/src/spirv/mod.rs b/crates/metal2vulkan/src/spirv/mod.rs index 2ce2592cc..67d4a57a0 100644 --- a/crates/metal2vulkan/src/spirv/mod.rs +++ b/crates/metal2vulkan/src/spirv/mod.rs @@ -14,6 +14,7 @@ pub mod ids; #[cfg(test)] pub mod interp; pub mod ir; +pub mod outline; pub mod requirements; pub mod roll; pub mod serialize; diff --git a/crates/metal2vulkan/src/spirv/outline/mod.rs b/crates/metal2vulkan/src/spirv/outline/mod.rs new file mode 100644 index 000000000..28ebcb62b --- /dev/null +++ b/crates/metal2vulkan/src/spirv/outline/mod.rs @@ -0,0 +1,1631 @@ +//! Repeated regions written once, as a kept routine each region's place calls. +//! +//! The AIR inliner (`air::canon::inline`) writes each AIR call's body in line, so a function a +//! kernel calls from several places is in its entry several times. A driver that honours a call's +//! `DontInline` (NVIDIA's) compiles a kept routine once and calls it; one that inlines every call +//! (Mesa's RADV and ANV) compiles the same code either way. On the RTX 3090 (NVIDIA 620.70.06, job +//! m3-max-64453), a generated kernel calling one function of 1,984 instructions from 50 places +//! was created in 21.5 s with the function written in line at each and in 52 ms with it a kept +//! routine; with 64 values live across every call, 51.8 s and 75 ms. Its dispatches took the same +//! GPU time either way with few values live, and 20.6% less as a routine with 64 live; a function +//! of 497 instructions measured the same (0.1% less, and 19.7% less with 64 live). +//! +//! On every profile, the pass finds a set of regions of one function that are the same code, each +//! of at least [`MIN_REGION`] instructions (`roll::entry_weight`'s count, a call of a kept routine +//! counting one), writes the first as a new kept routine (`Function::kept`, the form of +//! `ModuleBuilder::kept_routine`), and replaces each region with a call of it; then it looks again, +//! the routines' own bodies included, at most [`MAX_ROUTINES`] times. It runs after `spirv::roll`, +//! which writes a run of consecutive regions above its size once as a loop. +//! +//! A region is a run of items of one sequence of the function's spine (`roll::spine`, the bodies of +//! loops included): its first block from an instruction on, the constructs it holds, and its last +//! block up to an instruction. It is entered only at its start and left only at its end: a branch +//! out of it (a loop's break or continue), a return, a kill, or a branch into it from outside keeps +//! it in line. Regions are compared exactly, block by block and instruction by instruction, each +//! value one defines mapped to the first region's at the same place, and each branch target to the +//! first region's block at the same place: +//! +//! - a value defined before the region is a parameter of the routine, one per distinct set of the +//! regions' values at a place; where every region reads the same constant or module-scope +//! variable, the routine reads it too. A constant that differs between regions is a parameter +//! only in an operand any value may take (`roll::varying_ok`, or an index into an array, vector +//! or matrix of data), never in one that must be a constant; +//! - a pointer parameter is a `Function`, `Private`, `Workgroup` or `UniformConstant` variable +//! itself, or a `PhysicalStorageBuffer` pointer, and never an atomic's pointer; any other pointer +//! from before the region (an access chain), an image, a sampler, or a value decorated +//! `NonUniform` keeps the regions in line; +//! - a `Function` variable only its region uses becomes the routine's own where the region is in +//! no loop of its function, or where its first use stores the whole variable in a block the +//! region always runs (so no value of it carries from one run of the region to the next); +//! otherwise it is a pointer parameter; +//! - a value of a region read after it is returned, several in a struct, and each place reads it +//! from the call's result; a pointer or an opaque value read after it keeps the regions in line. +//! +//! What differs between regions in any other way keeps them in line. Candidate sets come from the +//! spine's keys: windows of [`WINDOW`] items whose keys hash alike seed a pair of regions, extended +//! both ways while the keys agree, and the set is the occurrences of a window that share the +//! extent with the largest saving; a set whose comparison fails at an operand or a result is +//! compared again without the item that holds it. Work is linear in the function per search, +//! bounded in functions, occurrences, extension steps and comparisons, and metered on the +//! per-attempt [`Meter`]. + +#[cfg(test)] +mod tests; + +use std::collections::BTreeMap; +use std::ops::Range; + +use crate::budget::Meter; +use crate::error::{Error, InternalError, InternalKind}; +use crate::spirv::ids::{BlockId, FuncId, TypeId, Value, VarId}; +use crate::spirv::ir::{ + Block, Function, Inst, InstNode, Merge, Shape, StorageClass, Terminator, Type, TypedModule, + Unrolling, ValueOrigin, +}; +use crate::spirv::roll::{ + block, block_mut, charge, move_tail, new_block, new_value, removed, rollable, spine, + switched_operand, ty, varying_ok, weights, HashMap, HashSet, Item, KeyCache, Spine, TAIL, +}; + +/// The smallest region written as a routine, in instructions: the smallest function measured to +/// run no slower as a kept routine than written in line on NVIDIA's driver (the module docs: 497 +/// instructions). +pub const MIN_REGION: u64 = 500; + +/// The most routines one module is given. +pub const MAX_ROUTINES: u32 = 64; +/// The items a seed window holds. +const WINDOW: usize = 16; +/// The most occurrences of a seed compared with its first. +const MAX_OCCURRENCES: usize = 64; +/// The exact comparisons per search. +const MAX_VERIFIES: usize = 32; +/// The times one candidate is compared again without the item where its comparison failed. +const MAX_TRIMS: usize = 4; +/// The most functions searched per search: the heaviest the entry reaches. +const MAX_FUNCS: usize = 8; +/// The most parameters a routine takes (SPIR-V's universal limit on a function's parameters). +const MAX_PARAMS: usize = 255; +/// The most values a routine returns. +const MAX_RESULTS: usize = 255; + +fn invariant(detail: impl Into) -> Error { + Error::Internal(InternalError::new(InternalKind::Invariant, detail)) +} + +/// Write `m`'s repeated regions of at least [`MIN_REGION`] instructions as kept routines, on +/// every profile. Returns the routines made. +pub fn outline_entry(m: &mut TypedModule, meter: &mut Meter) -> Result { + outline(m, MIN_REGION, meter) +} + +/// Write `m`'s repeated regions of at least `min_region` instructions as kept routines, largest +/// saving first. Returns the routines made. +pub fn outline(m: &mut TypedModule, min_region: u64, meter: &mut Meter) -> Result { + let mut cache = KeyCache::default(); + let r = outline_kept(m, min_region, &mut cache, meter); + meter.release(cache.charged); + r +} + +/// [`outline`] with the instructions' keys kept in `cache` from one search to the next. +fn outline_kept( + m: &mut TypedModule, + min_region: u64, + cache: &mut KeyCache, + meter: &mut Meter, +) -> Result { + // By function: searched to the end with nothing found, and unchanged since. + let mut settled: Vec = Vec::new(); + let mut made = 0u32; + while made < MAX_ROUTINES { + settled.resize(m.functions.len(), false); + let mut ledger = 0usize; + let found = find( + m, + min_region, + (&mut *cache, settled.as_mut_slice()), + meter, + &mut ledger, + ); + meter.release(ledger); + let Some(plan) = found? else { + break; + }; + if let Some(s) = settled.get_mut(plan.func.index()) { + *s = false; + } + apply(m, &plan)?; + made += 1; + } + if made > 0 { + m.rederive()?; + } + Ok(made) +} + +/// The best set of regions of `m` that becomes a routine, if any. +fn find( + m: &TypedModule, + min_region: u64, + (cache, settled): (&mut KeyCache, &mut [bool]), + meter: &mut Meter, + ledger: &mut usize, +) -> Result, Error> { + let w = weights(m, meter)?; + let n = m.functions.len(); + charge::(meter, ledger, n.saturating_mul(2))?; + // An item's weight counts a call of a kept routine as one instruction: a driver that keeps the + // routine compiles it once, whatever calls it. + let own: Vec = m + .functions + .iter() + .zip(&w) + .map(|(f, x)| if f.kept { 0 } else { *x }) + .collect(); + let mut reached = vec![false; n]; + let mut stack = vec![m.entry.func]; + let mut funcs = Vec::new(); + while let Some(f) = stack.pop() { + match reached.get_mut(f.index()) { + Some(r) if !*r => *r = true, + _ => continue, + } + funcs.push(f); + let blocks = m + .functions + .get(f.index()) + .map_or(&[][..], |x| &x.blocks[..]); + for b in blocks { + for node in &block(m, *b)?.insts { + if let Inst::Call { func, .. } = node.inst { + stack.push(func); + } + } + } + } + let weight = |f: FuncId| w.get(f.index()).copied().unwrap_or(0); + funcs.retain(|f| { + weight(*f) >= min_region.saturating_mul(2) + && !settled.get(f.index()).copied().unwrap_or(true) + }); + funcs.sort_unstable_by(|a, b| weight(*b).cmp(&weight(*a)).then(a.cmp(b))); + funcs.truncate(MAX_FUNCS); + let mut verifies = 0usize; + for f in funcs { + let mut fledger = 0usize; + let r = find_in( + m, + (f, own.as_slice(), min_region), + cache, + meter, + &mut fledger, + &mut verifies, + ); + meter.release(fledger); + let (plan, whole) = r?; + if plan.is_some() { + return Ok(plan); + } + if whole { + if let Some(s) = settled.get_mut(f.index()) { + *s = true; + } + } + if verifies >= MAX_VERIFIES { + break; + } + } + Ok(None) +} + +/// A candidate set: the regions' first items, ascending, and their length in items. +struct Cand { + starts: Vec, + len: usize, + saving: u64, +} + +/// The best set of regions of function `func` that becomes a routine, within `verifies`, and +/// whether the search reached its end. +fn find_in( + m: &TypedModule, + (func, w, min_region): (FuncId, &[u64], u64), + cache: &mut KeyCache, + meter: &mut Meter, + ledger: &mut usize, + verifies: &mut usize, +) -> Result<(Option, bool), Error> { + let sp = spine(m, (func, w, true), cache, meter, ledger)?; + let n = sp.items.len(); + if n < 2 * WINDOW { + return Ok((None, true)); + } + let key = |i: usize| sp.items.get(i).map(|it| it.key); + charge::(meter, ledger, n + 1)?; + let mut pw = Vec::with_capacity(n + 1); + pw.push(0u64); + for it in &sp.items { + let last = pw.last().copied().unwrap_or(0); + pw.push(last.saturating_add(it.weight)); + } + let span = |a: usize, len: usize| { + let end = pw.get(a + len).copied().unwrap_or(0); + end.saturating_sub(pw.get(a).copied().unwrap_or(0)) + }; + // Every window's hash (a polynomial over its keys, rolled one item at a time), each window's + // first item listed under it in order. + const B: u64 = 0x0000_0100_0000_01B3; + let pow = (1..WINDOW).fold(1u64, |p, _| p.wrapping_mul(B)); + let mut h = 0u64; + for i in 0..WINDOW { + h = h.wrapping_mul(B).wrapping_add(key(i).unwrap_or(0)); + } + charge::<(u64, u32, u32)>(meter, ledger, n)?; + let mut buckets: HashMap> = HashMap::default(); + meter.tick(u32::try_from(n).unwrap_or(u32::MAX))?; + for i in 0..=n - WINDOW { + let at = u32::try_from(i).map_err(|_| invariant("item index"))?; + buckets.entry(h).or_default().push(at); + if i + WINDOW < n { + let out = key(i).unwrap_or(0).wrapping_mul(pow); + h = h + .wrapping_sub(out) + .wrapping_mul(B) + .wrapping_add(key(i + WINDOW).unwrap_or(0)); + } + } + let mut groups: Vec> = buckets.into_values().filter(|g| g.len() >= 2).collect(); + groups.sort_unstable_by_key(|g| g.first().copied()); + // Each group's extent with the largest saving around its first window; a window inside an + // extent taken seeds nothing more. + charge::(meter, ledger, n)?; + let mut covered = vec![false; n]; + let mut cands = Vec::new(); + let mut steps = n.saturating_mul(8).saturating_add(1024); + let same = |i: usize, j: usize| key(i).is_some() && key(i) == key(j); + for g in groups { + let Some(&p0) = g.first() else { + continue; + }; + let p0 = p0 as usize; + if covered.get(p0).copied().unwrap_or(true) { + continue; + } + // Each later occurrence's extent beside the first's: its first item, and how far the keys + // agree to the left of the window and from its start. + let mut ext: Vec<(usize, usize, usize)> = Vec::new(); + for &pj in g.iter().skip(1).take(MAX_OCCURRENCES) { + let pj = pj as usize; + if pj < p0 + WINDOW || !(0..WINDOW).all(|d| same(p0 + d, pj + d)) { + continue; + } + let mut l = 0usize; + while l < p0 && l + WINDOW < pj - p0 && steps > 0 && same(p0 - 1 - l, pj - 1 - l) { + l += 1; + steps -= 1; + } + let mut r = WINDOW; + while p0 + r + l < pj && pj + r < n && steps > 0 && same(p0 + r, pj + r) { + r += 1; + steps -= 1; + } + ext.push((pj, l, r)); + } + meter.tick(u32::try_from(ext.len().saturating_mul(ext.len())).unwrap_or(u32::MAX))?; + let mut best: Option = None; + for &(_, l, r) in &ext { + let len = l + r; + let weight = span(p0 - l, len); + if weight < min_region { + continue; + } + // The occurrences that hold the extent, none overlapping the one before. + let mut starts = vec![p0 - l]; + for &(pk, lk, rk) in &ext { + let s = pk - l; + if lk >= l && rk >= r && starts.last().is_some_and(|q| s >= q + len) { + starts.push(s); + } + } + if starts.len() < 2 { + continue; + } + let saving = weight.saturating_mul(starts.len() as u64 - 1); + if best.as_ref().is_none_or(|b| saving > b.saving) { + best = Some(Cand { + starts, + len, + saving, + }); + } + } + if let Some(c) = best { + if let Some(first) = c.starts.first() { + for x in covered.iter_mut().skip(*first).take(c.len) { + *x = true; + } + } + cands.push(c); + } + if steps == 0 { + break; + } + } + if cands.is_empty() { + return Ok((None, steps > 0)); + } + cands.sort_unstable_by(|a, b| { + b.saving + .cmp(&a.saving) + .then(a.starts.first().cmp(&b.starts.first())) + }); + let mut cledger = 0usize; + let r = verify_each( + m, + (func, &sp, min_region), + &cands, + meter, + &mut cledger, + verifies, + ); + meter.release(cledger); + let (plan, whole) = r?; + Ok((plan, whole && steps > 0)) +} + +/// The first of `cands` that verifies, within `verifies`, and whether every one was compared. A +/// candidate whose comparison fails at a place is compared again without the item that holds it +/// (keys alike at a region's edge, as an addition after a call's body that reads the body's result +/// at one place and another value at the other), at most [`MAX_TRIMS`] times. +fn verify_each( + m: &TypedModule, + (func, sp, min_region): (FuncId, &Spine, u64), + cands: &[Cand], + meter: &mut Meter, + ledger: &mut usize, + verifies: &mut usize, +) -> Result<(Option, bool), Error> { + let cx = context(m, func, sp, meter, ledger)?; + for c in cands { + let mut cur = Cand { + starts: c.starts.clone(), + len: c.len, + saving: c.saving, + }; + for _ in 0..=MAX_TRIMS { + if *verifies >= MAX_VERIFIES { + return Ok((None, false)); + } + *verifies += 1; + let mut vledger = 0usize; + let r = verify(m, &cx, &cur, meter, &mut vledger); + meter.release(vledger); + match r? { + Ok(plan) => return Ok((Some(plan), false)), + Err(Some(loc)) => match trimmed(sp, &cur, loc, min_region) { + Some(next) => cur = next, + None => break, + }, + Err(None) => break, + } + } + } + Ok((None, true)) +} + +/// Candidate `c` without the item of its first region that holds `loc`: the part before the item +/// or the part after it, whichever weighs more, where that part weighs at least `min_region`. +fn trimmed(sp: &Spine, c: &Cand, loc: Loc, min_region: u64) -> Option { + let s0 = *c.starts.first()?; + // An operand's place, and each item's first place, ordered as the region runs: a block's phis, + // its instructions, then its construct. + let place = |b: BlockId, kind: u8, i: u32| sp.pos(b).map(|p| (p, kind, i)); + let target = match loc { + Loc::Phi { b, .. } => place(b, 0, 0), + Loc::Inst { b, i, .. } => place(b, 1, i), + Loc::Term { b } => place(b, 2, 0), + }?; + let items = sp.items.get(s0..s0 + c.len)?; + let mut j = None; + for (x, it) in items.iter().enumerate() { + let b = *sp.blocks.get(it.spine as usize)?; + let key = if it.inst == TAIL { + place(b, 2, 0) + } else { + place(b, 1, it.inst) + }?; + if key > target { + break; + } + j = Some(x); + } + let j = j?; + let weight = |part: &[Item]| part.iter().fold(0u64, |a, it| a.saturating_add(it.weight)); + let (wa, wb) = (weight(items.get(..j)?), weight(items.get(j + 1..)?)); + let others = (c.starts.len() as u64).saturating_sub(1); + if wa >= wb && wa >= min_region { + Some(Cand { + starts: c.starts.clone(), + len: j, + saving: wa.saturating_mul(others), + }) + } else if wb >= min_region { + Some(Cand { + starts: c.starts.iter().map(|s| s + j + 1).collect(), + len: c.len - j - 1, + saving: wb.saturating_mul(others), + }) + } else { + None + } +} + +/// What every comparison in one function reads: how many operands read each value, each block's +/// predecessors, and its loops and switch-at-use headers. +struct Cx<'s> { + func: FuncId, + sp: &'s Spine, + /// By value: the operands of the function that read it (a loop's exit selector counting one + /// at its header). + uses: Vec, + /// By layout position: the positions of the blocks that branch to it or name it as their + /// merge or continue target. + preds: Vec>, + /// The function's loops: the layout positions of the header and the merge. + loops: Vec<(usize, usize)>, + /// By header: its loop's index in the module's table. + header_of: HashMap, + at_use: HashSet, +} + +fn bump(uses: &mut [u32], v: Value) { + if let Some(u) = uses.get_mut(v.index()) { + *u = u.saturating_add(1); + } +} + +/// Every block `b`'s terminator branches to, then its merge and continue targets. +fn successors(b: &Block) -> Vec { + let mut out = Vec::new(); + let mut term = b.term.clone(); + term.for_each_target_mut(|t| out.push(*t)); + match b.merge { + Some(Merge::Selection { merge }) => out.push(merge), + Some(Merge::Loop { merge, cont }) => { + out.push(merge); + out.push(cont); + } + None => {} + } + out +} + +fn context<'s>( + m: &TypedModule, + func: FuncId, + sp: &'s Spine, + meter: &mut Meter, + ledger: &mut usize, +) -> Result, Error> { + charge::(meter, ledger, m.values.len())?; + let mut uses = vec![0u32; m.values.len()]; + let nl = sp.layout.len(); + charge::<(Vec, [u32; 3])>(meter, ledger, nl)?; + let mut preds: Vec> = vec![Vec::new(); nl]; + for (lp, b) in sp.layout.iter().enumerate() { + let bl = block(m, *b)?; + meter.tick(u32::try_from(bl.insts.len()).unwrap_or(u32::MAX))?; + for p in &bl.phis { + for (v, _) in &p.incoming { + bump(&mut uses, *v); + } + } + for node in &bl.insts { + node.inst.for_each_value(|v| bump(&mut uses, v)); + } + bl.term.for_each_value(|v| bump(&mut uses, v)); + let from = u32::try_from(lp).map_err(|_| invariant("layout"))?; + for t in successors(bl) { + if let Some(q) = sp.pos(t).and_then(|q| preds.get_mut(q)) { + q.push(from); + } + } + } + let mut loops = Vec::new(); + let mut header_of = HashMap::default(); + for (i, l) in m.loops.iter().enumerate() { + let (Some(h), Some(mg)) = (sp.pos(l.header), sp.pos(l.merge)) else { + continue; + }; + loops.push((h, mg)); + header_of.insert(l.header, i); + if let Some(var) = l.exit_sel.and_then(|v| m.vars.get(v.index())) { + bump(&mut uses, var.value); + } + } + charge::<(usize, usize, BlockId)>(meter, ledger, loops.len())?; + charge::(meter, ledger, m.switches_at_use.len())?; + let at_use = m.switches_at_use.iter().copied().collect(); + Ok(Cx { + func, + sp, + uses, + preds, + loops, + header_of, + at_use, + }) +} + +/// One region of a candidate set: its first block and the first instruction of it the region +/// holds, its last block and the instruction of it the region stops before, and their layout +/// positions. +struct Bounds { + first: BlockId, + a: usize, + last: BlockId, + idx: usize, + ps: usize, + pe: usize, +} + +/// The region of items `[s, s + len)` of spine `sp`. +fn bounds(m: &TypedModule, sp: &Spine, s: usize, len: usize) -> Result, Error> { + let (Some(start), Some(end)) = (sp.items.get(s), sp.items.get(s + len)) else { + return Ok(None); + }; + let at = |i: u32| { + sp.blocks + .get(i as usize) + .copied() + .ok_or_else(|| invariant("spine block")) + }; + let (first, last) = (at(start.spine)?, at(end.spine)?); + let a = match start.inst { + TAIL => block(m, first)?.insts.len(), + i => i as usize, + }; + let idx = match end.inst { + TAIL => block(m, last)?.insts.len(), + i => i as usize, + }; + let (Some(ps), Some(pe)) = (sp.pos(first), sp.pos(last)) else { + return Ok(None); + }; + if pe < ps || (pe == ps && idx <= a) { + return Ok(None); + } + Ok(Some(Bounds { + first, + a, + last, + idx, + ps, + pe, + })) +} + +/// The parts of the region's block `k` of `nb` (of `len` instructions) it holds: whether its phis, +/// the range of its instructions, and whether its merge and terminator. +fn parts(bd: &Bounds, k: usize, nb: usize, len: usize) -> (bool, Range, bool) { + if nb == 1 { + return (false, bd.a..bd.idx, false); + } + if k == 0 { + return (false, bd.a..len, true); + } + if k + 1 == nb { + return (true, 0..bd.idx, false); + } + (true, 0..len, true) +} + +/// An operand may be a constant that differs between regions, as a parameter. +const CONST_OK: u8 = 1; +/// An operand is an atomic's pointer, which must be a variable or an access chain's result. +const ATOMIC_PTR: u8 = 2; +/// An operand is the pointer a store writes whole. +const WHOLE_STORE: u8 = 4; + +/// An operand of a region, in walk order: its value, its block's offset in the region, and its +/// flags. +#[derive(Clone, Copy)] +struct Op { + v: Value, + k: u32, + flags: u8, +} + +/// Where an operand of the first region is: incoming value `j` of phi `p` of block `b`, operand +/// `o` (in [`Inst::for_each_value`] order) of instruction `i`, or the value operand of `b`'s +/// terminator. +#[derive(Clone, Copy, Debug)] +enum Loc { + Phi { b: BlockId, p: u32, j: u32 }, + Inst { b: BlockId, i: u32, o: u32 }, + Term { b: BlockId }, +} + +/// What two regions must agree on, part by part: a phi's type and incoming count, an instruction +/// with its value operands erased and its result's type, a block's merge kind and terminator +/// with its operands and targets erased (each target compared by its place in the region). +#[derive(Clone, Debug, PartialEq, Eq)] +enum Part { + Phi { ty: TypeId, incoming: usize }, + Inst { inst: Inst, ty: Option }, + Tail { merge: u8, term: Terminator }, +} + +/// One region read in layout order. +#[derive(Default)] +struct Walk { + defs: Vec, + ops: Vec, + /// Each operand's place: the first region's only. + locs: Vec, + /// Each definition's place: the first region's only. + def_locs: Vec, + /// The first region's parts; another region's are compared as it is read. + parts: Vec, + /// Each part's place: the first region's only. + part_locs: Vec, + /// The region offset of each branch, merge or continue target and of each phi's incoming + /// block, in order. + edges: Vec, + /// Each loop it holds: its unrolling and its exit selector's variable. + loops: Vec<(Unrolling, Option)>, + /// The offsets of the switch-at-use headers it holds. + at_use: Vec, + refused: bool, + /// The part where the walk was first refused, where one was. + failed: Option, +} + +impl Walk { + /// Refuse the region at part `at`. + fn fail(&mut self, at: usize) { + self.refused = true; + self.failed.get_or_insert(at); + } +} + +/// Record `part` at `loc`, or compare it with `reference`'s at the same place; returns its index. +fn note(w: &mut Walk, reference: Option<&Walk>, at: &mut usize, (part, loc): (Part, Loc)) -> usize { + let here = *at; + match reference { + Some(r) => { + if r.parts.get(here) != Some(&part) { + w.fail(here); + } + } + None => { + w.parts.push(part); + w.part_locs.push(loc); + } + } + *at += 1; + here +} + +/// Whether operand `operand` of `inst` (in [`Inst::for_each_value`] order) is an access chain's +/// index into an array, vector or matrix of data, which any value may be. +fn array_index(m: &TypedModule, inst: &Inst, operand: usize) -> Result { + let Inst::AccessChain { base, indices, .. } = inst else { + return Ok(false); + }; + if operand == 0 || operand > indices.len() { + return Ok(false); + } + let Type::Pointer { pointee, .. } = m.types.get(m.value(*base)?.ty)? else { + return Ok(false); + }; + let mut t = *pointee; + for i in indices.iter().take(operand - 1) { + t = match m.types.get(t)? { + Type::Array { elem, .. } + | Type::RuntimeArray { elem, .. } + | Type::Vector { elem, .. } => *elem, + Type::Matrix { col, .. } => *col, + Type::Struct { members, .. } => { + let ValueOrigin::Const(c) = m.value(*i)?.origin else { + return Ok(false); + }; + let member = m + .consts + .scalar_bits(c) + .and_then(|b| usize::try_from(b.0).ok()) + .and_then(|x| members.get(x)); + match member { + Some(x) => *x, + None => return Ok(false), + } + } + _ => return Ok(false), + }; + } + let indexed = matches!( + m.types.get(t)?, + Type::Array { .. } | Type::RuntimeArray { .. } | Type::Vector { .. } | Type::Matrix { .. } + ); + Ok(indexed && !matches!(m.types.facts(t)?.shape, Shape::DescriptorArray { .. })) +} + +/// Walk region `bd` of `cx`'s function, comparing its parts with `reference`'s where given. +fn walk( + m: &TypedModule, + cx: &Cx<'_>, + bd: &Bounds, + reference: Option<&Walk>, +) -> Result { + let mut w = Walk::default(); + let nb = bd.pe - bd.ps + 1; + let mut part_at = 0usize; + let offset = |t: BlockId| { + cx.sp + .pos(t) + .filter(|p| (bd.ps..=bd.pe).contains(p)) + .and_then(|p| u32::try_from(p - bd.ps).ok()) + }; + for k in 0..nb { + let b = *cx + .sp + .layout + .get(bd.ps + k) + .ok_or_else(|| invariant("layout"))?; + let bl = block(m, b)?; + let k32 = u32::try_from(k).map_err(|_| invariant("region block"))?; + let (phis, range, tail) = parts(bd, k, nb, bl.insts.len()); + if phis { + for (pi, p) in bl.phis.iter().enumerate() { + let pi = u32::try_from(pi).map_err(|_| invariant("phi index"))?; + w.defs.push(p.result); + if reference.is_none() { + w.def_locs.push(Loc::Phi { b, p: pi, j: 0 }); + } + let part = Part::Phi { + ty: m.value(p.result)?.ty, + incoming: p.incoming.len(), + }; + let loc = Loc::Phi { b, p: pi, j: 0 }; + let here = note(&mut w, reference, &mut part_at, (part, loc)); + for (j, (v, from)) in p.incoming.iter().enumerate() { + match offset(*from) { + Some(o) if (o as usize) + 1 < nb => w.edges.push(o), + _ => w.fail(here), + } + w.ops.push(Op { + v: *v, + k: k32, + flags: CONST_OK, + }); + if reference.is_none() { + w.locs.push(Loc::Phi { + b, + p: pi, + j: u32::try_from(j).map_err(|_| invariant("phi operand"))?, + }); + } + } + } + } + let start = range.start; + let insts = bl + .insts + .get(range) + .ok_or_else(|| invariant("region instructions"))?; + for (ii, node) in insts.iter().enumerate() { + let i = u32::try_from(start + ii).map_err(|_| invariant("instruction index"))?; + if let Some(r) = node.result { + w.defs.push(r); + if reference.is_none() { + w.def_locs.push(Loc::Inst { b, i, o: 0 }); + } + } + let mut erased = node.inst.clone(); + erased.for_each_value_mut(|v| *v = Value::new(0)); + let ty = match node.result { + Some(r) => Some(m.value(r)?.ty), + None => None, + }; + let part = Part::Inst { inst: erased, ty }; + let here = note( + &mut w, + reference, + &mut part_at, + (part, Loc::Inst { b, i, o: 0 }), + ); + if !rollable(&node.inst) { + w.fail(here); + } + let switched = switched_operand(&node.inst); + let atomic = node.inst.atomic_pointer(); + let store = matches!(node.inst, Inst::Store { .. }); + let mut vals = Vec::new(); + node.inst.for_each_value(|v| vals.push(v)); + for (o, v) in vals.into_iter().enumerate() { + let mut flags = 0u8; + if switched != Some(o) + && (varying_ok(&node.inst, o) || array_index(m, &node.inst, o)?) + { + flags |= CONST_OK; + } + if atomic == Some(v) { + flags |= ATOMIC_PTR; + } + if store && o == 0 { + flags |= WHOLE_STORE; + } + w.ops.push(Op { v, k: k32, flags }); + if reference.is_none() { + w.locs.push(Loc::Inst { + b, + i, + o: u32::try_from(o).map_err(|_| invariant("operand index"))?, + }); + } + } + } + if tail { + let merge = match bl.merge { + None => 0u8, + Some(Merge::Selection { .. }) => 1, + Some(Merge::Loop { .. }) => 2, + }; + let mut term = bl.term.clone(); + term.for_each_target_mut(|t| *t = BlockId::new(0)); + term.for_each_value_mut(|v| *v = Value::new(0)); + let part = Part::Tail { merge, term }; + let here = note(&mut w, reference, &mut part_at, (part, Loc::Term { b })); + match bl.term { + Terminator::Branch(_) + | Terminator::CondBranch { .. } + | Terminator::Switch { .. } + | Terminator::Unreachable => {} + _ => w.fail(here), + } + // Every target is a later block of the region. + for t in successors(bl) { + match offset(t) { + Some(o) if o >= 1 => w.edges.push(o), + _ => w.fail(here), + } + } + let mut vals = Vec::new(); + bl.term.for_each_value(|v| vals.push(v)); + for v in vals { + w.ops.push(Op { + v, + k: k32, + flags: CONST_OK, + }); + if reference.is_none() { + w.locs.push(Loc::Term { b }); + } + } + } + if let Some(&li) = cx.header_of.get(&b) { + let l = m.loops.get(li).ok_or_else(|| invariant("loop"))?; + let sel = match l.exit_sel { + Some(v) => Some( + m.vars + .get(v.index()) + .ok_or_else(|| invariant("variable"))? + .value, + ), + None => None, + }; + w.loops.push((l.unrolling, sel)); + } + if cx.at_use.contains(&b) { + w.at_use.push(k32); + } + if w.refused { + return Ok(w); + } + } + if reference.is_some_and(|r| r.parts.len() != part_at) { + w.fail(part_at); + } + Ok(w) +} + +/// Whether a value of type `t` may be a routine's parameter or result as data. +fn data(m: &TypedModule, t: TypeId) -> Result { + Ok(matches!( + m.types.facts(t)?.shape, + Shape::Plain | Shape::Implicit + )) +} + +/// Whether value `v` may be the argument of a parameter of pointer type `t`: a variable of a class +/// a call may pass, as the builder's calls take them. +fn pointer_arg(m: &TypedModule, t: TypeId, v: Value) -> Result { + let Type::Pointer { class, .. } = m.types.get(t)? else { + return Ok(false); + }; + Ok(match class { + StorageClass::PhysicalStorageBuffer => true, + StorageClass::UniformConstant + | StorageClass::Function + | StorageClass::Private + | StorageClass::Workgroup => matches!( + m.value(v)?.origin, + ValueOrigin::Global(_) | ValueOrigin::Var(_) + ), + _ => false, + }) +} + +/// How often a region reads a value, and its first read's flags and block offset. +#[derive(Clone, Copy)] +struct Seen { + count: u32, + flags: u8, + k: u32, +} + +/// One region of a plan, where it is before the rewrite. +#[derive(Clone, Debug)] +struct Occurrence { + first: BlockId, + a: usize, + last: BlockId, + idx: usize, + /// Its layout position and first instruction, which order the rewrite. + at: (usize, usize), + /// Its blocks in layout order, the first and last included. + blocks: Vec, + /// Its arguments, one per parameter. + args: Vec, + /// Its values read after it, in the routine's result order. + escapes: Vec, + /// The `Function` variables that become the routine's (the first region's) or leave with + /// their region (another's), in the same order. + locals: Vec, +} + +/// A set of regions that are the same code, and the routine they become. +#[derive(Clone, Debug)] +struct Plan { + func: FuncId, + /// The first region (whose code the routine takes), then the others. + occ: Vec, + /// Each parameter's type. + params: Vec, + /// Each operand of the first region that reads a parameter, and which. + subs: Vec<(Loc, usize)>, +} + +/// A candidate's plan, or where its first region's comparison failed, where it can say. +type Verdict = Result>; + +/// The plan for candidate `c`, where its regions are exactly the same code and each can become a +/// call; otherwise the place of the first region whose operand or result kept them in line, where +/// there is one. +fn verify( + m: &TypedModule, + cx: &Cx<'_>, + c: &Cand, + meter: &mut Meter, + ledger: &mut usize, +) -> Result { + let sp = cx.sp; + let mut occ: Vec = Vec::with_capacity(c.starts.len()); + for &s in &c.starts { + match bounds(m, sp, s, c.len)? { + Some(b) => occ.push(b), + None => return Ok(Err(None)), + } + } + let nb = occ.first().map_or(0, |b| b.pe - b.ps + 1); + if nb == 0 || occ.iter().any(|b| b.pe - b.ps + 1 != nb) { + return Ok(Err(None)); + } + // No two regions share a block but where one ends and the next starts. + let mut order: Vec = (0..occ.len()).collect(); + order.sort_unstable_by_key(|&t| occ.get(t).map(|b| (b.ps, b.a))); + for pair in order.windows(2) { + let (Some(x), Some(y)) = ( + pair.first().and_then(|&t| occ.get(t)), + pair.get(1).and_then(|&t| occ.get(t)), + ) else { + return Ok(Err(None)); + }; + if x.pe > y.ps || (x.pe == y.ps && x.idx > y.a) { + return Ok(Err(None)); + } + } + // Each region is entered only at its start and left only at its end: nothing outside it + // branches to, merges at or continues at its blocks, and nothing inside it branches back to + // its first block. Its last block, whose terminator stays outside, continues no loop. + for bd in &occ { + for k in 0..nb { + for &src in cx.preds.get(bd.ps + k).map_or(&[][..], |v| &v[..]) { + let src = src as usize; + let bad = if k == 0 { + (bd.ps..=bd.pe).contains(&src) + } else { + !(bd.ps..bd.pe).contains(&src) + }; + if bad { + return Ok(Err(None)); + } + } + } + if nb > 1 && m.loops.iter().any(|l| l.cont == bd.last) { + return Ok(Err(None)); + } + } + // Each region's walk, compared with the first's. + let mut walks: Vec = Vec::with_capacity(occ.len()); + for bd in &occ { + meter.tick(u32::try_from(bd.pe - bd.ps + 1).unwrap_or(u32::MAX))?; + let w = walk(m, cx, bd, walks.first())?; + if w.refused { + // Where the first region's walk was refused, or the part of it another's differs at. + let first = walks.first().unwrap_or(&w); + return Ok(Err(w + .failed + .and_then(|at| first.part_locs.get(at)) + .copied())); + } + charge::(meter, ledger, w.ops.len())?; + charge::<(Value, Value, u32)>(meter, ledger, w.defs.len())?; + charge::<(Loc, Part)>(meter, ledger, w.parts.len())?; + if let Some(w0) = walks.first() { + let differ = w.ops.len() != w0.ops.len() + || w.defs.len() != w0.defs.len() + || w.edges != w0.edges + || w.at_use != w0.at_use + || w.loops.len() != w0.loops.len() + || w.loops + .iter() + .zip(&w0.loops) + .any(|(x, y)| x.0 != y.0 || x.1.is_some() != y.1.is_some()); + if differ { + return Ok(Err(None)); + } + } + walks.push(w); + } + let Some(w0) = walks.first() else { + return Ok(Err(None)); + }; + // Each region's definitions by place, and what it reads. + let mut def_at: Vec> = Vec::with_capacity(walks.len()); + let mut seen: Vec> = Vec::with_capacity(walks.len()); + for w in &walks { + def_at.push(w.defs.iter().enumerate().map(|(i, v)| (*v, i)).collect()); + let mut s: HashMap = HashMap::default(); + for o in &w.ops { + s.entry(o.v) + .and_modify(|x| x.count = x.count.saturating_add(1)) + .or_insert(Seen { + count: 1, + flags: o.flags, + k: o.k, + }); + } + for v in w.loops.iter().filter_map(|l| l.1) { + s.entry(v) + .and_modify(|x| x.count = x.count.saturating_add(1)) + .or_insert(Seen { + count: 1, + flags: 0, + k: u32::MAX, + }); + } + seen.push(s); + } + // Whether each region is inside a loop of its function, and which of its blocks it always + // runs: the blocks of its own sequence. + let in_loop: Vec = occ + .iter() + .map(|bd| cx.loops.iter().any(|&(h, mg)| h < bd.ps && bd.ps < mg)) + .collect(); + let mut always: Vec> = Vec::with_capacity(occ.len()); + for (bd, &s) in occ.iter().zip(&c.starts) { + let mut f = vec![false; nb]; + for it in sp.items.get(s..=s + c.len).unwrap_or(&[]) { + let b = sp.blocks.get(it.spine as usize).copied(); + if let Some(x) = b + .and_then(|b| sp.pos(b)) + .and_then(|p| p.checked_sub(bd.ps)) + .and_then(|k| f.get_mut(k)) + { + *x = true; + } + } + always.push(f); + } + // A `Function` variable becomes the routine's where only its region reads it and no value of + // it carries from one run of the region to the next. + let local = |t: usize, v: Value| -> Result, Error> { + let ValueOrigin::Var(var) = m.value(v)?.origin else { + return Ok(None); + }; + let Some(x) = seen.get(t).and_then(|s| s.get(&v)) else { + return Ok(None); + }; + if cx.uses.get(v.index()).copied() != Some(x.count) { + return Ok(None); + } + let stored_first = x.flags & WHOLE_STORE != 0 + && always + .get(t) + .and_then(|f| f.get(x.k as usize)) + .copied() + .unwrap_or(false); + let fresh = !in_loop.get(t).copied().unwrap_or(true) || stored_first; + Ok(fresh.then_some(var)) + }; + // Every operand of every region against the first's: a definition at the same place, a local + // variable paired one to one, or a value from before it (a site). + let n_occ = occ.len(); + let mut site_vals: Vec> = vec![Vec::new(); n_occ]; + let mut site_ops: Vec = Vec::new(); + let mut locals: Vec> = vec![Vec::new(); n_occ]; + let mut var_map: Vec> = vec![HashMap::default(); n_occ]; + let mut var_taken: Vec> = vec![HashSet::default(); n_occ]; + for (t, w) in walks.iter().enumerate() { + let (Some(defs0), Some(defst)) = (def_at.first(), def_at.get(t)) else { + return Ok(Err(None)); + }; + meter.tick(u32::try_from(w.ops.len()).unwrap_or(u32::MAX))?; + for (i, (o0, ot)) in w0.ops.iter().zip(&w.ops).enumerate() { + match (defs0.get(&o0.v), defst.get(&ot.v)) { + (Some(x), Some(y)) if x == y => continue, + (None, None) => {} + _ => return Ok(Err(w0.locs.get(i).copied())), + } + match (local(0, o0.v)?, local(t, ot.v)?) { + (Some(x), Some(y)) => { + let map = var_map.get_mut(t).ok_or_else(|| invariant("variables"))?; + match map.get(&x) { + Some(z) if *z == y => {} + Some(_) => return Ok(Err(w0.locs.get(i).copied())), + None => { + let taken = + var_taken.get_mut(t).ok_or_else(|| invariant("variables"))?; + let (Some(vx), Some(vy)) = + (m.vars.get(x.index()), m.vars.get(y.index())) + else { + return Ok(Err(w0.locs.get(i).copied())); + }; + if !taken.insert(y) || vx.pointee != vy.pointee || vx.init != vy.init { + return Ok(Err(w0.locs.get(i).copied())); + } + map.insert(x, y); + locals + .get_mut(t) + .ok_or_else(|| invariant("locals"))? + .push(y); + } + } + continue; + } + (None, None) => {} + _ => return Ok(Err(w0.locs.get(i).copied())), + } + if m.value(o0.v)?.ty != m.value(ot.v)?.ty { + return Ok(Err(w0.locs.get(i).copied())); + } + site_vals + .get_mut(t) + .ok_or_else(|| invariant("sites"))? + .push(ot.v); + if t == 0 { + site_ops.push(i); + } + } + // Each loop's exit selector is a local variable, paired as the region's others are. + for (x, y) in w0.loops.iter().zip(&w.loops) { + match (x.1, y.1) { + (None, None) => {} + (Some(a), Some(b)) => { + let (ValueOrigin::Var(va), ValueOrigin::Var(vb)) = + (m.value(a)?.origin, m.value(b)?.origin) + else { + return Ok(Err(None)); + }; + if var_map.get(t).and_then(|map| map.get(&va)) != Some(&vb) { + return Ok(Err(None)); + } + } + _ => return Ok(Err(None)), + } + } + } + // Parameters: one per distinct set of the regions' values at a site. + let mut params: Vec = Vec::new(); + let mut subs: Vec<(Loc, usize)> = Vec::new(); + let mut by_tuple: BTreeMap, usize> = BTreeMap::new(); + let mut args: Vec> = vec![Vec::new(); n_occ]; + for (s, &oi) in site_ops.iter().enumerate() { + let tuple: Vec = site_vals + .iter() + .map(|x| x.get(s).copied()) + .collect::>() + .ok_or_else(|| invariant("site"))?; + let (Some(&v0), Some(op0)) = (tuple.first(), w0.ops.get(oi)) else { + return Ok(Err(None)); + }; + let same = tuple.iter().all(|v| *v == v0); + if same + && matches!( + m.value(v0)?.origin, + ValueOrigin::Const(_) | ValueOrigin::Global(_) + ) + { + continue; + } + let mut any_const = false; + for v in &tuple { + any_const |= matches!(m.value(*v)?.origin, ValueOrigin::Const(_)); + if m.non_uniform.contains(v) { + return Ok(Err(w0.locs.get(oi).copied())); + } + } + if (any_const && !same && op0.flags & CONST_OK == 0) || op0.flags & ATOMIC_PTR != 0 { + return Ok(Err(w0.locs.get(oi).copied())); + } + let t0 = m.value(v0)?.ty; + if !data(m, t0)? { + for v in &tuple { + if !pointer_arg(m, t0, *v)? { + return Ok(Err(w0.locs.get(oi).copied())); + } + } + } + let p = match by_tuple.get(&tuple) { + Some(p) => *p, + None => { + let p = params.len(); + if p >= MAX_PARAMS { + return Ok(Err(None)); + } + params.push(t0); + for (t, v) in tuple.iter().enumerate() { + args.get_mut(t) + .ok_or_else(|| invariant("arguments"))? + .push(*v); + } + by_tuple.insert(tuple, p); + p + } + }; + let loc = *w0.locs.get(oi).ok_or_else(|| invariant("site place"))?; + subs.push((loc, p)); + } + // Results: each place whose definition some region's code reads after it. + let mut out_idx: Vec = Vec::new(); + for (i, v0) in w0.defs.iter().enumerate() { + let mut escapes = false; + for (t, w) in walks.iter().enumerate() { + let v = *w.defs.get(i).ok_or_else(|| invariant("definition"))?; + let inside = seen.get(t).and_then(|s| s.get(&v)).map_or(0, |x| x.count); + escapes |= cx.uses.get(v.index()).copied().unwrap_or(0) > inside; + } + if !escapes { + continue; + } + if !data(m, m.value(*v0)?.ty)? + || walks + .iter() + .any(|w| w.defs.get(i).is_some_and(|v| m.non_uniform.contains(v))) + { + return Ok(Err(w0.def_locs.get(i).copied())); + } + out_idx.push(i); + } + if out_idx.len() > MAX_RESULTS { + return Ok(Err(None)); + } + let mut plan_occ = Vec::with_capacity(n_occ); + for (t, (bd, w)) in occ.iter().zip(&walks).enumerate() { + let blocks = sp + .layout + .get(bd.ps..=bd.pe) + .ok_or_else(|| invariant("layout"))? + .to_vec(); + let escapes = out_idx + .iter() + .map(|i| w.defs.get(*i).copied().ok_or_else(|| invariant("result"))) + .collect::, _>>()?; + plan_occ.push(Occurrence { + first: bd.first, + a: bd.a, + last: bd.last, + idx: bd.idx, + at: (bd.ps, bd.a), + blocks, + args: args.get(t).cloned().unwrap_or_default(), + escapes, + locals: locals.get(t).cloned().unwrap_or_default(), + }); + } + Ok(Ok(Plan { + func: cx.func, + occ: plan_occ, + params, + subs, + })) +} + +/// Set the operand at `loc` to `v`. +fn set_operand(m: &mut TypedModule, loc: Loc, v: Value) -> Result<(), Error> { + match loc { + Loc::Phi { b, p, j } => { + let slot = block_mut(m, b)? + .phis + .get_mut(p as usize) + .and_then(|p| p.incoming.get_mut(j as usize)) + .ok_or_else(|| invariant("phi operand"))?; + slot.0 = v; + } + Loc::Inst { b, i, o } => { + let node = block_mut(m, b)? + .insts + .get_mut(i as usize) + .ok_or_else(|| invariant("instruction"))?; + let mut at = 0u32; + node.inst.for_each_value_mut(|x| { + if at == o { + *x = v; + } + at += 1; + }); + } + Loc::Term { b } => block_mut(m, b)?.term.for_each_value_mut(|x| *x = v), + } + Ok(()) +} + +/// Make each of `nodes`' results defined in block `b`. +fn set_origins(m: &mut TypedModule, nodes: &[InstNode], b: BlockId) { + for r in nodes.iter().filter_map(|n| n.result) { + if let Some(d) = m.values.get_mut(r.index()) { + d.origin = ValueOrigin::Inst(b); + } + } +} + +/// End block `b` of a routine returning `escapes` (several as a struct of type `ret`). +fn finish_return( + m: &mut TypedModule, + b: BlockId, + escapes: &[Value], + ret: TypeId, +) -> Result<(), Error> { + let term = match escapes { + [] => Terminator::Return, + [one] => Terminator::ReturnValue(*one), + _ => { + let r = new_value(m, ret, ValueOrigin::Inst(b))?; + block_mut(m, b)?.insts.push(InstNode { + result: Some(r), + inst: Inst::CompositeConstruct { + parts: escapes.into(), + }, + }); + Terminator::ReturnValue(r) + } + }; + let bl = block_mut(m, b)?; + bl.merge = None; + bl.term = term; + Ok(()) +} + +/// The call of routine `f` (returning `ret`, whose results have types `results`) that stands for +/// region `o`, in `o`'s first block, and the value each of its results is read from. +fn call_nodes( + m: &mut TypedModule, + o: &Occurrence, + f: FuncId, + ret: TypeId, + results: &[TypeId], +) -> Result<(Vec, Vec), Error> { + let b = o.first; + let call = Inst::Call { + func: f, + args: o.args.clone().into_boxed_slice(), + }; + let mut nodes = Vec::with_capacity(results.len() + 1); + let mut outs = Vec::with_capacity(results.len()); + if results.is_empty() { + nodes.push(InstNode { + result: None, + inst: call, + }); + return Ok((nodes, outs)); + } + let r = new_value(m, ret, ValueOrigin::Inst(b))?; + nodes.push(InstNode { + result: Some(r), + inst: call, + }); + if results.len() == 1 { + outs.push(r); + return Ok((nodes, outs)); + } + for (i, t) in results.iter().enumerate() { + let x = new_value(m, *t, ValueOrigin::Inst(b))?; + let lane = u32::try_from(i).map_err(|_| invariant("result index"))?; + nodes.push(InstNode { + result: Some(x), + inst: Inst::CompositeExtract { + composite: r, + indices: [lane].into(), + }, + }); + outs.push(x); + } + Ok((nodes, outs)) +} + +/// Write `plan`'s first region as a new kept routine, and every region as a call of it. +fn apply(m: &mut TypedModule, plan: &Plan) -> Result<(), Error> { + let caller = plan.func; + let tmpl = plan.occ.first().ok_or_else(|| invariant("no region"))?; + let f = FuncId::new(u32::try_from(m.functions.len()).map_err(|_| invariant("function ids"))?); + // The routine: its parameters, and what it returns. + let mut results = Vec::with_capacity(tmpl.escapes.len()); + for v in &tmpl.escapes { + results.push(m.value(*v)?.ty); + } + let ret = match results.as_slice() { + [] => ty(m, Type::Void)?, + [one] => *one, + _ => ty( + m, + Type::Struct { + members: results.clone().into_boxed_slice(), + layout: None, + }, + )?, + }; + let fn_ty = ty( + m, + Type::Function { + ret, + params: plan.params.clone().into_boxed_slice(), + }, + )?; + let mut params = Vec::with_capacity(plan.params.len()); + for (i, t) in plan.params.iter().enumerate() { + let index = u32::try_from(i).map_err(|_| invariant("parameter index"))?; + params.push(new_value(m, *t, ValueOrigin::Param { func: f, index })?); + } + m.functions.push(Function { + ret, + fn_ty, + params: params.clone(), + blocks: Vec::new(), + vars: tmpl.locals.clone(), + kept: true, + }); + // The first region reads the parameters where it read values from before it, and its + // variables are the routine's. + for (loc, p) in &plan.subs { + let v = *params.get(*p).ok_or_else(|| invariant("parameter"))?; + set_operand(m, *loc, v)?; + } + let moved_vars: HashSet = tmpl.locals.iter().copied().collect(); + for var in &tmpl.locals { + m.vars + .get_mut(var.index()) + .ok_or_else(|| invariant("variable"))? + .func = f; + } + m.functions + .get_mut(caller.index()) + .ok_or_else(|| invariant("function"))? + .vars + .retain(|v| !moved_vars.contains(v)); + // Each region in turn from the last in layout, so a block where one region ends and the next + // starts is cut at the later region first. + let mut order: Vec = (0..plan.occ.len()).collect(); + order.sort_unstable_by_key(|&t| std::cmp::Reverse(plan.occ.get(t).map(|o| o.at))); + let mut rename: HashMap = HashMap::default(); + let mut gone: HashSet = HashSet::default(); + let mut body: Vec = Vec::new(); + for t in order { + let o = plan.occ.get(t).ok_or_else(|| invariant("region"))?; + let (call, outs) = call_nodes(m, o, f, ret, &results)?; + for (v, e) in o.escapes.iter().zip(&outs) { + rename.insert(*v, *e); + } + let rest = if o.blocks.len() == 1 { + // One block: its instructions from `a` to `idx` leave it. + let rest = block_mut(m, o.first)?.insts.split_off(o.idx); + let taken = block_mut(m, o.first)?.insts.split_off(o.a); + if t == 0 { + let e = new_block(m, f)?; + set_origins(m, &taken, e); + block_mut(m, e)?.insts.extend(taken); + finish_return(m, e, &o.escapes, ret)?; + body.push(e); + } else { + removed(m, taken.iter().filter_map(|n| n.result)); + } + rest + } else { + // The first block's instructions from `a`, and its merge and terminator, leave it; the + // last block's after `idx`, and its merge and terminator, are the first block's. + let taken = block_mut(m, o.first)?.insts.split_off(o.a); + if t == 0 { + let e = new_block(m, f)?; + set_origins(m, &taken, e); + block_mut(m, e)?.insts.extend(taken); + move_tail(m, o.first, e)?; + body.push(e); + body.extend(o.blocks.iter().skip(1).copied()); + } else { + removed(m, taken.iter().filter_map(|n| n.result)); + m.switches_at_use.retain(|h| *h != o.first); + gone.extend(o.blocks.iter().skip(1).copied()); + } + let rest = block_mut(m, o.last)?.insts.split_off(o.idx); + set_origins(m, &rest, o.first); + move_tail(m, o.last, o.first)?; + if t == 0 { + for b in o.blocks.iter().skip(1) { + block_mut(m, *b)?.func = f; + } + finish_return(m, o.last, &o.escapes, ret)?; + } + rest + }; + let bs = block_mut(m, o.first)?; + bs.insts.extend(call); + bs.insts.extend(rest); + } + let moved: HashSet = body.iter().copied().collect(); + m.functions + .get_mut(f.index()) + .ok_or_else(|| invariant("routine"))? + .blocks = body; + let cf = m + .functions + .get_mut(caller.index()) + .ok_or_else(|| invariant("function"))?; + cf.blocks.retain(|b| !moved.contains(b)); + let caller_blocks = cf.blocks.clone(); + // Each value a region leaves for after it is read from its call. + let swap = |v: &mut Value| { + if let Some(r) = rename.get(&*v) { + *v = *r; + } + }; + for b in caller_blocks { + if gone.contains(&b) { + continue; + } + let bl = block_mut(m, b)?; + for p in &mut bl.phis { + for (v, _) in &mut p.incoming { + swap(v); + } + } + for n in &mut bl.insts { + n.inst.for_each_value_mut(swap); + } + bl.term.for_each_value_mut(swap); + } + let mut keep_block = vec![true; m.blocks.len()]; + for b in &gone { + if let Some(k) = keep_block.get_mut(b.index()) { + *k = false; + } + } + let mut keep_var = vec![true; m.vars.len()]; + for v in plan.occ.iter().skip(1).flat_map(|o| &o.locals) { + if let Some(k) = keep_var.get_mut(v.index()) { + *k = false; + } + } + m.drop_ids(&[], &keep_block, &keep_var); + Ok(()) +} diff --git a/crates/metal2vulkan/src/spirv/outline/tests.rs b/crates/metal2vulkan/src/spirv/outline/tests.rs new file mode 100644 index 000000000..87a6abc61 --- /dev/null +++ b/crates/metal2vulkan/src/spirv/outline/tests.rs @@ -0,0 +1,506 @@ +//! A set of regions written as a kept routine computes what the regions computed, a region that +//! differs stays in line, and a variable whose value carries from one run of its region to the next +//! is passed to the routine, not made its own. + +use super::*; +use crate::budget::Budget; +use crate::device::profile::DeviceProfile; +use crate::pipeline::sym::{ResourceKind, ResourceSlot, Stage}; +use crate::spirv::builder::constructs::{ArmEnd, LoopSpec}; +use crate::spirv::builder::{FunctionBuilder, ModuleBuilder}; +use crate::spirv::error::BResult; +use crate::spirv::ids::GlobalId; +use crate::spirv::interp::{run, Buffer, Config, Resources}; +use crate::spirv::ir::{natural_layout, BinOp, ExecMode, ExecModel, LayoutRule, MemAccess}; +use crate::spirv::testing::{assert_valid, committed_profiles}; + +fn meter() -> Meter { + Meter::new(&Budget::default()) +} + +fn u32_ty(b: &mut ModuleBuilder) -> TypeId { + b.ty(Type::Int { + width: 32, + signed: false, + }) + .expect("u32") +} + +fn c32(f: &mut FunctionBuilder<'_>, v: u32) -> Value { + let c = f.u32_const(v).expect("constant"); + f.module().const_value(c).expect("value") +} + +fn slot(kind: ResourceKind, index: u32) -> ResourceSlot { + ResourceSlot { + stage: Stage::Kernel, + kind, + index, + } +} + +/// A `Block { u32 data[]; }` storage buffer at buffer slot `index`. +fn buffer(b: &mut ModuleBuilder, index: u32) -> GlobalId { + let u32_ = u32_ty(b); + let rta = b + .ty(Type::RuntimeArray { + elem: u32_, + stride: Some(4), + }) + .expect("runtime array"); + let layout = natural_layout(b.types(), LayoutRule::Std430, true, &[rta]).expect("layout"); + let block = b + .ty(Type::Struct { + members: [rta].into(), + layout: Some(layout), + }) + .expect("block"); + b.resource( + slot(ResourceKind::Buffer, index), + StorageClass::StorageBuffer, + block, + ) + .expect("resource") +} + +fn elem(f: &mut FunctionBuilder<'_>, buf: GlobalId, i: Value) -> BResult { + let zero = c32(f, 0); + let base = f.module().global_value(buf)?; + f.access_chain(base, &[zero, i]) +} + +fn load_at(f: &mut FunctionBuilder<'_>, buf: GlobalId, i: u32) -> BResult { + let i = c32(f, i); + let p = elem(f, buf, i)?; + f.load(p, MemAccess::default()) +} + +fn store_at(f: &mut FunctionBuilder<'_>, buf: GlobalId, i: Value, v: Value) -> BResult<()> { + let p = elem(f, buf, i)?; + f.store(p, v, MemAccess::default()) +} + +fn words(bytes: &[u8]) -> Vec { + bytes + .as_chunks::<4>() + .0 + .iter() + .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]])) + .collect() +} + +fn resources(input: &[u32], out_words: usize) -> Resources { + Resources { + buffers: vec![ + Buffer { + slot: Some(slot(ResourceKind::Buffer, 0)), + address: None, + bytes: input.iter().flat_map(|w| w.to_le_bytes()).collect(), + }, + Buffer { + slot: Some(slot(ResourceKind::Buffer, 1)), + address: None, + bytes: vec![0; out_words * 4], + }, + ], + push: Vec::new(), + spec: Default::default(), + } +} + +fn moltenvk() -> DeviceProfile { + committed_profiles() + .expect("profiles") + .into_iter() + .find(|(s, _)| s == "moltenvk-m3") + .expect("moltenvk-m3") + .1 +} + +/// The eight operations of a region's chain, its fourth `fourth`. +fn chain(fourth: BinOp) -> [(BinOp, u32); 8] { + [ + (BinOp::IMul, 3), + (BinOp::BitwiseXor, 5), + (BinOp::IAdd, 7), + (fourth, 11), + (BinOp::BitwiseXor, 13), + (BinOp::IAdd, 17), + (BinOp::IMul, 19), + (BinOp::BitwiseXor, 23), + ] +} + +/// [`chain`] computed on `x`. +fn apply_chain(x: u32, fourth: BinOp) -> u32 { + chain(fourth).iter().fold(x, |p, (op, k)| match op { + BinOp::IMul => p.wrapping_mul(*k), + BinOp::BitwiseXor => p ^ k, + BinOp::IAdd => p.wrapping_add(*k), + BinOp::ISub => p.wrapping_sub(*k), + _ => panic!("no such chain operation"), + }) +} + +/// Region `j`: `x = seed + 10 (j + 1) + dep` through a variable, [`chain`] on `x` (`p`), `y = x > +/// 25 ? 2 x : x + 1` through a selection's phi, a loop counting a variable initialized to 0 up to +/// 3, and `out[base + 2 + j] = y + count + p`. Returns `y`. +fn region( + f: &mut FunctionBuilder<'_>, + out: GlobalId, + (seed, base, dep): (Value, Value, Value), + j: u32, + fourth: BinOp, +) -> BResult { + let u32_ = u32_ty(f.module()); + let v = f.local_var(u32_, None)?; + let vp = f.var_ptr(v)?; + let add = c32(f, 10 * (j + 1)); + let x0 = f.binary(BinOp::IAdd, seed, add)?; + let x0 = f.binary(BinOp::IAdd, x0, dep)?; + f.store(vp, x0, MemAccess::default())?; + let x = f.load(vp, MemAccess::default())?; + let mut p = x; + for (op, k) in chain(fourth) { + let k = c32(f, k); + p = f.binary(op, p, k)?; + } + let c25 = c32(f, 25); + let big = f.binary(BinOp::UGreaterThan, x, c25)?; + let y = f.if_( + &mut (), + big, + Box::new(move |f, _| { + let two = c32(f, 2); + Ok(ArmEnd::FallThrough(vec![f.binary(BinOp::IMul, x, two)?])) + }), + Some(Box::new(move |f, _| { + let one = c32(f, 1); + Ok(ArmEnd::FallThrough(vec![f.binary(BinOp::IAdd, x, one)?])) + })), + )?; + let y = *y.0.first().expect("phi"); + let zero = f.u32_const(0)?; + let cnt = f.local_var(u32_, Some(zero))?; + let cp = f.var_ptr(cnt)?; + f.loop_( + &mut (), + LoopSpec::default(), + Box::new(move |f, _, lc| { + let c = f.load(cp, MemAccess::default())?; + let three = c32(f, 3); + let done = f.binary(BinOp::UGreaterThanEqual, c, three)?; + f.if_( + &mut (), + done, + Box::new(|f, _| { + lc.break_(f)?; + Ok(ArmEnd::Exits) + }), + None, + )?; + let one = c32(f, 1); + let c1 = f.binary(BinOp::IAdd, c, one)?; + f.store(cp, c1, MemAccess::default()) + }), + )?; + let n = f.load(cp, MemAccess::default())?; + let r = f.binary(BinOp::IAdd, y, n)?; + let r = f.binary(BinOp::IAdd, r, p)?; + let at = c32(f, 2 + j); + let idx = f.binary(BinOp::IAdd, base, at)?; + store_at(f, out, idx, r)?; + Ok(y) +} + +/// One [`region`] after another, region `j`'s chain's fourth operation `fourths[j]`, reading `in = +/// [seed, base, seed2]` and writing `out`; then `out[0] = y + 100` of the last region. With +/// `cross`, each region but the first adds the previous region's `y` where the first adds `seed2`. +fn kernel(fourths: &[BinOp], cross: bool) -> TypedModule { + let mut b = ModuleBuilder::new(); + let input = buffer(&mut b, 0); + let out = buffer(&mut b, 1); + let void = b.ty(Type::Void).expect("void"); + let mut f = b.function(void).expect("function"); + let body = |f: &mut FunctionBuilder<'_>| -> BResult<()> { + let seed = load_at(f, input, 0)?; + let base = load_at(f, input, 1)?; + let seed2 = load_at(f, input, 2)?; + let mut y = seed2; + for (j, fourth) in fourths.iter().enumerate() { + let dep = if cross && j > 0 { y } else { seed2 }; + y = region(f, out, (seed, base, dep), j as u32, *fourth)?; + } + let c100 = c32(f, 100); + let z = f.binary(BinOp::IAdd, y, c100)?; + let zero = c32(f, 0); + store_at(f, out, zero, z)?; + f.ret(None) + }; + body(&mut f).expect("body"); + let main = f.finish().expect("finish"); + b.entry( + ExecModel::GLCompute, + main, + vec![ExecMode::LocalSize { x: 1, y: 1, z: 1 }], + ) + .expect("entry"); + b.finalize().expect("finalize") +} + +/// The words [`kernel`] writes, `base` 0. +fn expected(fourths: &[BinOp], cross: bool, seed: u32, seed2: u32) -> Vec { + let mut out = vec![0u32; 2 + fourths.len()]; + let mut y = seed2; + for (j, fourth) in fourths.iter().enumerate() { + let dep = if cross && j > 0 { y } else { seed2 }; + let x = seed.wrapping_add(10 * (j as u32 + 1)).wrapping_add(dep); + let p = apply_chain(x, *fourth); + y = if x > 25 { + x.wrapping_mul(2) + } else { + x.wrapping_add(1) + }; + out[2 + j] = y.wrapping_add(3).wrapping_add(p); + } + out[0] = y.wrapping_add(100); + out +} + +/// Each kept routine of `m`, with the calls of it in the entry. +fn routines(m: &TypedModule) -> Vec<(FuncId, usize)> { + let entry = &m.functions()[m.entry().func.index()]; + let mut out = Vec::new(); + for (i, f) in m.functions().iter().enumerate() { + if !f.kept { + continue; + } + let id = FuncId::new(i as u32); + let calls = entry + .blocks + .iter() + .flat_map(|b| &m.blocks()[b.index()].insts) + .filter(|n| matches!(n.inst, Inst::Call { func, .. } if func == id)) + .count(); + out.push((id, calls)); + } + out +} + +/// The `OpFunction`s (opcode 54) of a module's words whose control is `DontInline`. +fn dont_inline(words: &[u32]) -> usize { + let mut out = 0; + let mut at = 5; + while at < words.len() { + let (count, op) = ((words[at] >> 16) as usize, words[at] & 0xFFFF); + if op == 54 && words[at + 3] & 0x2 != 0 { + out += 1; + } + at += count.max(1); + } + out +} + +/// Run `before` and `after` over `input` and check both write `want`. +fn same_words(before: &TypedModule, after: &TypedModule, input: &[u32], want: &[u32]) { + let n = want.len(); + let a = run(before, &Config::default(), resources(input, n)).expect("run in line"); + let b = run(after, &Config::default(), resources(input, n)).expect("run as calls"); + assert_eq!(words(&a.buffers[1]), want, "in line, input {input:?}"); + assert_eq!(words(&b.buffers[1]), want, "as calls, input {input:?}"); +} + +/// Three regions that are the same code but their constants, each with its own variables (one +/// with an initializer), a selection merging a value by phi and a counting loop, become calls of +/// one kept routine, which takes their selection and loop; the last region's `y`, read after it, +/// is the routine's result. The module is valid, declares the routine `DontInline`, and writes +/// what the regions wrote, for inputs on both sides of the selection. Each region is under the +/// size measured to run no slower as a routine, so the pass as compiles run it leaves them. +#[test] +fn three_equal_regions_become_calls_of_one_kept_routine_that_computes_the_same() { + let fourths = [BinOp::IMul; 3]; + let orig = kernel(&fourths, false); + let mut kept = orig.clone(); + assert_eq!(outline_entry(&mut kept, &mut meter()).expect("outline"), 0); + let mut m = orig.clone(); + assert_eq!(outline(&mut m, 1, &mut meter()).expect("outline"), 1); + assert_eq!(routines(&m), [(FuncId::new(1), 3)]); + let routine = &m.functions()[1]; + assert_eq!(routine.vars.len(), 2, "the region's two variables"); + assert_eq!(orig.loops().len(), 3); + assert_eq!(m.loops().len(), 1, "the routine's loop"); + let spirv = assert_valid(m.clone(), &moltenvk()); + assert_eq!(dont_inline(&spirv), 1); + for (seed, seed2) in [(1u32, 2u32), (20, 0), (0, 0), (7, 30)] { + let want = expected(&fourths, false, seed, seed2); + same_words(&orig, &m, &[seed, 0, seed2], &want); + } + // Nothing is left to write as a routine. + assert_eq!(outline(&mut m, 1, &mut meter()).expect("outline again"), 0); +} + +/// A chain: each region but the first reads the previous region's `y` where the first reads +/// `seed2`. Every region's `y` is the routine's result, and each call after the first is passed +/// the call before's. +#[test] +fn a_chain_of_regions_passes_each_call_the_result_of_the_one_before() { + let fourths = [BinOp::IMul; 3]; + let orig = kernel(&fourths, true); + let mut m = orig.clone(); + assert_eq!(outline(&mut m, 1, &mut meter()).expect("outline"), 1); + assert_eq!(routines(&m), [(FuncId::new(1), 3)]); + assert_valid(m.clone(), &moltenvk()); + for (seed, seed2) in [(3u32, 4u32), (20, 0), (0, 0), (1, 30)] { + let want = expected(&fourths, true, seed, seed2); + same_words(&orig, &m, &[seed, 0, seed2], &want); + } +} + +/// Of three regions, the middle one's chain subtracts where the others' multiply: the first and +/// last become calls (their keys agree one item past the first region's end, where the next +/// region's first addition reads a value from before it and the kernel's last reads the last +/// region's `y`, so the set is compared again without that item), and the middle one stays in +/// line. Two regions that differ so become nothing. +#[test] +fn a_region_that_differs_stays_in_line_beside_the_calls_of_the_equal_ones() { + let fourths = [BinOp::IMul, BinOp::ISub, BinOp::IMul]; + let orig = kernel(&fourths, false); + let mut m = orig.clone(); + assert_eq!(outline(&mut m, 1, &mut meter()).expect("outline"), 1); + assert_eq!(routines(&m), [(FuncId::new(1), 2)]); + assert_eq!( + m.loops().len(), + 2, + "the routine's loop and the middle region's" + ); + assert_valid(m.clone(), &moltenvk()); + for (seed, seed2) in [(1u32, 2u32), (20, 0), (7, 30)] { + let want = expected(&fourths, false, seed, seed2); + same_words(&orig, &m, &[seed, 0, seed2], &want); + } + let mut two = kernel(&[BinOp::IMul, BinOp::ISub], false); + assert_eq!(outline(&mut two, 1, &mut meter()).expect("outline"), 0); +} + +/// Region `j` of the looped kernel: `a = acc`, `t = a * 5` through a variable it stores first, +/// [`chain`] on `t` (`p`), `acc = a + p`, and `out[2 + j] = acc`. +fn looped_region(f: &mut FunctionBuilder<'_>, out: GlobalId, acc: VarId, j: u32) -> BResult<()> { + let u32_ = u32_ty(f.module()); + let ap = f.var_ptr(acc)?; + let a = f.load(ap, MemAccess::default())?; + let tmp = f.local_var(u32_, None)?; + let tp = f.var_ptr(tmp)?; + let five = c32(f, 5); + let t0 = f.binary(BinOp::IMul, a, five)?; + f.store(tp, t0, MemAccess::default())?; + let t = f.load(tp, MemAccess::default())?; + let mut p = t; + for (op, k) in chain(BinOp::IMul) { + let k = c32(f, k); + p = f.binary(op, p, k)?; + } + let s = f.binary(BinOp::IAdd, a, p)?; + f.store(ap, s, MemAccess::default())?; + let at = c32(f, 2 + j); + store_at(f, out, at, s) +} + +/// A loop of four trips running `k` [`looped_region`]s one after another, region `j` on its own +/// accumulator, a variable initialized to `j`. +fn looped_kernel(k: u32) -> TypedModule { + let mut b = ModuleBuilder::new(); + let input = buffer(&mut b, 0); + let out = buffer(&mut b, 1); + let void = b.ty(Type::Void).expect("void"); + let mut f = b.function(void).expect("function"); + let body = |f: &mut FunctionBuilder<'_>| -> BResult<()> { + let u32_ = u32_ty(f.module()); + let first = load_at(f, input, 0)?; + let mut accs = Vec::new(); + for j in 0..k { + let c = f.u32_const(j)?; + accs.push(f.local_var(u32_, Some(c))?); + } + let zero = f.u32_const(0)?; + let it = f.local_var(u32_, Some(zero))?; + let ip = f.var_ptr(it)?; + f.loop_( + &mut (), + LoopSpec::default(), + Box::new(move |f, _, lc| { + let c = f.load(ip, MemAccess::default())?; + let four = c32(f, 4); + let done = f.binary(BinOp::UGreaterThanEqual, c, four)?; + f.if_( + &mut (), + done, + Box::new(|f, _| { + lc.break_(f)?; + Ok(ArmEnd::Exits) + }), + None, + )?; + for (j, acc) in accs.iter().enumerate() { + looped_region(f, out, *acc, j as u32)?; + } + let one = c32(f, 1); + let c1 = f.binary(BinOp::IAdd, c, one)?; + f.store(ip, c1, MemAccess::default()) + }), + )?; + let zero = c32(f, 0); + store_at(f, out, zero, first)?; + f.ret(None) + }; + body(&mut f).expect("body"); + let main = f.finish().expect("finish"); + b.entry( + ExecModel::GLCompute, + main, + vec![ExecMode::LocalSize { x: 1, y: 1, z: 1 }], + ) + .expect("entry"); + b.finalize().expect("finalize") +} + +/// The words [`looped_kernel`] writes for `in[0] = first`. +fn looped_expected(k: u32, first: u32) -> Vec { + let mut out = vec![0u32; 2 + k as usize]; + let mut accs: Vec = (0..k).collect(); + for _ in 0..4 { + for (j, acc) in accs.iter_mut().enumerate() { + let a = *acc; + *acc = a.wrapping_add(apply_chain(a.wrapping_mul(5), BinOp::IMul)); + out[2 + j] = *acc; + } + } + out[0] = first; + out +} + +/// Two regions in a loop, each on its own accumulator: the accumulator, read before it is written, +/// carries its value from one trip to the next, so the routine is passed it as a pointer; the +/// variable each region stores before it reads it becomes the routine's own. Both regions are +/// one block each, and the module writes what they wrote. +#[test] +fn a_variable_read_before_it_is_written_in_a_loop_is_passed_to_the_routine() { + let orig = looped_kernel(2); + let mut m = orig.clone(); + assert_eq!(outline(&mut m, 1, &mut meter()).expect("outline"), 1); + assert_eq!(routines(&m), [(FuncId::new(1), 2)]); + let routine = &m.functions()[1]; + assert_eq!(routine.vars.len(), 1, "the variable stored first"); + let pointers = routine + .params + .iter() + .filter(|p| { + let t = m.value(**p).expect("parameter").ty; + matches!(m.types().get(t).expect("type"), Type::Pointer { .. }) + }) + .count(); + assert_eq!(pointers, 1, "the accumulator"); + assert_valid(m.clone(), &moltenvk()); + for first in [0u32, 9] { + same_words(&orig, &m, &[first], &looped_expected(2, first)); + } +} diff --git a/crates/metal2vulkan/src/spirv/roll/mod.rs b/crates/metal2vulkan/src/spirv/roll/mod.rs index 315aa43f3..99cff27be 100644 --- a/crates/metal2vulkan/src/spirv/roll/mod.rs +++ b/crates/metal2vulkan/src/spirv/roll/mod.rs @@ -115,7 +115,7 @@ const MAX_PAIRS: usize = 8; /// The exact comparisons per search. const MAX_VERIFIES: usize = 32; /// [`Item::inst`] of a construct (or the spine's end). -const TAIL: u32 = u32::MAX; +pub(in crate::spirv) const TAIL: u32 = u32::MAX; /// The most functions searched per run: the heaviest the entry reaches. const MAX_FUNCS: usize = 8; /// The deepest arm searched: a sequence inside this many selections and switches. @@ -124,8 +124,8 @@ const MAX_DEPTH: u32 = 8; /// The pass's maps and sets of ids, hashed by [`Fnv`]'s word mix rather than SipHash: their keys /// are the module's own dense ids, and no output depends on their order (each one the pass walks /// is sorted first, or only read by key). -type HashMap = std::collections::HashMap>; -type HashSet = std::collections::HashSet>; +pub(in crate::spirv) type HashMap = std::collections::HashMap>; +pub(in crate::spirv) type HashSet = std::collections::HashSet>; fn invariant(detail: impl Into) -> Error { Error::Internal(InternalError::new(InternalKind::Invariant, detail)) @@ -188,16 +188,21 @@ const UNKEYED: u64 = u64::MAX; /// first region's read turns into a copy of the trip's lane, and a copy's key is never read /// (every reader skips copies). Everything else the roll writes takes a new result. #[derive(Default)] -struct KeyCache { +pub(in crate::spirv) struct KeyCache { /// By value: its instruction's key, or [`UNKEYED`]. keys: Vec, /// The bytes charged for `keys`. - charged: usize, + pub(in crate::spirv) charged: usize, } impl KeyCache { /// The key of `node`, kept or taken now. - fn key(&mut self, m: &TypedModule, node: &InstNode, meter: &mut Meter) -> Result { + pub(in crate::spirv) fn key( + &mut self, + m: &TypedModule, + node: &InstNode, + meter: &mut Meter, + ) -> Result { let Some(r) = node.result else { return inst_key(m, node); }; @@ -233,7 +238,7 @@ pub fn entry_weight(m: &TypedModule, meter: &mut Meter) -> Result { } /// [`entry_weight`] of every function. -fn weights(m: &TypedModule, meter: &mut Meter) -> Result, Error> { +pub(in crate::spirv) fn weights(m: &TypedModule, meter: &mut Meter) -> Result, Error> { let n = m.functions.len(); let mut w = vec![0u64; n]; // 0: not reached, 1: callees pending, 2: weighed. @@ -318,7 +323,7 @@ fn inst_weight(node: &InstNode, w: &[u64]) -> u64 { /// of is mixed in whole (a multiply and a shift, each a bijection of the state), where a byte-wise /// FNV-1a took a multiply per byte. Keys are only compared for equality, never ordered, so the /// hash function decides nothing but which keys collide. -struct Fnv(u64); +pub(in crate::spirv) struct Fnv(u64); impl Default for Fnv { fn default() -> Fnv { @@ -400,22 +405,22 @@ impl Hasher for Fnv { /// An item of the spine: instruction `inst` of spine block `spine`, or ([`TAIL`]) the construct /// that block heads, its merge's phis included, or the spine's end. #[derive(Clone, Copy, Debug)] -struct Item { - spine: u32, - inst: u32, - key: u64, - weight: u64, +pub(in crate::spirv) struct Item { + pub(in crate::spirv) spine: u32, + pub(in crate::spirv) inst: u32, + pub(in crate::spirv) key: u64, + pub(in crate::spirv) weight: u64, } /// The entry's top-level code (see the module docs). -struct Spine { +pub(in crate::spirv) struct Spine { /// The layout position of each module block in the entry (`u32::MAX`: another function's). - pos: Vec, + pub(in crate::spirv) pos: Vec, /// The entry's blocks in layout order. - layout: Vec, + pub(in crate::spirv) layout: Vec, /// The spine's blocks, in order. - blocks: Vec, - items: Vec, + pub(in crate::spirv) blocks: Vec, + pub(in crate::spirv) items: Vec, /// By layout position, each instruction's [`inst_key`] (0 for a copy, which no key reads), /// taken once: an item's key, a construct's (which combines its instructions'), and the /// alignment of two regions read it. @@ -423,7 +428,7 @@ struct Spine { } impl Spine { - fn pos(&self, b: BlockId) -> Option { + pub(in crate::spirv) fn pos(&self, b: BlockId) -> Option { self.pos .get(b.index()) .copied() @@ -439,17 +444,21 @@ impl Spine { } } -fn block(m: &TypedModule, b: BlockId) -> Result<&Block, Error> { +pub(in crate::spirv) fn block(m: &TypedModule, b: BlockId) -> Result<&Block, Error> { m.blocks.get(b.index()).ok_or_else(|| invariant("block")) } -fn block_mut(m: &mut TypedModule, b: BlockId) -> Result<&mut Block, Error> { +pub(in crate::spirv) fn block_mut(m: &mut TypedModule, b: BlockId) -> Result<&mut Block, Error> { m.blocks .get_mut(b.index()) .ok_or_else(|| invariant("block")) } -fn charge(meter: &mut Meter, ledger: &mut usize, n: usize) -> Result<(), Error> { +pub(in crate::spirv) fn charge( + meter: &mut Meter, + ledger: &mut usize, + n: usize, +) -> Result<(), Error> { let bytes = n.saturating_mul(std::mem::size_of::()); meter.charge(bytes)?; *ledger = ledger.saturating_add(bytes); @@ -539,11 +548,12 @@ fn fixed(m: &TypedModule, sp: &Spine, lo: usize, inv: &HashSet, v: Value) } /// The entry's spine: its top-level sequence, then the arms of its selections and switches, -/// each a sequence of its own, to [`MAX_DEPTH`] constructs deep. Each sequence ends in an item +/// each a sequence of its own, to [`MAX_DEPTH`] constructs deep, and with `loops` the body of +/// each loop too (from its header's branch to its continue block). Each sequence ends in an item /// whose key no other item has, so no run crosses from one into another. -fn spine( +pub(in crate::spirv) fn spine( m: &TypedModule, - (func, w): (FuncId, &[u64]), + (func, w, loops): (FuncId, &[u64], bool), cache: &mut KeyCache, meter: &mut Meter, ledger: &mut usize, @@ -697,6 +707,24 @@ fn spine( key: h.finish(), weight, }); + // A loop's body is a sequence of its own, where asked: from its header's branch to + // its continue block. + if loops && depth < MAX_DEPTH { + if let (None, Terminator::Branch(h)) = (&b.merge, &b.term) { + let hb = block(m, *h)?; + if let (Some(Merge::Loop { cont, .. }), Terminator::Branch(first)) = + (hb.merge, &hb.term) + { + let hi = sp.pos(cont).unwrap_or(0); + if let Some(fp) = sp.pos(*first).filter(|p| *p > lp + 1 && *p < hi) { + if let Some(x) = begun.get_mut(fp).filter(|x| !**x) { + *x = true; + seqs.push((*first, hi, depth + 1)); + } + } + } + } + } // The arms of a selection or switch are sequences of their own. if depth < MAX_DEPTH && matches!(b.merge, Some(Merge::Selection { .. })) { let mut arms = Vec::new(); @@ -807,7 +835,7 @@ fn find_in( ledger: &mut usize, verifies: &mut usize, ) -> Result, Error> { - let sp = spine(m, (func, w), cache, meter, ledger)?; + let sp = spine(m, (func, w, false), cache, meter, ledger)?; let n = sp.items.len(); // What each value of the function is through its copies. charge::(meter, ledger, m.values.len())?; @@ -1143,7 +1171,7 @@ fn region_insts(b: &Block, range: Range) -> Vec<(usize, &InstNode)> { /// Whether a value may differ by trip in operand `operand` of `inst`: an operand any value of /// its type may stand in. -fn varying_ok(inst: &Inst, operand: usize) -> bool { +pub(in crate::spirv) fn varying_ok(inst: &Inst, operand: usize) -> bool { match inst { Inst::Store { .. } => operand == 1, Inst::Unary { .. } @@ -1169,7 +1197,7 @@ fn varying_ok(inst: &Inst, operand: usize) -> bool { /// Whether a region may hold `inst`: an instruction the stage executes at most once (interlock, /// mesh output counts) may not be repeated by a loop. -fn rollable(inst: &Inst) -> bool { +pub(in crate::spirv) fn rollable(inst: &Inst) -> bool { !matches!( inst, Inst::BeginInterlock | Inst::EndInterlock | Inst::SetMeshOutputs { .. } @@ -1190,7 +1218,7 @@ fn image_use(inst: &Inst) -> bool { /// The operand of `inst` (in [`Inst::for_each_value`] order) that must be a constant but may /// differ by trip through an `OpSwitch` on the trip index ([`split_by_trip`]): an image /// instruction's `ConstOffset`. -fn switched_operand(inst: &Inst) -> Option { +pub(in crate::spirv) fn switched_operand(inst: &Inst) -> Option { let (lead, ops) = match inst { Inst::ImageSample { dref, ops, .. } => (2 + usize::from(dref.is_some()), ops), Inst::ImageFetch { ops, .. } | Inst::ImageGather { ops, .. } => (2, ops), @@ -2173,7 +2201,7 @@ fn verify( } /// Intern type `t`, declaring it when new. -fn ty(m: &mut TypedModule, t: Type) -> Result { +pub(in crate::spirv) fn ty(m: &mut TypedModule, t: Type) -> Result { let (id, fresh) = m.types.intern(t, &m.consts)?; if fresh { m.decls.push(Decl::Type(id)); @@ -2181,7 +2209,11 @@ fn ty(m: &mut TypedModule, t: Type) -> Result { Ok(id) } -fn new_value(m: &mut TypedModule, ty: TypeId, origin: ValueOrigin) -> Result { +pub(in crate::spirv) fn new_value( + m: &mut TypedModule, + ty: TypeId, + origin: ValueOrigin, +) -> Result { let v = Value::new(u32::try_from(m.values.len()).map_err(|_| invariant("value ids"))?); m.values.push(ValueDef { ty, origin }); Ok(v) @@ -2234,7 +2266,7 @@ fn local(m: &mut TypedModule, entry: FuncId, pointee: TypeId) -> Result Result { +pub(in crate::spirv) fn new_block(m: &mut TypedModule, func: FuncId) -> Result { let id = BlockId::new(u32::try_from(m.blocks.len()).map_err(|_| invariant("block ids"))?); m.blocks.push(Block { func, @@ -2262,7 +2294,7 @@ fn push_void(m: &mut TypedModule, b: BlockId, inst: Inst) -> Result<(), Error> { } /// Mark `results` (of instructions and phis that left their block) removed. -fn removed(m: &mut TypedModule, results: impl Iterator) { +pub(in crate::spirv) fn removed(m: &mut TypedModule, results: impl Iterator) { for r in results { if let Some(d) = m.values.get_mut(r.index()) { d.origin = ValueOrigin::Removed; @@ -2272,7 +2304,11 @@ fn removed(m: &mut TypedModule, results: impl Iterator) { /// Move the merge and terminator of `from` to `to` (a new block that takes over `from`'s /// edges): the successors' phis, a loop's preheader, and a switch-at-use header follow. -fn move_tail(m: &mut TypedModule, from: BlockId, to: BlockId) -> Result<(), Error> { +pub(in crate::spirv) fn move_tail( + m: &mut TypedModule, + from: BlockId, + to: BlockId, +) -> Result<(), Error> { let (merge, term) = { let b = block(m, from)?; (b.merge, b.term.clone()) diff --git a/crates/metal2vulkan/src/spirv/serialize/mod.rs b/crates/metal2vulkan/src/spirv/serialize/mod.rs index 38029ffb5..3e92863b3 100644 --- a/crates/metal2vulkan/src/spirv/serialize/mod.rs +++ b/crates/metal2vulkan/src/spirv/serialize/mod.rs @@ -14,7 +14,8 @@ //! `?:` arm or a `&&`/`||` operand. A value SPIRV-Cross would forward into its one reader without //! the dependencies of its operands is written the same way (`forwards_without_dependencies`), so //! it is evaluated where the module does, before any later write overwrites what it reads. A kept -//! routine (a texel routine's sum or a sampling routine's emulation) is declared `DontInline`. +//! routine (a texel routine's sum, a sampling routine's emulation, or a repeated region +//! `spirv::outline` wrote once) is declared `DontInline`. //! //! Every number link assigns (descriptor set and binding, `Location`/`Component`, `SpecId`) is a //! literal operand of an `OpDecorate`. The serializer writes 0 there and records a relocation diff --git a/docs/API.md b/docs/API.md index 6721b6220..8b6804635 100644 --- a/docs/API.md +++ b/docs/API.md @@ -523,10 +523,14 @@ assert!(stats.reachable_blocks <= stats.blocks); blocks are the program's, while a module that calls a many-block function from many sites holds a program far larger than its own blocks; the two read alike in `blocks` alone. Lowering emits such functions itself: the handle-table search (unrolled branch-free steps) and the root-table - search (a loop), each emitted once and called at every access. + search (a loop), each emitted once and called at every access. A region of 500 or more + instructions that a function holds more than once (a function the program calls from several + places, inlined at each) is written once too, as a routine declared `DontInline` and called + from each place: NVIDIA's driver compiles it once, and RADV and ANV (Mesa 26.2.6) compile the + same code with or without the control. - `entry_weight` is what a driver compiles once it has inlined every call: the entry's instructions with each call counted as its callee's body, after repeated regions are rolled - into loops. A stage past `pipeline::COMPILED_STAGE_WEIGHT` (99,109, the heaviest measured to + into loops (a region written as a routine counts at each of its calls). A stage past `pipeline::COMPILED_STAGE_WEIGHT` (99,109, the heaviest measured to compile within host-sized memory on the slowest compiler measured) is one whose creation may take the host's memory: the translator makes it all the same, and `metal2vulkan-runtime` compiles such a kernel only in a helper process the consumer caps (its README, "Kernels past diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index ab7bddbeb..fa9277d8f 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -23,10 +23,11 @@ metallib | AIR bitcode ── structurize ─► a structured tree over the AIR CFG structurize ── emit ────────► a typed SPIR-V module through the builder structurize::emit, lower::*, spirv::builder ── tidy ────────► single-store promotion, dead values removed, structurize::promote, spirv::{dead, roll, - half values held at 16 bits under the 16-bit float hoist, half_hold} + half values held at 16 bits under the 16-bit float hoist, outline, half_hold} controls, repeated regions of large entries rolled into loops, loop-invariant loads of uniform views - moved before their loops + moved before their loops, repeated regions of 500 + instructions or more written once as kept routines ── verify ──────► the full verifier; requirements within the profile spirv::verify, spirv::requirements ── serialize ───► SPIR-V 1.5 words with typed relocations, held to spirv::serialize the word cap @@ -75,7 +76,7 @@ rest is `crates/metal2vulkan/src/`. | `memory/` | Pointer provenance, the `MemoryPlan`, leaf access emitters, arenas, root and handle lookups, word composition, atomics; `memory::views`, a descriptor slot's views: a storage block or a uniform block | | `lower/` | `select` (routing every instruction, intrinsic and parameter into the `Plan`), `strategy`, `push` (push-constant placement), `specialized` (bound-texture values read as push data), and one module per family: `arith` (each float operation in the AIR's order, decorated by its own fast-math flags, so the device's compiler groups and fuses only what they allow; `half_chain`, a fast-math `half` chain held at `float` precision between its roundings where the device has no 16-bit floats), `access`, `texture`, `sampler`, `simd`, `sync`, `fragment`, `stage_io`, `dispatch`, `tessellation`, `mesh`, `raytracing` | | `structurize/` | The structurizer over the AIR CFG, the emit walk that drives the families, and single-store promotion | -| `spirv/` | The typed SPIR-V IR and interners, the module and function builders, dead-value removal, the roll pass, the move of loop-invariant loads of uniform views out of their loops (`hoist`), the 16-bit holding of `half` values under the 16-bit float controls (`half_hold`), the verifier, requirement derivation and the serializer. `spirv::interp` is a CPU interpreter used only by tests | +| `spirv/` | The typed SPIR-V IR and interners, the module and function builders, dead-value removal, the roll pass, the writing of repeated regions as kept routines (`outline`), the move of loop-invariant loads of uniform views out of their loops (`hoist`), the 16-bit holding of `half` values under the 16-bit float controls (`half_hold`), the verifier, requirement derivation and the serializer. `spirv::interp` is a CPU interpreter used only by tests | | `spv.rs` | The SPIR-V grammar's opcodes and enumerants the translator names, generated from Khronos's grammar (`scripts/regen-spirv-grammar/`) | | `spirv_binary/` | A reader of SPIR-V's binary form over the grammar's instruction table, used only by tests | | `device/` | `DeviceDescription` (from a Khronos profile, or from `ash`), `DevicePolicy`, and the derived `DeviceProfile` every decision reads |