| 1 |
// Copyright © 2022 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::api::{GetDebugFlags, DEBUG}; |
| 5 |
use crate::const_tracker::ConstTracker; |
| 6 |
use crate::ir::*; |
| 7 |
use crate::liveness::{BlockLiveness, Liveness, SimpleLiveness}; |
| 8 |
|
| 9 |
use rustc_hash::{FxHashMap, FxHashSet}; |
| 10 |
|
| 11 |
pub fn src_is_upred_reg(src: &Src) -> bool { |
| 12 |
match &src.src_ref { |
| 13 |
SrcRef::True | SrcRef::False => false, |
| 14 |
SrcRef::SSA(ssa) => { |
| 15 |
assert!(ssa.comps() == 1); |
| 16 |
match ssa[0].file() { |
| 17 |
RegFile::Pred => false, |
| 18 |
RegFile::UPred => true, |
| 19 |
_ => panic!("Not a predicate source"), |
| 20 |
} |
| 21 |
} |
| 22 |
SrcRef::Reg(_) => panic!("Not in SSA form"), |
| 23 |
_ => panic!("Not a predicate source"), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
pub fn src_is_reg(src: &Src, reg_file: RegFile) -> bool { |
| 28 |
match &src.src_ref { |
| 29 |
SrcRef::Zero | SrcRef::True | SrcRef::False => true, |
| 30 |
SrcRef::SSA(ssa) => ssa.file() == reg_file, |
| 31 |
SrcRef::Imm32(_) | SrcRef::CBuf(_) => false, |
| 32 |
SrcRef::Reg(_) => panic!("Not in SSA form"), |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
pub fn swap_srcs_if_not_reg( |
| 37 |
x: &mut Src, |
| 38 |
y: &mut Src, |
| 39 |
reg_file: RegFile, |
| 40 |
) -> bool { |
| 41 |
if !src_is_reg(x, reg_file) && src_is_reg(y, reg_file) { |
| 42 |
std::mem::swap(x, y); |
| 43 |
true |
| 44 |
} else { |
| 45 |
false |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
fn src_is_imm(src: &Src) -> bool { |
| 50 |
matches!(src.src_ref, SrcRef::Imm32(_)) |
| 51 |
} |
| 52 |
|
| 53 |
pub enum PadValue { |
| 54 |
Zero, |
| 55 |
#[allow(dead_code)] |
| 56 |
Undefined, |
| 57 |
} |
| 58 |
|
| 59 |
pub trait LegalizeBuildHelpers: SSABuilder { |
| 60 |
fn copy_ssa(&mut self, ssa: &mut SSAValue, reg_file: RegFile) { |
| 61 |
let tmp = self.alloc_ssa(reg_file); |
| 62 |
self.copy_to(tmp.into(), (*ssa).into()); |
| 63 |
*ssa = tmp; |
| 64 |
} |
| 65 |
|
| 66 |
fn copy_ssa_ref(&mut self, vec: &mut SSARef, reg_file: RegFile) { |
| 67 |
for ssa in &mut vec[..] { |
| 68 |
self.copy_ssa(ssa, reg_file); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn copy_pred_ssa_if_uniform(&mut self, ssa: &mut SSAValue) { |
| 73 |
match ssa.file() { |
| 74 |
RegFile::Pred => (), |
| 75 |
RegFile::UPred => self.copy_ssa(ssa, RegFile::Pred), |
| 76 |
_ => panic!("Not a predicate value"), |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
fn copy_pred_if_upred(&mut self, pred: &mut Pred) { |
| 81 |
match &mut pred.pred_ref { |
| 82 |
PredRef::None => (), |
| 83 |
PredRef::SSA(ssa) => { |
| 84 |
self.copy_pred_ssa_if_uniform(ssa); |
| 85 |
} |
| 86 |
PredRef::Reg(_) => panic!("Not in SSA form"), |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
fn copy_src_if_upred(&mut self, src: &mut Src) { |
| 91 |
match &mut src.src_ref { |
| 92 |
SrcRef::True | SrcRef::False => (), |
| 93 |
SrcRef::SSA(ssa) => { |
| 94 |
assert!(ssa.comps() == 1); |
| 95 |
self.copy_pred_ssa_if_uniform(&mut ssa[0]); |
| 96 |
} |
| 97 |
SrcRef::Reg(_) => panic!("Not in SSA form"), |
| 98 |
_ => panic!("Not a predicate source"), |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
fn copy_src_if_not_same_file(&mut self, src: &mut Src) { |
| 103 |
let SrcRef::SSA(vec) = &mut src.src_ref else { |
| 104 |
return; |
| 105 |
}; |
| 106 |
|
| 107 |
if vec.comps() == 1 { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
let mut all_same = true; |
| 112 |
let file = vec[0].file(); |
| 113 |
for i in 1..vec.comps() { |
| 114 |
let c_file = vec[usize::from(i)].file(); |
| 115 |
if c_file != file { |
| 116 |
debug_assert!(c_file.to_warp() == file.to_warp()); |
| 117 |
all_same = false; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if !all_same { |
| 122 |
self.copy_ssa_ref(vec, file.to_warp()); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
fn align_reg( |
| 127 |
&mut self, |
| 128 |
src: &mut Src, |
| 129 |
n_comps: usize, |
| 130 |
pad_value: PadValue, |
| 131 |
) { |
| 132 |
debug_assert!(!matches!(src.src_ref, SrcRef::Reg(_))); |
| 133 |
let SrcRef::SSA(ref old_val) = src.src_ref else { |
| 134 |
return; |
| 135 |
}; |
| 136 |
assert!(old_val.len() <= n_comps); |
| 137 |
assert!(src.is_unmodified()); |
| 138 |
|
| 139 |
let pad_fn = || { |
| 140 |
Some(match pad_value { |
| 141 |
PadValue::Zero => self.copy(0.into()), |
| 142 |
PadValue::Undefined => self.undef(), |
| 143 |
}) |
| 144 |
}; |
| 145 |
|
| 146 |
// Pad the given ssa_ref with either undefined or zero |
| 147 |
let ssa_vals: Vec<_> = old_val |
| 148 |
.iter() |
| 149 |
.copied() |
| 150 |
.chain(std::iter::from_fn(pad_fn)) |
| 151 |
.take(n_comps) |
| 152 |
.collect(); |
| 153 |
|
| 154 |
// Collect it in a new ssa_ref and replace it with the original. |
| 155 |
let val = SSARef::try_from(ssa_vals).expect("Cannot create SSARef"); |
| 156 |
src.src_ref = val.into(); |
| 157 |
} |
| 158 |
|
| 159 |
fn copy_alu_src( |
| 160 |
&mut self, |
| 161 |
src: &mut Src, |
| 162 |
reg_file: RegFile, |
| 163 |
src_type: SrcType, |
| 164 |
) { |
| 165 |
let val = match src_type { |
| 166 |
SrcType::GPR |
| 167 |
| SrcType::ALU |
| 168 |
| SrcType::F32 |
| 169 |
| SrcType::F16 |
| 170 |
| SrcType::F16v2 |
| 171 |
| SrcType::I32 |
| 172 |
| SrcType::B32 => self.alloc_ssa_vec(reg_file, 1), |
| 173 |
SrcType::F64 => self.alloc_ssa_vec(reg_file, 2), |
| 174 |
SrcType::Pred => self.alloc_ssa_vec(reg_file, 1), |
| 175 |
_ => panic!("Unknown source type"), |
| 176 |
}; |
| 177 |
|
| 178 |
if DEBUG.annotate() { |
| 179 |
self.push_instr(Instr::new(OpAnnotate { |
| 180 |
annotation: "copy generated by legalizer".into(), |
| 181 |
})); |
| 182 |
} |
| 183 |
|
| 184 |
let old_src_ref = |
| 185 |
std::mem::replace(&mut src.src_ref, val.clone().into()); |
| 186 |
if val.comps() == 1 { |
| 187 |
self.copy_to(val[0].into(), old_src_ref.into()); |
| 188 |
} else { |
| 189 |
match old_src_ref { |
| 190 |
SrcRef::Imm32(u) => { |
| 191 |
// Immediates go in the top bits |
| 192 |
self.copy_to(val[0].into(), 0.into()); |
| 193 |
self.copy_to(val[1].into(), u.into()); |
| 194 |
} |
| 195 |
SrcRef::CBuf(cb) => { |
| 196 |
// CBufs load 8B |
| 197 |
self.copy_to(val[0].into(), cb.clone().into()); |
| 198 |
self.copy_to(val[1].into(), cb.offset(4).into()); |
| 199 |
} |
| 200 |
SrcRef::SSA(vec) => { |
| 201 |
assert!(vec.comps() == 2); |
| 202 |
self.copy_to(val[0].into(), vec[0].into()); |
| 203 |
self.copy_to(val[1].into(), vec[1].into()); |
| 204 |
} |
| 205 |
_ => panic!("Invalid 64-bit SrcRef"), |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
fn copy_alu_src_if_not_reg( |
| 211 |
&mut self, |
| 212 |
src: &mut Src, |
| 213 |
reg_file: RegFile, |
| 214 |
src_type: SrcType, |
| 215 |
) { |
| 216 |
if !src_is_reg(src, reg_file) { |
| 217 |
self.copy_alu_src(src, reg_file, src_type); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
fn copy_alu_src_if_not_reg_or_imm( |
| 222 |
&mut self, |
| 223 |
src: &mut Src, |
| 224 |
reg_file: RegFile, |
| 225 |
src_type: SrcType, |
| 226 |
) { |
| 227 |
if !src_is_reg(src, reg_file) |
| 228 |
&& !matches!(&src.src_ref, SrcRef::Imm32(_)) |
| 229 |
{ |
| 230 |
self.copy_alu_src(src, reg_file, src_type); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
fn copy_alu_src_if_imm( |
| 235 |
&mut self, |
| 236 |
src: &mut Src, |
| 237 |
reg_file: RegFile, |
| 238 |
src_type: SrcType, |
| 239 |
) { |
| 240 |
if src_is_imm(src) { |
| 241 |
self.copy_alu_src(src, reg_file, src_type); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
fn copy_alu_src_if_ineg_imm( |
| 246 |
&mut self, |
| 247 |
src: &mut Src, |
| 248 |
reg_file: RegFile, |
| 249 |
src_type: SrcType, |
| 250 |
) { |
| 251 |
assert!(src_type == SrcType::I32); |
| 252 |
if src_is_imm(src) && src.src_mod.is_ineg() { |
| 253 |
self.copy_alu_src(src, reg_file, src_type); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
fn copy_alu_src_if_both_not_reg( |
| 258 |
&mut self, |
| 259 |
src1: &Src, |
| 260 |
src2: &mut Src, |
| 261 |
reg_file: RegFile, |
| 262 |
src_type: SrcType, |
| 263 |
) { |
| 264 |
if !src_is_reg(src1, reg_file) && !src_is_reg(src2, reg_file) { |
| 265 |
self.copy_alu_src(src2, reg_file, src_type); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
fn copy_alu_src_and_lower_fmod( |
| 270 |
&mut self, |
| 271 |
src: &mut Src, |
| 272 |
reg_file: RegFile, |
| 273 |
src_type: SrcType, |
| 274 |
) { |
| 275 |
match src_type { |
| 276 |
SrcType::F16 | SrcType::F16v2 => { |
| 277 |
let val = self.alloc_ssa(reg_file); |
| 278 |
let old_src = std::mem::replace(src, val.into()); |
| 279 |
self.push_op(OpHAdd2 { |
| 280 |
dst: val.into(), |
| 281 |
srcs: [Src::ZERO.fneg(), old_src], |
| 282 |
saturate: false, |
| 283 |
ftz: false, |
| 284 |
f32: false, |
| 285 |
}); |
| 286 |
} |
| 287 |
SrcType::F32 => { |
| 288 |
let val = self.alloc_ssa(reg_file); |
| 289 |
let old_src = std::mem::replace(src, val.into()); |
| 290 |
self.push_op(OpFAdd { |
| 291 |
dst: val.into(), |
| 292 |
srcs: [Src::ZERO.fneg(), old_src], |
| 293 |
saturate: false, |
| 294 |
rnd_mode: FRndMode::NearestEven, |
| 295 |
ftz: false, |
| 296 |
}); |
| 297 |
} |
| 298 |
SrcType::F64 => { |
| 299 |
let val = self.alloc_ssa_vec(reg_file, 2); |
| 300 |
let old_src = std::mem::replace(src, val.clone().into()); |
| 301 |
self.push_op(OpDAdd { |
| 302 |
dst: val.into(), |
| 303 |
srcs: [Src::ZERO.fneg(), old_src], |
| 304 |
rnd_mode: FRndMode::NearestEven, |
| 305 |
}); |
| 306 |
} |
| 307 |
_ => panic!("Invalid ffabs srouce type"), |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
fn copy_alu_src_and_lower_ineg( |
| 312 |
&mut self, |
| 313 |
src: &mut Src, |
| 314 |
reg_file: RegFile, |
| 315 |
src_type: SrcType, |
| 316 |
) { |
| 317 |
assert!(src_type == SrcType::I32); |
| 318 |
let val = self.alloc_ssa(reg_file); |
| 319 |
let old_src = std::mem::replace(src, val.into()); |
| 320 |
if self.sm() >= 70 { |
| 321 |
self.push_op(OpIAdd3 { |
| 322 |
srcs: [Src::ZERO, old_src, Src::ZERO], |
| 323 |
overflow: [Dst::None, Dst::None], |
| 324 |
dst: val.into(), |
| 325 |
}); |
| 326 |
} else { |
| 327 |
self.push_op(OpIAdd2 { |
| 328 |
dst: val.into(), |
| 329 |
carry_out: Dst::None, |
| 330 |
srcs: [Src::ZERO, old_src], |
| 331 |
}); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
fn copy_alu_src_if_fabs( |
| 336 |
&mut self, |
| 337 |
src: &mut Src, |
| 338 |
reg_file: RegFile, |
| 339 |
src_type: SrcType, |
| 340 |
) { |
| 341 |
if src.src_mod.has_fabs() { |
| 342 |
self.copy_alu_src_and_lower_fmod(src, reg_file, src_type); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
fn copy_alu_src_if_i20_overflow( |
| 347 |
&mut self, |
| 348 |
src: &mut Src, |
| 349 |
reg_file: RegFile, |
| 350 |
src_type: SrcType, |
| 351 |
) { |
| 352 |
if src.as_imm_not_i20().is_some() { |
| 353 |
self.copy_alu_src(src, reg_file, src_type); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
fn copy_alu_src_if_f20_overflow( |
| 358 |
&mut self, |
| 359 |
src: &mut Src, |
| 360 |
reg_file: RegFile, |
| 361 |
src_type: SrcType, |
| 362 |
) { |
| 363 |
if src.as_imm_not_f20().is_some() { |
| 364 |
self.copy_alu_src(src, reg_file, src_type); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
fn copy_ssa_ref_if_uniform(&mut self, ssa_ref: &mut SSARef) { |
| 369 |
for ssa in &mut ssa_ref[..] { |
| 370 |
if ssa.is_uniform() { |
| 371 |
let warp = self.alloc_ssa(ssa.file().to_warp()); |
| 372 |
self.copy_to(warp.into(), (*ssa).into()); |
| 373 |
*ssa = warp; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
pub struct LegalizeBuilder<'a> { |
| 380 |
b: SSAInstrBuilder<'a>, |
| 381 |
const_tracker: &'a mut ConstTracker, |
| 382 |
} |
| 383 |
|
| 384 |
impl<'a> LegalizeBuilder<'a> { |
| 385 |
fn new( |
| 386 |
sm: &'a ShaderModelInfo, |
| 387 |
alloc: &'a mut SSAValueAllocator, |
| 388 |
const_tracker: &'a mut ConstTracker, |
| 389 |
) -> Self { |
| 390 |
LegalizeBuilder { |
| 391 |
b: SSAInstrBuilder::new(sm, alloc), |
| 392 |
const_tracker, |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
pub fn into_vec(self) -> Vec<Instr> { |
| 397 |
self.b.into_vec() |
| 398 |
} |
| 399 |
|
| 400 |
#[allow(dead_code)] |
| 401 |
pub fn into_mapped_instrs(self) -> MappedInstrs { |
| 402 |
self.b.into_mapped_instrs() |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
impl<'a> Builder for LegalizeBuilder<'a> { |
| 407 |
fn push_instr(&mut self, instr: Instr) -> &mut Instr { |
| 408 |
self.b.push_instr(instr) |
| 409 |
} |
| 410 |
|
| 411 |
fn sm(&self) -> u8 { |
| 412 |
self.b.sm() |
| 413 |
} |
| 414 |
|
| 415 |
fn copy_to(&mut self, dst: Dst, mut src: Src) { |
| 416 |
if let Some(ssa_ref) = src.as_ssa() { |
| 417 |
if let &[ssa_value] = &ssa_ref[..] { |
| 418 |
if let Some(new_src) = self.const_tracker.get(&ssa_value) { |
| 419 |
src = new_src.clone().into(); |
| 420 |
} |
| 421 |
} |
| 422 |
}; |
| 423 |
self.b.copy_to(dst, src); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
impl<'a> SSABuilder for LegalizeBuilder<'a> { |
| 428 |
fn alloc_ssa(&mut self, file: RegFile) -> SSAValue { |
| 429 |
self.b.alloc_ssa(file) |
| 430 |
} |
| 431 |
|
| 432 |
fn alloc_ssa_vec(&mut self, file: RegFile, comps: u8) -> SSARef { |
| 433 |
self.b.alloc_ssa_vec(file, comps) |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
impl LegalizeBuildHelpers for LegalizeBuilder<'_> {} |
| 438 |
|
| 439 |
fn legalize_instr( |
| 440 |
sm: &ShaderModelInfo, |
| 441 |
b: &mut LegalizeBuilder, |
| 442 |
bl: &impl BlockLiveness, |
| 443 |
block_uniform: bool, |
| 444 |
pinned: &FxHashSet<SSARef>, |
| 445 |
ip: usize, |
| 446 |
instr: &mut Instr, |
| 447 |
) { |
| 448 |
// Handle a few no-op cases up-front |
| 449 |
match &instr.op { |
| 450 |
Op::Annotate(_) => { |
| 451 |
// OpAnnotate does nothing. There's nothing to legalize. |
| 452 |
return; |
| 453 |
} |
| 454 |
Op::Undef(_) |
| 455 |
| Op::PhiSrcs(_) |
| 456 |
| Op::PhiDsts(_) |
| 457 |
| Op::Pin(_) |
| 458 |
| Op::Unpin(_) |
| 459 |
| Op::RegOut(_) => { |
| 460 |
// These are implemented by RA and can take pretty much anything |
| 461 |
// you can throw at them. |
| 462 |
debug_assert!(instr.pred.is_true()); |
| 463 |
return; |
| 464 |
} |
| 465 |
Op::Copy(_) => { |
| 466 |
// OpCopy is implemented in a lowering pass and can handle anything |
| 467 |
return; |
| 468 |
} |
| 469 |
Op::SrcBar(_) => { |
| 470 |
// This is turned into a nop by calc_instr_deps |
| 471 |
return; |
| 472 |
} |
| 473 |
Op::Swap(_) | Op::ParCopy(_) => { |
| 474 |
// These are generated by RA and should not exist yet |
| 475 |
panic!("Unsupported instruction"); |
| 476 |
} |
| 477 |
_ => (), |
| 478 |
} |
| 479 |
|
| 480 |
if !instr.is_uniform() { |
| 481 |
b.copy_pred_if_upred(&mut instr.pred); |
| 482 |
} |
| 483 |
|
| 484 |
let src_types = instr.src_types(); |
| 485 |
for (i, src) in instr.srcs_mut().iter_mut().enumerate() { |
| 486 |
if matches!(src.src_ref, SrcRef::Imm32(_)) { |
| 487 |
// Fold modifiers on Imm32 sources whenever possible. Not all |
| 488 |
// instructions suppport modifiers and immediates at the same time. |
| 489 |
// But leave Zero sources alone as we don't want to make things |
| 490 |
// immediates that could just be rZ. |
| 491 |
if let Some(u) = src.as_u32(src_types[i]) { |
| 492 |
*src = u.into(); |
| 493 |
} |
| 494 |
} |
| 495 |
b.copy_src_if_not_same_file(src); |
| 496 |
|
| 497 |
if !block_uniform { |
| 498 |
// In non-uniform control-flow, we can't collect uniform vectors so |
| 499 |
// we need to insert copies to warp regs which we can collect. |
| 500 |
match &mut src.src_ref { |
| 501 |
SrcRef::SSA(vec) => { |
| 502 |
if vec.is_uniform() |
| 503 |
&& vec.comps() > 1 |
| 504 |
&& !pinned.contains(vec) |
| 505 |
{ |
| 506 |
b.copy_ssa_ref(vec, vec.file().to_warp()); |
| 507 |
} |
| 508 |
} |
| 509 |
SrcRef::CBuf(CBufRef { |
| 510 |
buf: CBuf::BindlessSSA(handle), |
| 511 |
.. |
| 512 |
}) => assert!(pinned.contains(&SSARef::new(handle))), |
| 513 |
_ => (), |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
// OpBreak and OpBSsy impose additional RA constraints |
| 519 |
let mut legalize_break_bssy = |bar_in: &mut Src, bar_out: &mut Dst| { |
| 520 |
let bar_in_ssa = bar_in.src_ref.as_ssa().unwrap(); |
| 521 |
if !bar_out.is_none() && bl.is_live_after_ip(&bar_in_ssa[0], ip) { |
| 522 |
let gpr = b.bmov_to_gpr(bar_in.clone()); |
| 523 |
let tmp = b.bmov_to_bar(gpr.into()); |
| 524 |
*bar_in = tmp.into(); |
| 525 |
} |
| 526 |
}; |
| 527 |
match &mut instr.op { |
| 528 |
Op::Break(op) => legalize_break_bssy(&mut op.bar_in, &mut op.bar_out), |
| 529 |
Op::BSSy(op) => legalize_break_bssy(&mut op.bar_in, &mut op.bar_out), |
| 530 |
_ => (), |
| 531 |
} |
| 532 |
|
| 533 |
sm.legalize_op(b, &mut instr.op); |
| 534 |
|
| 535 |
let mut vec_src_map: FxHashMap<SSARef, SSARef> = Default::default(); |
| 536 |
let mut vec_comps: FxHashSet<_> = Default::default(); |
| 537 |
for src in instr.srcs_mut() { |
| 538 |
if let SrcRef::SSA(vec) = &src.src_ref { |
| 539 |
if vec.comps() == 1 { |
| 540 |
continue; |
| 541 |
} |
| 542 |
|
| 543 |
// If the same vector shows up twice in one instruction, that's |
| 544 |
// okay. Just make it look the same as the previous source we |
| 545 |
// fixed up. |
| 546 |
if let Some(new_vec) = vec_src_map.get(vec) { |
| 547 |
src.src_ref = new_vec.clone().into(); |
| 548 |
continue; |
| 549 |
} |
| 550 |
|
| 551 |
let mut new_vec = vec.clone(); |
| 552 |
for c in 0..vec.comps() { |
| 553 |
let ssa = vec[usize::from(c)]; |
| 554 |
// If the same SSA value shows up in multiple non-identical |
| 555 |
// vector sources or as multiple components in the same |
| 556 |
// source, we need to make a copy so it can get assigned to |
| 557 |
// multiple different registers. |
| 558 |
if vec_comps.contains(&ssa) { |
| 559 |
let copy = b.alloc_ssa(ssa.file()); |
| 560 |
b.copy_to(copy.into(), ssa.into()); |
| 561 |
new_vec[usize::from(c)] = copy; |
| 562 |
} else { |
| 563 |
vec_comps.insert(ssa); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
vec_src_map.insert(vec.clone(), new_vec.clone()); |
| 568 |
src.src_ref = new_vec.into(); |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
impl Shader<'_> { |
| 574 |
pub fn legalize(&mut self) { |
| 575 |
let sm = self.sm; |
| 576 |
for f in &mut self.functions { |
| 577 |
let live = SimpleLiveness::for_function(f); |
| 578 |
let mut pinned: FxHashSet<_> = Default::default(); |
| 579 |
let mut const_tracker = ConstTracker::new(); |
| 580 |
|
| 581 |
for (bi, b) in f.blocks.iter_mut().enumerate() { |
| 582 |
let bl = live.block_live(bi); |
| 583 |
let bu = b.uniform; |
| 584 |
|
| 585 |
let mut instrs = Vec::new(); |
| 586 |
for (ip, mut instr) in b.instrs.drain(..).enumerate() { |
| 587 |
match &instr.op { |
| 588 |
Op::Pin(pin) => { |
| 589 |
if let Dst::SSA(ssa) = &pin.dst { |
| 590 |
pinned.insert(ssa.clone()); |
| 591 |
} |
| 592 |
} |
| 593 |
Op::Copy(copy) => { |
| 594 |
const_tracker.add_copy(copy); |
| 595 |
} |
| 596 |
_ => (), |
| 597 |
} |
| 598 |
|
| 599 |
let mut b = LegalizeBuilder::new( |
| 600 |
sm, |
| 601 |
&mut f.ssa_alloc, |
| 602 |
&mut const_tracker, |
| 603 |
); |
| 604 |
legalize_instr(sm, &mut b, bl, bu, &pinned, ip, &mut instr); |
| 605 |
b.push_instr(instr); |
| 606 |
instrs.append(&mut b.into_vec()); |
| 607 |
} |
| 608 |
b.instrs = instrs; |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
} |