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