34 #define DEBUG_TYPE "amdgpu-perf-hint" 38 cl::desc(
"Function mem bound threshold in %"));
42 cl::desc(
"Kernel limit wave threshold in %"));
46 cl::desc(
"Indirect access memory instruction weight"));
50 cl::desc(
"Large stride memory access weight"));
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");
63 "Analysis if a function is memory bound",
true,
true)
67 struct AMDGPUPerfHint {
73 : FIM(FIM_), DL(
nullptr), TLI(TLI_) {}
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) 87 OS <<
"Value: " << *V <<
'\n' 88 <<
"Base: " << *Base <<
" Offset: " << Offset <<
'\n';
94 MemAccessInfo makeMemAccessInfo(
Instruction *)
const;
96 MemAccessInfo LastAccess;
110 bool isIndirectAccess(
const Instruction *Inst)
const;
121 bool isGlobalAddr(
const Value *V)
const;
122 bool isLocalAddr(
const Value *V)
const;
123 bool isConstantAddr(
const Value *V)
const;
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 {
150 if (
const Value *MO = getMemoryInstrPtr(Inst)) {
151 if (isGlobalAddr(MO))
155 while (!WorkSet.
empty()) {
158 if (!Visited.
insert(V).second)
162 if (
auto LD = dyn_cast<LoadInst>(V)) {
163 auto M =
LD->getPointerOperand();
164 if (isGlobalAddr(M) || isLocalAddr(M) || isConstantAddr(M)) {
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)
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());
208 void AMDGPUPerfHint::visit(
const Function &
F) {
218 LastAccess = MemAccessInfo();
220 if (getMemoryInstrPtr(&
I)) {
221 if (isIndirectAccess(&
I))
223 if (isLargeStride(&
I))
229 CallSite CS(const_cast<Instruction *>(&
I));
240 auto Loc = FIM.find(Callee);
242 assert(Loc != FIM.end() &&
"No func info");
247 }
else if (
auto *
GEP = dyn_cast<GetElementPtrInst>(&
I)) {
250 AM.
BaseGV = dyn_cast_or_null<GlobalValue>(
const_cast<Value *
>(
Ptr));
252 if (TLI->isLegalAddressingMode(*DL, AM,
GEP->getResultElementType(),
253 GEP->getPointerAddressSpace()))
265 if (FIM.find(&F) != FIM.end())
273 auto Loc = FIM.find(&F);
275 assert(Loc != FIM.end() &&
"No func info");
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)) {
305 bool AMDGPUPerfHint::isGlobalAddr(
const Value *V)
const {
306 if (
auto PT = dyn_cast<PointerType>(V->
getType())) {
307 unsigned As = PT->getAddressSpace();
314 bool AMDGPUPerfHint::isLocalAddr(
const Value *V)
const {
315 if (
auto PT = dyn_cast<PointerType>(V->
getType()))
320 bool AMDGPUPerfHint::isLargeStride(
const Instruction *Inst) {
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);
346 bool AMDGPUPerfHint::isConstantAddr(
const Value *V)
const {
347 if (
auto PT = dyn_cast<PointerType>(V->
getType())) {
348 unsigned As = PT->getAddressSpace();
354 bool AMDGPUPerfHint::MemAccessInfo::isLargeStride(
357 if (!
Base || !Reference.Base ||
Base != Reference.Base)
360 uint64_t Diff =
Offset > Reference.Offset ?
Offset - Reference.Offset
361 : Reference.Offset -
Offset;
364 <<
print() <<
"<=>\n" 365 << Reference.print() <<
"Result:" << Result <<
'\n');
371 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
378 AMDGPUPerfHint Analyzer(FIM, ST->getTargetLowering());
379 Analyzer.runOnFunction(F);
384 auto FI = FIM.find(F);
388 return AMDGPUPerfHint::isMemBound(FI->second);
392 auto FI = FIM.find(F);
396 return AMDGPUPerfHint::needLimitWave(FI->second);
A parsed version of the target data layout string in and methods for querying it. ...
This represents an addressing mode of: BaseGV + BaseOffs + BaseReg + Scale*ScaleReg If BaseGV is null...
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
bool needsWaveLimiter(const Function *F) const
AMDGPUAS getAMDGPUAS(const Module &M)
static cl::opt< unsigned > LimitWaveThresh("amdgpu-limit-wave-threshold", cl::init(50), cl::Hidden, cl::desc("Kernel limit wave threshold in %"))
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
STATISTIC(NumFunctions, "Total number of functions")
Address space for local memory.
Address space for constant memory (VTX2)
LLVM_NODISCARD bool empty() const
const_iterator begin() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Type * getType() const
All values are typed, get the type of this value.
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset...
static cl::opt< unsigned > LSWeight("amdgpu-large-stride-weight", cl::init(1000), cl::Hidden, cl::desc("Large stride memory access weight"))
static cl::opt< unsigned > LargeStrideThresh("amdgpu-large-stride-threshold", cl::init(64), cl::Hidden, cl::desc("Large stride memory access threshold"))
amdgpu Simplify well known AMD library false Value * Callee
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isEntryFunctionCC(CallingConv::ID CC)
char & AMDGPUPerfHintAnalysisID
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static cl::opt< unsigned > IAWeight("amdgpu-indirect-access-weight", cl::init(1000), cl::Hidden, cl::desc("Indirect access memory instruction weight"))
INITIALIZE_PASS(AMDGPUPerfHintAnalysis, DEBUG_TYPE, "Analysis if a function is memory bound", true, true) namespace
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Address space for 32-bit constant memory.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
static cl::opt< unsigned > MemBoundThresh("amdgpu-membound-threshold", cl::init(50), cl::Hidden, cl::desc("Function mem bound threshold in %"))
Module.h This file contains the declarations for the Module class.
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Address space for global memory (RAT0, VTX0).
TargetSubtargetInfo - Generic base class for all target subtargets.
bool isMemoryBound(const Function *F) const
StringRef getName() const
Return a constant reference to the value's name.
unsigned FLAT_ADDRESS
Address space for flat memory.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it's an indirect...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream...
Primary interface to the complete machine description for the target machine.
Simple wrapper around std::function<void(raw_ostream&)>.
Analyzes if a function potentially memory bound and if a kernel kernel may benefit from limiting numb...
This file describes how to lower LLVM code to machine code.