1 //===-- GCNHazardRecognizers.cpp - GCN Hazard Recognizer Impls ------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements hazard recognizers for scheduling on GCN processors. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "GCNHazardRecognizer.h" 14 #include "AMDGPUSubtarget.h" 15 #include "SIDefines.h" 16 #include "SIInstrInfo.h" 17 #include "SIRegisterInfo.h" 18 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 19 #include "Utils/AMDGPUBaseInfo.h" 20 #include "llvm/ADT/iterator_range.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineInstr.h" 23 #include "llvm/CodeGen/MachineInstrBuilder.h" 24 #include "llvm/CodeGen/MachineOperand.h" 25 #include "llvm/CodeGen/ScheduleDAG.h" 26 #include "llvm/MC/MCInstrDesc.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include <algorithm> 29 #include <cassert> 30 #include <limits> 31 #include <set> 32 #include <vector> 33 34 using namespace llvm; 35 36 //===----------------------------------------------------------------------===// 37 // Hazard Recoginizer Implementation 38 //===----------------------------------------------------------------------===// 39 40 GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) : 41 IsHazardRecognizerMode(false), 42 CurrCycleInstr(nullptr), 43 MF(MF), 44 ST(MF.getSubtarget<GCNSubtarget>()), 45 TII(*ST.getInstrInfo()), 46 TRI(TII.getRegisterInfo()), 47 ClauseUses(TRI.getNumRegUnits()), 48 ClauseDefs(TRI.getNumRegUnits()) { 49 MaxLookAhead = MF.getRegInfo().isPhysRegUsed(AMDGPU::AGPR0) ? 18 : 5; 50 TSchedModel.init(&ST); 51 } 52 53 void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { 54 EmitInstruction(SU->getInstr()); 55 } 56 57 void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { 58 CurrCycleInstr = MI; 59 } 60 61 static bool isDivFMas(unsigned Opcode) { 62 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64; 63 } 64 65 static bool isSGetReg(unsigned Opcode) { 66 return Opcode == AMDGPU::S_GETREG_B32; 67 } 68 69 static bool isSSetReg(unsigned Opcode) { 70 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32; 71 } 72 73 static bool isRWLane(unsigned Opcode) { 74 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; 75 } 76 77 static bool isRFE(unsigned Opcode) { 78 return Opcode == AMDGPU::S_RFE_B64; 79 } 80 81 static bool isSMovRel(unsigned Opcode) { 82 switch (Opcode) { 83 case AMDGPU::S_MOVRELS_B32: 84 case AMDGPU::S_MOVRELS_B64: 85 case AMDGPU::S_MOVRELD_B32: 86 case AMDGPU::S_MOVRELD_B64: 87 return true; 88 default: 89 return false; 90 } 91 } 92 93 static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII, 94 const MachineInstr &MI) { 95 if (TII.isAlwaysGDS(MI.getOpcode())) 96 return true; 97 98 switch (MI.getOpcode()) { 99 case AMDGPU::S_SENDMSG: 100 case AMDGPU::S_SENDMSGHALT: 101 case AMDGPU::S_TTRACEDATA: 102 return true; 103 // These DS opcodes don't support GDS. 104 case AMDGPU::DS_NOP: 105 case AMDGPU::DS_PERMUTE_B32: 106 case AMDGPU::DS_BPERMUTE_B32: 107 return false; 108 default: 109 if (TII.isDS(MI.getOpcode())) { 110 int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(), 111 AMDGPU::OpName::gds); 112 if (MI.getOperand(GDS).getImm()) 113 return true; 114 } 115 return false; 116 } 117 } 118 119 static bool isPermlane(const MachineInstr &MI) { 120 unsigned Opcode = MI.getOpcode(); 121 return Opcode == AMDGPU::V_PERMLANE16_B32 || 122 Opcode == AMDGPU::V_PERMLANEX16_B32; 123 } 124 125 static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { 126 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, 127 AMDGPU::OpName::simm16); 128 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; 129 } 130 131 ScheduleHazardRecognizer::HazardType 132 GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { 133 MachineInstr *MI = SU->getInstr(); 134 if (MI->isBundle()) 135 return NoHazard; 136 137 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) 138 return NoopHazard; 139 140 // FIXME: Should flat be considered vmem? 141 if ((SIInstrInfo::isVMEM(*MI) || 142 SIInstrInfo::isFLAT(*MI)) 143 && checkVMEMHazards(MI) > 0) 144 return NoopHazard; 145 146 if (ST.hasNSAtoVMEMBug() && checkNSAtoVMEMHazard(MI) > 0) 147 return NoopHazard; 148 149 if (checkFPAtomicToDenormModeHazard(MI) > 0) 150 return NoopHazard; 151 152 if (ST.hasNoDataDepHazard()) 153 return NoHazard; 154 155 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) 156 return NoopHazard; 157 158 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) 159 return NoopHazard; 160 161 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) 162 return NoopHazard; 163 164 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) 165 return NoopHazard; 166 167 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) 168 return NoopHazard; 169 170 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) 171 return NoopHazard; 172 173 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) 174 return NoopHazard; 175 176 if (ST.hasReadM0MovRelInterpHazard() && 177 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) && 178 checkReadM0Hazards(MI) > 0) 179 return NoopHazard; 180 181 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI) && 182 checkReadM0Hazards(MI) > 0) 183 return NoopHazard; 184 185 if (SIInstrInfo::isMAI(*MI) && checkMAIHazards(MI) > 0) 186 return NoopHazard; 187 188 if (MI->mayLoadOrStore() && checkMAILdStHazards(MI) > 0) 189 return NoopHazard; 190 191 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0) 192 return NoopHazard; 193 194 if (checkAnyInstHazards(MI) > 0) 195 return NoopHazard; 196 197 return NoHazard; 198 } 199 200 static void insertNoopInBundle(MachineInstr *MI, const SIInstrInfo &TII) { 201 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII.get(AMDGPU::S_NOP)) 202 .addImm(0); 203 } 204 205 void GCNHazardRecognizer::processBundle() { 206 MachineBasicBlock::instr_iterator MI = std::next(CurrCycleInstr->getIterator()); 207 MachineBasicBlock::instr_iterator E = CurrCycleInstr->getParent()->instr_end(); 208 // Check bundled MachineInstr's for hazards. 209 for (; MI != E && MI->isInsideBundle(); ++MI) { 210 CurrCycleInstr = &*MI; 211 unsigned WaitStates = PreEmitNoopsCommon(CurrCycleInstr); 212 213 if (IsHazardRecognizerMode) 214 fixHazards(CurrCycleInstr); 215 216 for (unsigned i = 0; i < WaitStates; ++i) 217 insertNoopInBundle(CurrCycleInstr, TII); 218 219 // It’s unnecessary to track more than MaxLookAhead instructions. Since we 220 // include the bundled MI directly after, only add a maximum of 221 // (MaxLookAhead - 1) noops to EmittedInstrs. 222 for (unsigned i = 0, e = std::min(WaitStates, MaxLookAhead - 1); i < e; ++i) 223 EmittedInstrs.push_front(nullptr); 224 225 EmittedInstrs.push_front(CurrCycleInstr); 226 EmittedInstrs.resize(MaxLookAhead); 227 } 228 CurrCycleInstr = nullptr; 229 } 230 231 unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { 232 IsHazardRecognizerMode = true; 233 CurrCycleInstr = MI; 234 unsigned W = PreEmitNoopsCommon(MI); 235 fixHazards(MI); 236 CurrCycleInstr = nullptr; 237 return W; 238 } 239 240 unsigned GCNHazardRecognizer::PreEmitNoopsCommon(MachineInstr *MI) { 241 if (MI->isBundle()) 242 return 0; 243 244 int WaitStates = std::max(0, checkAnyInstHazards(MI)); 245 246 if (SIInstrInfo::isSMRD(*MI)) 247 return std::max(WaitStates, checkSMRDHazards(MI)); 248 249 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI)) 250 WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); 251 252 if (ST.hasNSAtoVMEMBug()) 253 WaitStates = std::max(WaitStates, checkNSAtoVMEMHazard(MI)); 254 255 WaitStates = std::max(WaitStates, checkFPAtomicToDenormModeHazard(MI)); 256 257 if (ST.hasNoDataDepHazard()) 258 return WaitStates; 259 260 if (SIInstrInfo::isVALU(*MI)) 261 WaitStates = std::max(WaitStates, checkVALUHazards(MI)); 262 263 if (SIInstrInfo::isDPP(*MI)) 264 WaitStates = std::max(WaitStates, checkDPPHazards(MI)); 265 266 if (isDivFMas(MI->getOpcode())) 267 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); 268 269 if (isRWLane(MI->getOpcode())) 270 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); 271 272 if (MI->isInlineAsm()) 273 return std::max(WaitStates, checkInlineAsmHazards(MI)); 274 275 if (isSGetReg(MI->getOpcode())) 276 return std::max(WaitStates, checkGetRegHazards(MI)); 277 278 if (isSSetReg(MI->getOpcode())) 279 return std::max(WaitStates, checkSetRegHazards(MI)); 280 281 if (isRFE(MI->getOpcode())) 282 return std::max(WaitStates, checkRFEHazards(MI)); 283 284 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) || 285 isSMovRel(MI->getOpcode()))) 286 return std::max(WaitStates, checkReadM0Hazards(MI)); 287 288 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI)) 289 return std::max(WaitStates, checkReadM0Hazards(MI)); 290 291 if (SIInstrInfo::isMAI(*MI)) 292 return std::max(WaitStates, checkMAIHazards(MI)); 293 294 if (MI->mayLoadOrStore()) 295 return std::max(WaitStates, checkMAILdStHazards(MI)); 296 297 return WaitStates; 298 } 299 300 void GCNHazardRecognizer::EmitNoop() { 301 EmittedInstrs.push_front(nullptr); 302 } 303 304 void GCNHazardRecognizer::AdvanceCycle() { 305 // When the scheduler detects a stall, it will call AdvanceCycle() without 306 // emitting any instructions. 307 if (!CurrCycleInstr) 308 return; 309 310 // Do not track non-instructions which do not affect the wait states. 311 // If included, these instructions can lead to buffer overflow such that 312 // detectable hazards are missed. 313 if (CurrCycleInstr->isImplicitDef() || CurrCycleInstr->isDebugInstr() || 314 CurrCycleInstr->isKill()) 315 return; 316 317 if (CurrCycleInstr->isBundle()) { 318 processBundle(); 319 return; 320 } 321 322 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr); 323 324 // Keep track of emitted instructions 325 EmittedInstrs.push_front(CurrCycleInstr); 326 327 // Add a nullptr for each additional wait state after the first. Make sure 328 // not to add more than getMaxLookAhead() items to the list, since we 329 // truncate the list to that size right after this loop. 330 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); 331 i < e; ++i) { 332 EmittedInstrs.push_front(nullptr); 333 } 334 335 // getMaxLookahead() is the largest number of wait states we will ever need 336 // to insert, so there is no point in keeping track of more than that many 337 // wait states. 338 EmittedInstrs.resize(getMaxLookAhead()); 339 340 CurrCycleInstr = nullptr; 341 } 342 343 void GCNHazardRecognizer::RecedeCycle() { 344 llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); 345 } 346 347 //===----------------------------------------------------------------------===// 348 // Helper Functions 349 //===----------------------------------------------------------------------===// 350 351 typedef function_ref<bool(MachineInstr *, int WaitStates)> IsExpiredFn; 352 353 // Returns a minimum wait states since \p I walking all predecessors. 354 // Only scans until \p IsExpired does not return true. 355 // Can only be run in a hazard recognizer mode. 356 static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, 357 MachineBasicBlock *MBB, 358 MachineBasicBlock::reverse_instr_iterator I, 359 int WaitStates, 360 IsExpiredFn IsExpired, 361 DenseSet<const MachineBasicBlock *> &Visited) { 362 for (auto E = MBB->instr_rend(); I != E; ++I) { 363 // Don't add WaitStates for parent BUNDLE instructions. 364 if (I->isBundle()) 365 continue; 366 367 if (IsHazard(&*I)) 368 return WaitStates; 369 370 if (I->isInlineAsm() || I->isImplicitDef() || I->isDebugInstr()) 371 continue; 372 373 WaitStates += SIInstrInfo::getNumWaitStates(*I); 374 375 if (IsExpired(&*I, WaitStates)) 376 return std::numeric_limits<int>::max(); 377 } 378 379 int MinWaitStates = WaitStates; 380 bool Found = false; 381 for (MachineBasicBlock *Pred : MBB->predecessors()) { 382 if (!Visited.insert(Pred).second) 383 continue; 384 385 int W = getWaitStatesSince(IsHazard, Pred, Pred->instr_rbegin(), 386 WaitStates, IsExpired, Visited); 387 388 if (W == std::numeric_limits<int>::max()) 389 continue; 390 391 MinWaitStates = Found ? std::min(MinWaitStates, W) : W; 392 if (IsExpired(nullptr, MinWaitStates)) 393 return MinWaitStates; 394 395 Found = true; 396 } 397 398 if (Found) 399 return MinWaitStates; 400 401 return std::numeric_limits<int>::max(); 402 } 403 404 static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, 405 MachineInstr *MI, 406 IsExpiredFn IsExpired) { 407 DenseSet<const MachineBasicBlock *> Visited; 408 return getWaitStatesSince(IsHazard, MI->getParent(), 409 std::next(MI->getReverseIterator()), 410 0, IsExpired, Visited); 411 } 412 413 int GCNHazardRecognizer::getWaitStatesSince(IsHazardFn IsHazard, int Limit) { 414 if (IsHazardRecognizerMode) { 415 auto IsExpiredFn = [Limit] (MachineInstr *, int WaitStates) { 416 return WaitStates >= Limit; 417 }; 418 return ::getWaitStatesSince(IsHazard, CurrCycleInstr, IsExpiredFn); 419 } 420 421 int WaitStates = 0; 422 for (MachineInstr *MI : EmittedInstrs) { 423 if (MI) { 424 if (IsHazard(MI)) 425 return WaitStates; 426 427 if (MI->isInlineAsm()) 428 continue; 429 } 430 ++WaitStates; 431 432 if (WaitStates >= Limit) 433 break; 434 } 435 return std::numeric_limits<int>::max(); 436 } 437 438 int GCNHazardRecognizer::getWaitStatesSinceDef(unsigned Reg, 439 IsHazardFn IsHazardDef, 440 int Limit) { 441 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 442 443 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) { 444 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI); 445 }; 446 447 return getWaitStatesSince(IsHazardFn, Limit); 448 } 449 450 int GCNHazardRecognizer::getWaitStatesSinceSetReg(IsHazardFn IsHazard, 451 int Limit) { 452 auto IsHazardFn = [IsHazard] (MachineInstr *MI) { 453 return isSSetReg(MI->getOpcode()) && IsHazard(MI); 454 }; 455 456 return getWaitStatesSince(IsHazardFn, Limit); 457 } 458 459 //===----------------------------------------------------------------------===// 460 // No-op Hazard Detection 461 //===----------------------------------------------------------------------===// 462 463 static void addRegUnits(const SIRegisterInfo &TRI, 464 BitVector &BV, unsigned Reg) { 465 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) 466 BV.set(*RUI); 467 } 468 469 static void addRegsToSet(const SIRegisterInfo &TRI, 470 iterator_range<MachineInstr::const_mop_iterator> Ops, 471 BitVector &Set) { 472 for (const MachineOperand &Op : Ops) { 473 if (Op.isReg()) 474 addRegUnits(TRI, Set, Op.getReg()); 475 } 476 } 477 478 void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) { 479 // XXX: Do we need to worry about implicit operands 480 addRegsToSet(TRI, MI.defs(), ClauseDefs); 481 addRegsToSet(TRI, MI.uses(), ClauseUses); 482 } 483 484 static bool breaksSMEMSoftClause(MachineInstr *MI) { 485 return !SIInstrInfo::isSMRD(*MI); 486 } 487 488 static bool breaksVMEMSoftClause(MachineInstr *MI) { 489 return !SIInstrInfo::isVMEM(*MI) && !SIInstrInfo::isFLAT(*MI); 490 } 491 492 int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) { 493 // SMEM soft clause are only present on VI+, and only matter if xnack is 494 // enabled. 495 if (!ST.isXNACKEnabled()) 496 return 0; 497 498 bool IsSMRD = TII.isSMRD(*MEM); 499 500 resetClause(); 501 502 // A soft-clause is any group of consecutive SMEM instructions. The 503 // instructions in this group may return out of order and/or may be 504 // replayed (i.e. the same instruction issued more than once). 505 // 506 // In order to handle these situations correctly we need to make sure that 507 // when a clause has more than one instruction, no instruction in the clause 508 // writes to a register that is read by another instruction in the clause 509 // (including itself). If we encounter this situaion, we need to break the 510 // clause by inserting a non SMEM instruction. 511 512 for (MachineInstr *MI : EmittedInstrs) { 513 // When we hit a non-SMEM instruction then we have passed the start of the 514 // clause and we can stop. 515 if (!MI) 516 break; 517 518 if (IsSMRD ? breaksSMEMSoftClause(MI) : breaksVMEMSoftClause(MI)) 519 break; 520 521 addClauseInst(*MI); 522 } 523 524 if (ClauseDefs.none()) 525 return 0; 526 527 // We need to make sure not to put loads and stores in the same clause if they 528 // use the same address. For now, just start a new clause whenever we see a 529 // store. 530 if (MEM->mayStore()) 531 return 1; 532 533 addClauseInst(*MEM); 534 535 // If the set of defs and uses intersect then we cannot add this instruction 536 // to the clause, so we have a hazard. 537 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0; 538 } 539 540 int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { 541 int WaitStatesNeeded = 0; 542 543 WaitStatesNeeded = checkSoftClauseHazards(SMRD); 544 545 // This SMRD hazard only affects SI. 546 if (!ST.hasSMRDReadVALUDefHazard()) 547 return WaitStatesNeeded; 548 549 // A read of an SGPR by SMRD instruction requires 4 wait states when the 550 // SGPR was written by a VALU instruction. 551 int SmrdSgprWaitStates = 4; 552 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; 553 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); }; 554 555 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD); 556 557 for (const MachineOperand &Use : SMRD->uses()) { 558 if (!Use.isReg()) 559 continue; 560 int WaitStatesNeededForUse = 561 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 562 SmrdSgprWaitStates); 563 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 564 565 // This fixes what appears to be undocumented hardware behavior in SI where 566 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor 567 // needs some number of nops in between. We don't know how many we need, but 568 // let's use 4. This wasn't discovered before probably because the only 569 // case when this happens is when we expand a 64-bit pointer into a full 570 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was 571 // probably never encountered in the closed-source land. 572 if (IsBufferSMRD) { 573 int WaitStatesNeededForUse = 574 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), 575 IsBufferHazardDefFn, 576 SmrdSgprWaitStates); 577 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 578 } 579 } 580 581 return WaitStatesNeeded; 582 } 583 584 int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { 585 if (!ST.hasVMEMReadSGPRVALUDefHazard()) 586 return 0; 587 588 int WaitStatesNeeded = checkSoftClauseHazards(VMEM); 589 590 // A read of an SGPR by a VMEM instruction requires 5 wait states when the 591 // SGPR was written by a VALU Instruction. 592 const int VmemSgprWaitStates = 5; 593 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; 594 for (const MachineOperand &Use : VMEM->uses()) { 595 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 596 continue; 597 598 int WaitStatesNeededForUse = 599 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 600 VmemSgprWaitStates); 601 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 602 } 603 return WaitStatesNeeded; 604 } 605 606 int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { 607 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 608 const SIInstrInfo *TII = ST.getInstrInfo(); 609 610 // Check for DPP VGPR read after VALU VGPR write and EXEC write. 611 int DppVgprWaitStates = 2; 612 int DppExecWaitStates = 5; 613 int WaitStatesNeeded = 0; 614 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; 615 616 for (const MachineOperand &Use : DPP->uses()) { 617 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 618 continue; 619 int WaitStatesNeededForUse = 620 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg(), 621 [](MachineInstr *) { return true; }, 622 DppVgprWaitStates); 623 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 624 } 625 626 WaitStatesNeeded = std::max( 627 WaitStatesNeeded, 628 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn, 629 DppExecWaitStates)); 630 631 return WaitStatesNeeded; 632 } 633 634 int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { 635 const SIInstrInfo *TII = ST.getInstrInfo(); 636 637 // v_div_fmas requires 4 wait states after a write to vcc from a VALU 638 // instruction. 639 const int DivFMasWaitStates = 4; 640 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; 641 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn, 642 DivFMasWaitStates); 643 644 return DivFMasWaitStates - WaitStatesNeeded; 645 } 646 647 int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { 648 const SIInstrInfo *TII = ST.getInstrInfo(); 649 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); 650 651 const int GetRegWaitStates = 2; 652 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) { 653 return GetRegHWReg == getHWReg(TII, *MI); 654 }; 655 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates); 656 657 return GetRegWaitStates - WaitStatesNeeded; 658 } 659 660 int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { 661 const SIInstrInfo *TII = ST.getInstrInfo(); 662 unsigned HWReg = getHWReg(TII, *SetRegInstr); 663 664 const int SetRegWaitStates = ST.getSetRegWaitStates(); 665 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) { 666 return HWReg == getHWReg(TII, *MI); 667 }; 668 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates); 669 return SetRegWaitStates - WaitStatesNeeded; 670 } 671 672 int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { 673 if (!MI.mayStore()) 674 return -1; 675 676 const SIInstrInfo *TII = ST.getInstrInfo(); 677 unsigned Opcode = MI.getOpcode(); 678 const MCInstrDesc &Desc = MI.getDesc(); 679 680 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 681 int VDataRCID = -1; 682 if (VDataIdx != -1) 683 VDataRCID = Desc.OpInfo[VDataIdx].RegClass; 684 685 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { 686 // There is no hazard if the instruction does not use vector regs 687 // (like wbinvl1) 688 if (VDataIdx == -1) 689 return -1; 690 // For MUBUF/MTBUF instructions this hazard only exists if the 691 // instruction is not using a register in the soffset field. 692 const MachineOperand *SOffset = 693 TII->getNamedOperand(MI, AMDGPU::OpName::soffset); 694 // If we have no soffset operand, then assume this field has been 695 // hardcoded to zero. 696 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && 697 (!SOffset || !SOffset->isReg())) 698 return VDataIdx; 699 } 700 701 // MIMG instructions create a hazard if they don't use a 256-bit T# and 702 // the store size is greater than 8 bytes and they have more than two bits 703 // of their dmask set. 704 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. 705 if (TII->isMIMG(MI)) { 706 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 707 assert(SRsrcIdx != -1 && 708 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); 709 (void)SRsrcIdx; 710 } 711 712 if (TII->isFLAT(MI)) { 713 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 714 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) 715 return DataIdx; 716 } 717 718 return -1; 719 } 720 721 int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def, 722 const MachineRegisterInfo &MRI) { 723 // Helper to check for the hazard where VMEM instructions that store more than 724 // 8 bytes can have there store data over written by the next instruction. 725 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 726 727 const int VALUWaitStates = 1; 728 int WaitStatesNeeded = 0; 729 730 if (!TRI->isVGPR(MRI, Def.getReg())) 731 return WaitStatesNeeded; 732 Register Reg = Def.getReg(); 733 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) { 734 int DataIdx = createsVALUHazard(*MI); 735 return DataIdx >= 0 && 736 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg); 737 }; 738 int WaitStatesNeededForDef = 739 VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates); 740 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 741 742 return WaitStatesNeeded; 743 } 744 745 int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { 746 // This checks for the hazard where VMEM instructions that store more than 747 // 8 bytes can have there store data over written by the next instruction. 748 if (!ST.has12DWordStoreHazard()) 749 return 0; 750 751 const MachineRegisterInfo &MRI = MF.getRegInfo(); 752 int WaitStatesNeeded = 0; 753 754 for (const MachineOperand &Def : VALU->defs()) { 755 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI)); 756 } 757 758 return WaitStatesNeeded; 759 } 760 761 int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) { 762 // This checks for hazards associated with inline asm statements. 763 // Since inline asms can contain just about anything, we use this 764 // to call/leverage other check*Hazard routines. Note that 765 // this function doesn't attempt to address all possible inline asm 766 // hazards (good luck), but is a collection of what has been 767 // problematic thus far. 768 769 // see checkVALUHazards() 770 if (!ST.has12DWordStoreHazard()) 771 return 0; 772 773 const MachineRegisterInfo &MRI = MF.getRegInfo(); 774 int WaitStatesNeeded = 0; 775 776 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands(); 777 I != E; ++I) { 778 const MachineOperand &Op = IA->getOperand(I); 779 if (Op.isReg() && Op.isDef()) { 780 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI)); 781 } 782 } 783 784 return WaitStatesNeeded; 785 } 786 787 int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { 788 const SIInstrInfo *TII = ST.getInstrInfo(); 789 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 790 const MachineRegisterInfo &MRI = MF.getRegInfo(); 791 792 const MachineOperand *LaneSelectOp = 793 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); 794 795 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) 796 return 0; 797 798 Register LaneSelectReg = LaneSelectOp->getReg(); 799 auto IsHazardFn = [TII] (MachineInstr *MI) { 800 return TII->isVALU(*MI); 801 }; 802 803 const int RWLaneWaitStates = 4; 804 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn, 805 RWLaneWaitStates); 806 return RWLaneWaitStates - WaitStatesSince; 807 } 808 809 int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { 810 if (!ST.hasRFEHazards()) 811 return 0; 812 813 const SIInstrInfo *TII = ST.getInstrInfo(); 814 815 const int RFEWaitStates = 1; 816 817 auto IsHazardFn = [TII] (MachineInstr *MI) { 818 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS; 819 }; 820 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates); 821 return RFEWaitStates - WaitStatesNeeded; 822 } 823 824 int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) { 825 if (MI->isDebugInstr()) 826 return 0; 827 828 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 829 if (!ST.hasSMovFedHazard()) 830 return 0; 831 832 // Check for any instruction reading an SGPR after a write from 833 // s_mov_fed_b32. 834 int MovFedWaitStates = 1; 835 int WaitStatesNeeded = 0; 836 837 for (const MachineOperand &Use : MI->uses()) { 838 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 839 continue; 840 auto IsHazardFn = [] (MachineInstr *MI) { 841 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32; 842 }; 843 int WaitStatesNeededForUse = 844 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn, 845 MovFedWaitStates); 846 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 847 } 848 849 return WaitStatesNeeded; 850 } 851 852 int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { 853 const SIInstrInfo *TII = ST.getInstrInfo(); 854 const int SMovRelWaitStates = 1; 855 auto IsHazardFn = [TII] (MachineInstr *MI) { 856 return TII->isSALU(*MI); 857 }; 858 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn, 859 SMovRelWaitStates); 860 } 861 862 void GCNHazardRecognizer::fixHazards(MachineInstr *MI) { 863 fixVMEMtoScalarWriteHazards(MI); 864 fixVcmpxPermlaneHazards(MI); 865 fixSMEMtoVectorWriteHazards(MI); 866 fixVcmpxExecWARHazard(MI); 867 fixLdsBranchVmemWARHazard(MI); 868 } 869 870 bool GCNHazardRecognizer::fixVcmpxPermlaneHazards(MachineInstr *MI) { 871 if (!ST.hasVcmpxPermlaneHazard() || !isPermlane(*MI)) 872 return false; 873 874 const SIInstrInfo *TII = ST.getInstrInfo(); 875 auto IsHazardFn = [TII] (MachineInstr *MI) { 876 return TII->isVOPC(*MI); 877 }; 878 879 auto IsExpiredFn = [] (MachineInstr *MI, int) { 880 if (!MI) 881 return false; 882 unsigned Opc = MI->getOpcode(); 883 return SIInstrInfo::isVALU(*MI) && 884 Opc != AMDGPU::V_NOP_e32 && 885 Opc != AMDGPU::V_NOP_e64 && 886 Opc != AMDGPU::V_NOP_sdwa; 887 }; 888 889 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 890 std::numeric_limits<int>::max()) 891 return false; 892 893 // V_NOP will be discarded by SQ. 894 // Use V_MOB_B32 v?, v?. Register must be alive so use src0 of V_PERMLANE* 895 // which is always a VGPR and available. 896 auto *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0); 897 Register Reg = Src0->getReg(); 898 bool IsUndef = Src0->isUndef(); 899 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 900 TII->get(AMDGPU::V_MOV_B32_e32)) 901 .addReg(Reg, RegState::Define | (IsUndef ? RegState::Dead : 0)) 902 .addReg(Reg, IsUndef ? RegState::Undef : RegState::Kill); 903 904 return true; 905 } 906 907 bool GCNHazardRecognizer::fixVMEMtoScalarWriteHazards(MachineInstr *MI) { 908 if (!ST.hasVMEMtoScalarWriteHazard()) 909 return false; 910 911 if (!SIInstrInfo::isSALU(*MI) && !SIInstrInfo::isSMRD(*MI)) 912 return false; 913 914 if (MI->getNumDefs() == 0) 915 return false; 916 917 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 918 919 auto IsHazardFn = [TRI, MI] (MachineInstr *I) { 920 if (!SIInstrInfo::isVMEM(*I) && !SIInstrInfo::isDS(*I) && 921 !SIInstrInfo::isFLAT(*I)) 922 return false; 923 924 for (const MachineOperand &Def : MI->defs()) { 925 MachineOperand *Op = I->findRegisterUseOperand(Def.getReg(), false, TRI); 926 if (!Op) 927 continue; 928 return true; 929 } 930 return false; 931 }; 932 933 auto IsExpiredFn = [](MachineInstr *MI, int) { 934 return MI && (SIInstrInfo::isVALU(*MI) || 935 (MI->getOpcode() == AMDGPU::S_WAITCNT && 936 !MI->getOperand(0).getImm()) || 937 (MI->getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 938 MI->getOperand(0).getImm() == 0xffe3)); 939 }; 940 941 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 942 std::numeric_limits<int>::max()) 943 return false; 944 945 const SIInstrInfo *TII = ST.getInstrInfo(); 946 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 947 TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 948 .addImm(0xffe3); 949 return true; 950 } 951 952 bool GCNHazardRecognizer::fixSMEMtoVectorWriteHazards(MachineInstr *MI) { 953 if (!ST.hasSMEMtoVectorWriteHazard()) 954 return false; 955 956 if (!SIInstrInfo::isVALU(*MI)) 957 return false; 958 959 unsigned SDSTName; 960 switch (MI->getOpcode()) { 961 case AMDGPU::V_READLANE_B32: 962 case AMDGPU::V_READLANE_B32_gfx10: 963 case AMDGPU::V_READFIRSTLANE_B32: 964 SDSTName = AMDGPU::OpName::vdst; 965 break; 966 default: 967 SDSTName = AMDGPU::OpName::sdst; 968 break; 969 } 970 971 const SIInstrInfo *TII = ST.getInstrInfo(); 972 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 973 const AMDGPU::IsaVersion IV = AMDGPU::getIsaVersion(ST.getCPU()); 974 const MachineOperand *SDST = TII->getNamedOperand(*MI, SDSTName); 975 if (!SDST) { 976 for (const auto &MO : MI->implicit_operands()) { 977 if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg()))) { 978 SDST = &MO; 979 break; 980 } 981 } 982 } 983 984 if (!SDST) 985 return false; 986 987 const Register SDSTReg = SDST->getReg(); 988 auto IsHazardFn = [SDSTReg, TRI] (MachineInstr *I) { 989 return SIInstrInfo::isSMRD(*I) && I->readsRegister(SDSTReg, TRI); 990 }; 991 992 auto IsExpiredFn = [TII, IV] (MachineInstr *MI, int) { 993 if (MI) { 994 if (TII->isSALU(*MI)) { 995 switch (MI->getOpcode()) { 996 case AMDGPU::S_SETVSKIP: 997 case AMDGPU::S_VERSION: 998 case AMDGPU::S_WAITCNT_VSCNT: 999 case AMDGPU::S_WAITCNT_VMCNT: 1000 case AMDGPU::S_WAITCNT_EXPCNT: 1001 // These instructions cannot not mitigate the hazard. 1002 return false; 1003 case AMDGPU::S_WAITCNT_LGKMCNT: 1004 // Reducing lgkmcnt count to 0 always mitigates the hazard. 1005 return (MI->getOperand(1).getImm() == 0) && 1006 (MI->getOperand(0).getReg() == AMDGPU::SGPR_NULL); 1007 case AMDGPU::S_WAITCNT: { 1008 const int64_t Imm = MI->getOperand(0).getImm(); 1009 AMDGPU::Waitcnt Decoded = AMDGPU::decodeWaitcnt(IV, Imm); 1010 return (Decoded.LgkmCnt == 0); 1011 } 1012 default: 1013 // SOPP instructions cannot mitigate the hazard. 1014 if (TII->isSOPP(*MI)) 1015 return false; 1016 // At this point the SALU can be assumed to mitigate the hazard 1017 // because either: 1018 // (a) it is independent of the at risk SMEM (breaking chain), 1019 // or 1020 // (b) it is dependent on the SMEM, in which case an appropriate 1021 // s_waitcnt lgkmcnt _must_ exist between it and the at risk 1022 // SMEM instruction. 1023 return true; 1024 } 1025 } 1026 } 1027 return false; 1028 }; 1029 1030 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1031 std::numeric_limits<int>::max()) 1032 return false; 1033 1034 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1035 TII->get(AMDGPU::S_MOV_B32), AMDGPU::SGPR_NULL) 1036 .addImm(0); 1037 return true; 1038 } 1039 1040 bool GCNHazardRecognizer::fixVcmpxExecWARHazard(MachineInstr *MI) { 1041 if (!ST.hasVcmpxExecWARHazard() || !SIInstrInfo::isVALU(*MI)) 1042 return false; 1043 1044 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1045 if (!MI->modifiesRegister(AMDGPU::EXEC, TRI)) 1046 return false; 1047 1048 auto IsHazardFn = [TRI] (MachineInstr *I) { 1049 if (SIInstrInfo::isVALU(*I)) 1050 return false; 1051 return I->readsRegister(AMDGPU::EXEC, TRI); 1052 }; 1053 1054 const SIInstrInfo *TII = ST.getInstrInfo(); 1055 auto IsExpiredFn = [TII, TRI] (MachineInstr *MI, int) { 1056 if (!MI) 1057 return false; 1058 if (SIInstrInfo::isVALU(*MI)) { 1059 if (TII->getNamedOperand(*MI, AMDGPU::OpName::sdst)) 1060 return true; 1061 for (auto MO : MI->implicit_operands()) 1062 if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg()))) 1063 return true; 1064 } 1065 if (MI->getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1066 (MI->getOperand(0).getImm() & 0xfffe) == 0xfffe) 1067 return true; 1068 return false; 1069 }; 1070 1071 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1072 std::numeric_limits<int>::max()) 1073 return false; 1074 1075 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1076 TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 1077 .addImm(0xfffe); 1078 return true; 1079 } 1080 1081 bool GCNHazardRecognizer::fixLdsBranchVmemWARHazard(MachineInstr *MI) { 1082 if (!ST.hasLdsBranchVmemWARHazard()) 1083 return false; 1084 1085 auto IsHazardInst = [] (const MachineInstr *MI) { 1086 if (SIInstrInfo::isDS(*MI)) 1087 return 1; 1088 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isSegmentSpecificFLAT(*MI)) 1089 return 2; 1090 return 0; 1091 }; 1092 1093 auto InstType = IsHazardInst(MI); 1094 if (!InstType) 1095 return false; 1096 1097 auto IsExpiredFn = [&IsHazardInst] (MachineInstr *I, int) { 1098 return I && (IsHazardInst(I) || 1099 (I->getOpcode() == AMDGPU::S_WAITCNT_VSCNT && 1100 I->getOperand(0).getReg() == AMDGPU::SGPR_NULL && 1101 !I->getOperand(1).getImm())); 1102 }; 1103 1104 auto IsHazardFn = [InstType, &IsHazardInst] (MachineInstr *I) { 1105 if (!I->isBranch()) 1106 return false; 1107 1108 auto IsHazardFn = [InstType, IsHazardInst] (MachineInstr *I) { 1109 auto InstType2 = IsHazardInst(I); 1110 return InstType2 && InstType != InstType2; 1111 }; 1112 1113 auto IsExpiredFn = [InstType, &IsHazardInst] (MachineInstr *I, int) { 1114 if (!I) 1115 return false; 1116 1117 auto InstType2 = IsHazardInst(I); 1118 if (InstType == InstType2) 1119 return true; 1120 1121 return I->getOpcode() == AMDGPU::S_WAITCNT_VSCNT && 1122 I->getOperand(0).getReg() == AMDGPU::SGPR_NULL && 1123 !I->getOperand(1).getImm(); 1124 }; 1125 1126 return ::getWaitStatesSince(IsHazardFn, I, IsExpiredFn) != 1127 std::numeric_limits<int>::max(); 1128 }; 1129 1130 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1131 std::numeric_limits<int>::max()) 1132 return false; 1133 1134 const SIInstrInfo *TII = ST.getInstrInfo(); 1135 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1136 TII->get(AMDGPU::S_WAITCNT_VSCNT)) 1137 .addReg(AMDGPU::SGPR_NULL, RegState::Undef) 1138 .addImm(0); 1139 1140 return true; 1141 } 1142 1143 int GCNHazardRecognizer::checkNSAtoVMEMHazard(MachineInstr *MI) { 1144 int NSAtoVMEMWaitStates = 1; 1145 1146 if (!ST.hasNSAtoVMEMBug()) 1147 return 0; 1148 1149 if (!SIInstrInfo::isMUBUF(*MI) && !SIInstrInfo::isMTBUF(*MI)) 1150 return 0; 1151 1152 const SIInstrInfo *TII = ST.getInstrInfo(); 1153 const auto *Offset = TII->getNamedOperand(*MI, AMDGPU::OpName::offset); 1154 if (!Offset || (Offset->getImm() & 6) == 0) 1155 return 0; 1156 1157 auto IsHazardFn = [TII] (MachineInstr *I) { 1158 if (!SIInstrInfo::isMIMG(*I)) 1159 return false; 1160 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(I->getOpcode()); 1161 return Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA && 1162 TII->getInstSizeInBytes(*I) >= 16; 1163 }; 1164 1165 return NSAtoVMEMWaitStates - getWaitStatesSince(IsHazardFn, 1); 1166 } 1167 1168 int GCNHazardRecognizer::checkFPAtomicToDenormModeHazard(MachineInstr *MI) { 1169 int FPAtomicToDenormModeWaitStates = 3; 1170 1171 if (MI->getOpcode() != AMDGPU::S_DENORM_MODE) 1172 return 0; 1173 1174 auto IsHazardFn = [] (MachineInstr *I) { 1175 if (!SIInstrInfo::isVMEM(*I) && !SIInstrInfo::isFLAT(*I)) 1176 return false; 1177 return SIInstrInfo::isFPAtomic(*I); 1178 }; 1179 1180 auto IsExpiredFn = [] (MachineInstr *MI, int WaitStates) { 1181 if (WaitStates >= 3 || SIInstrInfo::isVALU(*MI)) 1182 return true; 1183 1184 switch (MI->getOpcode()) { 1185 case AMDGPU::S_WAITCNT: 1186 case AMDGPU::S_WAITCNT_VSCNT: 1187 case AMDGPU::S_WAITCNT_VMCNT: 1188 case AMDGPU::S_WAITCNT_EXPCNT: 1189 case AMDGPU::S_WAITCNT_LGKMCNT: 1190 case AMDGPU::S_WAITCNT_IDLE: 1191 return true; 1192 default: 1193 break; 1194 } 1195 1196 return false; 1197 }; 1198 1199 1200 return FPAtomicToDenormModeWaitStates - 1201 ::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn); 1202 } 1203 1204 int GCNHazardRecognizer::checkMAIHazards(MachineInstr *MI) { 1205 assert(SIInstrInfo::isMAI(*MI)); 1206 1207 int WaitStatesNeeded = 0; 1208 unsigned Opc = MI->getOpcode(); 1209 1210 auto IsVALUFn = [] (MachineInstr *MI) { 1211 return SIInstrInfo::isVALU(*MI); 1212 }; 1213 1214 if (Opc != AMDGPU::V_ACCVGPR_READ_B32) { // MFMA or v_accvgpr_write 1215 const int LegacyVALUWritesVGPRWaitStates = 2; 1216 const int VALUWritesExecWaitStates = 4; 1217 const int MaxWaitStates = 4; 1218 1219 int WaitStatesNeededForUse = VALUWritesExecWaitStates - 1220 getWaitStatesSinceDef(AMDGPU::EXEC, IsVALUFn, MaxWaitStates); 1221 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1222 1223 if (WaitStatesNeeded < MaxWaitStates) { 1224 for (const MachineOperand &Use : MI->explicit_uses()) { 1225 const int MaxWaitStates = 2; 1226 1227 if (!Use.isReg() || !TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 1228 continue; 1229 1230 int WaitStatesNeededForUse = LegacyVALUWritesVGPRWaitStates - 1231 getWaitStatesSinceDef(Use.getReg(), IsVALUFn, MaxWaitStates); 1232 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1233 1234 if (WaitStatesNeeded == MaxWaitStates) 1235 break; 1236 } 1237 } 1238 } 1239 1240 auto IsMFMAFn = [] (MachineInstr *MI) { 1241 return SIInstrInfo::isMAI(*MI) && 1242 MI->getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32 && 1243 MI->getOpcode() != AMDGPU::V_ACCVGPR_READ_B32; 1244 }; 1245 1246 for (const MachineOperand &Op : MI->explicit_operands()) { 1247 if (!Op.isReg() || !TRI.isAGPR(MF.getRegInfo(), Op.getReg())) 1248 continue; 1249 1250 if (Op.isDef() && Opc != AMDGPU::V_ACCVGPR_WRITE_B32) 1251 continue; 1252 1253 const int MFMAWritesAGPROverlappedSrcABWaitStates = 4; 1254 const int MFMAWritesAGPROverlappedSrcCWaitStates = 2; 1255 const int MFMA4x4WritesAGPRAccVgprReadWaitStates = 4; 1256 const int MFMA16x16WritesAGPRAccVgprReadWaitStates = 10; 1257 const int MFMA32x32WritesAGPRAccVgprReadWaitStates = 18; 1258 const int MFMA4x4WritesAGPRAccVgprWriteWaitStates = 1; 1259 const int MFMA16x16WritesAGPRAccVgprWriteWaitStates = 7; 1260 const int MFMA32x32WritesAGPRAccVgprWriteWaitStates = 15; 1261 const int MaxWaitStates = 18; 1262 Register Reg = Op.getReg(); 1263 unsigned HazardDefLatency = 0; 1264 1265 auto IsOverlappedMFMAFn = [Reg, &IsMFMAFn, &HazardDefLatency, this] 1266 (MachineInstr *MI) { 1267 if (!IsMFMAFn(MI)) 1268 return false; 1269 Register DstReg = MI->getOperand(0).getReg(); 1270 if (DstReg == Reg) 1271 return false; 1272 HazardDefLatency = std::max(HazardDefLatency, 1273 TSchedModel.computeInstrLatency(MI)); 1274 return TRI.regsOverlap(DstReg, Reg); 1275 }; 1276 1277 int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsOverlappedMFMAFn, 1278 MaxWaitStates); 1279 int NeedWaitStates = MFMAWritesAGPROverlappedSrcABWaitStates; 1280 int SrcCIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 1281 int OpNo = MI->getOperandNo(&Op); 1282 if (OpNo == SrcCIdx) { 1283 NeedWaitStates = MFMAWritesAGPROverlappedSrcCWaitStates; 1284 } else if (Opc == AMDGPU::V_ACCVGPR_READ_B32) { 1285 switch (HazardDefLatency) { 1286 case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprReadWaitStates; 1287 break; 1288 case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprReadWaitStates; 1289 break; 1290 case 16: LLVM_FALLTHROUGH; 1291 default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprReadWaitStates; 1292 break; 1293 } 1294 } else if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32) { 1295 switch (HazardDefLatency) { 1296 case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprWriteWaitStates; 1297 break; 1298 case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprWriteWaitStates; 1299 break; 1300 case 16: LLVM_FALLTHROUGH; 1301 default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprWriteWaitStates; 1302 break; 1303 } 1304 } 1305 1306 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 1307 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1308 1309 if (WaitStatesNeeded == MaxWaitStates) 1310 return WaitStatesNeeded; // Early exit. 1311 1312 auto IsAccVgprWriteFn = [Reg, this] (MachineInstr *MI) { 1313 if (MI->getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32) 1314 return false; 1315 Register DstReg = MI->getOperand(0).getReg(); 1316 return TRI.regsOverlap(Reg, DstReg); 1317 }; 1318 1319 const int AccVGPRWriteMFMAReadSrcCWaitStates = 1; 1320 const int AccVGPRWriteMFMAReadSrcABWaitStates = 3; 1321 const int AccVGPRWriteAccVgprReadWaitStates = 3; 1322 NeedWaitStates = AccVGPRWriteMFMAReadSrcABWaitStates; 1323 if (OpNo == SrcCIdx) 1324 NeedWaitStates = AccVGPRWriteMFMAReadSrcCWaitStates; 1325 else if (Opc == AMDGPU::V_ACCVGPR_READ_B32) 1326 NeedWaitStates = AccVGPRWriteAccVgprReadWaitStates; 1327 1328 WaitStatesNeededForUse = NeedWaitStates - 1329 getWaitStatesSinceDef(Reg, IsAccVgprWriteFn, MaxWaitStates); 1330 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1331 1332 if (WaitStatesNeeded == MaxWaitStates) 1333 return WaitStatesNeeded; // Early exit. 1334 } 1335 1336 if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32) { 1337 const int MFMA4x4ReadSrcCAccVgprWriteWaitStates = 0; 1338 const int MFMA16x16ReadSrcCAccVgprWriteWaitStates = 5; 1339 const int MFMA32x32ReadSrcCAccVgprWriteWaitStates = 13; 1340 const int MaxWaitStates = 13; 1341 Register DstReg = MI->getOperand(0).getReg(); 1342 unsigned HazardDefLatency = 0; 1343 1344 auto IsSrcCMFMAFn = [DstReg, &IsMFMAFn, &HazardDefLatency, this] 1345 (MachineInstr *MI) { 1346 if (!IsMFMAFn(MI)) 1347 return false; 1348 Register Reg = TII.getNamedOperand(*MI, AMDGPU::OpName::src2)->getReg(); 1349 HazardDefLatency = std::max(HazardDefLatency, 1350 TSchedModel.computeInstrLatency(MI)); 1351 return TRI.regsOverlap(Reg, DstReg); 1352 }; 1353 1354 int WaitStatesSince = getWaitStatesSince(IsSrcCMFMAFn, MaxWaitStates); 1355 int NeedWaitStates; 1356 switch (HazardDefLatency) { 1357 case 2: NeedWaitStates = MFMA4x4ReadSrcCAccVgprWriteWaitStates; 1358 break; 1359 case 8: NeedWaitStates = MFMA16x16ReadSrcCAccVgprWriteWaitStates; 1360 break; 1361 case 16: LLVM_FALLTHROUGH; 1362 default: NeedWaitStates = MFMA32x32ReadSrcCAccVgprWriteWaitStates; 1363 break; 1364 } 1365 1366 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSince; 1367 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1368 } 1369 1370 return WaitStatesNeeded; 1371 } 1372 1373 int GCNHazardRecognizer::checkMAILdStHazards(MachineInstr *MI) { 1374 if (!ST.hasMAIInsts()) 1375 return 0; 1376 1377 int WaitStatesNeeded = 0; 1378 1379 auto IsAccVgprReadFn = [] (MachineInstr *MI) { 1380 return MI->getOpcode() == AMDGPU::V_ACCVGPR_READ_B32; 1381 }; 1382 1383 for (const MachineOperand &Op : MI->explicit_uses()) { 1384 if (!Op.isReg() || !TRI.isVGPR(MF.getRegInfo(), Op.getReg())) 1385 continue; 1386 1387 Register Reg = Op.getReg(); 1388 1389 const int AccVgprReadLdStWaitStates = 2; 1390 const int VALUWriteAccVgprReadLdStDepVALUWaitStates = 1; 1391 const int MaxWaitStates = 2; 1392 1393 int WaitStatesNeededForUse = AccVgprReadLdStWaitStates - 1394 getWaitStatesSinceDef(Reg, IsAccVgprReadFn, MaxWaitStates); 1395 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1396 1397 if (WaitStatesNeeded == MaxWaitStates) 1398 return WaitStatesNeeded; // Early exit. 1399 1400 auto IsVALUAccVgprReadCheckFn = [Reg, this] (MachineInstr *MI) { 1401 if (MI->getOpcode() != AMDGPU::V_ACCVGPR_READ_B32) 1402 return false; 1403 auto IsVALUFn = [] (MachineInstr *MI) { 1404 return SIInstrInfo::isVALU(*MI) && !SIInstrInfo::isMAI(*MI); 1405 }; 1406 return getWaitStatesSinceDef(Reg, IsVALUFn, 2 /*MaxWaitStates*/) < 1407 std::numeric_limits<int>::max(); 1408 }; 1409 1410 WaitStatesNeededForUse = VALUWriteAccVgprReadLdStDepVALUWaitStates - 1411 getWaitStatesSince(IsVALUAccVgprReadCheckFn, MaxWaitStates); 1412 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1413 } 1414 1415 return WaitStatesNeeded; 1416 } 1417