| 1 |
// Copyright © 2022 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::{ |
| 5 |
api::{GetDebugFlags, DEBUG}, |
| 6 |
ir::*, |
| 7 |
}; |
| 8 |
|
| 9 |
use std::cmp::max; |
| 10 |
|
| 11 |
struct LowerCopySwap { |
| 12 |
slm_start: u32, |
| 13 |
slm_size: u32, |
| 14 |
} |
| 15 |
|
| 16 |
impl LowerCopySwap { |
| 17 |
fn new(slm_size: u32) -> Self { |
| 18 |
Self { |
| 19 |
slm_start: slm_size, |
| 20 |
slm_size: slm_size, |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
fn lower_copy(&mut self, b: &mut impl Builder, copy: OpCopy) { |
| 25 |
let dst_reg = copy.dst.as_reg().unwrap(); |
| 26 |
assert!(dst_reg.comps() == 1); |
| 27 |
assert!(copy.src.is_unmodified()); |
| 28 |
assert!(copy.src.is_uniform() || !dst_reg.is_uniform()); |
| 29 |
|
| 30 |
match dst_reg.file() { |
| 31 |
RegFile::GPR | RegFile::UGPR => match copy.src.src_ref { |
| 32 |
SrcRef::Zero | SrcRef::Imm32(_) => { |
| 33 |
b.push_op(OpMov { |
| 34 |
dst: copy.dst, |
| 35 |
src: copy.src, |
| 36 |
quad_lanes: 0xf, |
| 37 |
}); |
| 38 |
} |
| 39 |
SrcRef::CBuf(_) => match dst_reg.file() { |
| 40 |
RegFile::GPR => { |
| 41 |
if b.sm() >= 100 { |
| 42 |
b.push_op(OpLdc { |
| 43 |
dst: copy.dst, |
| 44 |
cb: copy.src, |
| 45 |
offset: 0.into(), |
| 46 |
mode: LdcMode::Indexed, |
| 47 |
mem_type: MemType::B32, |
| 48 |
}); |
| 49 |
} else { |
| 50 |
b.push_op(OpMov { |
| 51 |
dst: copy.dst, |
| 52 |
src: copy.src, |
| 53 |
quad_lanes: 0xf, |
| 54 |
}); |
| 55 |
} |
| 56 |
} |
| 57 |
RegFile::UGPR => { |
| 58 |
b.push_op(OpLdc { |
| 59 |
dst: copy.dst, |
| 60 |
cb: copy.src, |
| 61 |
offset: 0.into(), |
| 62 |
mode: LdcMode::Indexed, |
| 63 |
mem_type: MemType::B32, |
| 64 |
}); |
| 65 |
} |
| 66 |
_ => panic!("Invalid cbuf destination"), |
| 67 |
}, |
| 68 |
SrcRef::True | SrcRef::False => { |
| 69 |
panic!("Cannot copy to GPR"); |
| 70 |
} |
| 71 |
SrcRef::Reg(src_reg) => match src_reg.file() { |
| 72 |
RegFile::GPR | RegFile::UGPR => { |
| 73 |
b.push_op(OpMov { |
| 74 |
dst: copy.dst, |
| 75 |
src: copy.src, |
| 76 |
quad_lanes: 0xf, |
| 77 |
}); |
| 78 |
} |
| 79 |
RegFile::Bar => { |
| 80 |
b.push_op(OpBMov { |
| 81 |
dst: copy.dst, |
| 82 |
src: copy.src, |
| 83 |
clear: false, |
| 84 |
}); |
| 85 |
} |
| 86 |
RegFile::Mem => { |
| 87 |
let access = MemAccess { |
| 88 |
mem_type: MemType::B32, |
| 89 |
space: MemSpace::Local, |
| 90 |
order: MemOrder::Strong(MemScope::CTA), |
| 91 |
eviction_priority: MemEvictionPriority::Normal, |
| 92 |
}; |
| 93 |
let addr = self.slm_start + src_reg.base_idx() * 4; |
| 94 |
self.slm_size = max(self.slm_size, addr + 4); |
| 95 |
b.push_op(OpLd { |
| 96 |
dst: copy.dst, |
| 97 |
addr: Src::ZERO, |
| 98 |
offset: addr.try_into().unwrap(), |
| 99 |
access: access, |
| 100 |
}); |
| 101 |
} |
| 102 |
_ => panic!("Cannot copy to GPR"), |
| 103 |
}, |
| 104 |
SrcRef::SSA(_) => panic!("Should be run after RA"), |
| 105 |
}, |
| 106 |
RegFile::Pred | RegFile::UPred => match copy.src.src_ref { |
| 107 |
SrcRef::Zero | SrcRef::Imm32(_) | SrcRef::CBuf(_) => { |
| 108 |
panic!("Cannot copy to Pred"); |
| 109 |
} |
| 110 |
SrcRef::True => { |
| 111 |
b.lop2_to( |
| 112 |
copy.dst, |
| 113 |
LogicOp2::PassB, |
| 114 |
Src::new_imm_bool(true), |
| 115 |
Src::new_imm_bool(true), |
| 116 |
); |
| 117 |
} |
| 118 |
SrcRef::False => { |
| 119 |
b.lop2_to( |
| 120 |
copy.dst, |
| 121 |
LogicOp2::PassB, |
| 122 |
Src::new_imm_bool(true), |
| 123 |
Src::new_imm_bool(false), |
| 124 |
); |
| 125 |
} |
| 126 |
SrcRef::Reg(src_reg) => match src_reg.file() { |
| 127 |
RegFile::Pred => { |
| 128 |
b.lop2_to( |
| 129 |
copy.dst, |
| 130 |
LogicOp2::PassB, |
| 131 |
Src::new_imm_bool(true), |
| 132 |
copy.src, |
| 133 |
); |
| 134 |
} |
| 135 |
RegFile::UPred => { |
| 136 |
// PLOP3 supports a UPred in src[2] |
| 137 |
b.push_op(OpPLop3 { |
| 138 |
dsts: [copy.dst, Dst::None], |
| 139 |
srcs: [true.into(), true.into(), copy.src], |
| 140 |
ops: [ |
| 141 |
LogicOp3::new_lut(&|_, _, z| z), |
| 142 |
LogicOp3::new_const(false), |
| 143 |
], |
| 144 |
}); |
| 145 |
} |
| 146 |
_ => panic!("Cannot copy to Pred"), |
| 147 |
}, |
| 148 |
SrcRef::SSA(_) => panic!("Should be run after RA"), |
| 149 |
}, |
| 150 |
RegFile::Bar => match copy.src.src_ref { |
| 151 |
SrcRef::Reg(src_reg) => match src_reg.file() { |
| 152 |
RegFile::GPR | RegFile::UGPR => { |
| 153 |
b.push_op(OpBMov { |
| 154 |
dst: copy.dst, |
| 155 |
src: copy.src, |
| 156 |
clear: false, |
| 157 |
}); |
| 158 |
} |
| 159 |
_ => panic!("Cannot copy to Bar"), |
| 160 |
}, |
| 161 |
_ => panic!("Cannot copy to Bar"), |
| 162 |
}, |
| 163 |
RegFile::Mem => match copy.src.src_ref { |
| 164 |
SrcRef::Reg(src_reg) => match src_reg.file() { |
| 165 |
RegFile::GPR => { |
| 166 |
let access = MemAccess { |
| 167 |
mem_type: MemType::B32, |
| 168 |
space: MemSpace::Local, |
| 169 |
order: MemOrder::Strong(MemScope::CTA), |
| 170 |
eviction_priority: MemEvictionPriority::Normal, |
| 171 |
}; |
| 172 |
let addr = self.slm_start + dst_reg.base_idx() * 4; |
| 173 |
self.slm_size = max(self.slm_size, addr + 4); |
| 174 |
b.push_op(OpSt { |
| 175 |
addr: Src::ZERO, |
| 176 |
data: copy.src, |
| 177 |
offset: addr.try_into().unwrap(), |
| 178 |
access: access, |
| 179 |
}); |
| 180 |
} |
| 181 |
_ => panic!("Cannot copy to Mem"), |
| 182 |
}, |
| 183 |
_ => panic!("Cannot copy to Mem"), |
| 184 |
}, |
| 185 |
_ => panic!("Unhandled register file"), |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
fn lower_r2ur(&mut self, b: &mut impl Builder, r2ur: OpR2UR) { |
| 190 |
assert!(r2ur.src.is_unmodified()); |
| 191 |
if r2ur.src.is_uniform() { |
| 192 |
let copy = OpCopy { |
| 193 |
dst: r2ur.dst, |
| 194 |
src: r2ur.src, |
| 195 |
}; |
| 196 |
self.lower_copy(b, copy); |
| 197 |
} else { |
| 198 |
let src_file = r2ur.src.src_ref.as_reg().unwrap().file(); |
| 199 |
let dst_file = r2ur.dst.as_reg().unwrap().file(); |
| 200 |
match src_file { |
| 201 |
RegFile::GPR => { |
| 202 |
assert!(dst_file == RegFile::UGPR); |
| 203 |
b.push_op(r2ur); |
| 204 |
} |
| 205 |
RegFile::Pred => { |
| 206 |
assert!(dst_file == RegFile::UPred); |
| 207 |
// It doesn't matter what channel we take |
| 208 |
b.push_op(OpVote { |
| 209 |
op: VoteOp::Any, |
| 210 |
ballot: Dst::None, |
| 211 |
vote: r2ur.dst, |
| 212 |
pred: r2ur.src, |
| 213 |
}); |
| 214 |
} |
| 215 |
_ => panic!("No matching uniform register file"), |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
fn lower_swap(&mut self, b: &mut impl Builder, swap: OpSwap) { |
| 221 |
let x = *swap.dsts[0].as_reg().unwrap(); |
| 222 |
let y = *swap.dsts[1].as_reg().unwrap(); |
| 223 |
|
| 224 |
assert!(x.file() == y.file()); |
| 225 |
assert!(x.file() != RegFile::Mem); |
| 226 |
assert!(x.comps() == 1 && y.comps() == 1); |
| 227 |
assert!(swap.srcs[0].is_unmodified()); |
| 228 |
assert!(*swap.srcs[0].src_ref.as_reg().unwrap() == y); |
| 229 |
assert!(swap.srcs[1].is_unmodified()); |
| 230 |
assert!(*swap.srcs[1].src_ref.as_reg().unwrap() == x); |
| 231 |
|
| 232 |
if x == y { |
| 233 |
// Nothing to do |
| 234 |
} else if x.is_predicate() && b.sm() >= 70 { |
| 235 |
b.push_op(OpPLop3 { |
| 236 |
dsts: [x.into(), y.into()], |
| 237 |
srcs: [x.into(), y.into(), Src::new_imm_bool(true)], |
| 238 |
ops: [ |
| 239 |
LogicOp3::new_lut(&|_, y, _| y), |
| 240 |
LogicOp3::new_lut(&|x, _, _| x), |
| 241 |
], |
| 242 |
}); |
| 243 |
} else { |
| 244 |
b.lop2_to(x.into(), LogicOp2::Xor, x.into(), y.into()); |
| 245 |
b.lop2_to(y.into(), LogicOp2::Xor, x.into(), y.into()); |
| 246 |
b.lop2_to(x.into(), LogicOp2::Xor, x.into(), y.into()); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
fn run(&mut self, s: &mut Shader) { |
| 251 |
let sm = s.sm; |
| 252 |
s.map_instrs(|instr: Instr, _| -> MappedInstrs { |
| 253 |
match instr.op { |
| 254 |
Op::R2UR(r2ur) => { |
| 255 |
debug_assert!(instr.pred.is_true()); |
| 256 |
let mut b = InstrBuilder::new(sm); |
| 257 |
if DEBUG.annotate() { |
| 258 |
b.push_instr(Instr::new(OpAnnotate { |
| 259 |
annotation: "r2ur lowered by lower_copy_swap" |
| 260 |
.into(), |
| 261 |
})); |
| 262 |
} |
| 263 |
self.lower_r2ur(&mut b, *r2ur); |
| 264 |
b.into_mapped_instrs() |
| 265 |
} |
| 266 |
Op::Copy(copy) => { |
| 267 |
debug_assert!(instr.pred.is_true()); |
| 268 |
let mut b = InstrBuilder::new(sm); |
| 269 |
if DEBUG.annotate() { |
| 270 |
b.push_instr(Instr::new(OpAnnotate { |
| 271 |
annotation: "copy lowered by lower_copy_swap" |
| 272 |
.into(), |
| 273 |
})); |
| 274 |
} |
| 275 |
self.lower_copy(&mut b, *copy); |
| 276 |
b.into_mapped_instrs() |
| 277 |
} |
| 278 |
Op::Swap(swap) => { |
| 279 |
debug_assert!(instr.pred.is_true()); |
| 280 |
let mut b = InstrBuilder::new(sm); |
| 281 |
if DEBUG.annotate() { |
| 282 |
b.push_instr(Instr::new(OpAnnotate { |
| 283 |
annotation: "swap lowered by lower_copy_swap" |
| 284 |
.into(), |
| 285 |
})); |
| 286 |
} |
| 287 |
self.lower_swap(&mut b, *swap); |
| 288 |
b.into_mapped_instrs() |
| 289 |
} |
| 290 |
_ => MappedInstrs::One(instr), |
| 291 |
} |
| 292 |
}); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
impl Shader<'_> { |
| 297 |
pub fn lower_copy_swap(&mut self) { |
| 298 |
let mut pass = LowerCopySwap::new(self.info.slm_size); |
| 299 |
pass.run(self); |
| 300 |
self.info.slm_size = pass.slm_size; |
| 301 |
} |
| 302 |
} |