LLVM  14.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 
14 #include "llvm/Config/llvm-config.h"
15 #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.
40 
41 class InlineAdvisor;
42 /// Capture state between an inlining decision having had been made, and
43 /// its impact being observable. When collecting model training data, this
44 /// allows recording features/decisions/partial reward data sets.
45 ///
46 /// Derivations of this type are expected to be tightly coupled with their
47 /// InliningAdvisors. The base type implements the minimal contractual
48 /// obligations.
49 class InlineAdvice {
50 public:
53 
54  InlineAdvice(InlineAdvice &&) = delete;
55  InlineAdvice(const InlineAdvice &) = delete;
56  virtual ~InlineAdvice() {
57  assert(Recorded && "InlineAdvice should have been informed of the "
58  "inliner's decision in all cases");
59  }
60 
61  /// Exactly one of the record* APIs must be called. Implementers may extend
62  /// behavior by implementing the corresponding record*Impl.
63  ///
64  /// Call after inlining succeeded, and did not result in deleting the callee.
65  void recordInlining();
66 
67  /// Call after inlining succeeded, and resulted in deleting the callee.
69 
70  /// Call after the decision for a call site was to not inline.
72  markRecorded();
74  }
75 
76  /// Call to indicate inlining was not attempted.
78  markRecorded();
80  }
81 
82  /// Get the inlining recommendation.
84  const DebugLoc &getOriginalCallSiteDebugLoc() const { return DLoc; }
85  const BasicBlock *getOriginalCallSiteBasicBlock() const { return Block; }
86 
87 protected:
88  virtual void recordInliningImpl() {}
90  virtual void recordUnsuccessfulInliningImpl(const InlineResult &Result) {}
92 
94  /// Caller and Callee are pre-inlining.
95  Function *const Caller;
96  Function *const Callee;
97 
98  // Capture the context of CB before inlining, as a successful inlining may
99  // change that context, and we want to report success or failure in the
100  // original context.
101  const DebugLoc DLoc;
102  const BasicBlock *const Block;
105 
106 private:
107  void markRecorded() {
108  assert(!Recorded && "Recording should happen exactly once");
109  Recorded = true;
110  }
111  void recordInlineStatsIfNeeded();
112 
113  bool Recorded = false;
114 };
115 
117 public:
120  bool EmitRemarks = true)
121  : InlineAdvice(Advisor, CB, ORE, OIC.hasValue()), OriginalCB(&CB),
122  OIC(OIC), EmitRemarks(EmitRemarks) {}
123 
124 private:
125  void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
126  void recordInliningWithCalleeDeletedImpl() override;
127  void recordInliningImpl() override;
128 
129 private:
130  CallBase *const OriginalCB;
132  bool EmitRemarks;
133 };
134 
135 /// Interface for deciding whether to inline a call site or not.
137 public:
138  InlineAdvisor(InlineAdvisor &&) = delete;
139  virtual ~InlineAdvisor();
140 
141  /// Get an InlineAdvice containing a recommendation on whether to
142  /// inline or not. \p CB is assumed to be a direct call. \p FAM is assumed to
143  /// be up-to-date wrt previous inlining decisions. \p MandatoryOnly indicates
144  /// only mandatory (always-inline) call sites should be recommended - this
145  /// allows the InlineAdvisor track such inlininings.
146  /// Returns an InlineAdvice with the inlining recommendation.
147  std::unique_ptr<InlineAdvice> getAdvice(CallBase &CB,
148  bool MandatoryOnly = false);
149 
150  /// This must be called when the Inliner pass is entered, to allow the
151  /// InlineAdvisor update internal state, as result of function passes run
152  /// between Inliner pass runs (for the same module).
153  virtual void onPassEntry() {}
154 
155  /// This must be called when the Inliner pass is exited, as function passes
156  /// may be run subsequently. This allows an implementation of InlineAdvisor
157  /// to prepare for a partial update.
158  virtual void onPassExit() {}
159 
160 protected:
162  virtual std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) = 0;
163  virtual std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
164  bool Advice);
165 
168  std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
169 
170  /// We may want to defer deleting functions to after the inlining for a whole
171  /// module has finished. This allows us to reliably use function pointers as
172  /// unique identifiers, as an efficient implementation detail of the
173  /// InlineAdvisor. Otherwise, it is possible the memory allocator
174  /// re-allocate Function objects at the same address of a deleted Function;
175  /// and Functions are potentially created during the function passes called
176  /// after each SCC inlining (e.g. argument promotion does that).
177  void freeDeletedFunctions();
178 
179  bool isFunctionDeleted(const Function *F) const {
180  return DeletedFunctions.count(F);
181  }
182 
184 
188 
190 
191 private:
192  friend class InlineAdvice;
193  void markFunctionAsDeleted(Function *F);
194  std::unordered_set<const Function *> DeletedFunctions;
195 };
196 
197 /// The default (manual heuristics) implementation of the InlineAdvisor. This
198 /// implementation does not need to keep state between inliner pass runs, and is
199 /// reusable as-is for inliner pass test scenarios, as well as for regular use.
201 public:
203  InlineParams Params)
204  : InlineAdvisor(M, FAM), Params(Params) {}
205 
206 private:
207  std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
208 
209  void onPassExit() override { freeDeletedFunctions(); }
210 
211  InlineParams Params;
212 };
213 
214 /// The InlineAdvisorAnalysis is a module pass because the InlineAdvisor
215 /// needs to capture state right before inlining commences over a module.
216 class InlineAdvisorAnalysis : public AnalysisInfoMixin<InlineAdvisorAnalysis> {
217 public:
218  static AnalysisKey Key;
219  InlineAdvisorAnalysis() = default;
220  struct Result {
224  // InlineAdvisor must be preserved across analysis invalidations.
225  return false;
226  }
228  StringRef ReplayFile);
229  InlineAdvisor *getAdvisor() const { return Advisor.get(); }
230  void clear() { Advisor.reset(); }
231 
232  private:
233  Module &M;
235  std::unique_ptr<InlineAdvisor> Advisor;
236  };
237 
239 };
240 
241 #ifdef LLVM_HAVE_TF_AOT
242 std::unique_ptr<InlineAdvisor>
243 getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM);
244 #endif
245 
246 #ifdef LLVM_HAVE_TF_API
247 std::unique_ptr<InlineAdvisor>
248 getDevelopmentModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
249  std::function<bool(CallBase &)> GetDefaultAdvice);
250 #endif
251 
252 // Default (manual policy) decision making helper APIs. Shared with the legacy
253 // pass manager inliner.
254 
255 /// Return the cost only if the inliner should attempt to inline at the given
256 /// CallSite. If we return the cost, we will emit an optimisation remark later
257 /// using that cost, so we won't do so from this function. Return None if
258 /// inlining should not be attempted.
259 Optional<InlineCost>
260 shouldInline(CallBase &CB, function_ref<InlineCost(CallBase &CB)> GetInlineCost,
261  OptimizationRemarkEmitter &ORE, bool EnableDeferral = true);
262 
263 /// Emit ORE message.
264 void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
265  const BasicBlock *Block, const Function &Callee,
266  const Function &Caller, const InlineCost &IC,
267  bool ForProfileContext = false,
268  const char *PassName = nullptr);
269 
270 /// get call site location as string
271 std::string getCallSiteLocation(DebugLoc DLoc);
272 
273 /// Add location info to ORE message.
274 void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc);
275 
276 /// Set the inline-remark attribute.
277 void setInlineRemark(CallBase &CB, StringRef Message);
278 
279 /// Utility for extracting the inline cost message to a string.
280 std::string inlineCostStr(const InlineCost &IC);
281 } // namespace llvm
282 #endif // LLVM_ANALYSIS_INLINEADVISOR_H
llvm::InlineAdvisor::isFunctionDeleted
bool isFunctionDeleted(const Function *F) const
Definition: InlineAdvisor.h:179
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:229
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
---------------------— PointerInfo ------------------------------------—
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:230
llvm::getCallSiteLocation
std::string getCallSiteLocation(DebugLoc DLoc)
get call site location as string
Definition: InlineAdvisor.cpp:386
llvm::InlineAdvisor::ImportedFunctionsStats
std::unique_ptr< ImportedFunctionsInliningStatistics > ImportedFunctionsStats
Definition: InlineAdvisor.h:168
llvm::InlineAdvisor::M
Module & M
Definition: InlineAdvisor.h:166
llvm::Function
Definition: Function.h:61
llvm::InlineAdvisorAnalysis::run
Result run(Module &M, ModuleAnalysisManager &MAM)
Definition: InlineAdvisor.h:238
llvm::InlineAdvice::Caller
Function *const Caller
Caller and Callee are pre-inlining.
Definition: InlineAdvisor.h:95
llvm::InlineAdvice::~InlineAdvice
virtual ~InlineAdvice()
Definition: InlineAdvisor.h:56
llvm::ModuleAnalysisManager
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition: PassManager.h:912
llvm::Optional
Definition: APInt.h:33
llvm::InlineParams
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:185
llvm::InlineAdvisor::MandatoryInliningKind::Always
@ Always
llvm::InlineAdvisor::MandatoryInliningKind::Never
@ Never
llvm::DefaultInlineAdvice
Definition: InlineAdvisor.h:116
llvm::addLocationToRemarks
void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc)
Add location info to ORE message.
Definition: InlineAdvisor.cpp:412
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::InlineAdvisor::getCallerORE
OptimizationRemarkEmitter & getCallerORE(CallBase &CB)
Definition: InlineAdvisor.cpp:514
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
ImportedFunctionsInliningStatistics.h
llvm::InlineAdvice::IsInliningRecommended
const bool IsInliningRecommended
Definition: InlineAdvisor.h:104
llvm::InlineAdvice::ORE
OptimizationRemarkEmitter & ORE
Definition: InlineAdvisor.h:103
llvm::DefaultInlineAdvisor
The default (manual heuristics) implementation of the InlineAdvisor.
Definition: InlineAdvisor.h:200
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:202
llvm::InlineAdvice::getOriginalCallSiteBasicBlock
const BasicBlock * getOriginalCallSiteBasicBlock() const
Definition: InlineAdvisor.h:85
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:666
llvm::InlineAdvisor::MandatoryInliningKind
MandatoryInliningKind
Definition: InlineAdvisor.h:183
llvm::InlineAdvisorAnalysis::Result
Definition: InlineAdvisor.h:220
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:438
llvm::InlineAdvice
Capture state between an inlining decision having had been made, and its impact being observable.
Definition: InlineAdvisor.h:49
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:77
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:222
llvm::InlineAdvice::Block
const BasicBlock *const Block
Definition: InlineAdvisor.h:102
llvm::InlineAdvice::recordInliningImpl
virtual void recordInliningImpl()
Definition: InlineAdvisor.h:88
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::setInlineRemark
void setInlineRemark(CallBase &CB, StringRef Message)
Set the inline-remark attribute.
Definition: InlineAdvisor.cpp:311
llvm::InlineAdvice::DLoc
const DebugLoc DLoc
Definition: InlineAdvisor.h:101
InlineCost.h
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
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:397
llvm::InlineAdvice::recordUnsuccessfulInliningImpl
virtual void recordUnsuccessfulInliningImpl(const InlineResult &Result)
Definition: InlineAdvisor.h:90
llvm::InlineAdvice::isInliningRecommended
bool isInliningRecommended() const
Get the inlining recommendation.
Definition: InlineAdvisor.h:83
llvm::InlineAdvisor::MandatoryInliningKind::NotMandatory
@ NotMandatory
llvm::InlineAdvisorAnalysis::Result::Result
Result(Module &M, ModuleAnalysisManager &MAM)
Definition: InlineAdvisor.h:221
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:153
llvm::InlineAdvisor
Interface for deciding whether to inline a call site or not.
Definition: InlineAdvisor.h:136
llvm::InlineAdvisorAnalysis::InlineAdvisorAnalysis
InlineAdvisorAnalysis()=default
llvm::InlineAdvice::recordUnattemptedInliningImpl
virtual void recordUnattemptedInliningImpl()
Definition: InlineAdvisor.h:91
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::InlineAdvice::recordInliningWithCalleeDeletedImpl
virtual void recordInliningWithCalleeDeletedImpl()
Definition: InlineAdvisor.h:89
llvm::InlineAdvisor::getMandatoryAdvice
virtual std::unique_ptr< InlineAdvice > getMandatoryAdvice(CallBase &CB, bool Advice)
Definition: InlineAdvisor.cpp:476
llvm::InlineAdvisor::FAM
FunctionAnalysisManager & FAM
Definition: InlineAdvisor.h:167
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:504
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:158
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:304
llvm::InlineAdvisor::getMandatoryKind
static MandatoryInliningKind getMandatoryKind(CallBase &CB, FunctionAnalysisManager &FAM, OptimizationRemarkEmitter &ORE)
Definition: InlineAdvisor.cpp:482
llvm::InlineAdvisorAnalysis
The InlineAdvisorAnalysis is a module pass because the InlineAdvisor needs to capture state right bef...
Definition: InlineAdvisor.h:216
llvm::InlineAdvisorAnalysis::Key
static AnalysisKey Key
Definition: InlineAdvisor.h:218
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:1161
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:118
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::InlineAdvice::getOriginalCallSiteDebugLoc
const DebugLoc & getOriginalCallSiteDebugLoc() const
Definition: InlineAdvisor.h:84
llvm::InlineAdvice::Advisor
InlineAdvisor *const Advisor
Definition: InlineAdvisor.h:93
llvm::InlineResult
InlineResult is basically true or false.
Definition: InlineCost.h:159
llvm::InlineAdvice::Callee
Function *const Callee
Definition: InlineAdvisor.h:96
llvm::InlineAdvice::recordUnsuccessfulInlining
void recordUnsuccessfulInlining(const InlineResult &Result)
Call after the decision for a call site was to not inline.
Definition: InlineAdvisor.h:71
llvm::InlineAdvisor::~InlineAdvisor
virtual ~InlineAdvisor()
Definition: InlineAdvisor.cpp:466
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:324