From 8901cf190c28c4675bc5fba007f58221b70ffa1d Mon Sep 17 00:00:00 2001 From: stoltembergg Date: Wed, 25 Nov 2026 17:05:41 +0000 Subject: [PATCH] Shaders: walk predecessor live-outs once during IR renaming Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/victory-shader/Cargo.toml | 4 ++ crates/victory-shader/benches/ir_build.rs | 48 +++++++++++++++++++++++ crates/victory-shader/src/ir.rs | 41 +++++++++++++------ 3 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 crates/victory-shader/benches/ir_build.rs diff --git a/crates/victory-shader/Cargo.toml b/crates/victory-shader/Cargo.toml index 3d0caf1c..9b1624ed 100644 --- a/crates/victory-shader/Cargo.toml +++ b/crates/victory-shader/Cargo.toml @@ -17,6 +17,10 @@ host = ["dep:windows"] # Debugging switches (emitted-module dumps and the like). diagnostics = [] +[[bench]] +name = "ir_build" +harness = false + [target.'cfg(windows)'.dependencies] windows-sys = { workspace = true, features = ["Win32_System_LibraryLoader"] } windows = { workspace = true, optional = true, features = [ diff --git a/crates/victory-shader/benches/ir_build.rs b/crates/victory-shader/benches/ir_build.rs new file mode 100644 index 00000000..6a406502 --- /dev/null +++ b/crates/victory-shader/benches/ir_build.rs @@ -0,0 +1,48 @@ +//! Time the IR's construction (liveness, renaming, phis) on synthetic +//! programs; they need no title files or GPU. + +use std::hint::black_box; +use std::time::Instant; + +use victory_shader::ir; + +/// `regs` VGPRs defined first and read at the end, so they are live +/// through `ifs` divergent branches in between. +fn program(regs: u32, ifs: usize) -> Vec { + let mut words = Vec::new(); + for i in 0..regs { + words.push(0x7E00_0280 | (i << 17)); // v_mov_b32 v[i], 0 + } + for _ in 0..ifs { + words.extend([ + 0x7D84_0080, // v_cmp_eq_u32 vcc, 0, v0 + 0xBE82_246A, // s_and_saveexec_b64 s[2:3], vcc + 0xBF88_0001, // s_cbranch_execz +1 + 0x4A00_0081, // v_add_nc_u32 v0, 1, v0 + 0x88FE_027E, // s_or_b64 exec, exec, s[2:3] + ]); + } + for i in 0..regs { + words.push(0x4A00_0000 | (i << 9) | (i << 17)); // v_add_nc_u32 v[i], v[i], v[i] + } + words.push(0xBF81_0000); // s_endpgm + words.into_iter().flat_map(u32::to_le_bytes).collect() +} + +fn main() { + for (regs, ifs) in [(8, 500), (64, 500), (128, 500), (8, 2000), (128, 2000)] { + let code = program(regs, ifs); + black_box(ir::build(&code, true, 256).unwrap()); + let mut samples = [std::time::Duration::ZERO; 9]; + for sample in &mut samples { + let start = Instant::now(); + black_box(ir::build(black_box(&code), true, 256).unwrap()); + *sample = start.elapsed(); + } + samples.sort_unstable(); + println!( + "{regs} registers, {ifs} divergent ifs: {:?} (median)", + samples[samples.len() / 2] + ); + } +} diff --git a/crates/victory-shader/src/ir.rs b/crates/victory-shader/src/ir.rs index ca6c58aa..083cf7a9 100644 --- a/crates/victory-shader/src/ir.rs +++ b/crates/victory-shader/src/ir.rs @@ -542,6 +542,9 @@ fn rename(mut blocks: Vec, wave64: bool, vgprs: u16) -> Program { for &b in &order { current.fill(NONE); let block = &mut blocks[b]; + // `members` goes up in register order, as does `outs`: one walk + // along a predecessor's `outs` finds every register. + let mut walk = 0; for reg in members(&live_in[b]) { current[index(reg)] = match block.preds[..] { [] => { @@ -550,7 +553,7 @@ fn rename(mut blocks: Vec, wave64: bool, vgprs: u16) -> Program { v } // A single predecessor comes earlier in reverse postorder. - [p] => lookup(&outs[p], reg), + [p] => lookup_from(&outs[p], &mut walk, reg), _ => { let value = new_value(); block.phis.push(Phi { @@ -572,19 +575,24 @@ fn rename(mut blocks: Vec, wave64: bool, vgprs: u16) -> Program { current[index(*reg)] = *v; } } - outs[b] = members(&live_out[b]) - .map(|reg| (reg, current[index(reg)])) - .collect(); + let live = live_out[b].iter().map(|w| w.count_ones() as usize).sum(); + let mut out = Vec::with_capacity(live); + out.extend(members(&live_out[b]).map(|reg| (reg, current[index(reg)]))); + outs[b] = out; } for block in &mut blocks { + // The phis are in register order (they were pushed by `members`): + // one walk along each predecessor's `outs` serves them all. for phi in &mut block.phis { - phi.args = block - .preds - .iter() - .map(|&p| lookup(&outs[p], phi.reg)) - .collect(); - debug_assert!(!phi.args.contains(&NONE), "{phi:?}"); + phi.args = Vec::with_capacity(block.preds.len()); } + for &p in &block.preds { + let mut walk = 0; + for phi in &mut block.phis { + phi.args.push(lookup_from(&outs[p], &mut walk, phi.reg)); + } + } + debug_assert!(block.phis.iter().all(|phi| !phi.args.contains(&NONE))); } remove_trivial_phis(&mut blocks, values); Program { @@ -889,9 +897,16 @@ fn members(set: &RegSet) -> impl Iterator + '_ { } /// `outs` is sorted by register, and holds every register live out. -fn lookup(outs: &[(Reg, Value)], reg: Reg) -> Value { - outs.binary_search_by_key(®, |&(r, _)| r) - .map_or(NONE, |i| outs[i].1) +/// `at` is where the walk is along it: ask for registers in rising order +/// and every one costs a step, not a search. +fn lookup_from(outs: &[(Reg, Value)], at: &mut usize, reg: Reg) -> Value { + while outs.get(*at).is_some_and(|&(r, _)| r < reg) { + *at += 1; + } + match outs.get(*at) { + Some(&(r, v)) if r == reg => v, + _ => NONE, + } } pub(crate) fn reverse_postorder(blocks: &[Block]) -> Vec {