Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/victory-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
48 changes: 48 additions & 0 deletions crates/victory-shader/benches/ir_build.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
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]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Unintended scalar input in benchmark

program reads s0 in every final add, despite documenting two vector operands. The benchmark keeps s0 live through every branch alongside the intended VGPRs.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

}
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]
);
}
}
41 changes: 28 additions & 13 deletions crates/victory-shader/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ fn rename(mut blocks: Vec<Block>, 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[..] {
[] => {
Expand All @@ -550,7 +553,7 @@ fn rename(mut blocks: Vec<Block>, 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 {
Expand All @@ -572,19 +575,24 @@ fn rename(mut blocks: Vec<Block>, 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 {
Expand Down Expand Up @@ -889,9 +897,16 @@ fn members(set: &RegSet) -> impl Iterator<Item = Reg> + '_ {
}

/// `outs` is sorted by register, and holds every register live out.
fn lookup(outs: &[(Reg, Value)], reg: Reg) -> Value {
outs.binary_search_by_key(&reg, |&(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<usize> {
Expand Down
Loading