10b57cec5SDimitry Andric //===-- GCNHazardRecognizers.cpp - GCN Hazard Recognizer Impls ------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements hazard recognizers for scheduling on GCN processors. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "GCNHazardRecognizer.h" 14e8d8bef9SDimitry Andric #include "GCNSubtarget.h" 150b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 1681ad6265SDimitry Andric #include "SIMachineFunctionInfo.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/ScheduleDAG.h" 19e8d8bef9SDimitry Andric #include "llvm/Support/TargetParser.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace llvm; 220b57cec5SDimitry Andric 2381ad6265SDimitry Andric namespace { 2481ad6265SDimitry Andric 2581ad6265SDimitry Andric struct MFMAPaddingRatioParser : public cl::parser<unsigned> { 2681ad6265SDimitry Andric MFMAPaddingRatioParser(cl::Option &O) : cl::parser<unsigned>(O) {} 2781ad6265SDimitry Andric 2881ad6265SDimitry Andric bool parse(cl::Option &O, StringRef ArgName, StringRef Arg, unsigned &Value) { 2981ad6265SDimitry Andric if (Arg.getAsInteger(0, Value)) 3081ad6265SDimitry Andric return O.error("'" + Arg + "' value invalid for uint argument!"); 3181ad6265SDimitry Andric 3281ad6265SDimitry Andric if (Value > 100) 3381ad6265SDimitry Andric return O.error("'" + Arg + "' value must be in the range [0, 100]!"); 3481ad6265SDimitry Andric 3581ad6265SDimitry Andric return false; 3681ad6265SDimitry Andric } 3781ad6265SDimitry Andric }; 3881ad6265SDimitry Andric 3981ad6265SDimitry Andric } // end anonymous namespace 4081ad6265SDimitry Andric 4181ad6265SDimitry Andric static cl::opt<unsigned, false, MFMAPaddingRatioParser> 4281ad6265SDimitry Andric MFMAPaddingRatio("amdgpu-mfma-padding-ratio", cl::init(0), cl::Hidden, 4381ad6265SDimitry Andric cl::desc("Fill a percentage of the latency between " 4481ad6265SDimitry Andric "neighboring MFMA with s_nops.")); 4581ad6265SDimitry Andric 460b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4781ad6265SDimitry Andric // Hazard Recognizer Implementation 480b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 490b57cec5SDimitry Andric 50fe6060f1SDimitry Andric static bool shouldRunLdsBranchVmemWARHazardFixup(const MachineFunction &MF, 51fe6060f1SDimitry Andric const GCNSubtarget &ST); 52fe6060f1SDimitry Andric 530b57cec5SDimitry Andric GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) : 540b57cec5SDimitry Andric IsHazardRecognizerMode(false), 550b57cec5SDimitry Andric CurrCycleInstr(nullptr), 560b57cec5SDimitry Andric MF(MF), 570b57cec5SDimitry Andric ST(MF.getSubtarget<GCNSubtarget>()), 580b57cec5SDimitry Andric TII(*ST.getInstrInfo()), 590b57cec5SDimitry Andric TRI(TII.getRegisterInfo()), 600b57cec5SDimitry Andric ClauseUses(TRI.getNumRegUnits()), 610b57cec5SDimitry Andric ClauseDefs(TRI.getNumRegUnits()) { 62fe6060f1SDimitry Andric MaxLookAhead = MF.getRegInfo().isPhysRegUsed(AMDGPU::AGPR0) ? 19 : 5; 630b57cec5SDimitry Andric TSchedModel.init(&ST); 64fe6060f1SDimitry Andric RunLdsBranchVmemWARHazardFixup = shouldRunLdsBranchVmemWARHazardFixup(MF, ST); 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric 67e8d8bef9SDimitry Andric void GCNHazardRecognizer::Reset() { 68e8d8bef9SDimitry Andric EmittedInstrs.clear(); 69e8d8bef9SDimitry Andric } 70e8d8bef9SDimitry Andric 710b57cec5SDimitry Andric void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { 720b57cec5SDimitry Andric EmitInstruction(SU->getInstr()); 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { 760b57cec5SDimitry Andric CurrCycleInstr = MI; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric static bool isDivFMas(unsigned Opcode) { 80e8d8bef9SDimitry Andric return Opcode == AMDGPU::V_DIV_FMAS_F32_e64 || Opcode == AMDGPU::V_DIV_FMAS_F64_e64; 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric static bool isSGetReg(unsigned Opcode) { 840b57cec5SDimitry Andric return Opcode == AMDGPU::S_GETREG_B32; 850b57cec5SDimitry Andric } 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric static bool isSSetReg(unsigned Opcode) { 88e8d8bef9SDimitry Andric switch (Opcode) { 89e8d8bef9SDimitry Andric case AMDGPU::S_SETREG_B32: 90e8d8bef9SDimitry Andric case AMDGPU::S_SETREG_B32_mode: 91e8d8bef9SDimitry Andric case AMDGPU::S_SETREG_IMM32_B32: 92e8d8bef9SDimitry Andric case AMDGPU::S_SETREG_IMM32_B32_mode: 93e8d8bef9SDimitry Andric return true; 94e8d8bef9SDimitry Andric } 95e8d8bef9SDimitry Andric return false; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric static bool isRWLane(unsigned Opcode) { 990b57cec5SDimitry Andric return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric static bool isRFE(unsigned Opcode) { 1030b57cec5SDimitry Andric return Opcode == AMDGPU::S_RFE_B64; 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric static bool isSMovRel(unsigned Opcode) { 1070b57cec5SDimitry Andric switch (Opcode) { 1080b57cec5SDimitry Andric case AMDGPU::S_MOVRELS_B32: 1090b57cec5SDimitry Andric case AMDGPU::S_MOVRELS_B64: 1100b57cec5SDimitry Andric case AMDGPU::S_MOVRELD_B32: 1110b57cec5SDimitry Andric case AMDGPU::S_MOVRELD_B64: 1120b57cec5SDimitry Andric return true; 1130b57cec5SDimitry Andric default: 1140b57cec5SDimitry Andric return false; 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 118fe6060f1SDimitry Andric static bool isDGEMM(unsigned Opcode) { 11981ad6265SDimitry Andric return AMDGPU::getMAIIsDGEMM(Opcode); 120fe6060f1SDimitry Andric } 121fe6060f1SDimitry Andric 122fe6060f1SDimitry Andric static bool isXDL(const GCNSubtarget &ST, const MachineInstr &MI) { 123fe6060f1SDimitry Andric unsigned Opcode = MI.getOpcode(); 124fe6060f1SDimitry Andric 125fe6060f1SDimitry Andric if (!SIInstrInfo::isMAI(MI) || 126fe6060f1SDimitry Andric isDGEMM(Opcode) || 127fe6060f1SDimitry Andric Opcode == AMDGPU::V_ACCVGPR_WRITE_B32_e64 || 128fe6060f1SDimitry Andric Opcode == AMDGPU::V_ACCVGPR_READ_B32_e64) 129fe6060f1SDimitry Andric return false; 130fe6060f1SDimitry Andric 13181ad6265SDimitry Andric if (!ST.hasGFX940Insts()) 132fe6060f1SDimitry Andric return true; 13381ad6265SDimitry Andric 13481ad6265SDimitry Andric return AMDGPU::getMAIIsGFX940XDL(Opcode); 135fe6060f1SDimitry Andric } 136fe6060f1SDimitry Andric 1370b57cec5SDimitry Andric static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII, 1380b57cec5SDimitry Andric const MachineInstr &MI) { 1390b57cec5SDimitry Andric if (TII.isAlwaysGDS(MI.getOpcode())) 1400b57cec5SDimitry Andric return true; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric switch (MI.getOpcode()) { 1430b57cec5SDimitry Andric case AMDGPU::S_SENDMSG: 1440b57cec5SDimitry Andric case AMDGPU::S_SENDMSGHALT: 1450b57cec5SDimitry Andric case AMDGPU::S_TTRACEDATA: 1460b57cec5SDimitry Andric return true; 1470b57cec5SDimitry Andric // These DS opcodes don't support GDS. 1480b57cec5SDimitry Andric case AMDGPU::DS_NOP: 1490b57cec5SDimitry Andric case AMDGPU::DS_PERMUTE_B32: 1500b57cec5SDimitry Andric case AMDGPU::DS_BPERMUTE_B32: 1510b57cec5SDimitry Andric return false; 1520b57cec5SDimitry Andric default: 1530b57cec5SDimitry Andric if (TII.isDS(MI.getOpcode())) { 1540b57cec5SDimitry Andric int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(), 1550b57cec5SDimitry Andric AMDGPU::OpName::gds); 1560b57cec5SDimitry Andric if (MI.getOperand(GDS).getImm()) 1570b57cec5SDimitry Andric return true; 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric return false; 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric static bool isPermlane(const MachineInstr &MI) { 1640b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode(); 165e8d8bef9SDimitry Andric return Opcode == AMDGPU::V_PERMLANE16_B32_e64 || 166e8d8bef9SDimitry Andric Opcode == AMDGPU::V_PERMLANEX16_B32_e64; 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 16981ad6265SDimitry Andric static bool isLdsDma(const MachineInstr &MI) { 17081ad6265SDimitry Andric return SIInstrInfo::isVALU(MI) && 17181ad6265SDimitry Andric (SIInstrInfo::isMUBUF(MI) || SIInstrInfo::isFLAT(MI)); 17281ad6265SDimitry Andric } 17381ad6265SDimitry Andric 1740b57cec5SDimitry Andric static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { 1750b57cec5SDimitry Andric const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, 1760b57cec5SDimitry Andric AMDGPU::OpName::simm16); 1770b57cec5SDimitry Andric return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric ScheduleHazardRecognizer::HazardType 1810b57cec5SDimitry Andric GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { 1820b57cec5SDimitry Andric MachineInstr *MI = SU->getInstr(); 183e8d8bef9SDimitry Andric // If we are not in "HazardRecognizerMode" and therefore not being run from 184e8d8bef9SDimitry Andric // the scheduler, track possible stalls from hazards but don't insert noops. 185e8d8bef9SDimitry Andric auto HazardType = IsHazardRecognizerMode ? NoopHazard : Hazard; 186e8d8bef9SDimitry Andric 1870b57cec5SDimitry Andric if (MI->isBundle()) 1880b57cec5SDimitry Andric return NoHazard; 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) 191e8d8bef9SDimitry Andric return HazardType; 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric if (ST.hasNSAtoVMEMBug() && checkNSAtoVMEMHazard(MI) > 0) 194e8d8bef9SDimitry Andric return HazardType; 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric if (checkFPAtomicToDenormModeHazard(MI) > 0) 197e8d8bef9SDimitry Andric return HazardType; 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric if (ST.hasNoDataDepHazard()) 2000b57cec5SDimitry Andric return NoHazard; 2010b57cec5SDimitry Andric 202fe6060f1SDimitry Andric // FIXME: Should flat be considered vmem? 203fe6060f1SDimitry Andric if ((SIInstrInfo::isVMEM(*MI) || 204fe6060f1SDimitry Andric SIInstrInfo::isFLAT(*MI)) 205fe6060f1SDimitry Andric && checkVMEMHazards(MI) > 0) 206fe6060f1SDimitry Andric return HazardType; 207fe6060f1SDimitry Andric 2080b57cec5SDimitry Andric if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) 209e8d8bef9SDimitry Andric return HazardType; 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) 212e8d8bef9SDimitry Andric return HazardType; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) 215e8d8bef9SDimitry Andric return HazardType; 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) 218e8d8bef9SDimitry Andric return HazardType; 2190b57cec5SDimitry Andric 220fe6060f1SDimitry Andric if ((SIInstrInfo::isVALU(*MI) || SIInstrInfo::isVMEM(*MI) || 221fe6060f1SDimitry Andric SIInstrInfo::isFLAT(*MI) || SIInstrInfo::isDS(*MI) || 222fe6060f1SDimitry Andric SIInstrInfo::isEXP(*MI)) && checkMAIVALUHazards(MI) > 0) 223fe6060f1SDimitry Andric return HazardType; 224fe6060f1SDimitry Andric 2250b57cec5SDimitry Andric if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) 226e8d8bef9SDimitry Andric return HazardType; 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) 229e8d8bef9SDimitry Andric return HazardType; 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) 232e8d8bef9SDimitry Andric return HazardType; 2330b57cec5SDimitry Andric 23481ad6265SDimitry Andric if (((ST.hasReadM0MovRelInterpHazard() && 235*bdd1243dSDimitry Andric (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()) || 236*bdd1243dSDimitry Andric MI->getOpcode() == AMDGPU::DS_WRITE_ADDTID_B32 || 237*bdd1243dSDimitry Andric MI->getOpcode() == AMDGPU::DS_READ_ADDTID_B32)) || 23881ad6265SDimitry Andric (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI)) || 23981ad6265SDimitry Andric (ST.hasReadM0LdsDmaHazard() && isLdsDma(*MI)) || 24081ad6265SDimitry Andric (ST.hasReadM0LdsDirectHazard() && 24181ad6265SDimitry Andric MI->readsRegister(AMDGPU::LDS_DIRECT))) && 2420b57cec5SDimitry Andric checkReadM0Hazards(MI) > 0) 243e8d8bef9SDimitry Andric return HazardType; 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric if (SIInstrInfo::isMAI(*MI) && checkMAIHazards(MI) > 0) 246e8d8bef9SDimitry Andric return HazardType; 2470b57cec5SDimitry Andric 248e8d8bef9SDimitry Andric if ((SIInstrInfo::isVMEM(*MI) || 249e8d8bef9SDimitry Andric SIInstrInfo::isFLAT(*MI) || 250e8d8bef9SDimitry Andric SIInstrInfo::isDS(*MI)) && checkMAILdStHazards(MI) > 0) 251e8d8bef9SDimitry Andric return HazardType; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0) 254e8d8bef9SDimitry Andric return HazardType; 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric return NoHazard; 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 259e8d8bef9SDimitry Andric static void insertNoopsInBundle(MachineInstr *MI, const SIInstrInfo &TII, 260e8d8bef9SDimitry Andric unsigned Quantity) { 261e8d8bef9SDimitry Andric while (Quantity > 0) { 262e8d8bef9SDimitry Andric unsigned Arg = std::min(Quantity, 8u); 263e8d8bef9SDimitry Andric Quantity -= Arg; 2640b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII.get(AMDGPU::S_NOP)) 265e8d8bef9SDimitry Andric .addImm(Arg - 1); 266e8d8bef9SDimitry Andric } 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric 26981ad6265SDimitry Andric unsigned 27081ad6265SDimitry Andric GCNHazardRecognizer::getMFMAPipelineWaitStates(const MachineInstr &MI) const { 27181ad6265SDimitry Andric const MCSchedClassDesc *SC = TSchedModel.resolveSchedClass(&MI); 27281ad6265SDimitry Andric assert(TSchedModel.getWriteProcResBegin(SC) != 27381ad6265SDimitry Andric TSchedModel.getWriteProcResEnd(SC)); 27481ad6265SDimitry Andric return TSchedModel.getWriteProcResBegin(SC)->Cycles; 27581ad6265SDimitry Andric } 27681ad6265SDimitry Andric 2770b57cec5SDimitry Andric void GCNHazardRecognizer::processBundle() { 2780b57cec5SDimitry Andric MachineBasicBlock::instr_iterator MI = std::next(CurrCycleInstr->getIterator()); 2790b57cec5SDimitry Andric MachineBasicBlock::instr_iterator E = CurrCycleInstr->getParent()->instr_end(); 2800b57cec5SDimitry Andric // Check bundled MachineInstr's for hazards. 2810b57cec5SDimitry Andric for (; MI != E && MI->isInsideBundle(); ++MI) { 2820b57cec5SDimitry Andric CurrCycleInstr = &*MI; 2830b57cec5SDimitry Andric unsigned WaitStates = PreEmitNoopsCommon(CurrCycleInstr); 2840b57cec5SDimitry Andric 285e8d8bef9SDimitry Andric if (IsHazardRecognizerMode) { 2860b57cec5SDimitry Andric fixHazards(CurrCycleInstr); 2870b57cec5SDimitry Andric 288e8d8bef9SDimitry Andric insertNoopsInBundle(CurrCycleInstr, TII, WaitStates); 289e8d8bef9SDimitry Andric } 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric // It’s unnecessary to track more than MaxLookAhead instructions. Since we 2920b57cec5SDimitry Andric // include the bundled MI directly after, only add a maximum of 2930b57cec5SDimitry Andric // (MaxLookAhead - 1) noops to EmittedInstrs. 2940b57cec5SDimitry Andric for (unsigned i = 0, e = std::min(WaitStates, MaxLookAhead - 1); i < e; ++i) 2950b57cec5SDimitry Andric EmittedInstrs.push_front(nullptr); 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric EmittedInstrs.push_front(CurrCycleInstr); 2980b57cec5SDimitry Andric EmittedInstrs.resize(MaxLookAhead); 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric CurrCycleInstr = nullptr; 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric 303*bdd1243dSDimitry Andric void GCNHazardRecognizer::runOnInstruction(MachineInstr *MI) { 304*bdd1243dSDimitry Andric assert(IsHazardRecognizerMode); 305*bdd1243dSDimitry Andric 306*bdd1243dSDimitry Andric unsigned NumPreNoops = PreEmitNoops(MI); 307*bdd1243dSDimitry Andric EmitNoops(NumPreNoops); 308*bdd1243dSDimitry Andric if (MI->isInsideBundle()) 309*bdd1243dSDimitry Andric insertNoopsInBundle(MI, TII, NumPreNoops); 310*bdd1243dSDimitry Andric else 311*bdd1243dSDimitry Andric TII.insertNoops(*MI->getParent(), MachineBasicBlock::iterator(MI), 312*bdd1243dSDimitry Andric NumPreNoops); 313*bdd1243dSDimitry Andric EmitInstruction(MI); 314*bdd1243dSDimitry Andric AdvanceCycle(); 315*bdd1243dSDimitry Andric } 316*bdd1243dSDimitry Andric 3170b57cec5SDimitry Andric unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { 3180b57cec5SDimitry Andric IsHazardRecognizerMode = true; 3190b57cec5SDimitry Andric CurrCycleInstr = MI; 3200b57cec5SDimitry Andric unsigned W = PreEmitNoopsCommon(MI); 3210b57cec5SDimitry Andric fixHazards(MI); 3220b57cec5SDimitry Andric CurrCycleInstr = nullptr; 3230b57cec5SDimitry Andric return W; 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric unsigned GCNHazardRecognizer::PreEmitNoopsCommon(MachineInstr *MI) { 3270b57cec5SDimitry Andric if (MI->isBundle()) 3280b57cec5SDimitry Andric return 0; 3290b57cec5SDimitry Andric 330e8d8bef9SDimitry Andric int WaitStates = 0; 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric if (SIInstrInfo::isSMRD(*MI)) 3330b57cec5SDimitry Andric return std::max(WaitStates, checkSMRDHazards(MI)); 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric if (ST.hasNSAtoVMEMBug()) 3360b57cec5SDimitry Andric WaitStates = std::max(WaitStates, checkNSAtoVMEMHazard(MI)); 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric WaitStates = std::max(WaitStates, checkFPAtomicToDenormModeHazard(MI)); 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric if (ST.hasNoDataDepHazard()) 3410b57cec5SDimitry Andric return WaitStates; 3420b57cec5SDimitry Andric 343fe6060f1SDimitry Andric if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI)) 344fe6060f1SDimitry Andric WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); 345fe6060f1SDimitry Andric 3460b57cec5SDimitry Andric if (SIInstrInfo::isVALU(*MI)) 3470b57cec5SDimitry Andric WaitStates = std::max(WaitStates, checkVALUHazards(MI)); 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric if (SIInstrInfo::isDPP(*MI)) 3500b57cec5SDimitry Andric WaitStates = std::max(WaitStates, checkDPPHazards(MI)); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric if (isDivFMas(MI->getOpcode())) 3530b57cec5SDimitry Andric WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric if (isRWLane(MI->getOpcode())) 3560b57cec5SDimitry Andric WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); 3570b57cec5SDimitry Andric 358fe6060f1SDimitry Andric if ((SIInstrInfo::isVALU(*MI) || SIInstrInfo::isVMEM(*MI) || 359fe6060f1SDimitry Andric SIInstrInfo::isFLAT(*MI) || SIInstrInfo::isDS(*MI) || 360fe6060f1SDimitry Andric SIInstrInfo::isEXP(*MI)) && checkMAIVALUHazards(MI) > 0) 361fe6060f1SDimitry Andric WaitStates = std::max(WaitStates, checkMAIVALUHazards(MI)); 362fe6060f1SDimitry Andric 3630b57cec5SDimitry Andric if (MI->isInlineAsm()) 3640b57cec5SDimitry Andric return std::max(WaitStates, checkInlineAsmHazards(MI)); 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric if (isSGetReg(MI->getOpcode())) 3670b57cec5SDimitry Andric return std::max(WaitStates, checkGetRegHazards(MI)); 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric if (isSSetReg(MI->getOpcode())) 3700b57cec5SDimitry Andric return std::max(WaitStates, checkSetRegHazards(MI)); 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric if (isRFE(MI->getOpcode())) 3730b57cec5SDimitry Andric return std::max(WaitStates, checkRFEHazards(MI)); 3740b57cec5SDimitry Andric 37581ad6265SDimitry Andric if ((ST.hasReadM0MovRelInterpHazard() && 376*bdd1243dSDimitry Andric (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()) || 377*bdd1243dSDimitry Andric MI->getOpcode() == AMDGPU::DS_WRITE_ADDTID_B32 || 378*bdd1243dSDimitry Andric MI->getOpcode() == AMDGPU::DS_READ_ADDTID_B32)) || 37981ad6265SDimitry Andric (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI)) || 38081ad6265SDimitry Andric (ST.hasReadM0LdsDmaHazard() && isLdsDma(*MI)) || 38181ad6265SDimitry Andric (ST.hasReadM0LdsDirectHazard() && MI->readsRegister(AMDGPU::LDS_DIRECT))) 3820b57cec5SDimitry Andric return std::max(WaitStates, checkReadM0Hazards(MI)); 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric if (SIInstrInfo::isMAI(*MI)) 3850b57cec5SDimitry Andric return std::max(WaitStates, checkMAIHazards(MI)); 3860b57cec5SDimitry Andric 387e8d8bef9SDimitry Andric if (SIInstrInfo::isVMEM(*MI) || 388e8d8bef9SDimitry Andric SIInstrInfo::isFLAT(*MI) || 389e8d8bef9SDimitry Andric SIInstrInfo::isDS(*MI)) 3900b57cec5SDimitry Andric return std::max(WaitStates, checkMAILdStHazards(MI)); 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric return WaitStates; 3930b57cec5SDimitry Andric } 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric void GCNHazardRecognizer::EmitNoop() { 3960b57cec5SDimitry Andric EmittedInstrs.push_front(nullptr); 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric void GCNHazardRecognizer::AdvanceCycle() { 4000b57cec5SDimitry Andric // When the scheduler detects a stall, it will call AdvanceCycle() without 4010b57cec5SDimitry Andric // emitting any instructions. 402e8d8bef9SDimitry Andric if (!CurrCycleInstr) { 403e8d8bef9SDimitry Andric EmittedInstrs.push_front(nullptr); 4040b57cec5SDimitry Andric return; 405e8d8bef9SDimitry Andric } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric if (CurrCycleInstr->isBundle()) { 4080b57cec5SDimitry Andric processBundle(); 4090b57cec5SDimitry Andric return; 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr); 413349cc55cSDimitry Andric if (!NumWaitStates) { 414349cc55cSDimitry Andric CurrCycleInstr = nullptr; 415349cc55cSDimitry Andric return; 416349cc55cSDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric // Keep track of emitted instructions 4190b57cec5SDimitry Andric EmittedInstrs.push_front(CurrCycleInstr); 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Add a nullptr for each additional wait state after the first. Make sure 4220b57cec5SDimitry Andric // not to add more than getMaxLookAhead() items to the list, since we 4230b57cec5SDimitry Andric // truncate the list to that size right after this loop. 4240b57cec5SDimitry Andric for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); 4250b57cec5SDimitry Andric i < e; ++i) { 4260b57cec5SDimitry Andric EmittedInstrs.push_front(nullptr); 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric // getMaxLookahead() is the largest number of wait states we will ever need 4300b57cec5SDimitry Andric // to insert, so there is no point in keeping track of more than that many 4310b57cec5SDimitry Andric // wait states. 4320b57cec5SDimitry Andric EmittedInstrs.resize(getMaxLookAhead()); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric CurrCycleInstr = nullptr; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric void GCNHazardRecognizer::RecedeCycle() { 4380b57cec5SDimitry Andric llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4420b57cec5SDimitry Andric // Helper Functions 4430b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4440b57cec5SDimitry Andric 44581ad6265SDimitry Andric typedef enum { HazardFound, HazardExpired, NoHazardFound } HazardFnResult; 44681ad6265SDimitry Andric 447fe6060f1SDimitry Andric typedef function_ref<bool(const MachineInstr &, int WaitStates)> IsExpiredFn; 44881ad6265SDimitry Andric typedef function_ref<unsigned int(const MachineInstr &)> GetNumWaitStatesFn; 44981ad6265SDimitry Andric 45081ad6265SDimitry Andric // Search for a hazard in a block and its predecessors. 45181ad6265SDimitry Andric template <typename StateT> 45281ad6265SDimitry Andric static bool 45381ad6265SDimitry Andric hasHazard(StateT State, 45481ad6265SDimitry Andric function_ref<HazardFnResult(StateT &, const MachineInstr &)> IsHazard, 45581ad6265SDimitry Andric function_ref<void(StateT &, const MachineInstr &)> UpdateState, 45681ad6265SDimitry Andric const MachineBasicBlock *MBB, 45781ad6265SDimitry Andric MachineBasicBlock::const_reverse_instr_iterator I, 45881ad6265SDimitry Andric DenseSet<const MachineBasicBlock *> &Visited) { 45981ad6265SDimitry Andric for (auto E = MBB->instr_rend(); I != E; ++I) { 46081ad6265SDimitry Andric // No need to look at parent BUNDLE instructions. 46181ad6265SDimitry Andric if (I->isBundle()) 46281ad6265SDimitry Andric continue; 46381ad6265SDimitry Andric 46481ad6265SDimitry Andric switch (IsHazard(State, *I)) { 46581ad6265SDimitry Andric case HazardFound: 46681ad6265SDimitry Andric return true; 46781ad6265SDimitry Andric case HazardExpired: 46881ad6265SDimitry Andric return false; 46981ad6265SDimitry Andric default: 47081ad6265SDimitry Andric // Continue search 47181ad6265SDimitry Andric break; 47281ad6265SDimitry Andric } 47381ad6265SDimitry Andric 47481ad6265SDimitry Andric if (I->isInlineAsm() || I->isMetaInstruction()) 47581ad6265SDimitry Andric continue; 47681ad6265SDimitry Andric 47781ad6265SDimitry Andric UpdateState(State, *I); 47881ad6265SDimitry Andric } 47981ad6265SDimitry Andric 48081ad6265SDimitry Andric for (MachineBasicBlock *Pred : MBB->predecessors()) { 48181ad6265SDimitry Andric if (!Visited.insert(Pred).second) 48281ad6265SDimitry Andric continue; 48381ad6265SDimitry Andric 48481ad6265SDimitry Andric if (hasHazard(State, IsHazard, UpdateState, Pred, Pred->instr_rbegin(), 48581ad6265SDimitry Andric Visited)) 48681ad6265SDimitry Andric return true; 48781ad6265SDimitry Andric } 48881ad6265SDimitry Andric 48981ad6265SDimitry Andric return false; 49081ad6265SDimitry Andric } 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric // Returns a minimum wait states since \p I walking all predecessors. 4930b57cec5SDimitry Andric // Only scans until \p IsExpired does not return true. 4940b57cec5SDimitry Andric // Can only be run in a hazard recognizer mode. 49581ad6265SDimitry Andric static int getWaitStatesSince( 49681ad6265SDimitry Andric GCNHazardRecognizer::IsHazardFn IsHazard, const MachineBasicBlock *MBB, 49781ad6265SDimitry Andric MachineBasicBlock::const_reverse_instr_iterator I, int WaitStates, 49881ad6265SDimitry Andric IsExpiredFn IsExpired, DenseSet<const MachineBasicBlock *> &Visited, 49981ad6265SDimitry Andric GetNumWaitStatesFn GetNumWaitStates = SIInstrInfo::getNumWaitStates) { 5000b57cec5SDimitry Andric for (auto E = MBB->instr_rend(); I != E; ++I) { 5010b57cec5SDimitry Andric // Don't add WaitStates for parent BUNDLE instructions. 5020b57cec5SDimitry Andric if (I->isBundle()) 5030b57cec5SDimitry Andric continue; 5040b57cec5SDimitry Andric 505fe6060f1SDimitry Andric if (IsHazard(*I)) 5060b57cec5SDimitry Andric return WaitStates; 5070b57cec5SDimitry Andric 508349cc55cSDimitry Andric if (I->isInlineAsm()) 5090b57cec5SDimitry Andric continue; 5100b57cec5SDimitry Andric 51181ad6265SDimitry Andric WaitStates += GetNumWaitStates(*I); 5120b57cec5SDimitry Andric 513fe6060f1SDimitry Andric if (IsExpired(*I, WaitStates)) 5140b57cec5SDimitry Andric return std::numeric_limits<int>::max(); 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric 517fe6060f1SDimitry Andric int MinWaitStates = std::numeric_limits<int>::max(); 5180b57cec5SDimitry Andric for (MachineBasicBlock *Pred : MBB->predecessors()) { 5190b57cec5SDimitry Andric if (!Visited.insert(Pred).second) 5200b57cec5SDimitry Andric continue; 5210b57cec5SDimitry Andric 52281ad6265SDimitry Andric int W = getWaitStatesSince(IsHazard, Pred, Pred->instr_rbegin(), WaitStates, 52381ad6265SDimitry Andric IsExpired, Visited, GetNumWaitStates); 5240b57cec5SDimitry Andric 525fe6060f1SDimitry Andric MinWaitStates = std::min(MinWaitStates, W); 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric return MinWaitStates; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, 532fe6060f1SDimitry Andric const MachineInstr *MI, IsExpiredFn IsExpired) { 5330b57cec5SDimitry Andric DenseSet<const MachineBasicBlock *> Visited; 5340b57cec5SDimitry Andric return getWaitStatesSince(IsHazard, MI->getParent(), 5350b57cec5SDimitry Andric std::next(MI->getReverseIterator()), 5360b57cec5SDimitry Andric 0, IsExpired, Visited); 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric int GCNHazardRecognizer::getWaitStatesSince(IsHazardFn IsHazard, int Limit) { 5400b57cec5SDimitry Andric if (IsHazardRecognizerMode) { 541fe6060f1SDimitry Andric auto IsExpiredFn = [Limit](const MachineInstr &, int WaitStates) { 5420b57cec5SDimitry Andric return WaitStates >= Limit; 5430b57cec5SDimitry Andric }; 5440b57cec5SDimitry Andric return ::getWaitStatesSince(IsHazard, CurrCycleInstr, IsExpiredFn); 5450b57cec5SDimitry Andric } 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric int WaitStates = 0; 5480b57cec5SDimitry Andric for (MachineInstr *MI : EmittedInstrs) { 5490b57cec5SDimitry Andric if (MI) { 550fe6060f1SDimitry Andric if (IsHazard(*MI)) 5510b57cec5SDimitry Andric return WaitStates; 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric if (MI->isInlineAsm()) 5540b57cec5SDimitry Andric continue; 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric ++WaitStates; 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric if (WaitStates >= Limit) 5590b57cec5SDimitry Andric break; 5600b57cec5SDimitry Andric } 5610b57cec5SDimitry Andric return std::numeric_limits<int>::max(); 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric int GCNHazardRecognizer::getWaitStatesSinceDef(unsigned Reg, 5650b57cec5SDimitry Andric IsHazardFn IsHazardDef, 5660b57cec5SDimitry Andric int Limit) { 5670b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 5680b57cec5SDimitry Andric 569fe6060f1SDimitry Andric auto IsHazardFn = [IsHazardDef, TRI, Reg](const MachineInstr &MI) { 570fe6060f1SDimitry Andric return IsHazardDef(MI) && MI.modifiesRegister(Reg, TRI); 5710b57cec5SDimitry Andric }; 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric return getWaitStatesSince(IsHazardFn, Limit); 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric int GCNHazardRecognizer::getWaitStatesSinceSetReg(IsHazardFn IsHazard, 5770b57cec5SDimitry Andric int Limit) { 578fe6060f1SDimitry Andric auto IsHazardFn = [IsHazard](const MachineInstr &MI) { 579fe6060f1SDimitry Andric return isSSetReg(MI.getOpcode()) && IsHazard(MI); 5800b57cec5SDimitry Andric }; 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric return getWaitStatesSince(IsHazardFn, Limit); 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 5860b57cec5SDimitry Andric // No-op Hazard Detection 5870b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 5880b57cec5SDimitry Andric 589e8d8bef9SDimitry Andric static void addRegUnits(const SIRegisterInfo &TRI, BitVector &BV, 590e8d8bef9SDimitry Andric MCRegister Reg) { 5910b57cec5SDimitry Andric for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) 5920b57cec5SDimitry Andric BV.set(*RUI); 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric static void addRegsToSet(const SIRegisterInfo &TRI, 5960b57cec5SDimitry Andric iterator_range<MachineInstr::const_mop_iterator> Ops, 5970b57cec5SDimitry Andric BitVector &Set) { 5980b57cec5SDimitry Andric for (const MachineOperand &Op : Ops) { 5990b57cec5SDimitry Andric if (Op.isReg()) 600e8d8bef9SDimitry Andric addRegUnits(TRI, Set, Op.getReg().asMCReg()); 6010b57cec5SDimitry Andric } 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) { 6050b57cec5SDimitry Andric // XXX: Do we need to worry about implicit operands 6060b57cec5SDimitry Andric addRegsToSet(TRI, MI.defs(), ClauseDefs); 6070b57cec5SDimitry Andric addRegsToSet(TRI, MI.uses(), ClauseUses); 6080b57cec5SDimitry Andric } 6090b57cec5SDimitry Andric 6105ffd83dbSDimitry Andric static bool breaksSMEMSoftClause(MachineInstr *MI) { 6115ffd83dbSDimitry Andric return !SIInstrInfo::isSMRD(*MI); 6125ffd83dbSDimitry Andric } 6135ffd83dbSDimitry Andric 6145ffd83dbSDimitry Andric static bool breaksVMEMSoftClause(MachineInstr *MI) { 6155ffd83dbSDimitry Andric return !SIInstrInfo::isVMEM(*MI) && !SIInstrInfo::isFLAT(*MI); 6165ffd83dbSDimitry Andric } 6175ffd83dbSDimitry Andric 6180b57cec5SDimitry Andric int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) { 6190b57cec5SDimitry Andric // SMEM soft clause are only present on VI+, and only matter if xnack is 6200b57cec5SDimitry Andric // enabled. 6210b57cec5SDimitry Andric if (!ST.isXNACKEnabled()) 6220b57cec5SDimitry Andric return 0; 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric bool IsSMRD = TII.isSMRD(*MEM); 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric resetClause(); 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric // A soft-clause is any group of consecutive SMEM instructions. The 6290b57cec5SDimitry Andric // instructions in this group may return out of order and/or may be 6300b57cec5SDimitry Andric // replayed (i.e. the same instruction issued more than once). 6310b57cec5SDimitry Andric // 6320b57cec5SDimitry Andric // In order to handle these situations correctly we need to make sure that 6330b57cec5SDimitry Andric // when a clause has more than one instruction, no instruction in the clause 6340b57cec5SDimitry Andric // writes to a register that is read by another instruction in the clause 63581ad6265SDimitry Andric // (including itself). If we encounter this situation, we need to break the 6360b57cec5SDimitry Andric // clause by inserting a non SMEM instruction. 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric for (MachineInstr *MI : EmittedInstrs) { 6390b57cec5SDimitry Andric // When we hit a non-SMEM instruction then we have passed the start of the 6400b57cec5SDimitry Andric // clause and we can stop. 6410b57cec5SDimitry Andric if (!MI) 6420b57cec5SDimitry Andric break; 6430b57cec5SDimitry Andric 6445ffd83dbSDimitry Andric if (IsSMRD ? breaksSMEMSoftClause(MI) : breaksVMEMSoftClause(MI)) 6450b57cec5SDimitry Andric break; 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric addClauseInst(*MI); 6480b57cec5SDimitry Andric } 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric if (ClauseDefs.none()) 6510b57cec5SDimitry Andric return 0; 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric // We need to make sure not to put loads and stores in the same clause if they 6540b57cec5SDimitry Andric // use the same address. For now, just start a new clause whenever we see a 6550b57cec5SDimitry Andric // store. 6560b57cec5SDimitry Andric if (MEM->mayStore()) 6570b57cec5SDimitry Andric return 1; 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric addClauseInst(*MEM); 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric // If the set of defs and uses intersect then we cannot add this instruction 6620b57cec5SDimitry Andric // to the clause, so we have a hazard. 6630b57cec5SDimitry Andric return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0; 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { 6670b57cec5SDimitry Andric int WaitStatesNeeded = 0; 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric WaitStatesNeeded = checkSoftClauseHazards(SMRD); 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric // This SMRD hazard only affects SI. 6720b57cec5SDimitry Andric if (!ST.hasSMRDReadVALUDefHazard()) 6730b57cec5SDimitry Andric return WaitStatesNeeded; 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric // A read of an SGPR by SMRD instruction requires 4 wait states when the 6760b57cec5SDimitry Andric // SGPR was written by a VALU instruction. 6770b57cec5SDimitry Andric int SmrdSgprWaitStates = 4; 678fe6060f1SDimitry Andric auto IsHazardDefFn = [this](const MachineInstr &MI) { 679fe6060f1SDimitry Andric return TII.isVALU(MI); 680fe6060f1SDimitry Andric }; 681fe6060f1SDimitry Andric auto IsBufferHazardDefFn = [this](const MachineInstr &MI) { 682fe6060f1SDimitry Andric return TII.isSALU(MI); 683fe6060f1SDimitry Andric }; 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric bool IsBufferSMRD = TII.isBufferSMRD(*SMRD); 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric for (const MachineOperand &Use : SMRD->uses()) { 6880b57cec5SDimitry Andric if (!Use.isReg()) 6890b57cec5SDimitry Andric continue; 6900b57cec5SDimitry Andric int WaitStatesNeededForUse = 6910b57cec5SDimitry Andric SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 6920b57cec5SDimitry Andric SmrdSgprWaitStates); 6930b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric // This fixes what appears to be undocumented hardware behavior in SI where 6960b57cec5SDimitry Andric // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor 6970b57cec5SDimitry Andric // needs some number of nops in between. We don't know how many we need, but 6980b57cec5SDimitry Andric // let's use 4. This wasn't discovered before probably because the only 6990b57cec5SDimitry Andric // case when this happens is when we expand a 64-bit pointer into a full 7000b57cec5SDimitry Andric // descriptor and use s_buffer_load_dword instead of s_load_dword, which was 7010b57cec5SDimitry Andric // probably never encountered in the closed-source land. 7020b57cec5SDimitry Andric if (IsBufferSMRD) { 7030b57cec5SDimitry Andric int WaitStatesNeededForUse = 7040b57cec5SDimitry Andric SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), 7050b57cec5SDimitry Andric IsBufferHazardDefFn, 7060b57cec5SDimitry Andric SmrdSgprWaitStates); 7070b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric return WaitStatesNeeded; 7120b57cec5SDimitry Andric } 7130b57cec5SDimitry Andric 7140b57cec5SDimitry Andric int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { 7150b57cec5SDimitry Andric if (!ST.hasVMEMReadSGPRVALUDefHazard()) 7160b57cec5SDimitry Andric return 0; 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric int WaitStatesNeeded = checkSoftClauseHazards(VMEM); 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric // A read of an SGPR by a VMEM instruction requires 5 wait states when the 7210b57cec5SDimitry Andric // SGPR was written by a VALU Instruction. 7220b57cec5SDimitry Andric const int VmemSgprWaitStates = 5; 723fe6060f1SDimitry Andric auto IsHazardDefFn = [this](const MachineInstr &MI) { 724fe6060f1SDimitry Andric return TII.isVALU(MI); 725fe6060f1SDimitry Andric }; 7260b57cec5SDimitry Andric for (const MachineOperand &Use : VMEM->uses()) { 727fe6060f1SDimitry Andric if (!Use.isReg() || TRI.isVectorRegister(MF.getRegInfo(), Use.getReg())) 7280b57cec5SDimitry Andric continue; 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric int WaitStatesNeededForUse = 7310b57cec5SDimitry Andric VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 7320b57cec5SDimitry Andric VmemSgprWaitStates); 7330b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 7340b57cec5SDimitry Andric } 7350b57cec5SDimitry Andric return WaitStatesNeeded; 7360b57cec5SDimitry Andric } 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { 7390b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 7400b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric // Check for DPP VGPR read after VALU VGPR write and EXEC write. 7430b57cec5SDimitry Andric int DppVgprWaitStates = 2; 7440b57cec5SDimitry Andric int DppExecWaitStates = 5; 7450b57cec5SDimitry Andric int WaitStatesNeeded = 0; 746fe6060f1SDimitry Andric auto IsHazardDefFn = [TII](const MachineInstr &MI) { 747fe6060f1SDimitry Andric return TII->isVALU(MI); 748fe6060f1SDimitry Andric }; 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric for (const MachineOperand &Use : DPP->uses()) { 7510b57cec5SDimitry Andric if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 7520b57cec5SDimitry Andric continue; 7530b57cec5SDimitry Andric int WaitStatesNeededForUse = 754fe6060f1SDimitry Andric DppVgprWaitStates - getWaitStatesSinceDef( 755fe6060f1SDimitry Andric Use.getReg(), 756fe6060f1SDimitry Andric [](const MachineInstr &) { return true; }, 7570b57cec5SDimitry Andric DppVgprWaitStates); 7580b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric WaitStatesNeeded = std::max( 7620b57cec5SDimitry Andric WaitStatesNeeded, 7630b57cec5SDimitry Andric DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn, 7640b57cec5SDimitry Andric DppExecWaitStates)); 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric return WaitStatesNeeded; 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { 7700b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric // v_div_fmas requires 4 wait states after a write to vcc from a VALU 7730b57cec5SDimitry Andric // instruction. 7740b57cec5SDimitry Andric const int DivFMasWaitStates = 4; 775fe6060f1SDimitry Andric auto IsHazardDefFn = [TII](const MachineInstr &MI) { 776fe6060f1SDimitry Andric return TII->isVALU(MI); 777fe6060f1SDimitry Andric }; 7780b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn, 7790b57cec5SDimitry Andric DivFMasWaitStates); 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric return DivFMasWaitStates - WaitStatesNeeded; 7820b57cec5SDimitry Andric } 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { 7850b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7860b57cec5SDimitry Andric unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric const int GetRegWaitStates = 2; 789fe6060f1SDimitry Andric auto IsHazardFn = [TII, GetRegHWReg](const MachineInstr &MI) { 790fe6060f1SDimitry Andric return GetRegHWReg == getHWReg(TII, MI); 7910b57cec5SDimitry Andric }; 7920b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates); 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric return GetRegWaitStates - WaitStatesNeeded; 7950b57cec5SDimitry Andric } 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { 7980b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7990b57cec5SDimitry Andric unsigned HWReg = getHWReg(TII, *SetRegInstr); 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric const int SetRegWaitStates = ST.getSetRegWaitStates(); 802fe6060f1SDimitry Andric auto IsHazardFn = [TII, HWReg](const MachineInstr &MI) { 803fe6060f1SDimitry Andric return HWReg == getHWReg(TII, MI); 8040b57cec5SDimitry Andric }; 8050b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates); 8060b57cec5SDimitry Andric return SetRegWaitStates - WaitStatesNeeded; 8070b57cec5SDimitry Andric } 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { 8100b57cec5SDimitry Andric if (!MI.mayStore()) 8110b57cec5SDimitry Andric return -1; 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 8140b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode(); 8150b57cec5SDimitry Andric const MCInstrDesc &Desc = MI.getDesc(); 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 8180b57cec5SDimitry Andric int VDataRCID = -1; 8190b57cec5SDimitry Andric if (VDataIdx != -1) 820*bdd1243dSDimitry Andric VDataRCID = Desc.operands()[VDataIdx].RegClass; 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { 8230b57cec5SDimitry Andric // There is no hazard if the instruction does not use vector regs 8240b57cec5SDimitry Andric // (like wbinvl1) 8250b57cec5SDimitry Andric if (VDataIdx == -1) 8260b57cec5SDimitry Andric return -1; 8270b57cec5SDimitry Andric // For MUBUF/MTBUF instructions this hazard only exists if the 8280b57cec5SDimitry Andric // instruction is not using a register in the soffset field. 8290b57cec5SDimitry Andric const MachineOperand *SOffset = 8300b57cec5SDimitry Andric TII->getNamedOperand(MI, AMDGPU::OpName::soffset); 8310b57cec5SDimitry Andric // If we have no soffset operand, then assume this field has been 8320b57cec5SDimitry Andric // hardcoded to zero. 8330b57cec5SDimitry Andric if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && 8340b57cec5SDimitry Andric (!SOffset || !SOffset->isReg())) 8350b57cec5SDimitry Andric return VDataIdx; 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric // MIMG instructions create a hazard if they don't use a 256-bit T# and 8390b57cec5SDimitry Andric // the store size is greater than 8 bytes and they have more than two bits 8400b57cec5SDimitry Andric // of their dmask set. 8410b57cec5SDimitry Andric // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. 8420b57cec5SDimitry Andric if (TII->isMIMG(MI)) { 8430b57cec5SDimitry Andric int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 8440b57cec5SDimitry Andric assert(SRsrcIdx != -1 && 845*bdd1243dSDimitry Andric AMDGPU::getRegBitWidth(Desc.operands()[SRsrcIdx].RegClass) == 256); 8460b57cec5SDimitry Andric (void)SRsrcIdx; 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric if (TII->isFLAT(MI)) { 8500b57cec5SDimitry Andric int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 851*bdd1243dSDimitry Andric if (AMDGPU::getRegBitWidth(Desc.operands()[DataIdx].RegClass) > 64) 8520b57cec5SDimitry Andric return DataIdx; 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric return -1; 8560b57cec5SDimitry Andric } 8570b57cec5SDimitry Andric 858e8d8bef9SDimitry Andric int 859e8d8bef9SDimitry Andric GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def, 8600b57cec5SDimitry Andric const MachineRegisterInfo &MRI) { 8610b57cec5SDimitry Andric // Helper to check for the hazard where VMEM instructions that store more than 8620b57cec5SDimitry Andric // 8 bytes can have there store data over written by the next instruction. 8630b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 8640b57cec5SDimitry Andric 86581ad6265SDimitry Andric const int VALUWaitStates = ST.hasGFX940Insts() ? 2 : 1; 8660b57cec5SDimitry Andric int WaitStatesNeeded = 0; 8670b57cec5SDimitry Andric 868fe6060f1SDimitry Andric if (!TRI->isVectorRegister(MRI, Def.getReg())) 8690b57cec5SDimitry Andric return WaitStatesNeeded; 8708bcb0991SDimitry Andric Register Reg = Def.getReg(); 871fe6060f1SDimitry Andric auto IsHazardFn = [this, Reg, TRI](const MachineInstr &MI) { 872fe6060f1SDimitry Andric int DataIdx = createsVALUHazard(MI); 8730b57cec5SDimitry Andric return DataIdx >= 0 && 874fe6060f1SDimitry Andric TRI->regsOverlap(MI.getOperand(DataIdx).getReg(), Reg); 8750b57cec5SDimitry Andric }; 8760b57cec5SDimitry Andric int WaitStatesNeededForDef = 8770b57cec5SDimitry Andric VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates); 8780b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 8790b57cec5SDimitry Andric 8800b57cec5SDimitry Andric return WaitStatesNeeded; 8810b57cec5SDimitry Andric } 8820b57cec5SDimitry Andric 8830b57cec5SDimitry Andric int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { 88481ad6265SDimitry Andric int WaitStatesNeeded = 0; 88581ad6265SDimitry Andric 88681ad6265SDimitry Andric if (ST.hasTransForwardingHazard() && !SIInstrInfo::isTRANS(*VALU)) { 88781ad6265SDimitry Andric const int TransDefWaitstates = 1; 88881ad6265SDimitry Andric 88981ad6265SDimitry Andric auto IsTransDefFn = [this, VALU](const MachineInstr &MI) { 89081ad6265SDimitry Andric if (!SIInstrInfo::isTRANS(MI)) 89181ad6265SDimitry Andric return false; 89281ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 89381ad6265SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 89481ad6265SDimitry Andric Register Def = TII->getNamedOperand(MI, AMDGPU::OpName::vdst)->getReg(); 89581ad6265SDimitry Andric 89681ad6265SDimitry Andric for (const MachineOperand &Use : VALU->explicit_uses()) { 89781ad6265SDimitry Andric if (Use.isReg() && TRI->regsOverlap(Def, Use.getReg())) 89881ad6265SDimitry Andric return true; 89981ad6265SDimitry Andric } 90081ad6265SDimitry Andric 90181ad6265SDimitry Andric return false; 90281ad6265SDimitry Andric }; 90381ad6265SDimitry Andric 90481ad6265SDimitry Andric int WaitStatesNeededForDef = 90581ad6265SDimitry Andric TransDefWaitstates - 90681ad6265SDimitry Andric getWaitStatesSince(IsTransDefFn, TransDefWaitstates); 90781ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 90881ad6265SDimitry Andric } 90981ad6265SDimitry Andric 91081ad6265SDimitry Andric if (ST.hasDstSelForwardingHazard()) { 91181ad6265SDimitry Andric const int Shift16DefWaitstates = 1; 91281ad6265SDimitry Andric 91381ad6265SDimitry Andric auto IsShift16BitDefFn = [this, VALU](const MachineInstr &MI) { 91481ad6265SDimitry Andric if (!SIInstrInfo::isVALU(MI)) 91581ad6265SDimitry Andric return false; 91681ad6265SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 91781ad6265SDimitry Andric if (SIInstrInfo::isSDWA(MI)) { 91881ad6265SDimitry Andric if (auto *DstSel = TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel)) 91981ad6265SDimitry Andric if (DstSel->getImm() == AMDGPU::SDWA::DWORD) 92081ad6265SDimitry Andric return false; 92181ad6265SDimitry Andric } else { 922*bdd1243dSDimitry Andric if (!AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::op_sel) || 92381ad6265SDimitry Andric !(TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers) 92481ad6265SDimitry Andric ->getImm() & 92581ad6265SDimitry Andric SISrcMods::DST_OP_SEL)) 92681ad6265SDimitry Andric return false; 92781ad6265SDimitry Andric } 92881ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 92981ad6265SDimitry Andric if (auto *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst)) { 93081ad6265SDimitry Andric Register Def = Dst->getReg(); 93181ad6265SDimitry Andric 93281ad6265SDimitry Andric for (const MachineOperand &Use : VALU->explicit_uses()) { 93381ad6265SDimitry Andric if (Use.isReg() && TRI->regsOverlap(Def, Use.getReg())) 93481ad6265SDimitry Andric return true; 93581ad6265SDimitry Andric } 93681ad6265SDimitry Andric } 93781ad6265SDimitry Andric 93881ad6265SDimitry Andric return false; 93981ad6265SDimitry Andric }; 94081ad6265SDimitry Andric 94181ad6265SDimitry Andric int WaitStatesNeededForDef = 94281ad6265SDimitry Andric Shift16DefWaitstates - 94381ad6265SDimitry Andric getWaitStatesSince(IsShift16BitDefFn, Shift16DefWaitstates); 94481ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 94581ad6265SDimitry Andric } 94681ad6265SDimitry Andric 94781ad6265SDimitry Andric if (ST.hasVDecCoExecHazard()) { 94881ad6265SDimitry Andric const int VALUWriteSGPRVALUReadWaitstates = 2; 94981ad6265SDimitry Andric const int VALUWriteEXECRWLane = 4; 95081ad6265SDimitry Andric const int VALUWriteVGPRReadlaneRead = 1; 95181ad6265SDimitry Andric 95281ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 95381ad6265SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 95481ad6265SDimitry Andric Register UseReg; 95581ad6265SDimitry Andric auto IsVALUDefSGPRFn = [&UseReg, TRI](const MachineInstr &MI) { 95681ad6265SDimitry Andric if (!SIInstrInfo::isVALU(MI)) 95781ad6265SDimitry Andric return false; 95881ad6265SDimitry Andric return MI.modifiesRegister(UseReg, TRI); 95981ad6265SDimitry Andric }; 96081ad6265SDimitry Andric 96181ad6265SDimitry Andric for (const MachineOperand &Use : VALU->explicit_uses()) { 96281ad6265SDimitry Andric if (!Use.isReg()) 96381ad6265SDimitry Andric continue; 96481ad6265SDimitry Andric 96581ad6265SDimitry Andric UseReg = Use.getReg(); 96681ad6265SDimitry Andric if (TRI->isSGPRReg(MRI, UseReg)) { 96781ad6265SDimitry Andric int WaitStatesNeededForDef = 96881ad6265SDimitry Andric VALUWriteSGPRVALUReadWaitstates - 96981ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, 97081ad6265SDimitry Andric VALUWriteSGPRVALUReadWaitstates); 97181ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 97281ad6265SDimitry Andric } 97381ad6265SDimitry Andric } 97481ad6265SDimitry Andric 97581ad6265SDimitry Andric if (VALU->readsRegister(AMDGPU::VCC, TRI)) { 97681ad6265SDimitry Andric UseReg = AMDGPU::VCC; 97781ad6265SDimitry Andric int WaitStatesNeededForDef = 97881ad6265SDimitry Andric VALUWriteSGPRVALUReadWaitstates - 97981ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteSGPRVALUReadWaitstates); 98081ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 98181ad6265SDimitry Andric } 98281ad6265SDimitry Andric 98381ad6265SDimitry Andric switch (VALU->getOpcode()) { 98481ad6265SDimitry Andric case AMDGPU::V_READLANE_B32: 98581ad6265SDimitry Andric case AMDGPU::V_READFIRSTLANE_B32: { 98681ad6265SDimitry Andric MachineOperand *Src = TII.getNamedOperand(*VALU, AMDGPU::OpName::src0); 98781ad6265SDimitry Andric UseReg = Src->getReg(); 98881ad6265SDimitry Andric int WaitStatesNeededForDef = 98981ad6265SDimitry Andric VALUWriteVGPRReadlaneRead - 99081ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteVGPRReadlaneRead); 99181ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 99281ad6265SDimitry Andric } 993*bdd1243dSDimitry Andric [[fallthrough]]; 99481ad6265SDimitry Andric case AMDGPU::V_WRITELANE_B32: { 99581ad6265SDimitry Andric UseReg = AMDGPU::EXEC; 99681ad6265SDimitry Andric int WaitStatesNeededForDef = 99781ad6265SDimitry Andric VALUWriteEXECRWLane - 99881ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteEXECRWLane); 99981ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 100081ad6265SDimitry Andric break; 100181ad6265SDimitry Andric } 100281ad6265SDimitry Andric default: 100381ad6265SDimitry Andric break; 100481ad6265SDimitry Andric } 100581ad6265SDimitry Andric } 100681ad6265SDimitry Andric 10070b57cec5SDimitry Andric // This checks for the hazard where VMEM instructions that store more than 10080b57cec5SDimitry Andric // 8 bytes can have there store data over written by the next instruction. 10090b57cec5SDimitry Andric if (!ST.has12DWordStoreHazard()) 101081ad6265SDimitry Andric return WaitStatesNeeded; 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric for (const MachineOperand &Def : VALU->defs()) { 10150b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI)); 10160b57cec5SDimitry Andric } 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric return WaitStatesNeeded; 10190b57cec5SDimitry Andric } 10200b57cec5SDimitry Andric 10210b57cec5SDimitry Andric int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) { 10220b57cec5SDimitry Andric // This checks for hazards associated with inline asm statements. 10230b57cec5SDimitry Andric // Since inline asms can contain just about anything, we use this 10240b57cec5SDimitry Andric // to call/leverage other check*Hazard routines. Note that 10250b57cec5SDimitry Andric // this function doesn't attempt to address all possible inline asm 10260b57cec5SDimitry Andric // hazards (good luck), but is a collection of what has been 10270b57cec5SDimitry Andric // problematic thus far. 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric // see checkVALUHazards() 10300b57cec5SDimitry Andric if (!ST.has12DWordStoreHazard()) 10310b57cec5SDimitry Andric return 0; 10320b57cec5SDimitry Andric 10330b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 10340b57cec5SDimitry Andric int WaitStatesNeeded = 0; 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands(); 10370b57cec5SDimitry Andric I != E; ++I) { 10380b57cec5SDimitry Andric const MachineOperand &Op = IA->getOperand(I); 10390b57cec5SDimitry Andric if (Op.isReg() && Op.isDef()) { 10400b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI)); 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric 10440b57cec5SDimitry Andric return WaitStatesNeeded; 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric 10470b57cec5SDimitry Andric int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { 10480b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 10490b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 10500b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 10510b57cec5SDimitry Andric 10520b57cec5SDimitry Andric const MachineOperand *LaneSelectOp = 10530b57cec5SDimitry Andric TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) 10560b57cec5SDimitry Andric return 0; 10570b57cec5SDimitry Andric 10588bcb0991SDimitry Andric Register LaneSelectReg = LaneSelectOp->getReg(); 1059fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &MI) { return TII->isVALU(MI); }; 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric const int RWLaneWaitStates = 4; 10620b57cec5SDimitry Andric int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn, 10630b57cec5SDimitry Andric RWLaneWaitStates); 10640b57cec5SDimitry Andric return RWLaneWaitStates - WaitStatesSince; 10650b57cec5SDimitry Andric } 10660b57cec5SDimitry Andric 10670b57cec5SDimitry Andric int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { 10680b57cec5SDimitry Andric if (!ST.hasRFEHazards()) 10690b57cec5SDimitry Andric return 0; 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 10720b57cec5SDimitry Andric 10730b57cec5SDimitry Andric const int RFEWaitStates = 1; 10740b57cec5SDimitry Andric 1075fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &MI) { 1076fe6060f1SDimitry Andric return getHWReg(TII, MI) == AMDGPU::Hwreg::ID_TRAPSTS; 10770b57cec5SDimitry Andric }; 10780b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates); 10790b57cec5SDimitry Andric return RFEWaitStates - WaitStatesNeeded; 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { 10830b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 108481ad6265SDimitry Andric const int ReadM0WaitStates = 1; 1085fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &MI) { return TII->isSALU(MI); }; 108681ad6265SDimitry Andric return ReadM0WaitStates - 108781ad6265SDimitry Andric getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn, ReadM0WaitStates); 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric void GCNHazardRecognizer::fixHazards(MachineInstr *MI) { 10910b57cec5SDimitry Andric fixVMEMtoScalarWriteHazards(MI); 10920b57cec5SDimitry Andric fixVcmpxPermlaneHazards(MI); 10930b57cec5SDimitry Andric fixSMEMtoVectorWriteHazards(MI); 10940b57cec5SDimitry Andric fixVcmpxExecWARHazard(MI); 10950b57cec5SDimitry Andric fixLdsBranchVmemWARHazard(MI); 109681ad6265SDimitry Andric if (ST.hasLdsDirect()) { 109781ad6265SDimitry Andric fixLdsDirectVALUHazard(MI); 109881ad6265SDimitry Andric fixLdsDirectVMEMHazard(MI); 109981ad6265SDimitry Andric } 110081ad6265SDimitry Andric fixVALUPartialForwardingHazard(MI); 110181ad6265SDimitry Andric fixVALUTransUseHazard(MI); 110281ad6265SDimitry Andric fixWMMAHazards(MI); 1103*bdd1243dSDimitry Andric fixShift64HighRegBug(MI); 1104*bdd1243dSDimitry Andric fixVALUMaskWriteHazard(MI); 11050b57cec5SDimitry Andric } 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric bool GCNHazardRecognizer::fixVcmpxPermlaneHazards(MachineInstr *MI) { 11080b57cec5SDimitry Andric if (!ST.hasVcmpxPermlaneHazard() || !isPermlane(*MI)) 11090b57cec5SDimitry Andric return false; 11100b57cec5SDimitry Andric 11110b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 111281ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 111381ad6265SDimitry Andric auto IsHazardFn = [TII, TRI](const MachineInstr &MI) { 111481ad6265SDimitry Andric return (TII->isVOPC(MI) || 111581ad6265SDimitry Andric ((TII->isVOP3(MI) || TII->isSDWA(MI)) && MI.isCompare())) && 111681ad6265SDimitry Andric MI.modifiesRegister(AMDGPU::EXEC, TRI); 111781ad6265SDimitry Andric }; 11180b57cec5SDimitry Andric 1119fe6060f1SDimitry Andric auto IsExpiredFn = [](const MachineInstr &MI, int) { 1120fe6060f1SDimitry Andric unsigned Opc = MI.getOpcode(); 1121fe6060f1SDimitry Andric return SIInstrInfo::isVALU(MI) && Opc != AMDGPU::V_NOP_e32 && 1122fe6060f1SDimitry Andric Opc != AMDGPU::V_NOP_e64 && Opc != AMDGPU::V_NOP_sdwa; 11230b57cec5SDimitry Andric }; 11240b57cec5SDimitry Andric 11250b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 11260b57cec5SDimitry Andric std::numeric_limits<int>::max()) 11270b57cec5SDimitry Andric return false; 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric // V_NOP will be discarded by SQ. 113081ad6265SDimitry Andric // Use V_MOV_B32 v?, v?. Register must be alive so use src0 of V_PERMLANE* 11310b57cec5SDimitry Andric // which is always a VGPR and available. 11320b57cec5SDimitry Andric auto *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0); 11338bcb0991SDimitry Andric Register Reg = Src0->getReg(); 11340b57cec5SDimitry Andric bool IsUndef = Src0->isUndef(); 11350b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 11360b57cec5SDimitry Andric TII->get(AMDGPU::V_MOV_B32_e32)) 11370b57cec5SDimitry Andric .addReg(Reg, RegState::Define | (IsUndef ? RegState::Dead : 0)) 11380b57cec5SDimitry Andric .addReg(Reg, IsUndef ? RegState::Undef : RegState::Kill); 11390b57cec5SDimitry Andric 11400b57cec5SDimitry Andric return true; 11410b57cec5SDimitry Andric } 11420b57cec5SDimitry Andric 11430b57cec5SDimitry Andric bool GCNHazardRecognizer::fixVMEMtoScalarWriteHazards(MachineInstr *MI) { 11440b57cec5SDimitry Andric if (!ST.hasVMEMtoScalarWriteHazard()) 11450b57cec5SDimitry Andric return false; 11460b57cec5SDimitry Andric 11470b57cec5SDimitry Andric if (!SIInstrInfo::isSALU(*MI) && !SIInstrInfo::isSMRD(*MI)) 11480b57cec5SDimitry Andric return false; 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andric if (MI->getNumDefs() == 0) 11510b57cec5SDimitry Andric return false; 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 11540b57cec5SDimitry Andric 1155fe6060f1SDimitry Andric auto IsHazardFn = [TRI, MI](const MachineInstr &I) { 1156fe6060f1SDimitry Andric if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isDS(I) && 1157fe6060f1SDimitry Andric !SIInstrInfo::isFLAT(I)) 11580b57cec5SDimitry Andric return false; 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric for (const MachineOperand &Def : MI->defs()) { 1161fe6060f1SDimitry Andric const MachineOperand *Op = 1162fe6060f1SDimitry Andric I.findRegisterUseOperand(Def.getReg(), false, TRI); 11630b57cec5SDimitry Andric if (!Op) 11640b57cec5SDimitry Andric continue; 11650b57cec5SDimitry Andric return true; 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric return false; 11680b57cec5SDimitry Andric }; 11690b57cec5SDimitry Andric 1170fe6060f1SDimitry Andric auto IsExpiredFn = [](const MachineInstr &MI, int) { 1171fe6060f1SDimitry Andric return SIInstrInfo::isVALU(MI) || 1172fe6060f1SDimitry Andric (MI.getOpcode() == AMDGPU::S_WAITCNT && 1173fe6060f1SDimitry Andric !MI.getOperand(0).getImm()) || 1174fe6060f1SDimitry Andric (MI.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1175fe6060f1SDimitry Andric MI.getOperand(0).getImm() == 0xffe3); 11760b57cec5SDimitry Andric }; 11770b57cec5SDimitry Andric 11780b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 11790b57cec5SDimitry Andric std::numeric_limits<int>::max()) 11800b57cec5SDimitry Andric return false; 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 1183e8d8bef9SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1184e8d8bef9SDimitry Andric TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 1185e8d8bef9SDimitry Andric .addImm(0xffe3); 11860b57cec5SDimitry Andric return true; 11870b57cec5SDimitry Andric } 11880b57cec5SDimitry Andric 11890b57cec5SDimitry Andric bool GCNHazardRecognizer::fixSMEMtoVectorWriteHazards(MachineInstr *MI) { 11900b57cec5SDimitry Andric if (!ST.hasSMEMtoVectorWriteHazard()) 11910b57cec5SDimitry Andric return false; 11920b57cec5SDimitry Andric 11930b57cec5SDimitry Andric if (!SIInstrInfo::isVALU(*MI)) 11940b57cec5SDimitry Andric return false; 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric unsigned SDSTName; 11970b57cec5SDimitry Andric switch (MI->getOpcode()) { 11980b57cec5SDimitry Andric case AMDGPU::V_READLANE_B32: 11990b57cec5SDimitry Andric case AMDGPU::V_READFIRSTLANE_B32: 12000b57cec5SDimitry Andric SDSTName = AMDGPU::OpName::vdst; 12010b57cec5SDimitry Andric break; 12020b57cec5SDimitry Andric default: 12030b57cec5SDimitry Andric SDSTName = AMDGPU::OpName::sdst; 12040b57cec5SDimitry Andric break; 12050b57cec5SDimitry Andric } 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 12080b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 12090b57cec5SDimitry Andric const AMDGPU::IsaVersion IV = AMDGPU::getIsaVersion(ST.getCPU()); 12100b57cec5SDimitry Andric const MachineOperand *SDST = TII->getNamedOperand(*MI, SDSTName); 12110b57cec5SDimitry Andric if (!SDST) { 12120b57cec5SDimitry Andric for (const auto &MO : MI->implicit_operands()) { 1213*bdd1243dSDimitry Andric if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegBaseClass(MO.getReg()))) { 12140b57cec5SDimitry Andric SDST = &MO; 12150b57cec5SDimitry Andric break; 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric } 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric if (!SDST) 12210b57cec5SDimitry Andric return false; 12220b57cec5SDimitry Andric 12238bcb0991SDimitry Andric const Register SDSTReg = SDST->getReg(); 1224fe6060f1SDimitry Andric auto IsHazardFn = [SDSTReg, TRI](const MachineInstr &I) { 1225fe6060f1SDimitry Andric return SIInstrInfo::isSMRD(I) && I.readsRegister(SDSTReg, TRI); 12260b57cec5SDimitry Andric }; 12270b57cec5SDimitry Andric 1228fe6060f1SDimitry Andric auto IsExpiredFn = [TII, IV](const MachineInstr &MI, int) { 1229fe6060f1SDimitry Andric if (TII->isSALU(MI)) { 1230fe6060f1SDimitry Andric switch (MI.getOpcode()) { 12310b57cec5SDimitry Andric case AMDGPU::S_SETVSKIP: 12320b57cec5SDimitry Andric case AMDGPU::S_VERSION: 12330b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VSCNT: 12340b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VMCNT: 12350b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_EXPCNT: 12360b57cec5SDimitry Andric // These instructions cannot not mitigate the hazard. 12370b57cec5SDimitry Andric return false; 12380b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_LGKMCNT: 12390b57cec5SDimitry Andric // Reducing lgkmcnt count to 0 always mitigates the hazard. 1240fe6060f1SDimitry Andric return (MI.getOperand(1).getImm() == 0) && 1241fe6060f1SDimitry Andric (MI.getOperand(0).getReg() == AMDGPU::SGPR_NULL); 12420b57cec5SDimitry Andric case AMDGPU::S_WAITCNT: { 1243fe6060f1SDimitry Andric const int64_t Imm = MI.getOperand(0).getImm(); 12440b57cec5SDimitry Andric AMDGPU::Waitcnt Decoded = AMDGPU::decodeWaitcnt(IV, Imm); 12450b57cec5SDimitry Andric return (Decoded.LgkmCnt == 0); 12460b57cec5SDimitry Andric } 12470b57cec5SDimitry Andric default: 12480b57cec5SDimitry Andric // SOPP instructions cannot mitigate the hazard. 1249fe6060f1SDimitry Andric if (TII->isSOPP(MI)) 12500b57cec5SDimitry Andric return false; 12510b57cec5SDimitry Andric // At this point the SALU can be assumed to mitigate the hazard 12520b57cec5SDimitry Andric // because either: 12530b57cec5SDimitry Andric // (a) it is independent of the at risk SMEM (breaking chain), 12540b57cec5SDimitry Andric // or 12550b57cec5SDimitry Andric // (b) it is dependent on the SMEM, in which case an appropriate 12560b57cec5SDimitry Andric // s_waitcnt lgkmcnt _must_ exist between it and the at risk 12570b57cec5SDimitry Andric // SMEM instruction. 12580b57cec5SDimitry Andric return true; 12590b57cec5SDimitry Andric } 12600b57cec5SDimitry Andric } 12610b57cec5SDimitry Andric return false; 12620b57cec5SDimitry Andric }; 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 12650b57cec5SDimitry Andric std::numeric_limits<int>::max()) 12660b57cec5SDimitry Andric return false; 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 12690b57cec5SDimitry Andric TII->get(AMDGPU::S_MOV_B32), AMDGPU::SGPR_NULL) 12700b57cec5SDimitry Andric .addImm(0); 12710b57cec5SDimitry Andric return true; 12720b57cec5SDimitry Andric } 12730b57cec5SDimitry Andric 12740b57cec5SDimitry Andric bool GCNHazardRecognizer::fixVcmpxExecWARHazard(MachineInstr *MI) { 12750b57cec5SDimitry Andric if (!ST.hasVcmpxExecWARHazard() || !SIInstrInfo::isVALU(*MI)) 12760b57cec5SDimitry Andric return false; 12770b57cec5SDimitry Andric 12780b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 12790b57cec5SDimitry Andric if (!MI->modifiesRegister(AMDGPU::EXEC, TRI)) 12800b57cec5SDimitry Andric return false; 12810b57cec5SDimitry Andric 1282fe6060f1SDimitry Andric auto IsHazardFn = [TRI](const MachineInstr &I) { 1283fe6060f1SDimitry Andric if (SIInstrInfo::isVALU(I)) 12840b57cec5SDimitry Andric return false; 1285fe6060f1SDimitry Andric return I.readsRegister(AMDGPU::EXEC, TRI); 12860b57cec5SDimitry Andric }; 12870b57cec5SDimitry Andric 12880b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 1289fe6060f1SDimitry Andric auto IsExpiredFn = [TII, TRI](const MachineInstr &MI, int) { 1290fe6060f1SDimitry Andric if (SIInstrInfo::isVALU(MI)) { 1291fe6060f1SDimitry Andric if (TII->getNamedOperand(MI, AMDGPU::OpName::sdst)) 12920b57cec5SDimitry Andric return true; 1293fe6060f1SDimitry Andric for (auto MO : MI.implicit_operands()) 1294*bdd1243dSDimitry Andric if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegBaseClass(MO.getReg()))) 12950b57cec5SDimitry Andric return true; 12960b57cec5SDimitry Andric } 1297fe6060f1SDimitry Andric if (MI.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1298fe6060f1SDimitry Andric (MI.getOperand(0).getImm() & 0xfffe) == 0xfffe) 12990b57cec5SDimitry Andric return true; 13000b57cec5SDimitry Andric return false; 13010b57cec5SDimitry Andric }; 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 13040b57cec5SDimitry Andric std::numeric_limits<int>::max()) 13050b57cec5SDimitry Andric return false; 13060b57cec5SDimitry Andric 13070b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 13080b57cec5SDimitry Andric TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 13090b57cec5SDimitry Andric .addImm(0xfffe); 13100b57cec5SDimitry Andric return true; 13110b57cec5SDimitry Andric } 13120b57cec5SDimitry Andric 1313fe6060f1SDimitry Andric static bool shouldRunLdsBranchVmemWARHazardFixup(const MachineFunction &MF, 1314fe6060f1SDimitry Andric const GCNSubtarget &ST) { 13150b57cec5SDimitry Andric if (!ST.hasLdsBranchVmemWARHazard()) 13160b57cec5SDimitry Andric return false; 13170b57cec5SDimitry Andric 1318fe6060f1SDimitry Andric // Check if the necessary condition for the hazard is met: both LDS and VMEM 1319fe6060f1SDimitry Andric // instructions need to appear in the same function. 1320fe6060f1SDimitry Andric bool HasLds = false; 1321fe6060f1SDimitry Andric bool HasVmem = false; 1322fe6060f1SDimitry Andric for (auto &MBB : MF) { 1323fe6060f1SDimitry Andric for (auto &MI : MBB) { 1324fe6060f1SDimitry Andric HasLds |= SIInstrInfo::isDS(MI); 1325fe6060f1SDimitry Andric HasVmem |= 1326fe6060f1SDimitry Andric SIInstrInfo::isVMEM(MI) || SIInstrInfo::isSegmentSpecificFLAT(MI); 1327fe6060f1SDimitry Andric if (HasLds && HasVmem) 1328fe6060f1SDimitry Andric return true; 1329fe6060f1SDimitry Andric } 1330fe6060f1SDimitry Andric } 1331fe6060f1SDimitry Andric return false; 1332fe6060f1SDimitry Andric } 1333fe6060f1SDimitry Andric 1334*bdd1243dSDimitry Andric static bool isStoreCountWaitZero(const MachineInstr &I) { 1335*bdd1243dSDimitry Andric return I.getOpcode() == AMDGPU::S_WAITCNT_VSCNT && 1336*bdd1243dSDimitry Andric I.getOperand(0).getReg() == AMDGPU::SGPR_NULL && 1337*bdd1243dSDimitry Andric !I.getOperand(1).getImm(); 1338*bdd1243dSDimitry Andric } 1339*bdd1243dSDimitry Andric 1340fe6060f1SDimitry Andric bool GCNHazardRecognizer::fixLdsBranchVmemWARHazard(MachineInstr *MI) { 1341fe6060f1SDimitry Andric if (!RunLdsBranchVmemWARHazardFixup) 1342fe6060f1SDimitry Andric return false; 1343fe6060f1SDimitry Andric 1344fe6060f1SDimitry Andric assert(ST.hasLdsBranchVmemWARHazard()); 1345fe6060f1SDimitry Andric 1346fe6060f1SDimitry Andric auto IsHazardInst = [](const MachineInstr &MI) { 1347fe6060f1SDimitry Andric if (SIInstrInfo::isDS(MI)) 13480b57cec5SDimitry Andric return 1; 1349fe6060f1SDimitry Andric if (SIInstrInfo::isVMEM(MI) || SIInstrInfo::isSegmentSpecificFLAT(MI)) 13500b57cec5SDimitry Andric return 2; 13510b57cec5SDimitry Andric return 0; 13520b57cec5SDimitry Andric }; 13530b57cec5SDimitry Andric 1354fe6060f1SDimitry Andric auto InstType = IsHazardInst(*MI); 13550b57cec5SDimitry Andric if (!InstType) 13560b57cec5SDimitry Andric return false; 13570b57cec5SDimitry Andric 1358fe6060f1SDimitry Andric auto IsExpiredFn = [&IsHazardInst](const MachineInstr &I, int) { 1359*bdd1243dSDimitry Andric return IsHazardInst(I) || isStoreCountWaitZero(I); 13600b57cec5SDimitry Andric }; 13610b57cec5SDimitry Andric 1362fe6060f1SDimitry Andric auto IsHazardFn = [InstType, &IsHazardInst](const MachineInstr &I) { 1363fe6060f1SDimitry Andric if (!I.isBranch()) 13640b57cec5SDimitry Andric return false; 13650b57cec5SDimitry Andric 1366fe6060f1SDimitry Andric auto IsHazardFn = [InstType, IsHazardInst](const MachineInstr &I) { 13670b57cec5SDimitry Andric auto InstType2 = IsHazardInst(I); 13680b57cec5SDimitry Andric return InstType2 && InstType != InstType2; 13690b57cec5SDimitry Andric }; 13700b57cec5SDimitry Andric 1371fe6060f1SDimitry Andric auto IsExpiredFn = [InstType, &IsHazardInst](const MachineInstr &I, int) { 13720b57cec5SDimitry Andric auto InstType2 = IsHazardInst(I); 13730b57cec5SDimitry Andric if (InstType == InstType2) 13740b57cec5SDimitry Andric return true; 13750b57cec5SDimitry Andric 1376*bdd1243dSDimitry Andric return isStoreCountWaitZero(I); 13770b57cec5SDimitry Andric }; 13780b57cec5SDimitry Andric 1379fe6060f1SDimitry Andric return ::getWaitStatesSince(IsHazardFn, &I, IsExpiredFn) != 13800b57cec5SDimitry Andric std::numeric_limits<int>::max(); 13810b57cec5SDimitry Andric }; 13820b57cec5SDimitry Andric 13830b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 13840b57cec5SDimitry Andric std::numeric_limits<int>::max()) 13850b57cec5SDimitry Andric return false; 13860b57cec5SDimitry Andric 13870b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 13880b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 13890b57cec5SDimitry Andric TII->get(AMDGPU::S_WAITCNT_VSCNT)) 13900b57cec5SDimitry Andric .addReg(AMDGPU::SGPR_NULL, RegState::Undef) 13910b57cec5SDimitry Andric .addImm(0); 13920b57cec5SDimitry Andric 13930b57cec5SDimitry Andric return true; 13940b57cec5SDimitry Andric } 13950b57cec5SDimitry Andric 139681ad6265SDimitry Andric bool GCNHazardRecognizer::fixLdsDirectVALUHazard(MachineInstr *MI) { 139781ad6265SDimitry Andric if (!SIInstrInfo::isLDSDIR(*MI)) 139881ad6265SDimitry Andric return false; 139981ad6265SDimitry Andric 140081ad6265SDimitry Andric const int NoHazardWaitStates = 15; 140181ad6265SDimitry Andric const MachineOperand *VDST = TII.getNamedOperand(*MI, AMDGPU::OpName::vdst); 140281ad6265SDimitry Andric const Register VDSTReg = VDST->getReg(); 140381ad6265SDimitry Andric 140481ad6265SDimitry Andric bool VisitedTrans = false; 140581ad6265SDimitry Andric auto IsHazardFn = [this, VDSTReg, &VisitedTrans](const MachineInstr &I) { 140681ad6265SDimitry Andric if (!SIInstrInfo::isVALU(I)) 140781ad6265SDimitry Andric return false; 140881ad6265SDimitry Andric VisitedTrans = VisitedTrans || SIInstrInfo::isTRANS(I); 140981ad6265SDimitry Andric // Cover both WAR and WAW 141081ad6265SDimitry Andric return I.readsRegister(VDSTReg, &TRI) || I.modifiesRegister(VDSTReg, &TRI); 141181ad6265SDimitry Andric }; 141281ad6265SDimitry Andric auto IsExpiredFn = [&](const MachineInstr &I, int WaitStates) { 141381ad6265SDimitry Andric if (WaitStates >= NoHazardWaitStates) 141481ad6265SDimitry Andric return true; 141581ad6265SDimitry Andric // Instructions which cause va_vdst==0 expire hazard 141681ad6265SDimitry Andric return SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 141781ad6265SDimitry Andric SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I); 141881ad6265SDimitry Andric }; 141981ad6265SDimitry Andric auto GetWaitStatesFn = [](const MachineInstr &MI) { 142081ad6265SDimitry Andric return SIInstrInfo::isVALU(MI) ? 1 : 0; 142181ad6265SDimitry Andric }; 142281ad6265SDimitry Andric 142381ad6265SDimitry Andric DenseSet<const MachineBasicBlock *> Visited; 142481ad6265SDimitry Andric auto Count = ::getWaitStatesSince(IsHazardFn, MI->getParent(), 142581ad6265SDimitry Andric std::next(MI->getReverseIterator()), 0, 142681ad6265SDimitry Andric IsExpiredFn, Visited, GetWaitStatesFn); 142781ad6265SDimitry Andric 142881ad6265SDimitry Andric // Transcendentals can execute in parallel to other VALUs. 142981ad6265SDimitry Andric // This makes va_vdst count unusable with a mixture of VALU and TRANS. 143081ad6265SDimitry Andric if (VisitedTrans) 143181ad6265SDimitry Andric Count = 0; 143281ad6265SDimitry Andric 143381ad6265SDimitry Andric MachineOperand *WaitVdstOp = 143481ad6265SDimitry Andric TII.getNamedOperand(*MI, AMDGPU::OpName::waitvdst); 143581ad6265SDimitry Andric WaitVdstOp->setImm(std::min(Count, NoHazardWaitStates)); 143681ad6265SDimitry Andric 143781ad6265SDimitry Andric return true; 143881ad6265SDimitry Andric } 143981ad6265SDimitry Andric 144081ad6265SDimitry Andric bool GCNHazardRecognizer::fixLdsDirectVMEMHazard(MachineInstr *MI) { 144181ad6265SDimitry Andric if (!SIInstrInfo::isLDSDIR(*MI)) 144281ad6265SDimitry Andric return false; 144381ad6265SDimitry Andric 144481ad6265SDimitry Andric const MachineOperand *VDST = TII.getNamedOperand(*MI, AMDGPU::OpName::vdst); 144581ad6265SDimitry Andric const Register VDSTReg = VDST->getReg(); 144681ad6265SDimitry Andric 144781ad6265SDimitry Andric auto IsHazardFn = [this, VDSTReg](const MachineInstr &I) { 144881ad6265SDimitry Andric if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isFLAT(I) && 144981ad6265SDimitry Andric !SIInstrInfo::isDS(I)) 145081ad6265SDimitry Andric return false; 145181ad6265SDimitry Andric return I.readsRegister(VDSTReg, &TRI) || I.modifiesRegister(VDSTReg, &TRI); 145281ad6265SDimitry Andric }; 145381ad6265SDimitry Andric auto IsExpiredFn = [](const MachineInstr &I, int) { 145481ad6265SDimitry Andric return SIInstrInfo::isVALU(I) || SIInstrInfo::isEXP(I) || 145581ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT && !I.getOperand(0).getImm()) || 145681ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 145781ad6265SDimitry Andric I.getOperand(0).getImm() == 0xffe3); 145881ad6265SDimitry Andric }; 145981ad6265SDimitry Andric 146081ad6265SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 146181ad6265SDimitry Andric std::numeric_limits<int>::max()) 146281ad6265SDimitry Andric return false; 146381ad6265SDimitry Andric 146481ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 146581ad6265SDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 146681ad6265SDimitry Andric .addImm(0xffe3); 146781ad6265SDimitry Andric 146881ad6265SDimitry Andric return true; 146981ad6265SDimitry Andric } 147081ad6265SDimitry Andric 147181ad6265SDimitry Andric bool GCNHazardRecognizer::fixVALUPartialForwardingHazard(MachineInstr *MI) { 147281ad6265SDimitry Andric if (!ST.isWave64()) 147381ad6265SDimitry Andric return false; 147481ad6265SDimitry Andric if (!ST.hasVALUPartialForwardingHazard()) 147581ad6265SDimitry Andric return false; 147681ad6265SDimitry Andric if (!SIInstrInfo::isVALU(*MI)) 147781ad6265SDimitry Andric return false; 147881ad6265SDimitry Andric 147981ad6265SDimitry Andric SmallSetVector<Register, 4> SrcVGPRs; 148081ad6265SDimitry Andric 148181ad6265SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 148281ad6265SDimitry Andric if (Use.isReg() && TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 148381ad6265SDimitry Andric SrcVGPRs.insert(Use.getReg()); 148481ad6265SDimitry Andric } 148581ad6265SDimitry Andric 148681ad6265SDimitry Andric // Only applies with >= 2 unique VGPR sources 148781ad6265SDimitry Andric if (SrcVGPRs.size() <= 1) 148881ad6265SDimitry Andric return false; 148981ad6265SDimitry Andric 149081ad6265SDimitry Andric // Look for the following pattern: 149181ad6265SDimitry Andric // Va <- VALU [PreExecPos] 149281ad6265SDimitry Andric // intv1 149381ad6265SDimitry Andric // Exec <- SALU [ExecPos] 149481ad6265SDimitry Andric // intv2 149581ad6265SDimitry Andric // Vb <- VALU [PostExecPos] 149681ad6265SDimitry Andric // intv3 149781ad6265SDimitry Andric // MI Va, Vb (WaitState = 0) 149881ad6265SDimitry Andric // 149981ad6265SDimitry Andric // Where: 150081ad6265SDimitry Andric // intv1 + intv2 <= 2 VALUs 150181ad6265SDimitry Andric // intv3 <= 4 VALUs 150281ad6265SDimitry Andric // 150381ad6265SDimitry Andric // If found, insert an appropriate S_WAITCNT_DEPCTR before MI. 150481ad6265SDimitry Andric 150581ad6265SDimitry Andric const int Intv1plus2MaxVALUs = 2; 150681ad6265SDimitry Andric const int Intv3MaxVALUs = 4; 150781ad6265SDimitry Andric const int IntvMaxVALUs = 6; 150881ad6265SDimitry Andric const int NoHazardVALUWaitStates = IntvMaxVALUs + 2; 150981ad6265SDimitry Andric 151081ad6265SDimitry Andric struct StateType { 151181ad6265SDimitry Andric SmallDenseMap<Register, int, 4> DefPos; 151281ad6265SDimitry Andric int ExecPos = std::numeric_limits<int>::max(); 151381ad6265SDimitry Andric int VALUs = 0; 151481ad6265SDimitry Andric }; 151581ad6265SDimitry Andric 151681ad6265SDimitry Andric StateType State; 151781ad6265SDimitry Andric 151881ad6265SDimitry Andric // This overloads expiry testing with all the hazard detection 151981ad6265SDimitry Andric auto IsHazardFn = [&, this](StateType &State, const MachineInstr &I) { 152081ad6265SDimitry Andric // Too many VALU states have passed 152181ad6265SDimitry Andric if (State.VALUs > NoHazardVALUWaitStates) 152281ad6265SDimitry Andric return HazardExpired; 152381ad6265SDimitry Andric 152481ad6265SDimitry Andric // Instructions which cause va_vdst==0 expire hazard 152581ad6265SDimitry Andric if (SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 152681ad6265SDimitry Andric SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I) || 152781ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 152881ad6265SDimitry Andric I.getOperand(0).getImm() == 0x0fff)) 152981ad6265SDimitry Andric return HazardExpired; 153081ad6265SDimitry Andric 153181ad6265SDimitry Andric // Track registers writes 153281ad6265SDimitry Andric bool Changed = false; 153381ad6265SDimitry Andric if (SIInstrInfo::isVALU(I)) { 153481ad6265SDimitry Andric for (Register Src : SrcVGPRs) { 153581ad6265SDimitry Andric if (!State.DefPos.count(Src) && I.modifiesRegister(Src, &TRI)) { 153681ad6265SDimitry Andric State.DefPos[Src] = State.VALUs; 153781ad6265SDimitry Andric Changed = true; 153881ad6265SDimitry Andric } 153981ad6265SDimitry Andric } 154081ad6265SDimitry Andric } else if (SIInstrInfo::isSALU(I)) { 154181ad6265SDimitry Andric if (State.ExecPos == std::numeric_limits<int>::max()) { 154281ad6265SDimitry Andric if (!State.DefPos.empty() && I.modifiesRegister(AMDGPU::EXEC, &TRI)) { 154381ad6265SDimitry Andric State.ExecPos = State.VALUs; 154481ad6265SDimitry Andric Changed = true; 154581ad6265SDimitry Andric } 154681ad6265SDimitry Andric } 154781ad6265SDimitry Andric } 154881ad6265SDimitry Andric 154981ad6265SDimitry Andric // Early expiration: too many VALUs in intv3 155081ad6265SDimitry Andric if (State.VALUs > Intv3MaxVALUs && State.DefPos.empty()) 155181ad6265SDimitry Andric return HazardExpired; 155281ad6265SDimitry Andric 155381ad6265SDimitry Andric // Only evaluate state if something changed 155481ad6265SDimitry Andric if (!Changed) 155581ad6265SDimitry Andric return NoHazardFound; 155681ad6265SDimitry Andric 155781ad6265SDimitry Andric // Determine positions of VALUs pre/post exec change 155881ad6265SDimitry Andric if (State.ExecPos == std::numeric_limits<int>::max()) 155981ad6265SDimitry Andric return NoHazardFound; 156081ad6265SDimitry Andric 156181ad6265SDimitry Andric int PreExecPos = std::numeric_limits<int>::max(); 156281ad6265SDimitry Andric int PostExecPos = std::numeric_limits<int>::max(); 156381ad6265SDimitry Andric 156481ad6265SDimitry Andric for (auto Entry : State.DefPos) { 156581ad6265SDimitry Andric int DefVALUs = Entry.second; 156681ad6265SDimitry Andric if (DefVALUs != std::numeric_limits<int>::max()) { 156781ad6265SDimitry Andric if (DefVALUs >= State.ExecPos) 156881ad6265SDimitry Andric PreExecPos = std::min(PreExecPos, DefVALUs); 156981ad6265SDimitry Andric else if (DefVALUs < State.ExecPos) 157081ad6265SDimitry Andric PostExecPos = std::min(PostExecPos, DefVALUs); 157181ad6265SDimitry Andric } 157281ad6265SDimitry Andric } 157381ad6265SDimitry Andric 157481ad6265SDimitry Andric // Need a VALUs post exec change 157581ad6265SDimitry Andric if (PostExecPos == std::numeric_limits<int>::max()) 157681ad6265SDimitry Andric return NoHazardFound; 157781ad6265SDimitry Andric 157881ad6265SDimitry Andric // Too many VALUs in intv3? 157981ad6265SDimitry Andric int Intv3VALUs = PostExecPos; 158081ad6265SDimitry Andric if (Intv3VALUs > Intv3MaxVALUs) 158181ad6265SDimitry Andric return HazardExpired; 158281ad6265SDimitry Andric 158381ad6265SDimitry Andric // Too many VALUs in intv2? 158481ad6265SDimitry Andric int Intv2VALUs = (State.ExecPos - PostExecPos) - 1; 158581ad6265SDimitry Andric if (Intv2VALUs > Intv1plus2MaxVALUs) 158681ad6265SDimitry Andric return HazardExpired; 158781ad6265SDimitry Andric 158881ad6265SDimitry Andric // Need a VALUs pre exec change 158981ad6265SDimitry Andric if (PreExecPos == std::numeric_limits<int>::max()) 159081ad6265SDimitry Andric return NoHazardFound; 159181ad6265SDimitry Andric 159281ad6265SDimitry Andric // Too many VALUs in intv1? 159381ad6265SDimitry Andric int Intv1VALUs = PreExecPos - State.ExecPos; 159481ad6265SDimitry Andric if (Intv1VALUs > Intv1plus2MaxVALUs) 159581ad6265SDimitry Andric return HazardExpired; 159681ad6265SDimitry Andric 159781ad6265SDimitry Andric // Too many VALUs in intv1 + intv2 159881ad6265SDimitry Andric if (Intv1VALUs + Intv2VALUs > Intv1plus2MaxVALUs) 159981ad6265SDimitry Andric return HazardExpired; 160081ad6265SDimitry Andric 160181ad6265SDimitry Andric return HazardFound; 160281ad6265SDimitry Andric }; 160381ad6265SDimitry Andric auto UpdateStateFn = [](StateType &State, const MachineInstr &MI) { 160481ad6265SDimitry Andric if (SIInstrInfo::isVALU(MI)) 160581ad6265SDimitry Andric State.VALUs += 1; 160681ad6265SDimitry Andric }; 160781ad6265SDimitry Andric 160881ad6265SDimitry Andric DenseSet<const MachineBasicBlock *> Visited; 160981ad6265SDimitry Andric if (!hasHazard<StateType>(State, IsHazardFn, UpdateStateFn, MI->getParent(), 161081ad6265SDimitry Andric std::next(MI->getReverseIterator()), Visited)) 161181ad6265SDimitry Andric return false; 161281ad6265SDimitry Andric 161381ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 161481ad6265SDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 161581ad6265SDimitry Andric .addImm(0x0fff); 161681ad6265SDimitry Andric 161781ad6265SDimitry Andric return true; 161881ad6265SDimitry Andric } 161981ad6265SDimitry Andric 162081ad6265SDimitry Andric bool GCNHazardRecognizer::fixVALUTransUseHazard(MachineInstr *MI) { 162181ad6265SDimitry Andric if (!ST.hasVALUTransUseHazard()) 162281ad6265SDimitry Andric return false; 162381ad6265SDimitry Andric if (!SIInstrInfo::isVALU(*MI)) 162481ad6265SDimitry Andric return false; 162581ad6265SDimitry Andric 162681ad6265SDimitry Andric SmallSet<Register, 4> SrcVGPRs; 162781ad6265SDimitry Andric 162881ad6265SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 162981ad6265SDimitry Andric if (Use.isReg() && TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 163081ad6265SDimitry Andric SrcVGPRs.insert(Use.getReg()); 163181ad6265SDimitry Andric } 163281ad6265SDimitry Andric 163381ad6265SDimitry Andric // Look for the following pattern: 163481ad6265SDimitry Andric // Va <- TRANS VALU 163581ad6265SDimitry Andric // intv 163681ad6265SDimitry Andric // MI Va (WaitState = 0) 163781ad6265SDimitry Andric // 163881ad6265SDimitry Andric // Where: 163981ad6265SDimitry Andric // intv <= 5 VALUs / 1 TRANS 164081ad6265SDimitry Andric // 164181ad6265SDimitry Andric // If found, insert an appropriate S_WAITCNT_DEPCTR before MI. 164281ad6265SDimitry Andric 164381ad6265SDimitry Andric const int IntvMaxVALUs = 5; 164481ad6265SDimitry Andric const int IntvMaxTRANS = 1; 164581ad6265SDimitry Andric 164681ad6265SDimitry Andric struct StateType { 164781ad6265SDimitry Andric int VALUs = 0; 164881ad6265SDimitry Andric int TRANS = 0; 164981ad6265SDimitry Andric }; 165081ad6265SDimitry Andric 165181ad6265SDimitry Andric StateType State; 165281ad6265SDimitry Andric 165381ad6265SDimitry Andric // This overloads expiry testing with all the hazard detection 165481ad6265SDimitry Andric auto IsHazardFn = [&, this](StateType &State, const MachineInstr &I) { 165581ad6265SDimitry Andric // Too many VALU states have passed 165681ad6265SDimitry Andric if (State.VALUs > IntvMaxVALUs || State.TRANS > IntvMaxTRANS) 165781ad6265SDimitry Andric return HazardExpired; 165881ad6265SDimitry Andric 165981ad6265SDimitry Andric // Instructions which cause va_vdst==0 expire hazard 166081ad6265SDimitry Andric if (SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 166181ad6265SDimitry Andric SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I) || 166281ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 166381ad6265SDimitry Andric I.getOperand(0).getImm() == 0x0fff)) 166481ad6265SDimitry Andric return HazardExpired; 166581ad6265SDimitry Andric 166681ad6265SDimitry Andric // Track registers writes 166781ad6265SDimitry Andric if (SIInstrInfo::isTRANS(I)) { 166881ad6265SDimitry Andric for (Register Src : SrcVGPRs) { 166981ad6265SDimitry Andric if (I.modifiesRegister(Src, &TRI)) { 167081ad6265SDimitry Andric return HazardFound; 167181ad6265SDimitry Andric } 167281ad6265SDimitry Andric } 167381ad6265SDimitry Andric } 167481ad6265SDimitry Andric 167581ad6265SDimitry Andric return NoHazardFound; 167681ad6265SDimitry Andric }; 167781ad6265SDimitry Andric auto UpdateStateFn = [](StateType &State, const MachineInstr &MI) { 167881ad6265SDimitry Andric if (SIInstrInfo::isVALU(MI)) 167981ad6265SDimitry Andric State.VALUs += 1; 168081ad6265SDimitry Andric if (SIInstrInfo::isTRANS(MI)) 168181ad6265SDimitry Andric State.TRANS += 1; 168281ad6265SDimitry Andric }; 168381ad6265SDimitry Andric 168481ad6265SDimitry Andric DenseSet<const MachineBasicBlock *> Visited; 168581ad6265SDimitry Andric if (!hasHazard<StateType>(State, IsHazardFn, UpdateStateFn, MI->getParent(), 168681ad6265SDimitry Andric std::next(MI->getReverseIterator()), Visited)) 168781ad6265SDimitry Andric return false; 168881ad6265SDimitry Andric 168981ad6265SDimitry Andric // Hazard is observed - insert a wait on va_dst counter to ensure hazard is 169081ad6265SDimitry Andric // avoided (mask 0x0fff achieves this). 169181ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 169281ad6265SDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 169381ad6265SDimitry Andric .addImm(0x0fff); 169481ad6265SDimitry Andric 169581ad6265SDimitry Andric return true; 169681ad6265SDimitry Andric } 169781ad6265SDimitry Andric 169881ad6265SDimitry Andric bool GCNHazardRecognizer::fixWMMAHazards(MachineInstr *MI) { 169981ad6265SDimitry Andric if (!SIInstrInfo::isWMMA(*MI)) 170081ad6265SDimitry Andric return false; 170181ad6265SDimitry Andric 170281ad6265SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 170381ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 170481ad6265SDimitry Andric 170581ad6265SDimitry Andric auto IsHazardFn = [MI, TII, TRI](const MachineInstr &I) { 170681ad6265SDimitry Andric if (!SIInstrInfo::isWMMA(I)) 170781ad6265SDimitry Andric return false; 170881ad6265SDimitry Andric 170981ad6265SDimitry Andric // Src0 or Src1 of the current wmma instruction overlaps with the dest of 171081ad6265SDimitry Andric // the previous wmma. 171181ad6265SDimitry Andric const Register CurSrc0Reg = 171281ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src0)->getReg(); 171381ad6265SDimitry Andric const Register CurSrc1Reg = 171481ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src1)->getReg(); 171581ad6265SDimitry Andric 171681ad6265SDimitry Andric const Register PrevDstReg = 171781ad6265SDimitry Andric TII->getNamedOperand(I, AMDGPU::OpName::vdst)->getReg(); 171881ad6265SDimitry Andric 171981ad6265SDimitry Andric if (TRI->regsOverlap(PrevDstReg, CurSrc0Reg) || 172081ad6265SDimitry Andric TRI->regsOverlap(PrevDstReg, CurSrc1Reg)) { 172181ad6265SDimitry Andric return true; 172281ad6265SDimitry Andric } 172381ad6265SDimitry Andric 172481ad6265SDimitry Andric // Src2 of the current wmma instruction overlaps with the dest of the 172581ad6265SDimitry Andric // previous wmma. 172681ad6265SDimitry Andric const MachineOperand *Src2 = 172781ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src2); 172881ad6265SDimitry Andric const Register CurSrc2Reg = Src2->isReg() ? Src2->getReg() : Register(); 172981ad6265SDimitry Andric 173081ad6265SDimitry Andric if (CurSrc2Reg != AMDGPU::NoRegister && 173181ad6265SDimitry Andric TRI->regsOverlap(PrevDstReg, CurSrc2Reg)) { 173281ad6265SDimitry Andric 173381ad6265SDimitry Andric const MachineOperand *Src2Mods = 173481ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src2_modifiers); 173581ad6265SDimitry Andric const bool NoSrc2Mods = 173681ad6265SDimitry Andric (Src2Mods->getImm() & (SISrcMods::NEG | SISrcMods::NEG_HI)) == 0; 173781ad6265SDimitry Andric // Exception: there is no hazard if the wmma instructions are of the same 173881ad6265SDimitry Andric // type and there is no input modifier on src2 of the current instruction. 173981ad6265SDimitry Andric return !(NoSrc2Mods && (TII->pseudoToMCOpcode(I.getOpcode()) == 174081ad6265SDimitry Andric TII->pseudoToMCOpcode(MI->getOpcode()))); 174181ad6265SDimitry Andric } 174281ad6265SDimitry Andric 174381ad6265SDimitry Andric return false; 174481ad6265SDimitry Andric }; 174581ad6265SDimitry Andric 174681ad6265SDimitry Andric auto IsExpiredFn = [](const MachineInstr &I, int) { 174781ad6265SDimitry Andric return SIInstrInfo::isVALU(I); 174881ad6265SDimitry Andric }; 174981ad6265SDimitry Andric 175081ad6265SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 175181ad6265SDimitry Andric std::numeric_limits<int>::max()) 175281ad6265SDimitry Andric return false; 175381ad6265SDimitry Andric 175481ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII->get(AMDGPU::V_NOP_e32)); 175581ad6265SDimitry Andric 175681ad6265SDimitry Andric return true; 175781ad6265SDimitry Andric } 175881ad6265SDimitry Andric 1759*bdd1243dSDimitry Andric bool GCNHazardRecognizer::fixShift64HighRegBug(MachineInstr *MI) { 1760*bdd1243dSDimitry Andric if (!ST.hasShift64HighRegBug()) 1761*bdd1243dSDimitry Andric return false; 1762*bdd1243dSDimitry Andric 1763*bdd1243dSDimitry Andric switch (MI->getOpcode()) { 1764*bdd1243dSDimitry Andric default: 1765*bdd1243dSDimitry Andric return false; 1766*bdd1243dSDimitry Andric case AMDGPU::V_LSHLREV_B64_e64: 1767*bdd1243dSDimitry Andric case AMDGPU::V_LSHRREV_B64_e64: 1768*bdd1243dSDimitry Andric case AMDGPU::V_ASHRREV_I64_e64: 1769*bdd1243dSDimitry Andric break; 1770*bdd1243dSDimitry Andric } 1771*bdd1243dSDimitry Andric 1772*bdd1243dSDimitry Andric MachineOperand *Amt = TII.getNamedOperand(*MI, AMDGPU::OpName::src0); 1773*bdd1243dSDimitry Andric if (!Amt->isReg()) 1774*bdd1243dSDimitry Andric return false; 1775*bdd1243dSDimitry Andric 1776*bdd1243dSDimitry Andric Register AmtReg = Amt->getReg(); 1777*bdd1243dSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 1778*bdd1243dSDimitry Andric // Check if this is a last VGPR in the allocation block. 1779*bdd1243dSDimitry Andric if (!TRI.isVGPR(MRI, AmtReg) || ((AmtReg - AMDGPU::VGPR0) & 7) != 7) 1780*bdd1243dSDimitry Andric return false; 1781*bdd1243dSDimitry Andric 1782*bdd1243dSDimitry Andric if (AmtReg != AMDGPU::VGPR255 && MRI.isPhysRegUsed(AmtReg + 1)) 1783*bdd1243dSDimitry Andric return false; 1784*bdd1243dSDimitry Andric 1785*bdd1243dSDimitry Andric MachineOperand *Src1 = TII.getNamedOperand(*MI, AMDGPU::OpName::src1); 1786*bdd1243dSDimitry Andric bool OverlappedSrc = Src1->isReg() && TRI.regsOverlap(Src1->getReg(), AmtReg); 1787*bdd1243dSDimitry Andric bool OverlappedDst = MI->modifiesRegister(AmtReg, &TRI); 1788*bdd1243dSDimitry Andric bool Overlapped = OverlappedSrc || OverlappedDst; 1789*bdd1243dSDimitry Andric 1790*bdd1243dSDimitry Andric assert(!OverlappedDst || !OverlappedSrc || 1791*bdd1243dSDimitry Andric Src1->getReg() == MI->getOperand(0).getReg()); 1792*bdd1243dSDimitry Andric assert(ST.needsAlignedVGPRs()); 1793*bdd1243dSDimitry Andric static_assert(AMDGPU::VGPR0 + 1 == AMDGPU::VGPR1); 1794*bdd1243dSDimitry Andric 1795*bdd1243dSDimitry Andric Register NewReg; 1796*bdd1243dSDimitry Andric for (MCRegister Reg : Overlapped ? AMDGPU::VReg_64_Align2RegClass 1797*bdd1243dSDimitry Andric : AMDGPU::VGPR_32RegClass) { 1798*bdd1243dSDimitry Andric if (!MI->modifiesRegister(Reg, &TRI) && !MI->readsRegister(Reg, &TRI)) { 1799*bdd1243dSDimitry Andric NewReg = Reg; 1800*bdd1243dSDimitry Andric break; 1801*bdd1243dSDimitry Andric } 1802*bdd1243dSDimitry Andric } 1803*bdd1243dSDimitry Andric 1804*bdd1243dSDimitry Andric Register NewAmt = Overlapped ? (Register)TRI.getSubReg(NewReg, AMDGPU::sub1) 1805*bdd1243dSDimitry Andric : NewReg; 1806*bdd1243dSDimitry Andric Register NewAmtLo; 1807*bdd1243dSDimitry Andric 1808*bdd1243dSDimitry Andric if (Overlapped) 1809*bdd1243dSDimitry Andric NewAmtLo = TRI.getSubReg(NewReg, AMDGPU::sub0); 1810*bdd1243dSDimitry Andric 1811*bdd1243dSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 1812*bdd1243dSDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 1813*bdd1243dSDimitry Andric // Insert a full wait count because found register might be pending a wait. 1814*bdd1243dSDimitry Andric BuildMI(*MBB, MI, DL, TII.get(AMDGPU::S_WAITCNT)) 1815*bdd1243dSDimitry Andric .addImm(0); 1816*bdd1243dSDimitry Andric 1817*bdd1243dSDimitry Andric // Insert V_SWAP_B32 instruction(s) and run hazard recognizer on them. 1818*bdd1243dSDimitry Andric if (Overlapped) 1819*bdd1243dSDimitry Andric runOnInstruction( 1820*bdd1243dSDimitry Andric BuildMI(*MBB, MI, DL, TII.get(AMDGPU::V_SWAP_B32), NewAmtLo) 1821*bdd1243dSDimitry Andric .addDef(AmtReg - 1) 1822*bdd1243dSDimitry Andric .addReg(AmtReg - 1, RegState::Undef) 1823*bdd1243dSDimitry Andric .addReg(NewAmtLo, RegState::Undef)); 1824*bdd1243dSDimitry Andric runOnInstruction(BuildMI(*MBB, MI, DL, TII.get(AMDGPU::V_SWAP_B32), NewAmt) 1825*bdd1243dSDimitry Andric .addDef(AmtReg) 1826*bdd1243dSDimitry Andric .addReg(AmtReg, RegState::Undef) 1827*bdd1243dSDimitry Andric .addReg(NewAmt, RegState::Undef)); 1828*bdd1243dSDimitry Andric 1829*bdd1243dSDimitry Andric // Instructions emitted after the current instruction will be processed by the 1830*bdd1243dSDimitry Andric // parent loop of the hazard recognizer in a natural way. 1831*bdd1243dSDimitry Andric BuildMI(*MBB, std::next(MI->getIterator()), DL, TII.get(AMDGPU::V_SWAP_B32), 1832*bdd1243dSDimitry Andric AmtReg) 1833*bdd1243dSDimitry Andric .addDef(NewAmt) 1834*bdd1243dSDimitry Andric .addReg(NewAmt) 1835*bdd1243dSDimitry Andric .addReg(AmtReg); 1836*bdd1243dSDimitry Andric if (Overlapped) 1837*bdd1243dSDimitry Andric BuildMI(*MBB, std::next(MI->getIterator()), DL, TII.get(AMDGPU::V_SWAP_B32), 1838*bdd1243dSDimitry Andric AmtReg - 1) 1839*bdd1243dSDimitry Andric .addDef(NewAmtLo) 1840*bdd1243dSDimitry Andric .addReg(NewAmtLo) 1841*bdd1243dSDimitry Andric .addReg(AmtReg - 1); 1842*bdd1243dSDimitry Andric 1843*bdd1243dSDimitry Andric // Re-running hazard recognizer on the modified instruction is not necessary, 1844*bdd1243dSDimitry Andric // inserted V_SWAP_B32 has already both read and write new registers so 1845*bdd1243dSDimitry Andric // hazards related to these register has already been handled. 1846*bdd1243dSDimitry Andric Amt->setReg(NewAmt); 1847*bdd1243dSDimitry Andric Amt->setIsKill(false); 1848*bdd1243dSDimitry Andric // We do not update liveness, so verifier may see it as undef. 1849*bdd1243dSDimitry Andric Amt->setIsUndef(); 1850*bdd1243dSDimitry Andric if (OverlappedDst) 1851*bdd1243dSDimitry Andric MI->getOperand(0).setReg(NewReg); 1852*bdd1243dSDimitry Andric if (OverlappedSrc) { 1853*bdd1243dSDimitry Andric Src1->setReg(NewReg); 1854*bdd1243dSDimitry Andric Src1->setIsKill(false); 1855*bdd1243dSDimitry Andric Src1->setIsUndef(); 1856*bdd1243dSDimitry Andric } 1857*bdd1243dSDimitry Andric 1858*bdd1243dSDimitry Andric return true; 1859*bdd1243dSDimitry Andric } 1860*bdd1243dSDimitry Andric 18610b57cec5SDimitry Andric int GCNHazardRecognizer::checkNSAtoVMEMHazard(MachineInstr *MI) { 18620b57cec5SDimitry Andric int NSAtoVMEMWaitStates = 1; 18630b57cec5SDimitry Andric 18640b57cec5SDimitry Andric if (!ST.hasNSAtoVMEMBug()) 18650b57cec5SDimitry Andric return 0; 18660b57cec5SDimitry Andric 18670b57cec5SDimitry Andric if (!SIInstrInfo::isMUBUF(*MI) && !SIInstrInfo::isMTBUF(*MI)) 18680b57cec5SDimitry Andric return 0; 18690b57cec5SDimitry Andric 18700b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 18710b57cec5SDimitry Andric const auto *Offset = TII->getNamedOperand(*MI, AMDGPU::OpName::offset); 18720b57cec5SDimitry Andric if (!Offset || (Offset->getImm() & 6) == 0) 18730b57cec5SDimitry Andric return 0; 18740b57cec5SDimitry Andric 1875fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &I) { 1876fe6060f1SDimitry Andric if (!SIInstrInfo::isMIMG(I)) 18770b57cec5SDimitry Andric return false; 1878fe6060f1SDimitry Andric const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(I.getOpcode()); 18790b57cec5SDimitry Andric return Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA && 1880fe6060f1SDimitry Andric TII->getInstSizeInBytes(I) >= 16; 18810b57cec5SDimitry Andric }; 18820b57cec5SDimitry Andric 18830b57cec5SDimitry Andric return NSAtoVMEMWaitStates - getWaitStatesSince(IsHazardFn, 1); 18840b57cec5SDimitry Andric } 18850b57cec5SDimitry Andric 18860b57cec5SDimitry Andric int GCNHazardRecognizer::checkFPAtomicToDenormModeHazard(MachineInstr *MI) { 18870b57cec5SDimitry Andric int FPAtomicToDenormModeWaitStates = 3; 18880b57cec5SDimitry Andric 1889*bdd1243dSDimitry Andric if (!ST.hasFPAtomicToDenormModeHazard()) 1890*bdd1243dSDimitry Andric return 0; 1891*bdd1243dSDimitry Andric 18920b57cec5SDimitry Andric if (MI->getOpcode() != AMDGPU::S_DENORM_MODE) 18930b57cec5SDimitry Andric return 0; 18940b57cec5SDimitry Andric 1895fe6060f1SDimitry Andric auto IsHazardFn = [](const MachineInstr &I) { 1896fe6060f1SDimitry Andric if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isFLAT(I)) 18970b57cec5SDimitry Andric return false; 1898fe6060f1SDimitry Andric return SIInstrInfo::isFPAtomic(I); 18990b57cec5SDimitry Andric }; 19000b57cec5SDimitry Andric 1901fe6060f1SDimitry Andric auto IsExpiredFn = [](const MachineInstr &MI, int WaitStates) { 1902fe6060f1SDimitry Andric if (WaitStates >= 3 || SIInstrInfo::isVALU(MI)) 19030b57cec5SDimitry Andric return true; 19040b57cec5SDimitry Andric 1905fe6060f1SDimitry Andric switch (MI.getOpcode()) { 19060b57cec5SDimitry Andric case AMDGPU::S_WAITCNT: 19070b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VSCNT: 19080b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VMCNT: 19090b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_EXPCNT: 19100b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_LGKMCNT: 1911e8d8bef9SDimitry Andric case AMDGPU::S_WAIT_IDLE: 19120b57cec5SDimitry Andric return true; 19130b57cec5SDimitry Andric default: 19140b57cec5SDimitry Andric break; 19150b57cec5SDimitry Andric } 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric return false; 19180b57cec5SDimitry Andric }; 19190b57cec5SDimitry Andric 19200b57cec5SDimitry Andric return FPAtomicToDenormModeWaitStates - 19210b57cec5SDimitry Andric ::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn); 19220b57cec5SDimitry Andric } 19230b57cec5SDimitry Andric 19240b57cec5SDimitry Andric int GCNHazardRecognizer::checkMAIHazards(MachineInstr *MI) { 19250b57cec5SDimitry Andric assert(SIInstrInfo::isMAI(*MI)); 19260b57cec5SDimitry Andric 1927fe6060f1SDimitry Andric return ST.hasGFX90AInsts() ? checkMAIHazards90A(MI) : checkMAIHazards908(MI); 1928fe6060f1SDimitry Andric } 1929fe6060f1SDimitry Andric 193081ad6265SDimitry Andric int GCNHazardRecognizer::checkMFMAPadding(MachineInstr *MI) { 193181ad6265SDimitry Andric // Early exit if no padding is requested. 193281ad6265SDimitry Andric if (MFMAPaddingRatio == 0) 193381ad6265SDimitry Andric return 0; 193481ad6265SDimitry Andric 193581ad6265SDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 193681ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(*MI) || MFI->getOccupancy() < 2) 193781ad6265SDimitry Andric return 0; 193881ad6265SDimitry Andric 193981ad6265SDimitry Andric int NeighborMFMALatency = 0; 194081ad6265SDimitry Andric auto IsNeighboringMFMA = [&NeighborMFMALatency, 194181ad6265SDimitry Andric this](const MachineInstr &MI) { 194281ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 194381ad6265SDimitry Andric return false; 194481ad6265SDimitry Andric 194581ad6265SDimitry Andric NeighborMFMALatency = this->getMFMAPipelineWaitStates(MI); 194681ad6265SDimitry Andric return true; 194781ad6265SDimitry Andric }; 194881ad6265SDimitry Andric 194981ad6265SDimitry Andric const int MaxMFMAPipelineWaitStates = 16; 195081ad6265SDimitry Andric int WaitStatesSinceNeighborMFMA = 195181ad6265SDimitry Andric getWaitStatesSince(IsNeighboringMFMA, MaxMFMAPipelineWaitStates); 195281ad6265SDimitry Andric 195381ad6265SDimitry Andric int NeighborMFMAPaddingNeeded = 195481ad6265SDimitry Andric (NeighborMFMALatency * MFMAPaddingRatio / 100) - 195581ad6265SDimitry Andric WaitStatesSinceNeighborMFMA; 195681ad6265SDimitry Andric 195781ad6265SDimitry Andric return std::max(0, NeighborMFMAPaddingNeeded); 195881ad6265SDimitry Andric } 195981ad6265SDimitry Andric 1960fe6060f1SDimitry Andric int GCNHazardRecognizer::checkMAIHazards908(MachineInstr *MI) { 19610b57cec5SDimitry Andric int WaitStatesNeeded = 0; 19620b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 19630b57cec5SDimitry Andric 1964fe6060f1SDimitry Andric auto IsVALUFn = [](const MachineInstr &MI) { 1965*bdd1243dSDimitry Andric return SIInstrInfo::isVALU(MI) || MI.isInlineAsm(); 19660b57cec5SDimitry Andric }; 19670b57cec5SDimitry Andric 1968e8d8bef9SDimitry Andric if (Opc != AMDGPU::V_ACCVGPR_READ_B32_e64) { // MFMA or v_accvgpr_write 19690b57cec5SDimitry Andric const int LegacyVALUWritesVGPRWaitStates = 2; 19700b57cec5SDimitry Andric const int VALUWritesExecWaitStates = 4; 19710b57cec5SDimitry Andric const int MaxWaitStates = 4; 19720b57cec5SDimitry Andric 19730b57cec5SDimitry Andric int WaitStatesNeededForUse = VALUWritesExecWaitStates - 19740b57cec5SDimitry Andric getWaitStatesSinceDef(AMDGPU::EXEC, IsVALUFn, MaxWaitStates); 19750b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 19760b57cec5SDimitry Andric 19770b57cec5SDimitry Andric if (WaitStatesNeeded < MaxWaitStates) { 19780b57cec5SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 19790b57cec5SDimitry Andric const int MaxWaitStates = 2; 19800b57cec5SDimitry Andric 19810b57cec5SDimitry Andric if (!Use.isReg() || !TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 19820b57cec5SDimitry Andric continue; 19830b57cec5SDimitry Andric 19840b57cec5SDimitry Andric int WaitStatesNeededForUse = LegacyVALUWritesVGPRWaitStates - 19850b57cec5SDimitry Andric getWaitStatesSinceDef(Use.getReg(), IsVALUFn, MaxWaitStates); 19860b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 19870b57cec5SDimitry Andric 19880b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 19890b57cec5SDimitry Andric break; 19900b57cec5SDimitry Andric } 19910b57cec5SDimitry Andric } 19920b57cec5SDimitry Andric } 19930b57cec5SDimitry Andric 19940b57cec5SDimitry Andric for (const MachineOperand &Op : MI->explicit_operands()) { 19950b57cec5SDimitry Andric if (!Op.isReg() || !TRI.isAGPR(MF.getRegInfo(), Op.getReg())) 19960b57cec5SDimitry Andric continue; 19970b57cec5SDimitry Andric 1998e8d8bef9SDimitry Andric if (Op.isDef() && Opc != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 19990b57cec5SDimitry Andric continue; 20000b57cec5SDimitry Andric 20010b57cec5SDimitry Andric const int MFMAWritesAGPROverlappedSrcABWaitStates = 4; 20020b57cec5SDimitry Andric const int MFMAWritesAGPROverlappedSrcCWaitStates = 2; 20030b57cec5SDimitry Andric const int MFMA4x4WritesAGPRAccVgprReadWaitStates = 4; 20040b57cec5SDimitry Andric const int MFMA16x16WritesAGPRAccVgprReadWaitStates = 10; 20050b57cec5SDimitry Andric const int MFMA32x32WritesAGPRAccVgprReadWaitStates = 18; 20060b57cec5SDimitry Andric const int MFMA4x4WritesAGPRAccVgprWriteWaitStates = 1; 20070b57cec5SDimitry Andric const int MFMA16x16WritesAGPRAccVgprWriteWaitStates = 7; 20080b57cec5SDimitry Andric const int MFMA32x32WritesAGPRAccVgprWriteWaitStates = 15; 20090b57cec5SDimitry Andric const int MaxWaitStates = 18; 20108bcb0991SDimitry Andric Register Reg = Op.getReg(); 20110b57cec5SDimitry Andric unsigned HazardDefLatency = 0; 20120b57cec5SDimitry Andric 201381ad6265SDimitry Andric auto IsOverlappedMFMAFn = [Reg, &HazardDefLatency, 2014fe6060f1SDimitry Andric this](const MachineInstr &MI) { 201581ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 20160b57cec5SDimitry Andric return false; 2017fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 20180b57cec5SDimitry Andric if (DstReg == Reg) 20190b57cec5SDimitry Andric return false; 2020fe6060f1SDimitry Andric HazardDefLatency = 2021fe6060f1SDimitry Andric std::max(HazardDefLatency, TSchedModel.computeInstrLatency(&MI)); 20220b57cec5SDimitry Andric return TRI.regsOverlap(DstReg, Reg); 20230b57cec5SDimitry Andric }; 20240b57cec5SDimitry Andric 20250b57cec5SDimitry Andric int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsOverlappedMFMAFn, 20260b57cec5SDimitry Andric MaxWaitStates); 20270b57cec5SDimitry Andric int NeedWaitStates = MFMAWritesAGPROverlappedSrcABWaitStates; 20280b57cec5SDimitry Andric int SrcCIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 20290b57cec5SDimitry Andric int OpNo = MI->getOperandNo(&Op); 20300b57cec5SDimitry Andric if (OpNo == SrcCIdx) { 20310b57cec5SDimitry Andric NeedWaitStates = MFMAWritesAGPROverlappedSrcCWaitStates; 2032e8d8bef9SDimitry Andric } else if (Opc == AMDGPU::V_ACCVGPR_READ_B32_e64) { 20330b57cec5SDimitry Andric switch (HazardDefLatency) { 20340b57cec5SDimitry Andric case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprReadWaitStates; 20350b57cec5SDimitry Andric break; 20360b57cec5SDimitry Andric case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprReadWaitStates; 20370b57cec5SDimitry Andric break; 2038*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 20390b57cec5SDimitry Andric default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprReadWaitStates; 20400b57cec5SDimitry Andric break; 20410b57cec5SDimitry Andric } 2042e8d8bef9SDimitry Andric } else if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64) { 20430b57cec5SDimitry Andric switch (HazardDefLatency) { 20440b57cec5SDimitry Andric case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprWriteWaitStates; 20450b57cec5SDimitry Andric break; 20460b57cec5SDimitry Andric case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprWriteWaitStates; 20470b57cec5SDimitry Andric break; 2048*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 20490b57cec5SDimitry Andric default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprWriteWaitStates; 20500b57cec5SDimitry Andric break; 20510b57cec5SDimitry Andric } 20520b57cec5SDimitry Andric } 20530b57cec5SDimitry Andric 20540b57cec5SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 20550b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 20560b57cec5SDimitry Andric 20570b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 20580b57cec5SDimitry Andric return WaitStatesNeeded; // Early exit. 20590b57cec5SDimitry Andric 2060fe6060f1SDimitry Andric auto IsAccVgprWriteFn = [Reg, this](const MachineInstr &MI) { 2061fe6060f1SDimitry Andric if (MI.getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 20620b57cec5SDimitry Andric return false; 2063fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 20640b57cec5SDimitry Andric return TRI.regsOverlap(Reg, DstReg); 20650b57cec5SDimitry Andric }; 20660b57cec5SDimitry Andric 20670b57cec5SDimitry Andric const int AccVGPRWriteMFMAReadSrcCWaitStates = 1; 20680b57cec5SDimitry Andric const int AccVGPRWriteMFMAReadSrcABWaitStates = 3; 20690b57cec5SDimitry Andric const int AccVGPRWriteAccVgprReadWaitStates = 3; 20700b57cec5SDimitry Andric NeedWaitStates = AccVGPRWriteMFMAReadSrcABWaitStates; 20710b57cec5SDimitry Andric if (OpNo == SrcCIdx) 20720b57cec5SDimitry Andric NeedWaitStates = AccVGPRWriteMFMAReadSrcCWaitStates; 2073e8d8bef9SDimitry Andric else if (Opc == AMDGPU::V_ACCVGPR_READ_B32_e64) 20740b57cec5SDimitry Andric NeedWaitStates = AccVGPRWriteAccVgprReadWaitStates; 20750b57cec5SDimitry Andric 20760b57cec5SDimitry Andric WaitStatesNeededForUse = NeedWaitStates - 20770b57cec5SDimitry Andric getWaitStatesSinceDef(Reg, IsAccVgprWriteFn, MaxWaitStates); 20780b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 20790b57cec5SDimitry Andric 20800b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 20810b57cec5SDimitry Andric return WaitStatesNeeded; // Early exit. 20820b57cec5SDimitry Andric } 20830b57cec5SDimitry Andric 2084e8d8bef9SDimitry Andric if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64) { 20850b57cec5SDimitry Andric const int MFMA4x4ReadSrcCAccVgprWriteWaitStates = 0; 20860b57cec5SDimitry Andric const int MFMA16x16ReadSrcCAccVgprWriteWaitStates = 5; 20870b57cec5SDimitry Andric const int MFMA32x32ReadSrcCAccVgprWriteWaitStates = 13; 20880b57cec5SDimitry Andric const int MaxWaitStates = 13; 20898bcb0991SDimitry Andric Register DstReg = MI->getOperand(0).getReg(); 20900b57cec5SDimitry Andric unsigned HazardDefLatency = 0; 20910b57cec5SDimitry Andric 209281ad6265SDimitry Andric auto IsSrcCMFMAFn = [DstReg, &HazardDefLatency, 2093fe6060f1SDimitry Andric this](const MachineInstr &MI) { 209481ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 20950b57cec5SDimitry Andric return false; 2096fe6060f1SDimitry Andric Register Reg = TII.getNamedOperand(MI, AMDGPU::OpName::src2)->getReg(); 2097fe6060f1SDimitry Andric HazardDefLatency = 2098fe6060f1SDimitry Andric std::max(HazardDefLatency, TSchedModel.computeInstrLatency(&MI)); 20990b57cec5SDimitry Andric return TRI.regsOverlap(Reg, DstReg); 21000b57cec5SDimitry Andric }; 21010b57cec5SDimitry Andric 21020b57cec5SDimitry Andric int WaitStatesSince = getWaitStatesSince(IsSrcCMFMAFn, MaxWaitStates); 21030b57cec5SDimitry Andric int NeedWaitStates; 21040b57cec5SDimitry Andric switch (HazardDefLatency) { 21050b57cec5SDimitry Andric case 2: NeedWaitStates = MFMA4x4ReadSrcCAccVgprWriteWaitStates; 21060b57cec5SDimitry Andric break; 21070b57cec5SDimitry Andric case 8: NeedWaitStates = MFMA16x16ReadSrcCAccVgprWriteWaitStates; 21080b57cec5SDimitry Andric break; 2109*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 21100b57cec5SDimitry Andric default: NeedWaitStates = MFMA32x32ReadSrcCAccVgprWriteWaitStates; 21110b57cec5SDimitry Andric break; 21120b57cec5SDimitry Andric } 21130b57cec5SDimitry Andric 21140b57cec5SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSince; 21150b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 21160b57cec5SDimitry Andric } 21170b57cec5SDimitry Andric 211881ad6265SDimitry Andric // Pad neighboring MFMA with noops for better inter-wave performance. 211981ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, checkMFMAPadding(MI)); 212081ad6265SDimitry Andric 21210b57cec5SDimitry Andric return WaitStatesNeeded; 21220b57cec5SDimitry Andric } 21230b57cec5SDimitry Andric 2124fe6060f1SDimitry Andric int GCNHazardRecognizer::checkMAIHazards90A(MachineInstr *MI) { 2125fe6060f1SDimitry Andric int WaitStatesNeeded = 0; 2126fe6060f1SDimitry Andric unsigned Opc = MI->getOpcode(); 2127fe6060f1SDimitry Andric 212881ad6265SDimitry Andric auto IsLegacyVALUFn = [](const MachineInstr &MI) { 212981ad6265SDimitry Andric return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMFMA(MI); 2130fe6060f1SDimitry Andric }; 2131fe6060f1SDimitry Andric 213281ad6265SDimitry Andric auto IsLegacyVALUNotDotFn = [](const MachineInstr &MI) { 213381ad6265SDimitry Andric return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMFMA(MI) && 213481ad6265SDimitry Andric !SIInstrInfo::isDOT(MI); 2135fe6060f1SDimitry Andric }; 2136fe6060f1SDimitry Andric 213781ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(*MI)) 2138fe6060f1SDimitry Andric return WaitStatesNeeded; 2139fe6060f1SDimitry Andric 2140fe6060f1SDimitry Andric const int VALUWritesExecWaitStates = 4; 2141fe6060f1SDimitry Andric int WaitStatesNeededForUse = VALUWritesExecWaitStates - 2142fe6060f1SDimitry Andric getWaitStatesSinceDef(AMDGPU::EXEC, IsLegacyVALUFn, 2143fe6060f1SDimitry Andric VALUWritesExecWaitStates); 2144fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2145fe6060f1SDimitry Andric 2146fe6060f1SDimitry Andric int SrcCIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 2147fe6060f1SDimitry Andric 2148fe6060f1SDimitry Andric // Loop for both DGEMM and S/HGEMM 2nd instruction. 2149fe6060f1SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 2150fe6060f1SDimitry Andric const int LegacyVALUNotDotWritesVGPRWaitStates = 2; 2151fe6060f1SDimitry Andric const int SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates = 2; 215281ad6265SDimitry Andric const int GFX940_XDL2PassWritesVGPROverlappedSMFMASrcCWaitStates = 3; 215381ad6265SDimitry Andric const int GFX940_XDL4PassWritesVGPROverlappedSMFMASrcCWaitStates = 5; 215481ad6265SDimitry Andric const int GFX940_SMFMA4PassWritesVGPROverlappedSMFMASrcCWaitStates = 4; 215581ad6265SDimitry Andric const int GFX940_XDL8PassWritesVGPROverlappedSMFMASrcCWaitStates = 9; 215681ad6265SDimitry Andric const int GFX940_SMFMA8PassWritesVGPROverlappedSMFMASrcCWaitStates = 8; 215781ad6265SDimitry Andric const int GFX940_XDL16PassWritesVGPROverlappedSMFMASrcCWaitStates = 17; 215881ad6265SDimitry Andric const int GFX940_SMFMA16PassWritesVGPROverlappedSMFMASrcCWaitStates = 16; 2159fe6060f1SDimitry Andric const int SMFMA16x16WritesVGPROverlappedSMFMASrcCWaitStates = 8; 2160fe6060f1SDimitry Andric const int SMFMA32x32WritesVGPROverlappedSMFMASrcCWaitStates = 16; 2161fe6060f1SDimitry Andric const int SMFMA4x4WritesVGPROverlappedDMFMASrcCWaitStates = 3; 2162fe6060f1SDimitry Andric const int SMFMA16x16WritesVGPROverlappedDMFMASrcCWaitStates = 9; 2163fe6060f1SDimitry Andric const int SMFMA32x32WritesVGPROverlappedDMFMASrcCWaitStates = 17; 2164fe6060f1SDimitry Andric const int DMFMA16x16WritesVGPROverlappedSrcCWaitStates = 9; 2165fe6060f1SDimitry Andric const int DMFMA4x4WritesVGPROverlappedSrcCWaitStates = 4; 2166fe6060f1SDimitry Andric const int SMFMA4x4WritesVGPROverlappedSrcABWaitStates = 5; 2167fe6060f1SDimitry Andric const int SMFMA16x16WritesVGPROverlappedSrcABWaitStates = 11; 2168fe6060f1SDimitry Andric const int SMFMA32x32WritesVGPROverlappedSrcABWaitStates = 19; 216981ad6265SDimitry Andric const int GFX940_SMFMA2PassWritesVGPROverlappedSrcABWaitStates = 4; 217081ad6265SDimitry Andric const int GFX940_SMFMA4PassWritesVGPROverlappedSrcABWaitStates = 6; 217181ad6265SDimitry Andric const int GFX940_SMFMA8PassWritesVGPROverlappedSrcABWaitStates = 10; 217281ad6265SDimitry Andric const int GFX940_SMFMA16PassWritesVGPROverlappedSrcABWaitStates = 18; 217381ad6265SDimitry Andric const int GFX940_XDL2PassWritesVGPROverlappedSrcABWaitStates = 5; 217481ad6265SDimitry Andric const int GFX940_XDL4PassWritesVGPROverlappedSrcABWaitStates = 7; 217581ad6265SDimitry Andric const int GFX940_XDL8PassWritesVGPROverlappedSrcABWaitStates = 11; 217681ad6265SDimitry Andric const int GFX940_XDL16PassWritesVGPROverlappedSrcABWaitStates = 19; 2177fe6060f1SDimitry Andric const int DMFMA4x4WritesVGPROverlappedMFMASrcABWaitStates = 6; 2178fe6060f1SDimitry Andric const int DMFMA16x16WritesVGPROverlappedMFMASrcABWaitStates = 11; 2179fe6060f1SDimitry Andric const int DMFMA4x4WritesVGPRFullSrcCWaitStates = 4; 218081ad6265SDimitry Andric const int GFX940_SMFMA4x4WritesVGPRFullSrcCWaitStates = 2; 2181fe6060f1SDimitry Andric const int MaxWaitStates = 19; 2182fe6060f1SDimitry Andric 2183fe6060f1SDimitry Andric if (!Use.isReg()) 2184fe6060f1SDimitry Andric continue; 218504eeddc0SDimitry Andric Register Reg = Use.getReg(); 2186fe6060f1SDimitry Andric bool FullReg; 2187fe6060f1SDimitry Andric const MachineInstr *MI1; 2188fe6060f1SDimitry Andric 218981ad6265SDimitry Andric auto IsOverlappedMFMAFn = [Reg, &FullReg, &MI1, 2190fe6060f1SDimitry Andric this](const MachineInstr &MI) { 219181ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 2192fe6060f1SDimitry Andric return false; 2193fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 2194fe6060f1SDimitry Andric FullReg = (DstReg == Reg); 2195fe6060f1SDimitry Andric MI1 = &MI; 2196fe6060f1SDimitry Andric return TRI.regsOverlap(DstReg, Reg); 2197fe6060f1SDimitry Andric }; 2198fe6060f1SDimitry Andric 2199fe6060f1SDimitry Andric WaitStatesNeededForUse = LegacyVALUNotDotWritesVGPRWaitStates - 2200fe6060f1SDimitry Andric getWaitStatesSinceDef(Reg, IsLegacyVALUNotDotFn, MaxWaitStates); 2201fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2202fe6060f1SDimitry Andric 22034824e7fdSDimitry Andric int NumWaitStates = 22044824e7fdSDimitry Andric getWaitStatesSinceDef(Reg, IsOverlappedMFMAFn, MaxWaitStates); 2205fe6060f1SDimitry Andric if (NumWaitStates == std::numeric_limits<int>::max()) 2206fe6060f1SDimitry Andric continue; 2207fe6060f1SDimitry Andric 2208fe6060f1SDimitry Andric int OpNo = MI->getOperandNo(&Use); 2209fe6060f1SDimitry Andric unsigned Opc1 = MI1->getOpcode(); 2210fe6060f1SDimitry Andric int NeedWaitStates = 0; 2211fe6060f1SDimitry Andric if (OpNo == SrcCIdx) { 221281ad6265SDimitry Andric if (!isDGEMM(Opc) && (!ST.hasGFX940Insts() && isDGEMM(Opc1))) { 2213fe6060f1SDimitry Andric NeedWaitStates = 0; 2214fe6060f1SDimitry Andric } else if (FullReg) { 2215fe6060f1SDimitry Andric if ((Opc == AMDGPU::V_MFMA_F64_4X4X4F64_e64 || 2216fe6060f1SDimitry Andric Opc == AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64) && 2217fe6060f1SDimitry Andric (Opc1 == AMDGPU::V_MFMA_F64_4X4X4F64_e64 || 2218fe6060f1SDimitry Andric Opc1 == AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64)) 2219fe6060f1SDimitry Andric NeedWaitStates = DMFMA4x4WritesVGPRFullSrcCWaitStates; 222081ad6265SDimitry Andric else if (ST.hasGFX940Insts() && 222181ad6265SDimitry Andric TSchedModel.computeInstrLatency(MI1) == 2) 222281ad6265SDimitry Andric NeedWaitStates = GFX940_SMFMA4x4WritesVGPRFullSrcCWaitStates; 2223fe6060f1SDimitry Andric } else { 2224fe6060f1SDimitry Andric switch (Opc1) { 2225fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_e64: 2226fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_vgprcd_e64: 222704eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_e64: 222804eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_vgprcd_e64: 2229fe6060f1SDimitry Andric if (!isXDL(ST, *MI)) 2230fe6060f1SDimitry Andric NeedWaitStates = DMFMA16x16WritesVGPROverlappedSrcCWaitStates; 2231fe6060f1SDimitry Andric break; 2232fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_e64: 2233fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64: 2234fe6060f1SDimitry Andric if (!isXDL(ST, *MI)) 2235fe6060f1SDimitry Andric NeedWaitStates = DMFMA4x4WritesVGPROverlappedSrcCWaitStates; 2236fe6060f1SDimitry Andric break; 2237fe6060f1SDimitry Andric default: 223881ad6265SDimitry Andric if (ST.hasGFX940Insts() && isXDL(ST, *MI) && !isXDL(ST, *MI1)) 223981ad6265SDimitry Andric break; 2240fe6060f1SDimitry Andric switch (TSchedModel.computeInstrLatency(MI1)) { 2241fe6060f1SDimitry Andric case 2: 224281ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 224381ad6265SDimitry Andric ? isXDL(ST, *MI1) 224481ad6265SDimitry Andric ? GFX940_XDL2PassWritesVGPROverlappedSMFMASrcCWaitStates 224581ad6265SDimitry Andric : SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates 224681ad6265SDimitry Andric : isDGEMM(Opc) 2247fe6060f1SDimitry Andric ? SMFMA4x4WritesVGPROverlappedDMFMASrcCWaitStates 2248fe6060f1SDimitry Andric : SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates; 2249fe6060f1SDimitry Andric break; 225081ad6265SDimitry Andric case 4: 225181ad6265SDimitry Andric assert(ST.hasGFX940Insts()); 225281ad6265SDimitry Andric NeedWaitStates = isXDL(ST, *MI1) 225381ad6265SDimitry Andric ? GFX940_XDL4PassWritesVGPROverlappedSMFMASrcCWaitStates 225481ad6265SDimitry Andric : GFX940_SMFMA4PassWritesVGPROverlappedSMFMASrcCWaitStates; 225581ad6265SDimitry Andric break; 2256fe6060f1SDimitry Andric case 8: 225781ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 225881ad6265SDimitry Andric ? isXDL(ST, *MI1) 225981ad6265SDimitry Andric ? GFX940_XDL8PassWritesVGPROverlappedSMFMASrcCWaitStates 226081ad6265SDimitry Andric : GFX940_SMFMA8PassWritesVGPROverlappedSMFMASrcCWaitStates 226181ad6265SDimitry Andric : isDGEMM(Opc) 2262fe6060f1SDimitry Andric ? SMFMA16x16WritesVGPROverlappedDMFMASrcCWaitStates 2263fe6060f1SDimitry Andric : SMFMA16x16WritesVGPROverlappedSMFMASrcCWaitStates; 2264fe6060f1SDimitry Andric break; 2265*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2266fe6060f1SDimitry Andric default: 226781ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 226881ad6265SDimitry Andric ? isXDL(ST, *MI1) 226981ad6265SDimitry Andric ? GFX940_XDL16PassWritesVGPROverlappedSMFMASrcCWaitStates 227081ad6265SDimitry Andric : GFX940_SMFMA16PassWritesVGPROverlappedSMFMASrcCWaitStates 227181ad6265SDimitry Andric : isDGEMM(Opc) 2272fe6060f1SDimitry Andric ? SMFMA32x32WritesVGPROverlappedDMFMASrcCWaitStates 2273fe6060f1SDimitry Andric : SMFMA32x32WritesVGPROverlappedSMFMASrcCWaitStates; 2274fe6060f1SDimitry Andric } 2275fe6060f1SDimitry Andric } 2276fe6060f1SDimitry Andric } 2277fe6060f1SDimitry Andric } else { 2278fe6060f1SDimitry Andric switch (Opc1) { 2279fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_e64: 2280fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_vgprcd_e64: 228104eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_e64: 228204eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_vgprcd_e64: 2283fe6060f1SDimitry Andric NeedWaitStates = DMFMA16x16WritesVGPROverlappedMFMASrcABWaitStates; 2284fe6060f1SDimitry Andric break; 2285fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_e64: 2286fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64: 2287fe6060f1SDimitry Andric NeedWaitStates = DMFMA4x4WritesVGPROverlappedMFMASrcABWaitStates; 2288fe6060f1SDimitry Andric break; 2289fe6060f1SDimitry Andric default: 2290fe6060f1SDimitry Andric switch (TSchedModel.computeInstrLatency(MI1)) { 2291fe6060f1SDimitry Andric case 2: 229281ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 229381ad6265SDimitry Andric ? isXDL(ST, *MI1) 229481ad6265SDimitry Andric ? GFX940_XDL2PassWritesVGPROverlappedSrcABWaitStates 229581ad6265SDimitry Andric : GFX940_SMFMA2PassWritesVGPROverlappedSrcABWaitStates 229681ad6265SDimitry Andric : SMFMA4x4WritesVGPROverlappedSrcABWaitStates; 229781ad6265SDimitry Andric break; 229881ad6265SDimitry Andric case 4: 229981ad6265SDimitry Andric assert(ST.hasGFX940Insts()); 230081ad6265SDimitry Andric NeedWaitStates = isXDL(ST, *MI1) 230181ad6265SDimitry Andric ? GFX940_XDL4PassWritesVGPROverlappedSrcABWaitStates 230281ad6265SDimitry Andric : GFX940_SMFMA4PassWritesVGPROverlappedSrcABWaitStates; 2303fe6060f1SDimitry Andric break; 2304fe6060f1SDimitry Andric case 8: 230581ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 230681ad6265SDimitry Andric ? isXDL(ST, *MI1) 230781ad6265SDimitry Andric ? GFX940_XDL8PassWritesVGPROverlappedSrcABWaitStates 230881ad6265SDimitry Andric : GFX940_SMFMA8PassWritesVGPROverlappedSrcABWaitStates 230981ad6265SDimitry Andric : SMFMA16x16WritesVGPROverlappedSrcABWaitStates; 2310fe6060f1SDimitry Andric break; 2311*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2312fe6060f1SDimitry Andric default: 231381ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 231481ad6265SDimitry Andric ? isXDL(ST, *MI1) 231581ad6265SDimitry Andric ? GFX940_XDL16PassWritesVGPROverlappedSrcABWaitStates 231681ad6265SDimitry Andric : GFX940_SMFMA16PassWritesVGPROverlappedSrcABWaitStates 231781ad6265SDimitry Andric : SMFMA32x32WritesVGPROverlappedSrcABWaitStates; 2318fe6060f1SDimitry Andric } 2319fe6060f1SDimitry Andric } 2320fe6060f1SDimitry Andric } 2321fe6060f1SDimitry Andric if (WaitStatesNeeded >= NeedWaitStates) 2322fe6060f1SDimitry Andric continue; 2323fe6060f1SDimitry Andric 2324fe6060f1SDimitry Andric WaitStatesNeededForUse = NeedWaitStates - NumWaitStates; 2325fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2326fe6060f1SDimitry Andric 2327fe6060f1SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 2328fe6060f1SDimitry Andric break; 2329fe6060f1SDimitry Andric } 2330fe6060f1SDimitry Andric 2331fe6060f1SDimitry Andric return WaitStatesNeeded; 2332fe6060f1SDimitry Andric } 2333fe6060f1SDimitry Andric 23340b57cec5SDimitry Andric int GCNHazardRecognizer::checkMAILdStHazards(MachineInstr *MI) { 2335349cc55cSDimitry Andric // On gfx90a+ relevant hazards are checked in checkMAIVALUHazards() 2336fe6060f1SDimitry Andric if (!ST.hasMAIInsts() || ST.hasGFX90AInsts()) 23370b57cec5SDimitry Andric return 0; 23380b57cec5SDimitry Andric 23390b57cec5SDimitry Andric int WaitStatesNeeded = 0; 23400b57cec5SDimitry Andric 2341fe6060f1SDimitry Andric auto IsAccVgprReadFn = [](const MachineInstr &MI) { 2342fe6060f1SDimitry Andric return MI.getOpcode() == AMDGPU::V_ACCVGPR_READ_B32_e64; 23430b57cec5SDimitry Andric }; 23440b57cec5SDimitry Andric 23450b57cec5SDimitry Andric for (const MachineOperand &Op : MI->explicit_uses()) { 23460b57cec5SDimitry Andric if (!Op.isReg() || !TRI.isVGPR(MF.getRegInfo(), Op.getReg())) 23470b57cec5SDimitry Andric continue; 23480b57cec5SDimitry Andric 23498bcb0991SDimitry Andric Register Reg = Op.getReg(); 23500b57cec5SDimitry Andric 23510b57cec5SDimitry Andric const int AccVgprReadLdStWaitStates = 2; 2352e8d8bef9SDimitry Andric const int VALUWriteAccVgprRdWrLdStDepVALUWaitStates = 1; 23530b57cec5SDimitry Andric const int MaxWaitStates = 2; 23540b57cec5SDimitry Andric 23550b57cec5SDimitry Andric int WaitStatesNeededForUse = AccVgprReadLdStWaitStates - 23560b57cec5SDimitry Andric getWaitStatesSinceDef(Reg, IsAccVgprReadFn, MaxWaitStates); 23570b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 23580b57cec5SDimitry Andric 23590b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 23600b57cec5SDimitry Andric return WaitStatesNeeded; // Early exit. 23610b57cec5SDimitry Andric 2362fe6060f1SDimitry Andric auto IsVALUAccVgprRdWrCheckFn = [Reg, this](const MachineInstr &MI) { 2363fe6060f1SDimitry Andric if (MI.getOpcode() != AMDGPU::V_ACCVGPR_READ_B32_e64 && 2364fe6060f1SDimitry Andric MI.getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 23650b57cec5SDimitry Andric return false; 2366fe6060f1SDimitry Andric auto IsVALUFn = [](const MachineInstr &MI) { 2367fe6060f1SDimitry Andric return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMAI(MI); 23680b57cec5SDimitry Andric }; 23690b57cec5SDimitry Andric return getWaitStatesSinceDef(Reg, IsVALUFn, 2 /*MaxWaitStates*/) < 23700b57cec5SDimitry Andric std::numeric_limits<int>::max(); 23710b57cec5SDimitry Andric }; 23720b57cec5SDimitry Andric 2373e8d8bef9SDimitry Andric WaitStatesNeededForUse = VALUWriteAccVgprRdWrLdStDepVALUWaitStates - 2374e8d8bef9SDimitry Andric getWaitStatesSince(IsVALUAccVgprRdWrCheckFn, MaxWaitStates); 23750b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 23760b57cec5SDimitry Andric } 23770b57cec5SDimitry Andric 23780b57cec5SDimitry Andric return WaitStatesNeeded; 23790b57cec5SDimitry Andric } 2380e8d8bef9SDimitry Andric 2381fe6060f1SDimitry Andric int GCNHazardRecognizer::checkMAIVALUHazards(MachineInstr *MI) { 2382fe6060f1SDimitry Andric if (!ST.hasGFX90AInsts()) 2383fe6060f1SDimitry Andric return 0; 2384fe6060f1SDimitry Andric 2385fe6060f1SDimitry Andric auto IsDGEMMFn = [](const MachineInstr &MI) -> bool { 2386fe6060f1SDimitry Andric return isDGEMM(MI.getOpcode()); 2387fe6060f1SDimitry Andric }; 2388fe6060f1SDimitry Andric 2389fe6060f1SDimitry Andric // This is checked in checkMAIHazards90A() 239081ad6265SDimitry Andric if (SIInstrInfo::isMFMA(*MI)) 2391fe6060f1SDimitry Andric return 0; 2392fe6060f1SDimitry Andric 2393*bdd1243dSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 2394*bdd1243dSDimitry Andric 2395fe6060f1SDimitry Andric int WaitStatesNeeded = 0; 2396fe6060f1SDimitry Andric 2397*bdd1243dSDimitry Andric bool IsMem = SIInstrInfo::isVMEM(*MI) || 2398fe6060f1SDimitry Andric SIInstrInfo::isFLAT(*MI) || 2399*bdd1243dSDimitry Andric SIInstrInfo::isDS(*MI); 2400*bdd1243dSDimitry Andric bool IsMemOrExport = IsMem || SIInstrInfo::isEXP(*MI); 2401fe6060f1SDimitry Andric bool IsVALU = SIInstrInfo::isVALU(*MI); 2402fe6060f1SDimitry Andric 2403fe6060f1SDimitry Andric const MachineInstr *MFMA = nullptr; 2404fe6060f1SDimitry Andric unsigned Reg; 240581ad6265SDimitry Andric auto IsMFMAWriteFn = [&Reg, &MFMA, this](const MachineInstr &MI) { 240681ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI) || 240781ad6265SDimitry Andric !TRI.regsOverlap(MI.getOperand(0).getReg(), Reg)) 2408fe6060f1SDimitry Andric return false; 2409fe6060f1SDimitry Andric MFMA = &MI; 2410fe6060f1SDimitry Andric return true; 2411fe6060f1SDimitry Andric }; 2412fe6060f1SDimitry Andric 2413fe6060f1SDimitry Andric const MachineInstr *DOT = nullptr; 2414fe6060f1SDimitry Andric auto IsDotWriteFn = [&Reg, &DOT, this](const MachineInstr &MI) { 2415fe6060f1SDimitry Andric if (!SIInstrInfo::isDOT(MI) || 2416fe6060f1SDimitry Andric !TRI.regsOverlap(MI.getOperand(0).getReg(), Reg)) 2417fe6060f1SDimitry Andric return false; 2418fe6060f1SDimitry Andric DOT = &MI; 2419fe6060f1SDimitry Andric return true; 2420fe6060f1SDimitry Andric }; 2421fe6060f1SDimitry Andric 2422*bdd1243dSDimitry Andric bool DGEMMAfterVALUWrite = false; 2423*bdd1243dSDimitry Andric auto IsDGEMMHazard = [&DGEMMAfterVALUWrite, this](const MachineInstr &MI) { 2424*bdd1243dSDimitry Andric // Found DGEMM on reverse traversal to def. 2425*bdd1243dSDimitry Andric if (isDGEMM(MI.getOpcode())) 2426*bdd1243dSDimitry Andric DGEMMAfterVALUWrite = true; 2427*bdd1243dSDimitry Andric 2428*bdd1243dSDimitry Andric // Only hazard if register is defined by a VALU and a DGEMM is found after 2429*bdd1243dSDimitry Andric // after the def. 2430*bdd1243dSDimitry Andric if (!TII.isVALU(MI) || !DGEMMAfterVALUWrite) 2431*bdd1243dSDimitry Andric return false; 2432*bdd1243dSDimitry Andric 2433*bdd1243dSDimitry Andric return true; 2434*bdd1243dSDimitry Andric }; 2435*bdd1243dSDimitry Andric 2436fe6060f1SDimitry Andric int SrcCIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), 2437fe6060f1SDimitry Andric AMDGPU::OpName::src2); 2438fe6060f1SDimitry Andric 2439fe6060f1SDimitry Andric if (IsMemOrExport || IsVALU) { 2440fe6060f1SDimitry Andric const int SMFMA4x4WriteVgprVALUMemExpReadWaitStates = 5; 2441fe6060f1SDimitry Andric const int SMFMA16x16WriteVgprVALUMemExpReadWaitStates = 11; 2442fe6060f1SDimitry Andric const int SMFMA32x32WriteVgprVALUMemExpReadWaitStates = 19; 244381ad6265SDimitry Andric const int GFX940_SMFMA2PassWriteVgprVALUMemExpReadWaitStates = 4; 244481ad6265SDimitry Andric const int GFX940_SMFMA4PassWriteVgprVALUMemExpReadWaitStates = 6; 244581ad6265SDimitry Andric const int GFX940_SMFMA8PassWriteVgprVALUMemExpReadWaitStates = 10; 244681ad6265SDimitry Andric const int GFX940_SMFMA16PassWriteVgprVALUMemExpReadWaitStates = 18; 244781ad6265SDimitry Andric const int GFX940_XDL2PassWriteVgprVALUMemExpReadWaitStates = 5; 244881ad6265SDimitry Andric const int GFX940_XDL4PassWriteVgprVALUMemExpReadWaitStates = 7; 244981ad6265SDimitry Andric const int GFX940_XDL8PassWriteVgprVALUMemExpReadWaitStates = 11; 245081ad6265SDimitry Andric const int GFX940_XDL16PassWriteVgprVALUMemExpReadWaitStates = 19; 2451fe6060f1SDimitry Andric const int DMFMA4x4WriteVgprMemExpReadWaitStates = 9; 2452fe6060f1SDimitry Andric const int DMFMA16x16WriteVgprMemExpReadWaitStates = 18; 2453fe6060f1SDimitry Andric const int DMFMA4x4WriteVgprVALUReadWaitStates = 6; 2454fe6060f1SDimitry Andric const int DMFMA16x16WriteVgprVALUReadWaitStates = 11; 2455fe6060f1SDimitry Andric const int DotWriteSameDotReadSrcAB = 3; 2456fe6060f1SDimitry Andric const int DotWriteDifferentVALURead = 3; 2457*bdd1243dSDimitry Andric const int DMFMABetweenVALUWriteVMEMRead = 2; 2458fe6060f1SDimitry Andric const int MaxWaitStates = 19; 2459fe6060f1SDimitry Andric 2460fe6060f1SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 2461fe6060f1SDimitry Andric if (!Use.isReg()) 2462fe6060f1SDimitry Andric continue; 2463fe6060f1SDimitry Andric Reg = Use.getReg(); 2464fe6060f1SDimitry Andric 2465fe6060f1SDimitry Andric DOT = nullptr; 2466fe6060f1SDimitry Andric int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsDotWriteFn, 2467fe6060f1SDimitry Andric MaxWaitStates); 2468fe6060f1SDimitry Andric if (DOT) { 2469fe6060f1SDimitry Andric int NeedWaitStates = 0; 2470fe6060f1SDimitry Andric if (DOT->getOpcode() == MI->getOpcode()) { 2471fe6060f1SDimitry Andric if (&Use - &MI->getOperand(0) != SrcCIdx) 2472fe6060f1SDimitry Andric NeedWaitStates = DotWriteSameDotReadSrcAB; 2473fe6060f1SDimitry Andric } else { 2474fe6060f1SDimitry Andric NeedWaitStates = DotWriteDifferentVALURead; 2475fe6060f1SDimitry Andric } 2476fe6060f1SDimitry Andric 2477fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2478fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2479fe6060f1SDimitry Andric } 2480fe6060f1SDimitry Andric 2481*bdd1243dSDimitry Andric // Workaround for HW data hazard bug observed only in GFX90A. When there 2482*bdd1243dSDimitry Andric // is a DGEMM instruction in-between a VALU and a VMEM instruction it 2483*bdd1243dSDimitry Andric // causes the SQ to incorrectly not insert two wait states between the two 2484*bdd1243dSDimitry Andric // instructions needed to avoid data hazard. 2485*bdd1243dSDimitry Andric if (IsMem && ST.hasGFX90AInsts() && !ST.hasGFX940Insts()) { 2486*bdd1243dSDimitry Andric DGEMMAfterVALUWrite = false; 2487*bdd1243dSDimitry Andric if (TRI.isVectorRegister(MRI, Reg)) { 2488*bdd1243dSDimitry Andric int WaitStatesNeededForUse = 2489*bdd1243dSDimitry Andric DMFMABetweenVALUWriteVMEMRead - 2490*bdd1243dSDimitry Andric getWaitStatesSinceDef(Reg, IsDGEMMHazard, 2491*bdd1243dSDimitry Andric DMFMABetweenVALUWriteVMEMRead); 2492*bdd1243dSDimitry Andric 2493*bdd1243dSDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2494*bdd1243dSDimitry Andric } 2495*bdd1243dSDimitry Andric } 2496*bdd1243dSDimitry Andric 2497fe6060f1SDimitry Andric MFMA = nullptr; 24984824e7fdSDimitry Andric WaitStatesSinceDef = 24994824e7fdSDimitry Andric getWaitStatesSinceDef(Reg, IsMFMAWriteFn, MaxWaitStates); 2500fe6060f1SDimitry Andric if (!MFMA) 2501fe6060f1SDimitry Andric continue; 2502fe6060f1SDimitry Andric 2503fe6060f1SDimitry Andric unsigned HazardDefLatency = TSchedModel.computeInstrLatency(MFMA); 2504fe6060f1SDimitry Andric int NeedWaitStates = MaxWaitStates; 2505fe6060f1SDimitry Andric switch (HazardDefLatency) { 2506fe6060f1SDimitry Andric case 2: 250781ad6265SDimitry Andric NeedWaitStates = 250881ad6265SDimitry Andric ST.hasGFX940Insts() 250981ad6265SDimitry Andric ? isXDL(ST, *MFMA) 251081ad6265SDimitry Andric ? GFX940_XDL2PassWriteVgprVALUMemExpReadWaitStates 251181ad6265SDimitry Andric : GFX940_SMFMA2PassWriteVgprVALUMemExpReadWaitStates 251281ad6265SDimitry Andric : SMFMA4x4WriteVgprVALUMemExpReadWaitStates; 2513fe6060f1SDimitry Andric break; 2514fe6060f1SDimitry Andric case 4: 251581ad6265SDimitry Andric assert(isDGEMM(MFMA->getOpcode()) || ST.hasGFX940Insts()); 2516fe6060f1SDimitry Andric NeedWaitStates = 251781ad6265SDimitry Andric isDGEMM(MFMA->getOpcode()) 251881ad6265SDimitry Andric ? IsMemOrExport ? DMFMA4x4WriteVgprMemExpReadWaitStates 251981ad6265SDimitry Andric : DMFMA4x4WriteVgprVALUReadWaitStates 252081ad6265SDimitry Andric : isXDL(ST, *MFMA) 252181ad6265SDimitry Andric ? GFX940_XDL4PassWriteVgprVALUMemExpReadWaitStates 252281ad6265SDimitry Andric : GFX940_SMFMA4PassWriteVgprVALUMemExpReadWaitStates; 2523fe6060f1SDimitry Andric break; 2524fe6060f1SDimitry Andric case 8: 252581ad6265SDimitry Andric NeedWaitStates = 252681ad6265SDimitry Andric ST.hasGFX940Insts() 252781ad6265SDimitry Andric ? isXDL(ST, *MFMA) 252881ad6265SDimitry Andric ? GFX940_XDL8PassWriteVgprVALUMemExpReadWaitStates 252981ad6265SDimitry Andric : GFX940_SMFMA8PassWriteVgprVALUMemExpReadWaitStates 253081ad6265SDimitry Andric : SMFMA16x16WriteVgprVALUMemExpReadWaitStates; 2531fe6060f1SDimitry Andric break; 2532*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2533fe6060f1SDimitry Andric default: 2534fe6060f1SDimitry Andric NeedWaitStates = 2535fe6060f1SDimitry Andric isDGEMM(MFMA->getOpcode()) 2536fe6060f1SDimitry Andric ? IsMemOrExport ? DMFMA16x16WriteVgprMemExpReadWaitStates 2537fe6060f1SDimitry Andric : DMFMA16x16WriteVgprVALUReadWaitStates 253881ad6265SDimitry Andric : ST.hasGFX940Insts() 253981ad6265SDimitry Andric ? isXDL(ST, *MFMA) 254081ad6265SDimitry Andric ? GFX940_XDL16PassWriteVgprVALUMemExpReadWaitStates 254181ad6265SDimitry Andric : GFX940_SMFMA16PassWriteVgprVALUMemExpReadWaitStates 2542fe6060f1SDimitry Andric : SMFMA32x32WriteVgprVALUMemExpReadWaitStates; 2543fe6060f1SDimitry Andric break; 2544fe6060f1SDimitry Andric } 2545fe6060f1SDimitry Andric 2546fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2547fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2548fe6060f1SDimitry Andric 2549fe6060f1SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 2550fe6060f1SDimitry Andric break; 2551fe6060f1SDimitry Andric } 2552fe6060f1SDimitry Andric } 2553fe6060f1SDimitry Andric 2554fe6060f1SDimitry Andric unsigned Opc = MI->getOpcode(); 2555fe6060f1SDimitry Andric const int DMFMAToFMA64WaitStates = 2; 2556fe6060f1SDimitry Andric if ((Opc == AMDGPU::V_FMA_F64_e64 || 2557fe6060f1SDimitry Andric Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64 || 2558fe6060f1SDimitry Andric Opc == AMDGPU::V_FMAC_F64_dpp) && 2559fe6060f1SDimitry Andric WaitStatesNeeded < DMFMAToFMA64WaitStates) { 2560fe6060f1SDimitry Andric int WaitStatesNeededForUse = DMFMAToFMA64WaitStates - 2561fe6060f1SDimitry Andric getWaitStatesSince(IsDGEMMFn, DMFMAToFMA64WaitStates); 2562fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2563fe6060f1SDimitry Andric } 2564fe6060f1SDimitry Andric 2565fe6060f1SDimitry Andric if (!IsVALU && !IsMemOrExport) 2566fe6060f1SDimitry Andric return WaitStatesNeeded; 2567fe6060f1SDimitry Andric 2568fe6060f1SDimitry Andric for (const MachineOperand &Def : MI->defs()) { 2569fe6060f1SDimitry Andric const int SMFMA4x4WriteVgprVALUWawWaitStates = 5; 2570fe6060f1SDimitry Andric const int SMFMA16x16WriteVgprVALUWawWaitStates = 11; 2571fe6060f1SDimitry Andric const int SMFMA32x32WriteVgprVALUWawWaitStates = 19; 257281ad6265SDimitry Andric const int GFX940_SMFMA2PassWriteVgprVALUWawWaitStates = 4; 257381ad6265SDimitry Andric const int GFX940_SMFMA4PassWriteVgprVALUWawWaitStates = 6; 257481ad6265SDimitry Andric const int GFX940_SMFMA8PassWriteVgprVALUWawWaitStates = 10; 257581ad6265SDimitry Andric const int GFX940_SMFMA16PassWriteVgprVALUWawWaitStates = 18; 257681ad6265SDimitry Andric const int GFX940_XDL2PassWriteVgprVALUWawWaitStates = 5; 257781ad6265SDimitry Andric const int GFX940_XDL4PassWriteVgprVALUWawWaitStates = 7; 257881ad6265SDimitry Andric const int GFX940_XDL8PassWriteVgprVALUWawWaitStates = 11; 257981ad6265SDimitry Andric const int GFX940_XDL16PassWriteVgprVALUWawWaitStates = 19; 2580fe6060f1SDimitry Andric const int SMFMA4x4ReadVgprVALUWarWaitStates = 1; 258181ad6265SDimitry Andric const int GFX940_XDL4PassReadVgprVALUWarWaitStates = 3; 2582fe6060f1SDimitry Andric const int SMFMA16x16ReadVgprVALUWarWaitStates = 7; 2583fe6060f1SDimitry Andric const int SMFMA32x32ReadVgprVALUWarWaitStates = 15; 2584fe6060f1SDimitry Andric const int DMFMA4x4WriteVgprVALUWriteWaitStates = 6; 2585fe6060f1SDimitry Andric const int DMFMA16x16WriteVgprVALUWriteWaitStates = 11; 2586fe6060f1SDimitry Andric const int DotWriteDifferentVALUWrite = 3; 2587fe6060f1SDimitry Andric const int MaxWaitStates = 19; 2588fe6060f1SDimitry Andric const int MaxWarWaitStates = 15; 2589fe6060f1SDimitry Andric 2590fe6060f1SDimitry Andric Reg = Def.getReg(); 2591fe6060f1SDimitry Andric 2592fe6060f1SDimitry Andric DOT = nullptr; 2593fe6060f1SDimitry Andric int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsDotWriteFn, 2594fe6060f1SDimitry Andric MaxWaitStates); 2595fe6060f1SDimitry Andric if (DOT && DOT->getOpcode() != MI->getOpcode()) 2596fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, DotWriteDifferentVALUWrite - 2597fe6060f1SDimitry Andric WaitStatesSinceDef); 2598fe6060f1SDimitry Andric 2599fe6060f1SDimitry Andric MFMA = nullptr; 26004824e7fdSDimitry Andric WaitStatesSinceDef = 26014824e7fdSDimitry Andric getWaitStatesSinceDef(Reg, IsMFMAWriteFn, MaxWaitStates); 2602fe6060f1SDimitry Andric if (MFMA) { 2603fe6060f1SDimitry Andric int NeedWaitStates = MaxWaitStates; 2604fe6060f1SDimitry Andric switch (TSchedModel.computeInstrLatency(MFMA)) { 2605fe6060f1SDimitry Andric case 2: 260681ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 260781ad6265SDimitry Andric ? isXDL(ST, *MFMA) 260881ad6265SDimitry Andric ? GFX940_XDL2PassWriteVgprVALUWawWaitStates 260981ad6265SDimitry Andric : GFX940_SMFMA2PassWriteVgprVALUWawWaitStates 261081ad6265SDimitry Andric : SMFMA4x4WriteVgprVALUWawWaitStates; 2611fe6060f1SDimitry Andric break; 2612fe6060f1SDimitry Andric case 4: 261381ad6265SDimitry Andric assert(isDGEMM(MFMA->getOpcode()) || ST.hasGFX940Insts()); 261481ad6265SDimitry Andric NeedWaitStates = isDGEMM(MFMA->getOpcode()) 261581ad6265SDimitry Andric ? DMFMA4x4WriteVgprVALUWriteWaitStates 261681ad6265SDimitry Andric : isXDL(ST, *MFMA) 261781ad6265SDimitry Andric ? GFX940_XDL4PassWriteVgprVALUWawWaitStates 261881ad6265SDimitry Andric : GFX940_SMFMA4PassWriteVgprVALUWawWaitStates; 2619fe6060f1SDimitry Andric break; 2620fe6060f1SDimitry Andric case 8: 262181ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 262281ad6265SDimitry Andric ? isXDL(ST, *MFMA) 262381ad6265SDimitry Andric ? GFX940_XDL8PassWriteVgprVALUWawWaitStates 262481ad6265SDimitry Andric : GFX940_SMFMA8PassWriteVgprVALUWawWaitStates 262581ad6265SDimitry Andric : SMFMA16x16WriteVgprVALUWawWaitStates; 2626fe6060f1SDimitry Andric break; 2627*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2628fe6060f1SDimitry Andric default: 2629fe6060f1SDimitry Andric NeedWaitStates = isDGEMM(MFMA->getOpcode()) 2630fe6060f1SDimitry Andric ? DMFMA16x16WriteVgprVALUWriteWaitStates 263181ad6265SDimitry Andric : ST.hasGFX940Insts() 263281ad6265SDimitry Andric ? isXDL(ST, *MFMA) 263381ad6265SDimitry Andric ? GFX940_XDL16PassWriteVgprVALUWawWaitStates 263481ad6265SDimitry Andric : GFX940_SMFMA16PassWriteVgprVALUWawWaitStates 2635fe6060f1SDimitry Andric : SMFMA32x32WriteVgprVALUWawWaitStates; 2636fe6060f1SDimitry Andric break; 2637fe6060f1SDimitry Andric } 2638fe6060f1SDimitry Andric 2639fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2640fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2641fe6060f1SDimitry Andric 2642fe6060f1SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 2643fe6060f1SDimitry Andric break; 2644fe6060f1SDimitry Andric } 2645fe6060f1SDimitry Andric 264681ad6265SDimitry Andric auto IsSMFMAReadAsCFn = [&Reg, &MFMA, this](const MachineInstr &MI) { 264781ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI) || isDGEMM(MI.getOpcode()) || 2648fe6060f1SDimitry Andric !MI.readsRegister(Reg, &TRI)) 2649fe6060f1SDimitry Andric return false; 2650fe6060f1SDimitry Andric 265181ad6265SDimitry Andric if (ST.hasGFX940Insts() && !isXDL(ST, MI)) 265281ad6265SDimitry Andric return false; 265381ad6265SDimitry Andric 2654fe6060f1SDimitry Andric const MachineOperand *SrcC = 2655fe6060f1SDimitry Andric TII.getNamedOperand(MI, AMDGPU::OpName::src2); 2656fe6060f1SDimitry Andric assert(SrcC); 2657fe6060f1SDimitry Andric if (!SrcC->isReg() || !TRI.regsOverlap(SrcC->getReg(), Reg)) 2658fe6060f1SDimitry Andric return false; 2659fe6060f1SDimitry Andric 2660fe6060f1SDimitry Andric MFMA = &MI; 2661fe6060f1SDimitry Andric return true; 2662fe6060f1SDimitry Andric }; 2663fe6060f1SDimitry Andric 2664fe6060f1SDimitry Andric MFMA = nullptr; 2665fe6060f1SDimitry Andric int WaitStatesSinceUse = getWaitStatesSince(IsSMFMAReadAsCFn, 2666fe6060f1SDimitry Andric MaxWarWaitStates); 2667fe6060f1SDimitry Andric if (!MFMA) 2668fe6060f1SDimitry Andric continue; 2669fe6060f1SDimitry Andric 2670fe6060f1SDimitry Andric unsigned HazardDefLatency = TSchedModel.computeInstrLatency(MFMA); 2671fe6060f1SDimitry Andric int NeedWaitStates = MaxWaitStates; 2672fe6060f1SDimitry Andric switch (HazardDefLatency) { 2673fe6060f1SDimitry Andric case 2: NeedWaitStates = SMFMA4x4ReadVgprVALUWarWaitStates; 2674fe6060f1SDimitry Andric break; 267581ad6265SDimitry Andric case 4: assert(ST.hasGFX940Insts()); 267681ad6265SDimitry Andric NeedWaitStates = GFX940_XDL4PassReadVgprVALUWarWaitStates; 267781ad6265SDimitry Andric break; 2678fe6060f1SDimitry Andric case 8: NeedWaitStates = SMFMA16x16ReadVgprVALUWarWaitStates; 2679fe6060f1SDimitry Andric break; 2680*bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2681fe6060f1SDimitry Andric default: NeedWaitStates = SMFMA32x32ReadVgprVALUWarWaitStates; 2682fe6060f1SDimitry Andric break; 2683fe6060f1SDimitry Andric } 2684fe6060f1SDimitry Andric 2685fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceUse; 2686fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2687fe6060f1SDimitry Andric } 2688fe6060f1SDimitry Andric 2689fe6060f1SDimitry Andric return WaitStatesNeeded; 2690fe6060f1SDimitry Andric } 2691fe6060f1SDimitry Andric 2692e8d8bef9SDimitry Andric bool GCNHazardRecognizer::ShouldPreferAnother(SUnit *SU) { 2693e8d8bef9SDimitry Andric if (!SU->isInstr()) 2694e8d8bef9SDimitry Andric return false; 2695e8d8bef9SDimitry Andric 2696fe6060f1SDimitry Andric const MachineInstr *MAI = nullptr; 269781ad6265SDimitry Andric 2698fe6060f1SDimitry Andric auto IsMFMAFn = [&MAI](const MachineInstr &MI) { 2699e8d8bef9SDimitry Andric MAI = nullptr; 270081ad6265SDimitry Andric if (SIInstrInfo::isMFMA(MI)) 2701fe6060f1SDimitry Andric MAI = &MI; 2702e8d8bef9SDimitry Andric return MAI != nullptr; 2703e8d8bef9SDimitry Andric }; 2704e8d8bef9SDimitry Andric 2705e8d8bef9SDimitry Andric MachineInstr *MI = SU->getInstr(); 2706fe6060f1SDimitry Andric if (IsMFMAFn(*MI)) { 2707e8d8bef9SDimitry Andric int W = getWaitStatesSince(IsMFMAFn, 16); 2708e8d8bef9SDimitry Andric if (MAI) 2709e8d8bef9SDimitry Andric return W < (int)TSchedModel.computeInstrLatency(MAI); 2710e8d8bef9SDimitry Andric } 2711e8d8bef9SDimitry Andric 2712e8d8bef9SDimitry Andric return false; 2713e8d8bef9SDimitry Andric } 2714*bdd1243dSDimitry Andric 2715*bdd1243dSDimitry Andric bool GCNHazardRecognizer::fixVALUMaskWriteHazard(MachineInstr *MI) { 2716*bdd1243dSDimitry Andric if (!ST.isWave64()) 2717*bdd1243dSDimitry Andric return false; 2718*bdd1243dSDimitry Andric if (!ST.hasVALUMaskWriteHazard()) 2719*bdd1243dSDimitry Andric return false; 2720*bdd1243dSDimitry Andric if (!SIInstrInfo::isSALU(*MI)) 2721*bdd1243dSDimitry Andric return false; 2722*bdd1243dSDimitry Andric 2723*bdd1243dSDimitry Andric // The hazard sequence is three instructions: 2724*bdd1243dSDimitry Andric // 1. VALU reads SGPR as mask 2725*bdd1243dSDimitry Andric // 2. SALU writes SGPR 2726*bdd1243dSDimitry Andric // 3. SALU reads SGPR 2727*bdd1243dSDimitry Andric // The hazard can expire if the distance between 2 and 3 is sufficient. 2728*bdd1243dSDimitry Andric // In practice this happens <10% of the time, hence this always assumes 2729*bdd1243dSDimitry Andric // the hazard exists if 1 and 2 are present to avoid searching. 2730*bdd1243dSDimitry Andric 2731*bdd1243dSDimitry Andric const MachineOperand *SDSTOp = TII.getNamedOperand(*MI, AMDGPU::OpName::sdst); 2732*bdd1243dSDimitry Andric if (!SDSTOp || !SDSTOp->isReg()) 2733*bdd1243dSDimitry Andric return false; 2734*bdd1243dSDimitry Andric 2735*bdd1243dSDimitry Andric const Register HazardReg = SDSTOp->getReg(); 2736*bdd1243dSDimitry Andric if (HazardReg == AMDGPU::EXEC || 2737*bdd1243dSDimitry Andric HazardReg == AMDGPU::EXEC_LO || 2738*bdd1243dSDimitry Andric HazardReg == AMDGPU::EXEC_HI || 2739*bdd1243dSDimitry Andric HazardReg == AMDGPU::M0) 2740*bdd1243dSDimitry Andric return false; 2741*bdd1243dSDimitry Andric 2742*bdd1243dSDimitry Andric auto IsHazardFn = [HazardReg, this](const MachineInstr &I) { 2743*bdd1243dSDimitry Andric switch (I.getOpcode()) { 2744*bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_e32: 2745*bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_dpp: 2746*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_e32: 2747*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_dpp: 2748*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_e32: 2749*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_dpp: 2750*bdd1243dSDimitry Andric case AMDGPU::V_DIV_FMAS_F32_e64: 2751*bdd1243dSDimitry Andric case AMDGPU::V_DIV_FMAS_F64_e64: 2752*bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_e32: 2753*bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_dpp: 2754*bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_e32: 2755*bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_dpp: 2756*bdd1243dSDimitry Andric // These implicitly read VCC as mask source. 2757*bdd1243dSDimitry Andric return HazardReg == AMDGPU::VCC || 2758*bdd1243dSDimitry Andric HazardReg == AMDGPU::VCC_LO || 2759*bdd1243dSDimitry Andric HazardReg == AMDGPU::VCC_HI; 2760*bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_e64: 2761*bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_e64_dpp: 2762*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_e64: 2763*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_e64_dpp: 2764*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_e64: 2765*bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_e64_dpp: 2766*bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_e64: 2767*bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_e64_dpp: 2768*bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_e64: 2769*bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_e64_dpp: { 2770*bdd1243dSDimitry Andric // Only check mask register overlaps. 2771*bdd1243dSDimitry Andric const MachineOperand *SSRCOp = TII.getNamedOperand(I, AMDGPU::OpName::src2); 2772*bdd1243dSDimitry Andric assert(SSRCOp); 2773*bdd1243dSDimitry Andric return TRI.regsOverlap(SSRCOp->getReg(), HazardReg); 2774*bdd1243dSDimitry Andric } 2775*bdd1243dSDimitry Andric default: 2776*bdd1243dSDimitry Andric return false; 2777*bdd1243dSDimitry Andric } 2778*bdd1243dSDimitry Andric }; 2779*bdd1243dSDimitry Andric 2780*bdd1243dSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 2781*bdd1243dSDimitry Andric auto IsExpiredFn = [&MRI, this](const MachineInstr &I, int) { 2782*bdd1243dSDimitry Andric // s_waitcnt_depctr sa_sdst(0) mitigates hazard. 2783*bdd1243dSDimitry Andric if (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 2784*bdd1243dSDimitry Andric !(I.getOperand(0).getImm() & 0x1)) 2785*bdd1243dSDimitry Andric return true; 2786*bdd1243dSDimitry Andric 2787*bdd1243dSDimitry Andric // VALU access to any SGPR or literal constant other than HazardReg 2788*bdd1243dSDimitry Andric // mitigates hazard. No need to check HazardReg here as this will 2789*bdd1243dSDimitry Andric // only be called when !IsHazardFn. 2790*bdd1243dSDimitry Andric if (!SIInstrInfo::isVALU(I)) 2791*bdd1243dSDimitry Andric return false; 2792*bdd1243dSDimitry Andric for (int OpNo = 0, End = I.getNumOperands(); OpNo < End; ++OpNo) { 2793*bdd1243dSDimitry Andric const MachineOperand &Op = I.getOperand(OpNo); 2794*bdd1243dSDimitry Andric if (Op.isReg()) { 2795*bdd1243dSDimitry Andric Register OpReg = Op.getReg(); 2796*bdd1243dSDimitry Andric // Only consider uses 2797*bdd1243dSDimitry Andric if (!Op.isUse()) 2798*bdd1243dSDimitry Andric continue; 2799*bdd1243dSDimitry Andric // Ignore EXEC 2800*bdd1243dSDimitry Andric if (OpReg == AMDGPU::EXEC || 2801*bdd1243dSDimitry Andric OpReg == AMDGPU::EXEC_LO || 2802*bdd1243dSDimitry Andric OpReg == AMDGPU::EXEC_HI) 2803*bdd1243dSDimitry Andric continue; 2804*bdd1243dSDimitry Andric // Ignore all implicit uses except VCC 2805*bdd1243dSDimitry Andric if (Op.isImplicit()) { 2806*bdd1243dSDimitry Andric if (OpReg == AMDGPU::VCC || 2807*bdd1243dSDimitry Andric OpReg == AMDGPU::VCC_LO || 2808*bdd1243dSDimitry Andric OpReg == AMDGPU::VCC_HI) 2809*bdd1243dSDimitry Andric return true; 2810*bdd1243dSDimitry Andric continue; 2811*bdd1243dSDimitry Andric } 2812*bdd1243dSDimitry Andric if (TRI.isSGPRReg(MRI, OpReg)) 2813*bdd1243dSDimitry Andric return true; 2814*bdd1243dSDimitry Andric } else { 2815*bdd1243dSDimitry Andric const MCInstrDesc &InstDesc = I.getDesc(); 2816*bdd1243dSDimitry Andric const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo]; 2817*bdd1243dSDimitry Andric if (!TII.isInlineConstant(Op, OpInfo)) 2818*bdd1243dSDimitry Andric return true; 2819*bdd1243dSDimitry Andric } 2820*bdd1243dSDimitry Andric } 2821*bdd1243dSDimitry Andric return false; 2822*bdd1243dSDimitry Andric }; 2823*bdd1243dSDimitry Andric 2824*bdd1243dSDimitry Andric // Check for hazard 2825*bdd1243dSDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 2826*bdd1243dSDimitry Andric std::numeric_limits<int>::max()) 2827*bdd1243dSDimitry Andric return false; 2828*bdd1243dSDimitry Andric 2829*bdd1243dSDimitry Andric auto NextMI = std::next(MI->getIterator()); 2830*bdd1243dSDimitry Andric 2831*bdd1243dSDimitry Andric // Add s_waitcnt_depctr sa_sdst(0) after SALU write. 2832*bdd1243dSDimitry Andric BuildMI(*MI->getParent(), NextMI, MI->getDebugLoc(), 2833*bdd1243dSDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 2834*bdd1243dSDimitry Andric .addImm(0xfffe); 2835*bdd1243dSDimitry Andric 2836*bdd1243dSDimitry Andric // SALU write may be s_getpc in a bundle. 2837*bdd1243dSDimitry Andric if (MI->getOpcode() == AMDGPU::S_GETPC_B64) { 2838*bdd1243dSDimitry Andric // Update offsets of any references in the bundle. 2839*bdd1243dSDimitry Andric while (NextMI != MI->getParent()->end() && 2840*bdd1243dSDimitry Andric NextMI->isBundledWithPred()) { 2841*bdd1243dSDimitry Andric for (auto &Operand : NextMI->operands()) { 2842*bdd1243dSDimitry Andric if (Operand.isGlobal()) 2843*bdd1243dSDimitry Andric Operand.setOffset(Operand.getOffset() + 4); 2844*bdd1243dSDimitry Andric } 2845*bdd1243dSDimitry Andric NextMI++; 2846*bdd1243dSDimitry Andric } 2847*bdd1243dSDimitry Andric } 2848*bdd1243dSDimitry Andric 2849*bdd1243dSDimitry Andric return true; 2850*bdd1243dSDimitry Andric } 2851