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