nak: Heap-allocate Instrs

Heap-allocate Instrs to avoid copying them around whenever they are
mutated by a pass. This lowers the amount of copies in detriment of
cache-locality.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24998>
This commit is contained in:
Daniel Almeida 2023-05-01 12:48:32 -03:00 committed by Marge Bot
parent fa0891d37c
commit 5014b4697d
6 changed files with 370 additions and 312 deletions

View file

@ -757,12 +757,12 @@ impl AssignRegsBlock {
fn assign_regs_instr(
&mut self,
mut instr: Instr,
mut instr: Box<Instr>,
ip: usize,
sum: &SSAUseMap,
killed: &KillSet,
pcopy: &mut OpParCopy,
) -> Option<Instr> {
) -> Option<Box<Instr>> {
match &instr.op {
Op::Undef(undef) => {
if let Dst::SSA(ssa) = undef.dst {
@ -855,7 +855,7 @@ impl AssignRegsBlock {
self.assign_regs_instr(instr, ip, &sum, &killed, &mut pcopy);
if !pcopy.is_empty() {
instrs.push(pcopy.into());
instrs.push(Instr::new_boxed(pcopy));
}
if let Some(instr) = instr {
@ -891,9 +891,9 @@ impl AssignRegsBlock {
}
if b.branch().is_some() {
b.instrs.insert(b.instrs.len() - 1, pcopy.into());
b.instrs.insert(b.instrs.len() - 1, Instr::new_boxed(pcopy));
} else {
b.instrs.push(pcopy.into());
b.instrs.push(Instr::new_boxed(pcopy));
};
}
}

File diff suppressed because it is too large Load diff

View file

@ -3241,6 +3241,10 @@ impl Instr {
}
}
pub fn new_boxed(op: impl Into<Op>) -> Box<Self> {
Box::new(Instr::new(op))
}
pub fn new_fadd(dst: Dst, x: Src, y: Src) -> Instr {
OpFAdd {
dst: dst,
@ -3615,7 +3619,7 @@ impl<T: Into<Op>> From<T> for Instr {
pub struct BasicBlock {
pub id: u32,
pub instrs: Vec<Instr>,
pub instrs: Vec<Box<Instr>>,
}
impl BasicBlock {
@ -3626,7 +3630,9 @@ impl BasicBlock {
}
}
pub fn map_instrs<F: Fn(Instr, &mut SSAValueAllocator) -> Vec<Instr>>(
pub fn map_instrs<
F: Fn(Box<Instr>, &mut SSAValueAllocator) -> Vec<Box<Instr>>,
>(
&mut self,
map: &F,
ssa_alloc: &mut SSAValueAllocator,
@ -3694,7 +3700,9 @@ impl Function {
}
}
pub fn map_instrs<F: Fn(Instr, &mut SSAValueAllocator) -> Vec<Instr>>(
pub fn map_instrs<
F: Fn(Box<Instr>, &mut SSAValueAllocator) -> Vec<Box<Instr>>,
>(
&mut self,
map: &F,
) {
@ -3726,7 +3734,9 @@ impl Shader {
}
}
pub fn map_instrs<F: Fn(Instr, &mut SSAValueAllocator) -> Vec<Instr>>(
pub fn map_instrs<
F: Fn(Box<Instr>, &mut SSAValueAllocator) -> Vec<Box<Instr>>,
>(
&mut self,
map: &F,
) {
@ -3736,10 +3746,10 @@ impl Shader {
}
pub fn lower_vec_split(&mut self) {
self.map_instrs(&|instr: Instr, _| -> Vec<Instr> {
self.map_instrs(&|instr: Box<Instr>, _| -> Vec<Box<Instr>> {
match instr.op {
Op::INeg(neg) => {
vec![OpIAdd3 {
vec![Instr::new_boxed(OpIAdd3 {
dst: neg.dst,
overflow: Dst::None,
srcs: [
@ -3748,8 +3758,7 @@ impl Shader {
Src::new_zero(),
],
carry: Src::new_imm_bool(false),
}
.into()]
})]
}
Op::FSOut(out) => {
let mut pcopy = OpParCopy::new();
@ -3759,7 +3768,7 @@ impl Shader {
pcopy.srcs.push(*src);
pcopy.dsts.push(dst.into());
}
vec![pcopy.into()]
vec![Instr::new_boxed(pcopy)]
}
_ => vec![instr],
}
@ -3767,7 +3776,7 @@ impl Shader {
}
pub fn lower_swap(&mut self) {
self.map_instrs(&|instr: Instr, _| -> Vec<Instr> {
self.map_instrs(&|instr: Box<Instr>, _| -> Vec<Box<Instr>> {
match instr.op {
Op::Swap(swap) => {
let x = *swap.dsts[0].as_reg().unwrap();
@ -3783,20 +3792,19 @@ impl Shader {
if x == y {
Vec::new()
} else if x.is_predicate() {
vec![OpPLop3 {
vec![Instr::new_boxed(OpPLop3 {
dsts: [x.into(), y.into()],
srcs: [x.into(), y.into(), Src::new_imm_bool(true)],
ops: [
LogicOp::new_lut(&|_, y, _| y),
LogicOp::new_lut(&|x, _, _| x),
],
}
.into()]
})]
} else {
vec![
Instr::new_xor(x.into(), x.into(), y.into()),
Instr::new_xor(y.into(), x.into(), y.into()),
Instr::new_xor(x.into(), x.into(), y.into()),
Instr::new_xor(x.into(), x.into(), y.into()).into(),
Instr::new_xor(y.into(), x.into(), y.into()).into(),
Instr::new_xor(x.into(), x.into(), y.into()).into(),
]
}
}
@ -3806,7 +3814,7 @@ impl Shader {
}
pub fn lower_mov_predicate(&mut self) {
self.map_instrs(&|instr: Instr, _| -> Vec<Instr> {
self.map_instrs(&|instr: Box<Instr>, _| -> Vec<Box<Instr>> {
match &instr.op {
Op::Mov(mov) => {
assert!(mov.src.src_mod.is_none());
@ -3818,7 +3826,8 @@ impl Shader {
IntCmpOp::Eq,
Src::new_zero(),
Src::new_zero(),
)]
)
.into()]
}
SrcRef::False => {
vec![Instr::new_isetp(
@ -3827,7 +3836,8 @@ impl Shader {
IntCmpOp::Ne,
Src::new_zero(),
Src::new_zero(),
)]
)
.into()]
}
SrcRef::Reg(reg) => {
if reg.is_predicate() {
@ -3837,7 +3847,8 @@ impl Shader {
mov.src,
Src::new_imm_bool(true),
Src::new_imm_bool(true),
)]
)
.into()]
} else {
vec![instr]
}

View file

@ -9,7 +9,7 @@ use std::collections::{HashMap, HashSet};
struct LegalizeInstr<'a> {
ssa_alloc: &'a mut SSAValueAllocator,
instrs: Vec<Instr>,
instrs: Vec<Box<Instr>>,
}
fn src_is_reg(src: &Src) -> bool {
@ -56,7 +56,7 @@ impl<'a> LegalizeInstr<'a> {
pub fn mov_src(&mut self, src: &mut Src, file: RegFile) {
let val = self.ssa_alloc.alloc(file);
self.instrs
.push(Instr::new_mov(val.into(), src.src_ref.into()));
.push(Instr::new_mov(val.into(), src.src_ref.into()).into());
src.src_ref = val.into();
}
@ -72,7 +72,7 @@ impl<'a> LegalizeInstr<'a> {
}
}
pub fn map(&mut self, mut instr: Instr) -> Vec<Instr> {
pub fn map(&mut self, mut instr: Box<Instr>) -> Vec<Box<Instr>> {
match &mut instr.op {
Op::FAdd(op) => {
let [ref mut src0, ref mut src1] = op.srcs;
@ -293,7 +293,7 @@ impl<'a> LegalizeInstr<'a> {
}
if !pcopy.is_empty() {
self.instrs.push(Instr::new(Op::ParCopy(pcopy)));
self.instrs.push(Instr::new_boxed(Op::ParCopy(pcopy)));
}
self.instrs.push(instr);
@ -303,7 +303,7 @@ impl<'a> LegalizeInstr<'a> {
impl Shader {
pub fn legalize(&mut self) {
self.map_instrs(&|instr, ssa_alloc| -> Vec<Instr> {
self.map_instrs(&|instr, ssa_alloc| -> Vec<Box<Instr>> {
LegalizeInstr::new(ssa_alloc).map(instr)
});
}

View file

@ -60,7 +60,7 @@ impl CopyGraph {
}
}
fn lower_par_copy(pc: OpParCopy) -> Vec<Instr> {
fn lower_par_copy(pc: OpParCopy) -> Vec<Box<Instr>> {
let mut graph = CopyGraph::new();
let mut vals = Vec::new();
let mut reg_to_idx = HashMap::new();
@ -123,7 +123,7 @@ fn lower_par_copy(pc: OpParCopy) -> Vec<Instr> {
if let Some(src_idx) = graph.src(dst_idx) {
let dst = *vals[dst_idx].as_reg().unwrap();
let src = vals[src_idx];
instrs.push(Instr::new_mov(dst.into(), src.into()));
instrs.push(Instr::new_mov(dst.into(), src.into()).into());
if graph.del_edge(dst_idx, src_idx) {
ready.push(src_idx);
}
@ -162,10 +162,13 @@ fn lower_par_copy(pc: OpParCopy) -> Vec<Instr> {
/* We're part of a cycle so j also has a source */
let k = graph.src(j).unwrap();
instrs.push(Instr::new_swap(
*vals[j].as_reg().unwrap(),
*vals[k].as_reg().unwrap(),
));
instrs.push(
Instr::new_swap(
*vals[j].as_reg().unwrap(),
*vals[k].as_reg().unwrap(),
)
.into(),
);
graph.del_edge(i, j);
graph.del_edge(j, k);
@ -185,7 +188,7 @@ fn lower_par_copy(pc: OpParCopy) -> Vec<Instr> {
impl Shader {
pub fn lower_par_copies(&mut self) {
self.map_instrs(&|instr, _| -> Vec<Instr> {
self.map_instrs(&|instr, _| -> Vec<Box<Instr>> {
match instr.op {
Op::ParCopy(pc) => {
assert!(instr.pred.is_true());

View file

@ -125,7 +125,7 @@ impl DeadCodePass {
}
}
fn map_instr(&self, mut instr: Instr) -> Vec<Instr> {
fn map_instr(&self, mut instr: Box<Instr>) -> Vec<Box<Instr>> {
let is_live = match &mut instr.op {
Op::PhiSrcs(phi) => {
assert!(phi.ids.len() == phi.srcs.len());