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