14ba319b5SDimitry Andric //===- AMDGPUPerfHintAnalysis.cpp - analysis of functions memory traffic --===//
24ba319b5SDimitry Andric //
34ba319b5SDimitry Andric //                     The LLVM Compiler Infrastructure
44ba319b5SDimitry Andric //
54ba319b5SDimitry Andric // This file is distributed under the University of Illinois Open Source
64ba319b5SDimitry Andric // License. See LICENSE.TXT for details.
74ba319b5SDimitry Andric //
84ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
94ba319b5SDimitry Andric //
104ba319b5SDimitry Andric /// \file
114ba319b5SDimitry Andric /// \brief Analyzes if a function potentially memory bound and if a kernel
124ba319b5SDimitry Andric /// kernel may benefit from limiting number of waves to reduce cache thrashing.
134ba319b5SDimitry Andric ///
144ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
154ba319b5SDimitry Andric 
164ba319b5SDimitry Andric #include "AMDGPU.h"
174ba319b5SDimitry Andric #include "AMDGPUPerfHintAnalysis.h"
184ba319b5SDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
194ba319b5SDimitry Andric #include "llvm/ADT/SmallSet.h"
204ba319b5SDimitry Andric #include "llvm/ADT/Statistic.h"
214ba319b5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
224ba319b5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
234ba319b5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
244ba319b5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
254ba319b5SDimitry Andric #include "llvm/IR/Constants.h"
264ba319b5SDimitry Andric #include "llvm/IR/Instructions.h"
274ba319b5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
284ba319b5SDimitry Andric #include "llvm/IR/Module.h"
294ba319b5SDimitry Andric #include "llvm/IR/ValueMap.h"
304ba319b5SDimitry Andric #include "llvm/Support/CommandLine.h"
314ba319b5SDimitry Andric 
324ba319b5SDimitry Andric using namespace llvm;
334ba319b5SDimitry Andric 
344ba319b5SDimitry Andric #define DEBUG_TYPE "amdgpu-perf-hint"
354ba319b5SDimitry Andric 
364ba319b5SDimitry Andric static cl::opt<unsigned>
374ba319b5SDimitry Andric     MemBoundThresh("amdgpu-membound-threshold", cl::init(50), cl::Hidden,
384ba319b5SDimitry Andric                    cl::desc("Function mem bound threshold in %"));
394ba319b5SDimitry Andric 
404ba319b5SDimitry Andric static cl::opt<unsigned>
414ba319b5SDimitry Andric     LimitWaveThresh("amdgpu-limit-wave-threshold", cl::init(50), cl::Hidden,
424ba319b5SDimitry Andric                     cl::desc("Kernel limit wave threshold in %"));
434ba319b5SDimitry Andric 
444ba319b5SDimitry Andric static cl::opt<unsigned>
454ba319b5SDimitry Andric     IAWeight("amdgpu-indirect-access-weight", cl::init(1000), cl::Hidden,
464ba319b5SDimitry Andric              cl::desc("Indirect access memory instruction weight"));
474ba319b5SDimitry Andric 
484ba319b5SDimitry Andric static cl::opt<unsigned>
494ba319b5SDimitry Andric     LSWeight("amdgpu-large-stride-weight", cl::init(1000), cl::Hidden,
504ba319b5SDimitry Andric              cl::desc("Large stride memory access weight"));
514ba319b5SDimitry Andric 
524ba319b5SDimitry Andric static cl::opt<unsigned>
534ba319b5SDimitry Andric     LargeStrideThresh("amdgpu-large-stride-threshold", cl::init(64), cl::Hidden,
544ba319b5SDimitry Andric                       cl::desc("Large stride memory access threshold"));
554ba319b5SDimitry Andric 
564ba319b5SDimitry Andric STATISTIC(NumMemBound, "Number of functions marked as memory bound");
574ba319b5SDimitry Andric STATISTIC(NumLimitWave, "Number of functions marked as needing limit wave");
584ba319b5SDimitry Andric 
594ba319b5SDimitry Andric char llvm::AMDGPUPerfHintAnalysis::ID = 0;
604ba319b5SDimitry Andric char &llvm::AMDGPUPerfHintAnalysisID = AMDGPUPerfHintAnalysis::ID;
614ba319b5SDimitry Andric 
624ba319b5SDimitry Andric INITIALIZE_PASS(AMDGPUPerfHintAnalysis, DEBUG_TYPE,
634ba319b5SDimitry Andric                 "Analysis if a function is memory bound", true, true)
644ba319b5SDimitry Andric 
654ba319b5SDimitry Andric namespace {
664ba319b5SDimitry Andric 
674ba319b5SDimitry Andric struct AMDGPUPerfHint {
684ba319b5SDimitry Andric   friend AMDGPUPerfHintAnalysis;
694ba319b5SDimitry Andric 
704ba319b5SDimitry Andric public:
AMDGPUPerfHint__anon152fb87a0111::AMDGPUPerfHint714ba319b5SDimitry Andric   AMDGPUPerfHint(AMDGPUPerfHintAnalysis::FuncInfoMap &FIM_,
724ba319b5SDimitry Andric                  const TargetLowering *TLI_)
734ba319b5SDimitry Andric       : FIM(FIM_), DL(nullptr), TLI(TLI_) {}
744ba319b5SDimitry Andric 
754ba319b5SDimitry Andric   void runOnFunction(Function &F);
764ba319b5SDimitry Andric 
774ba319b5SDimitry Andric private:
784ba319b5SDimitry Andric   struct MemAccessInfo {
794ba319b5SDimitry Andric     const Value *V;
804ba319b5SDimitry Andric     const Value *Base;
814ba319b5SDimitry Andric     int64_t Offset;
MemAccessInfo__anon152fb87a0111::AMDGPUPerfHint::MemAccessInfo824ba319b5SDimitry Andric     MemAccessInfo() : V(nullptr), Base(nullptr), Offset(0) {}
834ba319b5SDimitry Andric     bool isLargeStride(MemAccessInfo &Reference) const;
844ba319b5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
print__anon152fb87a0111::AMDGPUPerfHint::MemAccessInfo854ba319b5SDimitry Andric     Printable print() const {
864ba319b5SDimitry Andric       return Printable([this](raw_ostream &OS) {
874ba319b5SDimitry Andric         OS << "Value: " << *V << '\n'
884ba319b5SDimitry Andric            << "Base: " << *Base << " Offset: " << Offset << '\n';
894ba319b5SDimitry Andric       });
904ba319b5SDimitry Andric     }
914ba319b5SDimitry Andric #endif
924ba319b5SDimitry Andric   };
934ba319b5SDimitry Andric 
944ba319b5SDimitry Andric   MemAccessInfo makeMemAccessInfo(Instruction *) const;
954ba319b5SDimitry Andric 
964ba319b5SDimitry Andric   MemAccessInfo LastAccess; // Last memory access info
974ba319b5SDimitry Andric 
984ba319b5SDimitry Andric   AMDGPUPerfHintAnalysis::FuncInfoMap &FIM;
994ba319b5SDimitry Andric 
1004ba319b5SDimitry Andric   const DataLayout *DL;
1014ba319b5SDimitry Andric 
1024ba319b5SDimitry Andric   const TargetLowering *TLI;
1034ba319b5SDimitry Andric 
1044ba319b5SDimitry Andric   void visit(const Function &F);
1054ba319b5SDimitry Andric   static bool isMemBound(const AMDGPUPerfHintAnalysis::FuncInfo &F);
1064ba319b5SDimitry Andric   static bool needLimitWave(const AMDGPUPerfHintAnalysis::FuncInfo &F);
1074ba319b5SDimitry Andric 
1084ba319b5SDimitry Andric   bool isIndirectAccess(const Instruction *Inst) const;
1094ba319b5SDimitry Andric 
1104ba319b5SDimitry Andric   /// Check if the instruction is large stride.
1114ba319b5SDimitry Andric   /// The purpose is to identify memory access pattern like:
1124ba319b5SDimitry Andric   /// x = a[i];
1134ba319b5SDimitry Andric   /// y = a[i+1000];
1144ba319b5SDimitry Andric   /// z = a[i+2000];
1154ba319b5SDimitry Andric   /// In the above example, the second and third memory access will be marked
1164ba319b5SDimitry Andric   /// large stride memory access.
1174ba319b5SDimitry Andric   bool isLargeStride(const Instruction *Inst);
1184ba319b5SDimitry Andric 
1194ba319b5SDimitry Andric   bool isGlobalAddr(const Value *V) const;
1204ba319b5SDimitry Andric   bool isLocalAddr(const Value *V) const;
1214ba319b5SDimitry Andric   bool isConstantAddr(const Value *V) const;
1224ba319b5SDimitry Andric };
1234ba319b5SDimitry Andric 
getMemoryInstrPtr(const Instruction * Inst)1244ba319b5SDimitry Andric static const Value *getMemoryInstrPtr(const Instruction *Inst) {
1254ba319b5SDimitry Andric   if (auto LI = dyn_cast<LoadInst>(Inst)) {
1264ba319b5SDimitry Andric     return LI->getPointerOperand();
1274ba319b5SDimitry Andric   }
1284ba319b5SDimitry Andric   if (auto SI = dyn_cast<StoreInst>(Inst)) {
1294ba319b5SDimitry Andric     return SI->getPointerOperand();
1304ba319b5SDimitry Andric   }
1314ba319b5SDimitry Andric   if (auto AI = dyn_cast<AtomicCmpXchgInst>(Inst)) {
1324ba319b5SDimitry Andric     return AI->getPointerOperand();
1334ba319b5SDimitry Andric   }
1344ba319b5SDimitry Andric   if (auto AI = dyn_cast<AtomicRMWInst>(Inst)) {
1354ba319b5SDimitry Andric     return AI->getPointerOperand();
1364ba319b5SDimitry Andric   }
1374ba319b5SDimitry Andric   if (auto MI = dyn_cast<AnyMemIntrinsic>(Inst)) {
1384ba319b5SDimitry Andric     return MI->getRawDest();
1394ba319b5SDimitry Andric   }
1404ba319b5SDimitry Andric 
1414ba319b5SDimitry Andric   return nullptr;
1424ba319b5SDimitry Andric }
1434ba319b5SDimitry Andric 
isIndirectAccess(const Instruction * Inst) const1444ba319b5SDimitry Andric bool AMDGPUPerfHint::isIndirectAccess(const Instruction *Inst) const {
1454ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "[isIndirectAccess] " << *Inst << '\n');
1464ba319b5SDimitry Andric   SmallSet<const Value *, 32> WorkSet;
1474ba319b5SDimitry Andric   SmallSet<const Value *, 32> Visited;
1484ba319b5SDimitry Andric   if (const Value *MO = getMemoryInstrPtr(Inst)) {
1494ba319b5SDimitry Andric     if (isGlobalAddr(MO))
1504ba319b5SDimitry Andric       WorkSet.insert(MO);
1514ba319b5SDimitry Andric   }
1524ba319b5SDimitry Andric 
1534ba319b5SDimitry Andric   while (!WorkSet.empty()) {
1544ba319b5SDimitry Andric     const Value *V = *WorkSet.begin();
1554ba319b5SDimitry Andric     WorkSet.erase(*WorkSet.begin());
1564ba319b5SDimitry Andric     if (!Visited.insert(V).second)
1574ba319b5SDimitry Andric       continue;
1584ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "  check: " << *V << '\n');
1594ba319b5SDimitry Andric 
1604ba319b5SDimitry Andric     if (auto LD = dyn_cast<LoadInst>(V)) {
1614ba319b5SDimitry Andric       auto M = LD->getPointerOperand();
1624ba319b5SDimitry Andric       if (isGlobalAddr(M) || isLocalAddr(M) || isConstantAddr(M)) {
1634ba319b5SDimitry Andric         LLVM_DEBUG(dbgs() << "    is IA\n");
1644ba319b5SDimitry Andric         return true;
1654ba319b5SDimitry Andric       }
1664ba319b5SDimitry Andric       continue;
1674ba319b5SDimitry Andric     }
1684ba319b5SDimitry Andric 
1694ba319b5SDimitry Andric     if (auto GEP = dyn_cast<GetElementPtrInst>(V)) {
1704ba319b5SDimitry Andric       auto P = GEP->getPointerOperand();
1714ba319b5SDimitry Andric       WorkSet.insert(P);
1724ba319b5SDimitry Andric       for (unsigned I = 1, E = GEP->getNumIndices() + 1; I != E; ++I)
1734ba319b5SDimitry Andric         WorkSet.insert(GEP->getOperand(I));
1744ba319b5SDimitry Andric       continue;
1754ba319b5SDimitry Andric     }
1764ba319b5SDimitry Andric 
1774ba319b5SDimitry Andric     if (auto U = dyn_cast<UnaryInstruction>(V)) {
1784ba319b5SDimitry Andric       WorkSet.insert(U->getOperand(0));
1794ba319b5SDimitry Andric       continue;
1804ba319b5SDimitry Andric     }
1814ba319b5SDimitry Andric 
1824ba319b5SDimitry Andric     if (auto BO = dyn_cast<BinaryOperator>(V)) {
1834ba319b5SDimitry Andric       WorkSet.insert(BO->getOperand(0));
1844ba319b5SDimitry Andric       WorkSet.insert(BO->getOperand(1));
1854ba319b5SDimitry Andric       continue;
1864ba319b5SDimitry Andric     }
1874ba319b5SDimitry Andric 
1884ba319b5SDimitry Andric     if (auto S = dyn_cast<SelectInst>(V)) {
1894ba319b5SDimitry Andric       WorkSet.insert(S->getFalseValue());
1904ba319b5SDimitry Andric       WorkSet.insert(S->getTrueValue());
1914ba319b5SDimitry Andric       continue;
1924ba319b5SDimitry Andric     }
1934ba319b5SDimitry Andric 
1944ba319b5SDimitry Andric     if (auto E = dyn_cast<ExtractElementInst>(V)) {
1954ba319b5SDimitry Andric       WorkSet.insert(E->getVectorOperand());
1964ba319b5SDimitry Andric       continue;
1974ba319b5SDimitry Andric     }
1984ba319b5SDimitry Andric 
1994ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "    dropped\n");
2004ba319b5SDimitry Andric   }
2014ba319b5SDimitry Andric 
2024ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "  is not IA\n");
2034ba319b5SDimitry Andric   return false;
2044ba319b5SDimitry Andric }
2054ba319b5SDimitry Andric 
visit(const Function & F)2064ba319b5SDimitry Andric void AMDGPUPerfHint::visit(const Function &F) {
2074ba319b5SDimitry Andric   auto FIP = FIM.insert(std::make_pair(&F, AMDGPUPerfHintAnalysis::FuncInfo()));
2084ba319b5SDimitry Andric   if (!FIP.second)
2094ba319b5SDimitry Andric     return;
2104ba319b5SDimitry Andric 
2114ba319b5SDimitry Andric   AMDGPUPerfHintAnalysis::FuncInfo &FI = FIP.first->second;
2124ba319b5SDimitry Andric 
2134ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "[AMDGPUPerfHint] process " << F.getName() << '\n');
2144ba319b5SDimitry Andric 
2154ba319b5SDimitry Andric   for (auto &B : F) {
2164ba319b5SDimitry Andric     LastAccess = MemAccessInfo();
2174ba319b5SDimitry Andric     for (auto &I : B) {
2184ba319b5SDimitry Andric       if (getMemoryInstrPtr(&I)) {
2194ba319b5SDimitry Andric         if (isIndirectAccess(&I))
2204ba319b5SDimitry Andric           ++FI.IAMInstCount;
2214ba319b5SDimitry Andric         if (isLargeStride(&I))
2224ba319b5SDimitry Andric           ++FI.LSMInstCount;
2234ba319b5SDimitry Andric         ++FI.MemInstCount;
2244ba319b5SDimitry Andric         ++FI.InstCount;
2254ba319b5SDimitry Andric         continue;
2264ba319b5SDimitry Andric       }
2274ba319b5SDimitry Andric       CallSite CS(const_cast<Instruction *>(&I));
2284ba319b5SDimitry Andric       if (CS) {
2294ba319b5SDimitry Andric         Function *Callee = CS.getCalledFunction();
2304ba319b5SDimitry Andric         if (!Callee || Callee->isDeclaration()) {
2314ba319b5SDimitry Andric           ++FI.InstCount;
2324ba319b5SDimitry Andric           continue;
2334ba319b5SDimitry Andric         }
2344ba319b5SDimitry Andric         if (&F == Callee) // Handle immediate recursion
2354ba319b5SDimitry Andric           continue;
2364ba319b5SDimitry Andric 
2374ba319b5SDimitry Andric         visit(*Callee);
2384ba319b5SDimitry Andric         auto Loc = FIM.find(Callee);
2394ba319b5SDimitry Andric 
2404ba319b5SDimitry Andric         assert(Loc != FIM.end() && "No func info");
2414ba319b5SDimitry Andric         FI.MemInstCount += Loc->second.MemInstCount;
2424ba319b5SDimitry Andric         FI.InstCount += Loc->second.InstCount;
2434ba319b5SDimitry Andric         FI.IAMInstCount += Loc->second.IAMInstCount;
2444ba319b5SDimitry Andric         FI.LSMInstCount += Loc->second.LSMInstCount;
2454ba319b5SDimitry Andric       } else if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
2464ba319b5SDimitry Andric         TargetLoweringBase::AddrMode AM;
2474ba319b5SDimitry Andric         auto *Ptr = GetPointerBaseWithConstantOffset(GEP, AM.BaseOffs, *DL);
2484ba319b5SDimitry Andric         AM.BaseGV = dyn_cast_or_null<GlobalValue>(const_cast<Value *>(Ptr));
2494ba319b5SDimitry Andric         AM.HasBaseReg = !AM.BaseGV;
2504ba319b5SDimitry Andric         if (TLI->isLegalAddressingMode(*DL, AM, GEP->getResultElementType(),
2514ba319b5SDimitry Andric                                        GEP->getPointerAddressSpace()))
2524ba319b5SDimitry Andric           // Offset will likely be folded into load or store
2534ba319b5SDimitry Andric           continue;
2544ba319b5SDimitry Andric         ++FI.InstCount;
2554ba319b5SDimitry Andric       } else {
2564ba319b5SDimitry Andric         ++FI.InstCount;
2574ba319b5SDimitry Andric       }
2584ba319b5SDimitry Andric     }
2594ba319b5SDimitry Andric   }
2604ba319b5SDimitry Andric }
2614ba319b5SDimitry Andric 
runOnFunction(Function & F)2624ba319b5SDimitry Andric void AMDGPUPerfHint::runOnFunction(Function &F) {
2634ba319b5SDimitry Andric   if (FIM.find(&F) != FIM.end())
2644ba319b5SDimitry Andric     return;
2654ba319b5SDimitry Andric 
2664ba319b5SDimitry Andric   const Module &M = *F.getParent();
2674ba319b5SDimitry Andric   DL = &M.getDataLayout();
2684ba319b5SDimitry Andric 
2694ba319b5SDimitry Andric   visit(F);
2704ba319b5SDimitry Andric   auto Loc = FIM.find(&F);
2714ba319b5SDimitry Andric 
2724ba319b5SDimitry Andric   assert(Loc != FIM.end() && "No func info");
2734ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << F.getName() << " MemInst: " << Loc->second.MemInstCount
2744ba319b5SDimitry Andric                     << '\n'
2754ba319b5SDimitry Andric                     << " IAMInst: " << Loc->second.IAMInstCount << '\n'
2764ba319b5SDimitry Andric                     << " LSMInst: " << Loc->second.LSMInstCount << '\n'
2774ba319b5SDimitry Andric                     << " TotalInst: " << Loc->second.InstCount << '\n');
2784ba319b5SDimitry Andric 
2794ba319b5SDimitry Andric   auto &FI = Loc->second;
2804ba319b5SDimitry Andric 
2814ba319b5SDimitry Andric   if (isMemBound(FI)) {
2824ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << F.getName() << " is memory bound\n");
2834ba319b5SDimitry Andric     NumMemBound++;
2844ba319b5SDimitry Andric   }
2854ba319b5SDimitry Andric 
2864ba319b5SDimitry Andric   if (AMDGPU::isEntryFunctionCC(F.getCallingConv()) && needLimitWave(FI)) {
2874ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << F.getName() << " needs limit wave\n");
2884ba319b5SDimitry Andric     NumLimitWave++;
2894ba319b5SDimitry Andric   }
2904ba319b5SDimitry Andric }
2914ba319b5SDimitry Andric 
isMemBound(const AMDGPUPerfHintAnalysis::FuncInfo & FI)2924ba319b5SDimitry Andric bool AMDGPUPerfHint::isMemBound(const AMDGPUPerfHintAnalysis::FuncInfo &FI) {
2934ba319b5SDimitry Andric   return FI.MemInstCount * 100 / FI.InstCount > MemBoundThresh;
2944ba319b5SDimitry Andric }
2954ba319b5SDimitry Andric 
needLimitWave(const AMDGPUPerfHintAnalysis::FuncInfo & FI)2964ba319b5SDimitry Andric bool AMDGPUPerfHint::needLimitWave(const AMDGPUPerfHintAnalysis::FuncInfo &FI) {
2974ba319b5SDimitry Andric   return ((FI.MemInstCount + FI.IAMInstCount * IAWeight +
2984ba319b5SDimitry Andric            FI.LSMInstCount * LSWeight) *
2994ba319b5SDimitry Andric           100 / FI.InstCount) > LimitWaveThresh;
3004ba319b5SDimitry Andric }
3014ba319b5SDimitry Andric 
isGlobalAddr(const Value * V) const3024ba319b5SDimitry Andric bool AMDGPUPerfHint::isGlobalAddr(const Value *V) const {
3034ba319b5SDimitry Andric   if (auto PT = dyn_cast<PointerType>(V->getType())) {
3044ba319b5SDimitry Andric     unsigned As = PT->getAddressSpace();
3054ba319b5SDimitry Andric     // Flat likely points to global too.
306*b5893f02SDimitry Andric     return As == AMDGPUAS::GLOBAL_ADDRESS || As == AMDGPUAS::FLAT_ADDRESS;
3074ba319b5SDimitry Andric   }
3084ba319b5SDimitry Andric   return false;
3094ba319b5SDimitry Andric }
3104ba319b5SDimitry Andric 
isLocalAddr(const Value * V) const3114ba319b5SDimitry Andric bool AMDGPUPerfHint::isLocalAddr(const Value *V) const {
3124ba319b5SDimitry Andric   if (auto PT = dyn_cast<PointerType>(V->getType()))
313*b5893f02SDimitry Andric     return PT->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
3144ba319b5SDimitry Andric   return false;
3154ba319b5SDimitry Andric }
3164ba319b5SDimitry Andric 
isLargeStride(const Instruction * Inst)3174ba319b5SDimitry Andric bool AMDGPUPerfHint::isLargeStride(const Instruction *Inst) {
3184ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "[isLargeStride] " << *Inst << '\n');
3194ba319b5SDimitry Andric 
3204ba319b5SDimitry Andric   MemAccessInfo MAI = makeMemAccessInfo(const_cast<Instruction *>(Inst));
3214ba319b5SDimitry Andric   bool IsLargeStride = MAI.isLargeStride(LastAccess);
3224ba319b5SDimitry Andric   if (MAI.Base)
3234ba319b5SDimitry Andric     LastAccess = std::move(MAI);
3244ba319b5SDimitry Andric 
3254ba319b5SDimitry Andric   return IsLargeStride;
3264ba319b5SDimitry Andric }
3274ba319b5SDimitry Andric 
3284ba319b5SDimitry Andric AMDGPUPerfHint::MemAccessInfo
makeMemAccessInfo(Instruction * Inst) const3294ba319b5SDimitry Andric AMDGPUPerfHint::makeMemAccessInfo(Instruction *Inst) const {
3304ba319b5SDimitry Andric   MemAccessInfo MAI;
3314ba319b5SDimitry Andric   const Value *MO = getMemoryInstrPtr(Inst);
3324ba319b5SDimitry Andric 
3334ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "[isLargeStride] MO: " << *MO << '\n');
3344ba319b5SDimitry Andric   // Do not treat local-addr memory access as large stride.
3354ba319b5SDimitry Andric   if (isLocalAddr(MO))
3364ba319b5SDimitry Andric     return MAI;
3374ba319b5SDimitry Andric 
3384ba319b5SDimitry Andric   MAI.V = MO;
3394ba319b5SDimitry Andric   MAI.Base = GetPointerBaseWithConstantOffset(MO, MAI.Offset, *DL);
3404ba319b5SDimitry Andric   return MAI;
3414ba319b5SDimitry Andric }
3424ba319b5SDimitry Andric 
isConstantAddr(const Value * V) const3434ba319b5SDimitry Andric bool AMDGPUPerfHint::isConstantAddr(const Value *V) const {
3444ba319b5SDimitry Andric   if (auto PT = dyn_cast<PointerType>(V->getType())) {
3454ba319b5SDimitry Andric     unsigned As = PT->getAddressSpace();
346*b5893f02SDimitry Andric     return As == AMDGPUAS::CONSTANT_ADDRESS ||
347*b5893f02SDimitry Andric            As == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
3484ba319b5SDimitry Andric   }
3494ba319b5SDimitry Andric   return false;
3504ba319b5SDimitry Andric }
3514ba319b5SDimitry Andric 
isLargeStride(MemAccessInfo & Reference) const3524ba319b5SDimitry Andric bool AMDGPUPerfHint::MemAccessInfo::isLargeStride(
3534ba319b5SDimitry Andric     MemAccessInfo &Reference) const {
3544ba319b5SDimitry Andric 
3554ba319b5SDimitry Andric   if (!Base || !Reference.Base || Base != Reference.Base)
3564ba319b5SDimitry Andric     return false;
3574ba319b5SDimitry Andric 
3584ba319b5SDimitry Andric   uint64_t Diff = Offset > Reference.Offset ? Offset - Reference.Offset
3594ba319b5SDimitry Andric                                             : Reference.Offset - Offset;
3604ba319b5SDimitry Andric   bool Result = Diff > LargeStrideThresh;
3614ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "[isLargeStride compare]\n"
3624ba319b5SDimitry Andric                << print() << "<=>\n"
3634ba319b5SDimitry Andric                << Reference.print() << "Result:" << Result << '\n');
3644ba319b5SDimitry Andric   return Result;
3654ba319b5SDimitry Andric }
3664ba319b5SDimitry Andric } // namespace
3674ba319b5SDimitry Andric 
runOnFunction(Function & F)3684ba319b5SDimitry Andric bool AMDGPUPerfHintAnalysis::runOnFunction(Function &F) {
3694ba319b5SDimitry Andric   auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
3704ba319b5SDimitry Andric   if (!TPC)
3714ba319b5SDimitry Andric     return false;
3724ba319b5SDimitry Andric 
3734ba319b5SDimitry Andric   const TargetMachine &TM = TPC->getTM<TargetMachine>();
3744ba319b5SDimitry Andric   const TargetSubtargetInfo *ST = TM.getSubtargetImpl(F);
3754ba319b5SDimitry Andric 
3764ba319b5SDimitry Andric   AMDGPUPerfHint Analyzer(FIM, ST->getTargetLowering());
3774ba319b5SDimitry Andric   Analyzer.runOnFunction(F);
3784ba319b5SDimitry Andric   return false;
3794ba319b5SDimitry Andric }
3804ba319b5SDimitry Andric 
isMemoryBound(const Function * F) const3814ba319b5SDimitry Andric bool AMDGPUPerfHintAnalysis::isMemoryBound(const Function *F) const {
3824ba319b5SDimitry Andric   auto FI = FIM.find(F);
3834ba319b5SDimitry Andric   if (FI == FIM.end())
3844ba319b5SDimitry Andric     return false;
3854ba319b5SDimitry Andric 
3864ba319b5SDimitry Andric   return AMDGPUPerfHint::isMemBound(FI->second);
3874ba319b5SDimitry Andric }
3884ba319b5SDimitry Andric 
needsWaveLimiter(const Function * F) const3894ba319b5SDimitry Andric bool AMDGPUPerfHintAnalysis::needsWaveLimiter(const Function *F) const {
3904ba319b5SDimitry Andric   auto FI = FIM.find(F);
3914ba319b5SDimitry Andric   if (FI == FIM.end())
3924ba319b5SDimitry Andric     return false;
3934ba319b5SDimitry Andric 
3944ba319b5SDimitry Andric   return AMDGPUPerfHint::needLimitWave(FI->second);
3954ba319b5SDimitry Andric }
396