From b9c8a8b0ffc007b2f48fa1340206de542cad2a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B0=D0=B7=D0=B0=D1=80?= Date: Thu, 26 Nov 2026 01:49:44 +0400 Subject: [PATCH] victory-shader: build the per-instruction read/write lists once (#1033) ir::build fills every instruction's register lists twice. `effects()` collects `Vec` (two allocations), and the caller immediately maps each one into `Vec<(Reg, Value)>` (two more), freeing the first pair. For lists that are almost always two to four entries, that is four allocations and two copies per instruction, on every instruction of every program. Have `effects()` and its helpers collect the `(Reg, Value)` pairs directly, with the placeholder the SSA pass fills in. Two allocations per instruction instead of four; `Inst` and everything downstream are unchanged. Measured with the synthetic bench in #1002 (no game data), ten interleaved rounds per arm, nanoseconds per instruction, at 256/1k/4k/16k instructions: ir::build 308 -> 269, 375 -> 324, 407 -> 340, 402 -> 353 (0.835-0.878) translation 898 -> 872, 898 -> 825, 951 -> 875, 956 -> 912 (0.919-0.971) gfx10::decode, structure, lower and the rest are unchanged. The emitted DXIL is byte-identical at all four sizes. cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored. Both clippy configurations clean; cargo xtask policy ok. Co-authored-by: pes7 --- crates/victory-shader/src/ir.rs | 50 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/crates/victory-shader/src/ir.rs b/crates/victory-shader/src/ir.rs index d32861a..0084c30 100644 --- a/crates/victory-shader/src/ir.rs +++ b/crates/victory-shader/src/ir.rs @@ -211,12 +211,11 @@ pub fn build_bounded( } let m0 = m0_max.get(&machine.offset).copied(); if let Some((reads, writes)) = effects(&machine, &fields, name, lanes, vgprs, m0)? { - let unset = |regs: Vec| regs.into_iter().map(|r| (r, NONE)).collect(); blocks[b].insts.push(Inst { machine, fields, - reads: unset(reads), - writes: unset(writes), + reads, + writes, }); } } @@ -700,8 +699,9 @@ const DROPPED: &[&str] = &[ "v_clrexcp", ]; -/// Registers read, registers written. -type Effects = (Vec, Vec); +/// Registers read, registers written, each with a placeholder value the +/// SSA pass fills in. +type Effects = (Vec<(Reg, Value)>, Vec<(Reg, Value)>); /// Registers an instruction reads and writes, one per dword, or `None` if /// the IR drops it. @@ -720,9 +720,9 @@ fn effects( offset: inst.offset, what: name, }; - let mask = |code: u8| (0..lanes).map(move |i| Reg::S(code + i)); + let mask = |code: u8| (0..lanes).map(move |i| (Reg::S(code + i), NONE)); let (mut r, mut w) = (Vec::new(), Vec::new()); - let regs = |op: &Operand, out: &mut Vec| { + let regs = |op: &Operand, out: &mut Vec<(Reg, Value)>| { add_regs(op, lanes, out).map_err(|_| ShaderError::Isa { offset: inst.offset, reason: "register range out of bounds", @@ -757,21 +757,21 @@ fn effects( || name.starts_with("s_cselect_") || name.starts_with("s_cmov_") { - r.push(Reg::Scc); + r.push((Reg::Scc, NONE)); } if name.contains("saveexec") || name.contains("wrexec") { r.extend(mask(EXEC)); w.extend(mask(EXEC)); } if writes_scc(inst.encoding, name) { - w.push(Reg::Scc); + w.push((Reg::Scc, NONE)); } } Fields::Sopp { .. } => match name { - "s_cbranch_scc0" | "s_cbranch_scc1" => r.push(Reg::Scc), + "s_cbranch_scc0" | "s_cbranch_scc1" => r.push((Reg::Scc, NONE)), "s_cbranch_vccz" | "s_cbranch_vccnz" => r.extend(mask(VCC)), "s_cbranch_execz" | "s_cbranch_execnz" => r.extend(mask(EXEC)), - "s_sendmsg" | "s_sendmsghalt" => r.push(Reg::S(M0)), + "s_sendmsg" | "s_sendmsghalt" => r.push((Reg::S(M0), NONE)), "s_endpgm" | "s_endpgm_saved" | "s_endpgm_ordered_ps_done" @@ -818,10 +818,10 @@ fn effects( }; // v[reg + M0] for any M0 that stays inside the program's VGPRs // (and below the proven bound). - let indexed = |op: &Operand, out: &mut Vec| match *op { + let indexed = |op: &Operand, out: &mut Vec<(Reg, Value)>| match *op { Operand::Vector { reg, .. } if u16::from(reg) < vgprs => { let end = m0_max.map_or(vgprs, |m| vgprs.min(u16::from(reg) + u16::from(m) + 1)); - out.extend((u16::from(reg)..end).map(|r| Reg::V(r as u8))); + out.extend((u16::from(reg)..end).map(|r| (Reg::V(r as u8), NONE))); Ok(()) } _ => Err(ShaderError::Isa { @@ -865,12 +865,12 @@ fn effects( _ => {} } if name.starts_with("v_interp_") || src_indexed || dst_indexed { - r.push(Reg::S(M0)); + r.push((Reg::S(M0), NONE)); } } Fields::Vintrp { dst, src, .. } => { regs(src, &mut r)?; - r.push(Reg::S(M0)); + r.push((Reg::S(M0), NONE)); regs(dst, &mut w)?; } Fields::Ds { @@ -891,7 +891,7 @@ fn effects( || name.contains("addtid") || matches!(name, "ds_ordered_count" | "ds_append" | "ds_consume") { - r.push(Reg::S(M0)); + r.push((Reg::S(M0), NONE)); } } Fields::Buffer { @@ -911,7 +911,7 @@ fn effects( regs(rsrc, &mut r)?; regs(soffset, &mut r)?; if *lds { - r.push(Reg::S(M0)); + r.push((Reg::S(M0), NONE)); } if let Some(d) = data { if name.contains("_load_") { @@ -975,7 +975,7 @@ fn effects( if vector { r.extend(mask(EXEC)); // Lanes disabled in EXEC keep the old value. - r.extend(w.iter().filter(|w| matches!(w, Reg::V(_)))); + r.extend(w.iter().filter(|(w, _)| matches!(w, Reg::V(_))).cloned()); } let mut seen = Vec::with_capacity(r.len()); r.retain(|reg| { @@ -1047,7 +1047,7 @@ fn atomic_result(data: &Operand, name: &str) -> Operand { /// Appends the register dwords an operand names; reading `vccz`, `execz`, /// `scc` or `lds_direct` reads the register behind it. -fn add_regs(op: &Operand, lanes: u8, out: &mut Vec) -> Result<(), ()> { +fn add_regs(op: &Operand, lanes: u8, out: &mut Vec<(Reg, Value)>) -> Result<(), ()> { match *op { Operand::Scalar { code, .. } if code == gfx10::NULL => {} Operand::Scalar { code, dwords } => { @@ -1055,18 +1055,18 @@ fn add_regs(op: &Operand, lanes: u8, out: &mut Vec) -> Result<(), ()> { if end > 128 { return Err(()); } - out.extend((code..end as u8).map(Reg::S)); + out.extend((code..end as u8).map(|c| (Reg::S(c), NONE))); } Operand::Vector { reg, dwords } => { if u16::from(reg) + u16::from(dwords) > 256 { return Err(()); } - out.extend((0..dwords).map(|i| Reg::V(reg + i))); + out.extend((0..dwords).map(|i| (Reg::V(reg + i), NONE))); } - Operand::Const(251) => out.extend((0..lanes).map(|i| Reg::S(VCC + i))), - Operand::Const(252) => out.extend((0..lanes).map(|i| Reg::S(EXEC + i))), - Operand::Const(253) => out.push(Reg::Scc), - Operand::Const(254) => out.push(Reg::S(M0)), + Operand::Const(251) => out.extend((0..lanes).map(|i| (Reg::S(VCC + i), NONE))), + Operand::Const(252) => out.extend((0..lanes).map(|i| (Reg::S(EXEC + i), NONE))), + Operand::Const(253) => out.push((Reg::Scc, NONE)), + Operand::Const(254) => out.push((Reg::S(M0), NONE)), Operand::Const(_) | Operand::Literal(_) | Operand::Attr { .. } => {} } Ok(())