forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMDGPUInsertDelayAlu.cpp
518 lines (449 loc) · 17.2 KB
/
AMDGPUInsertDelayAlu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//===- AMDGPUInsertDelayAlu.cpp - Insert s_delay_alu instructions ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// \file
/// Insert s_delay_alu instructions to avoid stalls on GFX11+.
//
//===----------------------------------------------------------------------===//
#include "AMDGPU.h"
#include "GCNSubtarget.h"
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
#include "SIInstrInfo.h"
#include "llvm/ADT/SetVector.h"
using namespace llvm;
#define DEBUG_TYPE "amdgpu-insert-delay-alu"
namespace {
class AMDGPUInsertDelayAlu {
public:
const SIInstrInfo *SII;
const TargetRegisterInfo *TRI;
const TargetSchedModel *SchedModel;
// Return true if MI waits for all outstanding VALU instructions to complete.
static bool instructionWaitsForVALU(const MachineInstr &MI) {
// These instruction types wait for VA_VDST==0 before issuing.
const uint64_t VA_VDST_0 = SIInstrFlags::DS | SIInstrFlags::EXP |
SIInstrFlags::FLAT | SIInstrFlags::MIMG |
SIInstrFlags::MTBUF | SIInstrFlags::MUBUF;
if (MI.getDesc().TSFlags & VA_VDST_0)
return true;
if (MI.getOpcode() == AMDGPU::S_SENDMSG_RTN_B32 ||
MI.getOpcode() == AMDGPU::S_SENDMSG_RTN_B64)
return true;
if (MI.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR &&
AMDGPU::DepCtr::decodeFieldVaVdst(MI.getOperand(0).getImm()) == 0)
return true;
return false;
}
static bool instructionWaitsForSGPRWrites(const MachineInstr &MI) {
// These instruction types wait for VA_SDST==0 before issuing.
const uint64_t VA_SDST_0 = SIInstrFlags::SALU | SIInstrFlags::SMRD;
return MI.getDesc().TSFlags & VA_SDST_0;
}
// Types of delay that can be encoded in an s_delay_alu instruction.
enum DelayType { VALU, TRANS, SALU, OTHER };
// Get the delay type for an instruction with the specified TSFlags.
static DelayType getDelayType(uint64_t TSFlags) {
if (TSFlags & SIInstrFlags::TRANS)
return TRANS;
if (TSFlags & SIInstrFlags::VALU)
return VALU;
if (TSFlags & SIInstrFlags::SALU)
return SALU;
return OTHER;
}
// Information about the last instruction(s) that wrote to a particular
// regunit. In straight-line code there will only be one such instruction, but
// when control flow converges we merge the delay information from each path
// to represent the union of the worst-case delays of each type.
struct DelayInfo {
// One larger than the maximum number of (non-TRANS) VALU instructions we
// can encode in an s_delay_alu instruction.
static constexpr unsigned VALU_MAX = 5;
// One larger than the maximum number of TRANS instructions we can encode in
// an s_delay_alu instruction.
static constexpr unsigned TRANS_MAX = 4;
// One larger than the maximum number of SALU cycles we can encode in an
// s_delay_alu instruction.
static constexpr unsigned SALU_CYCLES_MAX = 4;
// If it was written by a (non-TRANS) VALU, remember how many clock cycles
// are left until it completes, and how many other (non-TRANS) VALU we have
// seen since it was issued.
uint8_t VALUCycles = 0;
uint8_t VALUNum = VALU_MAX;
// If it was written by a TRANS, remember how many clock cycles are left
// until it completes, and how many other TRANS we have seen since it was
// issued.
uint8_t TRANSCycles = 0;
uint8_t TRANSNum = TRANS_MAX;
// Also remember how many other (non-TRANS) VALU we have seen since it was
// issued. When an instruction depends on both a prior TRANS and a prior
// non-TRANS VALU, this is used to decide whether to encode a wait for just
// one or both of them.
uint8_t TRANSNumVALU = VALU_MAX;
// If it was written by an SALU, remember how many clock cycles are left
// until it completes.
uint8_t SALUCycles = 0;
DelayInfo() = default;
DelayInfo(DelayType Type, unsigned Cycles) {
switch (Type) {
default:
llvm_unreachable("unexpected type");
case VALU:
VALUCycles = Cycles;
VALUNum = 0;
break;
case TRANS:
TRANSCycles = Cycles;
TRANSNum = 0;
TRANSNumVALU = 0;
break;
case SALU:
// Guard against pseudo-instructions like SI_CALL which are marked as
// SALU but with a very high latency.
SALUCycles = std::min(Cycles, SALU_CYCLES_MAX);
break;
}
}
bool operator==(const DelayInfo &RHS) const {
return VALUCycles == RHS.VALUCycles && VALUNum == RHS.VALUNum &&
TRANSCycles == RHS.TRANSCycles && TRANSNum == RHS.TRANSNum &&
TRANSNumVALU == RHS.TRANSNumVALU && SALUCycles == RHS.SALUCycles;
}
bool operator!=(const DelayInfo &RHS) const { return !(*this == RHS); }
// Merge another DelayInfo into this one, to represent the union of the
// worst-case delays of each type.
void merge(const DelayInfo &RHS) {
VALUCycles = std::max(VALUCycles, RHS.VALUCycles);
VALUNum = std::min(VALUNum, RHS.VALUNum);
TRANSCycles = std::max(TRANSCycles, RHS.TRANSCycles);
TRANSNum = std::min(TRANSNum, RHS.TRANSNum);
TRANSNumVALU = std::min(TRANSNumVALU, RHS.TRANSNumVALU);
SALUCycles = std::max(SALUCycles, RHS.SALUCycles);
}
// Update this DelayInfo after issuing an instruction. IsVALU should be 1
// when issuing a (non-TRANS) VALU, else 0. IsTRANS should be 1 when issuing
// a TRANS, else 0. Cycles is the number of cycles it takes to issue the
// instruction. Return true if there is no longer any useful delay info.
bool advance(DelayType Type, unsigned Cycles) {
bool Erase = true;
VALUNum += (Type == VALU);
if (VALUNum >= VALU_MAX || VALUCycles <= Cycles) {
// Forget about the VALU instruction. It was too far back or has
// definitely completed by now.
VALUNum = VALU_MAX;
VALUCycles = 0;
} else {
VALUCycles -= Cycles;
Erase = false;
}
TRANSNum += (Type == TRANS);
TRANSNumVALU += (Type == VALU);
if (TRANSNum >= TRANS_MAX || TRANSCycles <= Cycles) {
// Forget about any TRANS instruction. It was too far back or has
// definitely completed by now.
TRANSNum = TRANS_MAX;
TRANSNumVALU = VALU_MAX;
TRANSCycles = 0;
} else {
TRANSCycles -= Cycles;
Erase = false;
}
if (SALUCycles <= Cycles) {
// Forget about any SALU instruction. It has definitely completed by
// now.
SALUCycles = 0;
} else {
SALUCycles -= Cycles;
Erase = false;
}
return Erase;
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void dump() const {
if (VALUCycles)
dbgs() << " VALUCycles=" << (int)VALUCycles;
if (VALUNum < VALU_MAX)
dbgs() << " VALUNum=" << (int)VALUNum;
if (TRANSCycles)
dbgs() << " TRANSCycles=" << (int)TRANSCycles;
if (TRANSNum < TRANS_MAX)
dbgs() << " TRANSNum=" << (int)TRANSNum;
if (TRANSNumVALU < VALU_MAX)
dbgs() << " TRANSNumVALU=" << (int)TRANSNumVALU;
if (SALUCycles)
dbgs() << " SALUCycles=" << (int)SALUCycles;
}
#endif
};
// A map from regunits to the delay info for that regunit.
struct DelayState : DenseMap<unsigned, DelayInfo> {
// Merge another DelayState into this one by merging the delay info for each
// regunit.
void merge(const DelayState &RHS) {
for (const auto &KV : RHS) {
iterator It;
bool Inserted;
std::tie(It, Inserted) = insert(KV);
if (!Inserted)
It->second.merge(KV.second);
}
}
// Advance the delay info for each regunit, erasing any that are no longer
// useful.
void advance(DelayType Type, unsigned Cycles) {
iterator Next;
for (auto I = begin(), E = end(); I != E; I = Next) {
Next = std::next(I);
if (I->second.advance(Type, Cycles))
erase(I);
}
}
void advanceByVALUNum(unsigned VALUNum) {
iterator Next;
for (auto I = begin(), E = end(); I != E; I = Next) {
Next = std::next(I);
if (I->second.VALUNum >= VALUNum && I->second.VALUCycles > 0) {
erase(I);
}
}
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void dump(const TargetRegisterInfo *TRI) const {
if (empty()) {
dbgs() << " empty\n";
return;
}
// Dump DelayInfo for each RegUnit in numerical order.
SmallVector<const_iterator, 8> Order;
Order.reserve(size());
for (const_iterator I = begin(), E = end(); I != E; ++I)
Order.push_back(I);
llvm::sort(Order, [](const const_iterator &A, const const_iterator &B) {
return A->first < B->first;
});
for (const_iterator I : Order) {
dbgs() << " " << printRegUnit(I->first, TRI);
I->second.dump();
dbgs() << "\n";
}
}
#endif
};
// The saved delay state at the end of each basic block.
DenseMap<MachineBasicBlock *, DelayState> BlockState;
// Emit an s_delay_alu instruction if necessary before MI.
MachineInstr *emitDelayAlu(MachineInstr &MI, DelayInfo Delay,
MachineInstr *LastDelayAlu) {
unsigned Imm = 0;
// Wait for a TRANS instruction.
if (Delay.TRANSNum < DelayInfo::TRANS_MAX)
Imm |= 4 + Delay.TRANSNum;
// Wait for a VALU instruction (if it's more recent than any TRANS
// instruction that we're also waiting for).
if (Delay.VALUNum < DelayInfo::VALU_MAX &&
Delay.VALUNum <= Delay.TRANSNumVALU) {
if (Imm & 0xf)
Imm |= Delay.VALUNum << 7;
else
Imm |= Delay.VALUNum;
}
// Wait for an SALU instruction.
if (Delay.SALUCycles) {
assert(Delay.SALUCycles < DelayInfo::SALU_CYCLES_MAX);
if (Imm & 0x780) {
// We have already encoded a VALU and a TRANS delay. There's no room in
// the encoding for an SALU delay as well, so just drop it.
} else if (Imm & 0xf) {
Imm |= (Delay.SALUCycles + 8) << 7;
} else {
Imm |= Delay.SALUCycles + 8;
}
}
// Don't emit the s_delay_alu instruction if there's nothing to wait for.
if (!Imm)
return LastDelayAlu;
// If we only need to wait for one instruction, try encoding it in the last
// s_delay_alu that we emitted.
if (!(Imm & 0x780) && LastDelayAlu) {
unsigned Skip = 0;
for (auto I = MachineBasicBlock::instr_iterator(LastDelayAlu),
E = MachineBasicBlock::instr_iterator(MI);
++I != E;) {
if (!I->isBundle() && !I->isMetaInstruction())
++Skip;
}
if (Skip < 6) {
MachineOperand &Op = LastDelayAlu->getOperand(0);
unsigned LastImm = Op.getImm();
assert((LastImm & ~0xf) == 0 &&
"Remembered an s_delay_alu with no room for another delay!");
LastImm |= Imm << 7 | Skip << 4;
Op.setImm(LastImm);
return nullptr;
}
}
auto &MBB = *MI.getParent();
MachineInstr *DelayAlu =
BuildMI(MBB, MI, DebugLoc(), SII->get(AMDGPU::S_DELAY_ALU)).addImm(Imm);
// Remember the s_delay_alu for next time if there is still room in it to
// encode another delay.
return (Imm & 0x780) ? nullptr : DelayAlu;
}
bool runOnMachineBasicBlock(MachineBasicBlock &MBB, bool Emit) {
DelayState State;
for (auto *Pred : MBB.predecessors())
State.merge(BlockState[Pred]);
LLVM_DEBUG(dbgs() << " State at start of " << printMBBReference(MBB)
<< "\n";
State.dump(TRI););
bool Changed = false;
MachineInstr *LastDelayAlu = nullptr;
MCRegUnit LastSGPRFromVALU = 0;
// Iterate over the contents of bundles, but don't emit any instructions
// inside a bundle.
for (auto &MI : MBB.instrs()) {
if (MI.isBundle() || MI.isMetaInstruction())
continue;
// Ignore some more instructions that do not generate any code.
switch (MI.getOpcode()) {
case AMDGPU::SI_RETURN_TO_EPILOG:
continue;
}
DelayType Type = getDelayType(MI.getDesc().TSFlags);
if (instructionWaitsForSGPRWrites(MI)) {
auto It = State.find(LastSGPRFromVALU);
if (It != State.end()) {
DelayInfo Info = It->getSecond();
State.advanceByVALUNum(Info.VALUNum);
LastSGPRFromVALU = 0;
}
}
if (instructionWaitsForVALU(MI)) {
// Forget about all outstanding VALU delays.
// TODO: This is overkill since it also forgets about SALU delays.
State = DelayState();
} else if (Type != OTHER) {
DelayInfo Delay;
// TODO: Scan implicit uses too?
for (const auto &Op : MI.explicit_uses()) {
if (Op.isReg()) {
// One of the operands of the writelane is also the output operand.
// This creates the insertion of redundant delays. Hence, we have to
// ignore this operand.
if (MI.getOpcode() == AMDGPU::V_WRITELANE_B32 && Op.isTied())
continue;
for (MCRegUnit Unit : TRI->regunits(Op.getReg())) {
auto It = State.find(Unit);
if (It != State.end()) {
Delay.merge(It->second);
State.erase(Unit);
}
}
}
}
if (SII->isVALU(MI.getOpcode())) {
for (const auto &Op : MI.defs()) {
Register Reg = Op.getReg();
if (AMDGPU::isSGPR(Reg, TRI)) {
LastSGPRFromVALU = *TRI->regunits(Reg).begin();
break;
}
}
}
if (Emit && !MI.isBundledWithPred()) {
// TODO: For VALU->SALU delays should we use s_delay_alu or s_nop or
// just ignore them?
LastDelayAlu = emitDelayAlu(MI, Delay, LastDelayAlu);
}
}
if (Type != OTHER) {
// TODO: Scan implicit defs too?
for (const auto &Op : MI.defs()) {
unsigned Latency = SchedModel->computeOperandLatency(
&MI, Op.getOperandNo(), nullptr, 0);
for (MCRegUnit Unit : TRI->regunits(Op.getReg()))
State[Unit] = DelayInfo(Type, Latency);
}
}
// Advance by the number of cycles it takes to issue this instruction.
// TODO: Use a more advanced model that accounts for instructions that
// take multiple cycles to issue on a particular pipeline.
unsigned Cycles = SIInstrInfo::getNumWaitStates(MI);
// TODO: In wave64 mode, double the number of cycles for VALU and VMEM
// instructions on the assumption that they will usually have to be issued
// twice?
State.advance(Type, Cycles);
LLVM_DEBUG(dbgs() << " State after " << MI; State.dump(TRI););
}
if (Emit) {
assert(State == BlockState[&MBB] &&
"Basic block state should not have changed on final pass!");
} else if (DelayState &BS = BlockState[&MBB]; State != BS) {
BS = std::move(State);
Changed = true;
}
return Changed;
}
bool run(MachineFunction &MF) {
LLVM_DEBUG(dbgs() << "AMDGPUInsertDelayAlu running on " << MF.getName()
<< "\n");
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
if (!ST.hasDelayAlu())
return false;
SII = ST.getInstrInfo();
TRI = ST.getRegisterInfo();
SchedModel = &SII->getSchedModel();
// Calculate the delay state for each basic block, iterating until we reach
// a fixed point.
SetVector<MachineBasicBlock *> WorkList;
for (auto &MBB : reverse(MF))
WorkList.insert(&MBB);
while (!WorkList.empty()) {
auto &MBB = *WorkList.pop_back_val();
bool Changed = runOnMachineBasicBlock(MBB, false);
if (Changed)
WorkList.insert(MBB.succ_begin(), MBB.succ_end());
}
LLVM_DEBUG(dbgs() << "Final pass over all BBs\n");
// Make one last pass over all basic blocks to emit s_delay_alu
// instructions.
bool Changed = false;
for (auto &MBB : MF)
Changed |= runOnMachineBasicBlock(MBB, true);
return Changed;
}
};
class AMDGPUInsertDelayAluLegacy : public MachineFunctionPass {
public:
static char ID;
AMDGPUInsertDelayAluLegacy() : MachineFunctionPass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
MachineFunctionPass::getAnalysisUsage(AU);
}
bool runOnMachineFunction(MachineFunction &MF) override {
if (skipFunction(MF.getFunction()))
return false;
AMDGPUInsertDelayAlu Impl;
return Impl.run(MF);
}
};
} // namespace
PreservedAnalyses
AMDGPUInsertDelayAluPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
if (!AMDGPUInsertDelayAlu().run(MF))
return PreservedAnalyses::all();
auto PA = getMachineFunctionPassPreservedAnalyses();
PA.preserveSet<CFGAnalyses>();
return PA;
} // end namespace llvm
char AMDGPUInsertDelayAluLegacy::ID = 0;
char &llvm::AMDGPUInsertDelayAluID = AMDGPUInsertDelayAluLegacy::ID;
INITIALIZE_PASS(AMDGPUInsertDelayAluLegacy, DEBUG_TYPE,
"AMDGPU Insert Delay ALU", false, false)