| 1 |
// Copyright © 2022 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::ir::*; |
| 5 |
|
| 6 |
use rustc_hash::FxHashMap; |
| 7 |
|
| 8 |
enum CBufRule { |
| 9 |
Yes, |
| 10 |
No, |
| 11 |
BindlessRequiresBlock(usize), |
| 12 |
} |
| 13 |
|
| 14 |
impl CBufRule { |
| 15 |
fn allows_src(&self, src_bi: usize, src: &Src) -> bool { |
| 16 |
let SrcRef::CBuf(cb) = &src.src_ref else { |
| 17 |
return true; |
| 18 |
}; |
| 19 |
|
| 20 |
match self { |
| 21 |
CBufRule::Yes => true, |
| 22 |
CBufRule::No => false, |
| 23 |
CBufRule::BindlessRequiresBlock(bi) => match cb.buf { |
| 24 |
CBuf::Binding(_) => true, |
| 25 |
CBuf::BindlessSSA(_) => src_bi == *bi, |
| 26 |
CBuf::BindlessUGPR(_) => panic!("Not in SSA form"), |
| 27 |
}, |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
struct CopyEntry { |
| 33 |
bi: usize, |
| 34 |
src_type: SrcType, |
| 35 |
src: Src, |
| 36 |
} |
| 37 |
|
| 38 |
struct PrmtEntry { |
| 39 |
bi: usize, |
| 40 |
sel: PrmtSel, |
| 41 |
srcs: [Src; 2], |
| 42 |
} |
| 43 |
|
| 44 |
/// This entry tracks b2i conversions |
| 45 |
struct ConvBoolToInt { |
| 46 |
src: Src, |
| 47 |
} |
| 48 |
|
| 49 |
enum CopyPropEntry { |
| 50 |
Copy(CopyEntry), |
| 51 |
Prmt(PrmtEntry), |
| 52 |
ConvBoolToInt(ConvBoolToInt), |
| 53 |
} |
| 54 |
|
| 55 |
struct CopyPropPass<'a> { |
| 56 |
sm: &'a ShaderModelInfo, |
| 57 |
ssa_map: FxHashMap<SSAValue, CopyPropEntry>, |
| 58 |
} |
| 59 |
|
| 60 |
impl<'a> CopyPropPass<'a> { |
| 61 |
pub fn new(sm: &'a ShaderModelInfo) -> Self { |
| 62 |
CopyPropPass { |
| 63 |
sm: sm, |
| 64 |
ssa_map: Default::default(), |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
fn add_copy( |
| 69 |
&mut self, |
| 70 |
bi: usize, |
| 71 |
dst: SSAValue, |
| 72 |
src_type: SrcType, |
| 73 |
src: Src, |
| 74 |
) { |
| 75 |
assert!(src.src_ref.get_reg().is_none()); |
| 76 |
self.ssa_map |
| 77 |
.insert(dst, CopyPropEntry::Copy(CopyEntry { bi, src_type, src })); |
| 78 |
} |
| 79 |
|
| 80 |
fn add_b2i(&mut self, _bi: usize, dst: SSAValue, src: Src) { |
| 81 |
assert!(src.src_ref.get_reg().is_none()); |
| 82 |
assert!(src.is_predicate()); |
| 83 |
assert!(dst.is_gpr()); |
| 84 |
self.ssa_map |
| 85 |
.insert(dst, CopyPropEntry::ConvBoolToInt(ConvBoolToInt { src })); |
| 86 |
} |
| 87 |
|
| 88 |
fn add_i2b(&mut self, bi: usize, dst: SSAValue, src: Src, inverted: bool) { |
| 89 |
assert!(src.src_ref.get_reg().is_none()); |
| 90 |
assert!(dst.is_predicate()); |
| 91 |
// Fold i2b(b2i(x)) |
| 92 |
// if we find i2b(b2i(x)) replace that with a copy |
| 93 |
let parent = src.as_ssa().and_then(|x| self.get_copy(&x[0])); |
| 94 |
let Some(CopyPropEntry::ConvBoolToInt(par_entry)) = parent else { |
| 95 |
return; |
| 96 |
}; |
| 97 |
let mut copy_src = par_entry.src.clone().modify(src.src_mod); |
| 98 |
if inverted { |
| 99 |
copy_src = copy_src.bnot(); |
| 100 |
} |
| 101 |
|
| 102 |
self.add_copy(bi, dst, SrcType::Pred, copy_src); |
| 103 |
} |
| 104 |
|
| 105 |
fn add_prmt( |
| 106 |
&mut self, |
| 107 |
bi: usize, |
| 108 |
dst: SSAValue, |
| 109 |
sel: PrmtSel, |
| 110 |
srcs: [Src; 2], |
| 111 |
) { |
| 112 |
assert!( |
| 113 |
srcs[0].src_ref.get_reg().is_none() |
| 114 |
&& srcs[1].src_ref.get_reg().is_none() |
| 115 |
); |
| 116 |
self.ssa_map |
| 117 |
.insert(dst, CopyPropEntry::Prmt(PrmtEntry { bi, sel, srcs })); |
| 118 |
} |
| 119 |
|
| 120 |
fn add_fp64_copy(&mut self, bi: usize, dst: &SSARef, src: Src) { |
| 121 |
assert!(dst.comps() == 2); |
| 122 |
match src.src_ref { |
| 123 |
SrcRef::Zero | SrcRef::Imm32(_) => { |
| 124 |
self.add_copy(bi, dst[0], SrcType::ALU, Src::ZERO); |
| 125 |
self.add_copy(bi, dst[1], SrcType::F64, src); |
| 126 |
} |
| 127 |
SrcRef::CBuf(cb) => { |
| 128 |
let lo32 = Src::from(SrcRef::CBuf(cb.clone())); |
| 129 |
let hi32 = Src { |
| 130 |
src_ref: SrcRef::CBuf(cb.offset(4)), |
| 131 |
src_mod: src.src_mod, |
| 132 |
src_swizzle: src.src_swizzle, |
| 133 |
}; |
| 134 |
self.add_copy(bi, dst[0], SrcType::ALU, lo32); |
| 135 |
self.add_copy(bi, dst[1], SrcType::F64, hi32); |
| 136 |
} |
| 137 |
SrcRef::SSA(ssa) => { |
| 138 |
assert!(ssa.comps() == 2); |
| 139 |
let lo32 = Src::from(ssa[0]); |
| 140 |
let hi32 = Src { |
| 141 |
src_ref: ssa[1].into(), |
| 142 |
src_mod: src.src_mod, |
| 143 |
src_swizzle: src.src_swizzle, |
| 144 |
}; |
| 145 |
self.add_copy(bi, dst[0], SrcType::ALU, lo32); |
| 146 |
self.add_copy(bi, dst[1], SrcType::F64, hi32); |
| 147 |
} |
| 148 |
_ => (), |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
fn get_copy(&self, dst: &SSAValue) -> Option<&CopyPropEntry> { |
| 153 |
self.ssa_map.get(dst) |
| 154 |
} |
| 155 |
|
| 156 |
fn prop_to_pred(&self, pred: &mut Pred) { |
| 157 |
loop { |
| 158 |
let src_ssa = match &pred.pred_ref { |
| 159 |
PredRef::SSA(ssa) => ssa, |
| 160 |
_ => return, |
| 161 |
}; |
| 162 |
|
| 163 |
let Some(CopyPropEntry::Copy(entry)) = self.get_copy(src_ssa) |
| 164 |
else { |
| 165 |
return; |
| 166 |
}; |
| 167 |
|
| 168 |
match &entry.src.src_ref { |
| 169 |
SrcRef::True => { |
| 170 |
pred.pred_ref = PredRef::None; |
| 171 |
} |
| 172 |
SrcRef::False => { |
| 173 |
pred.pred_ref = PredRef::None; |
| 174 |
pred.pred_inv = !pred.pred_inv; |
| 175 |
} |
| 176 |
SrcRef::SSA(ssa) => { |
| 177 |
assert!(ssa.comps() == 1); |
| 178 |
pred.pred_ref = PredRef::SSA(ssa[0]); |
| 179 |
} |
| 180 |
_ => return, |
| 181 |
} |
| 182 |
|
| 183 |
match entry.src.src_mod { |
| 184 |
SrcMod::None => (), |
| 185 |
SrcMod::BNot => { |
| 186 |
pred.pred_inv = !pred.pred_inv; |
| 187 |
} |
| 188 |
_ => panic!("Invalid predicate modifier"), |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
fn prop_to_ssa_values( |
| 194 |
&self, |
| 195 |
src_ssa: &mut [SSAValue], |
| 196 |
same_file: bool, |
| 197 |
) -> bool { |
| 198 |
let mut progress = false; |
| 199 |
|
| 200 |
for c_ssa in src_ssa { |
| 201 |
let Some(CopyPropEntry::Copy(entry)) = self.get_copy(c_ssa) else { |
| 202 |
continue; |
| 203 |
}; |
| 204 |
|
| 205 |
if entry.src.is_unmodified() { |
| 206 |
if let SrcRef::SSA(entry_ssa) = &entry.src.src_ref { |
| 207 |
assert!(entry_ssa.comps() == 1); |
| 208 |
|
| 209 |
if same_file && (c_ssa.file() != entry_ssa[0].file()) { |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
*c_ssa = entry_ssa[0]; |
| 214 |
progress = true; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
progress |
| 220 |
} |
| 221 |
|
| 222 |
fn prop_to_ssa_ref(&self, src_ssa: &mut SSARef) -> bool { |
| 223 |
self.prop_to_ssa_values(&mut src_ssa[..], false) |
| 224 |
} |
| 225 |
|
| 226 |
fn prop_to_cbuf_ref(&self, cbuf: &mut CBufRef) { |
| 227 |
match cbuf.buf { |
| 228 |
CBuf::BindlessSSA(ref mut ssa_values) => loop { |
| 229 |
if !self.prop_to_ssa_values(&mut ssa_values[..], true) { |
| 230 |
break; |
| 231 |
} |
| 232 |
}, |
| 233 |
_ => (), |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
fn prop_to_ssa_src(&self, src: &mut Src) { |
| 238 |
assert!(src.is_unmodified()); |
| 239 |
if let SrcRef::SSA(src_ssa) = &mut src.src_ref { |
| 240 |
loop { |
| 241 |
if !self.prop_to_ssa_ref(src_ssa) { |
| 242 |
break; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
fn prop_to_gpr_src(&self, src: &mut Src) { |
| 249 |
loop { |
| 250 |
let src_ssa = match &mut src.src_ref { |
| 251 |
SrcRef::SSA(ssa) => { |
| 252 |
// First, try to propagate SSA components |
| 253 |
if self.prop_to_ssa_ref(ssa) { |
| 254 |
continue; |
| 255 |
} |
| 256 |
ssa |
| 257 |
} |
| 258 |
_ => return, |
| 259 |
}; |
| 260 |
|
| 261 |
for c in 0..usize::from(src_ssa.comps()) { |
| 262 |
let Some(CopyPropEntry::Copy(entry)) = |
| 263 |
self.get_copy(&src_ssa[c]) |
| 264 |
else { |
| 265 |
return; |
| 266 |
}; |
| 267 |
|
| 268 |
match entry.src.src_ref { |
| 269 |
SrcRef::Zero | SrcRef::Imm32(0) => (), |
| 270 |
_ => return, |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// If we got here, all the components are zero |
| 275 |
src.src_ref = SrcRef::Zero; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
fn prop_to_scalar_src( |
| 280 |
&self, |
| 281 |
src_type: SrcType, |
| 282 |
cbuf_rule: &CBufRule, |
| 283 |
src: &mut Src, |
| 284 |
) { |
| 285 |
loop { |
| 286 |
let src_ssa = match &src.src_ref { |
| 287 |
SrcRef::SSA(ssa) => ssa, |
| 288 |
_ => return, |
| 289 |
}; |
| 290 |
|
| 291 |
assert!(src_ssa.comps() == 1); |
| 292 |
let entry = match self.get_copy(&src_ssa[0]) { |
| 293 |
Some(e) => e, |
| 294 |
None => return, |
| 295 |
}; |
| 296 |
|
| 297 |
match entry { |
| 298 |
CopyPropEntry::Copy(entry) => { |
| 299 |
if !cbuf_rule.allows_src(entry.bi, &entry.src) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
// If there are modifiers, the source types have to match |
| 304 |
if !entry.src.is_unmodified() && entry.src_type != src_type |
| 305 |
{ |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
src.src_ref = entry.src.src_ref.clone(); |
| 310 |
src.src_mod = entry.src.src_mod.modify(src.src_mod); |
| 311 |
} |
| 312 |
CopyPropEntry::Prmt(entry) => { |
| 313 |
// Turn the swizzle into a permute. For F16, we use Xx to |
| 314 |
// indicate that it only takes the bottom 16 bits. |
| 315 |
let swizzle_prmt: [u8; 4] = match src_type { |
| 316 |
SrcType::F16 => [0, 1, 0, 1], |
| 317 |
SrcType::F16v2 => match src.src_swizzle { |
| 318 |
SrcSwizzle::None => [0, 1, 2, 3], |
| 319 |
SrcSwizzle::Xx => [0, 1, 0, 1], |
| 320 |
SrcSwizzle::Yy => [2, 3, 2, 3], |
| 321 |
}, |
| 322 |
_ => [0, 1, 2, 3], |
| 323 |
}; |
| 324 |
|
| 325 |
let mut entry_src_idx = None; |
| 326 |
let mut combined = [0_u8; 4]; |
| 327 |
|
| 328 |
for i in 0..4 { |
| 329 |
let prmt_byte = entry.sel.get(swizzle_prmt[i].into()); |
| 330 |
|
| 331 |
// If we have a sign extension, we cannot simplify it. |
| 332 |
if prmt_byte.msb() { |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
// Ensure we are using the same source, we cannot |
| 337 |
// combine multiple sources. |
| 338 |
if entry_src_idx.is_none() { |
| 339 |
entry_src_idx = Some(prmt_byte.src()); |
| 340 |
} else if entry_src_idx != Some(prmt_byte.src()) { |
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
combined[i] = prmt_byte.byte().try_into().unwrap(); |
| 345 |
} |
| 346 |
|
| 347 |
let entry_src_idx = entry_src_idx.unwrap(); |
| 348 |
let entry_src = &entry.srcs[entry_src_idx]; |
| 349 |
|
| 350 |
if !cbuf_rule.allows_src(entry.bi, entry_src) { |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
// See if that permute is a valid swizzle |
| 355 |
let new_swizzle = match src_type { |
| 356 |
SrcType::F16 => { |
| 357 |
if combined != [0, 1, 0, 1] { |
| 358 |
return; |
| 359 |
} |
| 360 |
SrcSwizzle::None |
| 361 |
} |
| 362 |
SrcType::F16v2 => match combined { |
| 363 |
[0, 1, 2, 3] => SrcSwizzle::None, |
| 364 |
[0, 1, 0, 1] => SrcSwizzle::Xx, |
| 365 |
[2, 3, 2, 3] => SrcSwizzle::Yy, |
| 366 |
_ => return, |
| 367 |
}, |
| 368 |
_ => { |
| 369 |
if combined != [0, 1, 2, 3] { |
| 370 |
return; |
| 371 |
} |
| 372 |
SrcSwizzle::None |
| 373 |
} |
| 374 |
}; |
| 375 |
|
| 376 |
src.src_ref = entry_src.src_ref.clone(); |
| 377 |
src.src_mod = entry_src.src_mod.modify(src.src_mod); |
| 378 |
src.src_swizzle = new_swizzle; |
| 379 |
} |
| 380 |
CopyPropEntry::ConvBoolToInt(_) => { |
| 381 |
// b2i(i2b(x)) can't be easily optimized |
| 382 |
return; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
fn prop_to_f64_src(&self, cbuf_rule: &CBufRule, src: &mut Src) { |
| 389 |
loop { |
| 390 |
let src_ssa = match &mut src.src_ref { |
| 391 |
SrcRef::SSA(ssa) => ssa, |
| 392 |
_ => return, |
| 393 |
}; |
| 394 |
|
| 395 |
assert!(src_ssa.comps() == 2); |
| 396 |
|
| 397 |
// First, try to propagate the two halves individually. Source |
| 398 |
// modifiers only apply to the high 32 bits so we have to reject |
| 399 |
// any copies with source modifiers in the low bits and apply |
| 400 |
// source modifiers as needed when propagating the high bits. |
| 401 |
let lo_entry_or_none = self.get_copy(&src_ssa[0]); |
| 402 |
if let Some(CopyPropEntry::Copy(lo_entry)) = lo_entry_or_none { |
| 403 |
if lo_entry.src.is_unmodified() { |
| 404 |
if let SrcRef::SSA(lo_entry_ssa) = &lo_entry.src.src_ref { |
| 405 |
src_ssa[0] = lo_entry_ssa[0]; |
| 406 |
continue; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
let hi_entry_or_none = self.get_copy(&src_ssa[1]); |
| 412 |
if let Some(CopyPropEntry::Copy(hi_entry)) = hi_entry_or_none { |
| 413 |
if hi_entry.src.is_unmodified() |
| 414 |
|| hi_entry.src_type == SrcType::F64 |
| 415 |
{ |
| 416 |
if let SrcRef::SSA(hi_entry_ssa) = &hi_entry.src.src_ref { |
| 417 |
src_ssa[1] = hi_entry_ssa[0]; |
| 418 |
src.src_mod = hi_entry.src.src_mod.modify(src.src_mod); |
| 419 |
continue; |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
let Some(CopyPropEntry::Copy(lo_entry)) = lo_entry_or_none else { |
| 425 |
return; |
| 426 |
}; |
| 427 |
|
| 428 |
let Some(CopyPropEntry::Copy(hi_entry)) = hi_entry_or_none else { |
| 429 |
return; |
| 430 |
}; |
| 431 |
|
| 432 |
if !lo_entry.src.is_unmodified() { |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
if !hi_entry.src.is_unmodified() |
| 437 |
&& hi_entry.src_type != SrcType::F64 |
| 438 |
{ |
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
if !cbuf_rule.allows_src(hi_entry.bi, &hi_entry.src) |
| 443 |
|| !cbuf_rule.allows_src(lo_entry.bi, &lo_entry.src) |
| 444 |
{ |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
let new_src_ref = match &hi_entry.src.src_ref { |
| 449 |
SrcRef::Zero => match &lo_entry.src.src_ref { |
| 450 |
SrcRef::Zero | SrcRef::Imm32(0) => SrcRef::Zero, |
| 451 |
_ => return, |
| 452 |
}, |
| 453 |
SrcRef::Imm32(i) => { |
| 454 |
// 32-bit immediates for f64 srouces are the top 32 bits |
| 455 |
// with zero in the lower 32. |
| 456 |
match lo_entry.src.src_ref { |
| 457 |
SrcRef::Zero | SrcRef::Imm32(0) => SrcRef::Imm32(*i), |
| 458 |
_ => return, |
| 459 |
} |
| 460 |
} |
| 461 |
SrcRef::CBuf(hi_cb) => match &lo_entry.src.src_ref { |
| 462 |
SrcRef::CBuf(lo_cb) => { |
| 463 |
if hi_cb.buf != lo_cb.buf { |
| 464 |
return; |
| 465 |
} |
| 466 |
if lo_cb.offset % 8 != 0 { |
| 467 |
return; |
| 468 |
} |
| 469 |
if hi_cb.offset != lo_cb.offset + 4 { |
| 470 |
return; |
| 471 |
} |
| 472 |
SrcRef::CBuf(lo_cb.clone()) |
| 473 |
} |
| 474 |
_ => return, |
| 475 |
}, |
| 476 |
// SrcRef::SSA is already handled above |
| 477 |
_ => return, |
| 478 |
}; |
| 479 |
|
| 480 |
src.src_ref = new_src_ref; |
| 481 |
src.src_mod = hi_entry.src.src_mod.modify(src.src_mod); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
fn prop_to_src( |
| 486 |
&self, |
| 487 |
src_type: SrcType, |
| 488 |
cbuf_rule: &CBufRule, |
| 489 |
src: &mut Src, |
| 490 |
) { |
| 491 |
match src_type { |
| 492 |
SrcType::SSA => { |
| 493 |
self.prop_to_ssa_src(src); |
| 494 |
} |
| 495 |
SrcType::GPR => { |
| 496 |
self.prop_to_gpr_src(src); |
| 497 |
} |
| 498 |
SrcType::ALU |
| 499 |
| SrcType::F16 |
| 500 |
| SrcType::F16v2 |
| 501 |
| SrcType::F32 |
| 502 |
| SrcType::I32 |
| 503 |
| SrcType::B32 |
| 504 |
| SrcType::Pred => { |
| 505 |
self.prop_to_scalar_src(src_type, cbuf_rule, src); |
| 506 |
} |
| 507 |
SrcType::F64 => { |
| 508 |
self.prop_to_f64_src(cbuf_rule, src); |
| 509 |
} |
| 510 |
SrcType::Carry | SrcType::Bar => (), |
| 511 |
} |
| 512 |
|
| 513 |
match &mut src.src_ref { |
| 514 |
SrcRef::CBuf(cbuf) => { |
| 515 |
self.prop_to_cbuf_ref(cbuf); |
| 516 |
} |
| 517 |
_ => (), |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
fn try_add_instr(&mut self, bi: usize, instr: &Instr) { |
| 522 |
match &instr.op { |
| 523 |
Op::HAdd2(add) => { |
| 524 |
let dst = add.dst.as_ssa().unwrap(); |
| 525 |
assert!(dst.comps() == 1); |
| 526 |
let dst = dst[0]; |
| 527 |
|
| 528 |
if !add.saturate && !add.ftz { |
| 529 |
if add.srcs[0].is_fneg_zero(SrcType::F16v2) { |
| 530 |
self.add_copy( |
| 531 |
bi, |
| 532 |
dst, |
| 533 |
SrcType::F16v2, |
| 534 |
add.srcs[1].clone(), |
| 535 |
); |
| 536 |
} else if add.srcs[1].is_fneg_zero(SrcType::F16v2) { |
| 537 |
self.add_copy( |
| 538 |
bi, |
| 539 |
dst, |
| 540 |
SrcType::F16v2, |
| 541 |
add.srcs[0].clone(), |
| 542 |
); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
Op::FAdd(add) => { |
| 547 |
let dst = add.dst.as_ssa().unwrap(); |
| 548 |
assert!(dst.comps() == 1); |
| 549 |
let dst = dst[0]; |
| 550 |
|
| 551 |
if !add.saturate && !add.ftz { |
| 552 |
if add.srcs[0].is_fneg_zero(SrcType::F32) { |
| 553 |
self.add_copy( |
| 554 |
bi, |
| 555 |
dst, |
| 556 |
SrcType::F32, |
| 557 |
add.srcs[1].clone(), |
| 558 |
); |
| 559 |
} else if add.srcs[1].is_fneg_zero(SrcType::F32) { |
| 560 |
self.add_copy( |
| 561 |
bi, |
| 562 |
dst, |
| 563 |
SrcType::F32, |
| 564 |
add.srcs[0].clone(), |
| 565 |
); |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
Op::DAdd(add) => { |
| 570 |
let dst = add.dst.as_ssa().unwrap(); |
| 571 |
if add.srcs[0].is_fneg_zero(SrcType::F64) { |
| 572 |
self.add_fp64_copy(bi, dst, add.srcs[1].clone()); |
| 573 |
} else if add.srcs[1].is_fneg_zero(SrcType::F64) { |
| 574 |
self.add_fp64_copy(bi, dst, add.srcs[0].clone()); |
| 575 |
} |
| 576 |
} |
| 577 |
Op::Lop3(lop) => { |
| 578 |
let dst = lop.dst.as_ssa().unwrap(); |
| 579 |
assert!(dst.comps() == 1); |
| 580 |
let dst = dst[0]; |
| 581 |
|
| 582 |
let op = lop.op; |
| 583 |
if op.lut == 0 { |
| 584 |
self.add_copy(bi, dst, SrcType::ALU, SrcRef::Zero.into()); |
| 585 |
} else if op.lut == !0 { |
| 586 |
self.add_copy( |
| 587 |
bi, |
| 588 |
dst, |
| 589 |
SrcType::ALU, |
| 590 |
SrcRef::Imm32(u32::MAX).into(), |
| 591 |
); |
| 592 |
} else { |
| 593 |
for s in 0..3 { |
| 594 |
if op.lut == LogicOp3::SRC_MASKS[s] { |
| 595 |
self.add_copy( |
| 596 |
bi, |
| 597 |
dst, |
| 598 |
SrcType::ALU, |
| 599 |
lop.srcs[s].clone(), |
| 600 |
); |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
Op::PLop3(lop) => { |
| 606 |
for i in 0..2 { |
| 607 |
let dst = match &lop.dsts[i] { |
| 608 |
Dst::SSA(vec) => { |
| 609 |
assert!(vec.comps() == 1); |
| 610 |
vec[0] |
| 611 |
} |
| 612 |
_ => continue, |
| 613 |
}; |
| 614 |
|
| 615 |
let op = lop.ops[i]; |
| 616 |
if op.lut == 0 { |
| 617 |
self.add_copy( |
| 618 |
bi, |
| 619 |
dst, |
| 620 |
SrcType::Pred, |
| 621 |
SrcRef::False.into(), |
| 622 |
); |
| 623 |
} else if op.lut == !0 { |
| 624 |
self.add_copy( |
| 625 |
bi, |
| 626 |
dst, |
| 627 |
SrcType::Pred, |
| 628 |
SrcRef::True.into(), |
| 629 |
); |
| 630 |
} else { |
| 631 |
for s in 0..3 { |
| 632 |
if op.lut == LogicOp3::SRC_MASKS[s] { |
| 633 |
self.add_copy( |
| 634 |
bi, |
| 635 |
dst, |
| 636 |
SrcType::Pred, |
| 637 |
lop.srcs[s].clone(), |
| 638 |
); |
| 639 |
} else if op.lut == !LogicOp3::SRC_MASKS[s] { |
| 640 |
self.add_copy( |
| 641 |
bi, |
| 642 |
dst, |
| 643 |
SrcType::Pred, |
| 644 |
lop.srcs[s].clone().bnot(), |
| 645 |
); |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
Op::Sel(sel) => { |
| 652 |
let dst = sel.dst.as_ssa().unwrap(); |
| 653 |
assert!(dst.comps() == 1); |
| 654 |
let dst = dst[0]; |
| 655 |
|
| 656 |
let src = match &sel.srcs { |
| 657 |
[z, u] if z.is_zero() && u.is_nonzero() => { |
| 658 |
sel.cond.clone().bnot() |
| 659 |
} |
| 660 |
[u, z] if z.is_zero() && u.is_nonzero() => sel.cond.clone(), |
| 661 |
_ => return, |
| 662 |
}; |
| 663 |
|
| 664 |
self.add_b2i(bi, dst, src); |
| 665 |
} |
| 666 |
Op::ISetP(isetp) if isetp.set_op.is_trivial(&isetp.accum) => { |
| 667 |
let dst = isetp.dst.as_ssa().unwrap(); |
| 668 |
assert!(dst.comps() == 1); |
| 669 |
let dst = dst[0]; |
| 670 |
|
| 671 |
let src = match (&isetp.srcs[0], &isetp.srcs[1]) { |
| 672 |
(z, x) | (x, z) if z.is_zero() => x, |
| 673 |
_ => return, |
| 674 |
}; |
| 675 |
|
| 676 |
// -0 = 0 |
| 677 |
// -x != 0 => x != 0 |
| 678 |
if !matches!(src.src_mod, SrcMod::None | SrcMod::INeg) { |
| 679 |
return; |
| 680 |
} |
| 681 |
|
| 682 |
// x op 0 |
| 683 |
let inverted = match isetp.cmp_op { |
| 684 |
IntCmpOp::Eq => true, |
| 685 |
IntCmpOp::Ne => false, |
| 686 |
_ => return, |
| 687 |
}; |
| 688 |
self.add_i2b(bi, dst, src.clone(), inverted); |
| 689 |
} |
| 690 |
Op::IAdd2(add) => { |
| 691 |
let dst = add.dst.as_ssa().unwrap(); |
| 692 |
assert!(dst.comps() == 1); |
| 693 |
let dst = dst[0]; |
| 694 |
|
| 695 |
if add.srcs[0].is_zero() { |
| 696 |
self.add_copy(bi, dst, SrcType::I32, add.srcs[1].clone()); |
| 697 |
} else if add.srcs[1].is_zero() { |
| 698 |
self.add_copy(bi, dst, SrcType::I32, add.srcs[0].clone()); |
| 699 |
} |
| 700 |
} |
| 701 |
Op::IAdd3(add) => { |
| 702 |
let dst = add.dst.as_ssa().unwrap(); |
| 703 |
assert!(dst.comps() == 1); |
| 704 |
let dst = dst[0]; |
| 705 |
|
| 706 |
if add.srcs[0].is_zero() { |
| 707 |
if add.srcs[1].is_zero() { |
| 708 |
self.add_copy( |
| 709 |
bi, |
| 710 |
dst, |
| 711 |
SrcType::I32, |
| 712 |
add.srcs[2].clone(), |
| 713 |
); |
| 714 |
} else if add.srcs[2].is_zero() { |
| 715 |
self.add_copy( |
| 716 |
bi, |
| 717 |
dst, |
| 718 |
SrcType::I32, |
| 719 |
add.srcs[1].clone(), |
| 720 |
); |
| 721 |
} |
| 722 |
} else if add.srcs[1].is_zero() && add.srcs[2].is_zero() { |
| 723 |
self.add_copy(bi, dst, SrcType::I32, add.srcs[0].clone()); |
| 724 |
} |
| 725 |
} |
| 726 |
Op::Prmt(prmt) => { |
| 727 |
let dst = prmt.dst.as_ssa().unwrap(); |
| 728 |
assert!(dst.comps() == 1); |
| 729 |
if let Some(sel) = prmt.get_sel() { |
| 730 |
if let Some(imm) = prmt.as_u32() { |
| 731 |
self.add_copy(bi, dst[0], SrcType::GPR, imm.into()); |
| 732 |
} else if sel == PrmtSel(0x3210) { |
| 733 |
self.add_copy( |
| 734 |
bi, |
| 735 |
dst[0], |
| 736 |
SrcType::GPR, |
| 737 |
prmt.srcs[0].clone(), |
| 738 |
); |
| 739 |
} else if sel == PrmtSel(0x7654) { |
| 740 |
self.add_copy( |
| 741 |
bi, |
| 742 |
dst[0], |
| 743 |
SrcType::GPR, |
| 744 |
prmt.srcs[1].clone(), |
| 745 |
); |
| 746 |
} else { |
| 747 |
self.add_prmt(bi, dst[0], sel, prmt.srcs.clone()); |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
Op::R2UR(r2ur) => { |
| 752 |
assert!(r2ur.src.is_unmodified()); |
| 753 |
if r2ur.src.is_uniform() { |
| 754 |
let dst = r2ur.dst.as_ssa().unwrap(); |
| 755 |
assert!(dst.comps() == 1); |
| 756 |
self.add_copy(bi, dst[0], SrcType::GPR, r2ur.src.clone()); |
| 757 |
} |
| 758 |
} |
| 759 |
Op::Copy(copy) => { |
| 760 |
let dst = copy.dst.as_ssa().unwrap(); |
| 761 |
assert!(dst.comps() == 1); |
| 762 |
self.add_copy(bi, dst[0], SrcType::GPR, copy.src.clone()); |
| 763 |
} |
| 764 |
Op::ParCopy(pcopy) => { |
| 765 |
for (dst, src) in pcopy.dsts_srcs.iter() { |
| 766 |
let dst = dst.as_ssa().unwrap(); |
| 767 |
assert!(dst.comps() == 1); |
| 768 |
self.add_copy(bi, dst[0], SrcType::GPR, src.clone()); |
| 769 |
} |
| 770 |
} |
| 771 |
_ => (), |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
pub fn run(&mut self, f: &mut Function) { |
| 776 |
for (bi, b) in f.blocks.iter_mut().enumerate() { |
| 777 |
let b_uniform = b.uniform; |
| 778 |
for instr in &mut b.instrs { |
| 779 |
self.try_add_instr(bi, instr); |
| 780 |
|
| 781 |
self.prop_to_pred(&mut instr.pred); |
| 782 |
|
| 783 |
let cbuf_rule = if self.sm.sm() >= 100 { |
| 784 |
// Blackwell+ doesn't allow cbufs directly in instruction |
| 785 |
// sources anymore and instead have to be explicitly loaded |
| 786 |
// with OpLdc. |
| 787 |
CBufRule::No |
| 788 |
} else if instr.is_uniform() { |
| 789 |
CBufRule::No |
| 790 |
} else if !b_uniform { |
| 791 |
CBufRule::BindlessRequiresBlock(bi) |
| 792 |
} else { |
| 793 |
CBufRule::Yes |
| 794 |
}; |
| 795 |
|
| 796 |
// Carry-out and overflow interact funny with SrcMod::INeg so we |
| 797 |
// can only propagate with modifiers if no carry/overflow is |
| 798 |
// written. |
| 799 |
let force_alu_src_type = match &instr.op { |
| 800 |
Op::IAdd2(add) => !add.carry_out.is_none(), |
| 801 |
Op::IAdd2X(add) => !add.carry_out.is_none(), |
| 802 |
Op::IAdd3(add) => { |
| 803 |
!add.overflow[0].is_none() || !add.overflow[1].is_none() |
| 804 |
} |
| 805 |
Op::IAdd3X(add) => { |
| 806 |
!add.overflow[0].is_none() || !add.overflow[1].is_none() |
| 807 |
} |
| 808 |
Op::Lea(lea) => !lea.overflow.is_none(), |
| 809 |
Op::LeaX(lea) => !lea.overflow.is_none(), |
| 810 |
_ => false, |
| 811 |
}; |
| 812 |
|
| 813 |
let src_types = instr.src_types(); |
| 814 |
for (i, src) in instr.srcs_mut().iter_mut().enumerate() { |
| 815 |
let mut src_type = src_types[i]; |
| 816 |
if force_alu_src_type { |
| 817 |
src_type = match src_type { |
| 818 |
SrcType::ALU | SrcType::B32 | SrcType::I32 => { |
| 819 |
SrcType::ALU |
| 820 |
} |
| 821 |
SrcType::Carry | SrcType::Pred => src_type, |
| 822 |
_ => panic!("Unhandled src_type"), |
| 823 |
}; |
| 824 |
}; |
| 825 |
self.prop_to_src(src_type, &cbuf_rule, src); |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
impl Shader<'_> { |
| 833 |
pub fn opt_copy_prop(&mut self) { |
| 834 |
for f in &mut self.functions { |
| 835 |
CopyPropPass::new(self.sm).run(f); |
| 836 |
} |
| 837 |
} |
| 838 |
} |