LLVM  15.0.0git
MLInlineAdvisor.h
Go to the documentation of this file.
1 //===- MLInlineAdvisor.h - ML - based InlineAdvisor factories ---*- 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_MLINLINEADVISOR_H
10 #define LLVM_ANALYSIS_MLINLINEADVISOR_H
11 
16 #include "llvm/IR/PassManager.h"
17 
18 #include <deque>
19 #include <map>
20 #include <memory>
21 
22 namespace llvm {
23 class DiagnosticInfoOptimizationBase;
24 class Module;
25 class MLInlineAdvice;
26 
28 public:
30  std::unique_ptr<MLModelRunner> ModelRunner);
31 
32  virtual ~MLInlineAdvisor() = default;
33 
34  void onPassEntry(LazyCallGraph::SCC *SCC) override;
35  void onPassExit(LazyCallGraph::SCC *SCC) override;
36 
37  int64_t getIRSize(Function &F) const {
39  }
40  void onSuccessfulInlining(const MLInlineAdvice &Advice,
41  bool CalleeWasDeleted);
42 
43  bool isForcedToStop() const { return ForceStop; }
44  int64_t getLocalCalls(Function &F);
45  const MLModelRunner &getModelRunner() const { return *ModelRunner.get(); }
47 
48 protected:
49  std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
50 
51  std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
52  bool Advice) override;
53 
54  virtual std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB);
55 
56  virtual std::unique_ptr<MLInlineAdvice>
58 
59  // Get the initial 'level' of the function, or 0 if the function has been
60  // introduced afterwards.
61  // TODO: should we keep this updated?
62  unsigned getInitialFunctionLevel(const Function &F) const;
63 
64  std::unique_ptr<MLModelRunner> ModelRunner;
65 
66 private:
67  int64_t getModuleIRSize() const;
68  std::unique_ptr<InlineAdvice>
69  getSkipAdviceIfUnreachableCallsite(CallBase &CB);
70  void print(raw_ostream &OS) const override;
71 
73 
74  LazyCallGraph &CG;
75 
76  int64_t NodeCount = 0;
77  int64_t EdgeCount = 0;
78  int64_t EdgesOfLastSeenNodes = 0;
79 
80  std::map<const LazyCallGraph::Node *, unsigned> FunctionLevels;
81  const int32_t InitialIRSize = 0;
82  int32_t CurrentIRSize = 0;
85  bool ForceStop = false;
86 };
87 
88 /// InlineAdvice that tracks changes post inlining. For that reason, it only
89 /// overrides the "successful inlining" extension points.
90 class MLInlineAdvice : public InlineAdvice {
91 public:
93  OptimizationRemarkEmitter &ORE, bool Recommendation);
94  virtual ~MLInlineAdvice() = default;
95 
96  void recordInliningImpl() override;
98  void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
99  void recordUnattemptedInliningImpl() override;
100 
101  Function *getCaller() const { return Caller; }
102  Function *getCallee() const { return Callee; }
103 
104  const int64_t CallerIRSize;
105  const int64_t CalleeIRSize;
106  const int64_t CallerAndCalleeEdges;
108 
109 private:
110  void reportContextForRemark(DiagnosticInfoOptimizationBase &OR);
111  MLInlineAdvisor *getAdvisor() const {
112  return static_cast<MLInlineAdvisor *>(Advisor);
113  };
114  // Make a copy of the FPI of the caller right before inlining. If inlining
115  // fails, we can just update the cache with that value.
116  const FunctionPropertiesInfo PreInlineCallerFPI;
118 };
119 
120 } // namespace llvm
121 
122 #endif // LLVM_ANALYSIS_MLINLINEADVISOR_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::ISD::OR
@ OR
Definition: ISDOpcodes.h:667
llvm::MLInlineAdvisor::getLocalCalls
int64_t getLocalCalls(Function &F)
Definition: MLInlineAdvisor.cpp:219
llvm::InlineAdvisor::M
Module & M
Definition: InlineAdvisor.h:208
llvm::Function
Definition: Function.h:60
MLModelRunner.h
llvm::InlineAdvice::Caller
Function *const Caller
Caller and Callee are pre-inlining.
Definition: InlineAdvisor.h:122
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::Optional
Definition: APInt.h:33
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
llvm::MLInlineAdvisor::ModelRunner
std::unique_ptr< MLModelRunner > ModelRunner
Definition: MLInlineAdvisor.h:64
llvm::MLInlineAdvice::CalleeIRSize
const int64_t CalleeIRSize
Definition: MLInlineAdvisor.h:105
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:414
llvm::MLInlineAdvice::recordInliningImpl
void recordInliningImpl() override
Definition: MLInlineAdvisor.cpp:454
llvm::MLInlineAdvice::~MLInlineAdvice
virtual ~MLInlineAdvice()=default
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::MLInlineAdvice::getCallee
Function * getCallee() const
Definition: MLInlineAdvisor.h:102
llvm::LazyCallGraph::SCC
An SCC of the call graph.
Definition: LazyCallGraph.h:419
llvm::MLInlineAdvice::recordInliningWithCalleeDeletedImpl
void recordInliningWithCalleeDeletedImpl() override
Definition: MLInlineAdvisor.cpp:463
llvm::InlineAdvice::ORE
OptimizationRemarkEmitter & ORE
Definition: InlineAdvisor.h:130
MAM
ModuleAnalysisManager MAM
Definition: PassBuilderBindings.cpp:61
llvm::MLInlineAdvice::recordUnsuccessfulInliningImpl
void recordUnsuccessfulInliningImpl(const InlineResult &Result) override
Definition: MLInlineAdvisor.cpp:473
InlineAdvisor.h
llvm::MLInlineAdvice::getCaller
Function * getCaller() const
Definition: MLInlineAdvisor.h:101
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
LazyCallGraph.h
llvm::MLInlineAdvisor::getMandatoryAdviceImpl
virtual std::unique_ptr< MLInlineAdvice > getMandatoryAdviceImpl(CallBase &CB)
Definition: MLInlineAdvisor.cpp:409
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::InlineAdvice
Capture state between an inlining decision having had been made, and its impact being observable.
Definition: InlineAdvisor.h:74
llvm::MLInlineAdvisor::MLInlineAdvisor
MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM, std::unique_ptr< MLModelRunner > ModelRunner)
Definition: MLInlineAdvisor.cpp:89
llvm::MLInlineAdvisor::getModelRunner
const MLModelRunner & getModelRunner() const
Definition: MLInlineAdvisor.h:45
llvm::MLInlineAdvisor::isForcedToStop
bool isForcedToStop() const
Definition: MLInlineAdvisor.h:43
llvm::DenseMap
Definition: DenseMap.h:716
llvm::MLInlineAdvisor::getMandatoryAdvice
std::unique_ptr< InlineAdvice > getMandatoryAdvice(CallBase &CB, bool Advice) override
Definition: MLInlineAdvisor.cpp:393
llvm::MLInlineAdvice::CallerAndCalleeEdges
const int64_t CallerAndCalleeEdges
Definition: MLInlineAdvisor.h:106
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MLInlineAdvisor::getCachedFPI
FunctionPropertiesInfo & getCachedFPI(Function &) const
Definition: MLInlineAdvisor.cpp:273
llvm::AMDGPU::CPol::SCC
@ SCC
Definition: SIDefines.h:307
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::MLInlineAdvisor::getInitialFunctionLevel
unsigned getInitialFunctionLevel(const Function &F) const
Definition: MLInlineAdvisor.cpp:138
llvm::InlineAdvisor
Interface for deciding whether to inline a call site or not.
Definition: InlineAdvisor.h:163
llvm::MLInlineAdvice::MLInlineAdvice
MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool Recommendation)
Definition: MLInlineAdvisor.cpp:425
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
FunctionPropertiesAnalysis.h
llvm::MLInlineAdvice::updateCachedCallerFPI
void updateCachedCallerFPI(FunctionAnalysisManager &FAM) const
Definition: MLInlineAdvisor.cpp:450
llvm::MLInlineAdvisor::onPassExit
void onPassExit(LazyCallGraph::SCC *SCC) override
This must be called when the Inliner pass is exited, as function passes may be run subsequently.
Definition: MLInlineAdvisor.cpp:189
PassManager.h
llvm::MLInlineAdvice::CallerIRSize
const int64_t CallerIRSize
Definition: MLInlineAdvisor.h:104
llvm::MLInlineAdvisor::getIRSize
int64_t getIRSize(Function &F) const
Definition: MLInlineAdvisor.h:37
llvm::MLInlineAdvisor::onSuccessfulInlining
void onSuccessfulInlining(const MLInlineAdvice &Advice, bool CalleeWasDeleted)
Definition: MLInlineAdvisor.cpp:228
llvm::MLInlineAdvisor
Definition: MLInlineAdvisor.h:27
llvm::FunctionPropertiesInfo::TotalInstructionCount
int64_t TotalInstructionCount
Definition: FunctionPropertiesAnalysis.h:78
llvm::MLInlineAdvisor::getAdviceImpl
std::unique_ptr< InlineAdvice > getAdviceImpl(CallBase &CB) override
Definition: MLInlineAdvisor.cpp:282
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::MLInlineAdvisor::getAdviceFromModel
virtual std::unique_ptr< MLInlineAdvice > getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE)
Definition: MLInlineAdvisor.cpp:379
llvm::LazyCallGraph
A lazily constructed view of the call graph of a module.
Definition: LazyCallGraph.h:110
llvm::FunctionPropertiesInfo
Definition: FunctionPropertiesAnalysis.h:26
llvm::MLInlineAdvice
InlineAdvice that tracks changes post inlining.
Definition: MLInlineAdvisor.h:90
llvm::InlineAdvice::Advisor
InlineAdvisor *const Advisor
Definition: InlineAdvisor.h:120
llvm::InlineResult
InlineResult is basically true or false.
Definition: InlineCost.h:164
llvm::InlineAdvice::Callee
Function *const Callee
Definition: InlineAdvisor.h:123
llvm::MLInlineAdvisor::~MLInlineAdvisor
virtual ~MLInlineAdvisor()=default
llvm::MLModelRunner
MLModelRunner interface: abstraction of a mechanism for evaluating a tensorflow "saved model".
Definition: MLModelRunner.h:24
llvm::MLInlineAdvice::recordUnattemptedInliningImpl
void recordUnattemptedInliningImpl() override
Definition: MLInlineAdvisor.cpp:483
llvm::MLInlineAdvisor::onPassEntry
void onPassEntry(LazyCallGraph::SCC *SCC) override
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
Definition: MLInlineAdvisor.cpp:142