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  CallGraph *callGraph() const { return CG.get(); }
30  virtual ~MLInlineAdvisor() = default;
31 
32  void onPassEntry() override;
33 
34  int64_t getIRSize(const Function &F) const { return F.getInstructionCount(); }
35  void onSuccessfulInlining(const MLInlineAdvice &Advice,
36  bool CalleeWasDeleted);
37 
38  bool isForcedToStop() const { return ForceStop; }
39  int64_t getLocalCalls(Function &F);
40  const MLModelRunner &getModelRunner() const { return *ModelRunner.get(); }
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  std::unique_ptr<MLModelRunner> ModelRunner;
54 
55 private:
56  int64_t getModuleIRSize() const;
57 
58  std::unique_ptr<CallGraph> CG;
59 
60  int64_t NodeCount = 0;
61  int64_t EdgeCount = 0;
62  std::map<const Function *, unsigned> FunctionLevels;
63  const int32_t InitialIRSize = 0;
64  int32_t CurrentIRSize = 0;
65 
66  bool ForceStop = false;
67 };
68 
69 /// InlineAdvice that tracks changes post inlining. For that reason, it only
70 /// overrides the "successful inlining" extension points.
71 class MLInlineAdvice : public InlineAdvice {
72 public:
74  OptimizationRemarkEmitter &ORE, bool Recommendation)
75  : InlineAdvice(Advisor, CB, ORE, Recommendation),
76  CallerIRSize(Advisor->isForcedToStop() ? 0
77  : Advisor->getIRSize(*Caller)),
78  CalleeIRSize(Advisor->isForcedToStop() ? 0
79  : Advisor->getIRSize(*Callee)),
80  CallerAndCalleeEdges(Advisor->isForcedToStop()
81  ? 0
82  : (Advisor->getLocalCalls(*Caller) +
83  Advisor->getLocalCalls(*Callee))) {}
84  virtual ~MLInlineAdvice() = default;
85 
86  void recordInliningImpl() override;
88  void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
89  void recordUnattemptedInliningImpl() override;
90 
91  Function *getCaller() const { return Caller; }
92  Function *getCallee() const { return Callee; }
93 
94  const int64_t CallerIRSize;
95  const int64_t CalleeIRSize;
96  const int64_t CallerAndCalleeEdges;
97 
98 private:
99  void reportContextForRemark(DiagnosticInfoOptimizationBase &OR);
100 
101  MLInlineAdvisor *getAdvisor() const {
102  return static_cast<MLInlineAdvisor *>(Advisor);
103  };
104 };
105 
106 } // namespace llvm
107 
108 #endif // LLVM_ANALYSIS_MLINLINEADVISOR_H
llvm::MLInlineAdvisor::getLocalCalls
int64_t getLocalCalls(Function &F)
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::ISD::OR
@ OR
Definition: ISDOpcodes.h:633
llvm::InlineAdvisor::M
Module & M
Definition: InlineAdvisor.h:173
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::CallGraph
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:73
llvm::MLInlineAdvisor::ModelRunner
std::unique_ptr< MLModelRunner > ModelRunner
Definition: MLInlineAdvisor.h:53
llvm::MLInlineAdvice::CalleeIRSize
const int64_t CalleeIRSize
Definition: MLInlineAdvisor.h:95
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
llvm::MLInlineAdvisor::callGraph
CallGraph * callGraph() const
Definition: MLInlineAdvisor.h:29
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::MLInlineAdvice::getCallee
Function * getCallee() const
Definition: MLInlineAdvisor.h:92
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:34
llvm::MLInlineAdvice::getCaller
Function * getCaller() const
Definition: MLInlineAdvisor.h:91
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:52
llvm::MLInlineAdvice::recordInliningWithCalleeDeletedImpl
void recordInliningWithCalleeDeletedImpl() override
llvm::MLInlineAdvisor::getModelRunner
const MLModelRunner & getModelRunner() const
Definition: MLInlineAdvisor.h:40
llvm::MLInlineAdvisor::isForcedToStop
bool isForcedToStop() const
Definition: MLInlineAdvisor.h:38
llvm::MLInlineAdvisor::MLInlineAdvisor
MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM, std::unique_ptr< MLModelRunner > ModelRunner)
llvm::MLInlineAdvice::CallerAndCalleeEdges
const int64_t CallerAndCalleeEdges
Definition: MLInlineAdvisor.h:96
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::MLInlineAdvice::MLInlineAdvice
MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool Recommendation)
Definition: MLInlineAdvisor.h:73
llvm::OptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: OptimizationRemarkEmitter.h:33
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:94
CallGraph.h
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:1161
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::MLInlineAdvice
InlineAdvice that tracks changes post inlining.
Definition: MLInlineAdvisor.h:71
llvm::InlineAdvice::Advisor
InlineAdvisor *const Advisor
Definition: InlineAdvisor.h:96
llvm::InlineResult
InlineResult is basically true or false.
Definition: InlineCost.h:159
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:21