blob: 3c13bccd94fa8552d5e8e3fbf52e0198b29d013b [file] [log] [blame]
Eugene Zelenko59e12822017-08-08 00:47:13 +00001//===- SIInsertWaitcnts.cpp - Insert Wait Instructions --------------------===//
Kannan Narayananacb089e2017-04-12 03:25:12 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// Insert wait instructions for memory reads and writes.
Kannan Narayananacb089e2017-04-12 03:25:12 +000012///
13/// Memory reads and writes are issued asynchronously, so we need to insert
14/// S_WAITCNT instructions when we want to access any of their results or
15/// overwrite any register that's used asynchronously.
Nicolai Haehnled1f45da2018-11-29 11:06:14 +000016///
17/// TODO: This pass currently keeps one timeline per hardware counter. A more
18/// finely-grained approach that keeps one timeline per event type could
19/// sometimes get away with generating weaker s_waitcnt instructions. For
20/// example, when both SMEM and LDS are in flight and we need to wait for
21/// the i-th-last LDS instruction, then an lgkmcnt(i) is actually sufficient,
22/// but the pass will currently generate a conservative lgkmcnt(0) because
23/// multiple event types are in flight.
Kannan Narayananacb089e2017-04-12 03:25:12 +000024//
25//===----------------------------------------------------------------------===//
26
27#include "AMDGPU.h"
28#include "AMDGPUSubtarget.h"
29#include "SIDefines.h"
30#include "SIInstrInfo.h"
31#include "SIMachineFunctionInfo.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000032#include "SIRegisterInfo.h"
Kannan Narayananacb089e2017-04-12 03:25:12 +000033#include "Utils/AMDGPUBaseInfo.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000034#include "llvm/ADT/DenseMap.h"
35#include "llvm/ADT/DenseSet.h"
Kannan Narayananacb089e2017-04-12 03:25:12 +000036#include "llvm/ADT/PostOrderIterator.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000037#include "llvm/ADT/STLExtras.h"
38#include "llvm/ADT/SmallVector.h"
39#include "llvm/CodeGen/MachineBasicBlock.h"
Kannan Narayananacb089e2017-04-12 03:25:12 +000040#include "llvm/CodeGen/MachineFunction.h"
41#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000042#include "llvm/CodeGen/MachineInstr.h"
Kannan Narayananacb089e2017-04-12 03:25:12 +000043#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000044#include "llvm/CodeGen/MachineMemOperand.h"
45#include "llvm/CodeGen/MachineOperand.h"
Kannan Narayananacb089e2017-04-12 03:25:12 +000046#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000047#include "llvm/IR/DebugLoc.h"
48#include "llvm/Pass.h"
49#include "llvm/Support/Debug.h"
Mark Searlesec581832018-04-25 19:21:26 +000050#include "llvm/Support/DebugCounter.h"
Eugene Zelenko59e12822017-08-08 00:47:13 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/raw_ostream.h"
53#include <algorithm>
54#include <cassert>
55#include <cstdint>
56#include <cstring>
57#include <memory>
58#include <utility>
59#include <vector>
Kannan Narayananacb089e2017-04-12 03:25:12 +000060
Mark Searlesec581832018-04-25 19:21:26 +000061using namespace llvm;
62
Kannan Narayananacb089e2017-04-12 03:25:12 +000063#define DEBUG_TYPE "si-insert-waitcnts"
64
Mark Searlesec581832018-04-25 19:21:26 +000065DEBUG_COUNTER(ForceExpCounter, DEBUG_TYPE"-forceexp",
66 "Force emit s_waitcnt expcnt(0) instrs");
67DEBUG_COUNTER(ForceLgkmCounter, DEBUG_TYPE"-forcelgkm",
68 "Force emit s_waitcnt lgkmcnt(0) instrs");
69DEBUG_COUNTER(ForceVMCounter, DEBUG_TYPE"-forcevm",
70 "Force emit s_waitcnt vmcnt(0) instrs");
71
72static cl::opt<unsigned> ForceEmitZeroFlag(
73 "amdgpu-waitcnt-forcezero",
74 cl::desc("Force all waitcnt instrs to be emitted as s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)"),
75 cl::init(0), cl::Hidden);
Kannan Narayananacb089e2017-04-12 03:25:12 +000076
77namespace {
78
Nicolai Haehnleae369d72018-11-29 11:06:11 +000079template <typename EnumT>
80class enum_iterator
81 : public iterator_facade_base<enum_iterator<EnumT>,
82 std::forward_iterator_tag, const EnumT> {
83 EnumT Value;
84public:
85 enum_iterator() = default;
86 enum_iterator(EnumT Value) : Value(Value) {}
87
88 enum_iterator &operator++() {
89 Value = static_cast<EnumT>(Value + 1);
90 return *this;
91 }
92
93 bool operator==(const enum_iterator &RHS) const { return Value == RHS.Value; }
94
95 EnumT operator*() const { return Value; }
96};
97
Kannan Narayananacb089e2017-04-12 03:25:12 +000098// Class of object that encapsulates latest instruction counter score
99// associated with the operand. Used for determining whether
100// s_waitcnt instruction needs to be emited.
101
102#define CNT_MASK(t) (1u << (t))
103
104enum InstCounterType { VM_CNT = 0, LGKM_CNT, EXP_CNT, NUM_INST_CNTS };
105
Nicolai Haehnleae369d72018-11-29 11:06:11 +0000106iterator_range<enum_iterator<InstCounterType>> inst_counter_types() {
107 return make_range(enum_iterator<InstCounterType>(VM_CNT),
108 enum_iterator<InstCounterType>(NUM_INST_CNTS));
109}
110
Eugene Zelenko59e12822017-08-08 00:47:13 +0000111using RegInterval = std::pair<signed, signed>;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000112
113struct {
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000114 uint32_t VmcntMax;
115 uint32_t ExpcntMax;
116 uint32_t LgkmcntMax;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000117 int32_t NumVGPRsMax;
118 int32_t NumSGPRsMax;
119} HardwareLimits;
120
121struct {
122 unsigned VGPR0;
123 unsigned VGPRL;
124 unsigned SGPR0;
125 unsigned SGPRL;
126} RegisterEncoding;
127
128enum WaitEventType {
129 VMEM_ACCESS, // vector-memory read & write
130 LDS_ACCESS, // lds read & write
131 GDS_ACCESS, // gds read & write
132 SQ_MESSAGE, // send message
133 SMEM_ACCESS, // scalar-memory read & write
134 EXP_GPR_LOCK, // export holding on its data src
135 GDS_GPR_LOCK, // GDS holding on its data and addr src
136 EXP_POS_ACCESS, // write to export position
137 EXP_PARAM_ACCESS, // write to export parameter
138 VMW_GPR_LOCK, // vector-memory write holding on its data src
139 NUM_WAIT_EVENTS,
140};
141
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000142static const uint32_t WaitEventMaskForInst[NUM_INST_CNTS] = {
143 (1 << VMEM_ACCESS),
144 (1 << SMEM_ACCESS) | (1 << LDS_ACCESS) | (1 << GDS_ACCESS) |
145 (1 << SQ_MESSAGE),
146 (1 << EXP_GPR_LOCK) | (1 << GDS_GPR_LOCK) | (1 << VMW_GPR_LOCK) |
147 (1 << EXP_PARAM_ACCESS) | (1 << EXP_POS_ACCESS),
148};
Nicolai Haehnleae369d72018-11-29 11:06:11 +0000149
Kannan Narayananacb089e2017-04-12 03:25:12 +0000150// The mapping is:
151// 0 .. SQ_MAX_PGM_VGPRS-1 real VGPRs
152// SQ_MAX_PGM_VGPRS .. NUM_ALL_VGPRS-1 extra VGPR-like slots
153// NUM_ALL_VGPRS .. NUM_ALL_VGPRS+SQ_MAX_PGM_SGPRS-1 real SGPRs
154// We reserve a fixed number of VGPR slots in the scoring tables for
155// special tokens like SCMEM_LDS (needed for buffer load to LDS).
156enum RegisterMapping {
157 SQ_MAX_PGM_VGPRS = 256, // Maximum programmable VGPRs across all targets.
158 SQ_MAX_PGM_SGPRS = 256, // Maximum programmable SGPRs across all targets.
159 NUM_EXTRA_VGPRS = 1, // A reserved slot for DS.
160 EXTRA_VGPR_LDS = 0, // This is a placeholder the Shader algorithm uses.
161 NUM_ALL_VGPRS = SQ_MAX_PGM_VGPRS + NUM_EXTRA_VGPRS, // Where SGPR starts.
162};
163
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000164void addWait(AMDGPU::Waitcnt &Wait, InstCounterType T, unsigned Count) {
165 switch (T) {
166 case VM_CNT:
167 Wait.VmCnt = std::min(Wait.VmCnt, Count);
168 break;
169 case EXP_CNT:
170 Wait.ExpCnt = std::min(Wait.ExpCnt, Count);
171 break;
172 case LGKM_CNT:
173 Wait.LgkmCnt = std::min(Wait.LgkmCnt, Count);
174 break;
175 default:
176 llvm_unreachable("bad InstCounterType");
177 }
178}
179
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000180// This objects maintains the current score brackets of each wait counter, and
181// a per-register scoreboard for each wait counter.
182//
Kannan Narayananacb089e2017-04-12 03:25:12 +0000183// We also maintain the latest score for every event type that can change the
184// waitcnt in order to know if there are multiple types of events within
185// the brackets. When multiple types of event happen in the bracket,
Mark Searlesc3c02bd2018-03-14 22:04:32 +0000186// wait count may get decreased out of order, therefore we need to put in
Kannan Narayananacb089e2017-04-12 03:25:12 +0000187// "s_waitcnt 0" before use.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000188class WaitcntBrackets {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000189public:
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000190 WaitcntBrackets(const GCNSubtarget *SubTarget) : ST(SubTarget) {
Nicolai Haehnleae369d72018-11-29 11:06:11 +0000191 for (auto T : inst_counter_types())
Eugene Zelenko59e12822017-08-08 00:47:13 +0000192 memset(VgprScores[T], 0, sizeof(VgprScores[T]));
Eugene Zelenko59e12822017-08-08 00:47:13 +0000193 }
194
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000195 static uint32_t getWaitCountMax(InstCounterType T) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000196 switch (T) {
197 case VM_CNT:
198 return HardwareLimits.VmcntMax;
199 case LGKM_CNT:
200 return HardwareLimits.LgkmcntMax;
201 case EXP_CNT:
202 return HardwareLimits.ExpcntMax;
203 default:
204 break;
205 }
206 return 0;
Eugene Zelenko59e12822017-08-08 00:47:13 +0000207 }
Kannan Narayananacb089e2017-04-12 03:25:12 +0000208
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000209 uint32_t getScoreLB(InstCounterType T) const {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000210 assert(T < NUM_INST_CNTS);
211 if (T >= NUM_INST_CNTS)
212 return 0;
213 return ScoreLBs[T];
Eugene Zelenko59e12822017-08-08 00:47:13 +0000214 }
Kannan Narayananacb089e2017-04-12 03:25:12 +0000215
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000216 uint32_t getScoreUB(InstCounterType T) const {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000217 assert(T < NUM_INST_CNTS);
218 if (T >= NUM_INST_CNTS)
219 return 0;
220 return ScoreUBs[T];
Eugene Zelenko59e12822017-08-08 00:47:13 +0000221 }
Kannan Narayananacb089e2017-04-12 03:25:12 +0000222
223 // Mapping from event to counter.
224 InstCounterType eventCounter(WaitEventType E) {
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000225 if (E == VMEM_ACCESS)
Kannan Narayananacb089e2017-04-12 03:25:12 +0000226 return VM_CNT;
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000227 if (WaitEventMaskForInst[LGKM_CNT] & (1 << E))
Kannan Narayananacb089e2017-04-12 03:25:12 +0000228 return LGKM_CNT;
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000229 assert(WaitEventMaskForInst[EXP_CNT] & (1 << E));
230 return EXP_CNT;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000231 }
232
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000233 uint32_t getRegScore(int GprNo, InstCounterType T) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000234 if (GprNo < NUM_ALL_VGPRS) {
235 return VgprScores[T][GprNo];
236 }
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000237 assert(T == LGKM_CNT);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000238 return SgprScores[GprNo - NUM_ALL_VGPRS];
239 }
240
241 void clear() {
242 memset(ScoreLBs, 0, sizeof(ScoreLBs));
243 memset(ScoreUBs, 0, sizeof(ScoreUBs));
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000244 PendingEvents = 0;
245 memset(MixedPendingEvents, 0, sizeof(MixedPendingEvents));
Nicolai Haehnleae369d72018-11-29 11:06:11 +0000246 for (auto T : inst_counter_types())
Kannan Narayananacb089e2017-04-12 03:25:12 +0000247 memset(VgprScores[T], 0, sizeof(VgprScores[T]));
Kannan Narayananacb089e2017-04-12 03:25:12 +0000248 memset(SgprScores, 0, sizeof(SgprScores));
249 }
250
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000251 bool merge(const WaitcntBrackets &Other);
252
Kannan Narayananacb089e2017-04-12 03:25:12 +0000253 RegInterval getRegInterval(const MachineInstr *MI, const SIInstrInfo *TII,
254 const MachineRegisterInfo *MRI,
255 const SIRegisterInfo *TRI, unsigned OpNo,
256 bool Def) const;
257
Kannan Narayananacb089e2017-04-12 03:25:12 +0000258 int32_t getMaxVGPR() const { return VgprUB; }
259 int32_t getMaxSGPR() const { return SgprUB; }
Eugene Zelenko59e12822017-08-08 00:47:13 +0000260
Nicolai Haehnlec548d912018-11-19 12:03:11 +0000261 bool counterOutOfOrder(InstCounterType T) const;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000262 bool simplifyWaitcnt(AMDGPU::Waitcnt &Wait) const;
263 bool simplifyWaitcnt(InstCounterType T, unsigned &Count) const;
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000264 void determineWait(InstCounterType T, uint32_t ScoreToWait,
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000265 AMDGPU::Waitcnt &Wait) const;
266 void applyWaitcnt(const AMDGPU::Waitcnt &Wait);
267 void applyWaitcnt(InstCounterType T, unsigned Count);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000268 void updateByEvent(const SIInstrInfo *TII, const SIRegisterInfo *TRI,
269 const MachineRegisterInfo *MRI, WaitEventType E,
270 MachineInstr &MI);
271
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000272 bool hasPending() const { return PendingEvents != 0; }
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000273 bool hasPendingEvent(WaitEventType E) const {
274 return PendingEvents & (1 << E);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000275 }
276
277 bool hasPendingFlat() const {
278 return ((LastFlat[LGKM_CNT] > ScoreLBs[LGKM_CNT] &&
279 LastFlat[LGKM_CNT] <= ScoreUBs[LGKM_CNT]) ||
280 (LastFlat[VM_CNT] > ScoreLBs[VM_CNT] &&
281 LastFlat[VM_CNT] <= ScoreUBs[VM_CNT]));
282 }
283
284 void setPendingFlat() {
285 LastFlat[VM_CNT] = ScoreUBs[VM_CNT];
286 LastFlat[LGKM_CNT] = ScoreUBs[LGKM_CNT];
287 }
288
Kannan Narayananacb089e2017-04-12 03:25:12 +0000289 void print(raw_ostream &);
290 void dump() { print(dbgs()); }
291
292private:
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000293 struct MergeInfo {
294 uint32_t OldLB;
295 uint32_t OtherLB;
296 uint32_t MyShift;
297 uint32_t OtherShift;
298 };
299 static bool mergeScore(const MergeInfo &M, uint32_t &Score,
300 uint32_t OtherScore);
301
302 void setScoreLB(InstCounterType T, uint32_t Val) {
303 assert(T < NUM_INST_CNTS);
304 if (T >= NUM_INST_CNTS)
305 return;
306 ScoreLBs[T] = Val;
307 }
308
309 void setScoreUB(InstCounterType T, uint32_t Val) {
310 assert(T < NUM_INST_CNTS);
311 if (T >= NUM_INST_CNTS)
312 return;
313 ScoreUBs[T] = Val;
314 if (T == EXP_CNT) {
315 uint32_t UB = ScoreUBs[T] - getWaitCountMax(EXP_CNT);
316 if (ScoreLBs[T] < UB && UB < ScoreUBs[T])
317 ScoreLBs[T] = UB;
318 }
319 }
320
321 void setRegScore(int GprNo, InstCounterType T, uint32_t Val) {
322 if (GprNo < NUM_ALL_VGPRS) {
323 if (GprNo > VgprUB) {
324 VgprUB = GprNo;
325 }
326 VgprScores[T][GprNo] = Val;
327 } else {
328 assert(T == LGKM_CNT);
329 if (GprNo - NUM_ALL_VGPRS > SgprUB) {
330 SgprUB = GprNo - NUM_ALL_VGPRS;
331 }
332 SgprScores[GprNo - NUM_ALL_VGPRS] = Val;
333 }
334 }
335
336 void setExpScore(const MachineInstr *MI, const SIInstrInfo *TII,
337 const SIRegisterInfo *TRI, const MachineRegisterInfo *MRI,
338 unsigned OpNo, uint32_t Val);
339
Tom Stellard5bfbae52018-07-11 20:59:01 +0000340 const GCNSubtarget *ST = nullptr;
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000341 uint32_t ScoreLBs[NUM_INST_CNTS] = {0};
342 uint32_t ScoreUBs[NUM_INST_CNTS] = {0};
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000343 uint32_t PendingEvents = 0;
344 bool MixedPendingEvents[NUM_INST_CNTS] = {false};
Kannan Narayananacb089e2017-04-12 03:25:12 +0000345 // Remember the last flat memory operation.
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000346 uint32_t LastFlat[NUM_INST_CNTS] = {0};
Kannan Narayananacb089e2017-04-12 03:25:12 +0000347 // wait_cnt scores for every vgpr.
348 // Keep track of the VgprUB and SgprUB to make merge at join efficient.
Eugene Zelenko59e12822017-08-08 00:47:13 +0000349 int32_t VgprUB = 0;
350 int32_t SgprUB = 0;
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000351 uint32_t VgprScores[NUM_INST_CNTS][NUM_ALL_VGPRS];
Kannan Narayananacb089e2017-04-12 03:25:12 +0000352 // Wait cnt scores for every sgpr, only lgkmcnt is relevant.
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000353 uint32_t SgprScores[SQ_MAX_PGM_SGPRS] = {0};
Kannan Narayananacb089e2017-04-12 03:25:12 +0000354};
355
Kannan Narayananacb089e2017-04-12 03:25:12 +0000356class SIInsertWaitcnts : public MachineFunctionPass {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000357private:
Tom Stellard5bfbae52018-07-11 20:59:01 +0000358 const GCNSubtarget *ST = nullptr;
Eugene Zelenko59e12822017-08-08 00:47:13 +0000359 const SIInstrInfo *TII = nullptr;
360 const SIRegisterInfo *TRI = nullptr;
361 const MachineRegisterInfo *MRI = nullptr;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000362 AMDGPU::IsaVersion IV;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000363
Mark Searles24c92ee2018-02-07 02:21:21 +0000364 DenseSet<MachineInstr *> TrackedWaitcntSet;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000365 DenseSet<MachineInstr *> VCCZBugHandledSet;
366
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000367 struct BlockInfo {
368 MachineBasicBlock *MBB;
369 std::unique_ptr<WaitcntBrackets> Incoming;
370 bool Dirty = true;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000371
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000372 explicit BlockInfo(MachineBasicBlock *MBB) : MBB(MBB) {}
373 };
Kannan Narayananacb089e2017-04-12 03:25:12 +0000374
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000375 std::vector<BlockInfo> BlockInfos; // by reverse post-order traversal index
376 DenseMap<MachineBasicBlock *, unsigned> RpotIdxMap;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000377
Mark Searles4a0f2c52018-05-07 14:43:28 +0000378 // ForceEmitZeroWaitcnts: force all waitcnts insts to be s_waitcnt 0
379 // because of amdgpu-waitcnt-forcezero flag
380 bool ForceEmitZeroWaitcnts;
Mark Searlesec581832018-04-25 19:21:26 +0000381 bool ForceEmitWaitcnt[NUM_INST_CNTS];
382
Kannan Narayananacb089e2017-04-12 03:25:12 +0000383public:
384 static char ID;
385
Konstantin Zhuravlyov77747772018-06-26 21:33:38 +0000386 SIInsertWaitcnts() : MachineFunctionPass(ID) {
387 (void)ForceExpCounter;
388 (void)ForceLgkmCounter;
389 (void)ForceVMCounter;
390 }
Kannan Narayananacb089e2017-04-12 03:25:12 +0000391
392 bool runOnMachineFunction(MachineFunction &MF) override;
393
394 StringRef getPassName() const override {
395 return "SI insert wait instructions";
396 }
397
398 void getAnalysisUsage(AnalysisUsage &AU) const override {
399 AU.setPreservesCFG();
Kannan Narayananacb089e2017-04-12 03:25:12 +0000400 MachineFunctionPass::getAnalysisUsage(AU);
401 }
402
Mark Searlesec581832018-04-25 19:21:26 +0000403 bool isForceEmitWaitcnt() const {
Nicolai Haehnleae369d72018-11-29 11:06:11 +0000404 for (auto T : inst_counter_types())
Mark Searlesec581832018-04-25 19:21:26 +0000405 if (ForceEmitWaitcnt[T])
406 return true;
407 return false;
408 }
409
410 void setForceEmitWaitcnt() {
411// For non-debug builds, ForceEmitWaitcnt has been initialized to false;
412// For debug builds, get the debug counter info and adjust if need be
413#ifndef NDEBUG
414 if (DebugCounter::isCounterSet(ForceExpCounter) &&
415 DebugCounter::shouldExecute(ForceExpCounter)) {
416 ForceEmitWaitcnt[EXP_CNT] = true;
417 } else {
418 ForceEmitWaitcnt[EXP_CNT] = false;
419 }
420
421 if (DebugCounter::isCounterSet(ForceLgkmCounter) &&
422 DebugCounter::shouldExecute(ForceLgkmCounter)) {
423 ForceEmitWaitcnt[LGKM_CNT] = true;
424 } else {
425 ForceEmitWaitcnt[LGKM_CNT] = false;
426 }
427
428 if (DebugCounter::isCounterSet(ForceVMCounter) &&
429 DebugCounter::shouldExecute(ForceVMCounter)) {
430 ForceEmitWaitcnt[VM_CNT] = true;
431 } else {
432 ForceEmitWaitcnt[VM_CNT] = false;
433 }
434#endif // NDEBUG
435 }
436
Matt Arsenault0ed39d32017-07-21 18:54:54 +0000437 bool mayAccessLDSThroughFlat(const MachineInstr &MI) const;
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000438 bool generateWaitcntInstBefore(MachineInstr &MI,
439 WaitcntBrackets &ScoreBrackets,
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000440 MachineInstr *OldWaitcntInstr);
Mark Searles70901b92018-04-24 15:59:59 +0000441 void updateEventWaitcntAfter(MachineInstr &Inst,
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000442 WaitcntBrackets *ScoreBrackets);
443 bool insertWaitcntInBlock(MachineFunction &MF, MachineBasicBlock &Block,
444 WaitcntBrackets &ScoreBrackets);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000445};
446
Eugene Zelenko59e12822017-08-08 00:47:13 +0000447} // end anonymous namespace
Kannan Narayananacb089e2017-04-12 03:25:12 +0000448
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000449RegInterval WaitcntBrackets::getRegInterval(const MachineInstr *MI,
450 const SIInstrInfo *TII,
451 const MachineRegisterInfo *MRI,
452 const SIRegisterInfo *TRI,
453 unsigned OpNo, bool Def) const {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000454 const MachineOperand &Op = MI->getOperand(OpNo);
455 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()) ||
456 (Def && !Op.isDef()))
457 return {-1, -1};
458
459 // A use via a PW operand does not need a waitcnt.
460 // A partial write is not a WAW.
461 assert(!Op.getSubReg() || !Op.isUndef());
462
463 RegInterval Result;
464 const MachineRegisterInfo &MRIA = *MRI;
465
466 unsigned Reg = TRI->getEncodingValue(Op.getReg());
467
468 if (TRI->isVGPR(MRIA, Op.getReg())) {
469 assert(Reg >= RegisterEncoding.VGPR0 && Reg <= RegisterEncoding.VGPRL);
470 Result.first = Reg - RegisterEncoding.VGPR0;
471 assert(Result.first >= 0 && Result.first < SQ_MAX_PGM_VGPRS);
472 } else if (TRI->isSGPRReg(MRIA, Op.getReg())) {
473 assert(Reg >= RegisterEncoding.SGPR0 && Reg < SQ_MAX_PGM_SGPRS);
474 Result.first = Reg - RegisterEncoding.SGPR0 + NUM_ALL_VGPRS;
475 assert(Result.first >= NUM_ALL_VGPRS &&
476 Result.first < SQ_MAX_PGM_SGPRS + NUM_ALL_VGPRS);
477 }
478 // TODO: Handle TTMP
479 // else if (TRI->isTTMP(MRIA, Reg.getReg())) ...
480 else
481 return {-1, -1};
482
483 const MachineInstr &MIA = *MI;
484 const TargetRegisterClass *RC = TII->getOpRegClass(MIA, OpNo);
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000485 unsigned Size = TRI->getRegSizeInBits(*RC);
486 Result.second = Result.first + (Size / 32);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000487
488 return Result;
489}
490
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000491void WaitcntBrackets::setExpScore(const MachineInstr *MI,
492 const SIInstrInfo *TII,
493 const SIRegisterInfo *TRI,
494 const MachineRegisterInfo *MRI, unsigned OpNo,
495 uint32_t Val) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000496 RegInterval Interval = getRegInterval(MI, TII, MRI, TRI, OpNo, false);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000497 LLVM_DEBUG({
Kannan Narayananacb089e2017-04-12 03:25:12 +0000498 const MachineOperand &Opnd = MI->getOperand(OpNo);
499 assert(TRI->isVGPR(*MRI, Opnd.getReg()));
500 });
501 for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
502 setRegScore(RegNo, EXP_CNT, Val);
503 }
504}
505
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000506void WaitcntBrackets::updateByEvent(const SIInstrInfo *TII,
507 const SIRegisterInfo *TRI,
508 const MachineRegisterInfo *MRI,
509 WaitEventType E, MachineInstr &Inst) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000510 const MachineRegisterInfo &MRIA = *MRI;
511 InstCounterType T = eventCounter(E);
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000512 uint32_t CurrScore = getScoreUB(T) + 1;
513 if (CurrScore == 0)
514 report_fatal_error("InsertWaitcnt score wraparound");
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000515 // PendingEvents and ScoreUB need to be update regardless if this event
516 // changes the score of a register or not.
Kannan Narayananacb089e2017-04-12 03:25:12 +0000517 // Examples including vm_cnt when buffer-store or lgkm_cnt when send-message.
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000518 if (!hasPendingEvent(E)) {
519 if (PendingEvents & WaitEventMaskForInst[T])
520 MixedPendingEvents[T] = true;
521 PendingEvents |= 1 << E;
522 }
Kannan Narayananacb089e2017-04-12 03:25:12 +0000523 setScoreUB(T, CurrScore);
524
525 if (T == EXP_CNT) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000526 // Put score on the source vgprs. If this is a store, just use those
527 // specific register(s).
528 if (TII->isDS(Inst) && (Inst.mayStore() || Inst.mayLoad())) {
529 // All GDS operations must protect their address register (same as
530 // export.)
531 if (Inst.getOpcode() != AMDGPU::DS_APPEND &&
532 Inst.getOpcode() != AMDGPU::DS_CONSUME) {
533 setExpScore(
534 &Inst, TII, TRI, MRI,
535 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::addr),
536 CurrScore);
537 }
538 if (Inst.mayStore()) {
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000539 if (AMDGPU::getNamedOperandIdx(Inst.getOpcode(),
540 AMDGPU::OpName::data0) != -1) {
541 setExpScore(
542 &Inst, TII, TRI, MRI,
543 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data0),
544 CurrScore);
545 }
Kannan Narayananacb089e2017-04-12 03:25:12 +0000546 if (AMDGPU::getNamedOperandIdx(Inst.getOpcode(),
547 AMDGPU::OpName::data1) != -1) {
548 setExpScore(&Inst, TII, TRI, MRI,
549 AMDGPU::getNamedOperandIdx(Inst.getOpcode(),
550 AMDGPU::OpName::data1),
551 CurrScore);
552 }
553 } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1 &&
554 Inst.getOpcode() != AMDGPU::DS_GWS_INIT &&
555 Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_V &&
556 Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_BR &&
557 Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_P &&
558 Inst.getOpcode() != AMDGPU::DS_GWS_BARRIER &&
559 Inst.getOpcode() != AMDGPU::DS_APPEND &&
560 Inst.getOpcode() != AMDGPU::DS_CONSUME &&
561 Inst.getOpcode() != AMDGPU::DS_ORDERED_COUNT) {
562 for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
563 const MachineOperand &Op = Inst.getOperand(I);
564 if (Op.isReg() && !Op.isDef() && TRI->isVGPR(MRIA, Op.getReg())) {
565 setExpScore(&Inst, TII, TRI, MRI, I, CurrScore);
566 }
567 }
568 }
569 } else if (TII->isFLAT(Inst)) {
570 if (Inst.mayStore()) {
571 setExpScore(
572 &Inst, TII, TRI, MRI,
573 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
574 CurrScore);
575 } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) {
576 setExpScore(
577 &Inst, TII, TRI, MRI,
578 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
579 CurrScore);
580 }
581 } else if (TII->isMIMG(Inst)) {
582 if (Inst.mayStore()) {
583 setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore);
584 } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) {
585 setExpScore(
586 &Inst, TII, TRI, MRI,
587 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
588 CurrScore);
589 }
590 } else if (TII->isMTBUF(Inst)) {
591 if (Inst.mayStore()) {
592 setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore);
593 }
594 } else if (TII->isMUBUF(Inst)) {
595 if (Inst.mayStore()) {
596 setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore);
597 } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) {
598 setExpScore(
599 &Inst, TII, TRI, MRI,
600 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
601 CurrScore);
602 }
603 } else {
604 if (TII->isEXP(Inst)) {
605 // For export the destination registers are really temps that
606 // can be used as the actual source after export patching, so
607 // we need to treat them like sources and set the EXP_CNT
608 // score.
609 for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
610 MachineOperand &DefMO = Inst.getOperand(I);
611 if (DefMO.isReg() && DefMO.isDef() &&
612 TRI->isVGPR(MRIA, DefMO.getReg())) {
613 setRegScore(TRI->getEncodingValue(DefMO.getReg()), EXP_CNT,
614 CurrScore);
615 }
616 }
617 }
618 for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
619 MachineOperand &MO = Inst.getOperand(I);
620 if (MO.isReg() && !MO.isDef() && TRI->isVGPR(MRIA, MO.getReg())) {
621 setExpScore(&Inst, TII, TRI, MRI, I, CurrScore);
622 }
623 }
624 }
625#if 0 // TODO: check if this is handled by MUBUF code above.
626 } else if (Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORD ||
Evgeny Mankovbf975172017-08-16 16:47:29 +0000627 Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORDX2 ||
628 Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORDX4) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000629 MachineOperand *MO = TII->getNamedOperand(Inst, AMDGPU::OpName::data);
630 unsigned OpNo;//TODO: find the OpNo for this operand;
631 RegInterval Interval = getRegInterval(&Inst, TII, MRI, TRI, OpNo, false);
632 for (signed RegNo = Interval.first; RegNo < Interval.second;
Evgeny Mankovbf975172017-08-16 16:47:29 +0000633 ++RegNo) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000634 setRegScore(RegNo + NUM_ALL_VGPRS, t, CurrScore);
635 }
636#endif
637 } else {
638 // Match the score to the destination registers.
639 for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
640 RegInterval Interval = getRegInterval(&Inst, TII, MRI, TRI, I, true);
641 if (T == VM_CNT && Interval.first >= NUM_ALL_VGPRS)
642 continue;
643 for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
644 setRegScore(RegNo, T, CurrScore);
645 }
646 }
647 if (TII->isDS(Inst) && Inst.mayStore()) {
648 setRegScore(SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS, T, CurrScore);
649 }
650 }
651}
652
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000653void WaitcntBrackets::print(raw_ostream &OS) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000654 OS << '\n';
Nicolai Haehnleae369d72018-11-29 11:06:11 +0000655 for (auto T : inst_counter_types()) {
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000656 uint32_t LB = getScoreLB(T);
657 uint32_t UB = getScoreUB(T);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000658
659 switch (T) {
660 case VM_CNT:
661 OS << " VM_CNT(" << UB - LB << "): ";
662 break;
663 case LGKM_CNT:
664 OS << " LGKM_CNT(" << UB - LB << "): ";
665 break;
666 case EXP_CNT:
667 OS << " EXP_CNT(" << UB - LB << "): ";
668 break;
669 default:
670 OS << " UNKNOWN(" << UB - LB << "): ";
671 break;
672 }
673
674 if (LB < UB) {
675 // Print vgpr scores.
676 for (int J = 0; J <= getMaxVGPR(); J++) {
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000677 uint32_t RegScore = getRegScore(J, T);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000678 if (RegScore <= LB)
679 continue;
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000680 uint32_t RelScore = RegScore - LB - 1;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000681 if (J < SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS) {
682 OS << RelScore << ":v" << J << " ";
683 } else {
684 OS << RelScore << ":ds ";
685 }
686 }
687 // Also need to print sgpr scores for lgkm_cnt.
688 if (T == LGKM_CNT) {
689 for (int J = 0; J <= getMaxSGPR(); J++) {
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000690 uint32_t RegScore = getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000691 if (RegScore <= LB)
692 continue;
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000693 uint32_t RelScore = RegScore - LB - 1;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000694 OS << RelScore << ":s" << J << " ";
695 }
696 }
697 }
698 OS << '\n';
699 }
700 OS << '\n';
Kannan Narayananacb089e2017-04-12 03:25:12 +0000701}
702
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000703/// Simplify the waitcnt, in the sense of removing redundant counts, and return
704/// whether a waitcnt instruction is needed at all.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000705bool WaitcntBrackets::simplifyWaitcnt(AMDGPU::Waitcnt &Wait) const {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000706 return simplifyWaitcnt(VM_CNT, Wait.VmCnt) |
707 simplifyWaitcnt(EXP_CNT, Wait.ExpCnt) |
708 simplifyWaitcnt(LGKM_CNT, Wait.LgkmCnt);
709}
710
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000711bool WaitcntBrackets::simplifyWaitcnt(InstCounterType T,
712 unsigned &Count) const {
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000713 const uint32_t LB = getScoreLB(T);
714 const uint32_t UB = getScoreUB(T);
715 if (Count < UB && UB - Count > LB)
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000716 return true;
717
718 Count = ~0u;
719 return false;
720}
721
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000722void WaitcntBrackets::determineWait(InstCounterType T, uint32_t ScoreToWait,
723 AMDGPU::Waitcnt &Wait) const {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000724 // If the score of src_operand falls within the bracket, we need an
725 // s_waitcnt instruction.
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000726 const uint32_t LB = getScoreLB(T);
727 const uint32_t UB = getScoreUB(T);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000728 if ((UB >= ScoreToWait) && (ScoreToWait > LB)) {
Mark Searlesf0b93f12018-06-04 16:51:59 +0000729 if ((T == VM_CNT || T == LGKM_CNT) &&
730 hasPendingFlat() &&
731 !ST->hasFlatLgkmVMemCountInOrder()) {
732 // If there is a pending FLAT operation, and this is a VMem or LGKM
733 // waitcnt and the target can report early completion, then we need
734 // to force a waitcnt 0.
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000735 addWait(Wait, T, 0);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000736 } else if (counterOutOfOrder(T)) {
737 // Counter can get decremented out-of-order when there
Mark Searlesc3c02bd2018-03-14 22:04:32 +0000738 // are multiple types event in the bracket. Also emit an s_wait counter
Kannan Narayananacb089e2017-04-12 03:25:12 +0000739 // with a conservative value of 0 for the counter.
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000740 addWait(Wait, T, 0);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000741 } else {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000742 addWait(Wait, T, UB - ScoreToWait);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000743 }
744 }
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000745}
Kannan Narayananacb089e2017-04-12 03:25:12 +0000746
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000747void WaitcntBrackets::applyWaitcnt(const AMDGPU::Waitcnt &Wait) {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000748 applyWaitcnt(VM_CNT, Wait.VmCnt);
749 applyWaitcnt(EXP_CNT, Wait.ExpCnt);
750 applyWaitcnt(LGKM_CNT, Wait.LgkmCnt);
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000751}
752
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000753void WaitcntBrackets::applyWaitcnt(InstCounterType T, unsigned Count) {
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000754 const uint32_t UB = getScoreUB(T);
755 if (Count >= UB)
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000756 return;
757 if (Count != 0) {
758 if (counterOutOfOrder(T))
759 return;
Nicolai Haehnleab43bf62018-11-29 11:06:21 +0000760 setScoreLB(T, std::max(getScoreLB(T), UB - Count));
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000761 } else {
762 setScoreLB(T, UB);
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000763 MixedPendingEvents[T] = false;
764 PendingEvents &= ~WaitEventMaskForInst[T];
765 }
766}
767
Kannan Narayananacb089e2017-04-12 03:25:12 +0000768// Where there are multiple types of event in the bracket of a counter,
769// the decrement may go out of order.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000770bool WaitcntBrackets::counterOutOfOrder(InstCounterType T) const {
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000771 // Scalar memory read always can go out of order.
772 if (T == LGKM_CNT && hasPendingEvent(SMEM_ACCESS))
773 return true;
774 return MixedPendingEvents[T];
Kannan Narayananacb089e2017-04-12 03:25:12 +0000775}
776
777INITIALIZE_PASS_BEGIN(SIInsertWaitcnts, DEBUG_TYPE, "SI Insert Waitcnts", false,
778 false)
779INITIALIZE_PASS_END(SIInsertWaitcnts, DEBUG_TYPE, "SI Insert Waitcnts", false,
780 false)
781
782char SIInsertWaitcnts::ID = 0;
783
784char &llvm::SIInsertWaitcntsID = SIInsertWaitcnts::ID;
785
786FunctionPass *llvm::createSIInsertWaitcntsPass() {
787 return new SIInsertWaitcnts();
788}
789
790static bool readsVCCZ(const MachineInstr &MI) {
791 unsigned Opc = MI.getOpcode();
792 return (Opc == AMDGPU::S_CBRANCH_VCCNZ || Opc == AMDGPU::S_CBRANCH_VCCZ) &&
793 !MI.getOperand(1).isUndef();
794}
795
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000796/// Generate s_waitcnt instruction to be placed before cur_Inst.
Kannan Narayananacb089e2017-04-12 03:25:12 +0000797/// Instructions of a given type are returned in order,
798/// but instructions of different types can complete out of order.
799/// We rely on this in-order completion
800/// and simply assign a score to the memory access instructions.
801/// We keep track of the active "score bracket" to determine
802/// if an access of a memory read requires an s_waitcnt
803/// and if so what the value of each counter is.
804/// The "score bracket" is bound by the lower bound and upper bound
805/// scores (*_score_LB and *_score_ub respectively).
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000806bool SIInsertWaitcnts::generateWaitcntInstBefore(
807 MachineInstr &MI, WaitcntBrackets &ScoreBrackets,
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000808 MachineInstr *OldWaitcntInstr) {
Mark Searles4a0f2c52018-05-07 14:43:28 +0000809 setForceEmitWaitcnt();
Mark Searlesec581832018-04-25 19:21:26 +0000810 bool IsForceEmitWaitcnt = isForceEmitWaitcnt();
811
Nicolai Haehnle61396ff2018-11-07 21:53:36 +0000812 if (MI.isDebugInstr())
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000813 return false;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000814
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000815 AMDGPU::Waitcnt Wait;
816
Kannan Narayananacb089e2017-04-12 03:25:12 +0000817 // See if this instruction has a forced S_WAITCNT VM.
818 // TODO: Handle other cases of NeedsWaitcntVmBefore()
Nicolai Haehnlef96456c2018-11-29 11:06:18 +0000819 if (MI.getOpcode() == AMDGPU::BUFFER_WBINVL1 ||
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000820 MI.getOpcode() == AMDGPU::BUFFER_WBINVL1_SC ||
821 MI.getOpcode() == AMDGPU::BUFFER_WBINVL1_VOL) {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000822 Wait.VmCnt = 0;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000823 }
824
825 // All waits must be resolved at call return.
826 // NOTE: this could be improved with knowledge of all call sites or
827 // with knowledge of the called routines.
Tom Stellardc5a154d2018-06-28 23:47:12 +0000828 if (MI.getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG ||
Mark Searles11d0a042017-05-31 16:44:23 +0000829 MI.getOpcode() == AMDGPU::S_SETPC_B64_return) {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000830 Wait = AMDGPU::Waitcnt::allZero();
Kannan Narayananacb089e2017-04-12 03:25:12 +0000831 }
832 // Resolve vm waits before gs-done.
833 else if ((MI.getOpcode() == AMDGPU::S_SENDMSG ||
834 MI.getOpcode() == AMDGPU::S_SENDMSGHALT) &&
835 ((MI.getOperand(0).getImm() & AMDGPU::SendMsg::ID_MASK_) ==
836 AMDGPU::SendMsg::ID_GS_DONE)) {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000837 Wait.VmCnt = 0;
Kannan Narayananacb089e2017-04-12 03:25:12 +0000838 }
839#if 0 // TODO: the following blocks of logic when we have fence.
840 else if (MI.getOpcode() == SC_FENCE) {
841 const unsigned int group_size =
842 context->shader_info->GetMaxThreadGroupSize();
843 // group_size == 0 means thread group size is unknown at compile time
844 const bool group_is_multi_wave =
845 (group_size == 0 || group_size > target_info->GetWaveFrontSize());
846 const bool fence_is_global = !((SCInstInternalMisc*)Inst)->IsGroupFence();
847
848 for (unsigned int i = 0; i < Inst->NumSrcOperands(); i++) {
849 SCRegType src_type = Inst->GetSrcType(i);
850 switch (src_type) {
851 case SCMEM_LDS:
852 if (group_is_multi_wave ||
Evgeny Mankovbf975172017-08-16 16:47:29 +0000853 context->OptFlagIsOn(OPT_R1100_LDSMEM_FENCE_CHICKEN_BIT)) {
Mark Searles70901b92018-04-24 15:59:59 +0000854 EmitWaitcnt |= ScoreBrackets->updateByWait(LGKM_CNT,
Kannan Narayananacb089e2017-04-12 03:25:12 +0000855 ScoreBrackets->getScoreUB(LGKM_CNT));
856 // LDS may have to wait for VM_CNT after buffer load to LDS
857 if (target_info->HasBufferLoadToLDS()) {
Mark Searles70901b92018-04-24 15:59:59 +0000858 EmitWaitcnt |= ScoreBrackets->updateByWait(VM_CNT,
Kannan Narayananacb089e2017-04-12 03:25:12 +0000859 ScoreBrackets->getScoreUB(VM_CNT));
860 }
861 }
862 break;
863
864 case SCMEM_GDS:
865 if (group_is_multi_wave || fence_is_global) {
Mark Searles70901b92018-04-24 15:59:59 +0000866 EmitWaitcnt |= ScoreBrackets->updateByWait(EXP_CNT,
Evgeny Mankovbf975172017-08-16 16:47:29 +0000867 ScoreBrackets->getScoreUB(EXP_CNT));
Mark Searles70901b92018-04-24 15:59:59 +0000868 EmitWaitcnt |= ScoreBrackets->updateByWait(LGKM_CNT,
Evgeny Mankovbf975172017-08-16 16:47:29 +0000869 ScoreBrackets->getScoreUB(LGKM_CNT));
Kannan Narayananacb089e2017-04-12 03:25:12 +0000870 }
871 break;
872
873 case SCMEM_UAV:
874 case SCMEM_TFBUF:
875 case SCMEM_RING:
876 case SCMEM_SCATTER:
877 if (group_is_multi_wave || fence_is_global) {
Mark Searles70901b92018-04-24 15:59:59 +0000878 EmitWaitcnt |= ScoreBrackets->updateByWait(EXP_CNT,
Evgeny Mankovbf975172017-08-16 16:47:29 +0000879 ScoreBrackets->getScoreUB(EXP_CNT));
Mark Searles70901b92018-04-24 15:59:59 +0000880 EmitWaitcnt |= ScoreBrackets->updateByWait(VM_CNT,
Evgeny Mankovbf975172017-08-16 16:47:29 +0000881 ScoreBrackets->getScoreUB(VM_CNT));
Kannan Narayananacb089e2017-04-12 03:25:12 +0000882 }
883 break;
884
885 case SCMEM_SCRATCH:
886 default:
887 break;
888 }
889 }
890 }
891#endif
892
893 // Export & GDS instructions do not read the EXEC mask until after the export
894 // is granted (which can occur well after the instruction is issued).
895 // The shader program must flush all EXP operations on the export-count
896 // before overwriting the EXEC mask.
897 else {
898 if (MI.modifiesRegister(AMDGPU::EXEC, TRI)) {
899 // Export and GDS are tracked individually, either may trigger a waitcnt
900 // for EXEC.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000901 if (ScoreBrackets.hasPendingEvent(EXP_GPR_LOCK) ||
902 ScoreBrackets.hasPendingEvent(EXP_PARAM_ACCESS) ||
903 ScoreBrackets.hasPendingEvent(EXP_POS_ACCESS) ||
904 ScoreBrackets.hasPendingEvent(GDS_GPR_LOCK)) {
Nicolai Haehnled1f45da2018-11-29 11:06:14 +0000905 Wait.ExpCnt = 0;
906 }
Kannan Narayananacb089e2017-04-12 03:25:12 +0000907 }
908
909#if 0 // TODO: the following code to handle CALL.
910 // The argument passing for CALLs should suffice for VM_CNT and LGKM_CNT.
911 // However, there is a problem with EXP_CNT, because the call cannot
912 // easily tell if a register is used in the function, and if it did, then
913 // the referring instruction would have to have an S_WAITCNT, which is
914 // dependent on all call sites. So Instead, force S_WAITCNT for EXP_CNTs
915 // before the call.
916 if (MI.getOpcode() == SC_CALL) {
917 if (ScoreBrackets->getScoreUB(EXP_CNT) >
Evgeny Mankovbf975172017-08-16 16:47:29 +0000918 ScoreBrackets->getScoreLB(EXP_CNT)) {
Kannan Narayananacb089e2017-04-12 03:25:12 +0000919 ScoreBrackets->setScoreLB(EXP_CNT, ScoreBrackets->getScoreUB(EXP_CNT));
Mark Searles70901b92018-04-24 15:59:59 +0000920 EmitWaitcnt |= CNT_MASK(EXP_CNT);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000921 }
922 }
923#endif
924
Matt Arsenault0ed39d32017-07-21 18:54:54 +0000925 // FIXME: Should not be relying on memoperands.
Kannan Narayananacb089e2017-04-12 03:25:12 +0000926 // Look at the source operands of every instruction to see if
927 // any of them results from a previous memory operation that affects
928 // its current usage. If so, an s_waitcnt instruction needs to be
929 // emitted.
930 // If the source operand was defined by a load, add the s_waitcnt
931 // instruction.
932 for (const MachineMemOperand *Memop : MI.memoperands()) {
933 unsigned AS = Memop->getAddrSpace();
Matt Arsenault0da63502018-08-31 05:49:54 +0000934 if (AS != AMDGPUAS::LOCAL_ADDRESS)
Kannan Narayananacb089e2017-04-12 03:25:12 +0000935 continue;
936 unsigned RegNo = SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS;
937 // VM_CNT is only relevant to vgpr or LDS.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000938 ScoreBrackets.determineWait(
939 VM_CNT, ScoreBrackets.getRegScore(RegNo, VM_CNT), Wait);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000940 }
Matt Arsenault0ed39d32017-07-21 18:54:54 +0000941
Kannan Narayananacb089e2017-04-12 03:25:12 +0000942 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
943 const MachineOperand &Op = MI.getOperand(I);
944 const MachineRegisterInfo &MRIA = *MRI;
945 RegInterval Interval =
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000946 ScoreBrackets.getRegInterval(&MI, TII, MRI, TRI, I, false);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000947 for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
948 if (TRI->isVGPR(MRIA, Op.getReg())) {
949 // VM_CNT is only relevant to vgpr or LDS.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000950 ScoreBrackets.determineWait(
951 VM_CNT, ScoreBrackets.getRegScore(RegNo, VM_CNT), Wait);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000952 }
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000953 ScoreBrackets.determineWait(
954 LGKM_CNT, ScoreBrackets.getRegScore(RegNo, LGKM_CNT), Wait);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000955 }
956 }
957 // End of for loop that looks at all source operands to decide vm_wait_cnt
958 // and lgk_wait_cnt.
959
960 // Two cases are handled for destination operands:
961 // 1) If the destination operand was defined by a load, add the s_waitcnt
962 // instruction to guarantee the right WAW order.
963 // 2) If a destination operand that was used by a recent export/store ins,
964 // add s_waitcnt on exp_cnt to guarantee the WAR order.
965 if (MI.mayStore()) {
Matt Arsenault0ed39d32017-07-21 18:54:54 +0000966 // FIXME: Should not be relying on memoperands.
Kannan Narayananacb089e2017-04-12 03:25:12 +0000967 for (const MachineMemOperand *Memop : MI.memoperands()) {
968 unsigned AS = Memop->getAddrSpace();
Matt Arsenault0da63502018-08-31 05:49:54 +0000969 if (AS != AMDGPUAS::LOCAL_ADDRESS)
Kannan Narayananacb089e2017-04-12 03:25:12 +0000970 continue;
971 unsigned RegNo = SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS;
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000972 ScoreBrackets.determineWait(
973 VM_CNT, ScoreBrackets.getRegScore(RegNo, VM_CNT), Wait);
974 ScoreBrackets.determineWait(
975 EXP_CNT, ScoreBrackets.getRegScore(RegNo, EXP_CNT), Wait);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000976 }
977 }
978 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
979 MachineOperand &Def = MI.getOperand(I);
980 const MachineRegisterInfo &MRIA = *MRI;
981 RegInterval Interval =
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000982 ScoreBrackets.getRegInterval(&MI, TII, MRI, TRI, I, true);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000983 for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
984 if (TRI->isVGPR(MRIA, Def.getReg())) {
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000985 ScoreBrackets.determineWait(
986 VM_CNT, ScoreBrackets.getRegScore(RegNo, VM_CNT), Wait);
987 ScoreBrackets.determineWait(
988 EXP_CNT, ScoreBrackets.getRegScore(RegNo, EXP_CNT), Wait);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000989 }
Nicolai Haehnle7bed6962018-11-29 11:06:26 +0000990 ScoreBrackets.determineWait(
991 LGKM_CNT, ScoreBrackets.getRegScore(RegNo, LGKM_CNT), Wait);
Kannan Narayananacb089e2017-04-12 03:25:12 +0000992 }
993 } // End of for loop that looks at all dest operands.
994 }
995
Kannan Narayananacb089e2017-04-12 03:25:12 +0000996 // Check to see if this is an S_BARRIER, and if an implicit S_WAITCNT 0
997 // occurs before the instruction. Doing it here prevents any additional
998 // S_WAITCNTs from being emitted if the instruction was marked as
999 // requiring a WAITCNT beforehand.
Konstantin Zhuravlyovbe6c0ca2017-06-02 17:40:26 +00001000 if (MI.getOpcode() == AMDGPU::S_BARRIER &&
1001 !ST->hasAutoWaitcntBeforeBarrier()) {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001002 Wait = AMDGPU::Waitcnt::allZero();
Kannan Narayananacb089e2017-04-12 03:25:12 +00001003 }
1004
1005 // TODO: Remove this work-around, enable the assert for Bug 457939
1006 // after fixing the scheduler. Also, the Shader Compiler code is
1007 // independent of target.
Tom Stellardc5a154d2018-06-28 23:47:12 +00001008 if (readsVCCZ(MI) && ST->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) {
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001009 if (ScoreBrackets.getScoreLB(LGKM_CNT) <
1010 ScoreBrackets.getScoreUB(LGKM_CNT) &&
1011 ScoreBrackets.hasPendingEvent(SMEM_ACCESS)) {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001012 Wait.LgkmCnt = 0;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001013 }
1014 }
1015
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001016 // Early-out if no wait is indicated.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001017 if (!ScoreBrackets.simplifyWaitcnt(Wait) && !IsForceEmitWaitcnt) {
1018 bool Modified = false;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001019 if (OldWaitcntInstr) {
1020 if (TrackedWaitcntSet.count(OldWaitcntInstr)) {
1021 TrackedWaitcntSet.erase(OldWaitcntInstr);
1022 OldWaitcntInstr->eraseFromParent();
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001023 Modified = true;
Nicolai Haehnle61396ff2018-11-07 21:53:36 +00001024 } else {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001025 int64_t Imm = OldWaitcntInstr->getOperand(0).getImm();
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001026 ScoreBrackets.applyWaitcnt(AMDGPU::decodeWaitcnt(IV, Imm));
Stanislav Mekhanoshindb39b4b2018-02-08 00:18:35 +00001027 }
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001028 Modified = true;
Nicolai Haehnle61396ff2018-11-07 21:53:36 +00001029 }
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001030 return Modified;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001031 }
Kannan Narayananacb089e2017-04-12 03:25:12 +00001032
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001033 if (ForceEmitZeroWaitcnts)
1034 Wait = AMDGPU::Waitcnt::allZero();
1035
1036 if (ForceEmitWaitcnt[VM_CNT])
1037 Wait.VmCnt = 0;
1038 if (ForceEmitWaitcnt[EXP_CNT])
1039 Wait.ExpCnt = 0;
1040 if (ForceEmitWaitcnt[LGKM_CNT])
1041 Wait.LgkmCnt = 0;
1042
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001043 ScoreBrackets.applyWaitcnt(Wait);
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001044
1045 AMDGPU::Waitcnt OldWait;
1046 if (OldWaitcntInstr) {
1047 OldWait =
1048 AMDGPU::decodeWaitcnt(IV, OldWaitcntInstr->getOperand(0).getImm());
1049 }
1050 if (OldWait.dominates(Wait))
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001051 return false;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001052
1053 if (OldWaitcntInstr && !TrackedWaitcntSet.count(OldWaitcntInstr))
1054 Wait = Wait.combined(OldWait);
1055
1056 unsigned Enc = AMDGPU::encodeWaitcnt(IV, Wait);
1057 if (OldWaitcntInstr) {
1058 OldWaitcntInstr->getOperand(0).setImm(Enc);
1059
1060 LLVM_DEBUG(dbgs() << "updateWaitcntInBlock\n"
1061 << "Old Instr: " << MI << '\n'
1062 << "New Instr: " << *OldWaitcntInstr << '\n');
1063 } else {
1064 auto SWaitInst = BuildMI(*MI.getParent(), MI.getIterator(),
1065 MI.getDebugLoc(), TII->get(AMDGPU::S_WAITCNT))
1066 .addImm(Enc);
1067 TrackedWaitcntSet.insert(SWaitInst);
1068
1069 LLVM_DEBUG(dbgs() << "insertWaitcntInBlock\n"
1070 << "Old Instr: " << MI << '\n'
1071 << "New Instr: " << *SWaitInst << '\n');
Kannan Narayananacb089e2017-04-12 03:25:12 +00001072 }
Kannan Narayananacb089e2017-04-12 03:25:12 +00001073
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001074 return true;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001075}
1076
Matt Arsenault0ed39d32017-07-21 18:54:54 +00001077// This is a flat memory operation. Check to see if it has memory
1078// tokens for both LDS and Memory, and if so mark it as a flat.
1079bool SIInsertWaitcnts::mayAccessLDSThroughFlat(const MachineInstr &MI) const {
1080 if (MI.memoperands_empty())
1081 return true;
1082
1083 for (const MachineMemOperand *Memop : MI.memoperands()) {
1084 unsigned AS = Memop->getAddrSpace();
Matt Arsenault0da63502018-08-31 05:49:54 +00001085 if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS)
Matt Arsenault0ed39d32017-07-21 18:54:54 +00001086 return true;
1087 }
1088
1089 return false;
1090}
1091
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001092void SIInsertWaitcnts::updateEventWaitcntAfter(MachineInstr &Inst,
1093 WaitcntBrackets *ScoreBrackets) {
Kannan Narayananacb089e2017-04-12 03:25:12 +00001094 // Now look at the instruction opcode. If it is a memory access
1095 // instruction, update the upper-bound of the appropriate counter's
1096 // bracket and the destination operand scores.
1097 // TODO: Use the (TSFlags & SIInstrFlags::LGKM_CNT) property everywhere.
Matt Arsenault6ab9ea92017-07-21 18:34:51 +00001098 if (TII->isDS(Inst) && TII->usesLGKM_CNT(Inst)) {
Marek Olsakc5cec5e2019-01-16 15:43:53 +00001099 if (TII->isAlwaysGDS(Inst.getOpcode()) ||
1100 TII->hasModifiersSet(Inst, AMDGPU::OpName::gds)) {
Kannan Narayananacb089e2017-04-12 03:25:12 +00001101 ScoreBrackets->updateByEvent(TII, TRI, MRI, GDS_ACCESS, Inst);
1102 ScoreBrackets->updateByEvent(TII, TRI, MRI, GDS_GPR_LOCK, Inst);
1103 } else {
1104 ScoreBrackets->updateByEvent(TII, TRI, MRI, LDS_ACCESS, Inst);
1105 }
1106 } else if (TII->isFLAT(Inst)) {
1107 assert(Inst.mayLoad() || Inst.mayStore());
Matt Arsenault6ab9ea92017-07-21 18:34:51 +00001108
1109 if (TII->usesVM_CNT(Inst))
1110 ScoreBrackets->updateByEvent(TII, TRI, MRI, VMEM_ACCESS, Inst);
1111
Matt Arsenault0ed39d32017-07-21 18:54:54 +00001112 if (TII->usesLGKM_CNT(Inst)) {
Matt Arsenault6ab9ea92017-07-21 18:34:51 +00001113 ScoreBrackets->updateByEvent(TII, TRI, MRI, LDS_ACCESS, Inst);
Kannan Narayananacb089e2017-04-12 03:25:12 +00001114
Matt Arsenault0ed39d32017-07-21 18:54:54 +00001115 // This is a flat memory operation, so note it - it will require
1116 // that both the VM and LGKM be flushed to zero if it is pending when
1117 // a VM or LGKM dependency occurs.
1118 if (mayAccessLDSThroughFlat(Inst))
1119 ScoreBrackets->setPendingFlat();
Kannan Narayananacb089e2017-04-12 03:25:12 +00001120 }
1121 } else if (SIInstrInfo::isVMEM(Inst) &&
1122 // TODO: get a better carve out.
1123 Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1 &&
1124 Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1_SC &&
1125 Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1_VOL) {
1126 ScoreBrackets->updateByEvent(TII, TRI, MRI, VMEM_ACCESS, Inst);
Mark Searles2a19af62018-04-26 16:11:19 +00001127 if (ST->vmemWriteNeedsExpWaitcnt() &&
Mark Searles11d0a042017-05-31 16:44:23 +00001128 (Inst.mayStore() || AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1)) {
Kannan Narayananacb089e2017-04-12 03:25:12 +00001129 ScoreBrackets->updateByEvent(TII, TRI, MRI, VMW_GPR_LOCK, Inst);
1130 }
1131 } else if (TII->isSMRD(Inst)) {
1132 ScoreBrackets->updateByEvent(TII, TRI, MRI, SMEM_ACCESS, Inst);
1133 } else {
1134 switch (Inst.getOpcode()) {
1135 case AMDGPU::S_SENDMSG:
1136 case AMDGPU::S_SENDMSGHALT:
1137 ScoreBrackets->updateByEvent(TII, TRI, MRI, SQ_MESSAGE, Inst);
1138 break;
1139 case AMDGPU::EXP:
1140 case AMDGPU::EXP_DONE: {
1141 int Imm = TII->getNamedOperand(Inst, AMDGPU::OpName::tgt)->getImm();
1142 if (Imm >= 32 && Imm <= 63)
1143 ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_PARAM_ACCESS, Inst);
1144 else if (Imm >= 12 && Imm <= 15)
1145 ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_POS_ACCESS, Inst);
1146 else
1147 ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_GPR_LOCK, Inst);
1148 break;
1149 }
1150 case AMDGPU::S_MEMTIME:
1151 case AMDGPU::S_MEMREALTIME:
1152 ScoreBrackets->updateByEvent(TII, TRI, MRI, SMEM_ACCESS, Inst);
1153 break;
1154 default:
1155 break;
1156 }
1157 }
1158}
1159
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001160bool WaitcntBrackets::mergeScore(const MergeInfo &M, uint32_t &Score,
1161 uint32_t OtherScore) {
1162 uint32_t MyShifted = Score <= M.OldLB ? 0 : Score + M.MyShift;
1163 uint32_t OtherShifted =
1164 OtherScore <= M.OtherLB ? 0 : OtherScore + M.OtherShift;
1165 Score = std::max(MyShifted, OtherShifted);
1166 return OtherShifted > MyShifted;
1167}
Kannan Narayananacb089e2017-04-12 03:25:12 +00001168
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001169/// Merge the pending events and associater score brackets of \p Other into
1170/// this brackets status.
1171///
1172/// Returns whether the merge resulted in a change that requires tighter waits
1173/// (i.e. the merged brackets strictly dominate the original brackets).
1174bool WaitcntBrackets::merge(const WaitcntBrackets &Other) {
1175 bool StrictDom = false;
Mark Searlesc3c02bd2018-03-14 22:04:32 +00001176
Nicolai Haehnleae369d72018-11-29 11:06:11 +00001177 for (auto T : inst_counter_types()) {
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001178 // Merge event flags for this counter
1179 const bool OldOutOfOrder = counterOutOfOrder(T);
1180 const uint32_t OldEvents = PendingEvents & WaitEventMaskForInst[T];
1181 const uint32_t OtherEvents = Other.PendingEvents & WaitEventMaskForInst[T];
1182 if (OtherEvents & ~OldEvents)
1183 StrictDom = true;
1184 if (Other.MixedPendingEvents[T] ||
1185 (OldEvents && OtherEvents && OldEvents != OtherEvents))
1186 MixedPendingEvents[T] = true;
1187 PendingEvents |= OtherEvents;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001188
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001189 // Merge scores for this counter
1190 const uint32_t MyPending = ScoreUBs[T] - ScoreLBs[T];
1191 const uint32_t OtherPending = Other.ScoreUBs[T] - Other.ScoreLBs[T];
1192 MergeInfo M;
1193 M.OldLB = ScoreLBs[T];
1194 M.OtherLB = Other.ScoreLBs[T];
1195 M.MyShift = OtherPending > MyPending ? OtherPending - MyPending : 0;
1196 M.OtherShift = ScoreUBs[T] - Other.ScoreUBs[T] + M.MyShift;
1197
1198 const uint32_t NewUB = ScoreUBs[T] + M.MyShift;
1199 if (NewUB < ScoreUBs[T])
1200 report_fatal_error("waitcnt score overflow");
1201 ScoreUBs[T] = NewUB;
1202 ScoreLBs[T] = std::min(M.OldLB + M.MyShift, M.OtherLB + M.OtherShift);
1203
1204 StrictDom |= mergeScore(M, LastFlat[T], Other.LastFlat[T]);
1205
1206 bool RegStrictDom = false;
1207 for (int J = 0, E = std::max(getMaxVGPR(), Other.getMaxVGPR()) + 1; J != E;
1208 J++) {
1209 RegStrictDom |= mergeScore(M, VgprScores[T][J], Other.VgprScores[T][J]);
Kannan Narayananacb089e2017-04-12 03:25:12 +00001210 }
1211
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001212 if (T == LGKM_CNT) {
1213 for (int J = 0, E = std::max(getMaxSGPR(), Other.getMaxSGPR()) + 1;
1214 J != E; J++) {
1215 RegStrictDom |= mergeScore(M, SgprScores[J], Other.SgprScores[J]);
Kannan Narayananacb089e2017-04-12 03:25:12 +00001216 }
1217 }
1218
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001219 if (RegStrictDom && !OldOutOfOrder)
1220 StrictDom = true;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001221 }
Mark Searlesc3c02bd2018-03-14 22:04:32 +00001222
Carl Ritsonc521ac32018-12-19 10:17:49 +00001223 VgprUB = std::max(getMaxVGPR(), Other.getMaxVGPR());
1224 SgprUB = std::max(getMaxSGPR(), Other.getMaxSGPR());
1225
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001226 return StrictDom;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001227}
1228
1229// Generate s_waitcnt instructions where needed.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001230bool SIInsertWaitcnts::insertWaitcntInBlock(MachineFunction &MF,
1231 MachineBasicBlock &Block,
1232 WaitcntBrackets &ScoreBrackets) {
1233 bool Modified = false;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001234
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001235 LLVM_DEBUG({
Mark Searlesec581832018-04-25 19:21:26 +00001236 dbgs() << "*** Block" << Block.getNumber() << " ***";
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001237 ScoreBrackets.dump();
Kannan Narayananacb089e2017-04-12 03:25:12 +00001238 });
1239
Kannan Narayananacb089e2017-04-12 03:25:12 +00001240 // Walk over the instructions.
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001241 MachineInstr *OldWaitcntInstr = nullptr;
1242
Kannan Narayananacb089e2017-04-12 03:25:12 +00001243 for (MachineBasicBlock::iterator Iter = Block.begin(), E = Block.end();
1244 Iter != E;) {
1245 MachineInstr &Inst = *Iter;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001246
Kannan Narayananacb089e2017-04-12 03:25:12 +00001247 // Remove any previously existing waitcnts.
1248 if (Inst.getOpcode() == AMDGPU::S_WAITCNT) {
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001249 if (OldWaitcntInstr) {
1250 if (TrackedWaitcntSet.count(OldWaitcntInstr)) {
1251 TrackedWaitcntSet.erase(OldWaitcntInstr);
1252 OldWaitcntInstr->eraseFromParent();
1253 OldWaitcntInstr = nullptr;
1254 } else if (!TrackedWaitcntSet.count(&Inst)) {
1255 // Two successive s_waitcnt's, both of which are pre-existing and
1256 // are therefore preserved.
1257 int64_t Imm = OldWaitcntInstr->getOperand(0).getImm();
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001258 ScoreBrackets.applyWaitcnt(AMDGPU::decodeWaitcnt(IV, Imm));
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001259 } else {
1260 ++Iter;
1261 Inst.eraseFromParent();
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001262 Modified = true;
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001263 continue;
1264 }
Kannan Narayananacb089e2017-04-12 03:25:12 +00001265 }
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001266
1267 OldWaitcntInstr = &Inst;
1268 ++Iter;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001269 continue;
1270 }
1271
Kannan Narayananacb089e2017-04-12 03:25:12 +00001272 bool VCCZBugWorkAround = false;
1273 if (readsVCCZ(Inst) &&
Mark Searles24c92ee2018-02-07 02:21:21 +00001274 (!VCCZBugHandledSet.count(&Inst))) {
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001275 if (ScoreBrackets.getScoreLB(LGKM_CNT) <
1276 ScoreBrackets.getScoreUB(LGKM_CNT) &&
1277 ScoreBrackets.hasPendingEvent(SMEM_ACCESS)) {
Tom Stellardc5a154d2018-06-28 23:47:12 +00001278 if (ST->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
Kannan Narayananacb089e2017-04-12 03:25:12 +00001279 VCCZBugWorkAround = true;
1280 }
1281 }
1282
1283 // Generate an s_waitcnt instruction to be placed before
1284 // cur_Inst, if needed.
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001285 Modified |= generateWaitcntInstBefore(Inst, ScoreBrackets, OldWaitcntInstr);
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +00001286 OldWaitcntInstr = nullptr;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001287
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001288 updateEventWaitcntAfter(Inst, &ScoreBrackets);
Kannan Narayananacb089e2017-04-12 03:25:12 +00001289
1290#if 0 // TODO: implement resource type check controlled by options with ub = LB.
1291 // If this instruction generates a S_SETVSKIP because it is an
1292 // indexed resource, and we are on Tahiti, then it will also force
1293 // an S_WAITCNT vmcnt(0)
1294 if (RequireCheckResourceType(Inst, context)) {
1295 // Force the score to as if an S_WAITCNT vmcnt(0) is emitted.
1296 ScoreBrackets->setScoreLB(VM_CNT,
Evgeny Mankovbf975172017-08-16 16:47:29 +00001297 ScoreBrackets->getScoreUB(VM_CNT));
Kannan Narayananacb089e2017-04-12 03:25:12 +00001298 }
1299#endif
1300
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001301 LLVM_DEBUG({
Mark Searles94ae3b22018-01-30 17:17:06 +00001302 Inst.print(dbgs());
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001303 ScoreBrackets.dump();
Kannan Narayananacb089e2017-04-12 03:25:12 +00001304 });
1305
1306 // Check to see if this is a GWS instruction. If so, and if this is CI or
1307 // VI, then the generated code sequence will include an S_WAITCNT 0.
1308 // TODO: Are these the only GWS instructions?
1309 if (Inst.getOpcode() == AMDGPU::DS_GWS_INIT ||
1310 Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_V ||
1311 Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_BR ||
1312 Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_P ||
1313 Inst.getOpcode() == AMDGPU::DS_GWS_BARRIER) {
1314 // TODO: && context->target_info->GwsRequiresMemViolTest() ) {
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001315 ScoreBrackets.applyWaitcnt(AMDGPU::Waitcnt::allZero());
Kannan Narayananacb089e2017-04-12 03:25:12 +00001316 }
1317
1318 // TODO: Remove this work-around after fixing the scheduler and enable the
1319 // assert above.
1320 if (VCCZBugWorkAround) {
1321 // Restore the vccz bit. Any time a value is written to vcc, the vcc
1322 // bit is updated, so we can restore the bit by reading the value of
1323 // vcc and then writing it back to the register.
1324 BuildMI(Block, Inst, Inst.getDebugLoc(), TII->get(AMDGPU::S_MOV_B64),
1325 AMDGPU::VCC)
1326 .addReg(AMDGPU::VCC);
1327 VCCZBugHandledSet.insert(&Inst);
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001328 Modified = true;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001329 }
1330
Kannan Narayananacb089e2017-04-12 03:25:12 +00001331 ++Iter;
1332 }
1333
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001334 return Modified;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001335}
1336
1337bool SIInsertWaitcnts::runOnMachineFunction(MachineFunction &MF) {
Tom Stellard5bfbae52018-07-11 20:59:01 +00001338 ST = &MF.getSubtarget<GCNSubtarget>();
Kannan Narayananacb089e2017-04-12 03:25:12 +00001339 TII = ST->getInstrInfo();
1340 TRI = &TII->getRegisterInfo();
1341 MRI = &MF.getRegInfo();
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +00001342 IV = AMDGPU::getIsaVersion(ST->getCPU());
Mark Searles11d0a042017-05-31 16:44:23 +00001343 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Kannan Narayananacb089e2017-04-12 03:25:12 +00001344
Mark Searles4a0f2c52018-05-07 14:43:28 +00001345 ForceEmitZeroWaitcnts = ForceEmitZeroFlag;
Nicolai Haehnleae369d72018-11-29 11:06:11 +00001346 for (auto T : inst_counter_types())
Mark Searlesec581832018-04-25 19:21:26 +00001347 ForceEmitWaitcnt[T] = false;
1348
Kannan Narayananacb089e2017-04-12 03:25:12 +00001349 HardwareLimits.VmcntMax = AMDGPU::getVmcntBitMask(IV);
1350 HardwareLimits.ExpcntMax = AMDGPU::getExpcntBitMask(IV);
1351 HardwareLimits.LgkmcntMax = AMDGPU::getLgkmcntBitMask(IV);
1352
1353 HardwareLimits.NumVGPRsMax = ST->getAddressableNumVGPRs();
1354 HardwareLimits.NumSGPRsMax = ST->getAddressableNumSGPRs();
1355 assert(HardwareLimits.NumVGPRsMax <= SQ_MAX_PGM_VGPRS);
1356 assert(HardwareLimits.NumSGPRsMax <= SQ_MAX_PGM_SGPRS);
1357
1358 RegisterEncoding.VGPR0 = TRI->getEncodingValue(AMDGPU::VGPR0);
1359 RegisterEncoding.VGPRL =
1360 RegisterEncoding.VGPR0 + HardwareLimits.NumVGPRsMax - 1;
1361 RegisterEncoding.SGPR0 = TRI->getEncodingValue(AMDGPU::SGPR0);
1362 RegisterEncoding.SGPRL =
1363 RegisterEncoding.SGPR0 + HardwareLimits.NumSGPRsMax - 1;
1364
Mark Searles24c92ee2018-02-07 02:21:21 +00001365 TrackedWaitcntSet.clear();
Mark Searles24c92ee2018-02-07 02:21:21 +00001366 VCCZBugHandledSet.clear();
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001367 RpotIdxMap.clear();
1368 BlockInfos.clear();
Mark Searles24c92ee2018-02-07 02:21:21 +00001369
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001370 // Keep iterating over the blocks in reverse post order, inserting and
1371 // updating s_waitcnt where needed, until a fix point is reached.
1372 for (MachineBasicBlock *MBB :
1373 ReversePostOrderTraversal<MachineFunction *>(&MF)) {
1374 RpotIdxMap[MBB] = BlockInfos.size();
1375 BlockInfos.emplace_back(MBB);
1376 }
1377
1378 std::unique_ptr<WaitcntBrackets> Brackets;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001379 bool Modified = false;
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001380 bool Repeat;
1381 do {
1382 Repeat = false;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001383
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001384 for (BlockInfo &BI : BlockInfos) {
1385 if (!BI.Dirty)
1386 continue;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001387
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001388 unsigned Idx = std::distance(&*BlockInfos.begin(), &BI);
Kannan Narayananacb089e2017-04-12 03:25:12 +00001389
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001390 if (BI.Incoming) {
1391 if (!Brackets)
1392 Brackets = llvm::make_unique<WaitcntBrackets>(*BI.Incoming);
1393 else
1394 *Brackets = *BI.Incoming;
1395 } else {
1396 if (!Brackets)
1397 Brackets = llvm::make_unique<WaitcntBrackets>(ST);
1398 else
1399 Brackets->clear();
Mark Searles1bc6e712018-04-19 15:42:30 +00001400 }
Kannan Narayananacb089e2017-04-12 03:25:12 +00001401
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001402 Modified |= insertWaitcntInBlock(MF, *BI.MBB, *Brackets);
1403 BI.Dirty = false;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001404
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001405 if (Brackets->hasPending()) {
1406 BlockInfo *MoveBracketsToSucc = nullptr;
1407 for (MachineBasicBlock *Succ : BI.MBB->successors()) {
1408 unsigned SuccIdx = RpotIdxMap[Succ];
1409 BlockInfo &SuccBI = BlockInfos[SuccIdx];
1410 if (!SuccBI.Incoming) {
1411 SuccBI.Dirty = true;
1412 if (SuccIdx <= Idx)
1413 Repeat = true;
1414 if (!MoveBracketsToSucc) {
1415 MoveBracketsToSucc = &SuccBI;
1416 } else {
1417 SuccBI.Incoming = llvm::make_unique<WaitcntBrackets>(*Brackets);
1418 }
1419 } else if (SuccBI.Incoming->merge(*Brackets)) {
1420 SuccBI.Dirty = true;
1421 if (SuccIdx <= Idx)
1422 Repeat = true;
Kannan Narayananacb089e2017-04-12 03:25:12 +00001423 }
1424 }
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001425 if (MoveBracketsToSucc)
1426 MoveBracketsToSucc->Incoming = std::move(Brackets);
Kannan Narayananacb089e2017-04-12 03:25:12 +00001427 }
1428 }
Nicolai Haehnle7bed6962018-11-29 11:06:26 +00001429 } while (Repeat);
Kannan Narayananacb089e2017-04-12 03:25:12 +00001430
1431 SmallVector<MachineBasicBlock *, 4> EndPgmBlocks;
1432
1433 bool HaveScalarStores = false;
1434
1435 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
1436 ++BI) {
Kannan Narayananacb089e2017-04-12 03:25:12 +00001437 MachineBasicBlock &MBB = *BI;
1438
1439 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;
1440 ++I) {
Kannan Narayananacb089e2017-04-12 03:25:12 +00001441 if (!HaveScalarStores && TII->isScalarStore(*I))
1442 HaveScalarStores = true;
1443
1444 if (I->getOpcode() == AMDGPU::S_ENDPGM ||
1445 I->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG)
1446 EndPgmBlocks.push_back(&MBB);
1447 }
1448 }
1449
1450 if (HaveScalarStores) {
1451 // If scalar writes are used, the cache must be flushed or else the next
1452 // wave to reuse the same scratch memory can be clobbered.
1453 //
1454 // Insert s_dcache_wb at wave termination points if there were any scalar
1455 // stores, and only if the cache hasn't already been flushed. This could be
1456 // improved by looking across blocks for flushes in postdominating blocks
1457 // from the stores but an explicitly requested flush is probably very rare.
1458 for (MachineBasicBlock *MBB : EndPgmBlocks) {
1459 bool SeenDCacheWB = false;
1460
1461 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
1462 ++I) {
Kannan Narayananacb089e2017-04-12 03:25:12 +00001463 if (I->getOpcode() == AMDGPU::S_DCACHE_WB)
1464 SeenDCacheWB = true;
1465 else if (TII->isScalarStore(*I))
1466 SeenDCacheWB = false;
1467
1468 // FIXME: It would be better to insert this before a waitcnt if any.
1469 if ((I->getOpcode() == AMDGPU::S_ENDPGM ||
1470 I->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) &&
1471 !SeenDCacheWB) {
1472 Modified = true;
1473 BuildMI(*MBB, I, I->getDebugLoc(), TII->get(AMDGPU::S_DCACHE_WB));
1474 }
1475 }
1476 }
1477 }
1478
Mark Searles11d0a042017-05-31 16:44:23 +00001479 if (!MFI->isEntryFunction()) {
1480 // Wait for any outstanding memory operations that the input registers may
Hiroshi Inouec8e92452018-01-29 05:17:03 +00001481 // depend on. We can't track them and it's better to the wait after the
Mark Searles11d0a042017-05-31 16:44:23 +00001482 // costly call sequence.
1483
1484 // TODO: Could insert earlier and schedule more liberally with operations
1485 // that only use caller preserved registers.
1486 MachineBasicBlock &EntryBB = MF.front();
Mark Searlesed54ff12018-05-30 16:27:57 +00001487 BuildMI(EntryBB, EntryBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
1488 .addImm(0);
Mark Searles11d0a042017-05-31 16:44:23 +00001489
1490 Modified = true;
1491 }
1492
Kannan Narayananacb089e2017-04-12 03:25:12 +00001493 return Modified;
1494}