LLVM  12.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  std::unique_ptr<InlineAdvice> getAdvice(CallBase &CB) override;
35 
36  int64_t getIRSize(const Function &F) const { return F.getInstructionCount(); }
37  void onSuccessfulInlining(const MLInlineAdvice &Advice,
38  bool CalleeWasDeleted);
39 
40  bool isForcedToStop() const { return ForceStop; }
41  int64_t getLocalCalls(Function &F);
42  const MLModelRunner &getModelRunner() const { return *ModelRunner.get(); }
43 
44 protected:
45  virtual std::unique_ptr<MLInlineAdvice>
47 
48  virtual std::unique_ptr<MLInlineAdvice>
50 
52  std::unique_ptr<MLModelRunner> ModelRunner;
53 
54 private:
55  int64_t getModuleIRSize() const;
56 
57  std::unique_ptr<CallGraph> CG;
58 
59  int64_t NodeCount = 0;
60  int64_t EdgeCount = 0;
61  std::map<const Function *, unsigned> FunctionLevels;
62  const int32_t InitialIRSize = 0;
63  int32_t CurrentIRSize = 0;
64 
65  bool ForceStop = false;
66 };
67 
68 /// InlineAdvice that tracks changes post inlining. For that reason, it only
69 /// overrides the "successful inlining" extension points.
70 class MLInlineAdvice : public InlineAdvice {
71 public:
73  OptimizationRemarkEmitter &ORE, bool Recommendation)
74  : InlineAdvice(Advisor, CB, ORE, Recommendation),
75  CallerIRSize(Advisor->isForcedToStop() ? 0
76  : Advisor->getIRSize(*Caller)),
77  CalleeIRSize(Advisor->isForcedToStop() ? 0
78  : Advisor->getIRSize(*Callee)),
79  CallerAndCalleeEdges(Advisor->isForcedToStop()
80  ? 0
81  : (Advisor->getLocalCalls(*Caller) +
82  Advisor->getLocalCalls(*Callee))) {}
83  virtual ~MLInlineAdvice() = default;
84 
85  void recordInliningImpl() override;
87  void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
88  void recordUnattemptedInliningImpl() override;
89 
90  Function *getCaller() const { return Caller; }
91  Function *getCallee() const { return Callee; }
92 
93  const int64_t CallerIRSize;
94  const int64_t CalleeIRSize;
95  const int64_t CallerAndCalleeEdges;
96 
97 private:
98  void reportContextForRemark(DiagnosticInfoOptimizationBase &OR);
99 
100  MLInlineAdvisor *getAdvisor() const {
101  return static_cast<MLInlineAdvisor *>(Advisor);
102  };
103 };
104 
105 } // namespace llvm
106 
107 #endif // LLVM_ANALYSIS_MLINLINEADVISOR_H
Capture state between an inlining decision having had been made, and its impact being observable.
Definition: InlineAdvisor.h:55
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
void recordInliningImpl() override
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1164
F(f)
const int64_t CalleeIRSize
Interface for deciding whether to inline a call site or not.
const MLModelRunner & getModelRunner() const
OptimizationRemarkEmitter & ORE
InlineResult is basically true or false.
Definition: InlineCost.h:134
Function * getCallee() const
Function *const Callee
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
virtual ~MLInlineAdvisor()=default
int64_t getIRSize(const Function &F) const
int64_t getLocalCalls(Function &F)
Function *const Caller
Caller and Callee are pre-inlining.
InlineAdvice that tracks changes post inlining.
void recordUnattemptedInliningImpl() override
bool isForcedToStop() const
Machine Check Debug Module
Function * getCaller() const
virtual std::unique_ptr< MLInlineAdvice > getMandatoryAdvice(CallBase &CB, OptimizationRemarkEmitter &ORE)
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
const int64_t CallerAndCalleeEdges
virtual std::unique_ptr< MLInlineAdvice > getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE)
CallGraph * callGraph() const
void recordUnsuccessfulInliningImpl(const InlineResult &Result) override
virtual ~MLInlineAdvice()=default
void onSuccessfulInlining(const MLInlineAdvice &Advice, bool CalleeWasDeleted)
void recordInliningWithCalleeDeletedImpl() override
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:73
std::unique_ptr< InlineAdvice > getAdvice(CallBase &CB) override
Get an InlineAdvice containing a recommendation on whether to inline or not.
const int64_t CallerIRSize
MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool Recommendation)
InlineAdvisor *const Advisor
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
void onPassEntry() override
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
MLModelRunner interface: abstraction of a mechanism for evaluating a tensorflow "saved model".
Definition: MLModelRunner.h:21
The optimization diagnostic interface.
std::unique_ptr< MLModelRunner > ModelRunner
MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM, std::unique_ptr< MLModelRunner > ModelRunner)