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