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/MachineOperand.h" 24 #include "llvm/CodeGen/ScheduleDAG.h" 25 #include "llvm/MC/MCInstrDesc.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include <algorithm> 28 #include <cassert> 29 #include <limits> 30 #include <set> 31 #include <vector> 32 33 using namespace llvm; 34 35 //===----------------------------------------------------------------------===// 36 // Hazard Recoginizer Implementation 37 //===----------------------------------------------------------------------===// 38 39 GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) : 40 IsHazardRecognizerMode(false), 41 CurrCycleInstr(nullptr), 42 MF(MF), 43 ST(MF.getSubtarget<GCNSubtarget>()), 44 TII(*ST.getInstrInfo()), 45 TRI(TII.getRegisterInfo()), 46 ClauseUses(TRI.getNumRegUnits()), 47 ClauseDefs(TRI.getNumRegUnits()) { 48 MaxLookAhead = 5; 49 } 50 51 void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { 52 EmitInstruction(SU->getInstr()); 53 } 54 55 void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { 56 CurrCycleInstr = MI; 57 } 58 59 static bool isDivFMas(unsigned Opcode) { 60 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64; 61 } 62 63 static bool isSGetReg(unsigned Opcode) { 64 return Opcode == AMDGPU::S_GETREG_B32; 65 } 66 67 static bool isSSetReg(unsigned Opcode) { 68 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32; 69 } 70 71 static bool isRWLane(unsigned Opcode) { 72 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; 73 } 74 75 static bool isRFE(unsigned Opcode) { 76 return Opcode == AMDGPU::S_RFE_B64; 77 } 78 79 static bool isSMovRel(unsigned Opcode) { 80 switch (Opcode) { 81 case AMDGPU::S_MOVRELS_B32: 82 case AMDGPU::S_MOVRELS_B64: 83 case AMDGPU::S_MOVRELD_B32: 84 case AMDGPU::S_MOVRELD_B64: 85 return true; 86 default: 87 return false; 88 } 89 } 90 91 static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII, 92 const MachineInstr &MI) { 93 if (TII.isAlwaysGDS(MI.getOpcode())) 94 return true; 95 96 switch (MI.getOpcode()) { 97 case AMDGPU::S_SENDMSG: 98 case AMDGPU::S_SENDMSGHALT: 99 case AMDGPU::S_TTRACEDATA: 100 return true; 101 // These DS opcodes don't support GDS. 102 case AMDGPU::DS_NOP: 103 case AMDGPU::DS_PERMUTE_B32: 104 case AMDGPU::DS_BPERMUTE_B32: 105 return false; 106 default: 107 if (TII.isDS(MI.getOpcode())) { 108 int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(), 109 AMDGPU::OpName::gds); 110 if (MI.getOperand(GDS).getImm()) 111 return true; 112 } 113 return false; 114 } 115 } 116 117 static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { 118 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, 119 AMDGPU::OpName::simm16); 120 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; 121 } 122 123 ScheduleHazardRecognizer::HazardType 124 GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { 125 MachineInstr *MI = SU->getInstr(); 126 127 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) 128 return NoopHazard; 129 130 // FIXME: Should flat be considered vmem? 131 if ((SIInstrInfo::isVMEM(*MI) || 132 SIInstrInfo::isFLAT(*MI)) 133 && checkVMEMHazards(MI) > 0) 134 return NoopHazard; 135 136 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) 137 return NoopHazard; 138 139 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) 140 return NoopHazard; 141 142 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) 143 return NoopHazard; 144 145 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) 146 return NoopHazard; 147 148 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) 149 return NoopHazard; 150 151 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) 152 return NoopHazard; 153 154 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) 155 return NoopHazard; 156 157 if (ST.hasReadM0MovRelInterpHazard() && 158 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) && 159 checkReadM0Hazards(MI) > 0) 160 return NoopHazard; 161 162 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI) && 163 checkReadM0Hazards(MI) > 0) 164 return NoopHazard; 165 166 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0) 167 return NoopHazard; 168 169 if (checkAnyInstHazards(MI) > 0) 170 return NoopHazard; 171 172 return NoHazard; 173 } 174 175 unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) { 176 IsHazardRecognizerMode = false; 177 return PreEmitNoopsCommon(SU->getInstr()); 178 } 179 180 unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { 181 IsHazardRecognizerMode = true; 182 CurrCycleInstr = MI; 183 unsigned W = PreEmitNoopsCommon(MI); 184 CurrCycleInstr = nullptr; 185 return W; 186 } 187 188 unsigned GCNHazardRecognizer::PreEmitNoopsCommon(MachineInstr *MI) { 189 int WaitStates = std::max(0, checkAnyInstHazards(MI)); 190 191 if (SIInstrInfo::isSMRD(*MI)) 192 return std::max(WaitStates, checkSMRDHazards(MI)); 193 194 if (SIInstrInfo::isVALU(*MI)) 195 WaitStates = std::max(WaitStates, checkVALUHazards(MI)); 196 197 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI)) 198 WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); 199 200 if (SIInstrInfo::isDPP(*MI)) 201 WaitStates = std::max(WaitStates, checkDPPHazards(MI)); 202 203 if (isDivFMas(MI->getOpcode())) 204 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); 205 206 if (isRWLane(MI->getOpcode())) 207 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); 208 209 if (MI->isInlineAsm()) 210 return std::max(WaitStates, checkInlineAsmHazards(MI)); 211 212 if (isSGetReg(MI->getOpcode())) 213 return std::max(WaitStates, checkGetRegHazards(MI)); 214 215 if (isSSetReg(MI->getOpcode())) 216 return std::max(WaitStates, checkSetRegHazards(MI)); 217 218 if (isRFE(MI->getOpcode())) 219 return std::max(WaitStates, checkRFEHazards(MI)); 220 221 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) || 222 isSMovRel(MI->getOpcode()))) 223 return std::max(WaitStates, checkReadM0Hazards(MI)); 224 225 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI)) 226 return std::max(WaitStates, checkReadM0Hazards(MI)); 227 228 return WaitStates; 229 } 230 231 void GCNHazardRecognizer::EmitNoop() { 232 EmittedInstrs.push_front(nullptr); 233 } 234 235 void GCNHazardRecognizer::AdvanceCycle() { 236 // When the scheduler detects a stall, it will call AdvanceCycle() without 237 // emitting any instructions. 238 if (!CurrCycleInstr) 239 return; 240 241 // Do not track non-instructions which do not affect the wait states. 242 // If included, these instructions can lead to buffer overflow such that 243 // detectable hazards are missed. 244 if (CurrCycleInstr->isImplicitDef() || CurrCycleInstr->isDebugInstr() || 245 CurrCycleInstr->isKill()) 246 return; 247 248 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr); 249 250 // Keep track of emitted instructions 251 EmittedInstrs.push_front(CurrCycleInstr); 252 253 // Add a nullptr for each additional wait state after the first. Make sure 254 // not to add more than getMaxLookAhead() items to the list, since we 255 // truncate the list to that size right after this loop. 256 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); 257 i < e; ++i) { 258 EmittedInstrs.push_front(nullptr); 259 } 260 261 // getMaxLookahead() is the largest number of wait states we will ever need 262 // to insert, so there is no point in keeping track of more than that many 263 // wait states. 264 EmittedInstrs.resize(getMaxLookAhead()); 265 266 CurrCycleInstr = nullptr; 267 } 268 269 void GCNHazardRecognizer::RecedeCycle() { 270 llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); 271 } 272 273 //===----------------------------------------------------------------------===// 274 // Helper Functions 275 //===----------------------------------------------------------------------===// 276 277 typedef function_ref<bool(MachineInstr *, int WaitStates)> IsExpiredFn; 278 279 // Returns a minimum wait states since \p I walking all predecessors. 280 // Only scans until \p IsExpired does not return true. 281 // Can only be run in a hazard recognizer mode. 282 static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, 283 MachineBasicBlock *MBB, 284 MachineBasicBlock::reverse_instr_iterator I, 285 int WaitStates, 286 IsExpiredFn IsExpired, 287 DenseSet<const MachineBasicBlock *> &Visited) { 288 289 for (auto E = MBB->rend() ; I != E; ++I) { 290 if (IsHazard(&*I)) 291 return WaitStates; 292 293 if (I->isInlineAsm() || I->isImplicitDef() || I->isDebugInstr()) 294 continue; 295 296 WaitStates += SIInstrInfo::getNumWaitStates(*I); 297 298 if (IsExpired(&*I, WaitStates)) 299 return std::numeric_limits<int>::max(); 300 } 301 302 int MinWaitStates = WaitStates; 303 bool Found = false; 304 for (MachineBasicBlock *Pred : MBB->predecessors()) { 305 if (!Visited.insert(Pred).second) 306 continue; 307 308 int W = getWaitStatesSince(IsHazard, Pred, Pred->instr_rbegin(), 309 WaitStates, IsExpired, Visited); 310 311 if (W == std::numeric_limits<int>::max()) 312 continue; 313 314 MinWaitStates = Found ? std::min(MinWaitStates, W) : W; 315 if (IsExpired(nullptr, MinWaitStates)) 316 return MinWaitStates; 317 318 Found = true; 319 } 320 321 if (Found) 322 return MinWaitStates; 323 324 return std::numeric_limits<int>::max(); 325 } 326 327 static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, 328 MachineInstr *MI, 329 IsExpiredFn IsExpired) { 330 DenseSet<const MachineBasicBlock *> Visited; 331 return getWaitStatesSince(IsHazard, MI->getParent(), 332 std::next(MI->getReverseIterator()), 333 0, IsExpired, Visited); 334 } 335 336 int GCNHazardRecognizer::getWaitStatesSince(IsHazardFn IsHazard, int Limit) { 337 if (IsHazardRecognizerMode) { 338 auto IsExpiredFn = [Limit] (MachineInstr *, int WaitStates) { 339 return WaitStates >= Limit; 340 }; 341 return ::getWaitStatesSince(IsHazard, CurrCycleInstr, IsExpiredFn); 342 } 343 344 int WaitStates = 0; 345 for (MachineInstr *MI : EmittedInstrs) { 346 if (MI) { 347 if (IsHazard(MI)) 348 return WaitStates; 349 350 if (MI->isInlineAsm()) 351 continue; 352 } 353 ++WaitStates; 354 355 if (WaitStates >= Limit) 356 break; 357 } 358 return std::numeric_limits<int>::max(); 359 } 360 361 int GCNHazardRecognizer::getWaitStatesSinceDef(unsigned Reg, 362 IsHazardFn IsHazardDef, 363 int Limit) { 364 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 365 366 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) { 367 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI); 368 }; 369 370 return getWaitStatesSince(IsHazardFn, Limit); 371 } 372 373 int GCNHazardRecognizer::getWaitStatesSinceSetReg(IsHazardFn IsHazard, 374 int Limit) { 375 auto IsHazardFn = [IsHazard] (MachineInstr *MI) { 376 return isSSetReg(MI->getOpcode()) && IsHazard(MI); 377 }; 378 379 return getWaitStatesSince(IsHazardFn, Limit); 380 } 381 382 //===----------------------------------------------------------------------===// 383 // No-op Hazard Detection 384 //===----------------------------------------------------------------------===// 385 386 static void addRegUnits(const SIRegisterInfo &TRI, 387 BitVector &BV, unsigned Reg) { 388 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) 389 BV.set(*RUI); 390 } 391 392 static void addRegsToSet(const SIRegisterInfo &TRI, 393 iterator_range<MachineInstr::const_mop_iterator> Ops, 394 BitVector &Set) { 395 for (const MachineOperand &Op : Ops) { 396 if (Op.isReg()) 397 addRegUnits(TRI, Set, Op.getReg()); 398 } 399 } 400 401 void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) { 402 // XXX: Do we need to worry about implicit operands 403 addRegsToSet(TRI, MI.defs(), ClauseDefs); 404 addRegsToSet(TRI, MI.uses(), ClauseUses); 405 } 406 407 int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) { 408 // SMEM soft clause are only present on VI+, and only matter if xnack is 409 // enabled. 410 if (!ST.isXNACKEnabled()) 411 return 0; 412 413 bool IsSMRD = TII.isSMRD(*MEM); 414 415 resetClause(); 416 417 // A soft-clause is any group of consecutive SMEM instructions. The 418 // instructions in this group may return out of order and/or may be 419 // replayed (i.e. the same instruction issued more than once). 420 // 421 // In order to handle these situations correctly we need to make sure 422 // that when a clause has more than one instruction, no instruction in the 423 // clause writes to a register that is read another instruction in the clause 424 // (including itself). If we encounter this situaion, we need to break the 425 // clause by inserting a non SMEM instruction. 426 427 for (MachineInstr *MI : EmittedInstrs) { 428 // When we hit a non-SMEM instruction then we have passed the start of the 429 // clause and we can stop. 430 if (!MI) 431 break; 432 433 if (IsSMRD != SIInstrInfo::isSMRD(*MI)) 434 break; 435 436 addClauseInst(*MI); 437 } 438 439 if (ClauseDefs.none()) 440 return 0; 441 442 // We need to make sure not to put loads and stores in the same clause if they 443 // use the same address. For now, just start a new clause whenever we see a 444 // store. 445 if (MEM->mayStore()) 446 return 1; 447 448 addClauseInst(*MEM); 449 450 // If the set of defs and uses intersect then we cannot add this instruction 451 // to the clause, so we have a hazard. 452 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0; 453 } 454 455 int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { 456 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 457 int WaitStatesNeeded = 0; 458 459 WaitStatesNeeded = checkSoftClauseHazards(SMRD); 460 461 // This SMRD hazard only affects SI. 462 if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS) 463 return WaitStatesNeeded; 464 465 // A read of an SGPR by SMRD instruction requires 4 wait states when the 466 // SGPR was written by a VALU instruction. 467 int SmrdSgprWaitStates = 4; 468 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; 469 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); }; 470 471 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD); 472 473 for (const MachineOperand &Use : SMRD->uses()) { 474 if (!Use.isReg()) 475 continue; 476 int WaitStatesNeededForUse = 477 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 478 SmrdSgprWaitStates); 479 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 480 481 // This fixes what appears to be undocumented hardware behavior in SI where 482 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor 483 // needs some number of nops in between. We don't know how many we need, but 484 // let's use 4. This wasn't discovered before probably because the only 485 // case when this happens is when we expand a 64-bit pointer into a full 486 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was 487 // probably never encountered in the closed-source land. 488 if (IsBufferSMRD) { 489 int WaitStatesNeededForUse = 490 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), 491 IsBufferHazardDefFn, 492 SmrdSgprWaitStates); 493 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 494 } 495 } 496 497 return WaitStatesNeeded; 498 } 499 500 int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { 501 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 502 return 0; 503 504 int WaitStatesNeeded = checkSoftClauseHazards(VMEM); 505 506 // A read of an SGPR by a VMEM instruction requires 5 wait states when the 507 // SGPR was written by a VALU Instruction. 508 const int VmemSgprWaitStates = 5; 509 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; 510 511 for (const MachineOperand &Use : VMEM->uses()) { 512 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 513 continue; 514 515 int WaitStatesNeededForUse = 516 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 517 VmemSgprWaitStates); 518 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 519 } 520 return WaitStatesNeeded; 521 } 522 523 int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { 524 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 525 const SIInstrInfo *TII = ST.getInstrInfo(); 526 527 // Check for DPP VGPR read after VALU VGPR write and EXEC write. 528 int DppVgprWaitStates = 2; 529 int DppExecWaitStates = 5; 530 int WaitStatesNeeded = 0; 531 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; 532 533 for (const MachineOperand &Use : DPP->uses()) { 534 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 535 continue; 536 int WaitStatesNeededForUse = 537 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg(), 538 [](MachineInstr *) { return true; }, 539 DppVgprWaitStates); 540 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 541 } 542 543 WaitStatesNeeded = std::max( 544 WaitStatesNeeded, 545 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn, 546 DppExecWaitStates)); 547 548 return WaitStatesNeeded; 549 } 550 551 int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { 552 const SIInstrInfo *TII = ST.getInstrInfo(); 553 554 // v_div_fmas requires 4 wait states after a write to vcc from a VALU 555 // instruction. 556 const int DivFMasWaitStates = 4; 557 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; 558 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn, 559 DivFMasWaitStates); 560 561 return DivFMasWaitStates - WaitStatesNeeded; 562 } 563 564 int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { 565 const SIInstrInfo *TII = ST.getInstrInfo(); 566 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); 567 568 const int GetRegWaitStates = 2; 569 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) { 570 return GetRegHWReg == getHWReg(TII, *MI); 571 }; 572 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates); 573 574 return GetRegWaitStates - WaitStatesNeeded; 575 } 576 577 int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { 578 const SIInstrInfo *TII = ST.getInstrInfo(); 579 unsigned HWReg = getHWReg(TII, *SetRegInstr); 580 581 const int SetRegWaitStates = 582 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2; 583 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) { 584 return HWReg == getHWReg(TII, *MI); 585 }; 586 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates); 587 return SetRegWaitStates - WaitStatesNeeded; 588 } 589 590 int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { 591 if (!MI.mayStore()) 592 return -1; 593 594 const SIInstrInfo *TII = ST.getInstrInfo(); 595 unsigned Opcode = MI.getOpcode(); 596 const MCInstrDesc &Desc = MI.getDesc(); 597 598 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 599 int VDataRCID = -1; 600 if (VDataIdx != -1) 601 VDataRCID = Desc.OpInfo[VDataIdx].RegClass; 602 603 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { 604 // There is no hazard if the instruction does not use vector regs 605 // (like wbinvl1) 606 if (VDataIdx == -1) 607 return -1; 608 // For MUBUF/MTBUF instructions this hazard only exists if the 609 // instruction is not using a register in the soffset field. 610 const MachineOperand *SOffset = 611 TII->getNamedOperand(MI, AMDGPU::OpName::soffset); 612 // If we have no soffset operand, then assume this field has been 613 // hardcoded to zero. 614 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && 615 (!SOffset || !SOffset->isReg())) 616 return VDataIdx; 617 } 618 619 // MIMG instructions create a hazard if they don't use a 256-bit T# and 620 // the store size is greater than 8 bytes and they have more than two bits 621 // of their dmask set. 622 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. 623 if (TII->isMIMG(MI)) { 624 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 625 assert(SRsrcIdx != -1 && 626 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); 627 (void)SRsrcIdx; 628 } 629 630 if (TII->isFLAT(MI)) { 631 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 632 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) 633 return DataIdx; 634 } 635 636 return -1; 637 } 638 639 int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def, 640 const MachineRegisterInfo &MRI) { 641 // Helper to check for the hazard where VMEM instructions that store more than 642 // 8 bytes can have there store data over written by the next instruction. 643 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 644 645 const int VALUWaitStates = 1; 646 int WaitStatesNeeded = 0; 647 648 if (!TRI->isVGPR(MRI, Def.getReg())) 649 return WaitStatesNeeded; 650 unsigned Reg = Def.getReg(); 651 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) { 652 int DataIdx = createsVALUHazard(*MI); 653 return DataIdx >= 0 && 654 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg); 655 }; 656 int WaitStatesNeededForDef = 657 VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates); 658 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 659 660 return WaitStatesNeeded; 661 } 662 663 int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { 664 // This checks for the hazard where VMEM instructions that store more than 665 // 8 bytes can have there store data over written by the next instruction. 666 if (!ST.has12DWordStoreHazard()) 667 return 0; 668 669 const MachineRegisterInfo &MRI = MF.getRegInfo(); 670 int WaitStatesNeeded = 0; 671 672 for (const MachineOperand &Def : VALU->defs()) { 673 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI)); 674 } 675 676 return WaitStatesNeeded; 677 } 678 679 int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) { 680 // This checks for hazards associated with inline asm statements. 681 // Since inline asms can contain just about anything, we use this 682 // to call/leverage other check*Hazard routines. Note that 683 // this function doesn't attempt to address all possible inline asm 684 // hazards (good luck), but is a collection of what has been 685 // problematic thus far. 686 687 // see checkVALUHazards() 688 if (!ST.has12DWordStoreHazard()) 689 return 0; 690 691 const MachineRegisterInfo &MRI = MF.getRegInfo(); 692 int WaitStatesNeeded = 0; 693 694 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands(); 695 I != E; ++I) { 696 const MachineOperand &Op = IA->getOperand(I); 697 if (Op.isReg() && Op.isDef()) { 698 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI)); 699 } 700 } 701 702 return WaitStatesNeeded; 703 } 704 705 int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { 706 const SIInstrInfo *TII = ST.getInstrInfo(); 707 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 708 const MachineRegisterInfo &MRI = MF.getRegInfo(); 709 710 const MachineOperand *LaneSelectOp = 711 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); 712 713 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) 714 return 0; 715 716 unsigned LaneSelectReg = LaneSelectOp->getReg(); 717 auto IsHazardFn = [TII] (MachineInstr *MI) { 718 return TII->isVALU(*MI); 719 }; 720 721 const int RWLaneWaitStates = 4; 722 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn, 723 RWLaneWaitStates); 724 return RWLaneWaitStates - WaitStatesSince; 725 } 726 727 int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { 728 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 729 return 0; 730 731 const SIInstrInfo *TII = ST.getInstrInfo(); 732 733 const int RFEWaitStates = 1; 734 735 auto IsHazardFn = [TII] (MachineInstr *MI) { 736 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS; 737 }; 738 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates); 739 return RFEWaitStates - WaitStatesNeeded; 740 } 741 742 int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) { 743 if (MI->isDebugInstr()) 744 return 0; 745 746 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 747 if (!ST.hasSMovFedHazard()) 748 return 0; 749 750 // Check for any instruction reading an SGPR after a write from 751 // s_mov_fed_b32. 752 int MovFedWaitStates = 1; 753 int WaitStatesNeeded = 0; 754 755 for (const MachineOperand &Use : MI->uses()) { 756 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 757 continue; 758 auto IsHazardFn = [] (MachineInstr *MI) { 759 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32; 760 }; 761 int WaitStatesNeededForUse = 762 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn, 763 MovFedWaitStates); 764 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 765 } 766 767 return WaitStatesNeeded; 768 } 769 770 int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { 771 const SIInstrInfo *TII = ST.getInstrInfo(); 772 const int SMovRelWaitStates = 1; 773 auto IsHazardFn = [TII] (MachineInstr *MI) { 774 return TII->isSALU(*MI); 775 }; 776 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn, 777 SMovRelWaitStates); 778 } 779