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