LLVM  6.0.0svn
Go to the documentation of this file.
1 //===- AMDGPUInline.cpp - Code to perform simple function inlining --------===//
2 //
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
11 /// \brief This is AMDGPU specific replacement of the standard inliner.
12 /// The main purpose is to account for the fact that calls not only expensive
13 /// on the AMDGPU, but much more expensive if a private memory pointer is
14 /// passed to a function as an argument. In this situation, we are unable to
15 /// eliminate private memory in the caller unless inlined and end up with slow
16 /// and expensive scratch access. Thus, we boost the inline threshold for such
17 /// functions here.
18 ///
19 //===----------------------------------------------------------------------===//
22 #include "AMDGPU.h"
23 #include "llvm/Transforms/IPO.h"
29 #include "llvm/IR/CallSite.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
35 #include "llvm/Support/Debug.h"
38 using namespace llvm;
40 #define DEBUG_TYPE "inline"
42 static cl::opt<int>
43 ArgAllocaCost("amdgpu-inline-arg-alloca-cost", cl::Hidden, cl::init(2200),
44  cl::desc("Cost of alloca argument"));
46 // If the amount of scratch memory to eliminate exceeds our ability to allocate
47 // it into registers we gain nothing by agressively inlining functions for that
48 // heuristic.
49 static cl::opt<unsigned>
50 ArgAllocaCutoff("amdgpu-inline-arg-alloca-cutoff", cl::Hidden, cl::init(256),
51  cl::desc("Maximum alloca size to use for inline cost"));
53 namespace {
55 class AMDGPUInliner : public LegacyInlinerBase {
57 public:
58  AMDGPUInliner() : LegacyInlinerBase(ID) {
60  Params = getInlineParams();
61  }
63  static char ID; // Pass identification, replacement for typeid
65  unsigned getInlineThreshold(CallSite CS) const;
67  InlineCost getInlineCost(CallSite CS) override;
69  bool runOnSCC(CallGraphSCC &SCC) override;
71  void getAnalysisUsage(AnalysisUsage &AU) const override;
73 private:
76  InlineParams Params;
77 };
79 } // end anonymous namespace
81 char AMDGPUInliner::ID = 0;
82 INITIALIZE_PASS_BEGIN(AMDGPUInliner, "amdgpu-inline",
83  "AMDGPU Function Integration/Inlining", false, false)
89 INITIALIZE_PASS_END(AMDGPUInliner, "amdgpu-inline",
90  "AMDGPU Function Integration/Inlining", false, false)
92 Pass *llvm::createAMDGPUFunctionInliningPass() { return new AMDGPUInliner(); }
94 bool AMDGPUInliner::runOnSCC(CallGraphSCC &SCC) {
95  TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
96  return LegacyInlinerBase::runOnSCC(SCC);
97 }
99 void AMDGPUInliner::getAnalysisUsage(AnalysisUsage &AU) const {
102 }
104 unsigned AMDGPUInliner::getInlineThreshold(CallSite CS) const {
105  int Thres = Params.DefaultThreshold;
107  Function *Caller = CS.getCaller();
108  // Listen to the inlinehint attribute when it would increase the threshold
109  // and the caller does not need to minimize its size.
111  bool InlineHint = Callee && !Callee->isDeclaration() &&
112  Callee->hasFnAttribute(Attribute::InlineHint);
113  if (InlineHint && Params.HintThreshold && Params.HintThreshold > Thres
114  && !Caller->hasFnAttribute(Attribute::MinSize))
115  Thres = Params.HintThreshold.getValue();
117  const DataLayout &DL = Caller->getParent()->getDataLayout();
118  if (!Callee)
119  return (unsigned)Thres;
121  const AMDGPUAS AS = AMDGPU::getAMDGPUAS(*Caller->getParent());
123  // If we have a pointer to private array passed into a function
124  // it will not be optimized out, leaving scratch usage.
125  // Increase the inline threshold to allow inliniting in this case.
126  uint64_t AllocaSize = 0;
128  for (Value *PtrArg : CS.args()) {
129  Type *Ty = PtrArg->getType();
130  if (!Ty->isPointerTy() ||
132  continue;
133  PtrArg = GetUnderlyingObject(PtrArg, DL);
134  if (const AllocaInst *AI = dyn_cast<AllocaInst>(PtrArg)) {
135  if (!AI->isStaticAlloca() || !AIVisited.insert(AI).second)
136  continue;
137  AllocaSize += DL.getTypeAllocSize(AI->getAllocatedType());
138  // If the amount of stack memory is excessive we will not be able
139  // to get rid of the scratch anyway, bail out.
140  if (AllocaSize > ArgAllocaCutoff) {
141  AllocaSize = 0;
142  break;
143  }
144  }
145  }
146  if (AllocaSize)
147  Thres += ArgAllocaCost;
149  return (unsigned)Thres;
150 }
152 // Check if call is just a wrapper around another call.
153 // In this case we only have call and ret instructions.
154 static bool isWrapperOnlyCall(CallSite CS) {
156  if (!Callee || Callee->size() != 1)
157  return false;
158  const BasicBlock &BB = Callee->getEntryBlock();
159  if (const Instruction *I = BB.getFirstNonPHI()) {
160  if (!isa<CallInst>(I)) {
161  return false;
162  }
163  if (isa<ReturnInst>(*std::next(I->getIterator()))) {
164  DEBUG(dbgs() << " Wrapper only call detected: "
165  << Callee->getName() << '\n');
166  return true;
167  }
168  }
169  return false;
170 }
174  Function *Caller = CS.getCaller();
175  TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
177  if (!Callee || Callee->isDeclaration() || CS.isNoInline() ||
178  !TTI.areInlineCompatible(Caller, Callee))
181  if (CS.hasFnAttr(Attribute::AlwaysInline)) {
182  if (isInlineViable(*Callee))
185  }
187  if (isWrapperOnlyCall(CS))
190  InlineParams LocalParams = Params;
191  LocalParams.DefaultThreshold = (int)getInlineThreshold(CS);
192  bool RemarksEnabled = false;
193  const auto &BBs = Caller->getBasicBlockList();
194  if (!BBs.empty()) {
195  auto DI = OptimizationRemark(DEBUG_TYPE, "", DebugLoc(), &BBs.front());
196  if (DI.isEnabled())
197  RemarksEnabled = true;
198  }
200  OptimizationRemarkEmitter ORE(Caller);
201  std::function<AssumptionCache &(Function &)> GetAssumptionCache =
202  [this](Function &F) -> AssumptionCache & {
203  return ACT->getAssumptionCache(F);
204  };
206  return llvm::getInlineCost(CS, Callee, LocalParams, TTI, GetAssumptionCache,
207  None, PSI, RemarksEnabled ? &ORE : nullptr);
208 }
size_t size() const
Definition: Function.h:593
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:130
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
#define DEBUG_TYPE
AMDGPUAS getAMDGPUAS(const Module &M)
An immutable pass that tracks lazily created AssumptionCache objects.
iterator_range< IterTy > args() const
Definition: CallSite.h:215
A cache of .assume calls within a function.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:262
A debug info location.
Definition: DebugLoc.h:34
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:503
static bool isWrapperOnlyCall(CallSite CS)
Represents the cost of inlining a function.
Definition: InlineCost.h:65
bool isNoInline() const
Return true if the call should not be inlined.
Definition: CallSite.h:438
AnalysisUsage & addRequired()
Definition: PassSupport.h:51
bool runOnSCC(CallGraphSCC &SCC) override
Main run interface method, this implements the interface required by the Pass class.
Definition: Inliner.cpp:458
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
Definition: AMDGPU.h:214
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Pass * createAMDGPUFunctionInliningPass()
This class contains all of the helper code which is used to perform the inlining operations that do n...
Definition: Inliner.h:31
amdgpu AMDGPU Function Integration Inlining
bool isInlineViable(Function &Callee)
Minimal filter to detect invalid constructs for inlining.
void initializeAMDGPUInlinerPass(PassRegistry &)
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
static cl::opt< unsigned > ArgAllocaCutoff("amdgpu-inline-arg-alloca-cutoff", cl::Hidden, cl::init(256), cl::desc("Maximum alloca size to use for inline cost"))
amdgpu Simplify well known AMD library false Value * Callee
const BasicBlock & getEntryBlock() const
Definition: Function.h:572
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
void getAnalysisUsage(AnalysisUsage &Info) const override
For this class, we declare that we require and preserve the call graph.
Definition: Inliner.cpp:123
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:171
Wrapper pass for TargetTransformInfo.
The ModulePass which wraps up a CallGraph and the logic to build it.
Definition: CallGraph.h:324
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool hasFnAttr(Attribute::AttrKind Kind) const
Return true if this function has the given attribute.
Definition: CallSite.h:362
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:221
amdgpu inline
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
Diagnostic information for applied optimization remarks.
Represent the analysis usage information of a pass.
static InlineCost getNever()
Definition: InlineCost.h:89
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
InlineCost getInlineCost(CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI, std::function< AssumptionCache &(Function &)> &GetAssumptionCache, Optional< function_ref< BlockFrequencyInfo &(Function &)>> GetBFI, ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE=nullptr)
Get an InlineCost object representing the cost of inlining this callsite.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
InlineParams getInlineParams()
Generate the parameters to tune the inline cost analysis based only on the commandline options...
Module.h This file contains the declarations for the Module class.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:403
FunTy * getCaller() const
Return the caller function for this call site.
Definition: CallSite.h:267
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
bool areInlineCompatible(const Function *Caller, const Function *Callee) const
#define I(x, y, z)
Definition: MD5.cpp:58
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
Definition: Function.h:565
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:201
static cl::opt< int > ArgAllocaCost("amdgpu-inline-arg-alloca-cost", cl::Hidden, cl::init(2200), cl::desc("Cost of alloca argument"))
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:107
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:556
INITIALIZE_PASS_BEGIN(AMDGPUInliner, "amdgpu-inline", "AMDGPU Function Integration/Inlining", false, false) INITIALIZE_PASS_END(AMDGPUInliner
LLVM Value Representation.
Definition: Value.h:73
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
#define DEBUG(X)
Definition: Debug.h:118
This pass exposes codegen information to IR-level passes.
static InlineCost getAlways()
Definition: InlineCost.h:86
The optimization diagnostic interface.
int DefaultThreshold
The default threshold to start with for a callee.
Definition: InlineCost.h:132
Address space for private memory.
Definition: AMDGPU.h:216
an instruction to allocate memory on the stack
Definition: Instructions.h:60