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 "GCNSubtarget.h" 15 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 16 #include "SIMachineFunctionInfo.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/CodeGen/ScheduleDAG.h" 19 #include "llvm/Support/TargetParser.h" 20 21 using namespace llvm; 22 23 namespace { 24 25 struct MFMAPaddingRatioParser : public cl::parser<unsigned> { 26 MFMAPaddingRatioParser(cl::Option &O) : cl::parser<unsigned>(O) {} 27 28 bool parse(cl::Option &O, StringRef ArgName, StringRef Arg, unsigned &Value) { 29 if (Arg.getAsInteger(0, Value)) 30 return O.error("'" + Arg + "' value invalid for uint argument!"); 31 32 if (Value > 100) 33 return O.error("'" + Arg + "' value must be in the range [0, 100]!"); 34 35 return false; 36 } 37 }; 38 39 } // end anonymous namespace 40 41 static cl::opt<unsigned, false, MFMAPaddingRatioParser> 42 MFMAPaddingRatio("amdgpu-mfma-padding-ratio", cl::init(0), cl::Hidden, 43 cl::desc("Fill a percentage of the latency between " 44 "neighboring MFMA with s_nops.")); 45 46 //===----------------------------------------------------------------------===// 47 // Hazard Recognizer Implementation 48 //===----------------------------------------------------------------------===// 49 50 static bool shouldRunLdsBranchVmemWARHazardFixup(const MachineFunction &MF, 51 const GCNSubtarget &ST); 52 53 GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) : 54 IsHazardRecognizerMode(false), 55 CurrCycleInstr(nullptr), 56 MF(MF), 57 ST(MF.getSubtarget<GCNSubtarget>()), 58 TII(*ST.getInstrInfo()), 59 TRI(TII.getRegisterInfo()), 60 ClauseUses(TRI.getNumRegUnits()), 61 ClauseDefs(TRI.getNumRegUnits()) { 62 MaxLookAhead = MF.getRegInfo().isPhysRegUsed(AMDGPU::AGPR0) ? 19 : 5; 63 TSchedModel.init(&ST); 64 RunLdsBranchVmemWARHazardFixup = shouldRunLdsBranchVmemWARHazardFixup(MF, ST); 65 } 66 67 void GCNHazardRecognizer::Reset() { 68 EmittedInstrs.clear(); 69 } 70 71 void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { 72 EmitInstruction(SU->getInstr()); 73 } 74 75 void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { 76 CurrCycleInstr = MI; 77 } 78 79 static bool isDivFMas(unsigned Opcode) { 80 return Opcode == AMDGPU::V_DIV_FMAS_F32_e64 || Opcode == AMDGPU::V_DIV_FMAS_F64_e64; 81 } 82 83 static bool isSGetReg(unsigned Opcode) { 84 return Opcode == AMDGPU::S_GETREG_B32; 85 } 86 87 static bool isSSetReg(unsigned Opcode) { 88 switch (Opcode) { 89 case AMDGPU::S_SETREG_B32: 90 case AMDGPU::S_SETREG_B32_mode: 91 case AMDGPU::S_SETREG_IMM32_B32: 92 case AMDGPU::S_SETREG_IMM32_B32_mode: 93 return true; 94 } 95 return false; 96 } 97 98 static bool isRWLane(unsigned Opcode) { 99 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; 100 } 101 102 static bool isRFE(unsigned Opcode) { 103 return Opcode == AMDGPU::S_RFE_B64; 104 } 105 106 static bool isSMovRel(unsigned Opcode) { 107 switch (Opcode) { 108 case AMDGPU::S_MOVRELS_B32: 109 case AMDGPU::S_MOVRELS_B64: 110 case AMDGPU::S_MOVRELD_B32: 111 case AMDGPU::S_MOVRELD_B64: 112 return true; 113 default: 114 return false; 115 } 116 } 117 118 static bool isDGEMM(unsigned Opcode) { 119 return AMDGPU::getMAIIsDGEMM(Opcode); 120 } 121 122 static bool isXDL(const GCNSubtarget &ST, const MachineInstr &MI) { 123 unsigned Opcode = MI.getOpcode(); 124 125 if (!SIInstrInfo::isMAI(MI) || 126 isDGEMM(Opcode) || 127 Opcode == AMDGPU::V_ACCVGPR_WRITE_B32_e64 || 128 Opcode == AMDGPU::V_ACCVGPR_READ_B32_e64) 129 return false; 130 131 if (!ST.hasGFX940Insts()) 132 return true; 133 134 return AMDGPU::getMAIIsGFX940XDL(Opcode); 135 } 136 137 static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII, 138 const MachineInstr &MI) { 139 if (TII.isAlwaysGDS(MI.getOpcode())) 140 return true; 141 142 switch (MI.getOpcode()) { 143 case AMDGPU::S_SENDMSG: 144 case AMDGPU::S_SENDMSGHALT: 145 case AMDGPU::S_TTRACEDATA: 146 return true; 147 // These DS opcodes don't support GDS. 148 case AMDGPU::DS_NOP: 149 case AMDGPU::DS_PERMUTE_B32: 150 case AMDGPU::DS_BPERMUTE_B32: 151 return false; 152 default: 153 if (TII.isDS(MI.getOpcode())) { 154 int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(), 155 AMDGPU::OpName::gds); 156 if (MI.getOperand(GDS).getImm()) 157 return true; 158 } 159 return false; 160 } 161 } 162 163 static bool isPermlane(const MachineInstr &MI) { 164 unsigned Opcode = MI.getOpcode(); 165 return Opcode == AMDGPU::V_PERMLANE16_B32_e64 || 166 Opcode == AMDGPU::V_PERMLANEX16_B32_e64; 167 } 168 169 static bool isLdsDma(const MachineInstr &MI) { 170 return SIInstrInfo::isVALU(MI) && 171 (SIInstrInfo::isMUBUF(MI) || SIInstrInfo::isFLAT(MI)); 172 } 173 174 static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { 175 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, 176 AMDGPU::OpName::simm16); 177 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; 178 } 179 180 ScheduleHazardRecognizer::HazardType 181 GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { 182 MachineInstr *MI = SU->getInstr(); 183 // If we are not in "HazardRecognizerMode" and therefore not being run from 184 // the scheduler, track possible stalls from hazards but don't insert noops. 185 auto HazardType = IsHazardRecognizerMode ? NoopHazard : Hazard; 186 187 if (MI->isBundle()) 188 return NoHazard; 189 190 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) 191 return HazardType; 192 193 if (ST.hasNSAtoVMEMBug() && checkNSAtoVMEMHazard(MI) > 0) 194 return HazardType; 195 196 if (checkFPAtomicToDenormModeHazard(MI) > 0) 197 return HazardType; 198 199 if (ST.hasNoDataDepHazard()) 200 return NoHazard; 201 202 // FIXME: Should flat be considered vmem? 203 if ((SIInstrInfo::isVMEM(*MI) || 204 SIInstrInfo::isFLAT(*MI)) 205 && checkVMEMHazards(MI) > 0) 206 return HazardType; 207 208 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) 209 return HazardType; 210 211 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) 212 return HazardType; 213 214 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) 215 return HazardType; 216 217 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) 218 return HazardType; 219 220 if ((SIInstrInfo::isVALU(*MI) || SIInstrInfo::isVMEM(*MI) || 221 SIInstrInfo::isFLAT(*MI) || SIInstrInfo::isDS(*MI) || 222 SIInstrInfo::isEXP(*MI)) && checkMAIVALUHazards(MI) > 0) 223 return HazardType; 224 225 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) 226 return HazardType; 227 228 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) 229 return HazardType; 230 231 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) 232 return HazardType; 233 234 if (((ST.hasReadM0MovRelInterpHazard() && 235 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()))) || 236 (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI)) || 237 (ST.hasReadM0LdsDmaHazard() && isLdsDma(*MI)) || 238 (ST.hasReadM0LdsDirectHazard() && 239 MI->readsRegister(AMDGPU::LDS_DIRECT))) && 240 checkReadM0Hazards(MI) > 0) 241 return HazardType; 242 243 if (SIInstrInfo::isMAI(*MI) && checkMAIHazards(MI) > 0) 244 return HazardType; 245 246 if ((SIInstrInfo::isVMEM(*MI) || 247 SIInstrInfo::isFLAT(*MI) || 248 SIInstrInfo::isDS(*MI)) && checkMAILdStHazards(MI) > 0) 249 return HazardType; 250 251 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0) 252 return HazardType; 253 254 return NoHazard; 255 } 256 257 static void insertNoopsInBundle(MachineInstr *MI, const SIInstrInfo &TII, 258 unsigned Quantity) { 259 while (Quantity > 0) { 260 unsigned Arg = std::min(Quantity, 8u); 261 Quantity -= Arg; 262 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII.get(AMDGPU::S_NOP)) 263 .addImm(Arg - 1); 264 } 265 } 266 267 unsigned 268 GCNHazardRecognizer::getMFMAPipelineWaitStates(const MachineInstr &MI) const { 269 const MCSchedClassDesc *SC = TSchedModel.resolveSchedClass(&MI); 270 assert(TSchedModel.getWriteProcResBegin(SC) != 271 TSchedModel.getWriteProcResEnd(SC)); 272 return TSchedModel.getWriteProcResBegin(SC)->Cycles; 273 } 274 275 void GCNHazardRecognizer::processBundle() { 276 MachineBasicBlock::instr_iterator MI = std::next(CurrCycleInstr->getIterator()); 277 MachineBasicBlock::instr_iterator E = CurrCycleInstr->getParent()->instr_end(); 278 // Check bundled MachineInstr's for hazards. 279 for (; MI != E && MI->isInsideBundle(); ++MI) { 280 CurrCycleInstr = &*MI; 281 unsigned WaitStates = PreEmitNoopsCommon(CurrCycleInstr); 282 283 if (IsHazardRecognizerMode) { 284 fixHazards(CurrCycleInstr); 285 286 insertNoopsInBundle(CurrCycleInstr, TII, WaitStates); 287 } 288 289 // It’s unnecessary to track more than MaxLookAhead instructions. Since we 290 // include the bundled MI directly after, only add a maximum of 291 // (MaxLookAhead - 1) noops to EmittedInstrs. 292 for (unsigned i = 0, e = std::min(WaitStates, MaxLookAhead - 1); i < e; ++i) 293 EmittedInstrs.push_front(nullptr); 294 295 EmittedInstrs.push_front(CurrCycleInstr); 296 EmittedInstrs.resize(MaxLookAhead); 297 } 298 CurrCycleInstr = nullptr; 299 } 300 301 unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { 302 IsHazardRecognizerMode = true; 303 CurrCycleInstr = MI; 304 unsigned W = PreEmitNoopsCommon(MI); 305 fixHazards(MI); 306 CurrCycleInstr = nullptr; 307 return W; 308 } 309 310 unsigned GCNHazardRecognizer::PreEmitNoopsCommon(MachineInstr *MI) { 311 if (MI->isBundle()) 312 return 0; 313 314 int WaitStates = 0; 315 316 if (SIInstrInfo::isSMRD(*MI)) 317 return std::max(WaitStates, checkSMRDHazards(MI)); 318 319 if (ST.hasNSAtoVMEMBug()) 320 WaitStates = std::max(WaitStates, checkNSAtoVMEMHazard(MI)); 321 322 WaitStates = std::max(WaitStates, checkFPAtomicToDenormModeHazard(MI)); 323 324 if (ST.hasNoDataDepHazard()) 325 return WaitStates; 326 327 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI)) 328 WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); 329 330 if (SIInstrInfo::isVALU(*MI)) 331 WaitStates = std::max(WaitStates, checkVALUHazards(MI)); 332 333 if (SIInstrInfo::isDPP(*MI)) 334 WaitStates = std::max(WaitStates, checkDPPHazards(MI)); 335 336 if (isDivFMas(MI->getOpcode())) 337 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); 338 339 if (isRWLane(MI->getOpcode())) 340 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); 341 342 if ((SIInstrInfo::isVALU(*MI) || SIInstrInfo::isVMEM(*MI) || 343 SIInstrInfo::isFLAT(*MI) || SIInstrInfo::isDS(*MI) || 344 SIInstrInfo::isEXP(*MI)) && checkMAIVALUHazards(MI) > 0) 345 WaitStates = std::max(WaitStates, checkMAIVALUHazards(MI)); 346 347 if (MI->isInlineAsm()) 348 return std::max(WaitStates, checkInlineAsmHazards(MI)); 349 350 if (isSGetReg(MI->getOpcode())) 351 return std::max(WaitStates, checkGetRegHazards(MI)); 352 353 if (isSSetReg(MI->getOpcode())) 354 return std::max(WaitStates, checkSetRegHazards(MI)); 355 356 if (isRFE(MI->getOpcode())) 357 return std::max(WaitStates, checkRFEHazards(MI)); 358 359 if ((ST.hasReadM0MovRelInterpHazard() && 360 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()))) || 361 (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI)) || 362 (ST.hasReadM0LdsDmaHazard() && isLdsDma(*MI)) || 363 (ST.hasReadM0LdsDirectHazard() && MI->readsRegister(AMDGPU::LDS_DIRECT))) 364 return std::max(WaitStates, checkReadM0Hazards(MI)); 365 366 if (SIInstrInfo::isMAI(*MI)) 367 return std::max(WaitStates, checkMAIHazards(MI)); 368 369 if (SIInstrInfo::isVMEM(*MI) || 370 SIInstrInfo::isFLAT(*MI) || 371 SIInstrInfo::isDS(*MI)) 372 return std::max(WaitStates, checkMAILdStHazards(MI)); 373 374 return WaitStates; 375 } 376 377 void GCNHazardRecognizer::EmitNoop() { 378 EmittedInstrs.push_front(nullptr); 379 } 380 381 void GCNHazardRecognizer::AdvanceCycle() { 382 // When the scheduler detects a stall, it will call AdvanceCycle() without 383 // emitting any instructions. 384 if (!CurrCycleInstr) { 385 EmittedInstrs.push_front(nullptr); 386 return; 387 } 388 389 if (CurrCycleInstr->isBundle()) { 390 processBundle(); 391 return; 392 } 393 394 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr); 395 if (!NumWaitStates) { 396 CurrCycleInstr = nullptr; 397 return; 398 } 399 400 // Keep track of emitted instructions 401 EmittedInstrs.push_front(CurrCycleInstr); 402 403 // Add a nullptr for each additional wait state after the first. Make sure 404 // not to add more than getMaxLookAhead() items to the list, since we 405 // truncate the list to that size right after this loop. 406 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); 407 i < e; ++i) { 408 EmittedInstrs.push_front(nullptr); 409 } 410 411 // getMaxLookahead() is the largest number of wait states we will ever need 412 // to insert, so there is no point in keeping track of more than that many 413 // wait states. 414 EmittedInstrs.resize(getMaxLookAhead()); 415 416 CurrCycleInstr = nullptr; 417 } 418 419 void GCNHazardRecognizer::RecedeCycle() { 420 llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); 421 } 422 423 //===----------------------------------------------------------------------===// 424 // Helper Functions 425 //===----------------------------------------------------------------------===// 426 427 typedef enum { HazardFound, HazardExpired, NoHazardFound } HazardFnResult; 428 429 typedef function_ref<bool(const MachineInstr &, int WaitStates)> IsExpiredFn; 430 typedef function_ref<unsigned int(const MachineInstr &)> GetNumWaitStatesFn; 431 432 // Search for a hazard in a block and its predecessors. 433 template <typename StateT> 434 static bool 435 hasHazard(StateT State, 436 function_ref<HazardFnResult(StateT &, const MachineInstr &)> IsHazard, 437 function_ref<void(StateT &, const MachineInstr &)> UpdateState, 438 const MachineBasicBlock *MBB, 439 MachineBasicBlock::const_reverse_instr_iterator I, 440 DenseSet<const MachineBasicBlock *> &Visited) { 441 for (auto E = MBB->instr_rend(); I != E; ++I) { 442 // No need to look at parent BUNDLE instructions. 443 if (I->isBundle()) 444 continue; 445 446 switch (IsHazard(State, *I)) { 447 case HazardFound: 448 return true; 449 case HazardExpired: 450 return false; 451 default: 452 // Continue search 453 break; 454 } 455 456 if (I->isInlineAsm() || I->isMetaInstruction()) 457 continue; 458 459 UpdateState(State, *I); 460 } 461 462 for (MachineBasicBlock *Pred : MBB->predecessors()) { 463 if (!Visited.insert(Pred).second) 464 continue; 465 466 if (hasHazard(State, IsHazard, UpdateState, Pred, Pred->instr_rbegin(), 467 Visited)) 468 return true; 469 } 470 471 return false; 472 } 473 474 // Returns a minimum wait states since \p I walking all predecessors. 475 // Only scans until \p IsExpired does not return true. 476 // Can only be run in a hazard recognizer mode. 477 static int getWaitStatesSince( 478 GCNHazardRecognizer::IsHazardFn IsHazard, const MachineBasicBlock *MBB, 479 MachineBasicBlock::const_reverse_instr_iterator I, int WaitStates, 480 IsExpiredFn IsExpired, DenseSet<const MachineBasicBlock *> &Visited, 481 GetNumWaitStatesFn GetNumWaitStates = SIInstrInfo::getNumWaitStates) { 482 for (auto E = MBB->instr_rend(); I != E; ++I) { 483 // Don't add WaitStates for parent BUNDLE instructions. 484 if (I->isBundle()) 485 continue; 486 487 if (IsHazard(*I)) 488 return WaitStates; 489 490 if (I->isInlineAsm()) 491 continue; 492 493 WaitStates += GetNumWaitStates(*I); 494 495 if (IsExpired(*I, WaitStates)) 496 return std::numeric_limits<int>::max(); 497 } 498 499 int MinWaitStates = std::numeric_limits<int>::max(); 500 for (MachineBasicBlock *Pred : MBB->predecessors()) { 501 if (!Visited.insert(Pred).second) 502 continue; 503 504 int W = getWaitStatesSince(IsHazard, Pred, Pred->instr_rbegin(), WaitStates, 505 IsExpired, Visited, GetNumWaitStates); 506 507 MinWaitStates = std::min(MinWaitStates, W); 508 } 509 510 return MinWaitStates; 511 } 512 513 static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, 514 const MachineInstr *MI, IsExpiredFn IsExpired) { 515 DenseSet<const MachineBasicBlock *> Visited; 516 return getWaitStatesSince(IsHazard, MI->getParent(), 517 std::next(MI->getReverseIterator()), 518 0, IsExpired, Visited); 519 } 520 521 int GCNHazardRecognizer::getWaitStatesSince(IsHazardFn IsHazard, int Limit) { 522 if (IsHazardRecognizerMode) { 523 auto IsExpiredFn = [Limit](const MachineInstr &, int WaitStates) { 524 return WaitStates >= Limit; 525 }; 526 return ::getWaitStatesSince(IsHazard, CurrCycleInstr, IsExpiredFn); 527 } 528 529 int WaitStates = 0; 530 for (MachineInstr *MI : EmittedInstrs) { 531 if (MI) { 532 if (IsHazard(*MI)) 533 return WaitStates; 534 535 if (MI->isInlineAsm()) 536 continue; 537 } 538 ++WaitStates; 539 540 if (WaitStates >= Limit) 541 break; 542 } 543 return std::numeric_limits<int>::max(); 544 } 545 546 int GCNHazardRecognizer::getWaitStatesSinceDef(unsigned Reg, 547 IsHazardFn IsHazardDef, 548 int Limit) { 549 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 550 551 auto IsHazardFn = [IsHazardDef, TRI, Reg](const MachineInstr &MI) { 552 return IsHazardDef(MI) && MI.modifiesRegister(Reg, TRI); 553 }; 554 555 return getWaitStatesSince(IsHazardFn, Limit); 556 } 557 558 int GCNHazardRecognizer::getWaitStatesSinceSetReg(IsHazardFn IsHazard, 559 int Limit) { 560 auto IsHazardFn = [IsHazard](const MachineInstr &MI) { 561 return isSSetReg(MI.getOpcode()) && IsHazard(MI); 562 }; 563 564 return getWaitStatesSince(IsHazardFn, Limit); 565 } 566 567 //===----------------------------------------------------------------------===// 568 // No-op Hazard Detection 569 //===----------------------------------------------------------------------===// 570 571 static void addRegUnits(const SIRegisterInfo &TRI, BitVector &BV, 572 MCRegister Reg) { 573 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) 574 BV.set(*RUI); 575 } 576 577 static void addRegsToSet(const SIRegisterInfo &TRI, 578 iterator_range<MachineInstr::const_mop_iterator> Ops, 579 BitVector &Set) { 580 for (const MachineOperand &Op : Ops) { 581 if (Op.isReg()) 582 addRegUnits(TRI, Set, Op.getReg().asMCReg()); 583 } 584 } 585 586 void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) { 587 // XXX: Do we need to worry about implicit operands 588 addRegsToSet(TRI, MI.defs(), ClauseDefs); 589 addRegsToSet(TRI, MI.uses(), ClauseUses); 590 } 591 592 static bool breaksSMEMSoftClause(MachineInstr *MI) { 593 return !SIInstrInfo::isSMRD(*MI); 594 } 595 596 static bool breaksVMEMSoftClause(MachineInstr *MI) { 597 return !SIInstrInfo::isVMEM(*MI) && !SIInstrInfo::isFLAT(*MI); 598 } 599 600 int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) { 601 // SMEM soft clause are only present on VI+, and only matter if xnack is 602 // enabled. 603 if (!ST.isXNACKEnabled()) 604 return 0; 605 606 bool IsSMRD = TII.isSMRD(*MEM); 607 608 resetClause(); 609 610 // A soft-clause is any group of consecutive SMEM instructions. The 611 // instructions in this group may return out of order and/or may be 612 // replayed (i.e. the same instruction issued more than once). 613 // 614 // In order to handle these situations correctly we need to make sure that 615 // when a clause has more than one instruction, no instruction in the clause 616 // writes to a register that is read by another instruction in the clause 617 // (including itself). If we encounter this situation, we need to break the 618 // clause by inserting a non SMEM instruction. 619 620 for (MachineInstr *MI : EmittedInstrs) { 621 // When we hit a non-SMEM instruction then we have passed the start of the 622 // clause and we can stop. 623 if (!MI) 624 break; 625 626 if (IsSMRD ? breaksSMEMSoftClause(MI) : breaksVMEMSoftClause(MI)) 627 break; 628 629 addClauseInst(*MI); 630 } 631 632 if (ClauseDefs.none()) 633 return 0; 634 635 // We need to make sure not to put loads and stores in the same clause if they 636 // use the same address. For now, just start a new clause whenever we see a 637 // store. 638 if (MEM->mayStore()) 639 return 1; 640 641 addClauseInst(*MEM); 642 643 // If the set of defs and uses intersect then we cannot add this instruction 644 // to the clause, so we have a hazard. 645 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0; 646 } 647 648 int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { 649 int WaitStatesNeeded = 0; 650 651 WaitStatesNeeded = checkSoftClauseHazards(SMRD); 652 653 // This SMRD hazard only affects SI. 654 if (!ST.hasSMRDReadVALUDefHazard()) 655 return WaitStatesNeeded; 656 657 // A read of an SGPR by SMRD instruction requires 4 wait states when the 658 // SGPR was written by a VALU instruction. 659 int SmrdSgprWaitStates = 4; 660 auto IsHazardDefFn = [this](const MachineInstr &MI) { 661 return TII.isVALU(MI); 662 }; 663 auto IsBufferHazardDefFn = [this](const MachineInstr &MI) { 664 return TII.isSALU(MI); 665 }; 666 667 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD); 668 669 for (const MachineOperand &Use : SMRD->uses()) { 670 if (!Use.isReg()) 671 continue; 672 int WaitStatesNeededForUse = 673 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 674 SmrdSgprWaitStates); 675 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 676 677 // This fixes what appears to be undocumented hardware behavior in SI where 678 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor 679 // needs some number of nops in between. We don't know how many we need, but 680 // let's use 4. This wasn't discovered before probably because the only 681 // case when this happens is when we expand a 64-bit pointer into a full 682 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was 683 // probably never encountered in the closed-source land. 684 if (IsBufferSMRD) { 685 int WaitStatesNeededForUse = 686 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), 687 IsBufferHazardDefFn, 688 SmrdSgprWaitStates); 689 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 690 } 691 } 692 693 return WaitStatesNeeded; 694 } 695 696 int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { 697 if (!ST.hasVMEMReadSGPRVALUDefHazard()) 698 return 0; 699 700 int WaitStatesNeeded = checkSoftClauseHazards(VMEM); 701 702 // A read of an SGPR by a VMEM instruction requires 5 wait states when the 703 // SGPR was written by a VALU Instruction. 704 const int VmemSgprWaitStates = 5; 705 auto IsHazardDefFn = [this](const MachineInstr &MI) { 706 return TII.isVALU(MI); 707 }; 708 for (const MachineOperand &Use : VMEM->uses()) { 709 if (!Use.isReg() || TRI.isVectorRegister(MF.getRegInfo(), Use.getReg())) 710 continue; 711 712 int WaitStatesNeededForUse = 713 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 714 VmemSgprWaitStates); 715 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 716 } 717 return WaitStatesNeeded; 718 } 719 720 int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { 721 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 722 const SIInstrInfo *TII = ST.getInstrInfo(); 723 724 // Check for DPP VGPR read after VALU VGPR write and EXEC write. 725 int DppVgprWaitStates = 2; 726 int DppExecWaitStates = 5; 727 int WaitStatesNeeded = 0; 728 auto IsHazardDefFn = [TII](const MachineInstr &MI) { 729 return TII->isVALU(MI); 730 }; 731 732 for (const MachineOperand &Use : DPP->uses()) { 733 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 734 continue; 735 int WaitStatesNeededForUse = 736 DppVgprWaitStates - getWaitStatesSinceDef( 737 Use.getReg(), 738 [](const MachineInstr &) { return true; }, 739 DppVgprWaitStates); 740 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 741 } 742 743 WaitStatesNeeded = std::max( 744 WaitStatesNeeded, 745 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn, 746 DppExecWaitStates)); 747 748 return WaitStatesNeeded; 749 } 750 751 int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { 752 const SIInstrInfo *TII = ST.getInstrInfo(); 753 754 // v_div_fmas requires 4 wait states after a write to vcc from a VALU 755 // instruction. 756 const int DivFMasWaitStates = 4; 757 auto IsHazardDefFn = [TII](const MachineInstr &MI) { 758 return TII->isVALU(MI); 759 }; 760 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn, 761 DivFMasWaitStates); 762 763 return DivFMasWaitStates - WaitStatesNeeded; 764 } 765 766 int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { 767 const SIInstrInfo *TII = ST.getInstrInfo(); 768 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); 769 770 const int GetRegWaitStates = 2; 771 auto IsHazardFn = [TII, GetRegHWReg](const MachineInstr &MI) { 772 return GetRegHWReg == getHWReg(TII, MI); 773 }; 774 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates); 775 776 return GetRegWaitStates - WaitStatesNeeded; 777 } 778 779 int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { 780 const SIInstrInfo *TII = ST.getInstrInfo(); 781 unsigned HWReg = getHWReg(TII, *SetRegInstr); 782 783 const int SetRegWaitStates = ST.getSetRegWaitStates(); 784 auto IsHazardFn = [TII, HWReg](const MachineInstr &MI) { 785 return HWReg == getHWReg(TII, MI); 786 }; 787 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates); 788 return SetRegWaitStates - WaitStatesNeeded; 789 } 790 791 int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { 792 if (!MI.mayStore()) 793 return -1; 794 795 const SIInstrInfo *TII = ST.getInstrInfo(); 796 unsigned Opcode = MI.getOpcode(); 797 const MCInstrDesc &Desc = MI.getDesc(); 798 799 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 800 int VDataRCID = -1; 801 if (VDataIdx != -1) 802 VDataRCID = Desc.OpInfo[VDataIdx].RegClass; 803 804 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { 805 // There is no hazard if the instruction does not use vector regs 806 // (like wbinvl1) 807 if (VDataIdx == -1) 808 return -1; 809 // For MUBUF/MTBUF instructions this hazard only exists if the 810 // instruction is not using a register in the soffset field. 811 const MachineOperand *SOffset = 812 TII->getNamedOperand(MI, AMDGPU::OpName::soffset); 813 // If we have no soffset operand, then assume this field has been 814 // hardcoded to zero. 815 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && 816 (!SOffset || !SOffset->isReg())) 817 return VDataIdx; 818 } 819 820 // MIMG instructions create a hazard if they don't use a 256-bit T# and 821 // the store size is greater than 8 bytes and they have more than two bits 822 // of their dmask set. 823 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. 824 if (TII->isMIMG(MI)) { 825 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 826 assert(SRsrcIdx != -1 && 827 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); 828 (void)SRsrcIdx; 829 } 830 831 if (TII->isFLAT(MI)) { 832 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 833 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) 834 return DataIdx; 835 } 836 837 return -1; 838 } 839 840 int 841 GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def, 842 const MachineRegisterInfo &MRI) { 843 // Helper to check for the hazard where VMEM instructions that store more than 844 // 8 bytes can have there store data over written by the next instruction. 845 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 846 847 const int VALUWaitStates = ST.hasGFX940Insts() ? 2 : 1; 848 int WaitStatesNeeded = 0; 849 850 if (!TRI->isVectorRegister(MRI, Def.getReg())) 851 return WaitStatesNeeded; 852 Register Reg = Def.getReg(); 853 auto IsHazardFn = [this, Reg, TRI](const MachineInstr &MI) { 854 int DataIdx = createsVALUHazard(MI); 855 return DataIdx >= 0 && 856 TRI->regsOverlap(MI.getOperand(DataIdx).getReg(), Reg); 857 }; 858 int WaitStatesNeededForDef = 859 VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates); 860 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 861 862 return WaitStatesNeeded; 863 } 864 865 int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { 866 int WaitStatesNeeded = 0; 867 868 if (ST.hasTransForwardingHazard() && !SIInstrInfo::isTRANS(*VALU)) { 869 const int TransDefWaitstates = 1; 870 871 auto IsTransDefFn = [this, VALU](const MachineInstr &MI) { 872 if (!SIInstrInfo::isTRANS(MI)) 873 return false; 874 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 875 const SIInstrInfo *TII = ST.getInstrInfo(); 876 Register Def = TII->getNamedOperand(MI, AMDGPU::OpName::vdst)->getReg(); 877 878 for (const MachineOperand &Use : VALU->explicit_uses()) { 879 if (Use.isReg() && TRI->regsOverlap(Def, Use.getReg())) 880 return true; 881 } 882 883 return false; 884 }; 885 886 int WaitStatesNeededForDef = 887 TransDefWaitstates - 888 getWaitStatesSince(IsTransDefFn, TransDefWaitstates); 889 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 890 } 891 892 if (ST.hasDstSelForwardingHazard()) { 893 const int Shift16DefWaitstates = 1; 894 895 auto IsShift16BitDefFn = [this, VALU](const MachineInstr &MI) { 896 if (!SIInstrInfo::isVALU(MI)) 897 return false; 898 const SIInstrInfo *TII = ST.getInstrInfo(); 899 if (SIInstrInfo::isSDWA(MI)) { 900 if (auto *DstSel = TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel)) 901 if (DstSel->getImm() == AMDGPU::SDWA::DWORD) 902 return false; 903 } else { 904 if ((AMDGPU::getNamedOperandIdx(MI.getOpcode(), 905 AMDGPU::OpName::op_sel) == -1) || 906 !(TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers) 907 ->getImm() & 908 SISrcMods::DST_OP_SEL)) 909 return false; 910 } 911 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 912 if (auto *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst)) { 913 Register Def = Dst->getReg(); 914 915 for (const MachineOperand &Use : VALU->explicit_uses()) { 916 if (Use.isReg() && TRI->regsOverlap(Def, Use.getReg())) 917 return true; 918 } 919 } 920 921 return false; 922 }; 923 924 int WaitStatesNeededForDef = 925 Shift16DefWaitstates - 926 getWaitStatesSince(IsShift16BitDefFn, Shift16DefWaitstates); 927 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 928 } 929 930 if (ST.hasVDecCoExecHazard()) { 931 const int VALUWriteSGPRVALUReadWaitstates = 2; 932 const int VALUWriteEXECRWLane = 4; 933 const int VALUWriteVGPRReadlaneRead = 1; 934 935 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 936 const MachineRegisterInfo &MRI = MF.getRegInfo(); 937 Register UseReg; 938 auto IsVALUDefSGPRFn = [&UseReg, TRI](const MachineInstr &MI) { 939 if (!SIInstrInfo::isVALU(MI)) 940 return false; 941 return MI.modifiesRegister(UseReg, TRI); 942 }; 943 944 for (const MachineOperand &Use : VALU->explicit_uses()) { 945 if (!Use.isReg()) 946 continue; 947 948 UseReg = Use.getReg(); 949 if (TRI->isSGPRReg(MRI, UseReg)) { 950 int WaitStatesNeededForDef = 951 VALUWriteSGPRVALUReadWaitstates - 952 getWaitStatesSince(IsVALUDefSGPRFn, 953 VALUWriteSGPRVALUReadWaitstates); 954 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 955 } 956 } 957 958 if (VALU->readsRegister(AMDGPU::VCC, TRI)) { 959 UseReg = AMDGPU::VCC; 960 int WaitStatesNeededForDef = 961 VALUWriteSGPRVALUReadWaitstates - 962 getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteSGPRVALUReadWaitstates); 963 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 964 } 965 966 switch (VALU->getOpcode()) { 967 case AMDGPU::V_READLANE_B32: 968 case AMDGPU::V_READFIRSTLANE_B32: { 969 MachineOperand *Src = TII.getNamedOperand(*VALU, AMDGPU::OpName::src0); 970 UseReg = Src->getReg(); 971 int WaitStatesNeededForDef = 972 VALUWriteVGPRReadlaneRead - 973 getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteVGPRReadlaneRead); 974 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 975 } 976 LLVM_FALLTHROUGH; 977 case AMDGPU::V_WRITELANE_B32: { 978 UseReg = AMDGPU::EXEC; 979 int WaitStatesNeededForDef = 980 VALUWriteEXECRWLane - 981 getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteEXECRWLane); 982 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 983 break; 984 } 985 default: 986 break; 987 } 988 } 989 990 // This checks for the hazard where VMEM instructions that store more than 991 // 8 bytes can have there store data over written by the next instruction. 992 if (!ST.has12DWordStoreHazard()) 993 return WaitStatesNeeded; 994 995 const MachineRegisterInfo &MRI = MF.getRegInfo(); 996 997 for (const MachineOperand &Def : VALU->defs()) { 998 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI)); 999 } 1000 1001 return WaitStatesNeeded; 1002 } 1003 1004 int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) { 1005 // This checks for hazards associated with inline asm statements. 1006 // Since inline asms can contain just about anything, we use this 1007 // to call/leverage other check*Hazard routines. Note that 1008 // this function doesn't attempt to address all possible inline asm 1009 // hazards (good luck), but is a collection of what has been 1010 // problematic thus far. 1011 1012 // see checkVALUHazards() 1013 if (!ST.has12DWordStoreHazard()) 1014 return 0; 1015 1016 const MachineRegisterInfo &MRI = MF.getRegInfo(); 1017 int WaitStatesNeeded = 0; 1018 1019 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands(); 1020 I != E; ++I) { 1021 const MachineOperand &Op = IA->getOperand(I); 1022 if (Op.isReg() && Op.isDef()) { 1023 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI)); 1024 } 1025 } 1026 1027 return WaitStatesNeeded; 1028 } 1029 1030 int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { 1031 const SIInstrInfo *TII = ST.getInstrInfo(); 1032 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1033 const MachineRegisterInfo &MRI = MF.getRegInfo(); 1034 1035 const MachineOperand *LaneSelectOp = 1036 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); 1037 1038 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) 1039 return 0; 1040 1041 Register LaneSelectReg = LaneSelectOp->getReg(); 1042 auto IsHazardFn = [TII](const MachineInstr &MI) { return TII->isVALU(MI); }; 1043 1044 const int RWLaneWaitStates = 4; 1045 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn, 1046 RWLaneWaitStates); 1047 return RWLaneWaitStates - WaitStatesSince; 1048 } 1049 1050 int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { 1051 if (!ST.hasRFEHazards()) 1052 return 0; 1053 1054 const SIInstrInfo *TII = ST.getInstrInfo(); 1055 1056 const int RFEWaitStates = 1; 1057 1058 auto IsHazardFn = [TII](const MachineInstr &MI) { 1059 return getHWReg(TII, MI) == AMDGPU::Hwreg::ID_TRAPSTS; 1060 }; 1061 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates); 1062 return RFEWaitStates - WaitStatesNeeded; 1063 } 1064 1065 int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { 1066 const SIInstrInfo *TII = ST.getInstrInfo(); 1067 const int ReadM0WaitStates = 1; 1068 auto IsHazardFn = [TII](const MachineInstr &MI) { return TII->isSALU(MI); }; 1069 return ReadM0WaitStates - 1070 getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn, ReadM0WaitStates); 1071 } 1072 1073 void GCNHazardRecognizer::fixHazards(MachineInstr *MI) { 1074 fixVMEMtoScalarWriteHazards(MI); 1075 fixVcmpxPermlaneHazards(MI); 1076 fixSMEMtoVectorWriteHazards(MI); 1077 fixVcmpxExecWARHazard(MI); 1078 fixLdsBranchVmemWARHazard(MI); 1079 if (ST.hasLdsDirect()) { 1080 fixLdsDirectVALUHazard(MI); 1081 fixLdsDirectVMEMHazard(MI); 1082 } 1083 fixVALUPartialForwardingHazard(MI); 1084 fixVALUTransUseHazard(MI); 1085 } 1086 1087 bool GCNHazardRecognizer::fixVcmpxPermlaneHazards(MachineInstr *MI) { 1088 if (!ST.hasVcmpxPermlaneHazard() || !isPermlane(*MI)) 1089 return false; 1090 1091 const SIInstrInfo *TII = ST.getInstrInfo(); 1092 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1093 auto IsHazardFn = [TII, TRI](const MachineInstr &MI) { 1094 return (TII->isVOPC(MI) || 1095 ((TII->isVOP3(MI) || TII->isSDWA(MI)) && MI.isCompare())) && 1096 MI.modifiesRegister(AMDGPU::EXEC, TRI); 1097 }; 1098 1099 auto IsExpiredFn = [](const MachineInstr &MI, int) { 1100 unsigned Opc = MI.getOpcode(); 1101 return SIInstrInfo::isVALU(MI) && Opc != AMDGPU::V_NOP_e32 && 1102 Opc != AMDGPU::V_NOP_e64 && Opc != AMDGPU::V_NOP_sdwa; 1103 }; 1104 1105 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1106 std::numeric_limits<int>::max()) 1107 return false; 1108 1109 // V_NOP will be discarded by SQ. 1110 // Use V_MOV_B32 v?, v?. Register must be alive so use src0 of V_PERMLANE* 1111 // which is always a VGPR and available. 1112 auto *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0); 1113 Register Reg = Src0->getReg(); 1114 bool IsUndef = Src0->isUndef(); 1115 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1116 TII->get(AMDGPU::V_MOV_B32_e32)) 1117 .addReg(Reg, RegState::Define | (IsUndef ? RegState::Dead : 0)) 1118 .addReg(Reg, IsUndef ? RegState::Undef : RegState::Kill); 1119 1120 return true; 1121 } 1122 1123 bool GCNHazardRecognizer::fixVMEMtoScalarWriteHazards(MachineInstr *MI) { 1124 if (!ST.hasVMEMtoScalarWriteHazard()) 1125 return false; 1126 1127 if (!SIInstrInfo::isSALU(*MI) && !SIInstrInfo::isSMRD(*MI)) 1128 return false; 1129 1130 if (MI->getNumDefs() == 0) 1131 return false; 1132 1133 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1134 1135 auto IsHazardFn = [TRI, MI](const MachineInstr &I) { 1136 if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isDS(I) && 1137 !SIInstrInfo::isFLAT(I)) 1138 return false; 1139 1140 for (const MachineOperand &Def : MI->defs()) { 1141 const MachineOperand *Op = 1142 I.findRegisterUseOperand(Def.getReg(), false, TRI); 1143 if (!Op) 1144 continue; 1145 return true; 1146 } 1147 return false; 1148 }; 1149 1150 auto IsExpiredFn = [](const MachineInstr &MI, int) { 1151 return SIInstrInfo::isVALU(MI) || 1152 (MI.getOpcode() == AMDGPU::S_WAITCNT && 1153 !MI.getOperand(0).getImm()) || 1154 (MI.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1155 MI.getOperand(0).getImm() == 0xffe3); 1156 }; 1157 1158 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1159 std::numeric_limits<int>::max()) 1160 return false; 1161 1162 const SIInstrInfo *TII = ST.getInstrInfo(); 1163 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1164 TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 1165 .addImm(0xffe3); 1166 return true; 1167 } 1168 1169 bool GCNHazardRecognizer::fixSMEMtoVectorWriteHazards(MachineInstr *MI) { 1170 if (!ST.hasSMEMtoVectorWriteHazard()) 1171 return false; 1172 1173 if (!SIInstrInfo::isVALU(*MI)) 1174 return false; 1175 1176 unsigned SDSTName; 1177 switch (MI->getOpcode()) { 1178 case AMDGPU::V_READLANE_B32: 1179 case AMDGPU::V_READFIRSTLANE_B32: 1180 SDSTName = AMDGPU::OpName::vdst; 1181 break; 1182 default: 1183 SDSTName = AMDGPU::OpName::sdst; 1184 break; 1185 } 1186 1187 const SIInstrInfo *TII = ST.getInstrInfo(); 1188 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1189 const AMDGPU::IsaVersion IV = AMDGPU::getIsaVersion(ST.getCPU()); 1190 const MachineOperand *SDST = TII->getNamedOperand(*MI, SDSTName); 1191 if (!SDST) { 1192 for (const auto &MO : MI->implicit_operands()) { 1193 if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg()))) { 1194 SDST = &MO; 1195 break; 1196 } 1197 } 1198 } 1199 1200 if (!SDST) 1201 return false; 1202 1203 const Register SDSTReg = SDST->getReg(); 1204 auto IsHazardFn = [SDSTReg, TRI](const MachineInstr &I) { 1205 return SIInstrInfo::isSMRD(I) && I.readsRegister(SDSTReg, TRI); 1206 }; 1207 1208 auto IsExpiredFn = [TII, IV](const MachineInstr &MI, int) { 1209 if (TII->isSALU(MI)) { 1210 switch (MI.getOpcode()) { 1211 case AMDGPU::S_SETVSKIP: 1212 case AMDGPU::S_VERSION: 1213 case AMDGPU::S_WAITCNT_VSCNT: 1214 case AMDGPU::S_WAITCNT_VMCNT: 1215 case AMDGPU::S_WAITCNT_EXPCNT: 1216 // These instructions cannot not mitigate the hazard. 1217 return false; 1218 case AMDGPU::S_WAITCNT_LGKMCNT: 1219 // Reducing lgkmcnt count to 0 always mitigates the hazard. 1220 return (MI.getOperand(1).getImm() == 0) && 1221 (MI.getOperand(0).getReg() == AMDGPU::SGPR_NULL); 1222 case AMDGPU::S_WAITCNT: { 1223 const int64_t Imm = MI.getOperand(0).getImm(); 1224 AMDGPU::Waitcnt Decoded = AMDGPU::decodeWaitcnt(IV, Imm); 1225 return (Decoded.LgkmCnt == 0); 1226 } 1227 default: 1228 // SOPP instructions cannot mitigate the hazard. 1229 if (TII->isSOPP(MI)) 1230 return false; 1231 // At this point the SALU can be assumed to mitigate the hazard 1232 // because either: 1233 // (a) it is independent of the at risk SMEM (breaking chain), 1234 // or 1235 // (b) it is dependent on the SMEM, in which case an appropriate 1236 // s_waitcnt lgkmcnt _must_ exist between it and the at risk 1237 // SMEM instruction. 1238 return true; 1239 } 1240 } 1241 return false; 1242 }; 1243 1244 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1245 std::numeric_limits<int>::max()) 1246 return false; 1247 1248 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1249 TII->get(AMDGPU::S_MOV_B32), AMDGPU::SGPR_NULL) 1250 .addImm(0); 1251 return true; 1252 } 1253 1254 bool GCNHazardRecognizer::fixVcmpxExecWARHazard(MachineInstr *MI) { 1255 if (!ST.hasVcmpxExecWARHazard() || !SIInstrInfo::isVALU(*MI)) 1256 return false; 1257 1258 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1259 if (!MI->modifiesRegister(AMDGPU::EXEC, TRI)) 1260 return false; 1261 1262 auto IsHazardFn = [TRI](const MachineInstr &I) { 1263 if (SIInstrInfo::isVALU(I)) 1264 return false; 1265 return I.readsRegister(AMDGPU::EXEC, TRI); 1266 }; 1267 1268 const SIInstrInfo *TII = ST.getInstrInfo(); 1269 auto IsExpiredFn = [TII, TRI](const MachineInstr &MI, int) { 1270 if (SIInstrInfo::isVALU(MI)) { 1271 if (TII->getNamedOperand(MI, AMDGPU::OpName::sdst)) 1272 return true; 1273 for (auto MO : MI.implicit_operands()) 1274 if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg()))) 1275 return true; 1276 } 1277 if (MI.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1278 (MI.getOperand(0).getImm() & 0xfffe) == 0xfffe) 1279 return true; 1280 return false; 1281 }; 1282 1283 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1284 std::numeric_limits<int>::max()) 1285 return false; 1286 1287 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1288 TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 1289 .addImm(0xfffe); 1290 return true; 1291 } 1292 1293 static bool shouldRunLdsBranchVmemWARHazardFixup(const MachineFunction &MF, 1294 const GCNSubtarget &ST) { 1295 if (!ST.hasLdsBranchVmemWARHazard()) 1296 return false; 1297 1298 // Check if the necessary condition for the hazard is met: both LDS and VMEM 1299 // instructions need to appear in the same function. 1300 bool HasLds = false; 1301 bool HasVmem = false; 1302 for (auto &MBB : MF) { 1303 for (auto &MI : MBB) { 1304 HasLds |= SIInstrInfo::isDS(MI); 1305 HasVmem |= 1306 SIInstrInfo::isVMEM(MI) || SIInstrInfo::isSegmentSpecificFLAT(MI); 1307 if (HasLds && HasVmem) 1308 return true; 1309 } 1310 } 1311 return false; 1312 } 1313 1314 bool GCNHazardRecognizer::fixLdsBranchVmemWARHazard(MachineInstr *MI) { 1315 if (!RunLdsBranchVmemWARHazardFixup) 1316 return false; 1317 1318 assert(ST.hasLdsBranchVmemWARHazard()); 1319 1320 auto IsHazardInst = [](const MachineInstr &MI) { 1321 if (SIInstrInfo::isDS(MI)) 1322 return 1; 1323 if (SIInstrInfo::isVMEM(MI) || SIInstrInfo::isSegmentSpecificFLAT(MI)) 1324 return 2; 1325 return 0; 1326 }; 1327 1328 auto InstType = IsHazardInst(*MI); 1329 if (!InstType) 1330 return false; 1331 1332 auto IsExpiredFn = [&IsHazardInst](const MachineInstr &I, int) { 1333 return IsHazardInst(I) || (I.getOpcode() == AMDGPU::S_WAITCNT_VSCNT && 1334 I.getOperand(0).getReg() == AMDGPU::SGPR_NULL && 1335 !I.getOperand(1).getImm()); 1336 }; 1337 1338 auto IsHazardFn = [InstType, &IsHazardInst](const MachineInstr &I) { 1339 if (!I.isBranch()) 1340 return false; 1341 1342 auto IsHazardFn = [InstType, IsHazardInst](const MachineInstr &I) { 1343 auto InstType2 = IsHazardInst(I); 1344 return InstType2 && InstType != InstType2; 1345 }; 1346 1347 auto IsExpiredFn = [InstType, &IsHazardInst](const MachineInstr &I, int) { 1348 auto InstType2 = IsHazardInst(I); 1349 if (InstType == InstType2) 1350 return true; 1351 1352 return I.getOpcode() == AMDGPU::S_WAITCNT_VSCNT && 1353 I.getOperand(0).getReg() == AMDGPU::SGPR_NULL && 1354 !I.getOperand(1).getImm(); 1355 }; 1356 1357 return ::getWaitStatesSince(IsHazardFn, &I, IsExpiredFn) != 1358 std::numeric_limits<int>::max(); 1359 }; 1360 1361 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1362 std::numeric_limits<int>::max()) 1363 return false; 1364 1365 const SIInstrInfo *TII = ST.getInstrInfo(); 1366 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1367 TII->get(AMDGPU::S_WAITCNT_VSCNT)) 1368 .addReg(AMDGPU::SGPR_NULL, RegState::Undef) 1369 .addImm(0); 1370 1371 return true; 1372 } 1373 1374 bool GCNHazardRecognizer::fixLdsDirectVALUHazard(MachineInstr *MI) { 1375 if (!SIInstrInfo::isLDSDIR(*MI)) 1376 return false; 1377 1378 const int NoHazardWaitStates = 15; 1379 const MachineOperand *VDST = TII.getNamedOperand(*MI, AMDGPU::OpName::vdst); 1380 const Register VDSTReg = VDST->getReg(); 1381 1382 bool VisitedTrans = false; 1383 auto IsHazardFn = [this, VDSTReg, &VisitedTrans](const MachineInstr &I) { 1384 if (!SIInstrInfo::isVALU(I)) 1385 return false; 1386 VisitedTrans = VisitedTrans || SIInstrInfo::isTRANS(I); 1387 // Cover both WAR and WAW 1388 return I.readsRegister(VDSTReg, &TRI) || I.modifiesRegister(VDSTReg, &TRI); 1389 }; 1390 auto IsExpiredFn = [&](const MachineInstr &I, int WaitStates) { 1391 if (WaitStates >= NoHazardWaitStates) 1392 return true; 1393 // Instructions which cause va_vdst==0 expire hazard 1394 return SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 1395 SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I); 1396 }; 1397 auto GetWaitStatesFn = [](const MachineInstr &MI) { 1398 return SIInstrInfo::isVALU(MI) ? 1 : 0; 1399 }; 1400 1401 DenseSet<const MachineBasicBlock *> Visited; 1402 auto Count = ::getWaitStatesSince(IsHazardFn, MI->getParent(), 1403 std::next(MI->getReverseIterator()), 0, 1404 IsExpiredFn, Visited, GetWaitStatesFn); 1405 1406 // Transcendentals can execute in parallel to other VALUs. 1407 // This makes va_vdst count unusable with a mixture of VALU and TRANS. 1408 if (VisitedTrans) 1409 Count = 0; 1410 1411 MachineOperand *WaitVdstOp = 1412 TII.getNamedOperand(*MI, AMDGPU::OpName::waitvdst); 1413 WaitVdstOp->setImm(std::min(Count, NoHazardWaitStates)); 1414 1415 return true; 1416 } 1417 1418 bool GCNHazardRecognizer::fixLdsDirectVMEMHazard(MachineInstr *MI) { 1419 if (!SIInstrInfo::isLDSDIR(*MI)) 1420 return false; 1421 1422 const MachineOperand *VDST = TII.getNamedOperand(*MI, AMDGPU::OpName::vdst); 1423 const Register VDSTReg = VDST->getReg(); 1424 1425 auto IsHazardFn = [this, VDSTReg](const MachineInstr &I) { 1426 if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isFLAT(I) && 1427 !SIInstrInfo::isDS(I)) 1428 return false; 1429 return I.readsRegister(VDSTReg, &TRI) || I.modifiesRegister(VDSTReg, &TRI); 1430 }; 1431 auto IsExpiredFn = [](const MachineInstr &I, int) { 1432 return SIInstrInfo::isVALU(I) || SIInstrInfo::isEXP(I) || 1433 (I.getOpcode() == AMDGPU::S_WAITCNT && !I.getOperand(0).getImm()) || 1434 (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1435 I.getOperand(0).getImm() == 0xffe3); 1436 }; 1437 1438 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 1439 std::numeric_limits<int>::max()) 1440 return false; 1441 1442 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1443 TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 1444 .addImm(0xffe3); 1445 1446 return true; 1447 } 1448 1449 bool GCNHazardRecognizer::fixVALUPartialForwardingHazard(MachineInstr *MI) { 1450 if (!ST.isWave64()) 1451 return false; 1452 if (!ST.hasVALUPartialForwardingHazard()) 1453 return false; 1454 if (!SIInstrInfo::isVALU(*MI)) 1455 return false; 1456 1457 SmallSetVector<Register, 4> SrcVGPRs; 1458 1459 for (const MachineOperand &Use : MI->explicit_uses()) { 1460 if (Use.isReg() && TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 1461 SrcVGPRs.insert(Use.getReg()); 1462 } 1463 1464 // Only applies with >= 2 unique VGPR sources 1465 if (SrcVGPRs.size() <= 1) 1466 return false; 1467 1468 // Look for the following pattern: 1469 // Va <- VALU [PreExecPos] 1470 // intv1 1471 // Exec <- SALU [ExecPos] 1472 // intv2 1473 // Vb <- VALU [PostExecPos] 1474 // intv3 1475 // MI Va, Vb (WaitState = 0) 1476 // 1477 // Where: 1478 // intv1 + intv2 <= 2 VALUs 1479 // intv3 <= 4 VALUs 1480 // 1481 // If found, insert an appropriate S_WAITCNT_DEPCTR before MI. 1482 1483 const int Intv1plus2MaxVALUs = 2; 1484 const int Intv3MaxVALUs = 4; 1485 const int IntvMaxVALUs = 6; 1486 const int NoHazardVALUWaitStates = IntvMaxVALUs + 2; 1487 1488 struct StateType { 1489 SmallDenseMap<Register, int, 4> DefPos; 1490 int ExecPos = std::numeric_limits<int>::max(); 1491 int VALUs = 0; 1492 }; 1493 1494 StateType State; 1495 1496 // This overloads expiry testing with all the hazard detection 1497 auto IsHazardFn = [&, this](StateType &State, const MachineInstr &I) { 1498 // Too many VALU states have passed 1499 if (State.VALUs > NoHazardVALUWaitStates) 1500 return HazardExpired; 1501 1502 // Instructions which cause va_vdst==0 expire hazard 1503 if (SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 1504 SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I) || 1505 (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1506 I.getOperand(0).getImm() == 0x0fff)) 1507 return HazardExpired; 1508 1509 // Track registers writes 1510 bool Changed = false; 1511 if (SIInstrInfo::isVALU(I)) { 1512 for (Register Src : SrcVGPRs) { 1513 if (!State.DefPos.count(Src) && I.modifiesRegister(Src, &TRI)) { 1514 State.DefPos[Src] = State.VALUs; 1515 Changed = true; 1516 } 1517 } 1518 } else if (SIInstrInfo::isSALU(I)) { 1519 if (State.ExecPos == std::numeric_limits<int>::max()) { 1520 if (!State.DefPos.empty() && I.modifiesRegister(AMDGPU::EXEC, &TRI)) { 1521 State.ExecPos = State.VALUs; 1522 Changed = true; 1523 } 1524 } 1525 } 1526 1527 // Early expiration: too many VALUs in intv3 1528 if (State.VALUs > Intv3MaxVALUs && State.DefPos.empty()) 1529 return HazardExpired; 1530 1531 // Only evaluate state if something changed 1532 if (!Changed) 1533 return NoHazardFound; 1534 1535 // Determine positions of VALUs pre/post exec change 1536 if (State.ExecPos == std::numeric_limits<int>::max()) 1537 return NoHazardFound; 1538 1539 int PreExecPos = std::numeric_limits<int>::max(); 1540 int PostExecPos = std::numeric_limits<int>::max(); 1541 1542 for (auto Entry : State.DefPos) { 1543 int DefVALUs = Entry.second; 1544 if (DefVALUs != std::numeric_limits<int>::max()) { 1545 if (DefVALUs >= State.ExecPos) 1546 PreExecPos = std::min(PreExecPos, DefVALUs); 1547 else if (DefVALUs < State.ExecPos) 1548 PostExecPos = std::min(PostExecPos, DefVALUs); 1549 } 1550 } 1551 1552 // Need a VALUs post exec change 1553 if (PostExecPos == std::numeric_limits<int>::max()) 1554 return NoHazardFound; 1555 1556 // Too many VALUs in intv3? 1557 int Intv3VALUs = PostExecPos; 1558 if (Intv3VALUs > Intv3MaxVALUs) 1559 return HazardExpired; 1560 1561 // Too many VALUs in intv2? 1562 int Intv2VALUs = (State.ExecPos - PostExecPos) - 1; 1563 if (Intv2VALUs > Intv1plus2MaxVALUs) 1564 return HazardExpired; 1565 1566 // Need a VALUs pre exec change 1567 if (PreExecPos == std::numeric_limits<int>::max()) 1568 return NoHazardFound; 1569 1570 // Too many VALUs in intv1? 1571 int Intv1VALUs = PreExecPos - State.ExecPos; 1572 if (Intv1VALUs > Intv1plus2MaxVALUs) 1573 return HazardExpired; 1574 1575 // Too many VALUs in intv1 + intv2 1576 if (Intv1VALUs + Intv2VALUs > Intv1plus2MaxVALUs) 1577 return HazardExpired; 1578 1579 return HazardFound; 1580 }; 1581 auto UpdateStateFn = [](StateType &State, const MachineInstr &MI) { 1582 if (SIInstrInfo::isVALU(MI)) 1583 State.VALUs += 1; 1584 }; 1585 1586 DenseSet<const MachineBasicBlock *> Visited; 1587 if (!hasHazard<StateType>(State, IsHazardFn, UpdateStateFn, MI->getParent(), 1588 std::next(MI->getReverseIterator()), Visited)) 1589 return false; 1590 1591 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1592 TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 1593 .addImm(0x0fff); 1594 1595 return true; 1596 } 1597 1598 bool GCNHazardRecognizer::fixVALUTransUseHazard(MachineInstr *MI) { 1599 if (!ST.hasVALUTransUseHazard()) 1600 return false; 1601 if (!SIInstrInfo::isVALU(*MI)) 1602 return false; 1603 1604 SmallSet<Register, 4> SrcVGPRs; 1605 1606 for (const MachineOperand &Use : MI->explicit_uses()) { 1607 if (Use.isReg() && TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 1608 SrcVGPRs.insert(Use.getReg()); 1609 } 1610 1611 // Look for the following pattern: 1612 // Va <- TRANS VALU 1613 // intv 1614 // MI Va (WaitState = 0) 1615 // 1616 // Where: 1617 // intv <= 5 VALUs / 1 TRANS 1618 // 1619 // If found, insert an appropriate S_WAITCNT_DEPCTR before MI. 1620 1621 const int IntvMaxVALUs = 5; 1622 const int IntvMaxTRANS = 1; 1623 1624 struct StateType { 1625 int VALUs = 0; 1626 int TRANS = 0; 1627 }; 1628 1629 StateType State; 1630 1631 // This overloads expiry testing with all the hazard detection 1632 auto IsHazardFn = [&, this](StateType &State, const MachineInstr &I) { 1633 // Too many VALU states have passed 1634 if (State.VALUs > IntvMaxVALUs || State.TRANS > IntvMaxTRANS) 1635 return HazardExpired; 1636 1637 // Instructions which cause va_vdst==0 expire hazard 1638 if (SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 1639 SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I) || 1640 (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1641 I.getOperand(0).getImm() == 0x0fff)) 1642 return HazardExpired; 1643 1644 // Track registers writes 1645 if (SIInstrInfo::isTRANS(I)) { 1646 for (Register Src : SrcVGPRs) { 1647 if (I.modifiesRegister(Src, &TRI)) { 1648 return HazardFound; 1649 } 1650 } 1651 } 1652 1653 return NoHazardFound; 1654 }; 1655 auto UpdateStateFn = [](StateType &State, const MachineInstr &MI) { 1656 if (SIInstrInfo::isVALU(MI)) 1657 State.VALUs += 1; 1658 if (SIInstrInfo::isTRANS(MI)) 1659 State.TRANS += 1; 1660 }; 1661 1662 DenseSet<const MachineBasicBlock *> Visited; 1663 if (!hasHazard<StateType>(State, IsHazardFn, UpdateStateFn, MI->getParent(), 1664 std::next(MI->getReverseIterator()), Visited)) 1665 return false; 1666 1667 // Hazard is observed - insert a wait on va_dst counter to ensure hazard is 1668 // avoided (mask 0x0fff achieves this). 1669 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1670 TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 1671 .addImm(0x0fff); 1672 1673 return true; 1674 } 1675 1676 int GCNHazardRecognizer::checkNSAtoVMEMHazard(MachineInstr *MI) { 1677 int NSAtoVMEMWaitStates = 1; 1678 1679 if (!ST.hasNSAtoVMEMBug()) 1680 return 0; 1681 1682 if (!SIInstrInfo::isMUBUF(*MI) && !SIInstrInfo::isMTBUF(*MI)) 1683 return 0; 1684 1685 const SIInstrInfo *TII = ST.getInstrInfo(); 1686 const auto *Offset = TII->getNamedOperand(*MI, AMDGPU::OpName::offset); 1687 if (!Offset || (Offset->getImm() & 6) == 0) 1688 return 0; 1689 1690 auto IsHazardFn = [TII](const MachineInstr &I) { 1691 if (!SIInstrInfo::isMIMG(I)) 1692 return false; 1693 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(I.getOpcode()); 1694 return Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA && 1695 TII->getInstSizeInBytes(I) >= 16; 1696 }; 1697 1698 return NSAtoVMEMWaitStates - getWaitStatesSince(IsHazardFn, 1); 1699 } 1700 1701 int GCNHazardRecognizer::checkFPAtomicToDenormModeHazard(MachineInstr *MI) { 1702 int FPAtomicToDenormModeWaitStates = 3; 1703 1704 if (MI->getOpcode() != AMDGPU::S_DENORM_MODE) 1705 return 0; 1706 1707 auto IsHazardFn = [](const MachineInstr &I) { 1708 if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isFLAT(I)) 1709 return false; 1710 return SIInstrInfo::isFPAtomic(I); 1711 }; 1712 1713 auto IsExpiredFn = [](const MachineInstr &MI, int WaitStates) { 1714 if (WaitStates >= 3 || SIInstrInfo::isVALU(MI)) 1715 return true; 1716 1717 switch (MI.getOpcode()) { 1718 case AMDGPU::S_WAITCNT: 1719 case AMDGPU::S_WAITCNT_VSCNT: 1720 case AMDGPU::S_WAITCNT_VMCNT: 1721 case AMDGPU::S_WAITCNT_EXPCNT: 1722 case AMDGPU::S_WAITCNT_LGKMCNT: 1723 case AMDGPU::S_WAIT_IDLE: 1724 return true; 1725 default: 1726 break; 1727 } 1728 1729 return false; 1730 }; 1731 1732 return FPAtomicToDenormModeWaitStates - 1733 ::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn); 1734 } 1735 1736 int GCNHazardRecognizer::checkMAIHazards(MachineInstr *MI) { 1737 assert(SIInstrInfo::isMAI(*MI)); 1738 1739 return ST.hasGFX90AInsts() ? checkMAIHazards90A(MI) : checkMAIHazards908(MI); 1740 } 1741 1742 int GCNHazardRecognizer::checkMFMAPadding(MachineInstr *MI) { 1743 // Early exit if no padding is requested. 1744 if (MFMAPaddingRatio == 0) 1745 return 0; 1746 1747 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 1748 if (!SIInstrInfo::isMFMA(*MI) || MFI->getOccupancy() < 2) 1749 return 0; 1750 1751 int NeighborMFMALatency = 0; 1752 auto IsNeighboringMFMA = [&NeighborMFMALatency, 1753 this](const MachineInstr &MI) { 1754 if (!SIInstrInfo::isMFMA(MI)) 1755 return false; 1756 1757 NeighborMFMALatency = this->getMFMAPipelineWaitStates(MI); 1758 return true; 1759 }; 1760 1761 const int MaxMFMAPipelineWaitStates = 16; 1762 int WaitStatesSinceNeighborMFMA = 1763 getWaitStatesSince(IsNeighboringMFMA, MaxMFMAPipelineWaitStates); 1764 1765 int NeighborMFMAPaddingNeeded = 1766 (NeighborMFMALatency * MFMAPaddingRatio / 100) - 1767 WaitStatesSinceNeighborMFMA; 1768 1769 return std::max(0, NeighborMFMAPaddingNeeded); 1770 } 1771 1772 int GCNHazardRecognizer::checkMAIHazards908(MachineInstr *MI) { 1773 int WaitStatesNeeded = 0; 1774 unsigned Opc = MI->getOpcode(); 1775 1776 auto IsVALUFn = [](const MachineInstr &MI) { 1777 return SIInstrInfo::isVALU(MI); 1778 }; 1779 1780 if (Opc != AMDGPU::V_ACCVGPR_READ_B32_e64) { // MFMA or v_accvgpr_write 1781 const int LegacyVALUWritesVGPRWaitStates = 2; 1782 const int VALUWritesExecWaitStates = 4; 1783 const int MaxWaitStates = 4; 1784 1785 int WaitStatesNeededForUse = VALUWritesExecWaitStates - 1786 getWaitStatesSinceDef(AMDGPU::EXEC, IsVALUFn, MaxWaitStates); 1787 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1788 1789 if (WaitStatesNeeded < MaxWaitStates) { 1790 for (const MachineOperand &Use : MI->explicit_uses()) { 1791 const int MaxWaitStates = 2; 1792 1793 if (!Use.isReg() || !TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 1794 continue; 1795 1796 int WaitStatesNeededForUse = LegacyVALUWritesVGPRWaitStates - 1797 getWaitStatesSinceDef(Use.getReg(), IsVALUFn, MaxWaitStates); 1798 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1799 1800 if (WaitStatesNeeded == MaxWaitStates) 1801 break; 1802 } 1803 } 1804 } 1805 1806 for (const MachineOperand &Op : MI->explicit_operands()) { 1807 if (!Op.isReg() || !TRI.isAGPR(MF.getRegInfo(), Op.getReg())) 1808 continue; 1809 1810 if (Op.isDef() && Opc != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 1811 continue; 1812 1813 const int MFMAWritesAGPROverlappedSrcABWaitStates = 4; 1814 const int MFMAWritesAGPROverlappedSrcCWaitStates = 2; 1815 const int MFMA4x4WritesAGPRAccVgprReadWaitStates = 4; 1816 const int MFMA16x16WritesAGPRAccVgprReadWaitStates = 10; 1817 const int MFMA32x32WritesAGPRAccVgprReadWaitStates = 18; 1818 const int MFMA4x4WritesAGPRAccVgprWriteWaitStates = 1; 1819 const int MFMA16x16WritesAGPRAccVgprWriteWaitStates = 7; 1820 const int MFMA32x32WritesAGPRAccVgprWriteWaitStates = 15; 1821 const int MaxWaitStates = 18; 1822 Register Reg = Op.getReg(); 1823 unsigned HazardDefLatency = 0; 1824 1825 auto IsOverlappedMFMAFn = [Reg, &HazardDefLatency, 1826 this](const MachineInstr &MI) { 1827 if (!SIInstrInfo::isMFMA(MI)) 1828 return false; 1829 Register DstReg = MI.getOperand(0).getReg(); 1830 if (DstReg == Reg) 1831 return false; 1832 HazardDefLatency = 1833 std::max(HazardDefLatency, TSchedModel.computeInstrLatency(&MI)); 1834 return TRI.regsOverlap(DstReg, Reg); 1835 }; 1836 1837 int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsOverlappedMFMAFn, 1838 MaxWaitStates); 1839 int NeedWaitStates = MFMAWritesAGPROverlappedSrcABWaitStates; 1840 int SrcCIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 1841 int OpNo = MI->getOperandNo(&Op); 1842 if (OpNo == SrcCIdx) { 1843 NeedWaitStates = MFMAWritesAGPROverlappedSrcCWaitStates; 1844 } else if (Opc == AMDGPU::V_ACCVGPR_READ_B32_e64) { 1845 switch (HazardDefLatency) { 1846 case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprReadWaitStates; 1847 break; 1848 case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprReadWaitStates; 1849 break; 1850 case 16: LLVM_FALLTHROUGH; 1851 default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprReadWaitStates; 1852 break; 1853 } 1854 } else if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64) { 1855 switch (HazardDefLatency) { 1856 case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprWriteWaitStates; 1857 break; 1858 case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprWriteWaitStates; 1859 break; 1860 case 16: LLVM_FALLTHROUGH; 1861 default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprWriteWaitStates; 1862 break; 1863 } 1864 } 1865 1866 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 1867 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1868 1869 if (WaitStatesNeeded == MaxWaitStates) 1870 return WaitStatesNeeded; // Early exit. 1871 1872 auto IsAccVgprWriteFn = [Reg, this](const MachineInstr &MI) { 1873 if (MI.getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 1874 return false; 1875 Register DstReg = MI.getOperand(0).getReg(); 1876 return TRI.regsOverlap(Reg, DstReg); 1877 }; 1878 1879 const int AccVGPRWriteMFMAReadSrcCWaitStates = 1; 1880 const int AccVGPRWriteMFMAReadSrcABWaitStates = 3; 1881 const int AccVGPRWriteAccVgprReadWaitStates = 3; 1882 NeedWaitStates = AccVGPRWriteMFMAReadSrcABWaitStates; 1883 if (OpNo == SrcCIdx) 1884 NeedWaitStates = AccVGPRWriteMFMAReadSrcCWaitStates; 1885 else if (Opc == AMDGPU::V_ACCVGPR_READ_B32_e64) 1886 NeedWaitStates = AccVGPRWriteAccVgprReadWaitStates; 1887 1888 WaitStatesNeededForUse = NeedWaitStates - 1889 getWaitStatesSinceDef(Reg, IsAccVgprWriteFn, MaxWaitStates); 1890 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1891 1892 if (WaitStatesNeeded == MaxWaitStates) 1893 return WaitStatesNeeded; // Early exit. 1894 } 1895 1896 if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64) { 1897 const int MFMA4x4ReadSrcCAccVgprWriteWaitStates = 0; 1898 const int MFMA16x16ReadSrcCAccVgprWriteWaitStates = 5; 1899 const int MFMA32x32ReadSrcCAccVgprWriteWaitStates = 13; 1900 const int MaxWaitStates = 13; 1901 Register DstReg = MI->getOperand(0).getReg(); 1902 unsigned HazardDefLatency = 0; 1903 1904 auto IsSrcCMFMAFn = [DstReg, &HazardDefLatency, 1905 this](const MachineInstr &MI) { 1906 if (!SIInstrInfo::isMFMA(MI)) 1907 return false; 1908 Register Reg = TII.getNamedOperand(MI, AMDGPU::OpName::src2)->getReg(); 1909 HazardDefLatency = 1910 std::max(HazardDefLatency, TSchedModel.computeInstrLatency(&MI)); 1911 return TRI.regsOverlap(Reg, DstReg); 1912 }; 1913 1914 int WaitStatesSince = getWaitStatesSince(IsSrcCMFMAFn, MaxWaitStates); 1915 int NeedWaitStates; 1916 switch (HazardDefLatency) { 1917 case 2: NeedWaitStates = MFMA4x4ReadSrcCAccVgprWriteWaitStates; 1918 break; 1919 case 8: NeedWaitStates = MFMA16x16ReadSrcCAccVgprWriteWaitStates; 1920 break; 1921 case 16: LLVM_FALLTHROUGH; 1922 default: NeedWaitStates = MFMA32x32ReadSrcCAccVgprWriteWaitStates; 1923 break; 1924 } 1925 1926 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSince; 1927 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1928 } 1929 1930 // Pad neighboring MFMA with noops for better inter-wave performance. 1931 WaitStatesNeeded = std::max(WaitStatesNeeded, checkMFMAPadding(MI)); 1932 1933 return WaitStatesNeeded; 1934 } 1935 1936 int GCNHazardRecognizer::checkMAIHazards90A(MachineInstr *MI) { 1937 int WaitStatesNeeded = 0; 1938 unsigned Opc = MI->getOpcode(); 1939 1940 auto IsLegacyVALUFn = [](const MachineInstr &MI) { 1941 return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMFMA(MI); 1942 }; 1943 1944 auto IsLegacyVALUNotDotFn = [](const MachineInstr &MI) { 1945 return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMFMA(MI) && 1946 !SIInstrInfo::isDOT(MI); 1947 }; 1948 1949 if (!SIInstrInfo::isMFMA(*MI)) 1950 return WaitStatesNeeded; 1951 1952 const int VALUWritesExecWaitStates = 4; 1953 int WaitStatesNeededForUse = VALUWritesExecWaitStates - 1954 getWaitStatesSinceDef(AMDGPU::EXEC, IsLegacyVALUFn, 1955 VALUWritesExecWaitStates); 1956 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 1957 1958 int SrcCIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 1959 1960 // Loop for both DGEMM and S/HGEMM 2nd instruction. 1961 for (const MachineOperand &Use : MI->explicit_uses()) { 1962 const int LegacyVALUNotDotWritesVGPRWaitStates = 2; 1963 const int SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates = 2; 1964 const int GFX940_XDL2PassWritesVGPROverlappedSMFMASrcCWaitStates = 3; 1965 const int GFX940_XDL4PassWritesVGPROverlappedSMFMASrcCWaitStates = 5; 1966 const int GFX940_SMFMA4PassWritesVGPROverlappedSMFMASrcCWaitStates = 4; 1967 const int GFX940_XDL8PassWritesVGPROverlappedSMFMASrcCWaitStates = 9; 1968 const int GFX940_SMFMA8PassWritesVGPROverlappedSMFMASrcCWaitStates = 8; 1969 const int GFX940_XDL16PassWritesVGPROverlappedSMFMASrcCWaitStates = 17; 1970 const int GFX940_SMFMA16PassWritesVGPROverlappedSMFMASrcCWaitStates = 16; 1971 const int SMFMA16x16WritesVGPROverlappedSMFMASrcCWaitStates = 8; 1972 const int SMFMA32x32WritesVGPROverlappedSMFMASrcCWaitStates = 16; 1973 const int SMFMA4x4WritesVGPROverlappedDMFMASrcCWaitStates = 3; 1974 const int SMFMA16x16WritesVGPROverlappedDMFMASrcCWaitStates = 9; 1975 const int SMFMA32x32WritesVGPROverlappedDMFMASrcCWaitStates = 17; 1976 const int DMFMA16x16WritesVGPROverlappedSrcCWaitStates = 9; 1977 const int DMFMA4x4WritesVGPROverlappedSrcCWaitStates = 4; 1978 const int SMFMA4x4WritesVGPROverlappedSrcABWaitStates = 5; 1979 const int SMFMA16x16WritesVGPROverlappedSrcABWaitStates = 11; 1980 const int SMFMA32x32WritesVGPROverlappedSrcABWaitStates = 19; 1981 const int GFX940_SMFMA2PassWritesVGPROverlappedSrcABWaitStates = 4; 1982 const int GFX940_SMFMA4PassWritesVGPROverlappedSrcABWaitStates = 6; 1983 const int GFX940_SMFMA8PassWritesVGPROverlappedSrcABWaitStates = 10; 1984 const int GFX940_SMFMA16PassWritesVGPROverlappedSrcABWaitStates = 18; 1985 const int GFX940_XDL2PassWritesVGPROverlappedSrcABWaitStates = 5; 1986 const int GFX940_XDL4PassWritesVGPROverlappedSrcABWaitStates = 7; 1987 const int GFX940_XDL8PassWritesVGPROverlappedSrcABWaitStates = 11; 1988 const int GFX940_XDL16PassWritesVGPROverlappedSrcABWaitStates = 19; 1989 const int DMFMA4x4WritesVGPROverlappedMFMASrcABWaitStates = 6; 1990 const int DMFMA16x16WritesVGPROverlappedMFMASrcABWaitStates = 11; 1991 const int DMFMA4x4WritesVGPRFullSrcCWaitStates = 4; 1992 const int GFX940_SMFMA4x4WritesVGPRFullSrcCWaitStates = 2; 1993 const int MaxWaitStates = 19; 1994 1995 if (!Use.isReg()) 1996 continue; 1997 Register Reg = Use.getReg(); 1998 bool FullReg; 1999 const MachineInstr *MI1; 2000 2001 auto IsOverlappedMFMAFn = [Reg, &FullReg, &MI1, 2002 this](const MachineInstr &MI) { 2003 if (!SIInstrInfo::isMFMA(MI)) 2004 return false; 2005 Register DstReg = MI.getOperand(0).getReg(); 2006 FullReg = (DstReg == Reg); 2007 MI1 = &MI; 2008 return TRI.regsOverlap(DstReg, Reg); 2009 }; 2010 2011 WaitStatesNeededForUse = LegacyVALUNotDotWritesVGPRWaitStates - 2012 getWaitStatesSinceDef(Reg, IsLegacyVALUNotDotFn, MaxWaitStates); 2013 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2014 2015 int NumWaitStates = 2016 getWaitStatesSinceDef(Reg, IsOverlappedMFMAFn, MaxWaitStates); 2017 if (NumWaitStates == std::numeric_limits<int>::max()) 2018 continue; 2019 2020 int OpNo = MI->getOperandNo(&Use); 2021 unsigned Opc1 = MI1->getOpcode(); 2022 int NeedWaitStates = 0; 2023 if (OpNo == SrcCIdx) { 2024 if (!isDGEMM(Opc) && (!ST.hasGFX940Insts() && isDGEMM(Opc1))) { 2025 NeedWaitStates = 0; 2026 } else if (FullReg) { 2027 if ((Opc == AMDGPU::V_MFMA_F64_4X4X4F64_e64 || 2028 Opc == AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64) && 2029 (Opc1 == AMDGPU::V_MFMA_F64_4X4X4F64_e64 || 2030 Opc1 == AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64)) 2031 NeedWaitStates = DMFMA4x4WritesVGPRFullSrcCWaitStates; 2032 else if (ST.hasGFX940Insts() && 2033 TSchedModel.computeInstrLatency(MI1) == 2) 2034 NeedWaitStates = GFX940_SMFMA4x4WritesVGPRFullSrcCWaitStates; 2035 } else { 2036 switch (Opc1) { 2037 case AMDGPU::V_MFMA_F64_16X16X4F64_e64: 2038 case AMDGPU::V_MFMA_F64_16X16X4F64_vgprcd_e64: 2039 case AMDGPU::V_MFMA_F64_16X16X4F64_mac_e64: 2040 case AMDGPU::V_MFMA_F64_16X16X4F64_mac_vgprcd_e64: 2041 if (!isXDL(ST, *MI)) 2042 NeedWaitStates = DMFMA16x16WritesVGPROverlappedSrcCWaitStates; 2043 break; 2044 case AMDGPU::V_MFMA_F64_4X4X4F64_e64: 2045 case AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64: 2046 if (!isXDL(ST, *MI)) 2047 NeedWaitStates = DMFMA4x4WritesVGPROverlappedSrcCWaitStates; 2048 break; 2049 default: 2050 if (ST.hasGFX940Insts() && isXDL(ST, *MI) && !isXDL(ST, *MI1)) 2051 break; 2052 switch (TSchedModel.computeInstrLatency(MI1)) { 2053 case 2: 2054 NeedWaitStates = ST.hasGFX940Insts() 2055 ? isXDL(ST, *MI1) 2056 ? GFX940_XDL2PassWritesVGPROverlappedSMFMASrcCWaitStates 2057 : SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates 2058 : isDGEMM(Opc) 2059 ? SMFMA4x4WritesVGPROverlappedDMFMASrcCWaitStates 2060 : SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates; 2061 break; 2062 case 4: 2063 assert(ST.hasGFX940Insts()); 2064 NeedWaitStates = isXDL(ST, *MI1) 2065 ? GFX940_XDL4PassWritesVGPROverlappedSMFMASrcCWaitStates 2066 : GFX940_SMFMA4PassWritesVGPROverlappedSMFMASrcCWaitStates; 2067 break; 2068 case 8: 2069 NeedWaitStates = ST.hasGFX940Insts() 2070 ? isXDL(ST, *MI1) 2071 ? GFX940_XDL8PassWritesVGPROverlappedSMFMASrcCWaitStates 2072 : GFX940_SMFMA8PassWritesVGPROverlappedSMFMASrcCWaitStates 2073 : isDGEMM(Opc) 2074 ? SMFMA16x16WritesVGPROverlappedDMFMASrcCWaitStates 2075 : SMFMA16x16WritesVGPROverlappedSMFMASrcCWaitStates; 2076 break; 2077 case 16: LLVM_FALLTHROUGH; 2078 default: 2079 NeedWaitStates = ST.hasGFX940Insts() 2080 ? isXDL(ST, *MI1) 2081 ? GFX940_XDL16PassWritesVGPROverlappedSMFMASrcCWaitStates 2082 : GFX940_SMFMA16PassWritesVGPROverlappedSMFMASrcCWaitStates 2083 : isDGEMM(Opc) 2084 ? SMFMA32x32WritesVGPROverlappedDMFMASrcCWaitStates 2085 : SMFMA32x32WritesVGPROverlappedSMFMASrcCWaitStates; 2086 } 2087 } 2088 } 2089 } else { 2090 switch (Opc1) { 2091 case AMDGPU::V_MFMA_F64_16X16X4F64_e64: 2092 case AMDGPU::V_MFMA_F64_16X16X4F64_vgprcd_e64: 2093 case AMDGPU::V_MFMA_F64_16X16X4F64_mac_e64: 2094 case AMDGPU::V_MFMA_F64_16X16X4F64_mac_vgprcd_e64: 2095 NeedWaitStates = DMFMA16x16WritesVGPROverlappedMFMASrcABWaitStates; 2096 break; 2097 case AMDGPU::V_MFMA_F64_4X4X4F64_e64: 2098 case AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64: 2099 NeedWaitStates = DMFMA4x4WritesVGPROverlappedMFMASrcABWaitStates; 2100 break; 2101 default: 2102 switch (TSchedModel.computeInstrLatency(MI1)) { 2103 case 2: 2104 NeedWaitStates = ST.hasGFX940Insts() 2105 ? isXDL(ST, *MI1) 2106 ? GFX940_XDL2PassWritesVGPROverlappedSrcABWaitStates 2107 : GFX940_SMFMA2PassWritesVGPROverlappedSrcABWaitStates 2108 : SMFMA4x4WritesVGPROverlappedSrcABWaitStates; 2109 break; 2110 case 4: 2111 assert(ST.hasGFX940Insts()); 2112 NeedWaitStates = isXDL(ST, *MI1) 2113 ? GFX940_XDL4PassWritesVGPROverlappedSrcABWaitStates 2114 : GFX940_SMFMA4PassWritesVGPROverlappedSrcABWaitStates; 2115 break; 2116 case 8: 2117 NeedWaitStates = ST.hasGFX940Insts() 2118 ? isXDL(ST, *MI1) 2119 ? GFX940_XDL8PassWritesVGPROverlappedSrcABWaitStates 2120 : GFX940_SMFMA8PassWritesVGPROverlappedSrcABWaitStates 2121 : SMFMA16x16WritesVGPROverlappedSrcABWaitStates; 2122 break; 2123 case 16: LLVM_FALLTHROUGH; 2124 default: 2125 NeedWaitStates = ST.hasGFX940Insts() 2126 ? isXDL(ST, *MI1) 2127 ? GFX940_XDL16PassWritesVGPROverlappedSrcABWaitStates 2128 : GFX940_SMFMA16PassWritesVGPROverlappedSrcABWaitStates 2129 : SMFMA32x32WritesVGPROverlappedSrcABWaitStates; 2130 } 2131 } 2132 } 2133 if (WaitStatesNeeded >= NeedWaitStates) 2134 continue; 2135 2136 WaitStatesNeededForUse = NeedWaitStates - NumWaitStates; 2137 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2138 2139 if (WaitStatesNeeded == MaxWaitStates) 2140 break; 2141 } 2142 2143 return WaitStatesNeeded; 2144 } 2145 2146 int GCNHazardRecognizer::checkMAILdStHazards(MachineInstr *MI) { 2147 // On gfx90a+ relevant hazards are checked in checkMAIVALUHazards() 2148 if (!ST.hasMAIInsts() || ST.hasGFX90AInsts()) 2149 return 0; 2150 2151 int WaitStatesNeeded = 0; 2152 2153 auto IsAccVgprReadFn = [](const MachineInstr &MI) { 2154 return MI.getOpcode() == AMDGPU::V_ACCVGPR_READ_B32_e64; 2155 }; 2156 2157 for (const MachineOperand &Op : MI->explicit_uses()) { 2158 if (!Op.isReg() || !TRI.isVGPR(MF.getRegInfo(), Op.getReg())) 2159 continue; 2160 2161 Register Reg = Op.getReg(); 2162 2163 const int AccVgprReadLdStWaitStates = 2; 2164 const int VALUWriteAccVgprRdWrLdStDepVALUWaitStates = 1; 2165 const int MaxWaitStates = 2; 2166 2167 int WaitStatesNeededForUse = AccVgprReadLdStWaitStates - 2168 getWaitStatesSinceDef(Reg, IsAccVgprReadFn, MaxWaitStates); 2169 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2170 2171 if (WaitStatesNeeded == MaxWaitStates) 2172 return WaitStatesNeeded; // Early exit. 2173 2174 auto IsVALUAccVgprRdWrCheckFn = [Reg, this](const MachineInstr &MI) { 2175 if (MI.getOpcode() != AMDGPU::V_ACCVGPR_READ_B32_e64 && 2176 MI.getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 2177 return false; 2178 auto IsVALUFn = [](const MachineInstr &MI) { 2179 return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMAI(MI); 2180 }; 2181 return getWaitStatesSinceDef(Reg, IsVALUFn, 2 /*MaxWaitStates*/) < 2182 std::numeric_limits<int>::max(); 2183 }; 2184 2185 WaitStatesNeededForUse = VALUWriteAccVgprRdWrLdStDepVALUWaitStates - 2186 getWaitStatesSince(IsVALUAccVgprRdWrCheckFn, MaxWaitStates); 2187 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2188 } 2189 2190 return WaitStatesNeeded; 2191 } 2192 2193 int GCNHazardRecognizer::checkMAIVALUHazards(MachineInstr *MI) { 2194 if (!ST.hasGFX90AInsts()) 2195 return 0; 2196 2197 auto IsDGEMMFn = [](const MachineInstr &MI) -> bool { 2198 return isDGEMM(MI.getOpcode()); 2199 }; 2200 2201 // This is checked in checkMAIHazards90A() 2202 if (SIInstrInfo::isMFMA(*MI)) 2203 return 0; 2204 2205 int WaitStatesNeeded = 0; 2206 2207 bool IsMemOrExport = SIInstrInfo::isVMEM(*MI) || 2208 SIInstrInfo::isFLAT(*MI) || 2209 SIInstrInfo::isDS(*MI) || 2210 SIInstrInfo::isEXP(*MI); 2211 bool IsVALU = SIInstrInfo::isVALU(*MI); 2212 2213 const MachineInstr *MFMA = nullptr; 2214 unsigned Reg; 2215 auto IsMFMAWriteFn = [&Reg, &MFMA, this](const MachineInstr &MI) { 2216 if (!SIInstrInfo::isMFMA(MI) || 2217 !TRI.regsOverlap(MI.getOperand(0).getReg(), Reg)) 2218 return false; 2219 MFMA = &MI; 2220 return true; 2221 }; 2222 2223 const MachineInstr *DOT = nullptr; 2224 auto IsDotWriteFn = [&Reg, &DOT, this](const MachineInstr &MI) { 2225 if (!SIInstrInfo::isDOT(MI) || 2226 !TRI.regsOverlap(MI.getOperand(0).getReg(), Reg)) 2227 return false; 2228 DOT = &MI; 2229 return true; 2230 }; 2231 2232 int SrcCIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), 2233 AMDGPU::OpName::src2); 2234 2235 if (IsMemOrExport || IsVALU) { 2236 const int SMFMA4x4WriteVgprVALUMemExpReadWaitStates = 5; 2237 const int SMFMA16x16WriteVgprVALUMemExpReadWaitStates = 11; 2238 const int SMFMA32x32WriteVgprVALUMemExpReadWaitStates = 19; 2239 const int GFX940_SMFMA2PassWriteVgprVALUMemExpReadWaitStates = 4; 2240 const int GFX940_SMFMA4PassWriteVgprVALUMemExpReadWaitStates = 6; 2241 const int GFX940_SMFMA8PassWriteVgprVALUMemExpReadWaitStates = 10; 2242 const int GFX940_SMFMA16PassWriteVgprVALUMemExpReadWaitStates = 18; 2243 const int GFX940_XDL2PassWriteVgprVALUMemExpReadWaitStates = 5; 2244 const int GFX940_XDL4PassWriteVgprVALUMemExpReadWaitStates = 7; 2245 const int GFX940_XDL8PassWriteVgprVALUMemExpReadWaitStates = 11; 2246 const int GFX940_XDL16PassWriteVgprVALUMemExpReadWaitStates = 19; 2247 const int DMFMA4x4WriteVgprMemExpReadWaitStates = 9; 2248 const int DMFMA16x16WriteVgprMemExpReadWaitStates = 18; 2249 const int DMFMA4x4WriteVgprVALUReadWaitStates = 6; 2250 const int DMFMA16x16WriteVgprVALUReadWaitStates = 11; 2251 const int DotWriteSameDotReadSrcAB = 3; 2252 const int DotWriteDifferentVALURead = 3; 2253 const int MaxWaitStates = 19; 2254 2255 for (const MachineOperand &Use : MI->explicit_uses()) { 2256 if (!Use.isReg()) 2257 continue; 2258 Reg = Use.getReg(); 2259 2260 DOT = nullptr; 2261 int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsDotWriteFn, 2262 MaxWaitStates); 2263 if (DOT) { 2264 int NeedWaitStates = 0; 2265 if (DOT->getOpcode() == MI->getOpcode()) { 2266 if (&Use - &MI->getOperand(0) != SrcCIdx) 2267 NeedWaitStates = DotWriteSameDotReadSrcAB; 2268 } else { 2269 NeedWaitStates = DotWriteDifferentVALURead; 2270 } 2271 2272 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2273 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2274 } 2275 2276 MFMA = nullptr; 2277 WaitStatesSinceDef = 2278 getWaitStatesSinceDef(Reg, IsMFMAWriteFn, MaxWaitStates); 2279 if (!MFMA) 2280 continue; 2281 2282 unsigned HazardDefLatency = TSchedModel.computeInstrLatency(MFMA); 2283 int NeedWaitStates = MaxWaitStates; 2284 switch (HazardDefLatency) { 2285 case 2: 2286 NeedWaitStates = 2287 ST.hasGFX940Insts() 2288 ? isXDL(ST, *MFMA) 2289 ? GFX940_XDL2PassWriteVgprVALUMemExpReadWaitStates 2290 : GFX940_SMFMA2PassWriteVgprVALUMemExpReadWaitStates 2291 : SMFMA4x4WriteVgprVALUMemExpReadWaitStates; 2292 break; 2293 case 4: 2294 assert(isDGEMM(MFMA->getOpcode()) || ST.hasGFX940Insts()); 2295 NeedWaitStates = 2296 isDGEMM(MFMA->getOpcode()) 2297 ? IsMemOrExport ? DMFMA4x4WriteVgprMemExpReadWaitStates 2298 : DMFMA4x4WriteVgprVALUReadWaitStates 2299 : isXDL(ST, *MFMA) 2300 ? GFX940_XDL4PassWriteVgprVALUMemExpReadWaitStates 2301 : GFX940_SMFMA4PassWriteVgprVALUMemExpReadWaitStates; 2302 break; 2303 case 8: 2304 NeedWaitStates = 2305 ST.hasGFX940Insts() 2306 ? isXDL(ST, *MFMA) 2307 ? GFX940_XDL8PassWriteVgprVALUMemExpReadWaitStates 2308 : GFX940_SMFMA8PassWriteVgprVALUMemExpReadWaitStates 2309 : SMFMA16x16WriteVgprVALUMemExpReadWaitStates; 2310 break; 2311 case 16: LLVM_FALLTHROUGH; 2312 default: 2313 NeedWaitStates = 2314 isDGEMM(MFMA->getOpcode()) 2315 ? IsMemOrExport ? DMFMA16x16WriteVgprMemExpReadWaitStates 2316 : DMFMA16x16WriteVgprVALUReadWaitStates 2317 : ST.hasGFX940Insts() 2318 ? isXDL(ST, *MFMA) 2319 ? GFX940_XDL16PassWriteVgprVALUMemExpReadWaitStates 2320 : GFX940_SMFMA16PassWriteVgprVALUMemExpReadWaitStates 2321 : SMFMA32x32WriteVgprVALUMemExpReadWaitStates; 2322 break; 2323 } 2324 2325 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2326 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2327 2328 if (WaitStatesNeeded == MaxWaitStates) 2329 break; 2330 } 2331 } 2332 2333 unsigned Opc = MI->getOpcode(); 2334 const int DMFMAToFMA64WaitStates = 2; 2335 if ((Opc == AMDGPU::V_FMA_F64_e64 || 2336 Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64 || 2337 Opc == AMDGPU::V_FMAC_F64_dpp) && 2338 WaitStatesNeeded < DMFMAToFMA64WaitStates) { 2339 int WaitStatesNeededForUse = DMFMAToFMA64WaitStates - 2340 getWaitStatesSince(IsDGEMMFn, DMFMAToFMA64WaitStates); 2341 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2342 } 2343 2344 if (!IsVALU && !IsMemOrExport) 2345 return WaitStatesNeeded; 2346 2347 for (const MachineOperand &Def : MI->defs()) { 2348 const int SMFMA4x4WriteVgprVALUWawWaitStates = 5; 2349 const int SMFMA16x16WriteVgprVALUWawWaitStates = 11; 2350 const int SMFMA32x32WriteVgprVALUWawWaitStates = 19; 2351 const int GFX940_SMFMA2PassWriteVgprVALUWawWaitStates = 4; 2352 const int GFX940_SMFMA4PassWriteVgprVALUWawWaitStates = 6; 2353 const int GFX940_SMFMA8PassWriteVgprVALUWawWaitStates = 10; 2354 const int GFX940_SMFMA16PassWriteVgprVALUWawWaitStates = 18; 2355 const int GFX940_XDL2PassWriteVgprVALUWawWaitStates = 5; 2356 const int GFX940_XDL4PassWriteVgprVALUWawWaitStates = 7; 2357 const int GFX940_XDL8PassWriteVgprVALUWawWaitStates = 11; 2358 const int GFX940_XDL16PassWriteVgprVALUWawWaitStates = 19; 2359 const int SMFMA4x4ReadVgprVALUWarWaitStates = 1; 2360 const int GFX940_XDL4PassReadVgprVALUWarWaitStates = 3; 2361 const int SMFMA16x16ReadVgprVALUWarWaitStates = 7; 2362 const int SMFMA32x32ReadVgprVALUWarWaitStates = 15; 2363 const int DMFMA4x4WriteVgprVALUWriteWaitStates = 6; 2364 const int DMFMA16x16WriteVgprVALUWriteWaitStates = 11; 2365 const int DotWriteDifferentVALUWrite = 3; 2366 const int MaxWaitStates = 19; 2367 const int MaxWarWaitStates = 15; 2368 2369 Reg = Def.getReg(); 2370 2371 DOT = nullptr; 2372 int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsDotWriteFn, 2373 MaxWaitStates); 2374 if (DOT && DOT->getOpcode() != MI->getOpcode()) 2375 WaitStatesNeeded = std::max(WaitStatesNeeded, DotWriteDifferentVALUWrite - 2376 WaitStatesSinceDef); 2377 2378 MFMA = nullptr; 2379 WaitStatesSinceDef = 2380 getWaitStatesSinceDef(Reg, IsMFMAWriteFn, MaxWaitStates); 2381 if (MFMA) { 2382 int NeedWaitStates = MaxWaitStates; 2383 switch (TSchedModel.computeInstrLatency(MFMA)) { 2384 case 2: 2385 NeedWaitStates = ST.hasGFX940Insts() 2386 ? isXDL(ST, *MFMA) 2387 ? GFX940_XDL2PassWriteVgprVALUWawWaitStates 2388 : GFX940_SMFMA2PassWriteVgprVALUWawWaitStates 2389 : SMFMA4x4WriteVgprVALUWawWaitStates; 2390 break; 2391 case 4: 2392 assert(isDGEMM(MFMA->getOpcode()) || ST.hasGFX940Insts()); 2393 NeedWaitStates = isDGEMM(MFMA->getOpcode()) 2394 ? DMFMA4x4WriteVgprVALUWriteWaitStates 2395 : isXDL(ST, *MFMA) 2396 ? GFX940_XDL4PassWriteVgprVALUWawWaitStates 2397 : GFX940_SMFMA4PassWriteVgprVALUWawWaitStates; 2398 break; 2399 case 8: 2400 NeedWaitStates = ST.hasGFX940Insts() 2401 ? isXDL(ST, *MFMA) 2402 ? GFX940_XDL8PassWriteVgprVALUWawWaitStates 2403 : GFX940_SMFMA8PassWriteVgprVALUWawWaitStates 2404 : SMFMA16x16WriteVgprVALUWawWaitStates; 2405 break; 2406 case 16: LLVM_FALLTHROUGH; 2407 default: 2408 NeedWaitStates = isDGEMM(MFMA->getOpcode()) 2409 ? DMFMA16x16WriteVgprVALUWriteWaitStates 2410 : ST.hasGFX940Insts() 2411 ? isXDL(ST, *MFMA) 2412 ? GFX940_XDL16PassWriteVgprVALUWawWaitStates 2413 : GFX940_SMFMA16PassWriteVgprVALUWawWaitStates 2414 : SMFMA32x32WriteVgprVALUWawWaitStates; 2415 break; 2416 } 2417 2418 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2419 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2420 2421 if (WaitStatesNeeded == MaxWaitStates) 2422 break; 2423 } 2424 2425 auto IsSMFMAReadAsCFn = [&Reg, &MFMA, this](const MachineInstr &MI) { 2426 if (!SIInstrInfo::isMFMA(MI) || isDGEMM(MI.getOpcode()) || 2427 !MI.readsRegister(Reg, &TRI)) 2428 return false; 2429 2430 if (ST.hasGFX940Insts() && !isXDL(ST, MI)) 2431 return false; 2432 2433 const MachineOperand *SrcC = 2434 TII.getNamedOperand(MI, AMDGPU::OpName::src2); 2435 assert(SrcC); 2436 if (!SrcC->isReg() || !TRI.regsOverlap(SrcC->getReg(), Reg)) 2437 return false; 2438 2439 MFMA = &MI; 2440 return true; 2441 }; 2442 2443 MFMA = nullptr; 2444 int WaitStatesSinceUse = getWaitStatesSince(IsSMFMAReadAsCFn, 2445 MaxWarWaitStates); 2446 if (!MFMA) 2447 continue; 2448 2449 unsigned HazardDefLatency = TSchedModel.computeInstrLatency(MFMA); 2450 int NeedWaitStates = MaxWaitStates; 2451 switch (HazardDefLatency) { 2452 case 2: NeedWaitStates = SMFMA4x4ReadVgprVALUWarWaitStates; 2453 break; 2454 case 4: assert(ST.hasGFX940Insts()); 2455 NeedWaitStates = GFX940_XDL4PassReadVgprVALUWarWaitStates; 2456 break; 2457 case 8: NeedWaitStates = SMFMA16x16ReadVgprVALUWarWaitStates; 2458 break; 2459 case 16: LLVM_FALLTHROUGH; 2460 default: NeedWaitStates = SMFMA32x32ReadVgprVALUWarWaitStates; 2461 break; 2462 } 2463 2464 int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceUse; 2465 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2466 } 2467 2468 return WaitStatesNeeded; 2469 } 2470 2471 bool GCNHazardRecognizer::ShouldPreferAnother(SUnit *SU) { 2472 if (!SU->isInstr()) 2473 return false; 2474 2475 const MachineInstr *MAI = nullptr; 2476 2477 auto IsMFMAFn = [&MAI](const MachineInstr &MI) { 2478 MAI = nullptr; 2479 if (SIInstrInfo::isMFMA(MI)) 2480 MAI = &MI; 2481 return MAI != nullptr; 2482 }; 2483 2484 MachineInstr *MI = SU->getInstr(); 2485 if (IsMFMAFn(*MI)) { 2486 int W = getWaitStatesSince(IsMFMAFn, 16); 2487 if (MAI) 2488 return W < (int)TSchedModel.computeInstrLatency(MAI); 2489 } 2490 2491 return false; 2492 } 2493