LLVM  13.0.0git
InlineAdvisor.h
Go to the documentation of this file.
1 //===- InlineAdvisor.h - Inlining decision making abstraction -*- C++ ---*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 #ifndef LLVM_ANALYSIS_INLINEADVISOR_H
10 #define LLVM_ANALYSIS_INLINEADVISOR_H
11 
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/IR/PassManager.h"
16 #include <memory>
17 #include <unordered_set>
18 
19 namespace llvm {
20 class BasicBlock;
21 class CallBase;
22 class Function;
23 class Module;
24 class OptimizationRemarkEmitter;
25 
26 /// There are 3 scenarios we can use the InlineAdvisor:
27 /// - Default - use manual heuristics.
28 ///
29 /// - Release mode, the expected mode for production, day to day deployments.
30 /// In this mode, when building the compiler, we also compile a pre-trained ML
31 /// model to native code, and link it as a static library. This mode has low
32 /// overhead and no additional dependencies for the compiler runtime.
33 ///
34 /// - Development mode, for training new models.
35 /// In this mode, we trade off runtime performance for flexibility. This mode
36 /// requires the full C Tensorflow API library, and evaluates models
37 /// dynamically. This mode also permits generating training logs, for offline
38 /// training.
39 enum class InliningAdvisorMode : int {
40  Default,
41  Release,
43 };
44 
45 class InlineAdvisor;
46 /// Capture state between an inlining decision having had been made, and
47 /// its impact being observable. When collecting model training data, this
48 /// allows recording features/decisions/partial reward data sets.
49 ///
50 /// Derivations of this type are expected to be tightly coupled with their
51 /// InliningAdvisors. The base type implements the minimal contractual
52 /// obligations.
53 class InlineAdvice {
54 public:
57 
58  InlineAdvice(InlineAdvice &&) = delete;
59  InlineAdvice(const InlineAdvice &) = delete;
60  virtual ~InlineAdvice() {
61  assert(Recorded && "InlineAdvice should have been informed of the "
62  "inliner's decision in all cases");
63  }
64 
65  /// Exactly one of the record* APIs must be called. Implementers may extend
66  /// behavior by implementing the corresponding record*Impl.
67  ///
68  /// Call after inlining succeeded, and did not result in deleting the callee.
69  void recordInlining();
70 
71  /// Call after inlining succeeded, and resulted in deleting the callee.
73 
74  /// Call after the decision for a call site was to not inline.
76  markRecorded();
78  }
79 
80  /// Call to indicate inlining was not attempted.
82  markRecorded();
84  }
85 
86  /// Get the inlining recommendation.
88  const DebugLoc &getOriginalCallSiteDebugLoc() const { return DLoc; }
89  const BasicBlock *getOriginalCallSiteBasicBlock() const { return Block; }
90 
91 protected:
92  virtual void recordInliningImpl() {}
94  virtual void recordUnsuccessfulInliningImpl(const InlineResult &Result) {}
96 
98  /// Caller and Callee are pre-inlining.
99  Function *const Caller;
100  Function *const Callee;
101 
102  // Capture the context of CB before inlining, as a successful inlining may
103  // change that context, and we want to report success or failure in the
104  // original context.
105  const DebugLoc DLoc;
106  const BasicBlock *const Block;
109 
110 private:
111  void markRecorded() {
112  assert(!Recorded && "Recording should happen exactly once");
113  Recorded = true;
114  }
115  void recordInlineStatsIfNeeded();
116 
117  bool Recorded = false;
118 };
119 
121 public:
124  bool EmitRemarks = true)
125  : InlineAdvice(Advisor, CB, ORE, OIC.hasValue()), OriginalCB(&CB),
126  OIC(OIC), EmitRemarks(EmitRemarks) {}
127 
128 private:
129  void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
130  void recordInliningWithCalleeDeletedImpl() override;
131  void recordInliningImpl() override;
132 
133 private:
134  CallBase *const OriginalCB;
136  bool EmitRemarks;
137 };
138 
139 /// Interface for deciding whether to inline a call site or not.
141 public:
142  InlineAdvisor(InlineAdvisor &&) = delete;
143  virtual ~InlineAdvisor();
144 
145  /// Get an InlineAdvice containing a recommendation on whether to
146  /// inline or not. \p CB is assumed to be a direct call. \p FAM is assumed to
147  /// be up-to-date wrt previous inlining decisions. \p MandatoryOnly indicates
148  /// only mandatory (always-inline) call sites should be recommended - this
149  /// allows the InlineAdvisor track such inlininings.
150  /// Returns an InlineAdvice with the inlining recommendation.
151  std::unique_ptr<InlineAdvice> getAdvice(CallBase &CB,
152  bool MandatoryOnly = false);
153 
154  /// This must be called when the Inliner pass is entered, to allow the
155  /// InlineAdvisor update internal state, as result of function passes run
156  /// between Inliner pass runs (for the same module).
157  virtual void onPassEntry() {}
158 
159  /// This must be called when the Inliner pass is exited, as function passes
160  /// may be run subsequently. This allows an implementation of InlineAdvisor
161  /// to prepare for a partial update.
162  virtual void onPassExit() {}
163 
164 protected:
166  virtual std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) = 0;
167  virtual std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
168  bool Advice);
169 
172  std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
173 
174  /// We may want to defer deleting functions to after the inlining for a whole
175  /// module has finished. This allows us to reliably use function pointers as
176  /// unique identifiers, as an efficient implementation detail of the
177  /// InlineAdvisor. Otherwise, it is possible the memory allocator
178  /// re-allocate Function objects at the same address of a deleted Function;
179  /// and Functions are potentially created during the function passes called
180  /// after each SCC inlining (e.g. argument promotion does that).
181  void freeDeletedFunctions();
182 
183  bool isFunctionDeleted(const Function *F) const {
184  return DeletedFunctions.count(F);
185  }
186 
188 
192 
194 
195 private:
196  friend class InlineAdvice;
197  void markFunctionAsDeleted(Function *F);
198  std::unordered_set<const Function *> DeletedFunctions;
199 };
200 
201 /// The default (manual heuristics) implementation of the InlineAdvisor. This
202 /// implementation does not need to keep state between inliner pass runs, and is
203 /// reusable as-is for inliner pass test scenarios, as well as for regular use.
205 public:
207  InlineParams Params)
208  : InlineAdvisor(M, FAM), Params(Params) {}
209 
210 private:
211  std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
212 
213  void onPassExit() override { freeDeletedFunctions(); }
214 
215  InlineParams Params;
216 };
217 
218 /// The InlineAdvisorAnalysis is a module pass because the InlineAdvisor
219 /// needs to capture state right before inlining commences over a module.
220 class InlineAdvisorAnalysis : public AnalysisInfoMixin<InlineAdvisorAnalysis> {
221 public:
222  static AnalysisKey Key;
223  InlineAdvisorAnalysis() = default;
224  struct Result {
228  // InlineAdvisor must be preserved across analysis invalidations.
229  return false;
230  }
232  StringRef ReplayFile);
233  InlineAdvisor *getAdvisor() const { return Advisor.get(); }
234  void clear() { Advisor.reset(); }
235 
236  private:
237  Module &M;
239  std::unique_ptr<InlineAdvisor> Advisor;
240  };
241 
243 };
244 
245 #ifdef LLVM_HAVE_TF_AOT
246 std::unique_ptr<InlineAdvisor>
247 getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM);
248 #endif
249 
250 #ifdef LLVM_HAVE_TF_API
251 std::unique_ptr<InlineAdvisor>
252 getDevelopmentModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
253  std::function<bool(CallBase &)> GetDefaultAdvice);
254 #endif
255 
256 // Default (manual policy) decision making helper APIs. Shared with the legacy
257 // pass manager inliner.
258 
259 /// Return the cost only if the inliner should attempt to inline at the given
260 /// CallSite. If we return the cost, we will emit an optimisation remark later
261 /// using that cost, so we won't do so from this function. Return None if
262 /// inlining should not be attempted.
263 Optional<InlineCost>
264 shouldInline(CallBase &CB, function_ref<InlineCost(CallBase &CB)> GetInlineCost,
265  OptimizationRemarkEmitter &ORE, bool EnableDeferral = true);
266 
267 /// Emit ORE message.
268 void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
269  const BasicBlock *Block, const Function &Callee,
270  const Function &Caller, const InlineCost &IC,
271  bool ForProfileContext = false,
272  const char *PassName = nullptr);
273 
274 /// get call site location as string
275 std::string getCallSiteLocation(DebugLoc DLoc);
276 
277 /// Add location info to ORE message.
278 void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc);
279 
280 /// Set the inline-remark attribute.
281 void setInlineRemark(CallBase &CB, StringRef Message);
282 
283 /// Utility for extracting the inline cost message to a string.
284 std::string inlineCostStr(const InlineCost &IC);
285 } // namespace llvm
286 #endif // LLVM_ANALYSIS_INLINEADVISOR_H
llvm::InlineAdvisor::isFunctionDeleted
bool isFunctionDeleted(const Function *F) const
Definition: InlineAdvisor.h:183
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::InlineAdvice::recordInlining
void recordInlining()
Exactly one of the record* APIs must be called.
Definition: InlineAdvisor.cpp:139
llvm::InlineAdvisorAnalysis::Result::getAdvisor
InlineAdvisor * getAdvisor() const
Definition: InlineAdvisor.h:233
llvm::InlineAdvisor::freeDeletedFunctions
void freeDeletedFunctions()
We may want to defer deleting functions to after the inlining for a whole module has finished.
Definition: InlineAdvisor.cpp:128
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::InlineAdvisorAnalysis::Result::tryCreate
bool tryCreate(InlineParams Params, InliningAdvisorMode Mode, StringRef ReplayFile)
Definition: InlineAdvisor.cpp:154
llvm::InlineAdvisorAnalysis::Result::clear
void clear()
Definition: InlineAdvisor.h:234
llvm::getCallSiteLocation
std::string getCallSiteLocation(DebugLoc DLoc)
get call site location as string
Definition: InlineAdvisor.cpp:383
llvm::InlineAdvisor::ImportedFunctionsStats
std::unique_ptr< ImportedFunctionsInliningStatistics > ImportedFunctionsStats
Definition: InlineAdvisor.h:172
llvm::InlineAdvisor::M
Module & M
Definition: InlineAdvisor.h:170
llvm::Function
Definition: Function.h:61
llvm::InlineAdvisorAnalysis::run
Result run(Module &M, ModuleAnalysisManager &MAM)
Definition: InlineAdvisor.h:242
llvm::InlineAdvice::Caller
Function *const Caller
Caller and Callee are pre-inlining.
Definition: InlineAdvisor.h:99
llvm::InlineAdvice::~InlineAdvice
virtual ~InlineAdvice()
Definition: InlineAdvisor.h:60
llvm::ModuleAnalysisManager
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition: PassManager.h:902
llvm::Optional
Definition: APInt.h:33
llvm::InlineParams
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:160
llvm::InlineAdvisor::MandatoryInliningKind::Always
@ Always
llvm::InlineAdvisor::MandatoryInliningKind::Never
@ Never
llvm::DefaultInlineAdvice
Definition: InlineAdvisor.h:120
llvm::addLocationToRemarks
void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc)
Add location info to ORE message.
Definition: InlineAdvisor.cpp:409
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::InlineAdvisor::getCallerORE
OptimizationRemarkEmitter & getCallerORE(CallBase &CB)
Definition: InlineAdvisor.cpp:511
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
ImportedFunctionsInliningStatistics.h
llvm::InlineAdvice::IsInliningRecommended
const bool IsInliningRecommended
Definition: InlineAdvisor.h:108
llvm::InlineAdvice::ORE
OptimizationRemarkEmitter & ORE
Definition: InlineAdvisor.h:107
llvm::DefaultInlineAdvisor
The default (manual heuristics) implementation of the InlineAdvisor.
Definition: InlineAdvisor.h:204
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
MAM
ModuleAnalysisManager MAM
Definition: PassBuilderBindings.cpp:61
llvm::DefaultInlineAdvisor::DefaultInlineAdvisor
DefaultInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, InlineParams Params)
Definition: InlineAdvisor.h:206
llvm::InlineAdvice::getOriginalCallSiteBasicBlock
const BasicBlock * getOriginalCallSiteBasicBlock() const
Definition: InlineAdvisor.h:89
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:656
llvm::InlineAdvisor::MandatoryInliningKind
MandatoryInliningKind
Definition: InlineAdvisor.h:187
llvm::InlineAdvisorAnalysis::Result
Definition: InlineAdvisor.h:224
llvm::emitInlinedInto
void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc, const BasicBlock *Block, const Function &Callee, const Function &Caller, const InlineCost &IC, bool ForProfileContext=false, const char *PassName=nullptr)
Emit ORE message.
Definition: InlineAdvisor.cpp:435
llvm::InlineAdvice
Capture state between an inlining decision having had been made, and its impact being observable.
Definition: InlineAdvisor.h:53
llvm::InliningAdvisorMode
InliningAdvisorMode
There are 3 scenarios we can use the InlineAdvisor:
Definition: InlineAdvisor.h:39
llvm::InlineAdvisor::InlineAdvisor
InlineAdvisor(InlineAdvisor &&)=delete
llvm::InlineAdvice::recordUnattemptedInlining
void recordUnattemptedInlining()
Call to indicate inlining was not attempted.
Definition: InlineAdvisor.h:81
llvm::InlineAdvice::recordInliningWithCalleeDeleted
void recordInliningWithCalleeDeleted()
Call after inlining succeeded, and resulted in deleting the callee.
Definition: InlineAdvisor.cpp:145
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::InlineAdvisorAnalysis::Result::invalidate
bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
Definition: InlineAdvisor.h:226
llvm::InlineAdvice::Block
const BasicBlock *const Block
Definition: InlineAdvisor.h:106
llvm::InlineAdvice::recordInliningImpl
virtual void recordInliningImpl()
Definition: InlineAdvisor.h:92
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::setInlineRemark
void setInlineRemark(CallBase &CB, StringRef Message)
Set the inline-remark attribute.
Definition: InlineAdvisor.cpp:308
llvm::InlineAdvice::DLoc
const DebugLoc DLoc
Definition: InlineAdvisor.h:105
InlineCost.h
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:64
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::InliningAdvisorMode::Development
@ Development
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:391
llvm::InlineAdvice::recordUnsuccessfulInliningImpl
virtual void recordUnsuccessfulInliningImpl(const InlineResult &Result)
Definition: InlineAdvisor.h:94
llvm::InlineAdvice::isInliningRecommended
bool isInliningRecommended() const
Get the inlining recommendation.
Definition: InlineAdvisor.h:87
llvm::InlineAdvisor::MandatoryInliningKind::NotMandatory
@ NotMandatory
llvm::InlineAdvisorAnalysis::Result::Result
Result(Module &M, ModuleAnalysisManager &MAM)
Definition: InlineAdvisor.h:225
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::InlineAdvisor::onPassEntry
virtual void onPassEntry()
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
Definition: InlineAdvisor.h:157
llvm::InlineAdvisor
Interface for deciding whether to inline a call site or not.
Definition: InlineAdvisor.h:140
llvm::InlineAdvisorAnalysis::InlineAdvisorAnalysis
InlineAdvisorAnalysis()=default
llvm::InlineAdvice::recordUnattemptedInliningImpl
virtual void recordUnattemptedInliningImpl()
Definition: InlineAdvisor.h:95
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::InlineAdvice::recordInliningWithCalleeDeletedImpl
virtual void recordInliningWithCalleeDeletedImpl()
Definition: InlineAdvisor.h:93
llvm::InlineAdvisor::getMandatoryAdvice
virtual std::unique_ptr< InlineAdvice > getMandatoryAdvice(CallBase &CB, bool Advice)
Definition: InlineAdvisor.cpp:473
llvm::InlineAdvisor::FAM
FunctionAnalysisManager & FAM
Definition: InlineAdvisor.h:171
llvm::InlineAdvisor::getAdvice
std::unique_ptr< InlineAdvice > getAdvice(CallBase &CB, bool MandatoryOnly=false)
Get an InlineAdvice containing a recommendation on whether to inline or not.
Definition: InlineAdvisor.cpp:501
llvm::InlineAdvisor::onPassExit
virtual void onPassExit()
This must be called when the Inliner pass is exited, as function passes may be run subsequently.
Definition: InlineAdvisor.h:162
llvm::InlineAdvisor::getAdviceImpl
virtual std::unique_ptr< InlineAdvice > getAdviceImpl(CallBase &CB)=0
PassManager.h
llvm::inlineCostStr
std::string inlineCostStr(const InlineCost &IC)
Utility for extracting the inline cost message to a string.
Definition: InlineAdvisor.cpp:301
llvm::InlineAdvisor::getMandatoryKind
static MandatoryInliningKind getMandatoryKind(CallBase &CB, FunctionAnalysisManager &FAM, OptimizationRemarkEmitter &ORE)
Definition: InlineAdvisor.cpp:479
llvm::InlineAdvisorAnalysis
The InlineAdvisorAnalysis is a module pass because the InlineAdvisor needs to capture state right bef...
Definition: InlineAdvisor.h:220
llvm::InlineAdvisorAnalysis::Key
static AnalysisKey Key
Definition: InlineAdvisor.h:222
llvm::InliningAdvisorMode::Default
@ Default
llvm::InlineAdvice::InlineAdvice
InlineAdvice(InlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool IsInliningRecommended)
Definition: InlineAdvisor.cpp:115
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1164
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::InliningAdvisorMode::Release
@ Release
llvm::DefaultInlineAdvice::DefaultInlineAdvice
DefaultInlineAdvice(InlineAdvisor *Advisor, CallBase &CB, Optional< InlineCost > OIC, OptimizationRemarkEmitter &ORE, bool EmitRemarks=true)
Definition: InlineAdvisor.h:122
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::InlineAdvice::getOriginalCallSiteDebugLoc
const DebugLoc & getOriginalCallSiteDebugLoc() const
Definition: InlineAdvisor.h:88
llvm::InlineAdvice::Advisor
InlineAdvisor *const Advisor
Definition: InlineAdvisor.h:97
llvm::InlineResult
InlineResult is basically true or false.
Definition: InlineCost.h:134
llvm::InlineAdvice::Callee
Function *const Callee
Definition: InlineAdvisor.h:100
llvm::InlineAdvice::recordUnsuccessfulInlining
void recordUnsuccessfulInlining(const InlineResult &Result)
Call after the decision for a call site was to not inline.
Definition: InlineAdvisor.h:75
llvm::InlineAdvisor::~InlineAdvisor
virtual ~InlineAdvisor()
Definition: InlineAdvisor.cpp:463
PassName
static const char PassName[]
Definition: X86LowerAMXIntrinsics.cpp:669
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::shouldInline
Optional< InlineCost > shouldInline(CallBase &CB, function_ref< InlineCost(CallBase &CB)> GetInlineCost, OptimizationRemarkEmitter &ORE, bool EnableDeferral=true)
Return the cost only if the inliner should attempt to inline at the given CallSite.
Definition: InlineAdvisor.cpp:321