Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 1 | //===- SIInsertWaitcnts.cpp - Insert Wait Instructions --------------------===// |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// Insert wait instructions for memory reads and writes. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 12 | /// |
| 13 | /// Memory reads and writes are issued asynchronously, so we need to insert |
| 14 | /// S_WAITCNT instructions when we want to access any of their results or |
| 15 | /// overwrite any register that's used asynchronously. |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 16 | /// |
| 17 | /// TODO: This pass currently keeps one timeline per hardware counter. A more |
| 18 | /// finely-grained approach that keeps one timeline per event type could |
| 19 | /// sometimes get away with generating weaker s_waitcnt instructions. For |
| 20 | /// example, when both SMEM and LDS are in flight and we need to wait for |
| 21 | /// the i-th-last LDS instruction, then an lgkmcnt(i) is actually sufficient, |
| 22 | /// but the pass will currently generate a conservative lgkmcnt(0) because |
| 23 | /// multiple event types are in flight. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 24 | // |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | #include "AMDGPU.h" |
| 28 | #include "AMDGPUSubtarget.h" |
| 29 | #include "SIDefines.h" |
| 30 | #include "SIInstrInfo.h" |
| 31 | #include "SIMachineFunctionInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 32 | #include "SIRegisterInfo.h" |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 33 | #include "Utils/AMDGPUBaseInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseMap.h" |
| 35 | #include "llvm/ADT/DenseSet.h" |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/PostOrderIterator.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/STLExtras.h" |
| 38 | #include "llvm/ADT/SmallVector.h" |
| 39 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineFunction.h" |
| 41 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/MachineInstr.h" |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 45 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 46 | #include "llvm/CodeGen/MachineOperand.h" |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 48 | #include "llvm/IR/DebugLoc.h" |
| 49 | #include "llvm/Pass.h" |
| 50 | #include "llvm/Support/Debug.h" |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 51 | #include "llvm/Support/DebugCounter.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ErrorHandling.h" |
| 53 | #include "llvm/Support/raw_ostream.h" |
| 54 | #include <algorithm> |
| 55 | #include <cassert> |
| 56 | #include <cstdint> |
| 57 | #include <cstring> |
| 58 | #include <memory> |
| 59 | #include <utility> |
| 60 | #include <vector> |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 61 | |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 62 | using namespace llvm; |
| 63 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 64 | #define DEBUG_TYPE "si-insert-waitcnts" |
| 65 | |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 66 | DEBUG_COUNTER(ForceExpCounter, DEBUG_TYPE"-forceexp", |
| 67 | "Force emit s_waitcnt expcnt(0) instrs"); |
| 68 | DEBUG_COUNTER(ForceLgkmCounter, DEBUG_TYPE"-forcelgkm", |
| 69 | "Force emit s_waitcnt lgkmcnt(0) instrs"); |
| 70 | DEBUG_COUNTER(ForceVMCounter, DEBUG_TYPE"-forcevm", |
| 71 | "Force emit s_waitcnt vmcnt(0) instrs"); |
| 72 | |
| 73 | static cl::opt<unsigned> ForceEmitZeroFlag( |
| 74 | "amdgpu-waitcnt-forcezero", |
| 75 | cl::desc("Force all waitcnt instrs to be emitted as s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)"), |
| 76 | cl::init(0), cl::Hidden); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 77 | |
| 78 | namespace { |
| 79 | |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 80 | template <typename EnumT> |
| 81 | class enum_iterator |
| 82 | : public iterator_facade_base<enum_iterator<EnumT>, |
| 83 | std::forward_iterator_tag, const EnumT> { |
| 84 | EnumT Value; |
| 85 | public: |
| 86 | enum_iterator() = default; |
| 87 | enum_iterator(EnumT Value) : Value(Value) {} |
| 88 | |
| 89 | enum_iterator &operator++() { |
| 90 | Value = static_cast<EnumT>(Value + 1); |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | bool operator==(const enum_iterator &RHS) const { return Value == RHS.Value; } |
| 95 | |
| 96 | EnumT operator*() const { return Value; } |
| 97 | }; |
| 98 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 99 | // Class of object that encapsulates latest instruction counter score |
| 100 | // associated with the operand. Used for determining whether |
| 101 | // s_waitcnt instruction needs to be emited. |
| 102 | |
| 103 | #define CNT_MASK(t) (1u << (t)) |
| 104 | |
| 105 | enum InstCounterType { VM_CNT = 0, LGKM_CNT, EXP_CNT, NUM_INST_CNTS }; |
| 106 | |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 107 | iterator_range<enum_iterator<InstCounterType>> inst_counter_types() { |
| 108 | return make_range(enum_iterator<InstCounterType>(VM_CNT), |
| 109 | enum_iterator<InstCounterType>(NUM_INST_CNTS)); |
| 110 | } |
| 111 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 112 | using RegInterval = std::pair<signed, signed>; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 113 | |
| 114 | struct { |
| 115 | int32_t VmcntMax; |
| 116 | int32_t ExpcntMax; |
| 117 | int32_t LgkmcntMax; |
| 118 | int32_t NumVGPRsMax; |
| 119 | int32_t NumSGPRsMax; |
| 120 | } HardwareLimits; |
| 121 | |
| 122 | struct { |
| 123 | unsigned VGPR0; |
| 124 | unsigned VGPRL; |
| 125 | unsigned SGPR0; |
| 126 | unsigned SGPRL; |
| 127 | } RegisterEncoding; |
| 128 | |
| 129 | enum WaitEventType { |
| 130 | VMEM_ACCESS, // vector-memory read & write |
| 131 | LDS_ACCESS, // lds read & write |
| 132 | GDS_ACCESS, // gds read & write |
| 133 | SQ_MESSAGE, // send message |
| 134 | SMEM_ACCESS, // scalar-memory read & write |
| 135 | EXP_GPR_LOCK, // export holding on its data src |
| 136 | GDS_GPR_LOCK, // GDS holding on its data and addr src |
| 137 | EXP_POS_ACCESS, // write to export position |
| 138 | EXP_PARAM_ACCESS, // write to export parameter |
| 139 | VMW_GPR_LOCK, // vector-memory write holding on its data src |
| 140 | NUM_WAIT_EVENTS, |
| 141 | }; |
| 142 | |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 143 | static const uint32_t WaitEventMaskForInst[NUM_INST_CNTS] = { |
| 144 | (1 << VMEM_ACCESS), |
| 145 | (1 << SMEM_ACCESS) | (1 << LDS_ACCESS) | (1 << GDS_ACCESS) | |
| 146 | (1 << SQ_MESSAGE), |
| 147 | (1 << EXP_GPR_LOCK) | (1 << GDS_GPR_LOCK) | (1 << VMW_GPR_LOCK) | |
| 148 | (1 << EXP_PARAM_ACCESS) | (1 << EXP_POS_ACCESS), |
| 149 | }; |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 150 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 151 | // The mapping is: |
| 152 | // 0 .. SQ_MAX_PGM_VGPRS-1 real VGPRs |
| 153 | // SQ_MAX_PGM_VGPRS .. NUM_ALL_VGPRS-1 extra VGPR-like slots |
| 154 | // NUM_ALL_VGPRS .. NUM_ALL_VGPRS+SQ_MAX_PGM_SGPRS-1 real SGPRs |
| 155 | // We reserve a fixed number of VGPR slots in the scoring tables for |
| 156 | // special tokens like SCMEM_LDS (needed for buffer load to LDS). |
| 157 | enum RegisterMapping { |
| 158 | SQ_MAX_PGM_VGPRS = 256, // Maximum programmable VGPRs across all targets. |
| 159 | SQ_MAX_PGM_SGPRS = 256, // Maximum programmable SGPRs across all targets. |
| 160 | NUM_EXTRA_VGPRS = 1, // A reserved slot for DS. |
| 161 | EXTRA_VGPR_LDS = 0, // This is a placeholder the Shader algorithm uses. |
| 162 | NUM_ALL_VGPRS = SQ_MAX_PGM_VGPRS + NUM_EXTRA_VGPRS, // Where SGPR starts. |
| 163 | }; |
| 164 | |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 165 | void addWait(AMDGPU::Waitcnt &Wait, InstCounterType T, unsigned Count) { |
| 166 | switch (T) { |
| 167 | case VM_CNT: |
| 168 | Wait.VmCnt = std::min(Wait.VmCnt, Count); |
| 169 | break; |
| 170 | case EXP_CNT: |
| 171 | Wait.ExpCnt = std::min(Wait.ExpCnt, Count); |
| 172 | break; |
| 173 | case LGKM_CNT: |
| 174 | Wait.LgkmCnt = std::min(Wait.LgkmCnt, Count); |
| 175 | break; |
| 176 | default: |
| 177 | llvm_unreachable("bad InstCounterType"); |
| 178 | } |
| 179 | } |
| 180 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 181 | // This is a per-basic-block object that maintains current score brackets |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 182 | // of each wait counter, and a per-register scoreboard for each wait counter. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 183 | // We also maintain the latest score for every event type that can change the |
| 184 | // waitcnt in order to know if there are multiple types of events within |
| 185 | // the brackets. When multiple types of event happen in the bracket, |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 186 | // wait count may get decreased out of order, therefore we need to put in |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 187 | // "s_waitcnt 0" before use. |
| 188 | class BlockWaitcntBrackets { |
| 189 | public: |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 190 | BlockWaitcntBrackets(const GCNSubtarget *SubTarget) : ST(SubTarget) { |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 191 | for (auto T : inst_counter_types()) |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 192 | memset(VgprScores[T], 0, sizeof(VgprScores[T])); |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | ~BlockWaitcntBrackets() = default; |
| 196 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 197 | static int32_t getWaitCountMax(InstCounterType T) { |
| 198 | switch (T) { |
| 199 | case VM_CNT: |
| 200 | return HardwareLimits.VmcntMax; |
| 201 | case LGKM_CNT: |
| 202 | return HardwareLimits.LgkmcntMax; |
| 203 | case EXP_CNT: |
| 204 | return HardwareLimits.ExpcntMax; |
| 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | return 0; |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 209 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 210 | |
| 211 | void setScoreLB(InstCounterType T, int32_t Val) { |
| 212 | assert(T < NUM_INST_CNTS); |
| 213 | if (T >= NUM_INST_CNTS) |
| 214 | return; |
| 215 | ScoreLBs[T] = Val; |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 216 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 217 | |
| 218 | void setScoreUB(InstCounterType T, int32_t Val) { |
| 219 | assert(T < NUM_INST_CNTS); |
| 220 | if (T >= NUM_INST_CNTS) |
| 221 | return; |
| 222 | ScoreUBs[T] = Val; |
| 223 | if (T == EXP_CNT) { |
| 224 | int32_t UB = (int)(ScoreUBs[T] - getWaitCountMax(EXP_CNT)); |
| 225 | if (ScoreLBs[T] < UB) |
| 226 | ScoreLBs[T] = UB; |
| 227 | } |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 228 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 229 | |
Nicolai Haehnle | c548d91 | 2018-11-19 12:03:11 +0000 | [diff] [blame] | 230 | int32_t getScoreLB(InstCounterType T) const { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 231 | assert(T < NUM_INST_CNTS); |
| 232 | if (T >= NUM_INST_CNTS) |
| 233 | return 0; |
| 234 | return ScoreLBs[T]; |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 235 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 236 | |
Nicolai Haehnle | c548d91 | 2018-11-19 12:03:11 +0000 | [diff] [blame] | 237 | int32_t getScoreUB(InstCounterType T) const { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 238 | assert(T < NUM_INST_CNTS); |
| 239 | if (T >= NUM_INST_CNTS) |
| 240 | return 0; |
| 241 | return ScoreUBs[T]; |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 242 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 243 | |
| 244 | // Mapping from event to counter. |
| 245 | InstCounterType eventCounter(WaitEventType E) { |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 246 | if (E == VMEM_ACCESS) |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 247 | return VM_CNT; |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 248 | if (WaitEventMaskForInst[LGKM_CNT] & (1 << E)) |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 249 | return LGKM_CNT; |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 250 | assert(WaitEventMaskForInst[EXP_CNT] & (1 << E)); |
| 251 | return EXP_CNT; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void setRegScore(int GprNo, InstCounterType T, int32_t Val) { |
| 255 | if (GprNo < NUM_ALL_VGPRS) { |
| 256 | if (GprNo > VgprUB) { |
| 257 | VgprUB = GprNo; |
| 258 | } |
| 259 | VgprScores[T][GprNo] = Val; |
| 260 | } else { |
| 261 | assert(T == LGKM_CNT); |
| 262 | if (GprNo - NUM_ALL_VGPRS > SgprUB) { |
| 263 | SgprUB = GprNo - NUM_ALL_VGPRS; |
| 264 | } |
| 265 | SgprScores[GprNo - NUM_ALL_VGPRS] = Val; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | int32_t getRegScore(int GprNo, InstCounterType T) { |
| 270 | if (GprNo < NUM_ALL_VGPRS) { |
| 271 | return VgprScores[T][GprNo]; |
| 272 | } |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 273 | assert(T == LGKM_CNT); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 274 | return SgprScores[GprNo - NUM_ALL_VGPRS]; |
| 275 | } |
| 276 | |
| 277 | void clear() { |
| 278 | memset(ScoreLBs, 0, sizeof(ScoreLBs)); |
| 279 | memset(ScoreUBs, 0, sizeof(ScoreUBs)); |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 280 | PendingEvents = 0; |
| 281 | memset(MixedPendingEvents, 0, sizeof(MixedPendingEvents)); |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 282 | for (auto T : inst_counter_types()) |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 283 | memset(VgprScores[T], 0, sizeof(VgprScores[T])); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 284 | memset(SgprScores, 0, sizeof(SgprScores)); |
| 285 | } |
| 286 | |
| 287 | RegInterval getRegInterval(const MachineInstr *MI, const SIInstrInfo *TII, |
| 288 | const MachineRegisterInfo *MRI, |
| 289 | const SIRegisterInfo *TRI, unsigned OpNo, |
| 290 | bool Def) const; |
| 291 | |
| 292 | void setExpScore(const MachineInstr *MI, const SIInstrInfo *TII, |
| 293 | const SIRegisterInfo *TRI, const MachineRegisterInfo *MRI, |
| 294 | unsigned OpNo, int32_t Val); |
| 295 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 296 | int32_t getMaxVGPR() const { return VgprUB; } |
| 297 | int32_t getMaxSGPR() const { return SgprUB; } |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 298 | |
Nicolai Haehnle | c548d91 | 2018-11-19 12:03:11 +0000 | [diff] [blame] | 299 | bool counterOutOfOrder(InstCounterType T) const; |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 300 | bool simplifyWaitcnt(AMDGPU::Waitcnt &Wait) const; |
| 301 | bool simplifyWaitcnt(InstCounterType T, unsigned &Count) const; |
| 302 | void determineWait(InstCounterType T, int ScoreToWait, |
| 303 | AMDGPU::Waitcnt &Wait) const; |
| 304 | void applyWaitcnt(const AMDGPU::Waitcnt &Wait); |
| 305 | void applyWaitcnt(InstCounterType T, unsigned Count); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 306 | void updateByEvent(const SIInstrInfo *TII, const SIRegisterInfo *TRI, |
| 307 | const MachineRegisterInfo *MRI, WaitEventType E, |
| 308 | MachineInstr &MI); |
| 309 | |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 310 | bool hasPendingEvent(WaitEventType E) const { |
| 311 | return PendingEvents & (1 << E); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 314 | void mergePendingEvents(const BlockWaitcntBrackets &Other); |
| 315 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 316 | bool hasPendingFlat() const { |
| 317 | return ((LastFlat[LGKM_CNT] > ScoreLBs[LGKM_CNT] && |
| 318 | LastFlat[LGKM_CNT] <= ScoreUBs[LGKM_CNT]) || |
| 319 | (LastFlat[VM_CNT] > ScoreLBs[VM_CNT] && |
| 320 | LastFlat[VM_CNT] <= ScoreUBs[VM_CNT])); |
| 321 | } |
| 322 | |
| 323 | void setPendingFlat() { |
| 324 | LastFlat[VM_CNT] = ScoreUBs[VM_CNT]; |
| 325 | LastFlat[LGKM_CNT] = ScoreUBs[LGKM_CNT]; |
| 326 | } |
| 327 | |
| 328 | int pendingFlat(InstCounterType Ct) const { return LastFlat[Ct]; } |
| 329 | |
| 330 | void setLastFlat(InstCounterType Ct, int Val) { LastFlat[Ct] = Val; } |
| 331 | |
| 332 | bool getRevisitLoop() const { return RevisitLoop; } |
| 333 | void setRevisitLoop(bool RevisitLoopIn) { RevisitLoop = RevisitLoopIn; } |
| 334 | |
| 335 | void setPostOrder(int32_t PostOrderIn) { PostOrder = PostOrderIn; } |
| 336 | int32_t getPostOrder() const { return PostOrder; } |
| 337 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 338 | void print(raw_ostream &); |
| 339 | void dump() { print(dbgs()); } |
| 340 | |
| 341 | private: |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 342 | const GCNSubtarget *ST = nullptr; |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 343 | bool RevisitLoop = false; |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 344 | int32_t PostOrder = 0; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 345 | int32_t ScoreLBs[NUM_INST_CNTS] = {0}; |
| 346 | int32_t ScoreUBs[NUM_INST_CNTS] = {0}; |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 347 | uint32_t PendingEvents = 0; |
| 348 | bool MixedPendingEvents[NUM_INST_CNTS] = {false}; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 349 | // Remember the last flat memory operation. |
| 350 | int32_t LastFlat[NUM_INST_CNTS] = {0}; |
| 351 | // wait_cnt scores for every vgpr. |
| 352 | // Keep track of the VgprUB and SgprUB to make merge at join efficient. |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 353 | int32_t VgprUB = 0; |
| 354 | int32_t SgprUB = 0; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 355 | int32_t VgprScores[NUM_INST_CNTS][NUM_ALL_VGPRS]; |
| 356 | // Wait cnt scores for every sgpr, only lgkmcnt is relevant. |
| 357 | int32_t SgprScores[SQ_MAX_PGM_SGPRS] = {0}; |
| 358 | }; |
| 359 | |
| 360 | // This is a per-loop-region object that records waitcnt status at the end of |
| 361 | // loop footer from the previous iteration. We also maintain an iteration |
| 362 | // count to track the number of times the loop has been visited. When it |
| 363 | // doesn't converge naturally, we force convergence by inserting s_waitcnt 0 |
| 364 | // at the end of the loop footer. |
| 365 | class LoopWaitcntData { |
| 366 | public: |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 367 | LoopWaitcntData() = default; |
| 368 | ~LoopWaitcntData() = default; |
| 369 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 370 | void incIterCnt() { IterCnt++; } |
| 371 | void resetIterCnt() { IterCnt = 0; } |
Mark Searles | 1054541 | 2018-05-30 15:47:45 +0000 | [diff] [blame] | 372 | unsigned getIterCnt() { return IterCnt; } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 373 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 374 | void setWaitcnt(MachineInstr *WaitcntIn) { LfWaitcnt = WaitcntIn; } |
| 375 | MachineInstr *getWaitcnt() const { return LfWaitcnt; } |
| 376 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 377 | void print() { LLVM_DEBUG(dbgs() << " iteration " << IterCnt << '\n';); } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 378 | |
| 379 | private: |
| 380 | // s_waitcnt added at the end of loop footer to stablize wait scores |
| 381 | // at the end of the loop footer. |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 382 | MachineInstr *LfWaitcnt = nullptr; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 383 | // Number of iterations the loop has been visited, not including the initial |
| 384 | // walk over. |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 385 | int32_t IterCnt = 0; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | class SIInsertWaitcnts : public MachineFunctionPass { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 389 | private: |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 390 | const GCNSubtarget *ST = nullptr; |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 391 | const SIInstrInfo *TII = nullptr; |
| 392 | const SIRegisterInfo *TRI = nullptr; |
| 393 | const MachineRegisterInfo *MRI = nullptr; |
| 394 | const MachineLoopInfo *MLI = nullptr; |
Konstantin Zhuravlyov | 71e43ee | 2018-09-12 18:50:47 +0000 | [diff] [blame] | 395 | AMDGPU::IsaVersion IV; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 396 | |
| 397 | DenseSet<MachineBasicBlock *> BlockVisitedSet; |
Mark Searles | 24c92ee | 2018-02-07 02:21:21 +0000 | [diff] [blame] | 398 | DenseSet<MachineInstr *> TrackedWaitcntSet; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 399 | DenseSet<MachineInstr *> VCCZBugHandledSet; |
| 400 | |
| 401 | DenseMap<MachineBasicBlock *, std::unique_ptr<BlockWaitcntBrackets>> |
| 402 | BlockWaitcntBracketsMap; |
| 403 | |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 404 | std::vector<MachineBasicBlock *> BlockWaitcntProcessedSet; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 405 | |
| 406 | DenseMap<MachineLoop *, std::unique_ptr<LoopWaitcntData>> LoopWaitcntDataMap; |
| 407 | |
Mark Searles | 4a0f2c5 | 2018-05-07 14:43:28 +0000 | [diff] [blame] | 408 | // ForceEmitZeroWaitcnts: force all waitcnts insts to be s_waitcnt 0 |
| 409 | // because of amdgpu-waitcnt-forcezero flag |
| 410 | bool ForceEmitZeroWaitcnts; |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 411 | bool ForceEmitWaitcnt[NUM_INST_CNTS]; |
| 412 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 413 | public: |
| 414 | static char ID; |
| 415 | |
Konstantin Zhuravlyov | 7774777 | 2018-06-26 21:33:38 +0000 | [diff] [blame] | 416 | SIInsertWaitcnts() : MachineFunctionPass(ID) { |
| 417 | (void)ForceExpCounter; |
| 418 | (void)ForceLgkmCounter; |
| 419 | (void)ForceVMCounter; |
| 420 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 421 | |
| 422 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 423 | |
| 424 | StringRef getPassName() const override { |
| 425 | return "SI insert wait instructions"; |
| 426 | } |
| 427 | |
| 428 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 429 | AU.setPreservesCFG(); |
| 430 | AU.addRequired<MachineLoopInfo>(); |
| 431 | MachineFunctionPass::getAnalysisUsage(AU); |
| 432 | } |
| 433 | |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 434 | bool isForceEmitWaitcnt() const { |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 435 | for (auto T : inst_counter_types()) |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 436 | if (ForceEmitWaitcnt[T]) |
| 437 | return true; |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | void setForceEmitWaitcnt() { |
| 442 | // For non-debug builds, ForceEmitWaitcnt has been initialized to false; |
| 443 | // For debug builds, get the debug counter info and adjust if need be |
| 444 | #ifndef NDEBUG |
| 445 | if (DebugCounter::isCounterSet(ForceExpCounter) && |
| 446 | DebugCounter::shouldExecute(ForceExpCounter)) { |
| 447 | ForceEmitWaitcnt[EXP_CNT] = true; |
| 448 | } else { |
| 449 | ForceEmitWaitcnt[EXP_CNT] = false; |
| 450 | } |
| 451 | |
| 452 | if (DebugCounter::isCounterSet(ForceLgkmCounter) && |
| 453 | DebugCounter::shouldExecute(ForceLgkmCounter)) { |
| 454 | ForceEmitWaitcnt[LGKM_CNT] = true; |
| 455 | } else { |
| 456 | ForceEmitWaitcnt[LGKM_CNT] = false; |
| 457 | } |
| 458 | |
| 459 | if (DebugCounter::isCounterSet(ForceVMCounter) && |
| 460 | DebugCounter::shouldExecute(ForceVMCounter)) { |
| 461 | ForceEmitWaitcnt[VM_CNT] = true; |
| 462 | } else { |
| 463 | ForceEmitWaitcnt[VM_CNT] = false; |
| 464 | } |
| 465 | #endif // NDEBUG |
| 466 | } |
| 467 | |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 468 | bool mayAccessLDSThroughFlat(const MachineInstr &MI) const; |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 469 | void generateWaitcntInstBefore(MachineInstr &MI, |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 470 | BlockWaitcntBrackets *ScoreBrackets, |
| 471 | MachineInstr *OldWaitcntInstr); |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 472 | void updateEventWaitcntAfter(MachineInstr &Inst, |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 473 | BlockWaitcntBrackets *ScoreBrackets); |
| 474 | void mergeInputScoreBrackets(MachineBasicBlock &Block); |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 475 | bool isLoopBottom(const MachineLoop *Loop, const MachineBasicBlock *Block); |
| 476 | unsigned countNumBottomBlocks(const MachineLoop *Loop); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 477 | void insertWaitcntInBlock(MachineFunction &MF, MachineBasicBlock &Block); |
| 478 | void insertWaitcntBeforeCF(MachineBasicBlock &Block, MachineInstr *Inst); |
| 479 | }; |
| 480 | |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 481 | } // end anonymous namespace |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 482 | |
| 483 | RegInterval BlockWaitcntBrackets::getRegInterval(const MachineInstr *MI, |
| 484 | const SIInstrInfo *TII, |
| 485 | const MachineRegisterInfo *MRI, |
| 486 | const SIRegisterInfo *TRI, |
| 487 | unsigned OpNo, |
| 488 | bool Def) const { |
| 489 | const MachineOperand &Op = MI->getOperand(OpNo); |
| 490 | if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()) || |
| 491 | (Def && !Op.isDef())) |
| 492 | return {-1, -1}; |
| 493 | |
| 494 | // A use via a PW operand does not need a waitcnt. |
| 495 | // A partial write is not a WAW. |
| 496 | assert(!Op.getSubReg() || !Op.isUndef()); |
| 497 | |
| 498 | RegInterval Result; |
| 499 | const MachineRegisterInfo &MRIA = *MRI; |
| 500 | |
| 501 | unsigned Reg = TRI->getEncodingValue(Op.getReg()); |
| 502 | |
| 503 | if (TRI->isVGPR(MRIA, Op.getReg())) { |
| 504 | assert(Reg >= RegisterEncoding.VGPR0 && Reg <= RegisterEncoding.VGPRL); |
| 505 | Result.first = Reg - RegisterEncoding.VGPR0; |
| 506 | assert(Result.first >= 0 && Result.first < SQ_MAX_PGM_VGPRS); |
| 507 | } else if (TRI->isSGPRReg(MRIA, Op.getReg())) { |
| 508 | assert(Reg >= RegisterEncoding.SGPR0 && Reg < SQ_MAX_PGM_SGPRS); |
| 509 | Result.first = Reg - RegisterEncoding.SGPR0 + NUM_ALL_VGPRS; |
| 510 | assert(Result.first >= NUM_ALL_VGPRS && |
| 511 | Result.first < SQ_MAX_PGM_SGPRS + NUM_ALL_VGPRS); |
| 512 | } |
| 513 | // TODO: Handle TTMP |
| 514 | // else if (TRI->isTTMP(MRIA, Reg.getReg())) ... |
| 515 | else |
| 516 | return {-1, -1}; |
| 517 | |
| 518 | const MachineInstr &MIA = *MI; |
| 519 | const TargetRegisterClass *RC = TII->getOpRegClass(MIA, OpNo); |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 520 | unsigned Size = TRI->getRegSizeInBits(*RC); |
| 521 | Result.second = Result.first + (Size / 32); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 522 | |
| 523 | return Result; |
| 524 | } |
| 525 | |
| 526 | void BlockWaitcntBrackets::setExpScore(const MachineInstr *MI, |
| 527 | const SIInstrInfo *TII, |
| 528 | const SIRegisterInfo *TRI, |
| 529 | const MachineRegisterInfo *MRI, |
| 530 | unsigned OpNo, int32_t Val) { |
| 531 | RegInterval Interval = getRegInterval(MI, TII, MRI, TRI, OpNo, false); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 532 | LLVM_DEBUG({ |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 533 | const MachineOperand &Opnd = MI->getOperand(OpNo); |
| 534 | assert(TRI->isVGPR(*MRI, Opnd.getReg())); |
| 535 | }); |
| 536 | for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) { |
| 537 | setRegScore(RegNo, EXP_CNT, Val); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | void BlockWaitcntBrackets::updateByEvent(const SIInstrInfo *TII, |
| 542 | const SIRegisterInfo *TRI, |
| 543 | const MachineRegisterInfo *MRI, |
| 544 | WaitEventType E, MachineInstr &Inst) { |
| 545 | const MachineRegisterInfo &MRIA = *MRI; |
| 546 | InstCounterType T = eventCounter(E); |
| 547 | int32_t CurrScore = getScoreUB(T) + 1; |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 548 | // PendingEvents and ScoreUB need to be update regardless if this event |
| 549 | // changes the score of a register or not. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 550 | // Examples including vm_cnt when buffer-store or lgkm_cnt when send-message. |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 551 | if (!hasPendingEvent(E)) { |
| 552 | if (PendingEvents & WaitEventMaskForInst[T]) |
| 553 | MixedPendingEvents[T] = true; |
| 554 | PendingEvents |= 1 << E; |
| 555 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 556 | setScoreUB(T, CurrScore); |
| 557 | |
| 558 | if (T == EXP_CNT) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 559 | // Put score on the source vgprs. If this is a store, just use those |
| 560 | // specific register(s). |
| 561 | if (TII->isDS(Inst) && (Inst.mayStore() || Inst.mayLoad())) { |
| 562 | // All GDS operations must protect their address register (same as |
| 563 | // export.) |
| 564 | if (Inst.getOpcode() != AMDGPU::DS_APPEND && |
| 565 | Inst.getOpcode() != AMDGPU::DS_CONSUME) { |
| 566 | setExpScore( |
| 567 | &Inst, TII, TRI, MRI, |
| 568 | AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::addr), |
| 569 | CurrScore); |
| 570 | } |
| 571 | if (Inst.mayStore()) { |
| 572 | setExpScore( |
| 573 | &Inst, TII, TRI, MRI, |
| 574 | AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data0), |
| 575 | CurrScore); |
| 576 | if (AMDGPU::getNamedOperandIdx(Inst.getOpcode(), |
| 577 | AMDGPU::OpName::data1) != -1) { |
| 578 | setExpScore(&Inst, TII, TRI, MRI, |
| 579 | AMDGPU::getNamedOperandIdx(Inst.getOpcode(), |
| 580 | AMDGPU::OpName::data1), |
| 581 | CurrScore); |
| 582 | } |
| 583 | } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1 && |
| 584 | Inst.getOpcode() != AMDGPU::DS_GWS_INIT && |
| 585 | Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_V && |
| 586 | Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_BR && |
| 587 | Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_P && |
| 588 | Inst.getOpcode() != AMDGPU::DS_GWS_BARRIER && |
| 589 | Inst.getOpcode() != AMDGPU::DS_APPEND && |
| 590 | Inst.getOpcode() != AMDGPU::DS_CONSUME && |
| 591 | Inst.getOpcode() != AMDGPU::DS_ORDERED_COUNT) { |
| 592 | for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) { |
| 593 | const MachineOperand &Op = Inst.getOperand(I); |
| 594 | if (Op.isReg() && !Op.isDef() && TRI->isVGPR(MRIA, Op.getReg())) { |
| 595 | setExpScore(&Inst, TII, TRI, MRI, I, CurrScore); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | } else if (TII->isFLAT(Inst)) { |
| 600 | if (Inst.mayStore()) { |
| 601 | setExpScore( |
| 602 | &Inst, TII, TRI, MRI, |
| 603 | AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data), |
| 604 | CurrScore); |
| 605 | } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) { |
| 606 | setExpScore( |
| 607 | &Inst, TII, TRI, MRI, |
| 608 | AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data), |
| 609 | CurrScore); |
| 610 | } |
| 611 | } else if (TII->isMIMG(Inst)) { |
| 612 | if (Inst.mayStore()) { |
| 613 | setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore); |
| 614 | } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) { |
| 615 | setExpScore( |
| 616 | &Inst, TII, TRI, MRI, |
| 617 | AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data), |
| 618 | CurrScore); |
| 619 | } |
| 620 | } else if (TII->isMTBUF(Inst)) { |
| 621 | if (Inst.mayStore()) { |
| 622 | setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore); |
| 623 | } |
| 624 | } else if (TII->isMUBUF(Inst)) { |
| 625 | if (Inst.mayStore()) { |
| 626 | setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore); |
| 627 | } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) { |
| 628 | setExpScore( |
| 629 | &Inst, TII, TRI, MRI, |
| 630 | AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data), |
| 631 | CurrScore); |
| 632 | } |
| 633 | } else { |
| 634 | if (TII->isEXP(Inst)) { |
| 635 | // For export the destination registers are really temps that |
| 636 | // can be used as the actual source after export patching, so |
| 637 | // we need to treat them like sources and set the EXP_CNT |
| 638 | // score. |
| 639 | for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) { |
| 640 | MachineOperand &DefMO = Inst.getOperand(I); |
| 641 | if (DefMO.isReg() && DefMO.isDef() && |
| 642 | TRI->isVGPR(MRIA, DefMO.getReg())) { |
| 643 | setRegScore(TRI->getEncodingValue(DefMO.getReg()), EXP_CNT, |
| 644 | CurrScore); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) { |
| 649 | MachineOperand &MO = Inst.getOperand(I); |
| 650 | if (MO.isReg() && !MO.isDef() && TRI->isVGPR(MRIA, MO.getReg())) { |
| 651 | setExpScore(&Inst, TII, TRI, MRI, I, CurrScore); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | #if 0 // TODO: check if this is handled by MUBUF code above. |
| 656 | } else if (Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORD || |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 657 | Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORDX2 || |
| 658 | Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORDX4) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 659 | MachineOperand *MO = TII->getNamedOperand(Inst, AMDGPU::OpName::data); |
| 660 | unsigned OpNo;//TODO: find the OpNo for this operand; |
| 661 | RegInterval Interval = getRegInterval(&Inst, TII, MRI, TRI, OpNo, false); |
| 662 | for (signed RegNo = Interval.first; RegNo < Interval.second; |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 663 | ++RegNo) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 664 | setRegScore(RegNo + NUM_ALL_VGPRS, t, CurrScore); |
| 665 | } |
| 666 | #endif |
| 667 | } else { |
| 668 | // Match the score to the destination registers. |
| 669 | for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) { |
| 670 | RegInterval Interval = getRegInterval(&Inst, TII, MRI, TRI, I, true); |
| 671 | if (T == VM_CNT && Interval.first >= NUM_ALL_VGPRS) |
| 672 | continue; |
| 673 | for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) { |
| 674 | setRegScore(RegNo, T, CurrScore); |
| 675 | } |
| 676 | } |
| 677 | if (TII->isDS(Inst) && Inst.mayStore()) { |
| 678 | setRegScore(SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS, T, CurrScore); |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | void BlockWaitcntBrackets::print(raw_ostream &OS) { |
| 684 | OS << '\n'; |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 685 | for (auto T : inst_counter_types()) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 686 | int LB = getScoreLB(T); |
| 687 | int UB = getScoreUB(T); |
| 688 | |
| 689 | switch (T) { |
| 690 | case VM_CNT: |
| 691 | OS << " VM_CNT(" << UB - LB << "): "; |
| 692 | break; |
| 693 | case LGKM_CNT: |
| 694 | OS << " LGKM_CNT(" << UB - LB << "): "; |
| 695 | break; |
| 696 | case EXP_CNT: |
| 697 | OS << " EXP_CNT(" << UB - LB << "): "; |
| 698 | break; |
| 699 | default: |
| 700 | OS << " UNKNOWN(" << UB - LB << "): "; |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | if (LB < UB) { |
| 705 | // Print vgpr scores. |
| 706 | for (int J = 0; J <= getMaxVGPR(); J++) { |
| 707 | int RegScore = getRegScore(J, T); |
| 708 | if (RegScore <= LB) |
| 709 | continue; |
| 710 | int RelScore = RegScore - LB - 1; |
| 711 | if (J < SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS) { |
| 712 | OS << RelScore << ":v" << J << " "; |
| 713 | } else { |
| 714 | OS << RelScore << ":ds "; |
| 715 | } |
| 716 | } |
| 717 | // Also need to print sgpr scores for lgkm_cnt. |
| 718 | if (T == LGKM_CNT) { |
| 719 | for (int J = 0; J <= getMaxSGPR(); J++) { |
| 720 | int RegScore = getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT); |
| 721 | if (RegScore <= LB) |
| 722 | continue; |
| 723 | int RelScore = RegScore - LB - 1; |
| 724 | OS << RelScore << ":s" << J << " "; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | OS << '\n'; |
| 729 | } |
| 730 | OS << '\n'; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 733 | /// Simplify the waitcnt, in the sense of removing redundant counts, and return |
| 734 | /// whether a waitcnt instruction is needed at all. |
| 735 | bool BlockWaitcntBrackets::simplifyWaitcnt(AMDGPU::Waitcnt &Wait) const { |
| 736 | return simplifyWaitcnt(VM_CNT, Wait.VmCnt) | |
| 737 | simplifyWaitcnt(EXP_CNT, Wait.ExpCnt) | |
| 738 | simplifyWaitcnt(LGKM_CNT, Wait.LgkmCnt); |
| 739 | } |
| 740 | |
| 741 | bool BlockWaitcntBrackets::simplifyWaitcnt(InstCounterType T, |
| 742 | unsigned &Count) const { |
| 743 | const int32_t LB = getScoreLB(T); |
| 744 | const int32_t UB = getScoreUB(T); |
| 745 | if (Count < (unsigned)UB && UB - (int32_t)Count > LB) |
| 746 | return true; |
| 747 | |
| 748 | Count = ~0u; |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | void BlockWaitcntBrackets::determineWait(InstCounterType T, int ScoreToWait, |
| 753 | AMDGPU::Waitcnt &Wait) const { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 754 | if (ScoreToWait == -1) { |
| 755 | // The score to wait is unknown. This implies that it was not encountered |
| 756 | // during the path of the CFG walk done during the current traversal but |
| 757 | // may be seen on a different path. Emit an s_wait counter with a |
| 758 | // conservative value of 0 for the counter. |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 759 | addWait(Wait, T, 0); |
| 760 | return; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | // If the score of src_operand falls within the bracket, we need an |
| 764 | // s_waitcnt instruction. |
| 765 | const int32_t LB = getScoreLB(T); |
| 766 | const int32_t UB = getScoreUB(T); |
| 767 | if ((UB >= ScoreToWait) && (ScoreToWait > LB)) { |
Mark Searles | f0b93f1 | 2018-06-04 16:51:59 +0000 | [diff] [blame] | 768 | if ((T == VM_CNT || T == LGKM_CNT) && |
| 769 | hasPendingFlat() && |
| 770 | !ST->hasFlatLgkmVMemCountInOrder()) { |
| 771 | // If there is a pending FLAT operation, and this is a VMem or LGKM |
| 772 | // waitcnt and the target can report early completion, then we need |
| 773 | // to force a waitcnt 0. |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 774 | addWait(Wait, T, 0); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 775 | } else if (counterOutOfOrder(T)) { |
| 776 | // Counter can get decremented out-of-order when there |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 777 | // are multiple types event in the bracket. Also emit an s_wait counter |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 778 | // with a conservative value of 0 for the counter. |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 779 | addWait(Wait, T, 0); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 780 | } else { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 781 | addWait(Wait, T, UB - ScoreToWait); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 784 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 785 | |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 786 | void BlockWaitcntBrackets::applyWaitcnt(const AMDGPU::Waitcnt &Wait) { |
| 787 | applyWaitcnt(VM_CNT, Wait.VmCnt); |
| 788 | applyWaitcnt(EXP_CNT, Wait.ExpCnt); |
| 789 | applyWaitcnt(LGKM_CNT, Wait.LgkmCnt); |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | void BlockWaitcntBrackets::applyWaitcnt(InstCounterType T, unsigned Count) { |
| 793 | const int32_t UB = getScoreUB(T); |
| 794 | if (Count >= (unsigned)UB) |
| 795 | return; |
| 796 | if (Count != 0) { |
| 797 | if (counterOutOfOrder(T)) |
| 798 | return; |
| 799 | setScoreLB(T, std::max(getScoreLB(T), UB - (int32_t)Count)); |
| 800 | } else { |
| 801 | setScoreLB(T, UB); |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 802 | MixedPendingEvents[T] = false; |
| 803 | PendingEvents &= ~WaitEventMaskForInst[T]; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | void BlockWaitcntBrackets::mergePendingEvents(const BlockWaitcntBrackets &Other) { |
| 808 | for (auto T : inst_counter_types()) { |
| 809 | uint32_t Old = PendingEvents & WaitEventMaskForInst[T]; |
| 810 | uint32_t New = Other.PendingEvents & WaitEventMaskForInst[T]; |
| 811 | if (Other.MixedPendingEvents[T] || (Old && New && Old != New)) |
| 812 | MixedPendingEvents[T] = true; |
| 813 | PendingEvents |= New; |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 814 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | // Where there are multiple types of event in the bracket of a counter, |
| 818 | // the decrement may go out of order. |
Nicolai Haehnle | c548d91 | 2018-11-19 12:03:11 +0000 | [diff] [blame] | 819 | bool BlockWaitcntBrackets::counterOutOfOrder(InstCounterType T) const { |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 820 | // Scalar memory read always can go out of order. |
| 821 | if (T == LGKM_CNT && hasPendingEvent(SMEM_ACCESS)) |
| 822 | return true; |
| 823 | return MixedPendingEvents[T]; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | INITIALIZE_PASS_BEGIN(SIInsertWaitcnts, DEBUG_TYPE, "SI Insert Waitcnts", false, |
| 827 | false) |
| 828 | INITIALIZE_PASS_END(SIInsertWaitcnts, DEBUG_TYPE, "SI Insert Waitcnts", false, |
| 829 | false) |
| 830 | |
| 831 | char SIInsertWaitcnts::ID = 0; |
| 832 | |
| 833 | char &llvm::SIInsertWaitcntsID = SIInsertWaitcnts::ID; |
| 834 | |
| 835 | FunctionPass *llvm::createSIInsertWaitcntsPass() { |
| 836 | return new SIInsertWaitcnts(); |
| 837 | } |
| 838 | |
| 839 | static bool readsVCCZ(const MachineInstr &MI) { |
| 840 | unsigned Opc = MI.getOpcode(); |
| 841 | return (Opc == AMDGPU::S_CBRANCH_VCCNZ || Opc == AMDGPU::S_CBRANCH_VCCZ) && |
| 842 | !MI.getOperand(1).isUndef(); |
| 843 | } |
| 844 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 845 | /// Generate s_waitcnt instruction to be placed before cur_Inst. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 846 | /// Instructions of a given type are returned in order, |
| 847 | /// but instructions of different types can complete out of order. |
| 848 | /// We rely on this in-order completion |
| 849 | /// and simply assign a score to the memory access instructions. |
| 850 | /// We keep track of the active "score bracket" to determine |
| 851 | /// if an access of a memory read requires an s_waitcnt |
| 852 | /// and if so what the value of each counter is. |
| 853 | /// The "score bracket" is bound by the lower bound and upper bound |
| 854 | /// scores (*_score_LB and *_score_ub respectively). |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 855 | void SIInsertWaitcnts::generateWaitcntInstBefore( |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 856 | MachineInstr &MI, BlockWaitcntBrackets *ScoreBrackets, |
| 857 | MachineInstr *OldWaitcntInstr) { |
Mark Searles | 4a0f2c5 | 2018-05-07 14:43:28 +0000 | [diff] [blame] | 858 | setForceEmitWaitcnt(); |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 859 | bool IsForceEmitWaitcnt = isForceEmitWaitcnt(); |
| 860 | |
Nicolai Haehnle | 61396ff | 2018-11-07 21:53:36 +0000 | [diff] [blame] | 861 | if (MI.isDebugInstr()) |
Stanislav Mekhanoshin | db39b4b | 2018-02-08 00:18:35 +0000 | [diff] [blame] | 862 | return; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 863 | |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 864 | AMDGPU::Waitcnt Wait; |
| 865 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 866 | // See if this instruction has a forced S_WAITCNT VM. |
| 867 | // TODO: Handle other cases of NeedsWaitcntVmBefore() |
Nicolai Haehnle | f96456c | 2018-11-29 11:06:18 +0000 | [diff] [blame^] | 868 | if (MI.getOpcode() == AMDGPU::BUFFER_WBINVL1 || |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 869 | MI.getOpcode() == AMDGPU::BUFFER_WBINVL1_SC || |
| 870 | MI.getOpcode() == AMDGPU::BUFFER_WBINVL1_VOL) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 871 | Wait.VmCnt = 0; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | // All waits must be resolved at call return. |
| 875 | // NOTE: this could be improved with knowledge of all call sites or |
| 876 | // with knowledge of the called routines. |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 877 | if (MI.getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG || |
Mark Searles | 11d0a04 | 2017-05-31 16:44:23 +0000 | [diff] [blame] | 878 | MI.getOpcode() == AMDGPU::S_SETPC_B64_return) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 879 | Wait = AMDGPU::Waitcnt::allZero(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 880 | } |
| 881 | // Resolve vm waits before gs-done. |
| 882 | else if ((MI.getOpcode() == AMDGPU::S_SENDMSG || |
| 883 | MI.getOpcode() == AMDGPU::S_SENDMSGHALT) && |
| 884 | ((MI.getOperand(0).getImm() & AMDGPU::SendMsg::ID_MASK_) == |
| 885 | AMDGPU::SendMsg::ID_GS_DONE)) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 886 | Wait.VmCnt = 0; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 887 | } |
| 888 | #if 0 // TODO: the following blocks of logic when we have fence. |
| 889 | else if (MI.getOpcode() == SC_FENCE) { |
| 890 | const unsigned int group_size = |
| 891 | context->shader_info->GetMaxThreadGroupSize(); |
| 892 | // group_size == 0 means thread group size is unknown at compile time |
| 893 | const bool group_is_multi_wave = |
| 894 | (group_size == 0 || group_size > target_info->GetWaveFrontSize()); |
| 895 | const bool fence_is_global = !((SCInstInternalMisc*)Inst)->IsGroupFence(); |
| 896 | |
| 897 | for (unsigned int i = 0; i < Inst->NumSrcOperands(); i++) { |
| 898 | SCRegType src_type = Inst->GetSrcType(i); |
| 899 | switch (src_type) { |
| 900 | case SCMEM_LDS: |
| 901 | if (group_is_multi_wave || |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 902 | context->OptFlagIsOn(OPT_R1100_LDSMEM_FENCE_CHICKEN_BIT)) { |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 903 | EmitWaitcnt |= ScoreBrackets->updateByWait(LGKM_CNT, |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 904 | ScoreBrackets->getScoreUB(LGKM_CNT)); |
| 905 | // LDS may have to wait for VM_CNT after buffer load to LDS |
| 906 | if (target_info->HasBufferLoadToLDS()) { |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 907 | EmitWaitcnt |= ScoreBrackets->updateByWait(VM_CNT, |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 908 | ScoreBrackets->getScoreUB(VM_CNT)); |
| 909 | } |
| 910 | } |
| 911 | break; |
| 912 | |
| 913 | case SCMEM_GDS: |
| 914 | if (group_is_multi_wave || fence_is_global) { |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 915 | EmitWaitcnt |= ScoreBrackets->updateByWait(EXP_CNT, |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 916 | ScoreBrackets->getScoreUB(EXP_CNT)); |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 917 | EmitWaitcnt |= ScoreBrackets->updateByWait(LGKM_CNT, |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 918 | ScoreBrackets->getScoreUB(LGKM_CNT)); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 919 | } |
| 920 | break; |
| 921 | |
| 922 | case SCMEM_UAV: |
| 923 | case SCMEM_TFBUF: |
| 924 | case SCMEM_RING: |
| 925 | case SCMEM_SCATTER: |
| 926 | if (group_is_multi_wave || fence_is_global) { |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 927 | EmitWaitcnt |= ScoreBrackets->updateByWait(EXP_CNT, |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 928 | ScoreBrackets->getScoreUB(EXP_CNT)); |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 929 | EmitWaitcnt |= ScoreBrackets->updateByWait(VM_CNT, |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 930 | ScoreBrackets->getScoreUB(VM_CNT)); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 931 | } |
| 932 | break; |
| 933 | |
| 934 | case SCMEM_SCRATCH: |
| 935 | default: |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | #endif |
| 941 | |
| 942 | // Export & GDS instructions do not read the EXEC mask until after the export |
| 943 | // is granted (which can occur well after the instruction is issued). |
| 944 | // The shader program must flush all EXP operations on the export-count |
| 945 | // before overwriting the EXEC mask. |
| 946 | else { |
| 947 | if (MI.modifiesRegister(AMDGPU::EXEC, TRI)) { |
| 948 | // Export and GDS are tracked individually, either may trigger a waitcnt |
| 949 | // for EXEC. |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 950 | if (ScoreBrackets->hasPendingEvent(EXP_GPR_LOCK) || |
| 951 | ScoreBrackets->hasPendingEvent(EXP_PARAM_ACCESS) || |
| 952 | ScoreBrackets->hasPendingEvent(EXP_POS_ACCESS) || |
| 953 | ScoreBrackets->hasPendingEvent(GDS_GPR_LOCK)) { |
| 954 | Wait.ExpCnt = 0; |
| 955 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | #if 0 // TODO: the following code to handle CALL. |
| 959 | // The argument passing for CALLs should suffice for VM_CNT and LGKM_CNT. |
| 960 | // However, there is a problem with EXP_CNT, because the call cannot |
| 961 | // easily tell if a register is used in the function, and if it did, then |
| 962 | // the referring instruction would have to have an S_WAITCNT, which is |
| 963 | // dependent on all call sites. So Instead, force S_WAITCNT for EXP_CNTs |
| 964 | // before the call. |
| 965 | if (MI.getOpcode() == SC_CALL) { |
| 966 | if (ScoreBrackets->getScoreUB(EXP_CNT) > |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 967 | ScoreBrackets->getScoreLB(EXP_CNT)) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 968 | ScoreBrackets->setScoreLB(EXP_CNT, ScoreBrackets->getScoreUB(EXP_CNT)); |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 969 | EmitWaitcnt |= CNT_MASK(EXP_CNT); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | #endif |
| 973 | |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 974 | // FIXME: Should not be relying on memoperands. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 975 | // Look at the source operands of every instruction to see if |
| 976 | // any of them results from a previous memory operation that affects |
| 977 | // its current usage. If so, an s_waitcnt instruction needs to be |
| 978 | // emitted. |
| 979 | // If the source operand was defined by a load, add the s_waitcnt |
| 980 | // instruction. |
| 981 | for (const MachineMemOperand *Memop : MI.memoperands()) { |
| 982 | unsigned AS = Memop->getAddrSpace(); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 983 | if (AS != AMDGPUAS::LOCAL_ADDRESS) |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 984 | continue; |
| 985 | unsigned RegNo = SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS; |
| 986 | // VM_CNT is only relevant to vgpr or LDS. |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 987 | ScoreBrackets->determineWait( |
| 988 | VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT), Wait); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 989 | } |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 990 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 991 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 992 | const MachineOperand &Op = MI.getOperand(I); |
| 993 | const MachineRegisterInfo &MRIA = *MRI; |
| 994 | RegInterval Interval = |
| 995 | ScoreBrackets->getRegInterval(&MI, TII, MRI, TRI, I, false); |
| 996 | for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) { |
| 997 | if (TRI->isVGPR(MRIA, Op.getReg())) { |
| 998 | // VM_CNT is only relevant to vgpr or LDS. |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 999 | ScoreBrackets->determineWait( |
| 1000 | VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT), Wait); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1001 | } |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1002 | ScoreBrackets->determineWait( |
| 1003 | LGKM_CNT, ScoreBrackets->getRegScore(RegNo, LGKM_CNT), Wait); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | // End of for loop that looks at all source operands to decide vm_wait_cnt |
| 1007 | // and lgk_wait_cnt. |
| 1008 | |
| 1009 | // Two cases are handled for destination operands: |
| 1010 | // 1) If the destination operand was defined by a load, add the s_waitcnt |
| 1011 | // instruction to guarantee the right WAW order. |
| 1012 | // 2) If a destination operand that was used by a recent export/store ins, |
| 1013 | // add s_waitcnt on exp_cnt to guarantee the WAR order. |
| 1014 | if (MI.mayStore()) { |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 1015 | // FIXME: Should not be relying on memoperands. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1016 | for (const MachineMemOperand *Memop : MI.memoperands()) { |
| 1017 | unsigned AS = Memop->getAddrSpace(); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 1018 | if (AS != AMDGPUAS::LOCAL_ADDRESS) |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1019 | continue; |
| 1020 | unsigned RegNo = SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS; |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1021 | ScoreBrackets->determineWait( |
| 1022 | VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT), Wait); |
| 1023 | ScoreBrackets->determineWait( |
| 1024 | EXP_CNT, ScoreBrackets->getRegScore(RegNo, EXP_CNT), Wait); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 1028 | MachineOperand &Def = MI.getOperand(I); |
| 1029 | const MachineRegisterInfo &MRIA = *MRI; |
| 1030 | RegInterval Interval = |
| 1031 | ScoreBrackets->getRegInterval(&MI, TII, MRI, TRI, I, true); |
| 1032 | for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) { |
| 1033 | if (TRI->isVGPR(MRIA, Def.getReg())) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1034 | ScoreBrackets->determineWait( |
| 1035 | VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT), Wait); |
| 1036 | ScoreBrackets->determineWait( |
| 1037 | EXP_CNT, ScoreBrackets->getRegScore(RegNo, EXP_CNT), Wait); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1038 | } |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1039 | ScoreBrackets->determineWait( |
| 1040 | LGKM_CNT, ScoreBrackets->getRegScore(RegNo, LGKM_CNT), Wait); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } // End of for loop that looks at all dest operands. |
| 1043 | } |
| 1044 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1045 | // Check to see if this is an S_BARRIER, and if an implicit S_WAITCNT 0 |
| 1046 | // occurs before the instruction. Doing it here prevents any additional |
| 1047 | // S_WAITCNTs from being emitted if the instruction was marked as |
| 1048 | // requiring a WAITCNT beforehand. |
Konstantin Zhuravlyov | be6c0ca | 2017-06-02 17:40:26 +0000 | [diff] [blame] | 1049 | if (MI.getOpcode() == AMDGPU::S_BARRIER && |
| 1050 | !ST->hasAutoWaitcntBeforeBarrier()) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1051 | Wait = AMDGPU::Waitcnt::allZero(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | // TODO: Remove this work-around, enable the assert for Bug 457939 |
| 1055 | // after fixing the scheduler. Also, the Shader Compiler code is |
| 1056 | // independent of target. |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 1057 | if (readsVCCZ(MI) && ST->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1058 | if (ScoreBrackets->getScoreLB(LGKM_CNT) < |
| 1059 | ScoreBrackets->getScoreUB(LGKM_CNT) && |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 1060 | ScoreBrackets->hasPendingEvent(SMEM_ACCESS)) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1061 | Wait.LgkmCnt = 0; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1065 | // Early-out if no wait is indicated. |
| 1066 | if (!ScoreBrackets->simplifyWaitcnt(Wait) && !IsForceEmitWaitcnt) { |
| 1067 | if (OldWaitcntInstr) { |
| 1068 | if (TrackedWaitcntSet.count(OldWaitcntInstr)) { |
| 1069 | TrackedWaitcntSet.erase(OldWaitcntInstr); |
| 1070 | OldWaitcntInstr->eraseFromParent(); |
Nicolai Haehnle | 61396ff | 2018-11-07 21:53:36 +0000 | [diff] [blame] | 1071 | } else { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1072 | int64_t Imm = OldWaitcntInstr->getOperand(0).getImm(); |
| 1073 | ScoreBrackets->applyWaitcnt(AMDGPU::decodeWaitcnt(IV, Imm)); |
Stanislav Mekhanoshin | db39b4b | 2018-02-08 00:18:35 +0000 | [diff] [blame] | 1074 | } |
Nicolai Haehnle | 61396ff | 2018-11-07 21:53:36 +0000 | [diff] [blame] | 1075 | } |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1076 | return; |
| 1077 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1078 | |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1079 | if (ForceEmitZeroWaitcnts) |
| 1080 | Wait = AMDGPU::Waitcnt::allZero(); |
| 1081 | |
| 1082 | if (ForceEmitWaitcnt[VM_CNT]) |
| 1083 | Wait.VmCnt = 0; |
| 1084 | if (ForceEmitWaitcnt[EXP_CNT]) |
| 1085 | Wait.ExpCnt = 0; |
| 1086 | if (ForceEmitWaitcnt[LGKM_CNT]) |
| 1087 | Wait.LgkmCnt = 0; |
| 1088 | |
| 1089 | ScoreBrackets->applyWaitcnt(Wait); |
| 1090 | |
| 1091 | AMDGPU::Waitcnt OldWait; |
| 1092 | if (OldWaitcntInstr) { |
| 1093 | OldWait = |
| 1094 | AMDGPU::decodeWaitcnt(IV, OldWaitcntInstr->getOperand(0).getImm()); |
| 1095 | } |
| 1096 | if (OldWait.dominates(Wait)) |
| 1097 | return; |
| 1098 | |
| 1099 | MachineLoop *ContainingLoop = MLI->getLoopFor(MI.getParent()); |
| 1100 | if (ContainingLoop) { |
| 1101 | MachineBasicBlock *TBB = ContainingLoop->getHeader(); |
| 1102 | BlockWaitcntBrackets *ScoreBracket = BlockWaitcntBracketsMap[TBB].get(); |
| 1103 | if (!ScoreBracket) { |
| 1104 | assert(!BlockVisitedSet.count(TBB)); |
| 1105 | BlockWaitcntBracketsMap[TBB] = |
| 1106 | llvm::make_unique<BlockWaitcntBrackets>(ST); |
| 1107 | ScoreBracket = BlockWaitcntBracketsMap[TBB].get(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1108 | } |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1109 | ScoreBracket->setRevisitLoop(true); |
| 1110 | LLVM_DEBUG(dbgs() << "set-revisit2: Block" |
| 1111 | << ContainingLoop->getHeader()->getNumber() << '\n';); |
| 1112 | } |
| 1113 | |
| 1114 | if (OldWaitcntInstr && !TrackedWaitcntSet.count(OldWaitcntInstr)) |
| 1115 | Wait = Wait.combined(OldWait); |
| 1116 | |
| 1117 | unsigned Enc = AMDGPU::encodeWaitcnt(IV, Wait); |
| 1118 | if (OldWaitcntInstr) { |
| 1119 | OldWaitcntInstr->getOperand(0).setImm(Enc); |
| 1120 | |
| 1121 | LLVM_DEBUG(dbgs() << "updateWaitcntInBlock\n" |
| 1122 | << "Old Instr: " << MI << '\n' |
| 1123 | << "New Instr: " << *OldWaitcntInstr << '\n'); |
| 1124 | } else { |
| 1125 | auto SWaitInst = BuildMI(*MI.getParent(), MI.getIterator(), |
| 1126 | MI.getDebugLoc(), TII->get(AMDGPU::S_WAITCNT)) |
| 1127 | .addImm(Enc); |
| 1128 | TrackedWaitcntSet.insert(SWaitInst); |
| 1129 | |
| 1130 | LLVM_DEBUG(dbgs() << "insertWaitcntInBlock\n" |
| 1131 | << "Old Instr: " << MI << '\n' |
| 1132 | << "New Instr: " << *SWaitInst << '\n'); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1133 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | void SIInsertWaitcnts::insertWaitcntBeforeCF(MachineBasicBlock &MBB, |
| 1137 | MachineInstr *Waitcnt) { |
| 1138 | if (MBB.empty()) { |
| 1139 | MBB.push_back(Waitcnt); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | MachineBasicBlock::iterator It = MBB.end(); |
| 1144 | MachineInstr *MI = &*(--It); |
| 1145 | if (MI->isBranch()) { |
| 1146 | MBB.insert(It, Waitcnt); |
| 1147 | } else { |
| 1148 | MBB.push_back(Waitcnt); |
| 1149 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 1152 | // This is a flat memory operation. Check to see if it has memory |
| 1153 | // tokens for both LDS and Memory, and if so mark it as a flat. |
| 1154 | bool SIInsertWaitcnts::mayAccessLDSThroughFlat(const MachineInstr &MI) const { |
| 1155 | if (MI.memoperands_empty()) |
| 1156 | return true; |
| 1157 | |
| 1158 | for (const MachineMemOperand *Memop : MI.memoperands()) { |
| 1159 | unsigned AS = Memop->getAddrSpace(); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 1160 | if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS) |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 1161 | return true; |
| 1162 | } |
| 1163 | |
| 1164 | return false; |
| 1165 | } |
| 1166 | |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 1167 | void SIInsertWaitcnts::updateEventWaitcntAfter( |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1168 | MachineInstr &Inst, BlockWaitcntBrackets *ScoreBrackets) { |
| 1169 | // Now look at the instruction opcode. If it is a memory access |
| 1170 | // instruction, update the upper-bound of the appropriate counter's |
| 1171 | // bracket and the destination operand scores. |
| 1172 | // TODO: Use the (TSFlags & SIInstrFlags::LGKM_CNT) property everywhere. |
Matt Arsenault | 6ab9ea9 | 2017-07-21 18:34:51 +0000 | [diff] [blame] | 1173 | if (TII->isDS(Inst) && TII->usesLGKM_CNT(Inst)) { |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 1174 | if (TII->hasModifiersSet(Inst, AMDGPU::OpName::gds)) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1175 | ScoreBrackets->updateByEvent(TII, TRI, MRI, GDS_ACCESS, Inst); |
| 1176 | ScoreBrackets->updateByEvent(TII, TRI, MRI, GDS_GPR_LOCK, Inst); |
| 1177 | } else { |
| 1178 | ScoreBrackets->updateByEvent(TII, TRI, MRI, LDS_ACCESS, Inst); |
| 1179 | } |
| 1180 | } else if (TII->isFLAT(Inst)) { |
| 1181 | assert(Inst.mayLoad() || Inst.mayStore()); |
Matt Arsenault | 6ab9ea9 | 2017-07-21 18:34:51 +0000 | [diff] [blame] | 1182 | |
| 1183 | if (TII->usesVM_CNT(Inst)) |
| 1184 | ScoreBrackets->updateByEvent(TII, TRI, MRI, VMEM_ACCESS, Inst); |
| 1185 | |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 1186 | if (TII->usesLGKM_CNT(Inst)) { |
Matt Arsenault | 6ab9ea9 | 2017-07-21 18:34:51 +0000 | [diff] [blame] | 1187 | ScoreBrackets->updateByEvent(TII, TRI, MRI, LDS_ACCESS, Inst); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1188 | |
Matt Arsenault | 0ed39d3 | 2017-07-21 18:54:54 +0000 | [diff] [blame] | 1189 | // This is a flat memory operation, so note it - it will require |
| 1190 | // that both the VM and LGKM be flushed to zero if it is pending when |
| 1191 | // a VM or LGKM dependency occurs. |
| 1192 | if (mayAccessLDSThroughFlat(Inst)) |
| 1193 | ScoreBrackets->setPendingFlat(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } else if (SIInstrInfo::isVMEM(Inst) && |
| 1196 | // TODO: get a better carve out. |
| 1197 | Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1 && |
| 1198 | Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1_SC && |
| 1199 | Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1_VOL) { |
| 1200 | ScoreBrackets->updateByEvent(TII, TRI, MRI, VMEM_ACCESS, Inst); |
Mark Searles | 2a19af6 | 2018-04-26 16:11:19 +0000 | [diff] [blame] | 1201 | if (ST->vmemWriteNeedsExpWaitcnt() && |
Mark Searles | 11d0a04 | 2017-05-31 16:44:23 +0000 | [diff] [blame] | 1202 | (Inst.mayStore() || AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1)) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1203 | ScoreBrackets->updateByEvent(TII, TRI, MRI, VMW_GPR_LOCK, Inst); |
| 1204 | } |
| 1205 | } else if (TII->isSMRD(Inst)) { |
| 1206 | ScoreBrackets->updateByEvent(TII, TRI, MRI, SMEM_ACCESS, Inst); |
| 1207 | } else { |
| 1208 | switch (Inst.getOpcode()) { |
| 1209 | case AMDGPU::S_SENDMSG: |
| 1210 | case AMDGPU::S_SENDMSGHALT: |
| 1211 | ScoreBrackets->updateByEvent(TII, TRI, MRI, SQ_MESSAGE, Inst); |
| 1212 | break; |
| 1213 | case AMDGPU::EXP: |
| 1214 | case AMDGPU::EXP_DONE: { |
| 1215 | int Imm = TII->getNamedOperand(Inst, AMDGPU::OpName::tgt)->getImm(); |
| 1216 | if (Imm >= 32 && Imm <= 63) |
| 1217 | ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_PARAM_ACCESS, Inst); |
| 1218 | else if (Imm >= 12 && Imm <= 15) |
| 1219 | ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_POS_ACCESS, Inst); |
| 1220 | else |
| 1221 | ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_GPR_LOCK, Inst); |
| 1222 | break; |
| 1223 | } |
| 1224 | case AMDGPU::S_MEMTIME: |
| 1225 | case AMDGPU::S_MEMREALTIME: |
| 1226 | ScoreBrackets->updateByEvent(TII, TRI, MRI, SMEM_ACCESS, Inst); |
| 1227 | break; |
| 1228 | default: |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 1234 | // Merge the score brackets of the Block's predecessors; |
| 1235 | // this merged score bracket is used when adding waitcnts to the Block |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1236 | void SIInsertWaitcnts::mergeInputScoreBrackets(MachineBasicBlock &Block) { |
| 1237 | BlockWaitcntBrackets *ScoreBrackets = BlockWaitcntBracketsMap[&Block].get(); |
| 1238 | int32_t MaxPending[NUM_INST_CNTS] = {0}; |
| 1239 | int32_t MaxFlat[NUM_INST_CNTS] = {0}; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1240 | |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 1241 | // For single basic block loops, we need to retain the Block's |
| 1242 | // score bracket to have accurate Pred info. So, make a copy of Block's |
| 1243 | // score bracket, clear() it (which retains several important bits of info), |
| 1244 | // populate, and then replace en masse. For non-single basic block loops, |
| 1245 | // just clear Block's current score bracket and repopulate in-place. |
| 1246 | bool IsSelfPred; |
| 1247 | std::unique_ptr<BlockWaitcntBrackets> S; |
| 1248 | |
| 1249 | IsSelfPred = (std::find(Block.pred_begin(), Block.pred_end(), &Block)) |
| 1250 | != Block.pred_end(); |
| 1251 | if (IsSelfPred) { |
| 1252 | S = llvm::make_unique<BlockWaitcntBrackets>(*ScoreBrackets); |
| 1253 | ScoreBrackets = S.get(); |
| 1254 | } |
| 1255 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1256 | ScoreBrackets->clear(); |
| 1257 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1258 | // See if there are any uninitialized predecessors. If so, emit an |
| 1259 | // s_waitcnt 0 at the beginning of the block. |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 1260 | for (MachineBasicBlock *Pred : Block.predecessors()) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1261 | BlockWaitcntBrackets *PredScoreBrackets = |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 1262 | BlockWaitcntBracketsMap[Pred].get(); |
| 1263 | bool Visited = BlockVisitedSet.count(Pred); |
Nicolai Haehnle | f96456c | 2018-11-29 11:06:18 +0000 | [diff] [blame^] | 1264 | if (!Visited) |
Tim Corringham | 6c6d5e2 | 2017-12-04 12:30:49 +0000 | [diff] [blame] | 1265 | continue; |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 1266 | for (auto T : inst_counter_types()) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1267 | int span = |
| 1268 | PredScoreBrackets->getScoreUB(T) - PredScoreBrackets->getScoreLB(T); |
| 1269 | MaxPending[T] = std::max(MaxPending[T], span); |
| 1270 | span = |
| 1271 | PredScoreBrackets->pendingFlat(T) - PredScoreBrackets->getScoreLB(T); |
| 1272 | MaxFlat[T] = std::max(MaxFlat[T], span); |
| 1273 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1276 | // Now set the current Block's brackets to the largest ending bracket. |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 1277 | for (auto T : inst_counter_types()) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1278 | ScoreBrackets->setScoreUB(T, MaxPending[T]); |
| 1279 | ScoreBrackets->setScoreLB(T, 0); |
| 1280 | ScoreBrackets->setLastFlat(T, MaxFlat[T]); |
| 1281 | } |
| 1282 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1283 | // Set the register scoreboard. |
| 1284 | for (MachineBasicBlock *Pred : Block.predecessors()) { |
Mark Searles | 24c92ee | 2018-02-07 02:21:21 +0000 | [diff] [blame] | 1285 | if (!BlockVisitedSet.count(Pred)) { |
Tim Corringham | 6c6d5e2 | 2017-12-04 12:30:49 +0000 | [diff] [blame] | 1286 | continue; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | BlockWaitcntBrackets *PredScoreBrackets = |
| 1290 | BlockWaitcntBracketsMap[Pred].get(); |
| 1291 | |
| 1292 | // Now merge the gpr_reg_score information |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 1293 | for (auto T : inst_counter_types()) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1294 | int PredLB = PredScoreBrackets->getScoreLB(T); |
| 1295 | int PredUB = PredScoreBrackets->getScoreUB(T); |
| 1296 | if (PredLB < PredUB) { |
| 1297 | int PredScale = MaxPending[T] - PredUB; |
| 1298 | // Merge vgpr scores. |
| 1299 | for (int J = 0; J <= PredScoreBrackets->getMaxVGPR(); J++) { |
| 1300 | int PredRegScore = PredScoreBrackets->getRegScore(J, T); |
| 1301 | if (PredRegScore <= PredLB) |
| 1302 | continue; |
| 1303 | int NewRegScore = PredScale + PredRegScore; |
| 1304 | ScoreBrackets->setRegScore( |
| 1305 | J, T, std::max(ScoreBrackets->getRegScore(J, T), NewRegScore)); |
| 1306 | } |
| 1307 | // Also need to merge sgpr scores for lgkm_cnt. |
| 1308 | if (T == LGKM_CNT) { |
| 1309 | for (int J = 0; J <= PredScoreBrackets->getMaxSGPR(); J++) { |
| 1310 | int PredRegScore = |
| 1311 | PredScoreBrackets->getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT); |
| 1312 | if (PredRegScore <= PredLB) |
| 1313 | continue; |
| 1314 | int NewRegScore = PredScale + PredRegScore; |
| 1315 | ScoreBrackets->setRegScore( |
| 1316 | J + NUM_ALL_VGPRS, LGKM_CNT, |
| 1317 | std::max( |
| 1318 | ScoreBrackets->getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT), |
| 1319 | NewRegScore)); |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 1325 | ScoreBrackets->mergePendingEvents(*PredScoreBrackets); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1326 | } |
Mark Searles | c3c02bd | 2018-03-14 22:04:32 +0000 | [diff] [blame] | 1327 | |
| 1328 | // if a single block loop, update the score brackets. Not needed for other |
| 1329 | // blocks, as we did this in-place |
| 1330 | if (IsSelfPred) { |
| 1331 | BlockWaitcntBracketsMap[&Block] = llvm::make_unique<BlockWaitcntBrackets>(*ScoreBrackets); |
| 1332 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Mark Searles | 1054541 | 2018-05-30 15:47:45 +0000 | [diff] [blame] | 1335 | /// Return true if the given basic block is a "bottom" block of a loop. |
| 1336 | /// This works even if the loop is discontiguous. This also handles |
| 1337 | /// multiple back-edges for the same "header" block of a loop. |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1338 | bool SIInsertWaitcnts::isLoopBottom(const MachineLoop *Loop, |
| 1339 | const MachineBasicBlock *Block) { |
| 1340 | for (MachineBasicBlock *MBB : Loop->blocks()) { |
| 1341 | if (MBB == Block && MBB->isSuccessor(Loop->getHeader())) { |
| 1342 | return true; |
| 1343 | } |
| 1344 | } |
| 1345 | return false; |
| 1346 | } |
| 1347 | |
| 1348 | /// Count the number of "bottom" basic blocks of a loop. |
| 1349 | unsigned SIInsertWaitcnts::countNumBottomBlocks(const MachineLoop *Loop) { |
| 1350 | unsigned Count = 0; |
| 1351 | for (MachineBasicBlock *MBB : Loop->blocks()) { |
| 1352 | if (MBB->isSuccessor(Loop->getHeader())) { |
| 1353 | Count++; |
| 1354 | } |
| 1355 | } |
| 1356 | return Count; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | // Generate s_waitcnt instructions where needed. |
| 1360 | void SIInsertWaitcnts::insertWaitcntInBlock(MachineFunction &MF, |
| 1361 | MachineBasicBlock &Block) { |
| 1362 | // Initialize the state information. |
| 1363 | mergeInputScoreBrackets(Block); |
| 1364 | |
| 1365 | BlockWaitcntBrackets *ScoreBrackets = BlockWaitcntBracketsMap[&Block].get(); |
| 1366 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1367 | LLVM_DEBUG({ |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 1368 | dbgs() << "*** Block" << Block.getNumber() << " ***"; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1369 | ScoreBrackets->dump(); |
| 1370 | }); |
| 1371 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1372 | // Walk over the instructions. |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1373 | MachineInstr *OldWaitcntInstr = nullptr; |
| 1374 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1375 | for (MachineBasicBlock::iterator Iter = Block.begin(), E = Block.end(); |
| 1376 | Iter != E;) { |
| 1377 | MachineInstr &Inst = *Iter; |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1378 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1379 | // Remove any previously existing waitcnts. |
| 1380 | if (Inst.getOpcode() == AMDGPU::S_WAITCNT) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1381 | if (OldWaitcntInstr) { |
| 1382 | if (TrackedWaitcntSet.count(OldWaitcntInstr)) { |
| 1383 | TrackedWaitcntSet.erase(OldWaitcntInstr); |
| 1384 | OldWaitcntInstr->eraseFromParent(); |
| 1385 | OldWaitcntInstr = nullptr; |
| 1386 | } else if (!TrackedWaitcntSet.count(&Inst)) { |
| 1387 | // Two successive s_waitcnt's, both of which are pre-existing and |
| 1388 | // are therefore preserved. |
| 1389 | int64_t Imm = OldWaitcntInstr->getOperand(0).getImm(); |
| 1390 | ScoreBrackets->applyWaitcnt(AMDGPU::decodeWaitcnt(IV, Imm)); |
| 1391 | } else { |
| 1392 | ++Iter; |
| 1393 | Inst.eraseFromParent(); |
| 1394 | continue; |
| 1395 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1396 | } |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1397 | |
| 1398 | OldWaitcntInstr = &Inst; |
| 1399 | ++Iter; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1400 | continue; |
| 1401 | } |
| 1402 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1403 | bool VCCZBugWorkAround = false; |
| 1404 | if (readsVCCZ(Inst) && |
Mark Searles | 24c92ee | 2018-02-07 02:21:21 +0000 | [diff] [blame] | 1405 | (!VCCZBugHandledSet.count(&Inst))) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1406 | if (ScoreBrackets->getScoreLB(LGKM_CNT) < |
| 1407 | ScoreBrackets->getScoreUB(LGKM_CNT) && |
Nicolai Haehnle | d1f45da | 2018-11-29 11:06:14 +0000 | [diff] [blame] | 1408 | ScoreBrackets->hasPendingEvent(SMEM_ACCESS)) { |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 1409 | if (ST->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1410 | VCCZBugWorkAround = true; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | // Generate an s_waitcnt instruction to be placed before |
| 1415 | // cur_Inst, if needed. |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1416 | generateWaitcntInstBefore(Inst, ScoreBrackets, OldWaitcntInstr); |
| 1417 | OldWaitcntInstr = nullptr; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1418 | |
Mark Searles | 70901b9 | 2018-04-24 15:59:59 +0000 | [diff] [blame] | 1419 | updateEventWaitcntAfter(Inst, ScoreBrackets); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1420 | |
| 1421 | #if 0 // TODO: implement resource type check controlled by options with ub = LB. |
| 1422 | // If this instruction generates a S_SETVSKIP because it is an |
| 1423 | // indexed resource, and we are on Tahiti, then it will also force |
| 1424 | // an S_WAITCNT vmcnt(0) |
| 1425 | if (RequireCheckResourceType(Inst, context)) { |
| 1426 | // Force the score to as if an S_WAITCNT vmcnt(0) is emitted. |
| 1427 | ScoreBrackets->setScoreLB(VM_CNT, |
Evgeny Mankov | bf97517 | 2017-08-16 16:47:29 +0000 | [diff] [blame] | 1428 | ScoreBrackets->getScoreUB(VM_CNT)); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1429 | } |
| 1430 | #endif |
| 1431 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1432 | LLVM_DEBUG({ |
Mark Searles | 94ae3b2 | 2018-01-30 17:17:06 +0000 | [diff] [blame] | 1433 | Inst.print(dbgs()); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1434 | ScoreBrackets->dump(); |
| 1435 | }); |
| 1436 | |
| 1437 | // Check to see if this is a GWS instruction. If so, and if this is CI or |
| 1438 | // VI, then the generated code sequence will include an S_WAITCNT 0. |
| 1439 | // TODO: Are these the only GWS instructions? |
| 1440 | if (Inst.getOpcode() == AMDGPU::DS_GWS_INIT || |
| 1441 | Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_V || |
| 1442 | Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_BR || |
| 1443 | Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_P || |
| 1444 | Inst.getOpcode() == AMDGPU::DS_GWS_BARRIER) { |
| 1445 | // TODO: && context->target_info->GwsRequiresMemViolTest() ) { |
Nicolai Haehnle | 1a94cbb | 2018-11-29 11:06:06 +0000 | [diff] [blame] | 1446 | ScoreBrackets->applyWaitcnt(AMDGPU::Waitcnt::allZero()); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | // TODO: Remove this work-around after fixing the scheduler and enable the |
| 1450 | // assert above. |
| 1451 | if (VCCZBugWorkAround) { |
| 1452 | // Restore the vccz bit. Any time a value is written to vcc, the vcc |
| 1453 | // bit is updated, so we can restore the bit by reading the value of |
| 1454 | // vcc and then writing it back to the register. |
| 1455 | BuildMI(Block, Inst, Inst.getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), |
| 1456 | AMDGPU::VCC) |
| 1457 | .addReg(AMDGPU::VCC); |
| 1458 | VCCZBugHandledSet.insert(&Inst); |
| 1459 | } |
| 1460 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1461 | ++Iter; |
| 1462 | } |
| 1463 | |
| 1464 | // Check if we need to force convergence at loop footer. |
| 1465 | MachineLoop *ContainingLoop = MLI->getLoopFor(&Block); |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1466 | if (ContainingLoop && isLoopBottom(ContainingLoop, &Block)) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1467 | LoopWaitcntData *WaitcntData = LoopWaitcntDataMap[ContainingLoop].get(); |
| 1468 | WaitcntData->print(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1469 | LLVM_DEBUG(dbgs() << '\n';); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1470 | |
| 1471 | // The iterative waitcnt insertion algorithm aims for optimal waitcnt |
Mark Searles | 1054541 | 2018-05-30 15:47:45 +0000 | [diff] [blame] | 1472 | // placement, but doesn't guarantee convergence for a loop. Each |
| 1473 | // loop should take at most (n+1) iterations for it to converge naturally, |
| 1474 | // where n is the number of bottom blocks. If this threshold is reached and |
| 1475 | // the result hasn't converged, then we force convergence by inserting |
| 1476 | // a s_waitcnt at the end of loop footer. |
| 1477 | if (WaitcntData->getIterCnt() > (countNumBottomBlocks(ContainingLoop) + 1)) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1478 | // To ensure convergence, need to make wait events at loop footer be no |
| 1479 | // more than those from the previous iteration. |
Mark Searles | 6520792 | 2018-02-19 19:19:59 +0000 | [diff] [blame] | 1480 | // As a simplification, instead of tracking individual scores and |
| 1481 | // generating the precise wait count, just wait on 0. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1482 | bool HasPending = false; |
| 1483 | MachineInstr *SWaitInst = WaitcntData->getWaitcnt(); |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 1484 | for (auto T : inst_counter_types()) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1485 | if (ScoreBrackets->getScoreUB(T) > ScoreBrackets->getScoreLB(T)) { |
| 1486 | ScoreBrackets->setScoreLB(T, ScoreBrackets->getScoreUB(T)); |
| 1487 | HasPending = true; |
Mark Searles | 1054541 | 2018-05-30 15:47:45 +0000 | [diff] [blame] | 1488 | break; |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | if (HasPending) { |
| 1493 | if (!SWaitInst) { |
Mark Searles | 1054541 | 2018-05-30 15:47:45 +0000 | [diff] [blame] | 1494 | SWaitInst = BuildMI(Block, Block.getFirstNonPHI(), |
| 1495 | DebugLoc(), TII->get(AMDGPU::S_WAITCNT)) |
| 1496 | .addImm(0); |
Mark Searles | 24c92ee | 2018-02-07 02:21:21 +0000 | [diff] [blame] | 1497 | TrackedWaitcntSet.insert(SWaitInst); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1498 | #if 0 // TODO: Format the debug output |
| 1499 | OutputTransformBanner("insertWaitcntInBlock",0,"Create:",context); |
| 1500 | OutputTransformAdd(SWaitInst, context); |
| 1501 | #endif |
| 1502 | } |
| 1503 | #if 0 // TODO: ?? |
| 1504 | _DEV( REPORTED_STATS->force_waitcnt_converge = 1; ) |
| 1505 | #endif |
| 1506 | } |
| 1507 | |
| 1508 | if (SWaitInst) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1509 | LLVM_DEBUG({ |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1510 | SWaitInst->print(dbgs()); |
| 1511 | dbgs() << "\nAdjusted score board:"; |
| 1512 | ScoreBrackets->dump(); |
| 1513 | }); |
| 1514 | |
| 1515 | // Add this waitcnt to the block. It is either newly created or |
| 1516 | // created in previous iterations and added back since block traversal |
Mark Searles | 6520792 | 2018-02-19 19:19:59 +0000 | [diff] [blame] | 1517 | // always removes waitcnts. |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1518 | insertWaitcntBeforeCF(Block, SWaitInst); |
| 1519 | WaitcntData->setWaitcnt(SWaitInst); |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | bool SIInsertWaitcnts::runOnMachineFunction(MachineFunction &MF) { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 1526 | ST = &MF.getSubtarget<GCNSubtarget>(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1527 | TII = ST->getInstrInfo(); |
| 1528 | TRI = &TII->getRegisterInfo(); |
| 1529 | MRI = &MF.getRegInfo(); |
| 1530 | MLI = &getAnalysis<MachineLoopInfo>(); |
Konstantin Zhuravlyov | 71e43ee | 2018-09-12 18:50:47 +0000 | [diff] [blame] | 1531 | IV = AMDGPU::getIsaVersion(ST->getCPU()); |
Mark Searles | 11d0a04 | 2017-05-31 16:44:23 +0000 | [diff] [blame] | 1532 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1533 | |
Mark Searles | 4a0f2c5 | 2018-05-07 14:43:28 +0000 | [diff] [blame] | 1534 | ForceEmitZeroWaitcnts = ForceEmitZeroFlag; |
Nicolai Haehnle | ae369d7 | 2018-11-29 11:06:11 +0000 | [diff] [blame] | 1535 | for (auto T : inst_counter_types()) |
Mark Searles | ec58183 | 2018-04-25 19:21:26 +0000 | [diff] [blame] | 1536 | ForceEmitWaitcnt[T] = false; |
| 1537 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1538 | HardwareLimits.VmcntMax = AMDGPU::getVmcntBitMask(IV); |
| 1539 | HardwareLimits.ExpcntMax = AMDGPU::getExpcntBitMask(IV); |
| 1540 | HardwareLimits.LgkmcntMax = AMDGPU::getLgkmcntBitMask(IV); |
| 1541 | |
| 1542 | HardwareLimits.NumVGPRsMax = ST->getAddressableNumVGPRs(); |
| 1543 | HardwareLimits.NumSGPRsMax = ST->getAddressableNumSGPRs(); |
| 1544 | assert(HardwareLimits.NumVGPRsMax <= SQ_MAX_PGM_VGPRS); |
| 1545 | assert(HardwareLimits.NumSGPRsMax <= SQ_MAX_PGM_SGPRS); |
| 1546 | |
| 1547 | RegisterEncoding.VGPR0 = TRI->getEncodingValue(AMDGPU::VGPR0); |
| 1548 | RegisterEncoding.VGPRL = |
| 1549 | RegisterEncoding.VGPR0 + HardwareLimits.NumVGPRsMax - 1; |
| 1550 | RegisterEncoding.SGPR0 = TRI->getEncodingValue(AMDGPU::SGPR0); |
| 1551 | RegisterEncoding.SGPRL = |
| 1552 | RegisterEncoding.SGPR0 + HardwareLimits.NumSGPRsMax - 1; |
| 1553 | |
Mark Searles | 24c92ee | 2018-02-07 02:21:21 +0000 | [diff] [blame] | 1554 | TrackedWaitcntSet.clear(); |
| 1555 | BlockVisitedSet.clear(); |
| 1556 | VCCZBugHandledSet.clear(); |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1557 | LoopWaitcntDataMap.clear(); |
Scott Linder | 5792dd0 | 2018-06-21 18:48:48 +0000 | [diff] [blame] | 1558 | BlockWaitcntProcessedSet.clear(); |
Mark Searles | 24c92ee | 2018-02-07 02:21:21 +0000 | [diff] [blame] | 1559 | |
Nicolai Haehnle | 0ab31c9 | 2018-11-07 21:53:29 +0000 | [diff] [blame] | 1560 | // Walk over the blocks in reverse post order, inserting |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1561 | // s_waitcnt where needed. |
| 1562 | ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); |
| 1563 | bool Modified = false; |
| 1564 | for (ReversePostOrderTraversal<MachineFunction *>::rpo_iterator |
| 1565 | I = RPOT.begin(), |
| 1566 | E = RPOT.end(), J = RPOT.begin(); |
| 1567 | I != E;) { |
| 1568 | MachineBasicBlock &MBB = **I; |
| 1569 | |
| 1570 | BlockVisitedSet.insert(&MBB); |
| 1571 | |
| 1572 | BlockWaitcntBrackets *ScoreBrackets = BlockWaitcntBracketsMap[&MBB].get(); |
| 1573 | if (!ScoreBrackets) { |
Mark Searles | f0b93f1 | 2018-06-04 16:51:59 +0000 | [diff] [blame] | 1574 | BlockWaitcntBracketsMap[&MBB] = llvm::make_unique<BlockWaitcntBrackets>(ST); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1575 | ScoreBrackets = BlockWaitcntBracketsMap[&MBB].get(); |
| 1576 | } |
| 1577 | ScoreBrackets->setPostOrder(MBB.getNumber()); |
| 1578 | MachineLoop *ContainingLoop = MLI->getLoopFor(&MBB); |
| 1579 | if (ContainingLoop && LoopWaitcntDataMap[ContainingLoop] == nullptr) |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 1580 | LoopWaitcntDataMap[ContainingLoop] = llvm::make_unique<LoopWaitcntData>(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1581 | |
| 1582 | // If we are walking into the block from before the loop, then guarantee |
| 1583 | // at least 1 re-walk over the loop to propagate the information, even if |
| 1584 | // no S_WAITCNT instructions were generated. |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1585 | if (ContainingLoop && ContainingLoop->getHeader() == &MBB) { |
| 1586 | unsigned Count = countNumBottomBlocks(ContainingLoop); |
| 1587 | |
| 1588 | // If the loop has multiple back-edges, and so more than one "bottom" |
| 1589 | // basic block, we have to guarantee a re-walk over every blocks. |
| 1590 | if ((std::count(BlockWaitcntProcessedSet.begin(), |
Mark Searles | f4e7025 | 2018-07-16 10:21:36 +0000 | [diff] [blame] | 1591 | BlockWaitcntProcessedSet.end(), &MBB) < (int)Count)) { |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1592 | BlockWaitcntBracketsMap[&MBB]->setRevisitLoop(true); |
Mark Searles | 1054541 | 2018-05-30 15:47:45 +0000 | [diff] [blame] | 1593 | LLVM_DEBUG(dbgs() << "set-revisit1: Block" |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1594 | << ContainingLoop->getHeader()->getNumber() << '\n';); |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1595 | } |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | // Walk over the instructions. |
| 1599 | insertWaitcntInBlock(MF, MBB); |
| 1600 | |
Mark Searles | 1054541 | 2018-05-30 15:47:45 +0000 | [diff] [blame] | 1601 | // Record that waitcnts have been processed at least once for this block. |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1602 | BlockWaitcntProcessedSet.push_back(&MBB); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1603 | |
Mark Searles | 1bc6e71 | 2018-04-19 15:42:30 +0000 | [diff] [blame] | 1604 | // See if we want to revisit the loop. If a loop has multiple back-edges, |
| 1605 | // we shouldn't revisit the same "bottom" basic block. |
| 1606 | if (ContainingLoop && isLoopBottom(ContainingLoop, &MBB) && |
| 1607 | std::count(BlockWaitcntProcessedSet.begin(), |
| 1608 | BlockWaitcntProcessedSet.end(), &MBB) == 1) { |
Kannan Narayanan | 5e73b04 | 2017-05-05 21:10:17 +0000 | [diff] [blame] | 1609 | MachineBasicBlock *EntryBB = ContainingLoop->getHeader(); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1610 | BlockWaitcntBrackets *EntrySB = BlockWaitcntBracketsMap[EntryBB].get(); |
| 1611 | if (EntrySB && EntrySB->getRevisitLoop()) { |
| 1612 | EntrySB->setRevisitLoop(false); |
| 1613 | J = I; |
| 1614 | int32_t PostOrder = EntrySB->getPostOrder(); |
| 1615 | // TODO: Avoid this loop. Find another way to set I. |
| 1616 | for (ReversePostOrderTraversal<MachineFunction *>::rpo_iterator |
| 1617 | X = RPOT.begin(), |
| 1618 | Y = RPOT.end(); |
| 1619 | X != Y; ++X) { |
| 1620 | MachineBasicBlock &MBBX = **X; |
| 1621 | if (MBBX.getNumber() == PostOrder) { |
| 1622 | I = X; |
| 1623 | break; |
| 1624 | } |
| 1625 | } |
| 1626 | LoopWaitcntData *WaitcntData = LoopWaitcntDataMap[ContainingLoop].get(); |
| 1627 | WaitcntData->incIterCnt(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1628 | LLVM_DEBUG(dbgs() << "revisit: Block" << EntryBB->getNumber() << '\n';); |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1629 | continue; |
| 1630 | } else { |
| 1631 | LoopWaitcntData *WaitcntData = LoopWaitcntDataMap[ContainingLoop].get(); |
| 1632 | // Loop converged, reset iteration count. If this loop gets revisited, |
| 1633 | // it must be from an outer loop, the counter will restart, this will |
| 1634 | // ensure we don't force convergence on such revisits. |
| 1635 | WaitcntData->resetIterCnt(); |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | J = I; |
| 1640 | ++I; |
| 1641 | } |
| 1642 | |
| 1643 | SmallVector<MachineBasicBlock *, 4> EndPgmBlocks; |
| 1644 | |
| 1645 | bool HaveScalarStores = false; |
| 1646 | |
| 1647 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE; |
| 1648 | ++BI) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1649 | MachineBasicBlock &MBB = *BI; |
| 1650 | |
| 1651 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; |
| 1652 | ++I) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1653 | if (!HaveScalarStores && TII->isScalarStore(*I)) |
| 1654 | HaveScalarStores = true; |
| 1655 | |
| 1656 | if (I->getOpcode() == AMDGPU::S_ENDPGM || |
| 1657 | I->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) |
| 1658 | EndPgmBlocks.push_back(&MBB); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | if (HaveScalarStores) { |
| 1663 | // If scalar writes are used, the cache must be flushed or else the next |
| 1664 | // wave to reuse the same scratch memory can be clobbered. |
| 1665 | // |
| 1666 | // Insert s_dcache_wb at wave termination points if there were any scalar |
| 1667 | // stores, and only if the cache hasn't already been flushed. This could be |
| 1668 | // improved by looking across blocks for flushes in postdominating blocks |
| 1669 | // from the stores but an explicitly requested flush is probably very rare. |
| 1670 | for (MachineBasicBlock *MBB : EndPgmBlocks) { |
| 1671 | bool SeenDCacheWB = false; |
| 1672 | |
| 1673 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; |
| 1674 | ++I) { |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1675 | if (I->getOpcode() == AMDGPU::S_DCACHE_WB) |
| 1676 | SeenDCacheWB = true; |
| 1677 | else if (TII->isScalarStore(*I)) |
| 1678 | SeenDCacheWB = false; |
| 1679 | |
| 1680 | // FIXME: It would be better to insert this before a waitcnt if any. |
| 1681 | if ((I->getOpcode() == AMDGPU::S_ENDPGM || |
| 1682 | I->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) && |
| 1683 | !SeenDCacheWB) { |
| 1684 | Modified = true; |
| 1685 | BuildMI(*MBB, I, I->getDebugLoc(), TII->get(AMDGPU::S_DCACHE_WB)); |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
Mark Searles | 11d0a04 | 2017-05-31 16:44:23 +0000 | [diff] [blame] | 1691 | if (!MFI->isEntryFunction()) { |
| 1692 | // Wait for any outstanding memory operations that the input registers may |
Hiroshi Inoue | c8e9245 | 2018-01-29 05:17:03 +0000 | [diff] [blame] | 1693 | // depend on. We can't track them and it's better to the wait after the |
Mark Searles | 11d0a04 | 2017-05-31 16:44:23 +0000 | [diff] [blame] | 1694 | // costly call sequence. |
| 1695 | |
| 1696 | // TODO: Could insert earlier and schedule more liberally with operations |
| 1697 | // that only use caller preserved registers. |
| 1698 | MachineBasicBlock &EntryBB = MF.front(); |
Mark Searles | ed54ff1 | 2018-05-30 16:27:57 +0000 | [diff] [blame] | 1699 | BuildMI(EntryBB, EntryBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WAITCNT)) |
| 1700 | .addImm(0); |
Mark Searles | 11d0a04 | 2017-05-31 16:44:23 +0000 | [diff] [blame] | 1701 | |
| 1702 | Modified = true; |
| 1703 | } |
| 1704 | |
Kannan Narayanan | acb089e | 2017-04-12 03:25:12 +0000 | [diff] [blame] | 1705 | return Modified; |
| 1706 | } |