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" |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineOperand.h" |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ScheduleDAG.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCInstrDesc.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include <algorithm> |
| 29 | #include <cassert> |
| 30 | #include <limits> |
| 31 | #include <set> |
| 32 | #include <vector> |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Hazard Recoginizer Implementation |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) : |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 41 | IsHazardRecognizerMode(false), |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 42 | CurrCycleInstr(nullptr), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 43 | MF(MF), |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 44 | ST(MF.getSubtarget<GCNSubtarget>()), |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 45 | TII(*ST.getInstrInfo()), |
| 46 | TRI(TII.getRegisterInfo()), |
| 47 | ClauseUses(TRI.getNumRegUnits()), |
| 48 | ClauseDefs(TRI.getNumRegUnits()) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 49 | MaxLookAhead = 5; |
| 50 | } |
| 51 | |
| 52 | void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { |
| 53 | EmitInstruction(SU->getInstr()); |
| 54 | } |
| 55 | |
| 56 | void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { |
| 57 | CurrCycleInstr = MI; |
| 58 | } |
| 59 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 60 | static bool isDivFMas(unsigned Opcode) { |
| 61 | return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64; |
| 62 | } |
| 63 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 64 | static bool isSGetReg(unsigned Opcode) { |
| 65 | return Opcode == AMDGPU::S_GETREG_B32; |
| 66 | } |
| 67 | |
| 68 | static bool isSSetReg(unsigned Opcode) { |
| 69 | return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32; |
| 70 | } |
| 71 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 72 | static bool isRWLane(unsigned Opcode) { |
| 73 | return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; |
| 74 | } |
| 75 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 76 | static bool isRFE(unsigned Opcode) { |
| 77 | return Opcode == AMDGPU::S_RFE_B64; |
| 78 | } |
| 79 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 80 | static bool isSMovRel(unsigned Opcode) { |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 81 | switch (Opcode) { |
| 82 | case AMDGPU::S_MOVRELS_B32: |
| 83 | case AMDGPU::S_MOVRELS_B64: |
| 84 | case AMDGPU::S_MOVRELD_B32: |
| 85 | case AMDGPU::S_MOVRELD_B64: |
| 86 | return true; |
| 87 | default: |
| 88 | return false; |
| 89 | } |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Marek Olsak | c5cec5e | 2019-01-16 15:43:53 +0000 | [diff] [blame] | 92 | static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII, |
| 93 | const MachineInstr &MI) { |
| 94 | if (TII.isAlwaysGDS(MI.getOpcode())) |
| 95 | return true; |
| 96 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 97 | switch (MI.getOpcode()) { |
| 98 | case AMDGPU::S_SENDMSG: |
| 99 | case AMDGPU::S_SENDMSGHALT: |
| 100 | case AMDGPU::S_TTRACEDATA: |
| 101 | return true; |
Marek Olsak | c5cec5e | 2019-01-16 15:43:53 +0000 | [diff] [blame] | 102 | // These DS opcodes don't support GDS. |
| 103 | case AMDGPU::DS_NOP: |
| 104 | case AMDGPU::DS_PERMUTE_B32: |
| 105 | case AMDGPU::DS_BPERMUTE_B32: |
| 106 | return false; |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 107 | default: |
Marek Olsak | c5cec5e | 2019-01-16 15:43:53 +0000 | [diff] [blame] | 108 | if (TII.isDS(MI.getOpcode())) { |
| 109 | int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(), |
| 110 | AMDGPU::OpName::gds); |
| 111 | if (MI.getOperand(GDS).getImm()) |
| 112 | return true; |
| 113 | } |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
Stanislav Mekhanoshin | 5f581c9 | 2019-06-12 17:52:51 +0000 | [diff] [blame^] | 118 | static bool isPermlane(const MachineInstr &MI) { |
| 119 | unsigned Opcode = MI.getOpcode(); |
| 120 | return Opcode == AMDGPU::V_PERMLANE16_B32 || |
| 121 | Opcode == AMDGPU::V_PERMLANEX16_B32; |
| 122 | } |
| 123 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 124 | static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 125 | const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, |
| 126 | AMDGPU::OpName::simm16); |
| 127 | return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; |
| 128 | } |
| 129 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 130 | ScheduleHazardRecognizer::HazardType |
| 131 | GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 132 | MachineInstr *MI = SU->getInstr(); |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 133 | if (MI->isBundle()) |
| 134 | return NoHazard; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 135 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 136 | if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 137 | return NoopHazard; |
| 138 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 139 | // FIXME: Should flat be considered vmem? |
| 140 | if ((SIInstrInfo::isVMEM(*MI) || |
| 141 | SIInstrInfo::isFLAT(*MI)) |
| 142 | && checkVMEMHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 143 | return NoopHazard; |
| 144 | |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 145 | if (ST.hasNSAtoVMEMBug() && checkNSAtoVMEMHazard(MI) > 0) |
| 146 | return NoopHazard; |
| 147 | |
| 148 | if (ST.hasNoDataDepHazard()) |
| 149 | return NoHazard; |
| 150 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 151 | if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) |
| 152 | return NoopHazard; |
| 153 | |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 154 | if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) |
| 155 | return NoopHazard; |
| 156 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 157 | if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) |
| 158 | return NoopHazard; |
| 159 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 160 | if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) |
| 161 | return NoopHazard; |
| 162 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 163 | if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) |
| 164 | return NoopHazard; |
| 165 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 166 | if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) |
| 167 | return NoopHazard; |
| 168 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 169 | if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) |
| 170 | return NoopHazard; |
| 171 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 172 | if (ST.hasReadM0MovRelInterpHazard() && |
| 173 | (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) && |
| 174 | checkReadM0Hazards(MI) > 0) |
| 175 | return NoopHazard; |
| 176 | |
Marek Olsak | c5cec5e | 2019-01-16 15:43:53 +0000 | [diff] [blame] | 177 | if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI) && |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 178 | checkReadM0Hazards(MI) > 0) |
| 179 | return NoopHazard; |
| 180 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 181 | if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0) |
| 182 | return NoopHazard; |
| 183 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 184 | if (checkAnyInstHazards(MI) > 0) |
| 185 | return NoopHazard; |
| 186 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 187 | return NoHazard; |
| 188 | } |
| 189 | |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 190 | static void insertNoopInBundle(MachineInstr *MI, const SIInstrInfo &TII) { |
| 191 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII.get(AMDGPU::S_NOP)) |
| 192 | .addImm(0); |
| 193 | } |
| 194 | |
| 195 | void GCNHazardRecognizer::processBundle() { |
| 196 | MachineBasicBlock::instr_iterator MI = std::next(CurrCycleInstr->getIterator()); |
| 197 | MachineBasicBlock::instr_iterator E = CurrCycleInstr->getParent()->instr_end(); |
| 198 | // Check bundled MachineInstr's for hazards. |
| 199 | for (; MI != E && MI->isInsideBundle(); ++MI) { |
| 200 | CurrCycleInstr = &*MI; |
| 201 | unsigned WaitStates = PreEmitNoopsCommon(CurrCycleInstr); |
| 202 | |
| 203 | if (IsHazardRecognizerMode) |
| 204 | fixHazards(CurrCycleInstr); |
| 205 | |
| 206 | for (unsigned i = 0; i < WaitStates; ++i) |
| 207 | insertNoopInBundle(CurrCycleInstr, TII); |
| 208 | |
| 209 | // It’s unnecessary to track more than MaxLookAhead instructions. Since we |
| 210 | // include the bundled MI directly after, only add a maximum of |
| 211 | // (MaxLookAhead - 1) noops to EmittedInstrs. |
| 212 | for (unsigned i = 0, e = std::min(WaitStates, MaxLookAhead - 1); i < e; ++i) |
| 213 | EmittedInstrs.push_front(nullptr); |
| 214 | |
| 215 | EmittedInstrs.push_front(CurrCycleInstr); |
| 216 | EmittedInstrs.resize(MaxLookAhead); |
| 217 | } |
| 218 | CurrCycleInstr = nullptr; |
| 219 | } |
| 220 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 221 | unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) { |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 222 | IsHazardRecognizerMode = false; |
| 223 | return PreEmitNoopsCommon(SU->getInstr()); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 227 | IsHazardRecognizerMode = true; |
| 228 | CurrCycleInstr = MI; |
| 229 | unsigned W = PreEmitNoopsCommon(MI); |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 230 | fixHazards(MI); |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 231 | CurrCycleInstr = nullptr; |
| 232 | return W; |
| 233 | } |
| 234 | |
| 235 | unsigned GCNHazardRecognizer::PreEmitNoopsCommon(MachineInstr *MI) { |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 236 | if (MI->isBundle()) |
| 237 | return 0; |
| 238 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 239 | int WaitStates = std::max(0, checkAnyInstHazards(MI)); |
| 240 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 241 | if (SIInstrInfo::isSMRD(*MI)) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 242 | return std::max(WaitStates, checkSMRDHazards(MI)); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 243 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 244 | if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI)) |
| 245 | WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 246 | |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 247 | if (ST.hasNSAtoVMEMBug()) |
| 248 | WaitStates = std::max(WaitStates, checkNSAtoVMEMHazard(MI)); |
| 249 | |
| 250 | if (ST.hasNoDataDepHazard()) |
| 251 | return WaitStates; |
| 252 | |
| 253 | if (SIInstrInfo::isVALU(*MI)) |
| 254 | WaitStates = std::max(WaitStates, checkVALUHazards(MI)); |
| 255 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 256 | if (SIInstrInfo::isDPP(*MI)) |
| 257 | WaitStates = std::max(WaitStates, checkDPPHazards(MI)); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 258 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 259 | if (isDivFMas(MI->getOpcode())) |
| 260 | WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 261 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 262 | if (isRWLane(MI->getOpcode())) |
| 263 | WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 264 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 265 | if (MI->isInlineAsm()) |
| 266 | return std::max(WaitStates, checkInlineAsmHazards(MI)); |
| 267 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 268 | if (isSGetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 269 | return std::max(WaitStates, checkGetRegHazards(MI)); |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 270 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 271 | if (isSSetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 272 | return std::max(WaitStates, checkSetRegHazards(MI)); |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 273 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 274 | if (isRFE(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 275 | return std::max(WaitStates, checkRFEHazards(MI)); |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 276 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 277 | if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) || |
| 278 | isSMovRel(MI->getOpcode()))) |
| 279 | return std::max(WaitStates, checkReadM0Hazards(MI)); |
| 280 | |
Marek Olsak | c5cec5e | 2019-01-16 15:43:53 +0000 | [diff] [blame] | 281 | if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI)) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 282 | return std::max(WaitStates, checkReadM0Hazards(MI)); |
| 283 | |
| 284 | return WaitStates; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void GCNHazardRecognizer::EmitNoop() { |
| 288 | EmittedInstrs.push_front(nullptr); |
| 289 | } |
| 290 | |
| 291 | void GCNHazardRecognizer::AdvanceCycle() { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 292 | // When the scheduler detects a stall, it will call AdvanceCycle() without |
| 293 | // emitting any instructions. |
| 294 | if (!CurrCycleInstr) |
| 295 | return; |
| 296 | |
Carl Ritson | f898edd | 2018-09-10 10:14:48 +0000 | [diff] [blame] | 297 | // Do not track non-instructions which do not affect the wait states. |
| 298 | // If included, these instructions can lead to buffer overflow such that |
| 299 | // detectable hazards are missed. |
David Stuttard | 81eec58 | 2019-03-05 10:25:16 +0000 | [diff] [blame] | 300 | if (CurrCycleInstr->isImplicitDef() || CurrCycleInstr->isDebugInstr() || |
| 301 | CurrCycleInstr->isKill()) |
Carl Ritson | f898edd | 2018-09-10 10:14:48 +0000 | [diff] [blame] | 302 | return; |
| 303 | |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 304 | if (CurrCycleInstr->isBundle()) { |
| 305 | processBundle(); |
| 306 | return; |
| 307 | } |
| 308 | |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 309 | unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 310 | |
| 311 | // Keep track of emitted instructions |
| 312 | EmittedInstrs.push_front(CurrCycleInstr); |
| 313 | |
| 314 | // Add a nullptr for each additional wait state after the first. Make sure |
| 315 | // not to add more than getMaxLookAhead() items to the list, since we |
| 316 | // truncate the list to that size right after this loop. |
| 317 | for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); |
| 318 | i < e; ++i) { |
| 319 | EmittedInstrs.push_front(nullptr); |
| 320 | } |
| 321 | |
| 322 | // getMaxLookahead() is the largest number of wait states we will ever need |
| 323 | // to insert, so there is no point in keeping track of more than that many |
| 324 | // wait states. |
| 325 | EmittedInstrs.resize(getMaxLookAhead()); |
| 326 | |
| 327 | CurrCycleInstr = nullptr; |
| 328 | } |
| 329 | |
| 330 | void GCNHazardRecognizer::RecedeCycle() { |
| 331 | llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); |
| 332 | } |
| 333 | |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | // Helper Functions |
| 336 | //===----------------------------------------------------------------------===// |
| 337 | |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 338 | typedef function_ref<bool(MachineInstr *, int WaitStates)> IsExpiredFn; |
| 339 | |
| 340 | // Returns a minimum wait states since \p I walking all predecessors. |
| 341 | // Only scans until \p IsExpired does not return true. |
| 342 | // Can only be run in a hazard recognizer mode. |
| 343 | static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, |
| 344 | MachineBasicBlock *MBB, |
| 345 | MachineBasicBlock::reverse_instr_iterator I, |
| 346 | int WaitStates, |
| 347 | IsExpiredFn IsExpired, |
| 348 | DenseSet<const MachineBasicBlock *> &Visited) { |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 349 | for (auto E = MBB->instr_rend(); I != E; ++I) { |
| 350 | // Don't add WaitStates for parent BUNDLE instructions. |
| 351 | if (I->isBundle()) |
| 352 | continue; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 353 | |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 354 | if (IsHazard(&*I)) |
| 355 | return WaitStates; |
| 356 | |
| 357 | if (I->isInlineAsm() || I->isImplicitDef() || I->isDebugInstr()) |
| 358 | continue; |
| 359 | |
| 360 | WaitStates += SIInstrInfo::getNumWaitStates(*I); |
| 361 | |
| 362 | if (IsExpired(&*I, WaitStates)) |
| 363 | return std::numeric_limits<int>::max(); |
| 364 | } |
| 365 | |
| 366 | int MinWaitStates = WaitStates; |
| 367 | bool Found = false; |
| 368 | for (MachineBasicBlock *Pred : MBB->predecessors()) { |
| 369 | if (!Visited.insert(Pred).second) |
| 370 | continue; |
| 371 | |
| 372 | int W = getWaitStatesSince(IsHazard, Pred, Pred->instr_rbegin(), |
| 373 | WaitStates, IsExpired, Visited); |
| 374 | |
| 375 | if (W == std::numeric_limits<int>::max()) |
| 376 | continue; |
| 377 | |
| 378 | MinWaitStates = Found ? std::min(MinWaitStates, W) : W; |
| 379 | if (IsExpired(nullptr, MinWaitStates)) |
| 380 | return MinWaitStates; |
| 381 | |
| 382 | Found = true; |
| 383 | } |
| 384 | |
| 385 | if (Found) |
| 386 | return MinWaitStates; |
| 387 | |
| 388 | return std::numeric_limits<int>::max(); |
| 389 | } |
| 390 | |
| 391 | static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard, |
| 392 | MachineInstr *MI, |
| 393 | IsExpiredFn IsExpired) { |
| 394 | DenseSet<const MachineBasicBlock *> Visited; |
| 395 | return getWaitStatesSince(IsHazard, MI->getParent(), |
| 396 | std::next(MI->getReverseIterator()), |
| 397 | 0, IsExpired, Visited); |
| 398 | } |
| 399 | |
| 400 | int GCNHazardRecognizer::getWaitStatesSince(IsHazardFn IsHazard, int Limit) { |
| 401 | if (IsHazardRecognizerMode) { |
| 402 | auto IsExpiredFn = [Limit] (MachineInstr *, int WaitStates) { |
| 403 | return WaitStates >= Limit; |
| 404 | }; |
| 405 | return ::getWaitStatesSince(IsHazard, CurrCycleInstr, IsExpiredFn); |
| 406 | } |
| 407 | |
Nicolai Haehnle | 75c98c3 | 2017-09-01 16:56:32 +0000 | [diff] [blame] | 408 | int WaitStates = 0; |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 409 | for (MachineInstr *MI : EmittedInstrs) { |
Nicolai Haehnle | 75c98c3 | 2017-09-01 16:56:32 +0000 | [diff] [blame] | 410 | if (MI) { |
| 411 | if (IsHazard(MI)) |
| 412 | return WaitStates; |
| 413 | |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 414 | if (MI->isInlineAsm()) |
Nicolai Haehnle | 75c98c3 | 2017-09-01 16:56:32 +0000 | [diff] [blame] | 415 | continue; |
| 416 | } |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 417 | ++WaitStates; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 418 | |
| 419 | if (WaitStates >= Limit) |
| 420 | break; |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 421 | } |
| 422 | return std::numeric_limits<int>::max(); |
| 423 | } |
| 424 | |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 425 | int GCNHazardRecognizer::getWaitStatesSinceDef(unsigned Reg, |
| 426 | IsHazardFn IsHazardDef, |
| 427 | int Limit) { |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 428 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 429 | |
| 430 | auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) { |
| 431 | return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI); |
| 432 | }; |
| 433 | |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 434 | return getWaitStatesSince(IsHazardFn, Limit); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 437 | int GCNHazardRecognizer::getWaitStatesSinceSetReg(IsHazardFn IsHazard, |
| 438 | int Limit) { |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 439 | auto IsHazardFn = [IsHazard] (MachineInstr *MI) { |
| 440 | return isSSetReg(MI->getOpcode()) && IsHazard(MI); |
| 441 | }; |
| 442 | |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 443 | return getWaitStatesSince(IsHazardFn, Limit); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 446 | //===----------------------------------------------------------------------===// |
| 447 | // No-op Hazard Detection |
| 448 | //===----------------------------------------------------------------------===// |
| 449 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 450 | static void addRegUnits(const SIRegisterInfo &TRI, |
| 451 | BitVector &BV, unsigned Reg) { |
| 452 | for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) |
| 453 | BV.set(*RUI); |
| 454 | } |
| 455 | |
| 456 | static void addRegsToSet(const SIRegisterInfo &TRI, |
| 457 | iterator_range<MachineInstr::const_mop_iterator> Ops, |
| 458 | BitVector &Set) { |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 459 | for (const MachineOperand &Op : Ops) { |
| 460 | if (Op.isReg()) |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 461 | addRegUnits(TRI, Set, Op.getReg()); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 465 | void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) { |
| 466 | // XXX: Do we need to worry about implicit operands |
| 467 | addRegsToSet(TRI, MI.defs(), ClauseDefs); |
| 468 | addRegsToSet(TRI, MI.uses(), ClauseUses); |
| 469 | } |
| 470 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 471 | int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) { |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 472 | // SMEM soft clause are only present on VI+, and only matter if xnack is |
| 473 | // enabled. |
| 474 | if (!ST.isXNACKEnabled()) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 475 | return 0; |
| 476 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 477 | bool IsSMRD = TII.isSMRD(*MEM); |
| 478 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 479 | resetClause(); |
| 480 | |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 481 | // A soft-clause is any group of consecutive SMEM instructions. The |
| 482 | // instructions in this group may return out of order and/or may be |
| 483 | // replayed (i.e. the same instruction issued more than once). |
| 484 | // |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 485 | // In order to handle these situations correctly we need to make sure that |
| 486 | // when a clause has more than one instruction, no instruction in the clause |
| 487 | // writes to a register that is read by another instruction in the clause |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 488 | // (including itself). If we encounter this situaion, we need to break the |
| 489 | // clause by inserting a non SMEM instruction. |
| 490 | |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 491 | for (MachineInstr *MI : EmittedInstrs) { |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 492 | // When we hit a non-SMEM instruction then we have passed the start of the |
| 493 | // clause and we can stop. |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 494 | if (!MI) |
| 495 | break; |
| 496 | |
| 497 | if (IsSMRD != SIInstrInfo::isSMRD(*MI)) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 498 | break; |
| 499 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 500 | addClauseInst(*MI); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 503 | if (ClauseDefs.none()) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 504 | return 0; |
| 505 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 506 | // We need to make sure not to put loads and stores in the same clause if they |
| 507 | // use the same address. For now, just start a new clause whenever we see a |
| 508 | // store. |
| 509 | if (MEM->mayStore()) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 510 | return 1; |
| 511 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 512 | addClauseInst(*MEM); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 513 | |
| 514 | // If the set of defs and uses intersect then we cannot add this instruction |
| 515 | // to the clause, so we have a hazard. |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 516 | return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0; |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 519 | int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 520 | int WaitStatesNeeded = 0; |
| 521 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 522 | WaitStatesNeeded = checkSoftClauseHazards(SMRD); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 523 | |
| 524 | // This SMRD hazard only affects SI. |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 525 | if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 526 | return WaitStatesNeeded; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 527 | |
| 528 | // A read of an SGPR by SMRD instruction requires 4 wait states when the |
| 529 | // SGPR was written by a VALU instruction. |
| 530 | int SmrdSgprWaitStates = 4; |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 531 | auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; |
Marek Olsak | 2232243 | 2017-10-26 14:43:02 +0000 | [diff] [blame] | 532 | auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); }; |
| 533 | |
Matt Arsenault | 4512d0a | 2017-11-17 04:18:26 +0000 | [diff] [blame] | 534 | bool IsBufferSMRD = TII.isBufferSMRD(*SMRD); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 535 | |
| 536 | for (const MachineOperand &Use : SMRD->uses()) { |
| 537 | if (!Use.isReg()) |
| 538 | continue; |
| 539 | int WaitStatesNeededForUse = |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 540 | SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, |
| 541 | SmrdSgprWaitStates); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 542 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
Marek Olsak | 2232243 | 2017-10-26 14:43:02 +0000 | [diff] [blame] | 543 | |
| 544 | // This fixes what appears to be undocumented hardware behavior in SI where |
| 545 | // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor |
| 546 | // needs some number of nops in between. We don't know how many we need, but |
| 547 | // let's use 4. This wasn't discovered before probably because the only |
| 548 | // case when this happens is when we expand a 64-bit pointer into a full |
| 549 | // descriptor and use s_buffer_load_dword instead of s_load_dword, which was |
| 550 | // probably never encountered in the closed-source land. |
| 551 | if (IsBufferSMRD) { |
| 552 | int WaitStatesNeededForUse = |
| 553 | SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 554 | IsBufferHazardDefFn, |
| 555 | SmrdSgprWaitStates); |
Marek Olsak | 2232243 | 2017-10-26 14:43:02 +0000 | [diff] [blame] | 556 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 557 | } |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 558 | } |
Marek Olsak | 2232243 | 2017-10-26 14:43:02 +0000 | [diff] [blame] | 559 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 560 | return WaitStatesNeeded; |
| 561 | } |
| 562 | |
| 563 | int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 564 | if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 565 | return 0; |
| 566 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 567 | int WaitStatesNeeded = checkSoftClauseHazards(VMEM); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 568 | |
| 569 | // A read of an SGPR by a VMEM instruction requires 5 wait states when the |
| 570 | // SGPR was written by a VALU Instruction. |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 571 | const int VmemSgprWaitStates = 5; |
| 572 | auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 573 | for (const MachineOperand &Use : VMEM->uses()) { |
| 574 | if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg())) |
| 575 | continue; |
| 576 | |
| 577 | int WaitStatesNeededForUse = |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 578 | VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn, |
| 579 | VmemSgprWaitStates); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 580 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 581 | } |
| 582 | return WaitStatesNeeded; |
| 583 | } |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 584 | |
| 585 | int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 586 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 587 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 588 | |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 589 | // Check for DPP VGPR read after VALU VGPR write and EXEC write. |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 590 | int DppVgprWaitStates = 2; |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 591 | int DppExecWaitStates = 5; |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 592 | int WaitStatesNeeded = 0; |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 593 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 594 | |
| 595 | for (const MachineOperand &Use : DPP->uses()) { |
| 596 | if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 597 | continue; |
| 598 | int WaitStatesNeededForUse = |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 599 | DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg(), |
| 600 | [](MachineInstr *) { return true; }, |
| 601 | DppVgprWaitStates); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 602 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 603 | } |
| 604 | |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 605 | WaitStatesNeeded = std::max( |
| 606 | WaitStatesNeeded, |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 607 | DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn, |
| 608 | DppExecWaitStates)); |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 609 | |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 610 | return WaitStatesNeeded; |
| 611 | } |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 612 | |
| 613 | int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { |
| 614 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 615 | |
| 616 | // v_div_fmas requires 4 wait states after a write to vcc from a VALU |
| 617 | // instruction. |
| 618 | const int DivFMasWaitStates = 4; |
| 619 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 620 | int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn, |
| 621 | DivFMasWaitStates); |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 622 | |
| 623 | return DivFMasWaitStates - WaitStatesNeeded; |
| 624 | } |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 625 | |
| 626 | int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { |
| 627 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 628 | unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); |
| 629 | |
| 630 | const int GetRegWaitStates = 2; |
| 631 | auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) { |
| 632 | return GetRegHWReg == getHWReg(TII, *MI); |
| 633 | }; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 634 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates); |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 635 | |
| 636 | return GetRegWaitStates - WaitStatesNeeded; |
| 637 | } |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 638 | |
| 639 | int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { |
| 640 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 641 | unsigned HWReg = getHWReg(TII, *SetRegInstr); |
| 642 | |
| 643 | const int SetRegWaitStates = |
| 644 | ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2; |
| 645 | auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) { |
| 646 | return HWReg == getHWReg(TII, *MI); |
| 647 | }; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 648 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates); |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 649 | return SetRegWaitStates - WaitStatesNeeded; |
| 650 | } |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 651 | |
| 652 | int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { |
| 653 | if (!MI.mayStore()) |
| 654 | return -1; |
| 655 | |
| 656 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 657 | unsigned Opcode = MI.getOpcode(); |
| 658 | const MCInstrDesc &Desc = MI.getDesc(); |
| 659 | |
| 660 | int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
| 661 | int VDataRCID = -1; |
| 662 | if (VDataIdx != -1) |
| 663 | VDataRCID = Desc.OpInfo[VDataIdx].RegClass; |
| 664 | |
| 665 | if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { |
Jan Vesely | e8cc395 | 2016-11-15 23:55:15 +0000 | [diff] [blame] | 666 | // There is no hazard if the instruction does not use vector regs |
| 667 | // (like wbinvl1) |
| 668 | if (VDataIdx == -1) |
| 669 | return -1; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 670 | // For MUBUF/MTBUF instructions this hazard only exists if the |
| 671 | // instruction is not using a register in the soffset field. |
| 672 | const MachineOperand *SOffset = |
| 673 | TII->getNamedOperand(MI, AMDGPU::OpName::soffset); |
| 674 | // If we have no soffset operand, then assume this field has been |
| 675 | // hardcoded to zero. |
| 676 | if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && |
| 677 | (!SOffset || !SOffset->isReg())) |
| 678 | return VDataIdx; |
| 679 | } |
| 680 | |
| 681 | // MIMG instructions create a hazard if they don't use a 256-bit T# and |
| 682 | // the store size is greater than 8 bytes and they have more than two bits |
| 683 | // of their dmask set. |
| 684 | // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. |
| 685 | if (TII->isMIMG(MI)) { |
| 686 | int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); |
| 687 | assert(SRsrcIdx != -1 && |
| 688 | AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); |
Tom Stellard | 6b9c1be | 2016-10-27 23:28:03 +0000 | [diff] [blame] | 689 | (void)SRsrcIdx; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | if (TII->isFLAT(MI)) { |
Matt Arsenault | 97279a8 | 2016-11-29 19:30:44 +0000 | [diff] [blame] | 693 | int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 694 | if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) |
| 695 | return DataIdx; |
| 696 | } |
| 697 | |
| 698 | return -1; |
| 699 | } |
| 700 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 701 | int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def, |
| 702 | const MachineRegisterInfo &MRI) { |
| 703 | // Helper to check for the hazard where VMEM instructions that store more than |
| 704 | // 8 bytes can have there store data over written by the next instruction. |
| 705 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 706 | |
| 707 | const int VALUWaitStates = 1; |
| 708 | int WaitStatesNeeded = 0; |
| 709 | |
| 710 | if (!TRI->isVGPR(MRI, Def.getReg())) |
| 711 | return WaitStatesNeeded; |
| 712 | unsigned Reg = Def.getReg(); |
| 713 | auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) { |
| 714 | int DataIdx = createsVALUHazard(*MI); |
| 715 | return DataIdx >= 0 && |
| 716 | TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg); |
| 717 | }; |
| 718 | int WaitStatesNeededForDef = |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 719 | VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates); |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 720 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); |
| 721 | |
| 722 | return WaitStatesNeeded; |
| 723 | } |
| 724 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 725 | int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { |
| 726 | // This checks for the hazard where VMEM instructions that store more than |
| 727 | // 8 bytes can have there store data over written by the next instruction. |
| 728 | if (!ST.has12DWordStoreHazard()) |
| 729 | return 0; |
| 730 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 731 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 732 | int WaitStatesNeeded = 0; |
| 733 | |
| 734 | for (const MachineOperand &Def : VALU->defs()) { |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 735 | WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI)); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 736 | } |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 737 | |
| 738 | return WaitStatesNeeded; |
| 739 | } |
| 740 | |
| 741 | int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) { |
| 742 | // This checks for hazards associated with inline asm statements. |
| 743 | // Since inline asms can contain just about anything, we use this |
| 744 | // to call/leverage other check*Hazard routines. Note that |
| 745 | // this function doesn't attempt to address all possible inline asm |
| 746 | // hazards (good luck), but is a collection of what has been |
| 747 | // problematic thus far. |
| 748 | |
| 749 | // see checkVALUHazards() |
| 750 | if (!ST.has12DWordStoreHazard()) |
| 751 | return 0; |
| 752 | |
| 753 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 754 | int WaitStatesNeeded = 0; |
| 755 | |
| 756 | for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands(); |
| 757 | I != E; ++I) { |
| 758 | const MachineOperand &Op = IA->getOperand(I); |
| 759 | if (Op.isReg() && Op.isDef()) { |
| 760 | WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI)); |
| 761 | } |
| 762 | } |
| 763 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 764 | return WaitStatesNeeded; |
| 765 | } |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 766 | |
| 767 | int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { |
| 768 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 769 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 770 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 771 | |
| 772 | const MachineOperand *LaneSelectOp = |
| 773 | TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); |
| 774 | |
| 775 | if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) |
| 776 | return 0; |
| 777 | |
| 778 | unsigned LaneSelectReg = LaneSelectOp->getReg(); |
| 779 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 780 | return TII->isVALU(*MI); |
| 781 | }; |
| 782 | |
| 783 | const int RWLaneWaitStates = 4; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 784 | int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn, |
| 785 | RWLaneWaitStates); |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 786 | return RWLaneWaitStates - WaitStatesSince; |
| 787 | } |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 788 | |
| 789 | int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 790 | if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 791 | return 0; |
| 792 | |
| 793 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 794 | |
| 795 | const int RFEWaitStates = 1; |
| 796 | |
| 797 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 798 | return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS; |
| 799 | }; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 800 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates); |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 801 | return RFEWaitStates - WaitStatesNeeded; |
| 802 | } |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 803 | |
| 804 | int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 805 | if (MI->isDebugInstr()) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 806 | return 0; |
| 807 | |
| 808 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 809 | if (!ST.hasSMovFedHazard()) |
| 810 | return 0; |
| 811 | |
| 812 | // Check for any instruction reading an SGPR after a write from |
| 813 | // s_mov_fed_b32. |
| 814 | int MovFedWaitStates = 1; |
| 815 | int WaitStatesNeeded = 0; |
| 816 | |
| 817 | for (const MachineOperand &Use : MI->uses()) { |
| 818 | if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 819 | continue; |
| 820 | auto IsHazardFn = [] (MachineInstr *MI) { |
| 821 | return MI->getOpcode() == AMDGPU::S_MOV_FED_B32; |
| 822 | }; |
| 823 | int WaitStatesNeededForUse = |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 824 | MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn, |
| 825 | MovFedWaitStates); |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 826 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 827 | } |
| 828 | |
| 829 | return WaitStatesNeeded; |
| 830 | } |
| 831 | |
| 832 | int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 833 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 834 | const int SMovRelWaitStates = 1; |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 835 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 836 | return TII->isSALU(*MI); |
| 837 | }; |
Stanislav Mekhanoshin | f92ed69 | 2019-01-21 19:11:26 +0000 | [diff] [blame] | 838 | return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn, |
| 839 | SMovRelWaitStates); |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 840 | } |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 841 | |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 842 | void GCNHazardRecognizer::fixHazards(MachineInstr *MI) { |
| 843 | fixVMEMtoScalarWriteHazards(MI); |
Stanislav Mekhanoshin | 5f581c9 | 2019-06-12 17:52:51 +0000 | [diff] [blame^] | 844 | fixVcmpxPermlaneHazards(MI); |
Austin Kerbow | 8a3d3a9 | 2019-05-07 22:12:15 +0000 | [diff] [blame] | 845 | fixSMEMtoVectorWriteHazards(MI); |
| 846 | fixVcmpxExecWARHazard(MI); |
| 847 | fixLdsBranchVmemWARHazard(MI); |
| 848 | } |
| 849 | |
Stanislav Mekhanoshin | 5f581c9 | 2019-06-12 17:52:51 +0000 | [diff] [blame^] | 850 | bool GCNHazardRecognizer::fixVcmpxPermlaneHazards(MachineInstr *MI) { |
| 851 | if (!ST.hasVcmpxPermlaneHazard() || !isPermlane(*MI)) |
| 852 | return false; |
| 853 | |
| 854 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 855 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 856 | return TII->isVOPC(*MI); |
| 857 | }; |
| 858 | |
| 859 | auto IsExpiredFn = [] (MachineInstr *MI, int) { |
| 860 | if (!MI) |
| 861 | return false; |
| 862 | unsigned Opc = MI->getOpcode(); |
| 863 | return SIInstrInfo::isVALU(*MI) && |
| 864 | Opc != AMDGPU::V_NOP_e32 && |
| 865 | Opc != AMDGPU::V_NOP_e64 && |
| 866 | Opc != AMDGPU::V_NOP_sdwa; |
| 867 | }; |
| 868 | |
| 869 | if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == |
| 870 | std::numeric_limits<int>::max()) |
| 871 | return false; |
| 872 | |
| 873 | // V_NOP will be discarded by SQ. |
| 874 | // Use V_MOB_B32 v?, v?. Register must be alive so use src0 of V_PERMLANE* |
| 875 | // which is always a VGPR and available. |
| 876 | auto *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0); |
| 877 | unsigned Reg = Src0->getReg(); |
| 878 | bool IsUndef = Src0->isUndef(); |
| 879 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), |
| 880 | TII->get(AMDGPU::V_MOV_B32_e32)) |
| 881 | .addReg(Reg, RegState::Define | (IsUndef ? RegState::Dead : 0)) |
| 882 | .addReg(Reg, IsUndef ? RegState::Undef : RegState::Kill); |
| 883 | |
| 884 | return true; |
| 885 | } |
| 886 | |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 887 | bool GCNHazardRecognizer::fixVMEMtoScalarWriteHazards(MachineInstr *MI) { |
| 888 | if (!ST.hasVMEMtoScalarWriteHazard()) |
| 889 | return false; |
| 890 | |
| 891 | if (!SIInstrInfo::isSALU(*MI) && !SIInstrInfo::isSMRD(*MI)) |
| 892 | return false; |
| 893 | |
| 894 | if (MI->getNumDefs() == 0) |
| 895 | return false; |
| 896 | |
| 897 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 898 | |
| 899 | auto IsHazardFn = [TRI, MI] (MachineInstr *I) { |
| 900 | if (!SIInstrInfo::isVMEM(*I) && !SIInstrInfo::isDS(*I) && |
| 901 | !SIInstrInfo::isFLAT(*I)) |
| 902 | return false; |
| 903 | |
| 904 | for (const MachineOperand &Def : MI->defs()) { |
| 905 | MachineOperand *Op = I->findRegisterUseOperand(Def.getReg(), false, TRI); |
| 906 | if (!Op || (Op->isImplicit() && Op->getReg() == AMDGPU::EXEC)) |
| 907 | continue; |
| 908 | return true; |
| 909 | } |
| 910 | return false; |
| 911 | }; |
| 912 | |
| 913 | auto IsExpiredFn = [] (MachineInstr *MI, int) { |
| 914 | return MI && (SIInstrInfo::isVALU(*MI) || |
| 915 | (MI->getOpcode() == AMDGPU::S_WAITCNT && |
| 916 | !MI->getOperand(0).getImm())); |
| 917 | }; |
| 918 | |
| 919 | if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == |
| 920 | std::numeric_limits<int>::max()) |
| 921 | return false; |
| 922 | |
| 923 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 924 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII->get(AMDGPU::V_NOP_e32)); |
| 925 | return true; |
| 926 | } |
| 927 | |
| 928 | bool GCNHazardRecognizer::fixSMEMtoVectorWriteHazards(MachineInstr *MI) { |
| 929 | if (!ST.hasSMEMtoVectorWriteHazard()) |
| 930 | return false; |
| 931 | |
| 932 | if (!SIInstrInfo::isVALU(*MI)) |
| 933 | return false; |
| 934 | |
| 935 | unsigned SDSTName; |
| 936 | switch (MI->getOpcode()) { |
| 937 | case AMDGPU::V_READLANE_B32: |
| 938 | case AMDGPU::V_READFIRSTLANE_B32: |
| 939 | SDSTName = AMDGPU::OpName::vdst; |
| 940 | break; |
| 941 | default: |
| 942 | SDSTName = AMDGPU::OpName::sdst; |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 947 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Carl Ritson | 34e95ce | 2019-05-20 07:20:12 +0000 | [diff] [blame] | 948 | const AMDGPU::IsaVersion IV = AMDGPU::getIsaVersion(ST.getCPU()); |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 949 | const MachineOperand *SDST = TII->getNamedOperand(*MI, SDSTName); |
| 950 | if (!SDST) { |
Stanislav Mekhanoshin | 5ddd564 | 2019-05-04 06:40:20 +0000 | [diff] [blame] | 951 | for (const auto &MO : MI->implicit_operands()) { |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 952 | if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg()))) { |
| 953 | SDST = &MO; |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (!SDST) |
| 960 | return false; |
| 961 | |
| 962 | const unsigned SDSTReg = SDST->getReg(); |
| 963 | auto IsHazardFn = [SDSTReg, TRI] (MachineInstr *I) { |
| 964 | return SIInstrInfo::isSMRD(*I) && I->readsRegister(SDSTReg, TRI); |
| 965 | }; |
| 966 | |
Carl Ritson | 34e95ce | 2019-05-20 07:20:12 +0000 | [diff] [blame] | 967 | auto IsExpiredFn = [TII, IV] (MachineInstr *MI, int) { |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 968 | if (MI) { |
| 969 | if (TII->isSALU(*MI)) { |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 970 | switch (MI->getOpcode()) { |
| 971 | case AMDGPU::S_SETVSKIP: |
| 972 | case AMDGPU::S_VERSION: |
| 973 | case AMDGPU::S_WAITCNT_VSCNT: |
| 974 | case AMDGPU::S_WAITCNT_VMCNT: |
| 975 | case AMDGPU::S_WAITCNT_EXPCNT: |
Carl Ritson | 34e95ce | 2019-05-20 07:20:12 +0000 | [diff] [blame] | 976 | // These instructions cannot not mitigate the hazard. |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 977 | return false; |
Carl Ritson | 34e95ce | 2019-05-20 07:20:12 +0000 | [diff] [blame] | 978 | case AMDGPU::S_WAITCNT_LGKMCNT: |
| 979 | // Reducing lgkmcnt count to 0 always mitigates the hazard. |
| 980 | return (MI->getOperand(1).getImm() == 0) && |
| 981 | (MI->getOperand(0).getReg() == AMDGPU::SGPR_NULL); |
| 982 | case AMDGPU::S_WAITCNT: { |
| 983 | const int64_t Imm = MI->getOperand(0).getImm(); |
| 984 | AMDGPU::Waitcnt Decoded = AMDGPU::decodeWaitcnt(IV, Imm); |
| 985 | return (Decoded.LgkmCnt == 0); |
| 986 | } |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 987 | default: |
Carl Ritson | 34e95ce | 2019-05-20 07:20:12 +0000 | [diff] [blame] | 988 | // SOPP instructions cannot mitigate the hazard. |
| 989 | if (TII->isSOPP(*MI)) |
| 990 | return false; |
| 991 | // At this point the SALU can be assumed to mitigate the hazard |
| 992 | // because either: |
| 993 | // (a) it is independent of the at risk SMEM (breaking chain), |
| 994 | // or |
| 995 | // (b) it is dependent on the SMEM, in which case an appropriate |
| 996 | // s_waitcnt lgkmcnt _must_ exist between it and the at risk |
| 997 | // SMEM instruction. |
Stanislav Mekhanoshin | 51d1415 | 2019-05-04 04:30:57 +0000 | [diff] [blame] | 998 | return true; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | return false; |
| 1003 | }; |
| 1004 | |
| 1005 | if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == |
| 1006 | std::numeric_limits<int>::max()) |
| 1007 | return false; |
| 1008 | |
| 1009 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), |
| 1010 | TII->get(AMDGPU::S_MOV_B32), AMDGPU::SGPR_NULL) |
| 1011 | .addImm(0); |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | bool GCNHazardRecognizer::fixVcmpxExecWARHazard(MachineInstr *MI) { |
| 1016 | if (!ST.hasVcmpxExecWARHazard() || !SIInstrInfo::isVALU(*MI)) |
| 1017 | return false; |
| 1018 | |
| 1019 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 1020 | if (!MI->modifiesRegister(AMDGPU::EXEC, TRI)) |
| 1021 | return false; |
| 1022 | |
| 1023 | auto IsHazardFn = [TRI] (MachineInstr *I) { |
| 1024 | if (SIInstrInfo::isVALU(*I)) |
| 1025 | return false; |
| 1026 | return I->readsRegister(AMDGPU::EXEC, TRI); |
| 1027 | }; |
| 1028 | |
| 1029 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 1030 | auto IsExpiredFn = [TII, TRI] (MachineInstr *MI, int) { |
| 1031 | if (!MI) |
| 1032 | return false; |
| 1033 | if (SIInstrInfo::isVALU(*MI)) { |
| 1034 | if (TII->getNamedOperand(*MI, AMDGPU::OpName::sdst)) |
| 1035 | return true; |
| 1036 | for (auto MO : MI->implicit_operands()) |
| 1037 | if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg()))) |
| 1038 | return true; |
| 1039 | } |
| 1040 | if (MI->getOpcode() == AMDGPU::S_WAITCNT_DEPCTR && |
| 1041 | (MI->getOperand(0).getImm() & 0xfffe) == 0xfffe) |
| 1042 | return true; |
| 1043 | return false; |
| 1044 | }; |
| 1045 | |
| 1046 | if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == |
| 1047 | std::numeric_limits<int>::max()) |
| 1048 | return false; |
| 1049 | |
| 1050 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), |
| 1051 | TII->get(AMDGPU::S_WAITCNT_DEPCTR)) |
| 1052 | .addImm(0xfffe); |
| 1053 | return true; |
| 1054 | } |
| 1055 | |
| 1056 | bool GCNHazardRecognizer::fixLdsBranchVmemWARHazard(MachineInstr *MI) { |
| 1057 | if (!ST.hasLdsBranchVmemWARHazard()) |
| 1058 | return false; |
| 1059 | |
| 1060 | auto IsHazardInst = [] (const MachineInstr *MI) { |
| 1061 | if (SIInstrInfo::isDS(*MI)) |
| 1062 | return 1; |
| 1063 | if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isSegmentSpecificFLAT(*MI)) |
| 1064 | return 2; |
| 1065 | return 0; |
| 1066 | }; |
| 1067 | |
| 1068 | auto InstType = IsHazardInst(MI); |
| 1069 | if (!InstType) |
| 1070 | return false; |
| 1071 | |
| 1072 | auto IsExpiredFn = [&IsHazardInst] (MachineInstr *I, int) { |
| 1073 | return I && (IsHazardInst(I) || |
| 1074 | (I->getOpcode() == AMDGPU::S_WAITCNT_VSCNT && |
| 1075 | I->getOperand(0).getReg() == AMDGPU::SGPR_NULL && |
| 1076 | !I->getOperand(1).getImm())); |
| 1077 | }; |
| 1078 | |
| 1079 | auto IsHazardFn = [InstType, &IsHazardInst] (MachineInstr *I) { |
| 1080 | if (!I->isBranch()) |
| 1081 | return false; |
| 1082 | |
| 1083 | auto IsHazardFn = [InstType, IsHazardInst] (MachineInstr *I) { |
| 1084 | auto InstType2 = IsHazardInst(I); |
| 1085 | return InstType2 && InstType != InstType2; |
| 1086 | }; |
| 1087 | |
| 1088 | auto IsExpiredFn = [InstType, &IsHazardInst] (MachineInstr *I, int) { |
| 1089 | if (!I) |
| 1090 | return false; |
| 1091 | |
| 1092 | auto InstType2 = IsHazardInst(I); |
| 1093 | if (InstType == InstType2) |
| 1094 | return true; |
| 1095 | |
| 1096 | return I->getOpcode() == AMDGPU::S_WAITCNT_VSCNT && |
| 1097 | I->getOperand(0).getReg() == AMDGPU::SGPR_NULL && |
| 1098 | !I->getOperand(1).getImm(); |
| 1099 | }; |
| 1100 | |
| 1101 | return ::getWaitStatesSince(IsHazardFn, I, IsExpiredFn) != |
| 1102 | std::numeric_limits<int>::max(); |
| 1103 | }; |
| 1104 | |
| 1105 | if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) == |
| 1106 | std::numeric_limits<int>::max()) |
| 1107 | return false; |
| 1108 | |
| 1109 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 1110 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), |
| 1111 | TII->get(AMDGPU::S_WAITCNT_VSCNT)) |
| 1112 | .addReg(AMDGPU::SGPR_NULL, RegState::Undef) |
| 1113 | .addImm(0); |
| 1114 | |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
| 1118 | int GCNHazardRecognizer::checkNSAtoVMEMHazard(MachineInstr *MI) { |
| 1119 | int NSAtoVMEMWaitStates = 1; |
| 1120 | |
| 1121 | if (!ST.hasNSAtoVMEMBug()) |
| 1122 | return 0; |
| 1123 | |
| 1124 | if (!SIInstrInfo::isMUBUF(*MI) && !SIInstrInfo::isMTBUF(*MI)) |
| 1125 | return 0; |
| 1126 | |
| 1127 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 1128 | const auto *Offset = TII->getNamedOperand(*MI, AMDGPU::OpName::offset); |
| 1129 | if (!Offset || (Offset->getImm() & 6) == 0) |
| 1130 | return 0; |
| 1131 | |
| 1132 | auto IsHazardFn = [TII] (MachineInstr *I) { |
| 1133 | if (!SIInstrInfo::isMIMG(*I)) |
| 1134 | return false; |
| 1135 | const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(I->getOpcode()); |
| 1136 | return Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA && |
| 1137 | TII->getInstSizeInBytes(*I) >= 16; |
| 1138 | }; |
| 1139 | |
| 1140 | return NSAtoVMEMWaitStates - getWaitStatesSince(IsHazardFn, 1); |
| 1141 | } |