1 //===- AMDGPUPerfHintAnalysis.cpp - analysis of functions memory traffic --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Analyzes if a function potentially memory bound and if a kernel
12 /// kernel may benefit from limiting number of waves to reduce cache thrashing.
14 //===----------------------------------------------------------------------===//
17 #include "AMDGPUPerfHintAnalysis.h"
18 #include "Utils/AMDGPUBaseInfo.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/CodeGen/TargetLowering.h"
23 #include "llvm/CodeGen/TargetPassConfig.h"
24 #include "llvm/CodeGen/TargetSubtargetInfo.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ValueMap.h"
30 #include "llvm/Support/CommandLine.h"
34 #define DEBUG_TYPE "amdgpu-perf-hint"
36 static cl::opt
<unsigned>
37 MemBoundThresh("amdgpu-membound-threshold", cl::init(50), cl::Hidden
,
38 cl::desc("Function mem bound threshold in %"));
40 static cl::opt
<unsigned>
41 LimitWaveThresh("amdgpu-limit-wave-threshold", cl::init(50), cl::Hidden
,
42 cl::desc("Kernel limit wave threshold in %"));
44 static cl::opt
<unsigned>
45 IAWeight("amdgpu-indirect-access-weight", cl::init(1000), cl::Hidden
,
46 cl::desc("Indirect access memory instruction weight"));
48 static cl::opt
<unsigned>
49 LSWeight("amdgpu-large-stride-weight", cl::init(1000), cl::Hidden
,
50 cl::desc("Large stride memory access weight"));
52 static cl::opt
<unsigned>
53 LargeStrideThresh("amdgpu-large-stride-threshold", cl::init(64), cl::Hidden
,
54 cl::desc("Large stride memory access threshold"));
56 STATISTIC(NumMemBound
, "Number of functions marked as memory bound");
57 STATISTIC(NumLimitWave
, "Number of functions marked as needing limit wave");
59 char llvm::AMDGPUPerfHintAnalysis::ID
= 0;
60 char &llvm::AMDGPUPerfHintAnalysisID
= AMDGPUPerfHintAnalysis::ID
;
62 INITIALIZE_PASS(AMDGPUPerfHintAnalysis
, DEBUG_TYPE
,
63 "Analysis if a function is memory bound", true, true)
67 struct AMDGPUPerfHint
{
68 friend AMDGPUPerfHintAnalysis
;
71 AMDGPUPerfHint(AMDGPUPerfHintAnalysis::FuncInfoMap
&FIM_
,
72 const TargetLowering
*TLI_
)
73 : FIM(FIM_
), DL(nullptr), TLI(TLI_
) {}
75 void runOnFunction(Function
&F
);
78 struct MemAccessInfo
{
82 MemAccessInfo() : V(nullptr), Base(nullptr), Offset(0) {}
83 bool isLargeStride(MemAccessInfo
&Reference
) const;
84 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
85 Printable
print() const {
86 return Printable([this](raw_ostream
&OS
) {
87 OS
<< "Value: " << *V
<< '\n'
88 << "Base: " << *Base
<< " Offset: " << Offset
<< '\n';
94 MemAccessInfo
makeMemAccessInfo(Instruction
*) const;
96 MemAccessInfo LastAccess
; // Last memory access info
98 AMDGPUPerfHintAnalysis::FuncInfoMap
&FIM
;
100 const DataLayout
*DL
;
104 const TargetLowering
*TLI
;
106 void visit(const Function
&F
);
107 static bool isMemBound(const AMDGPUPerfHintAnalysis::FuncInfo
&F
);
108 static bool needLimitWave(const AMDGPUPerfHintAnalysis::FuncInfo
&F
);
110 bool isIndirectAccess(const Instruction
*Inst
) const;
112 /// Check if the instruction is large stride.
113 /// The purpose is to identify memory access pattern like:
117 /// In the above example, the second and third memory access will be marked
118 /// large stride memory access.
119 bool isLargeStride(const Instruction
*Inst
);
121 bool isGlobalAddr(const Value
*V
) const;
122 bool isLocalAddr(const Value
*V
) const;
123 bool isConstantAddr(const Value
*V
) const;
126 static const Value
*getMemoryInstrPtr(const Instruction
*Inst
) {
127 if (auto LI
= dyn_cast
<LoadInst
>(Inst
)) {
128 return LI
->getPointerOperand();
130 if (auto SI
= dyn_cast
<StoreInst
>(Inst
)) {
131 return SI
->getPointerOperand();
133 if (auto AI
= dyn_cast
<AtomicCmpXchgInst
>(Inst
)) {
134 return AI
->getPointerOperand();
136 if (auto AI
= dyn_cast
<AtomicRMWInst
>(Inst
)) {
137 return AI
->getPointerOperand();
139 if (auto MI
= dyn_cast
<AnyMemIntrinsic
>(Inst
)) {
140 return MI
->getRawDest();
146 bool AMDGPUPerfHint::isIndirectAccess(const Instruction
*Inst
) const {
147 LLVM_DEBUG(dbgs() << "[isIndirectAccess] " << *Inst
<< '\n');
148 SmallSet
<const Value
*, 32> WorkSet
;
149 SmallSet
<const Value
*, 32> Visited
;
150 if (const Value
*MO
= getMemoryInstrPtr(Inst
)) {
151 if (isGlobalAddr(MO
))
155 while (!WorkSet
.empty()) {
156 const Value
*V
= *WorkSet
.begin();
157 WorkSet
.erase(*WorkSet
.begin());
158 if (!Visited
.insert(V
).second
)
160 LLVM_DEBUG(dbgs() << " check: " << *V
<< '\n');
162 if (auto LD
= dyn_cast
<LoadInst
>(V
)) {
163 auto M
= LD
->getPointerOperand();
164 if (isGlobalAddr(M
) || isLocalAddr(M
) || isConstantAddr(M
)) {
165 LLVM_DEBUG(dbgs() << " is IA\n");
171 if (auto GEP
= dyn_cast
<GetElementPtrInst
>(V
)) {
172 auto P
= GEP
->getPointerOperand();
174 for (unsigned I
= 1, E
= GEP
->getNumIndices() + 1; I
!= E
; ++I
)
175 WorkSet
.insert(GEP
->getOperand(I
));
179 if (auto U
= dyn_cast
<UnaryInstruction
>(V
)) {
180 WorkSet
.insert(U
->getOperand(0));
184 if (auto BO
= dyn_cast
<BinaryOperator
>(V
)) {
185 WorkSet
.insert(BO
->getOperand(0));
186 WorkSet
.insert(BO
->getOperand(1));
190 if (auto S
= dyn_cast
<SelectInst
>(V
)) {
191 WorkSet
.insert(S
->getFalseValue());
192 WorkSet
.insert(S
->getTrueValue());
196 if (auto E
= dyn_cast
<ExtractElementInst
>(V
)) {
197 WorkSet
.insert(E
->getVectorOperand());
201 LLVM_DEBUG(dbgs() << " dropped\n");
204 LLVM_DEBUG(dbgs() << " is not IA\n");
208 void AMDGPUPerfHint::visit(const Function
&F
) {
209 auto FIP
= FIM
.insert(std::make_pair(&F
, AMDGPUPerfHintAnalysis::FuncInfo()));
213 AMDGPUPerfHintAnalysis::FuncInfo
&FI
= FIP
.first
->second
;
215 LLVM_DEBUG(dbgs() << "[AMDGPUPerfHint] process " << F
.getName() << '\n');
218 LastAccess
= MemAccessInfo();
220 if (getMemoryInstrPtr(&I
)) {
221 if (isIndirectAccess(&I
))
223 if (isLargeStride(&I
))
229 CallSite
CS(const_cast<Instruction
*>(&I
));
231 Function
*Callee
= CS
.getCalledFunction();
232 if (!Callee
|| Callee
->isDeclaration()) {
236 if (&F
== Callee
) // Handle immediate recursion
240 auto Loc
= FIM
.find(Callee
);
242 assert(Loc
!= FIM
.end() && "No func info");
243 FI
.MemInstCount
+= Loc
->second
.MemInstCount
;
244 FI
.InstCount
+= Loc
->second
.InstCount
;
245 FI
.IAMInstCount
+= Loc
->second
.IAMInstCount
;
246 FI
.LSMInstCount
+= Loc
->second
.LSMInstCount
;
247 } else if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(&I
)) {
248 TargetLoweringBase::AddrMode AM
;
249 auto *Ptr
= GetPointerBaseWithConstantOffset(GEP
, AM
.BaseOffs
, *DL
);
250 AM
.BaseGV
= dyn_cast_or_null
<GlobalValue
>(const_cast<Value
*>(Ptr
));
251 AM
.HasBaseReg
= !AM
.BaseGV
;
252 if (TLI
->isLegalAddressingMode(*DL
, AM
, GEP
->getResultElementType(),
253 GEP
->getPointerAddressSpace()))
254 // Offset will likely be folded into load or store
264 void AMDGPUPerfHint::runOnFunction(Function
&F
) {
265 if (FIM
.find(&F
) != FIM
.end())
268 const Module
&M
= *F
.getParent();
269 DL
= &M
.getDataLayout();
270 AS
= AMDGPU::getAMDGPUAS(M
);
273 auto Loc
= FIM
.find(&F
);
275 assert(Loc
!= FIM
.end() && "No func info");
276 LLVM_DEBUG(dbgs() << F
.getName() << " MemInst: " << Loc
->second
.MemInstCount
278 << " IAMInst: " << Loc
->second
.IAMInstCount
<< '\n'
279 << " LSMInst: " << Loc
->second
.LSMInstCount
<< '\n'
280 << " TotalInst: " << Loc
->second
.InstCount
<< '\n');
282 auto &FI
= Loc
->second
;
284 if (isMemBound(FI
)) {
285 LLVM_DEBUG(dbgs() << F
.getName() << " is memory bound\n");
289 if (AMDGPU::isEntryFunctionCC(F
.getCallingConv()) && needLimitWave(FI
)) {
290 LLVM_DEBUG(dbgs() << F
.getName() << " needs limit wave\n");
295 bool AMDGPUPerfHint::isMemBound(const AMDGPUPerfHintAnalysis::FuncInfo
&FI
) {
296 return FI
.MemInstCount
* 100 / FI
.InstCount
> MemBoundThresh
;
299 bool AMDGPUPerfHint::needLimitWave(const AMDGPUPerfHintAnalysis::FuncInfo
&FI
) {
300 return ((FI
.MemInstCount
+ FI
.IAMInstCount
* IAWeight
+
301 FI
.LSMInstCount
* LSWeight
) *
302 100 / FI
.InstCount
) > LimitWaveThresh
;
305 bool AMDGPUPerfHint::isGlobalAddr(const Value
*V
) const {
306 if (auto PT
= dyn_cast
<PointerType
>(V
->getType())) {
307 unsigned As
= PT
->getAddressSpace();
308 // Flat likely points to global too.
309 return As
== AS
.GLOBAL_ADDRESS
|| As
== AS
.FLAT_ADDRESS
;
314 bool AMDGPUPerfHint::isLocalAddr(const Value
*V
) const {
315 if (auto PT
= dyn_cast
<PointerType
>(V
->getType()))
316 return PT
->getAddressSpace() == AS
.LOCAL_ADDRESS
;
320 bool AMDGPUPerfHint::isLargeStride(const Instruction
*Inst
) {
321 LLVM_DEBUG(dbgs() << "[isLargeStride] " << *Inst
<< '\n');
323 MemAccessInfo MAI
= makeMemAccessInfo(const_cast<Instruction
*>(Inst
));
324 bool IsLargeStride
= MAI
.isLargeStride(LastAccess
);
326 LastAccess
= std::move(MAI
);
328 return IsLargeStride
;
331 AMDGPUPerfHint::MemAccessInfo
332 AMDGPUPerfHint::makeMemAccessInfo(Instruction
*Inst
) const {
334 const Value
*MO
= getMemoryInstrPtr(Inst
);
336 LLVM_DEBUG(dbgs() << "[isLargeStride] MO: " << *MO
<< '\n');
337 // Do not treat local-addr memory access as large stride.
342 MAI
.Base
= GetPointerBaseWithConstantOffset(MO
, MAI
.Offset
, *DL
);
346 bool AMDGPUPerfHint::isConstantAddr(const Value
*V
) const {
347 if (auto PT
= dyn_cast
<PointerType
>(V
->getType())) {
348 unsigned As
= PT
->getAddressSpace();
349 return As
== AS
.CONSTANT_ADDRESS
|| As
== AS
.CONSTANT_ADDRESS_32BIT
;
354 bool AMDGPUPerfHint::MemAccessInfo::isLargeStride(
355 MemAccessInfo
&Reference
) const {
357 if (!Base
|| !Reference
.Base
|| Base
!= Reference
.Base
)
360 uint64_t Diff
= Offset
> Reference
.Offset
? Offset
- Reference
.Offset
361 : Reference
.Offset
- Offset
;
362 bool Result
= Diff
> LargeStrideThresh
;
363 LLVM_DEBUG(dbgs() << "[isLargeStride compare]\n"
364 << print() << "<=>\n"
365 << Reference
.print() << "Result:" << Result
<< '\n');
370 bool AMDGPUPerfHintAnalysis::runOnFunction(Function
&F
) {
371 auto *TPC
= getAnalysisIfAvailable
<TargetPassConfig
>();
375 const TargetMachine
&TM
= TPC
->getTM
<TargetMachine
>();
376 const TargetSubtargetInfo
*ST
= TM
.getSubtargetImpl(F
);
378 AMDGPUPerfHint
Analyzer(FIM
, ST
->getTargetLowering());
379 Analyzer
.runOnFunction(F
);
383 bool AMDGPUPerfHintAnalysis::isMemoryBound(const Function
*F
) const {
384 auto FI
= FIM
.find(F
);
388 return AMDGPUPerfHint::isMemBound(FI
->second
);
391 bool AMDGPUPerfHintAnalysis::needsWaveLimiter(const Function
*F
) const {
392 auto FI
= FIM
.find(F
);
396 return AMDGPUPerfHint::needLimitWave(FI
->second
);