1 //===-- GCNHazardRecognizers.cpp - GCN Hazard Recognizer Impls ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements hazard recognizers for scheduling on GCN processors. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "AMDGPUSubtarget.h" 15 #include "GCNHazardRecognizer.h" 16 #include "SIDefines.h" 17 #include "SIInstrInfo.h" 18 #include "SIRegisterInfo.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 CurrCycleInstr(nullptr), 41 MF(MF), 42 ST(MF.getSubtarget<SISubtarget>()) { 43 MaxLookAhead = 5; 44 } 45 46 void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { 47 EmitInstruction(SU->getInstr()); 48 } 49 50 void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { 51 CurrCycleInstr = MI; 52 } 53 54 static bool isDivFMas(unsigned Opcode) { 55 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64; 56 } 57 58 static bool isSGetReg(unsigned Opcode) { 59 return Opcode == AMDGPU::S_GETREG_B32; 60 } 61 62 static bool isSSetReg(unsigned Opcode) { 63 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32; 64 } 65 66 static bool isRWLane(unsigned Opcode) { 67 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; 68 } 69 70 static bool isRFE(unsigned Opcode) { 71 return Opcode == AMDGPU::S_RFE_B64; 72 } 73 74 static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { 75 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, 76 AMDGPU::OpName::simm16); 77 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; 78 } 79 80 ScheduleHazardRecognizer::HazardType 81 GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { 82 MachineInstr *MI = SU->getInstr(); 83 84 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) 85 return NoopHazard; 86 87 if (SIInstrInfo::isVMEM(*MI) && checkVMEMHazards(MI) > 0) 88 return NoopHazard; 89 90 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) 91 return NoopHazard; 92 93 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) 94 return NoopHazard; 95 96 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) 97 return NoopHazard; 98 99 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) 100 return NoopHazard; 101 102 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) 103 return NoopHazard; 104 105 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) 106 return NoopHazard; 107 108 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) 109 return NoopHazard; 110 111 return NoHazard; 112 } 113 114 unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) { 115 return PreEmitNoops(SU->getInstr()); 116 } 117 118 unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { 119 if (SIInstrInfo::isSMRD(*MI)) 120 return std::max(0, checkSMRDHazards(MI)); 121 122 if (SIInstrInfo::isVALU(*MI)) { 123 int WaitStates = std::max(0, checkVALUHazards(MI)); 124 125 if (SIInstrInfo::isVMEM(*MI)) 126 WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); 127 128 if (SIInstrInfo::isDPP(*MI)) 129 WaitStates = std::max(WaitStates, checkDPPHazards(MI)); 130 131 if (isDivFMas(MI->getOpcode())) 132 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); 133 134 if (isRWLane(MI->getOpcode())) 135 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); 136 137 return WaitStates; 138 } 139 140 if (isSGetReg(MI->getOpcode())) 141 return std::max(0, checkGetRegHazards(MI)); 142 143 if (isSSetReg(MI->getOpcode())) 144 return std::max(0, checkSetRegHazards(MI)); 145 146 if (isRFE(MI->getOpcode())) 147 return std::max(0, checkRFEHazards(MI)); 148 149 return 0; 150 } 151 152 void GCNHazardRecognizer::EmitNoop() { 153 EmittedInstrs.push_front(nullptr); 154 } 155 156 void GCNHazardRecognizer::AdvanceCycle() { 157 // When the scheduler detects a stall, it will call AdvanceCycle() without 158 // emitting any instructions. 159 if (!CurrCycleInstr) 160 return; 161 162 const SIInstrInfo *TII = ST.getInstrInfo(); 163 unsigned NumWaitStates = TII->getNumWaitStates(*CurrCycleInstr); 164 165 // Keep track of emitted instructions 166 EmittedInstrs.push_front(CurrCycleInstr); 167 168 // Add a nullptr for each additional wait state after the first. Make sure 169 // not to add more than getMaxLookAhead() items to the list, since we 170 // truncate the list to that size right after this loop. 171 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); 172 i < e; ++i) { 173 EmittedInstrs.push_front(nullptr); 174 } 175 176 // getMaxLookahead() is the largest number of wait states we will ever need 177 // to insert, so there is no point in keeping track of more than that many 178 // wait states. 179 EmittedInstrs.resize(getMaxLookAhead()); 180 181 CurrCycleInstr = nullptr; 182 } 183 184 void GCNHazardRecognizer::RecedeCycle() { 185 llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); 186 } 187 188 //===----------------------------------------------------------------------===// 189 // Helper Functions 190 //===----------------------------------------------------------------------===// 191 192 int GCNHazardRecognizer::getWaitStatesSince( 193 function_ref<bool(MachineInstr *)> IsHazard) { 194 int WaitStates = -1; 195 for (MachineInstr *MI : EmittedInstrs) { 196 ++WaitStates; 197 if (!MI || !IsHazard(MI)) 198 continue; 199 return WaitStates; 200 } 201 return std::numeric_limits<int>::max(); 202 } 203 204 int GCNHazardRecognizer::getWaitStatesSinceDef( 205 unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) { 206 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 207 208 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) { 209 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI); 210 }; 211 212 return getWaitStatesSince(IsHazardFn); 213 } 214 215 int GCNHazardRecognizer::getWaitStatesSinceSetReg( 216 function_ref<bool(MachineInstr *)> IsHazard) { 217 auto IsHazardFn = [IsHazard] (MachineInstr *MI) { 218 return isSSetReg(MI->getOpcode()) && IsHazard(MI); 219 }; 220 221 return getWaitStatesSince(IsHazardFn); 222 } 223 224 //===----------------------------------------------------------------------===// 225 // No-op Hazard Detection 226 //===----------------------------------------------------------------------===// 227 228 static void addRegsToSet(iterator_range<MachineInstr::const_mop_iterator> Ops, 229 std::set<unsigned> &Set) { 230 for (const MachineOperand &Op : Ops) { 231 if (Op.isReg()) 232 Set.insert(Op.getReg()); 233 } 234 } 235 236 int GCNHazardRecognizer::checkSMEMSoftClauseHazards(MachineInstr *SMEM) { 237 // SMEM soft clause are only present on VI+ 238 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 239 return 0; 240 241 // A soft-clause is any group of consecutive SMEM instructions. The 242 // instructions in this group may return out of order and/or may be 243 // replayed (i.e. the same instruction issued more than once). 244 // 245 // In order to handle these situations correctly we need to make sure 246 // that when a clause has more than one instruction, no instruction in the 247 // clause writes to a register that is read another instruction in the clause 248 // (including itself). If we encounter this situaion, we need to break the 249 // clause by inserting a non SMEM instruction. 250 251 std::set<unsigned> ClauseDefs; 252 std::set<unsigned> ClauseUses; 253 254 for (MachineInstr *MI : EmittedInstrs) { 255 256 // When we hit a non-SMEM instruction then we have passed the start of the 257 // clause and we can stop. 258 if (!MI || !SIInstrInfo::isSMRD(*MI)) 259 break; 260 261 addRegsToSet(MI->defs(), ClauseDefs); 262 addRegsToSet(MI->uses(), ClauseUses); 263 } 264 265 if (ClauseDefs.empty()) 266 return 0; 267 268 // FIXME: When we support stores, we need to make sure not to put loads and 269 // stores in the same clause if they use the same address. For now, just 270 // start a new clause whenever we see a store. 271 if (SMEM->mayStore()) 272 return 1; 273 274 addRegsToSet(SMEM->defs(), ClauseDefs); 275 addRegsToSet(SMEM->uses(), ClauseUses); 276 277 std::vector<unsigned> Result(std::max(ClauseDefs.size(), ClauseUses.size())); 278 std::vector<unsigned>::iterator End; 279 280 End = std::set_intersection(ClauseDefs.begin(), ClauseDefs.end(), 281 ClauseUses.begin(), ClauseUses.end(), Result.begin()); 282 283 // If the set of defs and uses intersect then we cannot add this instruction 284 // to the clause, so we have a hazard. 285 if (End != Result.begin()) 286 return 1; 287 288 return 0; 289 } 290 291 int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { 292 const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); 293 const SIInstrInfo *TII = ST.getInstrInfo(); 294 int WaitStatesNeeded = 0; 295 296 WaitStatesNeeded = checkSMEMSoftClauseHazards(SMRD); 297 298 // This SMRD hazard only affects SI. 299 if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS) 300 return WaitStatesNeeded; 301 302 // A read of an SGPR by SMRD instruction requires 4 wait states when the 303 // SGPR was written by a VALU instruction. 304 int SmrdSgprWaitStates = 4; 305 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; 306 307 for (const MachineOperand &Use : SMRD->uses()) { 308 if (!Use.isReg()) 309 continue; 310 int WaitStatesNeededForUse = 311 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); 312 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 313 } 314 return WaitStatesNeeded; 315 } 316 317 int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { 318 const SIInstrInfo *TII = ST.getInstrInfo(); 319 320 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 321 return 0; 322 323 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 324 325 // A read of an SGPR by a VMEM instruction requires 5 wait states when the 326 // SGPR was written by a VALU Instruction. 327 int VmemSgprWaitStates = 5; 328 int WaitStatesNeeded = 0; 329 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; 330 331 for (const MachineOperand &Use : VMEM->uses()) { 332 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 333 continue; 334 335 int WaitStatesNeededForUse = 336 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); 337 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 338 } 339 return WaitStatesNeeded; 340 } 341 342 int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { 343 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 344 345 // Check for DPP VGPR read after VALU VGPR write. 346 int DppVgprWaitStates = 2; 347 int WaitStatesNeeded = 0; 348 349 for (const MachineOperand &Use : DPP->uses()) { 350 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 351 continue; 352 int WaitStatesNeededForUse = 353 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg()); 354 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 355 } 356 357 return WaitStatesNeeded; 358 } 359 360 int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { 361 const SIInstrInfo *TII = ST.getInstrInfo(); 362 363 // v_div_fmas requires 4 wait states after a write to vcc from a VALU 364 // instruction. 365 const int DivFMasWaitStates = 4; 366 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; 367 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn); 368 369 return DivFMasWaitStates - WaitStatesNeeded; 370 } 371 372 int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { 373 const SIInstrInfo *TII = ST.getInstrInfo(); 374 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); 375 376 const int GetRegWaitStates = 2; 377 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) { 378 return GetRegHWReg == getHWReg(TII, *MI); 379 }; 380 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); 381 382 return GetRegWaitStates - WaitStatesNeeded; 383 } 384 385 int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { 386 const SIInstrInfo *TII = ST.getInstrInfo(); 387 unsigned HWReg = getHWReg(TII, *SetRegInstr); 388 389 const int SetRegWaitStates = 390 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2; 391 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) { 392 return HWReg == getHWReg(TII, *MI); 393 }; 394 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); 395 return SetRegWaitStates - WaitStatesNeeded; 396 } 397 398 int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { 399 if (!MI.mayStore()) 400 return -1; 401 402 const SIInstrInfo *TII = ST.getInstrInfo(); 403 unsigned Opcode = MI.getOpcode(); 404 const MCInstrDesc &Desc = MI.getDesc(); 405 406 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 407 int VDataRCID = -1; 408 if (VDataIdx != -1) 409 VDataRCID = Desc.OpInfo[VDataIdx].RegClass; 410 411 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { 412 // There is no hazard if the instruction does not use vector regs 413 // (like wbinvl1) 414 if (VDataIdx == -1) 415 return -1; 416 // For MUBUF/MTBUF instructions this hazard only exists if the 417 // instruction is not using a register in the soffset field. 418 const MachineOperand *SOffset = 419 TII->getNamedOperand(MI, AMDGPU::OpName::soffset); 420 // If we have no soffset operand, then assume this field has been 421 // hardcoded to zero. 422 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && 423 (!SOffset || !SOffset->isReg())) 424 return VDataIdx; 425 } 426 427 // MIMG instructions create a hazard if they don't use a 256-bit T# and 428 // the store size is greater than 8 bytes and they have more than two bits 429 // of their dmask set. 430 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. 431 if (TII->isMIMG(MI)) { 432 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 433 assert(SRsrcIdx != -1 && 434 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); 435 (void)SRsrcIdx; 436 } 437 438 if (TII->isFLAT(MI)) { 439 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 440 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) 441 return DataIdx; 442 } 443 444 return -1; 445 } 446 447 int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { 448 // This checks for the hazard where VMEM instructions that store more than 449 // 8 bytes can have there store data over written by the next instruction. 450 if (!ST.has12DWordStoreHazard()) 451 return 0; 452 453 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 454 const MachineRegisterInfo &MRI = VALU->getParent()->getParent()->getRegInfo(); 455 456 const int VALUWaitStates = 1; 457 int WaitStatesNeeded = 0; 458 459 for (const MachineOperand &Def : VALU->defs()) { 460 if (!TRI->isVGPR(MRI, Def.getReg())) 461 continue; 462 unsigned Reg = Def.getReg(); 463 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) { 464 int DataIdx = createsVALUHazard(*MI); 465 return DataIdx >= 0 && 466 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg); 467 }; 468 int WaitStatesNeededForDef = 469 VALUWaitStates - getWaitStatesSince(IsHazardFn); 470 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 471 } 472 return WaitStatesNeeded; 473 } 474 475 int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { 476 const SIInstrInfo *TII = ST.getInstrInfo(); 477 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 478 const MachineRegisterInfo &MRI = 479 RWLane->getParent()->getParent()->getRegInfo(); 480 481 const MachineOperand *LaneSelectOp = 482 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); 483 484 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) 485 return 0; 486 487 unsigned LaneSelectReg = LaneSelectOp->getReg(); 488 auto IsHazardFn = [TII] (MachineInstr *MI) { 489 return TII->isVALU(*MI); 490 }; 491 492 const int RWLaneWaitStates = 4; 493 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn); 494 return RWLaneWaitStates - WaitStatesSince; 495 } 496 497 int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { 498 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 499 return 0; 500 501 const SIInstrInfo *TII = ST.getInstrInfo(); 502 503 const int RFEWaitStates = 1; 504 505 auto IsHazardFn = [TII] (MachineInstr *MI) { 506 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS; 507 }; 508 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); 509 return RFEWaitStates - WaitStatesNeeded; 510 } 511