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" 19*06c3fb27SDimitry Andric #include "llvm/TargetParser/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() && 235bdd1243dSDimitry Andric (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()) || 236bdd1243dSDimitry Andric MI->getOpcode() == AMDGPU::DS_WRITE_ADDTID_B32 || 237bdd1243dSDimitry 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 303bdd1243dSDimitry Andric void GCNHazardRecognizer::runOnInstruction(MachineInstr *MI) { 304bdd1243dSDimitry Andric assert(IsHazardRecognizerMode); 305bdd1243dSDimitry Andric 306bdd1243dSDimitry Andric unsigned NumPreNoops = PreEmitNoops(MI); 307bdd1243dSDimitry Andric EmitNoops(NumPreNoops); 308bdd1243dSDimitry Andric if (MI->isInsideBundle()) 309bdd1243dSDimitry Andric insertNoopsInBundle(MI, TII, NumPreNoops); 310bdd1243dSDimitry Andric else 311bdd1243dSDimitry Andric TII.insertNoops(*MI->getParent(), MachineBasicBlock::iterator(MI), 312bdd1243dSDimitry Andric NumPreNoops); 313bdd1243dSDimitry Andric EmitInstruction(MI); 314bdd1243dSDimitry Andric AdvanceCycle(); 315bdd1243dSDimitry Andric } 316bdd1243dSDimitry 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() && 376bdd1243dSDimitry Andric (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()) || 377bdd1243dSDimitry Andric MI->getOpcode() == AMDGPU::DS_WRITE_ADDTID_B32 || 378bdd1243dSDimitry 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) { 591*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(Reg)) 592*06c3fb27SDimitry Andric BV.set(Unit); 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric static void addRegsToSet(const SIRegisterInfo &TRI, 5960b57cec5SDimitry Andric iterator_range<MachineInstr::const_mop_iterator> Ops, 597*06c3fb27SDimitry Andric BitVector &DefSet, BitVector &UseSet) { 5980b57cec5SDimitry Andric for (const MachineOperand &Op : Ops) { 5990b57cec5SDimitry Andric if (Op.isReg()) 600*06c3fb27SDimitry Andric addRegUnits(TRI, Op.isDef() ? DefSet : UseSet, Op.getReg().asMCReg()); 6010b57cec5SDimitry Andric } 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) { 605*06c3fb27SDimitry Andric addRegsToSet(TRI, MI.operands(), ClauseDefs, ClauseUses); 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric 6085ffd83dbSDimitry Andric static bool breaksSMEMSoftClause(MachineInstr *MI) { 6095ffd83dbSDimitry Andric return !SIInstrInfo::isSMRD(*MI); 6105ffd83dbSDimitry Andric } 6115ffd83dbSDimitry Andric 6125ffd83dbSDimitry Andric static bool breaksVMEMSoftClause(MachineInstr *MI) { 6135ffd83dbSDimitry Andric return !SIInstrInfo::isVMEM(*MI) && !SIInstrInfo::isFLAT(*MI); 6145ffd83dbSDimitry Andric } 6155ffd83dbSDimitry Andric 6160b57cec5SDimitry Andric int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) { 6170b57cec5SDimitry Andric // SMEM soft clause are only present on VI+, and only matter if xnack is 6180b57cec5SDimitry Andric // enabled. 6190b57cec5SDimitry Andric if (!ST.isXNACKEnabled()) 6200b57cec5SDimitry Andric return 0; 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric bool IsSMRD = TII.isSMRD(*MEM); 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric resetClause(); 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric // A soft-clause is any group of consecutive SMEM instructions. The 6270b57cec5SDimitry Andric // instructions in this group may return out of order and/or may be 6280b57cec5SDimitry Andric // replayed (i.e. the same instruction issued more than once). 6290b57cec5SDimitry Andric // 6300b57cec5SDimitry Andric // In order to handle these situations correctly we need to make sure that 6310b57cec5SDimitry Andric // when a clause has more than one instruction, no instruction in the clause 6320b57cec5SDimitry Andric // writes to a register that is read by another instruction in the clause 63381ad6265SDimitry Andric // (including itself). If we encounter this situation, we need to break the 6340b57cec5SDimitry Andric // clause by inserting a non SMEM instruction. 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric for (MachineInstr *MI : EmittedInstrs) { 6370b57cec5SDimitry Andric // When we hit a non-SMEM instruction then we have passed the start of the 6380b57cec5SDimitry Andric // clause and we can stop. 6390b57cec5SDimitry Andric if (!MI) 6400b57cec5SDimitry Andric break; 6410b57cec5SDimitry Andric 6425ffd83dbSDimitry Andric if (IsSMRD ? breaksSMEMSoftClause(MI) : breaksVMEMSoftClause(MI)) 6430b57cec5SDimitry Andric break; 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric addClauseInst(*MI); 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric if (ClauseDefs.none()) 6490b57cec5SDimitry Andric return 0; 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric // We need to make sure not to put loads and stores in the same clause if they 6520b57cec5SDimitry Andric // use the same address. For now, just start a new clause whenever we see a 6530b57cec5SDimitry Andric // store. 6540b57cec5SDimitry Andric if (MEM->mayStore()) 6550b57cec5SDimitry Andric return 1; 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric addClauseInst(*MEM); 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric // If the set of defs and uses intersect then we cannot add this instruction 6600b57cec5SDimitry Andric // to the clause, so we have a hazard. 6610b57cec5SDimitry Andric return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { 6650b57cec5SDimitry Andric int WaitStatesNeeded = 0; 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric WaitStatesNeeded = checkSoftClauseHazards(SMRD); 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric // This SMRD hazard only affects SI. 6700b57cec5SDimitry Andric if (!ST.hasSMRDReadVALUDefHazard()) 6710b57cec5SDimitry Andric return WaitStatesNeeded; 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric // A read of an SGPR by SMRD instruction requires 4 wait states when the 6740b57cec5SDimitry Andric // SGPR was written by a VALU instruction. 6750b57cec5SDimitry Andric int SmrdSgprWaitStates = 4; 676fe6060f1SDimitry Andric auto IsHazardDefFn = [this](const MachineInstr &MI) { 677fe6060f1SDimitry Andric return TII.isVALU(MI); 678fe6060f1SDimitry Andric }; 679fe6060f1SDimitry Andric auto IsBufferHazardDefFn = [this](const MachineInstr &MI) { 680fe6060f1SDimitry Andric return TII.isSALU(MI); 681fe6060f1SDimitry Andric }; 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric bool IsBufferSMRD = TII.isBufferSMRD(*SMRD); 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric for (const MachineOperand &Use : SMRD->uses()) { 6860b57cec5SDimitry Andric if (!Use.isReg()) 6870b57cec5SDimitry Andric continue; 6880b57cec5SDimitry Andric int WaitStatesNeededForUse = 6890b57cec5SDimitry Andric SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 6900b57cec5SDimitry Andric SmrdSgprWaitStates); 6910b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric // This fixes what appears to be undocumented hardware behavior in SI where 6940b57cec5SDimitry Andric // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor 6950b57cec5SDimitry Andric // needs some number of nops in between. We don't know how many we need, but 6960b57cec5SDimitry Andric // let's use 4. This wasn't discovered before probably because the only 6970b57cec5SDimitry Andric // case when this happens is when we expand a 64-bit pointer into a full 6980b57cec5SDimitry Andric // descriptor and use s_buffer_load_dword instead of s_load_dword, which was 6990b57cec5SDimitry Andric // probably never encountered in the closed-source land. 7000b57cec5SDimitry Andric if (IsBufferSMRD) { 7010b57cec5SDimitry Andric int WaitStatesNeededForUse = 7020b57cec5SDimitry Andric SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), 7030b57cec5SDimitry Andric IsBufferHazardDefFn, 7040b57cec5SDimitry Andric SmrdSgprWaitStates); 7050b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric } 7080b57cec5SDimitry Andric 7090b57cec5SDimitry Andric return WaitStatesNeeded; 7100b57cec5SDimitry Andric } 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { 7130b57cec5SDimitry Andric if (!ST.hasVMEMReadSGPRVALUDefHazard()) 7140b57cec5SDimitry Andric return 0; 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric int WaitStatesNeeded = checkSoftClauseHazards(VMEM); 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric // A read of an SGPR by a VMEM instruction requires 5 wait states when the 7190b57cec5SDimitry Andric // SGPR was written by a VALU Instruction. 7200b57cec5SDimitry Andric const int VmemSgprWaitStates = 5; 721fe6060f1SDimitry Andric auto IsHazardDefFn = [this](const MachineInstr &MI) { 722fe6060f1SDimitry Andric return TII.isVALU(MI); 723fe6060f1SDimitry Andric }; 7240b57cec5SDimitry Andric for (const MachineOperand &Use : VMEM->uses()) { 725fe6060f1SDimitry Andric if (!Use.isReg() || TRI.isVectorRegister(MF.getRegInfo(), Use.getReg())) 7260b57cec5SDimitry Andric continue; 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric int WaitStatesNeededForUse = 7290b57cec5SDimitry Andric VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, 7300b57cec5SDimitry Andric VmemSgprWaitStates); 7310b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric return WaitStatesNeeded; 7340b57cec5SDimitry Andric } 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { 7370b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 7380b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric // Check for DPP VGPR read after VALU VGPR write and EXEC write. 7410b57cec5SDimitry Andric int DppVgprWaitStates = 2; 7420b57cec5SDimitry Andric int DppExecWaitStates = 5; 7430b57cec5SDimitry Andric int WaitStatesNeeded = 0; 744fe6060f1SDimitry Andric auto IsHazardDefFn = [TII](const MachineInstr &MI) { 745fe6060f1SDimitry Andric return TII->isVALU(MI); 746fe6060f1SDimitry Andric }; 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric for (const MachineOperand &Use : DPP->uses()) { 7490b57cec5SDimitry Andric if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) 7500b57cec5SDimitry Andric continue; 7510b57cec5SDimitry Andric int WaitStatesNeededForUse = 752fe6060f1SDimitry Andric DppVgprWaitStates - getWaitStatesSinceDef( 753fe6060f1SDimitry Andric Use.getReg(), 754fe6060f1SDimitry Andric [](const MachineInstr &) { return true; }, 7550b57cec5SDimitry Andric DppVgprWaitStates); 7560b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric WaitStatesNeeded = std::max( 7600b57cec5SDimitry Andric WaitStatesNeeded, 7610b57cec5SDimitry Andric DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn, 7620b57cec5SDimitry Andric DppExecWaitStates)); 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric return WaitStatesNeeded; 7650b57cec5SDimitry Andric } 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { 7680b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric // v_div_fmas requires 4 wait states after a write to vcc from a VALU 7710b57cec5SDimitry Andric // instruction. 7720b57cec5SDimitry Andric const int DivFMasWaitStates = 4; 773fe6060f1SDimitry Andric auto IsHazardDefFn = [TII](const MachineInstr &MI) { 774fe6060f1SDimitry Andric return TII->isVALU(MI); 775fe6060f1SDimitry Andric }; 7760b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn, 7770b57cec5SDimitry Andric DivFMasWaitStates); 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric return DivFMasWaitStates - WaitStatesNeeded; 7800b57cec5SDimitry Andric } 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { 7830b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7840b57cec5SDimitry Andric unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric const int GetRegWaitStates = 2; 787fe6060f1SDimitry Andric auto IsHazardFn = [TII, GetRegHWReg](const MachineInstr &MI) { 788fe6060f1SDimitry Andric return GetRegHWReg == getHWReg(TII, MI); 7890b57cec5SDimitry Andric }; 7900b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates); 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric return GetRegWaitStates - WaitStatesNeeded; 7930b57cec5SDimitry Andric } 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { 7960b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 7970b57cec5SDimitry Andric unsigned HWReg = getHWReg(TII, *SetRegInstr); 7980b57cec5SDimitry Andric 7990b57cec5SDimitry Andric const int SetRegWaitStates = ST.getSetRegWaitStates(); 800fe6060f1SDimitry Andric auto IsHazardFn = [TII, HWReg](const MachineInstr &MI) { 801fe6060f1SDimitry Andric return HWReg == getHWReg(TII, MI); 8020b57cec5SDimitry Andric }; 8030b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates); 8040b57cec5SDimitry Andric return SetRegWaitStates - WaitStatesNeeded; 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { 8080b57cec5SDimitry Andric if (!MI.mayStore()) 8090b57cec5SDimitry Andric return -1; 8100b57cec5SDimitry Andric 8110b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 8120b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode(); 8130b57cec5SDimitry Andric const MCInstrDesc &Desc = MI.getDesc(); 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 8160b57cec5SDimitry Andric int VDataRCID = -1; 8170b57cec5SDimitry Andric if (VDataIdx != -1) 818bdd1243dSDimitry Andric VDataRCID = Desc.operands()[VDataIdx].RegClass; 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { 8210b57cec5SDimitry Andric // There is no hazard if the instruction does not use vector regs 8220b57cec5SDimitry Andric // (like wbinvl1) 8230b57cec5SDimitry Andric if (VDataIdx == -1) 8240b57cec5SDimitry Andric return -1; 8250b57cec5SDimitry Andric // For MUBUF/MTBUF instructions this hazard only exists if the 8260b57cec5SDimitry Andric // instruction is not using a register in the soffset field. 8270b57cec5SDimitry Andric const MachineOperand *SOffset = 8280b57cec5SDimitry Andric TII->getNamedOperand(MI, AMDGPU::OpName::soffset); 8290b57cec5SDimitry Andric // If we have no soffset operand, then assume this field has been 8300b57cec5SDimitry Andric // hardcoded to zero. 8310b57cec5SDimitry Andric if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && 8320b57cec5SDimitry Andric (!SOffset || !SOffset->isReg())) 8330b57cec5SDimitry Andric return VDataIdx; 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric // MIMG instructions create a hazard if they don't use a 256-bit T# and 8370b57cec5SDimitry Andric // the store size is greater than 8 bytes and they have more than two bits 8380b57cec5SDimitry Andric // of their dmask set. 8390b57cec5SDimitry Andric // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. 8400b57cec5SDimitry Andric if (TII->isMIMG(MI)) { 8410b57cec5SDimitry Andric int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 8420b57cec5SDimitry Andric assert(SRsrcIdx != -1 && 843bdd1243dSDimitry Andric AMDGPU::getRegBitWidth(Desc.operands()[SRsrcIdx].RegClass) == 256); 8440b57cec5SDimitry Andric (void)SRsrcIdx; 8450b57cec5SDimitry Andric } 8460b57cec5SDimitry Andric 8470b57cec5SDimitry Andric if (TII->isFLAT(MI)) { 8480b57cec5SDimitry Andric int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); 849bdd1243dSDimitry Andric if (AMDGPU::getRegBitWidth(Desc.operands()[DataIdx].RegClass) > 64) 8500b57cec5SDimitry Andric return DataIdx; 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric 8530b57cec5SDimitry Andric return -1; 8540b57cec5SDimitry Andric } 8550b57cec5SDimitry Andric 856e8d8bef9SDimitry Andric int 857e8d8bef9SDimitry Andric GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def, 8580b57cec5SDimitry Andric const MachineRegisterInfo &MRI) { 8590b57cec5SDimitry Andric // Helper to check for the hazard where VMEM instructions that store more than 8600b57cec5SDimitry Andric // 8 bytes can have there store data over written by the next instruction. 8610b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 8620b57cec5SDimitry Andric 86381ad6265SDimitry Andric const int VALUWaitStates = ST.hasGFX940Insts() ? 2 : 1; 8640b57cec5SDimitry Andric int WaitStatesNeeded = 0; 8650b57cec5SDimitry Andric 866fe6060f1SDimitry Andric if (!TRI->isVectorRegister(MRI, Def.getReg())) 8670b57cec5SDimitry Andric return WaitStatesNeeded; 8688bcb0991SDimitry Andric Register Reg = Def.getReg(); 869fe6060f1SDimitry Andric auto IsHazardFn = [this, Reg, TRI](const MachineInstr &MI) { 870fe6060f1SDimitry Andric int DataIdx = createsVALUHazard(MI); 8710b57cec5SDimitry Andric return DataIdx >= 0 && 872fe6060f1SDimitry Andric TRI->regsOverlap(MI.getOperand(DataIdx).getReg(), Reg); 8730b57cec5SDimitry Andric }; 8740b57cec5SDimitry Andric int WaitStatesNeededForDef = 8750b57cec5SDimitry Andric VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates); 8760b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric return WaitStatesNeeded; 8790b57cec5SDimitry Andric } 8800b57cec5SDimitry Andric 8810b57cec5SDimitry Andric int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { 88281ad6265SDimitry Andric int WaitStatesNeeded = 0; 88381ad6265SDimitry Andric 88481ad6265SDimitry Andric if (ST.hasTransForwardingHazard() && !SIInstrInfo::isTRANS(*VALU)) { 88581ad6265SDimitry Andric const int TransDefWaitstates = 1; 88681ad6265SDimitry Andric 88781ad6265SDimitry Andric auto IsTransDefFn = [this, VALU](const MachineInstr &MI) { 88881ad6265SDimitry Andric if (!SIInstrInfo::isTRANS(MI)) 88981ad6265SDimitry Andric return false; 89081ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 89181ad6265SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 89281ad6265SDimitry Andric Register Def = TII->getNamedOperand(MI, AMDGPU::OpName::vdst)->getReg(); 89381ad6265SDimitry Andric 89481ad6265SDimitry Andric for (const MachineOperand &Use : VALU->explicit_uses()) { 89581ad6265SDimitry Andric if (Use.isReg() && TRI->regsOverlap(Def, Use.getReg())) 89681ad6265SDimitry Andric return true; 89781ad6265SDimitry Andric } 89881ad6265SDimitry Andric 89981ad6265SDimitry Andric return false; 90081ad6265SDimitry Andric }; 90181ad6265SDimitry Andric 90281ad6265SDimitry Andric int WaitStatesNeededForDef = 90381ad6265SDimitry Andric TransDefWaitstates - 90481ad6265SDimitry Andric getWaitStatesSince(IsTransDefFn, TransDefWaitstates); 90581ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 90681ad6265SDimitry Andric } 90781ad6265SDimitry Andric 90881ad6265SDimitry Andric if (ST.hasDstSelForwardingHazard()) { 90981ad6265SDimitry Andric const int Shift16DefWaitstates = 1; 91081ad6265SDimitry Andric 91181ad6265SDimitry Andric auto IsShift16BitDefFn = [this, VALU](const MachineInstr &MI) { 91281ad6265SDimitry Andric if (!SIInstrInfo::isVALU(MI)) 91381ad6265SDimitry Andric return false; 91481ad6265SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 91581ad6265SDimitry Andric if (SIInstrInfo::isSDWA(MI)) { 91681ad6265SDimitry Andric if (auto *DstSel = TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel)) 91781ad6265SDimitry Andric if (DstSel->getImm() == AMDGPU::SDWA::DWORD) 91881ad6265SDimitry Andric return false; 91981ad6265SDimitry Andric } else { 920bdd1243dSDimitry Andric if (!AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::op_sel) || 92181ad6265SDimitry Andric !(TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers) 92281ad6265SDimitry Andric ->getImm() & 92381ad6265SDimitry Andric SISrcMods::DST_OP_SEL)) 92481ad6265SDimitry Andric return false; 92581ad6265SDimitry Andric } 92681ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 92781ad6265SDimitry Andric if (auto *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst)) { 92881ad6265SDimitry Andric Register Def = Dst->getReg(); 92981ad6265SDimitry Andric 93081ad6265SDimitry Andric for (const MachineOperand &Use : VALU->explicit_uses()) { 93181ad6265SDimitry Andric if (Use.isReg() && TRI->regsOverlap(Def, Use.getReg())) 93281ad6265SDimitry Andric return true; 93381ad6265SDimitry Andric } 93481ad6265SDimitry Andric } 93581ad6265SDimitry Andric 93681ad6265SDimitry Andric return false; 93781ad6265SDimitry Andric }; 93881ad6265SDimitry Andric 93981ad6265SDimitry Andric int WaitStatesNeededForDef = 94081ad6265SDimitry Andric Shift16DefWaitstates - 94181ad6265SDimitry Andric getWaitStatesSince(IsShift16BitDefFn, Shift16DefWaitstates); 94281ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 94381ad6265SDimitry Andric } 94481ad6265SDimitry Andric 94581ad6265SDimitry Andric if (ST.hasVDecCoExecHazard()) { 94681ad6265SDimitry Andric const int VALUWriteSGPRVALUReadWaitstates = 2; 94781ad6265SDimitry Andric const int VALUWriteEXECRWLane = 4; 94881ad6265SDimitry Andric const int VALUWriteVGPRReadlaneRead = 1; 94981ad6265SDimitry Andric 95081ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 95181ad6265SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 95281ad6265SDimitry Andric Register UseReg; 95381ad6265SDimitry Andric auto IsVALUDefSGPRFn = [&UseReg, TRI](const MachineInstr &MI) { 95481ad6265SDimitry Andric if (!SIInstrInfo::isVALU(MI)) 95581ad6265SDimitry Andric return false; 95681ad6265SDimitry Andric return MI.modifiesRegister(UseReg, TRI); 95781ad6265SDimitry Andric }; 95881ad6265SDimitry Andric 95981ad6265SDimitry Andric for (const MachineOperand &Use : VALU->explicit_uses()) { 96081ad6265SDimitry Andric if (!Use.isReg()) 96181ad6265SDimitry Andric continue; 96281ad6265SDimitry Andric 96381ad6265SDimitry Andric UseReg = Use.getReg(); 96481ad6265SDimitry Andric if (TRI->isSGPRReg(MRI, UseReg)) { 96581ad6265SDimitry Andric int WaitStatesNeededForDef = 96681ad6265SDimitry Andric VALUWriteSGPRVALUReadWaitstates - 96781ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, 96881ad6265SDimitry Andric VALUWriteSGPRVALUReadWaitstates); 96981ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 97081ad6265SDimitry Andric } 97181ad6265SDimitry Andric } 97281ad6265SDimitry Andric 97381ad6265SDimitry Andric if (VALU->readsRegister(AMDGPU::VCC, TRI)) { 97481ad6265SDimitry Andric UseReg = AMDGPU::VCC; 97581ad6265SDimitry Andric int WaitStatesNeededForDef = 97681ad6265SDimitry Andric VALUWriteSGPRVALUReadWaitstates - 97781ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteSGPRVALUReadWaitstates); 97881ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 97981ad6265SDimitry Andric } 98081ad6265SDimitry Andric 98181ad6265SDimitry Andric switch (VALU->getOpcode()) { 98281ad6265SDimitry Andric case AMDGPU::V_READLANE_B32: 98381ad6265SDimitry Andric case AMDGPU::V_READFIRSTLANE_B32: { 98481ad6265SDimitry Andric MachineOperand *Src = TII.getNamedOperand(*VALU, AMDGPU::OpName::src0); 98581ad6265SDimitry Andric UseReg = Src->getReg(); 98681ad6265SDimitry Andric int WaitStatesNeededForDef = 98781ad6265SDimitry Andric VALUWriteVGPRReadlaneRead - 98881ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteVGPRReadlaneRead); 98981ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 99081ad6265SDimitry Andric } 991bdd1243dSDimitry Andric [[fallthrough]]; 99281ad6265SDimitry Andric case AMDGPU::V_WRITELANE_B32: { 99381ad6265SDimitry Andric UseReg = AMDGPU::EXEC; 99481ad6265SDimitry Andric int WaitStatesNeededForDef = 99581ad6265SDimitry Andric VALUWriteEXECRWLane - 99681ad6265SDimitry Andric getWaitStatesSince(IsVALUDefSGPRFn, VALUWriteEXECRWLane); 99781ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); 99881ad6265SDimitry Andric break; 99981ad6265SDimitry Andric } 100081ad6265SDimitry Andric default: 100181ad6265SDimitry Andric break; 100281ad6265SDimitry Andric } 100381ad6265SDimitry Andric } 100481ad6265SDimitry Andric 10050b57cec5SDimitry Andric // This checks for the hazard where VMEM instructions that store more than 10060b57cec5SDimitry Andric // 8 bytes can have there store data over written by the next instruction. 10070b57cec5SDimitry Andric if (!ST.has12DWordStoreHazard()) 100881ad6265SDimitry Andric return WaitStatesNeeded; 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric for (const MachineOperand &Def : VALU->defs()) { 10130b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI)); 10140b57cec5SDimitry Andric } 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric return WaitStatesNeeded; 10170b57cec5SDimitry Andric } 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) { 10200b57cec5SDimitry Andric // This checks for hazards associated with inline asm statements. 10210b57cec5SDimitry Andric // Since inline asms can contain just about anything, we use this 10220b57cec5SDimitry Andric // to call/leverage other check*Hazard routines. Note that 10230b57cec5SDimitry Andric // this function doesn't attempt to address all possible inline asm 10240b57cec5SDimitry Andric // hazards (good luck), but is a collection of what has been 10250b57cec5SDimitry Andric // problematic thus far. 10260b57cec5SDimitry Andric 10270b57cec5SDimitry Andric // see checkVALUHazards() 10280b57cec5SDimitry Andric if (!ST.has12DWordStoreHazard()) 10290b57cec5SDimitry Andric return 0; 10300b57cec5SDimitry Andric 10310b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 10320b57cec5SDimitry Andric int WaitStatesNeeded = 0; 10330b57cec5SDimitry Andric 1034*06c3fb27SDimitry Andric for (const MachineOperand &Op : 1035*06c3fb27SDimitry Andric llvm::drop_begin(IA->operands(), InlineAsm::MIOp_FirstOperand)) { 10360b57cec5SDimitry Andric if (Op.isReg() && Op.isDef()) { 1037*06c3fb27SDimitry Andric WaitStatesNeeded = 1038*06c3fb27SDimitry Andric std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI)); 10390b57cec5SDimitry Andric } 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric return WaitStatesNeeded; 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric 10450b57cec5SDimitry Andric int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { 10460b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 10470b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 10480b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 10490b57cec5SDimitry Andric 10500b57cec5SDimitry Andric const MachineOperand *LaneSelectOp = 10510b57cec5SDimitry Andric TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) 10540b57cec5SDimitry Andric return 0; 10550b57cec5SDimitry Andric 10568bcb0991SDimitry Andric Register LaneSelectReg = LaneSelectOp->getReg(); 1057fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &MI) { return TII->isVALU(MI); }; 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric const int RWLaneWaitStates = 4; 10600b57cec5SDimitry Andric int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn, 10610b57cec5SDimitry Andric RWLaneWaitStates); 10620b57cec5SDimitry Andric return RWLaneWaitStates - WaitStatesSince; 10630b57cec5SDimitry Andric } 10640b57cec5SDimitry Andric 10650b57cec5SDimitry Andric int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { 10660b57cec5SDimitry Andric if (!ST.hasRFEHazards()) 10670b57cec5SDimitry Andric return 0; 10680b57cec5SDimitry Andric 10690b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric const int RFEWaitStates = 1; 10720b57cec5SDimitry Andric 1073fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &MI) { 1074fe6060f1SDimitry Andric return getHWReg(TII, MI) == AMDGPU::Hwreg::ID_TRAPSTS; 10750b57cec5SDimitry Andric }; 10760b57cec5SDimitry Andric int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates); 10770b57cec5SDimitry Andric return RFEWaitStates - WaitStatesNeeded; 10780b57cec5SDimitry Andric } 10790b57cec5SDimitry Andric 10800b57cec5SDimitry Andric int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { 10810b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 108281ad6265SDimitry Andric const int ReadM0WaitStates = 1; 1083fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &MI) { return TII->isSALU(MI); }; 108481ad6265SDimitry Andric return ReadM0WaitStates - 108581ad6265SDimitry Andric getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn, ReadM0WaitStates); 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric void GCNHazardRecognizer::fixHazards(MachineInstr *MI) { 10890b57cec5SDimitry Andric fixVMEMtoScalarWriteHazards(MI); 10900b57cec5SDimitry Andric fixVcmpxPermlaneHazards(MI); 10910b57cec5SDimitry Andric fixSMEMtoVectorWriteHazards(MI); 10920b57cec5SDimitry Andric fixVcmpxExecWARHazard(MI); 10930b57cec5SDimitry Andric fixLdsBranchVmemWARHazard(MI); 109481ad6265SDimitry Andric if (ST.hasLdsDirect()) { 109581ad6265SDimitry Andric fixLdsDirectVALUHazard(MI); 109681ad6265SDimitry Andric fixLdsDirectVMEMHazard(MI); 109781ad6265SDimitry Andric } 109881ad6265SDimitry Andric fixVALUPartialForwardingHazard(MI); 109981ad6265SDimitry Andric fixVALUTransUseHazard(MI); 110081ad6265SDimitry Andric fixWMMAHazards(MI); 1101bdd1243dSDimitry Andric fixShift64HighRegBug(MI); 1102bdd1243dSDimitry Andric fixVALUMaskWriteHazard(MI); 11030b57cec5SDimitry Andric } 11040b57cec5SDimitry Andric 11050b57cec5SDimitry Andric bool GCNHazardRecognizer::fixVcmpxPermlaneHazards(MachineInstr *MI) { 11060b57cec5SDimitry Andric if (!ST.hasVcmpxPermlaneHazard() || !isPermlane(*MI)) 11070b57cec5SDimitry Andric return false; 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 111081ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 111181ad6265SDimitry Andric auto IsHazardFn = [TII, TRI](const MachineInstr &MI) { 111281ad6265SDimitry Andric return (TII->isVOPC(MI) || 111381ad6265SDimitry Andric ((TII->isVOP3(MI) || TII->isSDWA(MI)) && MI.isCompare())) && 111481ad6265SDimitry Andric MI.modifiesRegister(AMDGPU::EXEC, TRI); 111581ad6265SDimitry Andric }; 11160b57cec5SDimitry Andric 1117fe6060f1SDimitry Andric auto IsExpiredFn = [](const MachineInstr &MI, int) { 1118fe6060f1SDimitry Andric unsigned Opc = MI.getOpcode(); 1119fe6060f1SDimitry Andric return SIInstrInfo::isVALU(MI) && Opc != AMDGPU::V_NOP_e32 && 1120fe6060f1SDimitry Andric Opc != AMDGPU::V_NOP_e64 && Opc != AMDGPU::V_NOP_sdwa; 11210b57cec5SDimitry Andric }; 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 11240b57cec5SDimitry Andric std::numeric_limits<int>::max()) 11250b57cec5SDimitry Andric return false; 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric // V_NOP will be discarded by SQ. 112881ad6265SDimitry Andric // Use V_MOV_B32 v?, v?. Register must be alive so use src0 of V_PERMLANE* 11290b57cec5SDimitry Andric // which is always a VGPR and available. 11300b57cec5SDimitry Andric auto *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0); 11318bcb0991SDimitry Andric Register Reg = Src0->getReg(); 11320b57cec5SDimitry Andric bool IsUndef = Src0->isUndef(); 11330b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 11340b57cec5SDimitry Andric TII->get(AMDGPU::V_MOV_B32_e32)) 11350b57cec5SDimitry Andric .addReg(Reg, RegState::Define | (IsUndef ? RegState::Dead : 0)) 11360b57cec5SDimitry Andric .addReg(Reg, IsUndef ? RegState::Undef : RegState::Kill); 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric return true; 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric bool GCNHazardRecognizer::fixVMEMtoScalarWriteHazards(MachineInstr *MI) { 11420b57cec5SDimitry Andric if (!ST.hasVMEMtoScalarWriteHazard()) 11430b57cec5SDimitry Andric return false; 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric if (!SIInstrInfo::isSALU(*MI) && !SIInstrInfo::isSMRD(*MI)) 11460b57cec5SDimitry Andric return false; 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric if (MI->getNumDefs() == 0) 11490b57cec5SDimitry Andric return false; 11500b57cec5SDimitry Andric 11510b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 11520b57cec5SDimitry Andric 1153fe6060f1SDimitry Andric auto IsHazardFn = [TRI, MI](const MachineInstr &I) { 1154fe6060f1SDimitry Andric if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isDS(I) && 1155fe6060f1SDimitry Andric !SIInstrInfo::isFLAT(I)) 11560b57cec5SDimitry Andric return false; 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andric for (const MachineOperand &Def : MI->defs()) { 1159fe6060f1SDimitry Andric const MachineOperand *Op = 1160fe6060f1SDimitry Andric I.findRegisterUseOperand(Def.getReg(), false, TRI); 11610b57cec5SDimitry Andric if (!Op) 11620b57cec5SDimitry Andric continue; 11630b57cec5SDimitry Andric return true; 11640b57cec5SDimitry Andric } 11650b57cec5SDimitry Andric return false; 11660b57cec5SDimitry Andric }; 11670b57cec5SDimitry Andric 1168fe6060f1SDimitry Andric auto IsExpiredFn = [](const MachineInstr &MI, int) { 1169fe6060f1SDimitry Andric return SIInstrInfo::isVALU(MI) || 1170fe6060f1SDimitry Andric (MI.getOpcode() == AMDGPU::S_WAITCNT && 1171fe6060f1SDimitry Andric !MI.getOperand(0).getImm()) || 1172fe6060f1SDimitry Andric (MI.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1173*06c3fb27SDimitry Andric AMDGPU::DepCtr::decodeFieldVmVsrc(MI.getOperand(0).getImm()) == 0); 11740b57cec5SDimitry Andric }; 11750b57cec5SDimitry Andric 11760b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 11770b57cec5SDimitry Andric std::numeric_limits<int>::max()) 11780b57cec5SDimitry Andric return false; 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 1181e8d8bef9SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 1182e8d8bef9SDimitry Andric TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 1183*06c3fb27SDimitry Andric .addImm(AMDGPU::DepCtr::encodeFieldVmVsrc(0)); 11840b57cec5SDimitry Andric return true; 11850b57cec5SDimitry Andric } 11860b57cec5SDimitry Andric 11870b57cec5SDimitry Andric bool GCNHazardRecognizer::fixSMEMtoVectorWriteHazards(MachineInstr *MI) { 11880b57cec5SDimitry Andric if (!ST.hasSMEMtoVectorWriteHazard()) 11890b57cec5SDimitry Andric return false; 11900b57cec5SDimitry Andric 11910b57cec5SDimitry Andric if (!SIInstrInfo::isVALU(*MI)) 11920b57cec5SDimitry Andric return false; 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric unsigned SDSTName; 11950b57cec5SDimitry Andric switch (MI->getOpcode()) { 11960b57cec5SDimitry Andric case AMDGPU::V_READLANE_B32: 11970b57cec5SDimitry Andric case AMDGPU::V_READFIRSTLANE_B32: 11980b57cec5SDimitry Andric SDSTName = AMDGPU::OpName::vdst; 11990b57cec5SDimitry Andric break; 12000b57cec5SDimitry Andric default: 12010b57cec5SDimitry Andric SDSTName = AMDGPU::OpName::sdst; 12020b57cec5SDimitry Andric break; 12030b57cec5SDimitry Andric } 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 12060b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 12070b57cec5SDimitry Andric const AMDGPU::IsaVersion IV = AMDGPU::getIsaVersion(ST.getCPU()); 12080b57cec5SDimitry Andric const MachineOperand *SDST = TII->getNamedOperand(*MI, SDSTName); 12090b57cec5SDimitry Andric if (!SDST) { 12100b57cec5SDimitry Andric for (const auto &MO : MI->implicit_operands()) { 1211bdd1243dSDimitry Andric if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegBaseClass(MO.getReg()))) { 12120b57cec5SDimitry Andric SDST = &MO; 12130b57cec5SDimitry Andric break; 12140b57cec5SDimitry Andric } 12150b57cec5SDimitry Andric } 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric if (!SDST) 12190b57cec5SDimitry Andric return false; 12200b57cec5SDimitry Andric 12218bcb0991SDimitry Andric const Register SDSTReg = SDST->getReg(); 1222fe6060f1SDimitry Andric auto IsHazardFn = [SDSTReg, TRI](const MachineInstr &I) { 1223fe6060f1SDimitry Andric return SIInstrInfo::isSMRD(I) && I.readsRegister(SDSTReg, TRI); 12240b57cec5SDimitry Andric }; 12250b57cec5SDimitry Andric 1226fe6060f1SDimitry Andric auto IsExpiredFn = [TII, IV](const MachineInstr &MI, int) { 1227fe6060f1SDimitry Andric if (TII->isSALU(MI)) { 1228fe6060f1SDimitry Andric switch (MI.getOpcode()) { 12290b57cec5SDimitry Andric case AMDGPU::S_SETVSKIP: 12300b57cec5SDimitry Andric case AMDGPU::S_VERSION: 12310b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VSCNT: 12320b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VMCNT: 12330b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_EXPCNT: 12340b57cec5SDimitry Andric // These instructions cannot not mitigate the hazard. 12350b57cec5SDimitry Andric return false; 12360b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_LGKMCNT: 12370b57cec5SDimitry Andric // Reducing lgkmcnt count to 0 always mitigates the hazard. 1238fe6060f1SDimitry Andric return (MI.getOperand(1).getImm() == 0) && 1239fe6060f1SDimitry Andric (MI.getOperand(0).getReg() == AMDGPU::SGPR_NULL); 12400b57cec5SDimitry Andric case AMDGPU::S_WAITCNT: { 1241fe6060f1SDimitry Andric const int64_t Imm = MI.getOperand(0).getImm(); 12420b57cec5SDimitry Andric AMDGPU::Waitcnt Decoded = AMDGPU::decodeWaitcnt(IV, Imm); 12430b57cec5SDimitry Andric return (Decoded.LgkmCnt == 0); 12440b57cec5SDimitry Andric } 12450b57cec5SDimitry Andric default: 12460b57cec5SDimitry Andric // SOPP instructions cannot mitigate the hazard. 1247fe6060f1SDimitry Andric if (TII->isSOPP(MI)) 12480b57cec5SDimitry Andric return false; 12490b57cec5SDimitry Andric // At this point the SALU can be assumed to mitigate the hazard 12500b57cec5SDimitry Andric // because either: 12510b57cec5SDimitry Andric // (a) it is independent of the at risk SMEM (breaking chain), 12520b57cec5SDimitry Andric // or 12530b57cec5SDimitry Andric // (b) it is dependent on the SMEM, in which case an appropriate 12540b57cec5SDimitry Andric // s_waitcnt lgkmcnt _must_ exist between it and the at risk 12550b57cec5SDimitry Andric // SMEM instruction. 12560b57cec5SDimitry Andric return true; 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric } 12590b57cec5SDimitry Andric return false; 12600b57cec5SDimitry Andric }; 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 12630b57cec5SDimitry Andric std::numeric_limits<int>::max()) 12640b57cec5SDimitry Andric return false; 12650b57cec5SDimitry Andric 12660b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 12670b57cec5SDimitry Andric TII->get(AMDGPU::S_MOV_B32), AMDGPU::SGPR_NULL) 12680b57cec5SDimitry Andric .addImm(0); 12690b57cec5SDimitry Andric return true; 12700b57cec5SDimitry Andric } 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric bool GCNHazardRecognizer::fixVcmpxExecWARHazard(MachineInstr *MI) { 12730b57cec5SDimitry Andric if (!ST.hasVcmpxExecWARHazard() || !SIInstrInfo::isVALU(*MI)) 12740b57cec5SDimitry Andric return false; 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 12770b57cec5SDimitry Andric if (!MI->modifiesRegister(AMDGPU::EXEC, TRI)) 12780b57cec5SDimitry Andric return false; 12790b57cec5SDimitry Andric 1280fe6060f1SDimitry Andric auto IsHazardFn = [TRI](const MachineInstr &I) { 1281fe6060f1SDimitry Andric if (SIInstrInfo::isVALU(I)) 12820b57cec5SDimitry Andric return false; 1283fe6060f1SDimitry Andric return I.readsRegister(AMDGPU::EXEC, TRI); 12840b57cec5SDimitry Andric }; 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 1287fe6060f1SDimitry Andric auto IsExpiredFn = [TII, TRI](const MachineInstr &MI, int) { 1288fe6060f1SDimitry Andric if (SIInstrInfo::isVALU(MI)) { 1289fe6060f1SDimitry Andric if (TII->getNamedOperand(MI, AMDGPU::OpName::sdst)) 12900b57cec5SDimitry Andric return true; 1291fe6060f1SDimitry Andric for (auto MO : MI.implicit_operands()) 1292bdd1243dSDimitry Andric if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegBaseClass(MO.getReg()))) 12930b57cec5SDimitry Andric return true; 12940b57cec5SDimitry Andric } 1295fe6060f1SDimitry Andric if (MI.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1296*06c3fb27SDimitry Andric AMDGPU::DepCtr::decodeFieldSaSdst(MI.getOperand(0).getImm()) == 0) 12970b57cec5SDimitry Andric return true; 12980b57cec5SDimitry Andric return false; 12990b57cec5SDimitry Andric }; 13000b57cec5SDimitry Andric 13010b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 13020b57cec5SDimitry Andric std::numeric_limits<int>::max()) 13030b57cec5SDimitry Andric return false; 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 13060b57cec5SDimitry Andric TII->get(AMDGPU::S_WAITCNT_DEPCTR)) 1307*06c3fb27SDimitry Andric .addImm(AMDGPU::DepCtr::encodeFieldSaSdst(0)); 13080b57cec5SDimitry Andric return true; 13090b57cec5SDimitry Andric } 13100b57cec5SDimitry Andric 1311fe6060f1SDimitry Andric static bool shouldRunLdsBranchVmemWARHazardFixup(const MachineFunction &MF, 1312fe6060f1SDimitry Andric const GCNSubtarget &ST) { 13130b57cec5SDimitry Andric if (!ST.hasLdsBranchVmemWARHazard()) 13140b57cec5SDimitry Andric return false; 13150b57cec5SDimitry Andric 1316fe6060f1SDimitry Andric // Check if the necessary condition for the hazard is met: both LDS and VMEM 1317fe6060f1SDimitry Andric // instructions need to appear in the same function. 1318fe6060f1SDimitry Andric bool HasLds = false; 1319fe6060f1SDimitry Andric bool HasVmem = false; 1320fe6060f1SDimitry Andric for (auto &MBB : MF) { 1321fe6060f1SDimitry Andric for (auto &MI : MBB) { 1322fe6060f1SDimitry Andric HasLds |= SIInstrInfo::isDS(MI); 1323fe6060f1SDimitry Andric HasVmem |= 1324fe6060f1SDimitry Andric SIInstrInfo::isVMEM(MI) || SIInstrInfo::isSegmentSpecificFLAT(MI); 1325fe6060f1SDimitry Andric if (HasLds && HasVmem) 1326fe6060f1SDimitry Andric return true; 1327fe6060f1SDimitry Andric } 1328fe6060f1SDimitry Andric } 1329fe6060f1SDimitry Andric return false; 1330fe6060f1SDimitry Andric } 1331fe6060f1SDimitry Andric 1332bdd1243dSDimitry Andric static bool isStoreCountWaitZero(const MachineInstr &I) { 1333bdd1243dSDimitry Andric return I.getOpcode() == AMDGPU::S_WAITCNT_VSCNT && 1334bdd1243dSDimitry Andric I.getOperand(0).getReg() == AMDGPU::SGPR_NULL && 1335bdd1243dSDimitry Andric !I.getOperand(1).getImm(); 1336bdd1243dSDimitry Andric } 1337bdd1243dSDimitry Andric 1338fe6060f1SDimitry Andric bool GCNHazardRecognizer::fixLdsBranchVmemWARHazard(MachineInstr *MI) { 1339fe6060f1SDimitry Andric if (!RunLdsBranchVmemWARHazardFixup) 1340fe6060f1SDimitry Andric return false; 1341fe6060f1SDimitry Andric 1342fe6060f1SDimitry Andric assert(ST.hasLdsBranchVmemWARHazard()); 1343fe6060f1SDimitry Andric 1344fe6060f1SDimitry Andric auto IsHazardInst = [](const MachineInstr &MI) { 1345fe6060f1SDimitry Andric if (SIInstrInfo::isDS(MI)) 13460b57cec5SDimitry Andric return 1; 1347fe6060f1SDimitry Andric if (SIInstrInfo::isVMEM(MI) || SIInstrInfo::isSegmentSpecificFLAT(MI)) 13480b57cec5SDimitry Andric return 2; 13490b57cec5SDimitry Andric return 0; 13500b57cec5SDimitry Andric }; 13510b57cec5SDimitry Andric 1352fe6060f1SDimitry Andric auto InstType = IsHazardInst(*MI); 13530b57cec5SDimitry Andric if (!InstType) 13540b57cec5SDimitry Andric return false; 13550b57cec5SDimitry Andric 1356fe6060f1SDimitry Andric auto IsExpiredFn = [&IsHazardInst](const MachineInstr &I, int) { 1357bdd1243dSDimitry Andric return IsHazardInst(I) || isStoreCountWaitZero(I); 13580b57cec5SDimitry Andric }; 13590b57cec5SDimitry Andric 1360fe6060f1SDimitry Andric auto IsHazardFn = [InstType, &IsHazardInst](const MachineInstr &I) { 1361fe6060f1SDimitry Andric if (!I.isBranch()) 13620b57cec5SDimitry Andric return false; 13630b57cec5SDimitry Andric 1364fe6060f1SDimitry Andric auto IsHazardFn = [InstType, IsHazardInst](const MachineInstr &I) { 13650b57cec5SDimitry Andric auto InstType2 = IsHazardInst(I); 13660b57cec5SDimitry Andric return InstType2 && InstType != InstType2; 13670b57cec5SDimitry Andric }; 13680b57cec5SDimitry Andric 1369fe6060f1SDimitry Andric auto IsExpiredFn = [InstType, &IsHazardInst](const MachineInstr &I, int) { 13700b57cec5SDimitry Andric auto InstType2 = IsHazardInst(I); 13710b57cec5SDimitry Andric if (InstType == InstType2) 13720b57cec5SDimitry Andric return true; 13730b57cec5SDimitry Andric 1374bdd1243dSDimitry Andric return isStoreCountWaitZero(I); 13750b57cec5SDimitry Andric }; 13760b57cec5SDimitry Andric 1377fe6060f1SDimitry Andric return ::getWaitStatesSince(IsHazardFn, &I, IsExpiredFn) != 13780b57cec5SDimitry Andric std::numeric_limits<int>::max(); 13790b57cec5SDimitry Andric }; 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 13820b57cec5SDimitry Andric std::numeric_limits<int>::max()) 13830b57cec5SDimitry Andric return false; 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 13860b57cec5SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 13870b57cec5SDimitry Andric TII->get(AMDGPU::S_WAITCNT_VSCNT)) 13880b57cec5SDimitry Andric .addReg(AMDGPU::SGPR_NULL, RegState::Undef) 13890b57cec5SDimitry Andric .addImm(0); 13900b57cec5SDimitry Andric 13910b57cec5SDimitry Andric return true; 13920b57cec5SDimitry Andric } 13930b57cec5SDimitry Andric 139481ad6265SDimitry Andric bool GCNHazardRecognizer::fixLdsDirectVALUHazard(MachineInstr *MI) { 139581ad6265SDimitry Andric if (!SIInstrInfo::isLDSDIR(*MI)) 139681ad6265SDimitry Andric return false; 139781ad6265SDimitry Andric 139881ad6265SDimitry Andric const int NoHazardWaitStates = 15; 139981ad6265SDimitry Andric const MachineOperand *VDST = TII.getNamedOperand(*MI, AMDGPU::OpName::vdst); 140081ad6265SDimitry Andric const Register VDSTReg = VDST->getReg(); 140181ad6265SDimitry Andric 140281ad6265SDimitry Andric bool VisitedTrans = false; 140381ad6265SDimitry Andric auto IsHazardFn = [this, VDSTReg, &VisitedTrans](const MachineInstr &I) { 140481ad6265SDimitry Andric if (!SIInstrInfo::isVALU(I)) 140581ad6265SDimitry Andric return false; 140681ad6265SDimitry Andric VisitedTrans = VisitedTrans || SIInstrInfo::isTRANS(I); 140781ad6265SDimitry Andric // Cover both WAR and WAW 140881ad6265SDimitry Andric return I.readsRegister(VDSTReg, &TRI) || I.modifiesRegister(VDSTReg, &TRI); 140981ad6265SDimitry Andric }; 141081ad6265SDimitry Andric auto IsExpiredFn = [&](const MachineInstr &I, int WaitStates) { 141181ad6265SDimitry Andric if (WaitStates >= NoHazardWaitStates) 141281ad6265SDimitry Andric return true; 141381ad6265SDimitry Andric // Instructions which cause va_vdst==0 expire hazard 141481ad6265SDimitry Andric return SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 141581ad6265SDimitry Andric SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I); 141681ad6265SDimitry Andric }; 141781ad6265SDimitry Andric auto GetWaitStatesFn = [](const MachineInstr &MI) { 141881ad6265SDimitry Andric return SIInstrInfo::isVALU(MI) ? 1 : 0; 141981ad6265SDimitry Andric }; 142081ad6265SDimitry Andric 142181ad6265SDimitry Andric DenseSet<const MachineBasicBlock *> Visited; 142281ad6265SDimitry Andric auto Count = ::getWaitStatesSince(IsHazardFn, MI->getParent(), 142381ad6265SDimitry Andric std::next(MI->getReverseIterator()), 0, 142481ad6265SDimitry Andric IsExpiredFn, Visited, GetWaitStatesFn); 142581ad6265SDimitry Andric 142681ad6265SDimitry Andric // Transcendentals can execute in parallel to other VALUs. 142781ad6265SDimitry Andric // This makes va_vdst count unusable with a mixture of VALU and TRANS. 142881ad6265SDimitry Andric if (VisitedTrans) 142981ad6265SDimitry Andric Count = 0; 143081ad6265SDimitry Andric 143181ad6265SDimitry Andric MachineOperand *WaitVdstOp = 143281ad6265SDimitry Andric TII.getNamedOperand(*MI, AMDGPU::OpName::waitvdst); 143381ad6265SDimitry Andric WaitVdstOp->setImm(std::min(Count, NoHazardWaitStates)); 143481ad6265SDimitry Andric 143581ad6265SDimitry Andric return true; 143681ad6265SDimitry Andric } 143781ad6265SDimitry Andric 143881ad6265SDimitry Andric bool GCNHazardRecognizer::fixLdsDirectVMEMHazard(MachineInstr *MI) { 143981ad6265SDimitry Andric if (!SIInstrInfo::isLDSDIR(*MI)) 144081ad6265SDimitry Andric return false; 144181ad6265SDimitry Andric 144281ad6265SDimitry Andric const MachineOperand *VDST = TII.getNamedOperand(*MI, AMDGPU::OpName::vdst); 144381ad6265SDimitry Andric const Register VDSTReg = VDST->getReg(); 144481ad6265SDimitry Andric 144581ad6265SDimitry Andric auto IsHazardFn = [this, VDSTReg](const MachineInstr &I) { 144681ad6265SDimitry Andric if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isFLAT(I) && 144781ad6265SDimitry Andric !SIInstrInfo::isDS(I)) 144881ad6265SDimitry Andric return false; 144981ad6265SDimitry Andric return I.readsRegister(VDSTReg, &TRI) || I.modifiesRegister(VDSTReg, &TRI); 145081ad6265SDimitry Andric }; 145181ad6265SDimitry Andric auto IsExpiredFn = [](const MachineInstr &I, int) { 145281ad6265SDimitry Andric return SIInstrInfo::isVALU(I) || SIInstrInfo::isEXP(I) || 145381ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT && !I.getOperand(0).getImm()) || 145481ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1455*06c3fb27SDimitry Andric AMDGPU::DepCtr::decodeFieldVmVsrc(I.getOperand(0).getImm()) == 0); 145681ad6265SDimitry Andric }; 145781ad6265SDimitry Andric 145881ad6265SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 145981ad6265SDimitry Andric std::numeric_limits<int>::max()) 146081ad6265SDimitry Andric return false; 146181ad6265SDimitry Andric 146281ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 146381ad6265SDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 1464*06c3fb27SDimitry Andric .addImm(AMDGPU::DepCtr::encodeFieldVmVsrc(0)); 146581ad6265SDimitry Andric 146681ad6265SDimitry Andric return true; 146781ad6265SDimitry Andric } 146881ad6265SDimitry Andric 146981ad6265SDimitry Andric bool GCNHazardRecognizer::fixVALUPartialForwardingHazard(MachineInstr *MI) { 147081ad6265SDimitry Andric if (!ST.isWave64()) 147181ad6265SDimitry Andric return false; 147281ad6265SDimitry Andric if (!ST.hasVALUPartialForwardingHazard()) 147381ad6265SDimitry Andric return false; 147481ad6265SDimitry Andric if (!SIInstrInfo::isVALU(*MI)) 147581ad6265SDimitry Andric return false; 147681ad6265SDimitry Andric 147781ad6265SDimitry Andric SmallSetVector<Register, 4> SrcVGPRs; 147881ad6265SDimitry Andric 147981ad6265SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 148081ad6265SDimitry Andric if (Use.isReg() && TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 148181ad6265SDimitry Andric SrcVGPRs.insert(Use.getReg()); 148281ad6265SDimitry Andric } 148381ad6265SDimitry Andric 148481ad6265SDimitry Andric // Only applies with >= 2 unique VGPR sources 148581ad6265SDimitry Andric if (SrcVGPRs.size() <= 1) 148681ad6265SDimitry Andric return false; 148781ad6265SDimitry Andric 148881ad6265SDimitry Andric // Look for the following pattern: 148981ad6265SDimitry Andric // Va <- VALU [PreExecPos] 149081ad6265SDimitry Andric // intv1 149181ad6265SDimitry Andric // Exec <- SALU [ExecPos] 149281ad6265SDimitry Andric // intv2 149381ad6265SDimitry Andric // Vb <- VALU [PostExecPos] 149481ad6265SDimitry Andric // intv3 149581ad6265SDimitry Andric // MI Va, Vb (WaitState = 0) 149681ad6265SDimitry Andric // 149781ad6265SDimitry Andric // Where: 149881ad6265SDimitry Andric // intv1 + intv2 <= 2 VALUs 149981ad6265SDimitry Andric // intv3 <= 4 VALUs 150081ad6265SDimitry Andric // 150181ad6265SDimitry Andric // If found, insert an appropriate S_WAITCNT_DEPCTR before MI. 150281ad6265SDimitry Andric 150381ad6265SDimitry Andric const int Intv1plus2MaxVALUs = 2; 150481ad6265SDimitry Andric const int Intv3MaxVALUs = 4; 150581ad6265SDimitry Andric const int IntvMaxVALUs = 6; 150681ad6265SDimitry Andric const int NoHazardVALUWaitStates = IntvMaxVALUs + 2; 150781ad6265SDimitry Andric 150881ad6265SDimitry Andric struct StateType { 150981ad6265SDimitry Andric SmallDenseMap<Register, int, 4> DefPos; 151081ad6265SDimitry Andric int ExecPos = std::numeric_limits<int>::max(); 151181ad6265SDimitry Andric int VALUs = 0; 151281ad6265SDimitry Andric }; 151381ad6265SDimitry Andric 151481ad6265SDimitry Andric StateType State; 151581ad6265SDimitry Andric 151681ad6265SDimitry Andric // This overloads expiry testing with all the hazard detection 151781ad6265SDimitry Andric auto IsHazardFn = [&, this](StateType &State, const MachineInstr &I) { 151881ad6265SDimitry Andric // Too many VALU states have passed 151981ad6265SDimitry Andric if (State.VALUs > NoHazardVALUWaitStates) 152081ad6265SDimitry Andric return HazardExpired; 152181ad6265SDimitry Andric 152281ad6265SDimitry Andric // Instructions which cause va_vdst==0 expire hazard 152381ad6265SDimitry Andric if (SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 152481ad6265SDimitry Andric SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I) || 152581ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 1526*06c3fb27SDimitry Andric AMDGPU::DepCtr::decodeFieldVaVdst(I.getOperand(0).getImm()) == 0)) 152781ad6265SDimitry Andric return HazardExpired; 152881ad6265SDimitry Andric 152981ad6265SDimitry Andric // Track registers writes 153081ad6265SDimitry Andric bool Changed = false; 153181ad6265SDimitry Andric if (SIInstrInfo::isVALU(I)) { 153281ad6265SDimitry Andric for (Register Src : SrcVGPRs) { 153381ad6265SDimitry Andric if (!State.DefPos.count(Src) && I.modifiesRegister(Src, &TRI)) { 153481ad6265SDimitry Andric State.DefPos[Src] = State.VALUs; 153581ad6265SDimitry Andric Changed = true; 153681ad6265SDimitry Andric } 153781ad6265SDimitry Andric } 153881ad6265SDimitry Andric } else if (SIInstrInfo::isSALU(I)) { 153981ad6265SDimitry Andric if (State.ExecPos == std::numeric_limits<int>::max()) { 154081ad6265SDimitry Andric if (!State.DefPos.empty() && I.modifiesRegister(AMDGPU::EXEC, &TRI)) { 154181ad6265SDimitry Andric State.ExecPos = State.VALUs; 154281ad6265SDimitry Andric Changed = true; 154381ad6265SDimitry Andric } 154481ad6265SDimitry Andric } 154581ad6265SDimitry Andric } 154681ad6265SDimitry Andric 154781ad6265SDimitry Andric // Early expiration: too many VALUs in intv3 154881ad6265SDimitry Andric if (State.VALUs > Intv3MaxVALUs && State.DefPos.empty()) 154981ad6265SDimitry Andric return HazardExpired; 155081ad6265SDimitry Andric 155181ad6265SDimitry Andric // Only evaluate state if something changed 155281ad6265SDimitry Andric if (!Changed) 155381ad6265SDimitry Andric return NoHazardFound; 155481ad6265SDimitry Andric 155581ad6265SDimitry Andric // Determine positions of VALUs pre/post exec change 155681ad6265SDimitry Andric if (State.ExecPos == std::numeric_limits<int>::max()) 155781ad6265SDimitry Andric return NoHazardFound; 155881ad6265SDimitry Andric 155981ad6265SDimitry Andric int PreExecPos = std::numeric_limits<int>::max(); 156081ad6265SDimitry Andric int PostExecPos = std::numeric_limits<int>::max(); 156181ad6265SDimitry Andric 156281ad6265SDimitry Andric for (auto Entry : State.DefPos) { 156381ad6265SDimitry Andric int DefVALUs = Entry.second; 156481ad6265SDimitry Andric if (DefVALUs != std::numeric_limits<int>::max()) { 156581ad6265SDimitry Andric if (DefVALUs >= State.ExecPos) 156681ad6265SDimitry Andric PreExecPos = std::min(PreExecPos, DefVALUs); 156781ad6265SDimitry Andric else if (DefVALUs < State.ExecPos) 156881ad6265SDimitry Andric PostExecPos = std::min(PostExecPos, DefVALUs); 156981ad6265SDimitry Andric } 157081ad6265SDimitry Andric } 157181ad6265SDimitry Andric 157281ad6265SDimitry Andric // Need a VALUs post exec change 157381ad6265SDimitry Andric if (PostExecPos == std::numeric_limits<int>::max()) 157481ad6265SDimitry Andric return NoHazardFound; 157581ad6265SDimitry Andric 157681ad6265SDimitry Andric // Too many VALUs in intv3? 157781ad6265SDimitry Andric int Intv3VALUs = PostExecPos; 157881ad6265SDimitry Andric if (Intv3VALUs > Intv3MaxVALUs) 157981ad6265SDimitry Andric return HazardExpired; 158081ad6265SDimitry Andric 158181ad6265SDimitry Andric // Too many VALUs in intv2? 158281ad6265SDimitry Andric int Intv2VALUs = (State.ExecPos - PostExecPos) - 1; 158381ad6265SDimitry Andric if (Intv2VALUs > Intv1plus2MaxVALUs) 158481ad6265SDimitry Andric return HazardExpired; 158581ad6265SDimitry Andric 158681ad6265SDimitry Andric // Need a VALUs pre exec change 158781ad6265SDimitry Andric if (PreExecPos == std::numeric_limits<int>::max()) 158881ad6265SDimitry Andric return NoHazardFound; 158981ad6265SDimitry Andric 159081ad6265SDimitry Andric // Too many VALUs in intv1? 159181ad6265SDimitry Andric int Intv1VALUs = PreExecPos - State.ExecPos; 159281ad6265SDimitry Andric if (Intv1VALUs > Intv1plus2MaxVALUs) 159381ad6265SDimitry Andric return HazardExpired; 159481ad6265SDimitry Andric 159581ad6265SDimitry Andric // Too many VALUs in intv1 + intv2 159681ad6265SDimitry Andric if (Intv1VALUs + Intv2VALUs > Intv1plus2MaxVALUs) 159781ad6265SDimitry Andric return HazardExpired; 159881ad6265SDimitry Andric 159981ad6265SDimitry Andric return HazardFound; 160081ad6265SDimitry Andric }; 160181ad6265SDimitry Andric auto UpdateStateFn = [](StateType &State, const MachineInstr &MI) { 160281ad6265SDimitry Andric if (SIInstrInfo::isVALU(MI)) 160381ad6265SDimitry Andric State.VALUs += 1; 160481ad6265SDimitry Andric }; 160581ad6265SDimitry Andric 160681ad6265SDimitry Andric DenseSet<const MachineBasicBlock *> Visited; 160781ad6265SDimitry Andric if (!hasHazard<StateType>(State, IsHazardFn, UpdateStateFn, MI->getParent(), 160881ad6265SDimitry Andric std::next(MI->getReverseIterator()), Visited)) 160981ad6265SDimitry Andric return false; 161081ad6265SDimitry Andric 161181ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 161281ad6265SDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 161381ad6265SDimitry Andric .addImm(0x0fff); 161481ad6265SDimitry Andric 161581ad6265SDimitry Andric return true; 161681ad6265SDimitry Andric } 161781ad6265SDimitry Andric 161881ad6265SDimitry Andric bool GCNHazardRecognizer::fixVALUTransUseHazard(MachineInstr *MI) { 161981ad6265SDimitry Andric if (!ST.hasVALUTransUseHazard()) 162081ad6265SDimitry Andric return false; 162181ad6265SDimitry Andric if (!SIInstrInfo::isVALU(*MI)) 162281ad6265SDimitry Andric return false; 162381ad6265SDimitry Andric 162481ad6265SDimitry Andric SmallSet<Register, 4> SrcVGPRs; 162581ad6265SDimitry Andric 162681ad6265SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 162781ad6265SDimitry Andric if (Use.isReg() && TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 162881ad6265SDimitry Andric SrcVGPRs.insert(Use.getReg()); 162981ad6265SDimitry Andric } 163081ad6265SDimitry Andric 163181ad6265SDimitry Andric // Look for the following pattern: 163281ad6265SDimitry Andric // Va <- TRANS VALU 163381ad6265SDimitry Andric // intv 163481ad6265SDimitry Andric // MI Va (WaitState = 0) 163581ad6265SDimitry Andric // 163681ad6265SDimitry Andric // Where: 163781ad6265SDimitry Andric // intv <= 5 VALUs / 1 TRANS 163881ad6265SDimitry Andric // 163981ad6265SDimitry Andric // If found, insert an appropriate S_WAITCNT_DEPCTR before MI. 164081ad6265SDimitry Andric 164181ad6265SDimitry Andric const int IntvMaxVALUs = 5; 164281ad6265SDimitry Andric const int IntvMaxTRANS = 1; 164381ad6265SDimitry Andric 164481ad6265SDimitry Andric struct StateType { 164581ad6265SDimitry Andric int VALUs = 0; 164681ad6265SDimitry Andric int TRANS = 0; 164781ad6265SDimitry Andric }; 164881ad6265SDimitry Andric 164981ad6265SDimitry Andric StateType State; 165081ad6265SDimitry Andric 165181ad6265SDimitry Andric // This overloads expiry testing with all the hazard detection 165281ad6265SDimitry Andric auto IsHazardFn = [&, this](StateType &State, const MachineInstr &I) { 165381ad6265SDimitry Andric // Too many VALU states have passed 165481ad6265SDimitry Andric if (State.VALUs > IntvMaxVALUs || State.TRANS > IntvMaxTRANS) 165581ad6265SDimitry Andric return HazardExpired; 165681ad6265SDimitry Andric 165781ad6265SDimitry Andric // Instructions which cause va_vdst==0 expire hazard 165881ad6265SDimitry Andric if (SIInstrInfo::isVMEM(I) || SIInstrInfo::isFLAT(I) || 165981ad6265SDimitry Andric SIInstrInfo::isDS(I) || SIInstrInfo::isEXP(I) || 166081ad6265SDimitry Andric (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 166181ad6265SDimitry Andric I.getOperand(0).getImm() == 0x0fff)) 166281ad6265SDimitry Andric return HazardExpired; 166381ad6265SDimitry Andric 166481ad6265SDimitry Andric // Track registers writes 166581ad6265SDimitry Andric if (SIInstrInfo::isTRANS(I)) { 166681ad6265SDimitry Andric for (Register Src : SrcVGPRs) { 166781ad6265SDimitry Andric if (I.modifiesRegister(Src, &TRI)) { 166881ad6265SDimitry Andric return HazardFound; 166981ad6265SDimitry Andric } 167081ad6265SDimitry Andric } 167181ad6265SDimitry Andric } 167281ad6265SDimitry Andric 167381ad6265SDimitry Andric return NoHazardFound; 167481ad6265SDimitry Andric }; 167581ad6265SDimitry Andric auto UpdateStateFn = [](StateType &State, const MachineInstr &MI) { 167681ad6265SDimitry Andric if (SIInstrInfo::isVALU(MI)) 167781ad6265SDimitry Andric State.VALUs += 1; 167881ad6265SDimitry Andric if (SIInstrInfo::isTRANS(MI)) 167981ad6265SDimitry Andric State.TRANS += 1; 168081ad6265SDimitry Andric }; 168181ad6265SDimitry Andric 168281ad6265SDimitry Andric DenseSet<const MachineBasicBlock *> Visited; 168381ad6265SDimitry Andric if (!hasHazard<StateType>(State, IsHazardFn, UpdateStateFn, MI->getParent(), 168481ad6265SDimitry Andric std::next(MI->getReverseIterator()), Visited)) 168581ad6265SDimitry Andric return false; 168681ad6265SDimitry Andric 168781ad6265SDimitry Andric // Hazard is observed - insert a wait on va_dst counter to ensure hazard is 1688*06c3fb27SDimitry Andric // avoided. 168981ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 169081ad6265SDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 1691*06c3fb27SDimitry Andric .addImm(AMDGPU::DepCtr::encodeFieldVaVdst(0)); 169281ad6265SDimitry Andric 169381ad6265SDimitry Andric return true; 169481ad6265SDimitry Andric } 169581ad6265SDimitry Andric 169681ad6265SDimitry Andric bool GCNHazardRecognizer::fixWMMAHazards(MachineInstr *MI) { 169781ad6265SDimitry Andric if (!SIInstrInfo::isWMMA(*MI)) 169881ad6265SDimitry Andric return false; 169981ad6265SDimitry Andric 170081ad6265SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 170181ad6265SDimitry Andric const SIRegisterInfo *TRI = ST.getRegisterInfo(); 170281ad6265SDimitry Andric 170381ad6265SDimitry Andric auto IsHazardFn = [MI, TII, TRI](const MachineInstr &I) { 170481ad6265SDimitry Andric if (!SIInstrInfo::isWMMA(I)) 170581ad6265SDimitry Andric return false; 170681ad6265SDimitry Andric 170781ad6265SDimitry Andric // Src0 or Src1 of the current wmma instruction overlaps with the dest of 170881ad6265SDimitry Andric // the previous wmma. 170981ad6265SDimitry Andric const Register CurSrc0Reg = 171081ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src0)->getReg(); 171181ad6265SDimitry Andric const Register CurSrc1Reg = 171281ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src1)->getReg(); 171381ad6265SDimitry Andric 171481ad6265SDimitry Andric const Register PrevDstReg = 171581ad6265SDimitry Andric TII->getNamedOperand(I, AMDGPU::OpName::vdst)->getReg(); 171681ad6265SDimitry Andric 171781ad6265SDimitry Andric if (TRI->regsOverlap(PrevDstReg, CurSrc0Reg) || 171881ad6265SDimitry Andric TRI->regsOverlap(PrevDstReg, CurSrc1Reg)) { 171981ad6265SDimitry Andric return true; 172081ad6265SDimitry Andric } 172181ad6265SDimitry Andric 172281ad6265SDimitry Andric // Src2 of the current wmma instruction overlaps with the dest of the 172381ad6265SDimitry Andric // previous wmma. 172481ad6265SDimitry Andric const MachineOperand *Src2 = 172581ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src2); 172681ad6265SDimitry Andric const Register CurSrc2Reg = Src2->isReg() ? Src2->getReg() : Register(); 172781ad6265SDimitry Andric 172881ad6265SDimitry Andric if (CurSrc2Reg != AMDGPU::NoRegister && 172981ad6265SDimitry Andric TRI->regsOverlap(PrevDstReg, CurSrc2Reg)) { 173081ad6265SDimitry Andric 173181ad6265SDimitry Andric const MachineOperand *Src2Mods = 173281ad6265SDimitry Andric TII->getNamedOperand(*MI, AMDGPU::OpName::src2_modifiers); 173381ad6265SDimitry Andric const bool NoSrc2Mods = 173481ad6265SDimitry Andric (Src2Mods->getImm() & (SISrcMods::NEG | SISrcMods::NEG_HI)) == 0; 173581ad6265SDimitry Andric // Exception: there is no hazard if the wmma instructions are of the same 173681ad6265SDimitry Andric // type and there is no input modifier on src2 of the current instruction. 173781ad6265SDimitry Andric return !(NoSrc2Mods && (TII->pseudoToMCOpcode(I.getOpcode()) == 173881ad6265SDimitry Andric TII->pseudoToMCOpcode(MI->getOpcode()))); 173981ad6265SDimitry Andric } 174081ad6265SDimitry Andric 174181ad6265SDimitry Andric return false; 174281ad6265SDimitry Andric }; 174381ad6265SDimitry Andric 174481ad6265SDimitry Andric auto IsExpiredFn = [](const MachineInstr &I, int) { 174581ad6265SDimitry Andric return SIInstrInfo::isVALU(I); 174681ad6265SDimitry Andric }; 174781ad6265SDimitry Andric 174881ad6265SDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 174981ad6265SDimitry Andric std::numeric_limits<int>::max()) 175081ad6265SDimitry Andric return false; 175181ad6265SDimitry Andric 175281ad6265SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII->get(AMDGPU::V_NOP_e32)); 175381ad6265SDimitry Andric 175481ad6265SDimitry Andric return true; 175581ad6265SDimitry Andric } 175681ad6265SDimitry Andric 1757bdd1243dSDimitry Andric bool GCNHazardRecognizer::fixShift64HighRegBug(MachineInstr *MI) { 1758bdd1243dSDimitry Andric if (!ST.hasShift64HighRegBug()) 1759bdd1243dSDimitry Andric return false; 1760bdd1243dSDimitry Andric 1761bdd1243dSDimitry Andric switch (MI->getOpcode()) { 1762bdd1243dSDimitry Andric default: 1763bdd1243dSDimitry Andric return false; 1764bdd1243dSDimitry Andric case AMDGPU::V_LSHLREV_B64_e64: 1765bdd1243dSDimitry Andric case AMDGPU::V_LSHRREV_B64_e64: 1766bdd1243dSDimitry Andric case AMDGPU::V_ASHRREV_I64_e64: 1767bdd1243dSDimitry Andric break; 1768bdd1243dSDimitry Andric } 1769bdd1243dSDimitry Andric 1770bdd1243dSDimitry Andric MachineOperand *Amt = TII.getNamedOperand(*MI, AMDGPU::OpName::src0); 1771bdd1243dSDimitry Andric if (!Amt->isReg()) 1772bdd1243dSDimitry Andric return false; 1773bdd1243dSDimitry Andric 1774bdd1243dSDimitry Andric Register AmtReg = Amt->getReg(); 1775bdd1243dSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 1776bdd1243dSDimitry Andric // Check if this is a last VGPR in the allocation block. 1777bdd1243dSDimitry Andric if (!TRI.isVGPR(MRI, AmtReg) || ((AmtReg - AMDGPU::VGPR0) & 7) != 7) 1778bdd1243dSDimitry Andric return false; 1779bdd1243dSDimitry Andric 1780bdd1243dSDimitry Andric if (AmtReg != AMDGPU::VGPR255 && MRI.isPhysRegUsed(AmtReg + 1)) 1781bdd1243dSDimitry Andric return false; 1782bdd1243dSDimitry Andric 1783bdd1243dSDimitry Andric MachineOperand *Src1 = TII.getNamedOperand(*MI, AMDGPU::OpName::src1); 1784bdd1243dSDimitry Andric bool OverlappedSrc = Src1->isReg() && TRI.regsOverlap(Src1->getReg(), AmtReg); 1785bdd1243dSDimitry Andric bool OverlappedDst = MI->modifiesRegister(AmtReg, &TRI); 1786bdd1243dSDimitry Andric bool Overlapped = OverlappedSrc || OverlappedDst; 1787bdd1243dSDimitry Andric 1788bdd1243dSDimitry Andric assert(!OverlappedDst || !OverlappedSrc || 1789bdd1243dSDimitry Andric Src1->getReg() == MI->getOperand(0).getReg()); 1790bdd1243dSDimitry Andric assert(ST.needsAlignedVGPRs()); 1791bdd1243dSDimitry Andric static_assert(AMDGPU::VGPR0 + 1 == AMDGPU::VGPR1); 1792bdd1243dSDimitry Andric 1793bdd1243dSDimitry Andric Register NewReg; 1794bdd1243dSDimitry Andric for (MCRegister Reg : Overlapped ? AMDGPU::VReg_64_Align2RegClass 1795bdd1243dSDimitry Andric : AMDGPU::VGPR_32RegClass) { 1796bdd1243dSDimitry Andric if (!MI->modifiesRegister(Reg, &TRI) && !MI->readsRegister(Reg, &TRI)) { 1797bdd1243dSDimitry Andric NewReg = Reg; 1798bdd1243dSDimitry Andric break; 1799bdd1243dSDimitry Andric } 1800bdd1243dSDimitry Andric } 1801bdd1243dSDimitry Andric 1802bdd1243dSDimitry Andric Register NewAmt = Overlapped ? (Register)TRI.getSubReg(NewReg, AMDGPU::sub1) 1803bdd1243dSDimitry Andric : NewReg; 1804bdd1243dSDimitry Andric Register NewAmtLo; 1805bdd1243dSDimitry Andric 1806bdd1243dSDimitry Andric if (Overlapped) 1807bdd1243dSDimitry Andric NewAmtLo = TRI.getSubReg(NewReg, AMDGPU::sub0); 1808bdd1243dSDimitry Andric 1809bdd1243dSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 1810bdd1243dSDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 1811bdd1243dSDimitry Andric // Insert a full wait count because found register might be pending a wait. 1812bdd1243dSDimitry Andric BuildMI(*MBB, MI, DL, TII.get(AMDGPU::S_WAITCNT)) 1813bdd1243dSDimitry Andric .addImm(0); 1814bdd1243dSDimitry Andric 1815bdd1243dSDimitry Andric // Insert V_SWAP_B32 instruction(s) and run hazard recognizer on them. 1816bdd1243dSDimitry Andric if (Overlapped) 1817bdd1243dSDimitry Andric runOnInstruction( 1818bdd1243dSDimitry Andric BuildMI(*MBB, MI, DL, TII.get(AMDGPU::V_SWAP_B32), NewAmtLo) 1819bdd1243dSDimitry Andric .addDef(AmtReg - 1) 1820bdd1243dSDimitry Andric .addReg(AmtReg - 1, RegState::Undef) 1821bdd1243dSDimitry Andric .addReg(NewAmtLo, RegState::Undef)); 1822bdd1243dSDimitry Andric runOnInstruction(BuildMI(*MBB, MI, DL, TII.get(AMDGPU::V_SWAP_B32), NewAmt) 1823bdd1243dSDimitry Andric .addDef(AmtReg) 1824bdd1243dSDimitry Andric .addReg(AmtReg, RegState::Undef) 1825bdd1243dSDimitry Andric .addReg(NewAmt, RegState::Undef)); 1826bdd1243dSDimitry Andric 1827bdd1243dSDimitry Andric // Instructions emitted after the current instruction will be processed by the 1828bdd1243dSDimitry Andric // parent loop of the hazard recognizer in a natural way. 1829bdd1243dSDimitry Andric BuildMI(*MBB, std::next(MI->getIterator()), DL, TII.get(AMDGPU::V_SWAP_B32), 1830bdd1243dSDimitry Andric AmtReg) 1831bdd1243dSDimitry Andric .addDef(NewAmt) 1832bdd1243dSDimitry Andric .addReg(NewAmt) 1833bdd1243dSDimitry Andric .addReg(AmtReg); 1834bdd1243dSDimitry Andric if (Overlapped) 1835bdd1243dSDimitry Andric BuildMI(*MBB, std::next(MI->getIterator()), DL, TII.get(AMDGPU::V_SWAP_B32), 1836bdd1243dSDimitry Andric AmtReg - 1) 1837bdd1243dSDimitry Andric .addDef(NewAmtLo) 1838bdd1243dSDimitry Andric .addReg(NewAmtLo) 1839bdd1243dSDimitry Andric .addReg(AmtReg - 1); 1840bdd1243dSDimitry Andric 1841bdd1243dSDimitry Andric // Re-running hazard recognizer on the modified instruction is not necessary, 1842bdd1243dSDimitry Andric // inserted V_SWAP_B32 has already both read and write new registers so 1843bdd1243dSDimitry Andric // hazards related to these register has already been handled. 1844bdd1243dSDimitry Andric Amt->setReg(NewAmt); 1845bdd1243dSDimitry Andric Amt->setIsKill(false); 1846bdd1243dSDimitry Andric // We do not update liveness, so verifier may see it as undef. 1847bdd1243dSDimitry Andric Amt->setIsUndef(); 1848bdd1243dSDimitry Andric if (OverlappedDst) 1849bdd1243dSDimitry Andric MI->getOperand(0).setReg(NewReg); 1850bdd1243dSDimitry Andric if (OverlappedSrc) { 1851bdd1243dSDimitry Andric Src1->setReg(NewReg); 1852bdd1243dSDimitry Andric Src1->setIsKill(false); 1853bdd1243dSDimitry Andric Src1->setIsUndef(); 1854bdd1243dSDimitry Andric } 1855bdd1243dSDimitry Andric 1856bdd1243dSDimitry Andric return true; 1857bdd1243dSDimitry Andric } 1858bdd1243dSDimitry Andric 18590b57cec5SDimitry Andric int GCNHazardRecognizer::checkNSAtoVMEMHazard(MachineInstr *MI) { 18600b57cec5SDimitry Andric int NSAtoVMEMWaitStates = 1; 18610b57cec5SDimitry Andric 18620b57cec5SDimitry Andric if (!ST.hasNSAtoVMEMBug()) 18630b57cec5SDimitry Andric return 0; 18640b57cec5SDimitry Andric 18650b57cec5SDimitry Andric if (!SIInstrInfo::isMUBUF(*MI) && !SIInstrInfo::isMTBUF(*MI)) 18660b57cec5SDimitry Andric return 0; 18670b57cec5SDimitry Andric 18680b57cec5SDimitry Andric const SIInstrInfo *TII = ST.getInstrInfo(); 18690b57cec5SDimitry Andric const auto *Offset = TII->getNamedOperand(*MI, AMDGPU::OpName::offset); 18700b57cec5SDimitry Andric if (!Offset || (Offset->getImm() & 6) == 0) 18710b57cec5SDimitry Andric return 0; 18720b57cec5SDimitry Andric 1873fe6060f1SDimitry Andric auto IsHazardFn = [TII](const MachineInstr &I) { 1874fe6060f1SDimitry Andric if (!SIInstrInfo::isMIMG(I)) 18750b57cec5SDimitry Andric return false; 1876fe6060f1SDimitry Andric const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(I.getOpcode()); 18770b57cec5SDimitry Andric return Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA && 1878fe6060f1SDimitry Andric TII->getInstSizeInBytes(I) >= 16; 18790b57cec5SDimitry Andric }; 18800b57cec5SDimitry Andric 18810b57cec5SDimitry Andric return NSAtoVMEMWaitStates - getWaitStatesSince(IsHazardFn, 1); 18820b57cec5SDimitry Andric } 18830b57cec5SDimitry Andric 18840b57cec5SDimitry Andric int GCNHazardRecognizer::checkFPAtomicToDenormModeHazard(MachineInstr *MI) { 18850b57cec5SDimitry Andric int FPAtomicToDenormModeWaitStates = 3; 18860b57cec5SDimitry Andric 1887bdd1243dSDimitry Andric if (!ST.hasFPAtomicToDenormModeHazard()) 1888bdd1243dSDimitry Andric return 0; 1889bdd1243dSDimitry Andric 18900b57cec5SDimitry Andric if (MI->getOpcode() != AMDGPU::S_DENORM_MODE) 18910b57cec5SDimitry Andric return 0; 18920b57cec5SDimitry Andric 1893fe6060f1SDimitry Andric auto IsHazardFn = [](const MachineInstr &I) { 1894fe6060f1SDimitry Andric if (!SIInstrInfo::isVMEM(I) && !SIInstrInfo::isFLAT(I)) 18950b57cec5SDimitry Andric return false; 1896fe6060f1SDimitry Andric return SIInstrInfo::isFPAtomic(I); 18970b57cec5SDimitry Andric }; 18980b57cec5SDimitry Andric 1899fe6060f1SDimitry Andric auto IsExpiredFn = [](const MachineInstr &MI, int WaitStates) { 1900fe6060f1SDimitry Andric if (WaitStates >= 3 || SIInstrInfo::isVALU(MI)) 19010b57cec5SDimitry Andric return true; 19020b57cec5SDimitry Andric 1903fe6060f1SDimitry Andric switch (MI.getOpcode()) { 19040b57cec5SDimitry Andric case AMDGPU::S_WAITCNT: 19050b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VSCNT: 19060b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_VMCNT: 19070b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_EXPCNT: 19080b57cec5SDimitry Andric case AMDGPU::S_WAITCNT_LGKMCNT: 1909e8d8bef9SDimitry Andric case AMDGPU::S_WAIT_IDLE: 19100b57cec5SDimitry Andric return true; 19110b57cec5SDimitry Andric default: 19120b57cec5SDimitry Andric break; 19130b57cec5SDimitry Andric } 19140b57cec5SDimitry Andric 19150b57cec5SDimitry Andric return false; 19160b57cec5SDimitry Andric }; 19170b57cec5SDimitry Andric 19180b57cec5SDimitry Andric return FPAtomicToDenormModeWaitStates - 19190b57cec5SDimitry Andric ::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn); 19200b57cec5SDimitry Andric } 19210b57cec5SDimitry Andric 19220b57cec5SDimitry Andric int GCNHazardRecognizer::checkMAIHazards(MachineInstr *MI) { 19230b57cec5SDimitry Andric assert(SIInstrInfo::isMAI(*MI)); 19240b57cec5SDimitry Andric 1925fe6060f1SDimitry Andric return ST.hasGFX90AInsts() ? checkMAIHazards90A(MI) : checkMAIHazards908(MI); 1926fe6060f1SDimitry Andric } 1927fe6060f1SDimitry Andric 192881ad6265SDimitry Andric int GCNHazardRecognizer::checkMFMAPadding(MachineInstr *MI) { 192981ad6265SDimitry Andric // Early exit if no padding is requested. 193081ad6265SDimitry Andric if (MFMAPaddingRatio == 0) 193181ad6265SDimitry Andric return 0; 193281ad6265SDimitry Andric 193381ad6265SDimitry Andric const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 193481ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(*MI) || MFI->getOccupancy() < 2) 193581ad6265SDimitry Andric return 0; 193681ad6265SDimitry Andric 193781ad6265SDimitry Andric int NeighborMFMALatency = 0; 193881ad6265SDimitry Andric auto IsNeighboringMFMA = [&NeighborMFMALatency, 193981ad6265SDimitry Andric this](const MachineInstr &MI) { 194081ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 194181ad6265SDimitry Andric return false; 194281ad6265SDimitry Andric 194381ad6265SDimitry Andric NeighborMFMALatency = this->getMFMAPipelineWaitStates(MI); 194481ad6265SDimitry Andric return true; 194581ad6265SDimitry Andric }; 194681ad6265SDimitry Andric 194781ad6265SDimitry Andric const int MaxMFMAPipelineWaitStates = 16; 194881ad6265SDimitry Andric int WaitStatesSinceNeighborMFMA = 194981ad6265SDimitry Andric getWaitStatesSince(IsNeighboringMFMA, MaxMFMAPipelineWaitStates); 195081ad6265SDimitry Andric 195181ad6265SDimitry Andric int NeighborMFMAPaddingNeeded = 195281ad6265SDimitry Andric (NeighborMFMALatency * MFMAPaddingRatio / 100) - 195381ad6265SDimitry Andric WaitStatesSinceNeighborMFMA; 195481ad6265SDimitry Andric 195581ad6265SDimitry Andric return std::max(0, NeighborMFMAPaddingNeeded); 195681ad6265SDimitry Andric } 195781ad6265SDimitry Andric 1958fe6060f1SDimitry Andric int GCNHazardRecognizer::checkMAIHazards908(MachineInstr *MI) { 19590b57cec5SDimitry Andric int WaitStatesNeeded = 0; 19600b57cec5SDimitry Andric unsigned Opc = MI->getOpcode(); 19610b57cec5SDimitry Andric 1962fe6060f1SDimitry Andric auto IsVALUFn = [](const MachineInstr &MI) { 1963bdd1243dSDimitry Andric return SIInstrInfo::isVALU(MI) || MI.isInlineAsm(); 19640b57cec5SDimitry Andric }; 19650b57cec5SDimitry Andric 1966e8d8bef9SDimitry Andric if (Opc != AMDGPU::V_ACCVGPR_READ_B32_e64) { // MFMA or v_accvgpr_write 19670b57cec5SDimitry Andric const int LegacyVALUWritesVGPRWaitStates = 2; 19680b57cec5SDimitry Andric const int VALUWritesExecWaitStates = 4; 19690b57cec5SDimitry Andric const int MaxWaitStates = 4; 19700b57cec5SDimitry Andric 19710b57cec5SDimitry Andric int WaitStatesNeededForUse = VALUWritesExecWaitStates - 19720b57cec5SDimitry Andric getWaitStatesSinceDef(AMDGPU::EXEC, IsVALUFn, MaxWaitStates); 19730b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 19740b57cec5SDimitry Andric 19750b57cec5SDimitry Andric if (WaitStatesNeeded < MaxWaitStates) { 19760b57cec5SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 19770b57cec5SDimitry Andric const int MaxWaitStates = 2; 19780b57cec5SDimitry Andric 19790b57cec5SDimitry Andric if (!Use.isReg() || !TRI.isVGPR(MF.getRegInfo(), Use.getReg())) 19800b57cec5SDimitry Andric continue; 19810b57cec5SDimitry Andric 19820b57cec5SDimitry Andric int WaitStatesNeededForUse = LegacyVALUWritesVGPRWaitStates - 19830b57cec5SDimitry Andric getWaitStatesSinceDef(Use.getReg(), IsVALUFn, MaxWaitStates); 19840b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 19850b57cec5SDimitry Andric 19860b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 19870b57cec5SDimitry Andric break; 19880b57cec5SDimitry Andric } 19890b57cec5SDimitry Andric } 19900b57cec5SDimitry Andric } 19910b57cec5SDimitry Andric 19920b57cec5SDimitry Andric for (const MachineOperand &Op : MI->explicit_operands()) { 19930b57cec5SDimitry Andric if (!Op.isReg() || !TRI.isAGPR(MF.getRegInfo(), Op.getReg())) 19940b57cec5SDimitry Andric continue; 19950b57cec5SDimitry Andric 1996e8d8bef9SDimitry Andric if (Op.isDef() && Opc != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 19970b57cec5SDimitry Andric continue; 19980b57cec5SDimitry Andric 19990b57cec5SDimitry Andric const int MFMAWritesAGPROverlappedSrcABWaitStates = 4; 20000b57cec5SDimitry Andric const int MFMAWritesAGPROverlappedSrcCWaitStates = 2; 20010b57cec5SDimitry Andric const int MFMA4x4WritesAGPRAccVgprReadWaitStates = 4; 20020b57cec5SDimitry Andric const int MFMA16x16WritesAGPRAccVgprReadWaitStates = 10; 20030b57cec5SDimitry Andric const int MFMA32x32WritesAGPRAccVgprReadWaitStates = 18; 20040b57cec5SDimitry Andric const int MFMA4x4WritesAGPRAccVgprWriteWaitStates = 1; 20050b57cec5SDimitry Andric const int MFMA16x16WritesAGPRAccVgprWriteWaitStates = 7; 20060b57cec5SDimitry Andric const int MFMA32x32WritesAGPRAccVgprWriteWaitStates = 15; 20070b57cec5SDimitry Andric const int MaxWaitStates = 18; 20088bcb0991SDimitry Andric Register Reg = Op.getReg(); 20090b57cec5SDimitry Andric unsigned HazardDefLatency = 0; 20100b57cec5SDimitry Andric 201181ad6265SDimitry Andric auto IsOverlappedMFMAFn = [Reg, &HazardDefLatency, 2012fe6060f1SDimitry Andric this](const MachineInstr &MI) { 201381ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 20140b57cec5SDimitry Andric return false; 2015fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 20160b57cec5SDimitry Andric if (DstReg == Reg) 20170b57cec5SDimitry Andric return false; 2018fe6060f1SDimitry Andric HazardDefLatency = 2019fe6060f1SDimitry Andric std::max(HazardDefLatency, TSchedModel.computeInstrLatency(&MI)); 20200b57cec5SDimitry Andric return TRI.regsOverlap(DstReg, Reg); 20210b57cec5SDimitry Andric }; 20220b57cec5SDimitry Andric 20230b57cec5SDimitry Andric int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsOverlappedMFMAFn, 20240b57cec5SDimitry Andric MaxWaitStates); 20250b57cec5SDimitry Andric int NeedWaitStates = MFMAWritesAGPROverlappedSrcABWaitStates; 20260b57cec5SDimitry Andric int SrcCIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 2027*06c3fb27SDimitry Andric int OpNo = Op.getOperandNo(); 20280b57cec5SDimitry Andric if (OpNo == SrcCIdx) { 20290b57cec5SDimitry Andric NeedWaitStates = MFMAWritesAGPROverlappedSrcCWaitStates; 2030e8d8bef9SDimitry Andric } else if (Opc == AMDGPU::V_ACCVGPR_READ_B32_e64) { 20310b57cec5SDimitry Andric switch (HazardDefLatency) { 20320b57cec5SDimitry Andric case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprReadWaitStates; 20330b57cec5SDimitry Andric break; 20340b57cec5SDimitry Andric case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprReadWaitStates; 20350b57cec5SDimitry Andric break; 2036bdd1243dSDimitry Andric case 16: [[fallthrough]]; 20370b57cec5SDimitry Andric default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprReadWaitStates; 20380b57cec5SDimitry Andric break; 20390b57cec5SDimitry Andric } 2040e8d8bef9SDimitry Andric } else if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64) { 20410b57cec5SDimitry Andric switch (HazardDefLatency) { 20420b57cec5SDimitry Andric case 2: NeedWaitStates = MFMA4x4WritesAGPRAccVgprWriteWaitStates; 20430b57cec5SDimitry Andric break; 20440b57cec5SDimitry Andric case 8: NeedWaitStates = MFMA16x16WritesAGPRAccVgprWriteWaitStates; 20450b57cec5SDimitry Andric break; 2046bdd1243dSDimitry Andric case 16: [[fallthrough]]; 20470b57cec5SDimitry Andric default: NeedWaitStates = MFMA32x32WritesAGPRAccVgprWriteWaitStates; 20480b57cec5SDimitry Andric break; 20490b57cec5SDimitry Andric } 20500b57cec5SDimitry Andric } 20510b57cec5SDimitry Andric 20520b57cec5SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 20530b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 20540b57cec5SDimitry Andric 20550b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 20560b57cec5SDimitry Andric return WaitStatesNeeded; // Early exit. 20570b57cec5SDimitry Andric 2058fe6060f1SDimitry Andric auto IsAccVgprWriteFn = [Reg, this](const MachineInstr &MI) { 2059fe6060f1SDimitry Andric if (MI.getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 20600b57cec5SDimitry Andric return false; 2061fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 20620b57cec5SDimitry Andric return TRI.regsOverlap(Reg, DstReg); 20630b57cec5SDimitry Andric }; 20640b57cec5SDimitry Andric 20650b57cec5SDimitry Andric const int AccVGPRWriteMFMAReadSrcCWaitStates = 1; 20660b57cec5SDimitry Andric const int AccVGPRWriteMFMAReadSrcABWaitStates = 3; 20670b57cec5SDimitry Andric const int AccVGPRWriteAccVgprReadWaitStates = 3; 20680b57cec5SDimitry Andric NeedWaitStates = AccVGPRWriteMFMAReadSrcABWaitStates; 20690b57cec5SDimitry Andric if (OpNo == SrcCIdx) 20700b57cec5SDimitry Andric NeedWaitStates = AccVGPRWriteMFMAReadSrcCWaitStates; 2071e8d8bef9SDimitry Andric else if (Opc == AMDGPU::V_ACCVGPR_READ_B32_e64) 20720b57cec5SDimitry Andric NeedWaitStates = AccVGPRWriteAccVgprReadWaitStates; 20730b57cec5SDimitry Andric 20740b57cec5SDimitry Andric WaitStatesNeededForUse = NeedWaitStates - 20750b57cec5SDimitry Andric getWaitStatesSinceDef(Reg, IsAccVgprWriteFn, MaxWaitStates); 20760b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 20770b57cec5SDimitry Andric 20780b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 20790b57cec5SDimitry Andric return WaitStatesNeeded; // Early exit. 20800b57cec5SDimitry Andric } 20810b57cec5SDimitry Andric 2082e8d8bef9SDimitry Andric if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64) { 20830b57cec5SDimitry Andric const int MFMA4x4ReadSrcCAccVgprWriteWaitStates = 0; 20840b57cec5SDimitry Andric const int MFMA16x16ReadSrcCAccVgprWriteWaitStates = 5; 20850b57cec5SDimitry Andric const int MFMA32x32ReadSrcCAccVgprWriteWaitStates = 13; 20860b57cec5SDimitry Andric const int MaxWaitStates = 13; 20878bcb0991SDimitry Andric Register DstReg = MI->getOperand(0).getReg(); 20880b57cec5SDimitry Andric unsigned HazardDefLatency = 0; 20890b57cec5SDimitry Andric 209081ad6265SDimitry Andric auto IsSrcCMFMAFn = [DstReg, &HazardDefLatency, 2091fe6060f1SDimitry Andric this](const MachineInstr &MI) { 209281ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 20930b57cec5SDimitry Andric return false; 2094fe6060f1SDimitry Andric Register Reg = TII.getNamedOperand(MI, AMDGPU::OpName::src2)->getReg(); 2095fe6060f1SDimitry Andric HazardDefLatency = 2096fe6060f1SDimitry Andric std::max(HazardDefLatency, TSchedModel.computeInstrLatency(&MI)); 20970b57cec5SDimitry Andric return TRI.regsOverlap(Reg, DstReg); 20980b57cec5SDimitry Andric }; 20990b57cec5SDimitry Andric 21000b57cec5SDimitry Andric int WaitStatesSince = getWaitStatesSince(IsSrcCMFMAFn, MaxWaitStates); 21010b57cec5SDimitry Andric int NeedWaitStates; 21020b57cec5SDimitry Andric switch (HazardDefLatency) { 21030b57cec5SDimitry Andric case 2: NeedWaitStates = MFMA4x4ReadSrcCAccVgprWriteWaitStates; 21040b57cec5SDimitry Andric break; 21050b57cec5SDimitry Andric case 8: NeedWaitStates = MFMA16x16ReadSrcCAccVgprWriteWaitStates; 21060b57cec5SDimitry Andric break; 2107bdd1243dSDimitry Andric case 16: [[fallthrough]]; 21080b57cec5SDimitry Andric default: NeedWaitStates = MFMA32x32ReadSrcCAccVgprWriteWaitStates; 21090b57cec5SDimitry Andric break; 21100b57cec5SDimitry Andric } 21110b57cec5SDimitry Andric 21120b57cec5SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSince; 21130b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 21140b57cec5SDimitry Andric } 21150b57cec5SDimitry Andric 211681ad6265SDimitry Andric // Pad neighboring MFMA with noops for better inter-wave performance. 211781ad6265SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, checkMFMAPadding(MI)); 211881ad6265SDimitry Andric 21190b57cec5SDimitry Andric return WaitStatesNeeded; 21200b57cec5SDimitry Andric } 21210b57cec5SDimitry Andric 2122fe6060f1SDimitry Andric int GCNHazardRecognizer::checkMAIHazards90A(MachineInstr *MI) { 2123fe6060f1SDimitry Andric int WaitStatesNeeded = 0; 2124fe6060f1SDimitry Andric unsigned Opc = MI->getOpcode(); 2125fe6060f1SDimitry Andric 212681ad6265SDimitry Andric auto IsLegacyVALUFn = [](const MachineInstr &MI) { 212781ad6265SDimitry Andric return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMFMA(MI); 2128fe6060f1SDimitry Andric }; 2129fe6060f1SDimitry Andric 213081ad6265SDimitry Andric auto IsLegacyVALUNotDotFn = [](const MachineInstr &MI) { 213181ad6265SDimitry Andric return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMFMA(MI) && 213281ad6265SDimitry Andric !SIInstrInfo::isDOT(MI); 2133fe6060f1SDimitry Andric }; 2134fe6060f1SDimitry Andric 213581ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(*MI)) 2136fe6060f1SDimitry Andric return WaitStatesNeeded; 2137fe6060f1SDimitry Andric 2138fe6060f1SDimitry Andric const int VALUWritesExecWaitStates = 4; 2139fe6060f1SDimitry Andric int WaitStatesNeededForUse = VALUWritesExecWaitStates - 2140fe6060f1SDimitry Andric getWaitStatesSinceDef(AMDGPU::EXEC, IsLegacyVALUFn, 2141fe6060f1SDimitry Andric VALUWritesExecWaitStates); 2142fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2143fe6060f1SDimitry Andric 2144fe6060f1SDimitry Andric int SrcCIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 2145fe6060f1SDimitry Andric 2146fe6060f1SDimitry Andric // Loop for both DGEMM and S/HGEMM 2nd instruction. 2147fe6060f1SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 2148fe6060f1SDimitry Andric const int LegacyVALUNotDotWritesVGPRWaitStates = 2; 2149fe6060f1SDimitry Andric const int SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates = 2; 215081ad6265SDimitry Andric const int GFX940_XDL2PassWritesVGPROverlappedSMFMASrcCWaitStates = 3; 215181ad6265SDimitry Andric const int GFX940_XDL4PassWritesVGPROverlappedSMFMASrcCWaitStates = 5; 215281ad6265SDimitry Andric const int GFX940_SMFMA4PassWritesVGPROverlappedSMFMASrcCWaitStates = 4; 215381ad6265SDimitry Andric const int GFX940_XDL8PassWritesVGPROverlappedSMFMASrcCWaitStates = 9; 215481ad6265SDimitry Andric const int GFX940_SMFMA8PassWritesVGPROverlappedSMFMASrcCWaitStates = 8; 215581ad6265SDimitry Andric const int GFX940_XDL16PassWritesVGPROverlappedSMFMASrcCWaitStates = 17; 215681ad6265SDimitry Andric const int GFX940_SMFMA16PassWritesVGPROverlappedSMFMASrcCWaitStates = 16; 2157fe6060f1SDimitry Andric const int SMFMA16x16WritesVGPROverlappedSMFMASrcCWaitStates = 8; 2158fe6060f1SDimitry Andric const int SMFMA32x32WritesVGPROverlappedSMFMASrcCWaitStates = 16; 2159fe6060f1SDimitry Andric const int SMFMA4x4WritesVGPROverlappedDMFMASrcCWaitStates = 3; 2160fe6060f1SDimitry Andric const int SMFMA16x16WritesVGPROverlappedDMFMASrcCWaitStates = 9; 2161fe6060f1SDimitry Andric const int SMFMA32x32WritesVGPROverlappedDMFMASrcCWaitStates = 17; 2162fe6060f1SDimitry Andric const int DMFMA16x16WritesVGPROverlappedSrcCWaitStates = 9; 2163fe6060f1SDimitry Andric const int DMFMA4x4WritesVGPROverlappedSrcCWaitStates = 4; 2164fe6060f1SDimitry Andric const int SMFMA4x4WritesVGPROverlappedSrcABWaitStates = 5; 2165fe6060f1SDimitry Andric const int SMFMA16x16WritesVGPROverlappedSrcABWaitStates = 11; 2166fe6060f1SDimitry Andric const int SMFMA32x32WritesVGPROverlappedSrcABWaitStates = 19; 216781ad6265SDimitry Andric const int GFX940_SMFMA2PassWritesVGPROverlappedSrcABWaitStates = 4; 216881ad6265SDimitry Andric const int GFX940_SMFMA4PassWritesVGPROverlappedSrcABWaitStates = 6; 216981ad6265SDimitry Andric const int GFX940_SMFMA8PassWritesVGPROverlappedSrcABWaitStates = 10; 217081ad6265SDimitry Andric const int GFX940_SMFMA16PassWritesVGPROverlappedSrcABWaitStates = 18; 217181ad6265SDimitry Andric const int GFX940_XDL2PassWritesVGPROverlappedSrcABWaitStates = 5; 217281ad6265SDimitry Andric const int GFX940_XDL4PassWritesVGPROverlappedSrcABWaitStates = 7; 217381ad6265SDimitry Andric const int GFX940_XDL8PassWritesVGPROverlappedSrcABWaitStates = 11; 217481ad6265SDimitry Andric const int GFX940_XDL16PassWritesVGPROverlappedSrcABWaitStates = 19; 2175fe6060f1SDimitry Andric const int DMFMA4x4WritesVGPROverlappedMFMASrcABWaitStates = 6; 2176fe6060f1SDimitry Andric const int DMFMA16x16WritesVGPROverlappedMFMASrcABWaitStates = 11; 2177fe6060f1SDimitry Andric const int DMFMA4x4WritesVGPRFullSrcCWaitStates = 4; 217881ad6265SDimitry Andric const int GFX940_SMFMA4x4WritesVGPRFullSrcCWaitStates = 2; 2179fe6060f1SDimitry Andric const int MaxWaitStates = 19; 2180fe6060f1SDimitry Andric 2181fe6060f1SDimitry Andric if (!Use.isReg()) 2182fe6060f1SDimitry Andric continue; 218304eeddc0SDimitry Andric Register Reg = Use.getReg(); 2184fe6060f1SDimitry Andric bool FullReg; 2185fe6060f1SDimitry Andric const MachineInstr *MI1; 2186fe6060f1SDimitry Andric 218781ad6265SDimitry Andric auto IsOverlappedMFMAFn = [Reg, &FullReg, &MI1, 2188fe6060f1SDimitry Andric this](const MachineInstr &MI) { 218981ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI)) 2190fe6060f1SDimitry Andric return false; 2191fe6060f1SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 2192fe6060f1SDimitry Andric FullReg = (DstReg == Reg); 2193fe6060f1SDimitry Andric MI1 = &MI; 2194fe6060f1SDimitry Andric return TRI.regsOverlap(DstReg, Reg); 2195fe6060f1SDimitry Andric }; 2196fe6060f1SDimitry Andric 2197fe6060f1SDimitry Andric WaitStatesNeededForUse = LegacyVALUNotDotWritesVGPRWaitStates - 2198fe6060f1SDimitry Andric getWaitStatesSinceDef(Reg, IsLegacyVALUNotDotFn, MaxWaitStates); 2199fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2200fe6060f1SDimitry Andric 22014824e7fdSDimitry Andric int NumWaitStates = 22024824e7fdSDimitry Andric getWaitStatesSinceDef(Reg, IsOverlappedMFMAFn, MaxWaitStates); 2203fe6060f1SDimitry Andric if (NumWaitStates == std::numeric_limits<int>::max()) 2204fe6060f1SDimitry Andric continue; 2205fe6060f1SDimitry Andric 2206*06c3fb27SDimitry Andric int OpNo = Use.getOperandNo(); 2207fe6060f1SDimitry Andric unsigned Opc1 = MI1->getOpcode(); 2208fe6060f1SDimitry Andric int NeedWaitStates = 0; 2209fe6060f1SDimitry Andric if (OpNo == SrcCIdx) { 221081ad6265SDimitry Andric if (!isDGEMM(Opc) && (!ST.hasGFX940Insts() && isDGEMM(Opc1))) { 2211fe6060f1SDimitry Andric NeedWaitStates = 0; 2212fe6060f1SDimitry Andric } else if (FullReg) { 2213fe6060f1SDimitry Andric if ((Opc == AMDGPU::V_MFMA_F64_4X4X4F64_e64 || 2214fe6060f1SDimitry Andric Opc == AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64) && 2215fe6060f1SDimitry Andric (Opc1 == AMDGPU::V_MFMA_F64_4X4X4F64_e64 || 2216fe6060f1SDimitry Andric Opc1 == AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64)) 2217fe6060f1SDimitry Andric NeedWaitStates = DMFMA4x4WritesVGPRFullSrcCWaitStates; 221881ad6265SDimitry Andric else if (ST.hasGFX940Insts() && 221981ad6265SDimitry Andric TSchedModel.computeInstrLatency(MI1) == 2) 222081ad6265SDimitry Andric NeedWaitStates = GFX940_SMFMA4x4WritesVGPRFullSrcCWaitStates; 2221fe6060f1SDimitry Andric } else { 2222fe6060f1SDimitry Andric switch (Opc1) { 2223fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_e64: 2224fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_vgprcd_e64: 222504eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_e64: 222604eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_vgprcd_e64: 2227fe6060f1SDimitry Andric if (!isXDL(ST, *MI)) 2228fe6060f1SDimitry Andric NeedWaitStates = DMFMA16x16WritesVGPROverlappedSrcCWaitStates; 2229fe6060f1SDimitry Andric break; 2230fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_e64: 2231fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64: 2232fe6060f1SDimitry Andric if (!isXDL(ST, *MI)) 2233fe6060f1SDimitry Andric NeedWaitStates = DMFMA4x4WritesVGPROverlappedSrcCWaitStates; 2234fe6060f1SDimitry Andric break; 2235fe6060f1SDimitry Andric default: 223681ad6265SDimitry Andric if (ST.hasGFX940Insts() && isXDL(ST, *MI) && !isXDL(ST, *MI1)) 223781ad6265SDimitry Andric break; 2238fe6060f1SDimitry Andric switch (TSchedModel.computeInstrLatency(MI1)) { 2239fe6060f1SDimitry Andric case 2: 224081ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 224181ad6265SDimitry Andric ? isXDL(ST, *MI1) 224281ad6265SDimitry Andric ? GFX940_XDL2PassWritesVGPROverlappedSMFMASrcCWaitStates 224381ad6265SDimitry Andric : SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates 224481ad6265SDimitry Andric : isDGEMM(Opc) 2245fe6060f1SDimitry Andric ? SMFMA4x4WritesVGPROverlappedDMFMASrcCWaitStates 2246fe6060f1SDimitry Andric : SMFMA4x4WritesVGPROverlappedSMFMASrcCWaitStates; 2247fe6060f1SDimitry Andric break; 224881ad6265SDimitry Andric case 4: 224981ad6265SDimitry Andric assert(ST.hasGFX940Insts()); 225081ad6265SDimitry Andric NeedWaitStates = isXDL(ST, *MI1) 225181ad6265SDimitry Andric ? GFX940_XDL4PassWritesVGPROverlappedSMFMASrcCWaitStates 225281ad6265SDimitry Andric : GFX940_SMFMA4PassWritesVGPROverlappedSMFMASrcCWaitStates; 225381ad6265SDimitry Andric break; 2254fe6060f1SDimitry Andric case 8: 225581ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 225681ad6265SDimitry Andric ? isXDL(ST, *MI1) 225781ad6265SDimitry Andric ? GFX940_XDL8PassWritesVGPROverlappedSMFMASrcCWaitStates 225881ad6265SDimitry Andric : GFX940_SMFMA8PassWritesVGPROverlappedSMFMASrcCWaitStates 225981ad6265SDimitry Andric : isDGEMM(Opc) 2260fe6060f1SDimitry Andric ? SMFMA16x16WritesVGPROverlappedDMFMASrcCWaitStates 2261fe6060f1SDimitry Andric : SMFMA16x16WritesVGPROverlappedSMFMASrcCWaitStates; 2262fe6060f1SDimitry Andric break; 2263bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2264fe6060f1SDimitry Andric default: 226581ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 226681ad6265SDimitry Andric ? isXDL(ST, *MI1) 226781ad6265SDimitry Andric ? GFX940_XDL16PassWritesVGPROverlappedSMFMASrcCWaitStates 226881ad6265SDimitry Andric : GFX940_SMFMA16PassWritesVGPROverlappedSMFMASrcCWaitStates 226981ad6265SDimitry Andric : isDGEMM(Opc) 2270fe6060f1SDimitry Andric ? SMFMA32x32WritesVGPROverlappedDMFMASrcCWaitStates 2271fe6060f1SDimitry Andric : SMFMA32x32WritesVGPROverlappedSMFMASrcCWaitStates; 2272fe6060f1SDimitry Andric } 2273fe6060f1SDimitry Andric } 2274fe6060f1SDimitry Andric } 2275fe6060f1SDimitry Andric } else { 2276fe6060f1SDimitry Andric switch (Opc1) { 2277fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_e64: 2278fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_vgprcd_e64: 227904eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_e64: 228004eeddc0SDimitry Andric case AMDGPU::V_MFMA_F64_16X16X4F64_mac_vgprcd_e64: 2281fe6060f1SDimitry Andric NeedWaitStates = DMFMA16x16WritesVGPROverlappedMFMASrcABWaitStates; 2282fe6060f1SDimitry Andric break; 2283fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_e64: 2284fe6060f1SDimitry Andric case AMDGPU::V_MFMA_F64_4X4X4F64_vgprcd_e64: 2285fe6060f1SDimitry Andric NeedWaitStates = DMFMA4x4WritesVGPROverlappedMFMASrcABWaitStates; 2286fe6060f1SDimitry Andric break; 2287fe6060f1SDimitry Andric default: 2288fe6060f1SDimitry Andric switch (TSchedModel.computeInstrLatency(MI1)) { 2289fe6060f1SDimitry Andric case 2: 229081ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 229181ad6265SDimitry Andric ? isXDL(ST, *MI1) 229281ad6265SDimitry Andric ? GFX940_XDL2PassWritesVGPROverlappedSrcABWaitStates 229381ad6265SDimitry Andric : GFX940_SMFMA2PassWritesVGPROverlappedSrcABWaitStates 229481ad6265SDimitry Andric : SMFMA4x4WritesVGPROverlappedSrcABWaitStates; 229581ad6265SDimitry Andric break; 229681ad6265SDimitry Andric case 4: 229781ad6265SDimitry Andric assert(ST.hasGFX940Insts()); 229881ad6265SDimitry Andric NeedWaitStates = isXDL(ST, *MI1) 229981ad6265SDimitry Andric ? GFX940_XDL4PassWritesVGPROverlappedSrcABWaitStates 230081ad6265SDimitry Andric : GFX940_SMFMA4PassWritesVGPROverlappedSrcABWaitStates; 2301fe6060f1SDimitry Andric break; 2302fe6060f1SDimitry Andric case 8: 230381ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 230481ad6265SDimitry Andric ? isXDL(ST, *MI1) 230581ad6265SDimitry Andric ? GFX940_XDL8PassWritesVGPROverlappedSrcABWaitStates 230681ad6265SDimitry Andric : GFX940_SMFMA8PassWritesVGPROverlappedSrcABWaitStates 230781ad6265SDimitry Andric : SMFMA16x16WritesVGPROverlappedSrcABWaitStates; 2308fe6060f1SDimitry Andric break; 2309bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2310fe6060f1SDimitry Andric default: 231181ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 231281ad6265SDimitry Andric ? isXDL(ST, *MI1) 231381ad6265SDimitry Andric ? GFX940_XDL16PassWritesVGPROverlappedSrcABWaitStates 231481ad6265SDimitry Andric : GFX940_SMFMA16PassWritesVGPROverlappedSrcABWaitStates 231581ad6265SDimitry Andric : SMFMA32x32WritesVGPROverlappedSrcABWaitStates; 2316fe6060f1SDimitry Andric } 2317fe6060f1SDimitry Andric } 2318fe6060f1SDimitry Andric } 2319fe6060f1SDimitry Andric if (WaitStatesNeeded >= NeedWaitStates) 2320fe6060f1SDimitry Andric continue; 2321fe6060f1SDimitry Andric 2322fe6060f1SDimitry Andric WaitStatesNeededForUse = NeedWaitStates - NumWaitStates; 2323fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2324fe6060f1SDimitry Andric 2325fe6060f1SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 2326fe6060f1SDimitry Andric break; 2327fe6060f1SDimitry Andric } 2328fe6060f1SDimitry Andric 2329fe6060f1SDimitry Andric return WaitStatesNeeded; 2330fe6060f1SDimitry Andric } 2331fe6060f1SDimitry Andric 23320b57cec5SDimitry Andric int GCNHazardRecognizer::checkMAILdStHazards(MachineInstr *MI) { 2333349cc55cSDimitry Andric // On gfx90a+ relevant hazards are checked in checkMAIVALUHazards() 2334fe6060f1SDimitry Andric if (!ST.hasMAIInsts() || ST.hasGFX90AInsts()) 23350b57cec5SDimitry Andric return 0; 23360b57cec5SDimitry Andric 23370b57cec5SDimitry Andric int WaitStatesNeeded = 0; 23380b57cec5SDimitry Andric 2339fe6060f1SDimitry Andric auto IsAccVgprReadFn = [](const MachineInstr &MI) { 2340fe6060f1SDimitry Andric return MI.getOpcode() == AMDGPU::V_ACCVGPR_READ_B32_e64; 23410b57cec5SDimitry Andric }; 23420b57cec5SDimitry Andric 23430b57cec5SDimitry Andric for (const MachineOperand &Op : MI->explicit_uses()) { 23440b57cec5SDimitry Andric if (!Op.isReg() || !TRI.isVGPR(MF.getRegInfo(), Op.getReg())) 23450b57cec5SDimitry Andric continue; 23460b57cec5SDimitry Andric 23478bcb0991SDimitry Andric Register Reg = Op.getReg(); 23480b57cec5SDimitry Andric 23490b57cec5SDimitry Andric const int AccVgprReadLdStWaitStates = 2; 2350e8d8bef9SDimitry Andric const int VALUWriteAccVgprRdWrLdStDepVALUWaitStates = 1; 23510b57cec5SDimitry Andric const int MaxWaitStates = 2; 23520b57cec5SDimitry Andric 23530b57cec5SDimitry Andric int WaitStatesNeededForUse = AccVgprReadLdStWaitStates - 23540b57cec5SDimitry Andric getWaitStatesSinceDef(Reg, IsAccVgprReadFn, MaxWaitStates); 23550b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 23580b57cec5SDimitry Andric return WaitStatesNeeded; // Early exit. 23590b57cec5SDimitry Andric 2360fe6060f1SDimitry Andric auto IsVALUAccVgprRdWrCheckFn = [Reg, this](const MachineInstr &MI) { 2361fe6060f1SDimitry Andric if (MI.getOpcode() != AMDGPU::V_ACCVGPR_READ_B32_e64 && 2362fe6060f1SDimitry Andric MI.getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 23630b57cec5SDimitry Andric return false; 2364fe6060f1SDimitry Andric auto IsVALUFn = [](const MachineInstr &MI) { 2365fe6060f1SDimitry Andric return SIInstrInfo::isVALU(MI) && !SIInstrInfo::isMAI(MI); 23660b57cec5SDimitry Andric }; 23670b57cec5SDimitry Andric return getWaitStatesSinceDef(Reg, IsVALUFn, 2 /*MaxWaitStates*/) < 23680b57cec5SDimitry Andric std::numeric_limits<int>::max(); 23690b57cec5SDimitry Andric }; 23700b57cec5SDimitry Andric 2371e8d8bef9SDimitry Andric WaitStatesNeededForUse = VALUWriteAccVgprRdWrLdStDepVALUWaitStates - 2372e8d8bef9SDimitry Andric getWaitStatesSince(IsVALUAccVgprRdWrCheckFn, MaxWaitStates); 23730b57cec5SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 23740b57cec5SDimitry Andric } 23750b57cec5SDimitry Andric 23760b57cec5SDimitry Andric return WaitStatesNeeded; 23770b57cec5SDimitry Andric } 2378e8d8bef9SDimitry Andric 2379fe6060f1SDimitry Andric int GCNHazardRecognizer::checkMAIVALUHazards(MachineInstr *MI) { 2380fe6060f1SDimitry Andric if (!ST.hasGFX90AInsts()) 2381fe6060f1SDimitry Andric return 0; 2382fe6060f1SDimitry Andric 2383fe6060f1SDimitry Andric auto IsDGEMMFn = [](const MachineInstr &MI) -> bool { 2384fe6060f1SDimitry Andric return isDGEMM(MI.getOpcode()); 2385fe6060f1SDimitry Andric }; 2386fe6060f1SDimitry Andric 2387fe6060f1SDimitry Andric // This is checked in checkMAIHazards90A() 238881ad6265SDimitry Andric if (SIInstrInfo::isMFMA(*MI)) 2389fe6060f1SDimitry Andric return 0; 2390fe6060f1SDimitry Andric 2391bdd1243dSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 2392bdd1243dSDimitry Andric 2393fe6060f1SDimitry Andric int WaitStatesNeeded = 0; 2394fe6060f1SDimitry Andric 2395bdd1243dSDimitry Andric bool IsMem = SIInstrInfo::isVMEM(*MI) || 2396fe6060f1SDimitry Andric SIInstrInfo::isFLAT(*MI) || 2397bdd1243dSDimitry Andric SIInstrInfo::isDS(*MI); 2398bdd1243dSDimitry Andric bool IsMemOrExport = IsMem || SIInstrInfo::isEXP(*MI); 2399fe6060f1SDimitry Andric bool IsVALU = SIInstrInfo::isVALU(*MI); 2400fe6060f1SDimitry Andric 2401fe6060f1SDimitry Andric const MachineInstr *MFMA = nullptr; 2402fe6060f1SDimitry Andric unsigned Reg; 240381ad6265SDimitry Andric auto IsMFMAWriteFn = [&Reg, &MFMA, this](const MachineInstr &MI) { 240481ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI) || 240581ad6265SDimitry Andric !TRI.regsOverlap(MI.getOperand(0).getReg(), Reg)) 2406fe6060f1SDimitry Andric return false; 2407fe6060f1SDimitry Andric MFMA = &MI; 2408fe6060f1SDimitry Andric return true; 2409fe6060f1SDimitry Andric }; 2410fe6060f1SDimitry Andric 2411fe6060f1SDimitry Andric const MachineInstr *DOT = nullptr; 2412fe6060f1SDimitry Andric auto IsDotWriteFn = [&Reg, &DOT, this](const MachineInstr &MI) { 2413fe6060f1SDimitry Andric if (!SIInstrInfo::isDOT(MI) || 2414fe6060f1SDimitry Andric !TRI.regsOverlap(MI.getOperand(0).getReg(), Reg)) 2415fe6060f1SDimitry Andric return false; 2416fe6060f1SDimitry Andric DOT = &MI; 2417fe6060f1SDimitry Andric return true; 2418fe6060f1SDimitry Andric }; 2419fe6060f1SDimitry Andric 2420bdd1243dSDimitry Andric bool DGEMMAfterVALUWrite = false; 2421bdd1243dSDimitry Andric auto IsDGEMMHazard = [&DGEMMAfterVALUWrite, this](const MachineInstr &MI) { 2422bdd1243dSDimitry Andric // Found DGEMM on reverse traversal to def. 2423bdd1243dSDimitry Andric if (isDGEMM(MI.getOpcode())) 2424bdd1243dSDimitry Andric DGEMMAfterVALUWrite = true; 2425bdd1243dSDimitry Andric 2426bdd1243dSDimitry Andric // Only hazard if register is defined by a VALU and a DGEMM is found after 2427bdd1243dSDimitry Andric // after the def. 2428bdd1243dSDimitry Andric if (!TII.isVALU(MI) || !DGEMMAfterVALUWrite) 2429bdd1243dSDimitry Andric return false; 2430bdd1243dSDimitry Andric 2431bdd1243dSDimitry Andric return true; 2432bdd1243dSDimitry Andric }; 2433bdd1243dSDimitry Andric 2434fe6060f1SDimitry Andric int SrcCIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), 2435fe6060f1SDimitry Andric AMDGPU::OpName::src2); 2436fe6060f1SDimitry Andric 2437fe6060f1SDimitry Andric if (IsMemOrExport || IsVALU) { 2438fe6060f1SDimitry Andric const int SMFMA4x4WriteVgprVALUMemExpReadWaitStates = 5; 2439fe6060f1SDimitry Andric const int SMFMA16x16WriteVgprVALUMemExpReadWaitStates = 11; 2440fe6060f1SDimitry Andric const int SMFMA32x32WriteVgprVALUMemExpReadWaitStates = 19; 244181ad6265SDimitry Andric const int GFX940_SMFMA2PassWriteVgprVALUMemExpReadWaitStates = 4; 244281ad6265SDimitry Andric const int GFX940_SMFMA4PassWriteVgprVALUMemExpReadWaitStates = 6; 244381ad6265SDimitry Andric const int GFX940_SMFMA8PassWriteVgprVALUMemExpReadWaitStates = 10; 244481ad6265SDimitry Andric const int GFX940_SMFMA16PassWriteVgprVALUMemExpReadWaitStates = 18; 244581ad6265SDimitry Andric const int GFX940_XDL2PassWriteVgprVALUMemExpReadWaitStates = 5; 244681ad6265SDimitry Andric const int GFX940_XDL4PassWriteVgprVALUMemExpReadWaitStates = 7; 244781ad6265SDimitry Andric const int GFX940_XDL8PassWriteVgprVALUMemExpReadWaitStates = 11; 244881ad6265SDimitry Andric const int GFX940_XDL16PassWriteVgprVALUMemExpReadWaitStates = 19; 2449fe6060f1SDimitry Andric const int DMFMA4x4WriteVgprMemExpReadWaitStates = 9; 2450fe6060f1SDimitry Andric const int DMFMA16x16WriteVgprMemExpReadWaitStates = 18; 2451fe6060f1SDimitry Andric const int DMFMA4x4WriteVgprVALUReadWaitStates = 6; 2452fe6060f1SDimitry Andric const int DMFMA16x16WriteVgprVALUReadWaitStates = 11; 2453fe6060f1SDimitry Andric const int DotWriteSameDotReadSrcAB = 3; 2454fe6060f1SDimitry Andric const int DotWriteDifferentVALURead = 3; 2455bdd1243dSDimitry Andric const int DMFMABetweenVALUWriteVMEMRead = 2; 2456fe6060f1SDimitry Andric const int MaxWaitStates = 19; 2457fe6060f1SDimitry Andric 2458fe6060f1SDimitry Andric for (const MachineOperand &Use : MI->explicit_uses()) { 2459fe6060f1SDimitry Andric if (!Use.isReg()) 2460fe6060f1SDimitry Andric continue; 2461fe6060f1SDimitry Andric Reg = Use.getReg(); 2462fe6060f1SDimitry Andric 2463fe6060f1SDimitry Andric DOT = nullptr; 2464fe6060f1SDimitry Andric int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsDotWriteFn, 2465fe6060f1SDimitry Andric MaxWaitStates); 2466fe6060f1SDimitry Andric if (DOT) { 2467fe6060f1SDimitry Andric int NeedWaitStates = 0; 2468fe6060f1SDimitry Andric if (DOT->getOpcode() == MI->getOpcode()) { 2469fe6060f1SDimitry Andric if (&Use - &MI->getOperand(0) != SrcCIdx) 2470fe6060f1SDimitry Andric NeedWaitStates = DotWriteSameDotReadSrcAB; 2471fe6060f1SDimitry Andric } else { 2472fe6060f1SDimitry Andric NeedWaitStates = DotWriteDifferentVALURead; 2473fe6060f1SDimitry Andric } 2474fe6060f1SDimitry Andric 2475fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2476fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2477fe6060f1SDimitry Andric } 2478fe6060f1SDimitry Andric 2479bdd1243dSDimitry Andric // Workaround for HW data hazard bug observed only in GFX90A. When there 2480bdd1243dSDimitry Andric // is a DGEMM instruction in-between a VALU and a VMEM instruction it 2481bdd1243dSDimitry Andric // causes the SQ to incorrectly not insert two wait states between the two 2482bdd1243dSDimitry Andric // instructions needed to avoid data hazard. 2483bdd1243dSDimitry Andric if (IsMem && ST.hasGFX90AInsts() && !ST.hasGFX940Insts()) { 2484bdd1243dSDimitry Andric DGEMMAfterVALUWrite = false; 2485bdd1243dSDimitry Andric if (TRI.isVectorRegister(MRI, Reg)) { 2486bdd1243dSDimitry Andric int WaitStatesNeededForUse = 2487bdd1243dSDimitry Andric DMFMABetweenVALUWriteVMEMRead - 2488bdd1243dSDimitry Andric getWaitStatesSinceDef(Reg, IsDGEMMHazard, 2489bdd1243dSDimitry Andric DMFMABetweenVALUWriteVMEMRead); 2490bdd1243dSDimitry Andric 2491bdd1243dSDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2492bdd1243dSDimitry Andric } 2493bdd1243dSDimitry Andric } 2494bdd1243dSDimitry Andric 2495fe6060f1SDimitry Andric MFMA = nullptr; 24964824e7fdSDimitry Andric WaitStatesSinceDef = 24974824e7fdSDimitry Andric getWaitStatesSinceDef(Reg, IsMFMAWriteFn, MaxWaitStates); 2498fe6060f1SDimitry Andric if (!MFMA) 2499fe6060f1SDimitry Andric continue; 2500fe6060f1SDimitry Andric 2501fe6060f1SDimitry Andric unsigned HazardDefLatency = TSchedModel.computeInstrLatency(MFMA); 2502fe6060f1SDimitry Andric int NeedWaitStates = MaxWaitStates; 2503fe6060f1SDimitry Andric switch (HazardDefLatency) { 2504fe6060f1SDimitry Andric case 2: 250581ad6265SDimitry Andric NeedWaitStates = 250681ad6265SDimitry Andric ST.hasGFX940Insts() 250781ad6265SDimitry Andric ? isXDL(ST, *MFMA) 250881ad6265SDimitry Andric ? GFX940_XDL2PassWriteVgprVALUMemExpReadWaitStates 250981ad6265SDimitry Andric : GFX940_SMFMA2PassWriteVgprVALUMemExpReadWaitStates 251081ad6265SDimitry Andric : SMFMA4x4WriteVgprVALUMemExpReadWaitStates; 2511fe6060f1SDimitry Andric break; 2512fe6060f1SDimitry Andric case 4: 251381ad6265SDimitry Andric assert(isDGEMM(MFMA->getOpcode()) || ST.hasGFX940Insts()); 2514fe6060f1SDimitry Andric NeedWaitStates = 251581ad6265SDimitry Andric isDGEMM(MFMA->getOpcode()) 251681ad6265SDimitry Andric ? IsMemOrExport ? DMFMA4x4WriteVgprMemExpReadWaitStates 251781ad6265SDimitry Andric : DMFMA4x4WriteVgprVALUReadWaitStates 251881ad6265SDimitry Andric : isXDL(ST, *MFMA) 251981ad6265SDimitry Andric ? GFX940_XDL4PassWriteVgprVALUMemExpReadWaitStates 252081ad6265SDimitry Andric : GFX940_SMFMA4PassWriteVgprVALUMemExpReadWaitStates; 2521fe6060f1SDimitry Andric break; 2522fe6060f1SDimitry Andric case 8: 252381ad6265SDimitry Andric NeedWaitStates = 252481ad6265SDimitry Andric ST.hasGFX940Insts() 252581ad6265SDimitry Andric ? isXDL(ST, *MFMA) 252681ad6265SDimitry Andric ? GFX940_XDL8PassWriteVgprVALUMemExpReadWaitStates 252781ad6265SDimitry Andric : GFX940_SMFMA8PassWriteVgprVALUMemExpReadWaitStates 252881ad6265SDimitry Andric : SMFMA16x16WriteVgprVALUMemExpReadWaitStates; 2529fe6060f1SDimitry Andric break; 2530bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2531fe6060f1SDimitry Andric default: 2532fe6060f1SDimitry Andric NeedWaitStates = 2533fe6060f1SDimitry Andric isDGEMM(MFMA->getOpcode()) 2534fe6060f1SDimitry Andric ? IsMemOrExport ? DMFMA16x16WriteVgprMemExpReadWaitStates 2535fe6060f1SDimitry Andric : DMFMA16x16WriteVgprVALUReadWaitStates 253681ad6265SDimitry Andric : ST.hasGFX940Insts() 253781ad6265SDimitry Andric ? isXDL(ST, *MFMA) 253881ad6265SDimitry Andric ? GFX940_XDL16PassWriteVgprVALUMemExpReadWaitStates 253981ad6265SDimitry Andric : GFX940_SMFMA16PassWriteVgprVALUMemExpReadWaitStates 2540fe6060f1SDimitry Andric : SMFMA32x32WriteVgprVALUMemExpReadWaitStates; 2541fe6060f1SDimitry Andric break; 2542fe6060f1SDimitry Andric } 2543fe6060f1SDimitry Andric 2544fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2545fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2546fe6060f1SDimitry Andric 2547fe6060f1SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 2548fe6060f1SDimitry Andric break; 2549fe6060f1SDimitry Andric } 2550fe6060f1SDimitry Andric } 2551fe6060f1SDimitry Andric 2552fe6060f1SDimitry Andric unsigned Opc = MI->getOpcode(); 2553fe6060f1SDimitry Andric const int DMFMAToFMA64WaitStates = 2; 2554fe6060f1SDimitry Andric if ((Opc == AMDGPU::V_FMA_F64_e64 || 2555fe6060f1SDimitry Andric Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64 || 2556fe6060f1SDimitry Andric Opc == AMDGPU::V_FMAC_F64_dpp) && 2557fe6060f1SDimitry Andric WaitStatesNeeded < DMFMAToFMA64WaitStates) { 2558fe6060f1SDimitry Andric int WaitStatesNeededForUse = DMFMAToFMA64WaitStates - 2559fe6060f1SDimitry Andric getWaitStatesSince(IsDGEMMFn, DMFMAToFMA64WaitStates); 2560fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2561fe6060f1SDimitry Andric } 2562fe6060f1SDimitry Andric 2563fe6060f1SDimitry Andric if (!IsVALU && !IsMemOrExport) 2564fe6060f1SDimitry Andric return WaitStatesNeeded; 2565fe6060f1SDimitry Andric 2566fe6060f1SDimitry Andric for (const MachineOperand &Def : MI->defs()) { 2567fe6060f1SDimitry Andric const int SMFMA4x4WriteVgprVALUWawWaitStates = 5; 2568fe6060f1SDimitry Andric const int SMFMA16x16WriteVgprVALUWawWaitStates = 11; 2569fe6060f1SDimitry Andric const int SMFMA32x32WriteVgprVALUWawWaitStates = 19; 257081ad6265SDimitry Andric const int GFX940_SMFMA2PassWriteVgprVALUWawWaitStates = 4; 257181ad6265SDimitry Andric const int GFX940_SMFMA4PassWriteVgprVALUWawWaitStates = 6; 257281ad6265SDimitry Andric const int GFX940_SMFMA8PassWriteVgprVALUWawWaitStates = 10; 257381ad6265SDimitry Andric const int GFX940_SMFMA16PassWriteVgprVALUWawWaitStates = 18; 257481ad6265SDimitry Andric const int GFX940_XDL2PassWriteVgprVALUWawWaitStates = 5; 257581ad6265SDimitry Andric const int GFX940_XDL4PassWriteVgprVALUWawWaitStates = 7; 257681ad6265SDimitry Andric const int GFX940_XDL8PassWriteVgprVALUWawWaitStates = 11; 257781ad6265SDimitry Andric const int GFX940_XDL16PassWriteVgprVALUWawWaitStates = 19; 2578fe6060f1SDimitry Andric const int SMFMA4x4ReadVgprVALUWarWaitStates = 1; 257981ad6265SDimitry Andric const int GFX940_XDL4PassReadVgprVALUWarWaitStates = 3; 2580fe6060f1SDimitry Andric const int SMFMA16x16ReadVgprVALUWarWaitStates = 7; 2581fe6060f1SDimitry Andric const int SMFMA32x32ReadVgprVALUWarWaitStates = 15; 2582fe6060f1SDimitry Andric const int DMFMA4x4WriteVgprVALUWriteWaitStates = 6; 2583fe6060f1SDimitry Andric const int DMFMA16x16WriteVgprVALUWriteWaitStates = 11; 2584fe6060f1SDimitry Andric const int DotWriteDifferentVALUWrite = 3; 2585fe6060f1SDimitry Andric const int MaxWaitStates = 19; 2586fe6060f1SDimitry Andric const int MaxWarWaitStates = 15; 2587fe6060f1SDimitry Andric 2588fe6060f1SDimitry Andric Reg = Def.getReg(); 2589fe6060f1SDimitry Andric 2590fe6060f1SDimitry Andric DOT = nullptr; 2591fe6060f1SDimitry Andric int WaitStatesSinceDef = getWaitStatesSinceDef(Reg, IsDotWriteFn, 2592fe6060f1SDimitry Andric MaxWaitStates); 2593fe6060f1SDimitry Andric if (DOT && DOT->getOpcode() != MI->getOpcode()) 2594fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, DotWriteDifferentVALUWrite - 2595fe6060f1SDimitry Andric WaitStatesSinceDef); 2596fe6060f1SDimitry Andric 2597fe6060f1SDimitry Andric MFMA = nullptr; 25984824e7fdSDimitry Andric WaitStatesSinceDef = 25994824e7fdSDimitry Andric getWaitStatesSinceDef(Reg, IsMFMAWriteFn, MaxWaitStates); 2600fe6060f1SDimitry Andric if (MFMA) { 2601fe6060f1SDimitry Andric int NeedWaitStates = MaxWaitStates; 2602fe6060f1SDimitry Andric switch (TSchedModel.computeInstrLatency(MFMA)) { 2603fe6060f1SDimitry Andric case 2: 260481ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 260581ad6265SDimitry Andric ? isXDL(ST, *MFMA) 260681ad6265SDimitry Andric ? GFX940_XDL2PassWriteVgprVALUWawWaitStates 260781ad6265SDimitry Andric : GFX940_SMFMA2PassWriteVgprVALUWawWaitStates 260881ad6265SDimitry Andric : SMFMA4x4WriteVgprVALUWawWaitStates; 2609fe6060f1SDimitry Andric break; 2610fe6060f1SDimitry Andric case 4: 261181ad6265SDimitry Andric assert(isDGEMM(MFMA->getOpcode()) || ST.hasGFX940Insts()); 261281ad6265SDimitry Andric NeedWaitStates = isDGEMM(MFMA->getOpcode()) 261381ad6265SDimitry Andric ? DMFMA4x4WriteVgprVALUWriteWaitStates 261481ad6265SDimitry Andric : isXDL(ST, *MFMA) 261581ad6265SDimitry Andric ? GFX940_XDL4PassWriteVgprVALUWawWaitStates 261681ad6265SDimitry Andric : GFX940_SMFMA4PassWriteVgprVALUWawWaitStates; 2617fe6060f1SDimitry Andric break; 2618fe6060f1SDimitry Andric case 8: 261981ad6265SDimitry Andric NeedWaitStates = ST.hasGFX940Insts() 262081ad6265SDimitry Andric ? isXDL(ST, *MFMA) 262181ad6265SDimitry Andric ? GFX940_XDL8PassWriteVgprVALUWawWaitStates 262281ad6265SDimitry Andric : GFX940_SMFMA8PassWriteVgprVALUWawWaitStates 262381ad6265SDimitry Andric : SMFMA16x16WriteVgprVALUWawWaitStates; 2624fe6060f1SDimitry Andric break; 2625bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2626fe6060f1SDimitry Andric default: 2627fe6060f1SDimitry Andric NeedWaitStates = isDGEMM(MFMA->getOpcode()) 2628fe6060f1SDimitry Andric ? DMFMA16x16WriteVgprVALUWriteWaitStates 262981ad6265SDimitry Andric : ST.hasGFX940Insts() 263081ad6265SDimitry Andric ? isXDL(ST, *MFMA) 263181ad6265SDimitry Andric ? GFX940_XDL16PassWriteVgprVALUWawWaitStates 263281ad6265SDimitry Andric : GFX940_SMFMA16PassWriteVgprVALUWawWaitStates 2633fe6060f1SDimitry Andric : SMFMA32x32WriteVgprVALUWawWaitStates; 2634fe6060f1SDimitry Andric break; 2635fe6060f1SDimitry Andric } 2636fe6060f1SDimitry Andric 2637fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceDef; 2638fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2639fe6060f1SDimitry Andric 2640fe6060f1SDimitry Andric if (WaitStatesNeeded == MaxWaitStates) 2641fe6060f1SDimitry Andric break; 2642fe6060f1SDimitry Andric } 2643fe6060f1SDimitry Andric 264481ad6265SDimitry Andric auto IsSMFMAReadAsCFn = [&Reg, &MFMA, this](const MachineInstr &MI) { 264581ad6265SDimitry Andric if (!SIInstrInfo::isMFMA(MI) || isDGEMM(MI.getOpcode()) || 2646fe6060f1SDimitry Andric !MI.readsRegister(Reg, &TRI)) 2647fe6060f1SDimitry Andric return false; 2648fe6060f1SDimitry Andric 264981ad6265SDimitry Andric if (ST.hasGFX940Insts() && !isXDL(ST, MI)) 265081ad6265SDimitry Andric return false; 265181ad6265SDimitry Andric 2652fe6060f1SDimitry Andric const MachineOperand *SrcC = 2653fe6060f1SDimitry Andric TII.getNamedOperand(MI, AMDGPU::OpName::src2); 2654fe6060f1SDimitry Andric assert(SrcC); 2655fe6060f1SDimitry Andric if (!SrcC->isReg() || !TRI.regsOverlap(SrcC->getReg(), Reg)) 2656fe6060f1SDimitry Andric return false; 2657fe6060f1SDimitry Andric 2658fe6060f1SDimitry Andric MFMA = &MI; 2659fe6060f1SDimitry Andric return true; 2660fe6060f1SDimitry Andric }; 2661fe6060f1SDimitry Andric 2662fe6060f1SDimitry Andric MFMA = nullptr; 2663fe6060f1SDimitry Andric int WaitStatesSinceUse = getWaitStatesSince(IsSMFMAReadAsCFn, 2664fe6060f1SDimitry Andric MaxWarWaitStates); 2665fe6060f1SDimitry Andric if (!MFMA) 2666fe6060f1SDimitry Andric continue; 2667fe6060f1SDimitry Andric 2668fe6060f1SDimitry Andric unsigned HazardDefLatency = TSchedModel.computeInstrLatency(MFMA); 2669fe6060f1SDimitry Andric int NeedWaitStates = MaxWaitStates; 2670fe6060f1SDimitry Andric switch (HazardDefLatency) { 2671fe6060f1SDimitry Andric case 2: NeedWaitStates = SMFMA4x4ReadVgprVALUWarWaitStates; 2672fe6060f1SDimitry Andric break; 267381ad6265SDimitry Andric case 4: assert(ST.hasGFX940Insts()); 267481ad6265SDimitry Andric NeedWaitStates = GFX940_XDL4PassReadVgprVALUWarWaitStates; 267581ad6265SDimitry Andric break; 2676fe6060f1SDimitry Andric case 8: NeedWaitStates = SMFMA16x16ReadVgprVALUWarWaitStates; 2677fe6060f1SDimitry Andric break; 2678bdd1243dSDimitry Andric case 16: [[fallthrough]]; 2679fe6060f1SDimitry Andric default: NeedWaitStates = SMFMA32x32ReadVgprVALUWarWaitStates; 2680fe6060f1SDimitry Andric break; 2681fe6060f1SDimitry Andric } 2682fe6060f1SDimitry Andric 2683fe6060f1SDimitry Andric int WaitStatesNeededForUse = NeedWaitStates - WaitStatesSinceUse; 2684fe6060f1SDimitry Andric WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); 2685fe6060f1SDimitry Andric } 2686fe6060f1SDimitry Andric 2687fe6060f1SDimitry Andric return WaitStatesNeeded; 2688fe6060f1SDimitry Andric } 2689fe6060f1SDimitry Andric 2690e8d8bef9SDimitry Andric bool GCNHazardRecognizer::ShouldPreferAnother(SUnit *SU) { 2691e8d8bef9SDimitry Andric if (!SU->isInstr()) 2692e8d8bef9SDimitry Andric return false; 2693e8d8bef9SDimitry Andric 2694fe6060f1SDimitry Andric const MachineInstr *MAI = nullptr; 269581ad6265SDimitry Andric 2696fe6060f1SDimitry Andric auto IsMFMAFn = [&MAI](const MachineInstr &MI) { 2697e8d8bef9SDimitry Andric MAI = nullptr; 269881ad6265SDimitry Andric if (SIInstrInfo::isMFMA(MI)) 2699fe6060f1SDimitry Andric MAI = &MI; 2700e8d8bef9SDimitry Andric return MAI != nullptr; 2701e8d8bef9SDimitry Andric }; 2702e8d8bef9SDimitry Andric 2703e8d8bef9SDimitry Andric MachineInstr *MI = SU->getInstr(); 2704fe6060f1SDimitry Andric if (IsMFMAFn(*MI)) { 2705e8d8bef9SDimitry Andric int W = getWaitStatesSince(IsMFMAFn, 16); 2706e8d8bef9SDimitry Andric if (MAI) 2707e8d8bef9SDimitry Andric return W < (int)TSchedModel.computeInstrLatency(MAI); 2708e8d8bef9SDimitry Andric } 2709e8d8bef9SDimitry Andric 2710e8d8bef9SDimitry Andric return false; 2711e8d8bef9SDimitry Andric } 2712bdd1243dSDimitry Andric 2713bdd1243dSDimitry Andric bool GCNHazardRecognizer::fixVALUMaskWriteHazard(MachineInstr *MI) { 2714bdd1243dSDimitry Andric if (!ST.isWave64()) 2715bdd1243dSDimitry Andric return false; 2716bdd1243dSDimitry Andric if (!ST.hasVALUMaskWriteHazard()) 2717bdd1243dSDimitry Andric return false; 2718bdd1243dSDimitry Andric if (!SIInstrInfo::isSALU(*MI)) 2719bdd1243dSDimitry Andric return false; 2720bdd1243dSDimitry Andric 2721bdd1243dSDimitry Andric // The hazard sequence is three instructions: 2722bdd1243dSDimitry Andric // 1. VALU reads SGPR as mask 2723bdd1243dSDimitry Andric // 2. SALU writes SGPR 2724bdd1243dSDimitry Andric // 3. SALU reads SGPR 2725bdd1243dSDimitry Andric // The hazard can expire if the distance between 2 and 3 is sufficient. 2726bdd1243dSDimitry Andric // In practice this happens <10% of the time, hence this always assumes 2727bdd1243dSDimitry Andric // the hazard exists if 1 and 2 are present to avoid searching. 2728bdd1243dSDimitry Andric 2729bdd1243dSDimitry Andric const MachineOperand *SDSTOp = TII.getNamedOperand(*MI, AMDGPU::OpName::sdst); 2730bdd1243dSDimitry Andric if (!SDSTOp || !SDSTOp->isReg()) 2731bdd1243dSDimitry Andric return false; 2732bdd1243dSDimitry Andric 2733bdd1243dSDimitry Andric const Register HazardReg = SDSTOp->getReg(); 2734bdd1243dSDimitry Andric if (HazardReg == AMDGPU::EXEC || 2735bdd1243dSDimitry Andric HazardReg == AMDGPU::EXEC_LO || 2736bdd1243dSDimitry Andric HazardReg == AMDGPU::EXEC_HI || 2737bdd1243dSDimitry Andric HazardReg == AMDGPU::M0) 2738bdd1243dSDimitry Andric return false; 2739bdd1243dSDimitry Andric 2740bdd1243dSDimitry Andric auto IsHazardFn = [HazardReg, this](const MachineInstr &I) { 2741bdd1243dSDimitry Andric switch (I.getOpcode()) { 2742bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_e32: 2743bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_dpp: 2744bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_e32: 2745bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_dpp: 2746bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_e32: 2747bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_dpp: 2748bdd1243dSDimitry Andric case AMDGPU::V_DIV_FMAS_F32_e64: 2749bdd1243dSDimitry Andric case AMDGPU::V_DIV_FMAS_F64_e64: 2750bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_e32: 2751bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_dpp: 2752bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_e32: 2753bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_dpp: 2754bdd1243dSDimitry Andric // These implicitly read VCC as mask source. 2755bdd1243dSDimitry Andric return HazardReg == AMDGPU::VCC || 2756bdd1243dSDimitry Andric HazardReg == AMDGPU::VCC_LO || 2757bdd1243dSDimitry Andric HazardReg == AMDGPU::VCC_HI; 2758bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_e64: 2759bdd1243dSDimitry Andric case AMDGPU::V_ADDC_U32_e64_dpp: 2760bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_e64: 2761bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B16_e64_dpp: 2762bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_e64: 2763bdd1243dSDimitry Andric case AMDGPU::V_CNDMASK_B32_e64_dpp: 2764bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_e64: 2765bdd1243dSDimitry Andric case AMDGPU::V_SUBB_U32_e64_dpp: 2766bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_e64: 2767bdd1243dSDimitry Andric case AMDGPU::V_SUBBREV_U32_e64_dpp: { 2768bdd1243dSDimitry Andric // Only check mask register overlaps. 2769bdd1243dSDimitry Andric const MachineOperand *SSRCOp = TII.getNamedOperand(I, AMDGPU::OpName::src2); 2770bdd1243dSDimitry Andric assert(SSRCOp); 2771bdd1243dSDimitry Andric return TRI.regsOverlap(SSRCOp->getReg(), HazardReg); 2772bdd1243dSDimitry Andric } 2773bdd1243dSDimitry Andric default: 2774bdd1243dSDimitry Andric return false; 2775bdd1243dSDimitry Andric } 2776bdd1243dSDimitry Andric }; 2777bdd1243dSDimitry Andric 2778bdd1243dSDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 2779bdd1243dSDimitry Andric auto IsExpiredFn = [&MRI, this](const MachineInstr &I, int) { 2780bdd1243dSDimitry Andric // s_waitcnt_depctr sa_sdst(0) mitigates hazard. 2781bdd1243dSDimitry Andric if (I.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && 2782*06c3fb27SDimitry Andric AMDGPU::DepCtr::decodeFieldSaSdst(I.getOperand(0).getImm()) == 0) 2783bdd1243dSDimitry Andric return true; 2784bdd1243dSDimitry Andric 2785bdd1243dSDimitry Andric // VALU access to any SGPR or literal constant other than HazardReg 2786bdd1243dSDimitry Andric // mitigates hazard. No need to check HazardReg here as this will 2787bdd1243dSDimitry Andric // only be called when !IsHazardFn. 2788bdd1243dSDimitry Andric if (!SIInstrInfo::isVALU(I)) 2789bdd1243dSDimitry Andric return false; 2790bdd1243dSDimitry Andric for (int OpNo = 0, End = I.getNumOperands(); OpNo < End; ++OpNo) { 2791bdd1243dSDimitry Andric const MachineOperand &Op = I.getOperand(OpNo); 2792bdd1243dSDimitry Andric if (Op.isReg()) { 2793bdd1243dSDimitry Andric Register OpReg = Op.getReg(); 2794bdd1243dSDimitry Andric // Only consider uses 2795bdd1243dSDimitry Andric if (!Op.isUse()) 2796bdd1243dSDimitry Andric continue; 2797bdd1243dSDimitry Andric // Ignore EXEC 2798bdd1243dSDimitry Andric if (OpReg == AMDGPU::EXEC || 2799bdd1243dSDimitry Andric OpReg == AMDGPU::EXEC_LO || 2800bdd1243dSDimitry Andric OpReg == AMDGPU::EXEC_HI) 2801bdd1243dSDimitry Andric continue; 2802bdd1243dSDimitry Andric // Ignore all implicit uses except VCC 2803bdd1243dSDimitry Andric if (Op.isImplicit()) { 2804bdd1243dSDimitry Andric if (OpReg == AMDGPU::VCC || 2805bdd1243dSDimitry Andric OpReg == AMDGPU::VCC_LO || 2806bdd1243dSDimitry Andric OpReg == AMDGPU::VCC_HI) 2807bdd1243dSDimitry Andric return true; 2808bdd1243dSDimitry Andric continue; 2809bdd1243dSDimitry Andric } 2810bdd1243dSDimitry Andric if (TRI.isSGPRReg(MRI, OpReg)) 2811bdd1243dSDimitry Andric return true; 2812bdd1243dSDimitry Andric } else { 2813bdd1243dSDimitry Andric const MCInstrDesc &InstDesc = I.getDesc(); 2814bdd1243dSDimitry Andric const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo]; 2815bdd1243dSDimitry Andric if (!TII.isInlineConstant(Op, OpInfo)) 2816bdd1243dSDimitry Andric return true; 2817bdd1243dSDimitry Andric } 2818bdd1243dSDimitry Andric } 2819bdd1243dSDimitry Andric return false; 2820bdd1243dSDimitry Andric }; 2821bdd1243dSDimitry Andric 2822bdd1243dSDimitry Andric // Check for hazard 2823bdd1243dSDimitry Andric if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == 2824bdd1243dSDimitry Andric std::numeric_limits<int>::max()) 2825bdd1243dSDimitry Andric return false; 2826bdd1243dSDimitry Andric 2827bdd1243dSDimitry Andric auto NextMI = std::next(MI->getIterator()); 2828bdd1243dSDimitry Andric 2829bdd1243dSDimitry Andric // Add s_waitcnt_depctr sa_sdst(0) after SALU write. 2830bdd1243dSDimitry Andric BuildMI(*MI->getParent(), NextMI, MI->getDebugLoc(), 2831bdd1243dSDimitry Andric TII.get(AMDGPU::S_WAITCNT_DEPCTR)) 2832*06c3fb27SDimitry Andric .addImm(AMDGPU::DepCtr::encodeFieldSaSdst(0)); 2833bdd1243dSDimitry Andric 2834bdd1243dSDimitry Andric // SALU write may be s_getpc in a bundle. 2835bdd1243dSDimitry Andric if (MI->getOpcode() == AMDGPU::S_GETPC_B64) { 2836bdd1243dSDimitry Andric // Update offsets of any references in the bundle. 2837bdd1243dSDimitry Andric while (NextMI != MI->getParent()->end() && 2838bdd1243dSDimitry Andric NextMI->isBundledWithPred()) { 2839bdd1243dSDimitry Andric for (auto &Operand : NextMI->operands()) { 2840bdd1243dSDimitry Andric if (Operand.isGlobal()) 2841bdd1243dSDimitry Andric Operand.setOffset(Operand.getOffset() + 4); 2842bdd1243dSDimitry Andric } 2843bdd1243dSDimitry Andric NextMI++; 2844bdd1243dSDimitry Andric } 2845bdd1243dSDimitry Andric } 2846bdd1243dSDimitry Andric 2847bdd1243dSDimitry Andric return true; 2848bdd1243dSDimitry Andric } 2849