LLVM  14.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 
15 #include "llvm/IR/PassManager.h"
16 
17 #include <memory>
18 #include <unordered_map>
19 
20 namespace llvm {
21 class Module;
22 class MLInlineAdvice;
23 
25 public:
27  std::unique_ptr<MLModelRunner> ModelRunner);
28 
29  virtual ~MLInlineAdvisor() = default;
30 
31  void onPassEntry() override;
32 
33  int64_t getIRSize(const Function &F) const { return F.getInstructionCount(); }
34  void onSuccessfulInlining(const MLInlineAdvice &Advice,
35  bool CalleeWasDeleted);
36 
37  bool isForcedToStop() const { return ForceStop; }
38  int64_t getLocalCalls(Function &F);
39  const MLModelRunner &getModelRunner() const { return *ModelRunner.get(); }
40  void onModuleInvalidated() override { Invalid = true; }
41 
42 protected:
43  std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
44 
45  std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
46  bool Advice) override;
47 
48  virtual std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB);
49 
50  virtual std::unique_ptr<MLInlineAdvice>
52 
53  // Get the initial 'level' of the function, or 0 if the function has been
54  // introduced afterwards.
55  // TODO: should we keep this updated?
56  unsigned getInitialFunctionLevel(const Function &F) const;
57 
58  std::unique_ptr<MLModelRunner> ModelRunner;
59 
60 private:
61  int64_t getModuleIRSize() const;
62 
63  bool Invalid = true;
64  LazyCallGraph &CG;
65 
66  int64_t NodeCount = 0;
67  int64_t EdgeCount = 0;
68  std::map<const LazyCallGraph::Node *, unsigned> FunctionLevels;
69  const int32_t InitialIRSize = 0;
70  int32_t CurrentIRSize = 0;
71 
72  bool ForceStop = false;
73 };
74 
75 /// InlineAdvice that tracks changes post inlining. For that reason, it only
76 /// overrides the "successful inlining" extension points.
77 class MLInlineAdvice : public InlineAdvice {
78 public:
80  OptimizationRemarkEmitter &ORE, bool Recommendation)
81  : InlineAdvice(Advisor, CB, ORE, Recommendation),
82  CallerIRSize(Advisor->isForcedToStop() ? 0
83  : Advisor->getIRSize(*Caller)),
84  CalleeIRSize(Advisor->isForcedToStop() ? 0
85  : Advisor->getIRSize(*Callee)),
86  CallerAndCalleeEdges(Advisor->isForcedToStop()
87  ? 0
88  : (Advisor->getLocalCalls(*Caller) +
89  Advisor->getLocalCalls(*Callee))) {}
90  virtual ~MLInlineAdvice() = default;
91 
92  void recordInliningImpl() override;
94  void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
95  void recordUnattemptedInliningImpl() override;
96 
97  Function *getCaller() const { return Caller; }
98  Function *getCallee() const { return Callee; }
99 
100  const int64_t CallerIRSize;
101  const int64_t CalleeIRSize;
102  const int64_t CallerAndCalleeEdges;
103 
104 private:
105  void reportContextForRemark(DiagnosticInfoOptimizationBase &OR);
106 
107  MLInlineAdvisor *getAdvisor() const {
108  return static_cast<MLInlineAdvisor *>(Advisor);
109  };
110 };
111 
112 } // namespace llvm
113 
114 #endif // LLVM_ANALYSIS_MLINLINEADVISOR_H
llvm::MLInlineAdvisor::getLocalCalls
int64_t getLocalCalls(Function &F)
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::ISD::OR
@ OR
Definition: ISDOpcodes.h:633
llvm::InlineAdvisor::M
Module & M
Definition: InlineAdvisor.h:179
llvm::Function
Definition: Function.h:62
llvm::MLInlineAdvisor::onSuccessfulInlining
void onSuccessfulInlining(const MLInlineAdvice &Advice, bool CalleeWasDeleted)
MLModelRunner.h
llvm::InlineAdvice::Caller
Function *const Caller
Caller and Callee are pre-inlining.
Definition: InlineAdvisor.h:98
llvm::MLInlineAdvisor::ModelRunner
std::unique_ptr< MLModelRunner > ModelRunner
Definition: MLInlineAdvisor.h:58
llvm::MLInlineAdvice::CalleeIRSize
const int64_t CalleeIRSize
Definition: MLInlineAdvisor.h:101
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:409
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:98
llvm::MLInlineAdvice::recordUnattemptedInliningImpl
void recordUnattemptedInliningImpl() override
llvm::MLInlineAdvisor::getMandatoryAdvice
std::unique_ptr< InlineAdvice > getMandatoryAdvice(CallBase &CB, bool Advice) override
llvm::MLInlineAdvisor::onPassEntry
void onPassEntry() override
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
llvm::InlineAdvice::ORE
OptimizationRemarkEmitter & ORE
Definition: InlineAdvisor.h:106
MAM
ModuleAnalysisManager MAM
Definition: PassBuilderBindings.cpp:61
InlineAdvisor.h
llvm::MLInlineAdvisor::getIRSize
int64_t getIRSize(const Function &F) const
Definition: MLInlineAdvisor.h:33
llvm::MLInlineAdvice::getCaller
Function * getCaller() const
Definition: MLInlineAdvisor.h:97
LazyCallGraph.h
llvm::MLInlineAdvice::recordUnsuccessfulInliningImpl
void recordUnsuccessfulInliningImpl(const InlineResult &Result) override
llvm::InlineAdvice
Capture state between an inlining decision having had been made, and its impact being observable.
Definition: InlineAdvisor.h:50
llvm::MLInlineAdvice::recordInliningWithCalleeDeletedImpl
void recordInliningWithCalleeDeletedImpl() override
llvm::MLInlineAdvisor::getModelRunner
const MLModelRunner & getModelRunner() const
Definition: MLInlineAdvisor.h:39
llvm::MLInlineAdvisor::isForcedToStop
bool isForcedToStop() const
Definition: MLInlineAdvisor.h:37
llvm::MLInlineAdvisor::MLInlineAdvisor
MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM, std::unique_ptr< MLModelRunner > ModelRunner)
llvm::MLInlineAdvice::CallerAndCalleeEdges
const int64_t CallerAndCalleeEdges
Definition: MLInlineAdvisor.h:102
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MLInlineAdvisor::onModuleInvalidated
void onModuleInvalidated() override
Called when the module is invalidated.
Definition: MLInlineAdvisor.h:40
llvm::MLInlineAdvice::MLInlineAdvice
MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool Recommendation)
Definition: MLInlineAdvisor.h:79
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
llvm::MLInlineAdvisor::getInitialFunctionLevel
unsigned getInitialFunctionLevel(const Function &F) const
llvm::InlineAdvisor
Interface for deciding whether to inline a call site or not.
Definition: InlineAdvisor.h:139
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
llvm::MLInlineAdvisor::getAdviceImpl
std::unique_ptr< InlineAdvice > getAdviceImpl(CallBase &CB) override
llvm::MLInlineAdvice::recordInliningImpl
void recordInliningImpl() override
PassManager.h
llvm::MLInlineAdvice::CallerIRSize
const int64_t CallerIRSize
Definition: MLInlineAdvisor.h:100
llvm::MLInlineAdvisor::getMandatoryAdviceImpl
virtual std::unique_ptr< MLInlineAdvice > getMandatoryAdviceImpl(CallBase &CB)
llvm::MLInlineAdvisor
Definition: MLInlineAdvisor.h:24
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1176
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::MLInlineAdvisor::getAdviceFromModel
virtual std::unique_ptr< MLInlineAdvice > getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE)
llvm::LazyCallGraph
A lazily constructed view of the call graph of a module.
Definition: LazyCallGraph.h:113
llvm::MLInlineAdvice
InlineAdvice that tracks changes post inlining.
Definition: MLInlineAdvisor.h:77
llvm::InlineAdvice::Advisor
InlineAdvisor *const Advisor
Definition: InlineAdvisor.h:96
llvm::InlineResult
InlineResult is basically true or false.
Definition: InlineCost.h:158
llvm::InlineAdvice::Callee
Function *const Callee
Definition: InlineAdvisor.h:99
llvm::MLInlineAdvisor::~MLInlineAdvisor
virtual ~MLInlineAdvisor()=default
llvm::MLModelRunner
MLModelRunner interface: abstraction of a mechanism for evaluating a tensorflow "saved model".
Definition: MLModelRunner.h:23