LLVM 24.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 <map>
19#include <memory>
20#include <optional>
21
22namespace llvm {
23struct AnalysisOptions;
25class Module;
26class MLInlineAdvice;
28
30public:
32 std::function<std::unique_ptr<MLModelRunner>(
33 const std::vector<TensorSpec> &)>
34 GetModelRunner,
35 std::function<bool(CallBase &)> GetDefaultAdvice);
36
37 ~MLInlineAdvisor() override = default;
38
39 void onPassEntry(LazyCallGraph::SCC *SCC) override;
40 void onPassExit(LazyCallGraph::SCC *SCC) override;
41
42 int64_t getIRSize(Function &F) const {
43 return getCachedFPI(F).TotalInstructionCount;
44 }
45 void onSuccessfulInlining(const MLInlineAdvice &Advice,
46 bool CalleeWasDeleted);
47
48 bool isForcedToStop() const { return ForceStop; }
49 int64_t getLocalCalls(Function &F);
50 const MLModelRunner &getModelRunner() const { return *ModelRunner; }
51 FunctionPropertiesInfo &getCachedFPI(Function &) const;
52 const std::vector<TensorSpec> &getFeatureMap() const { return FeatureMap; };
53 static const std::vector<TensorSpec> &getInitialFeatureMap();
54
55protected:
56 std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
57
58 std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
59 bool Advice) override;
60
61 virtual std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB);
62
63 virtual std::unique_ptr<MLInlineAdvice>
64 getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE);
65
66 // Get the initial 'level' of the function, or 0 if the function has been
67 // introduced afterwards.
68 // TODO: should we keep this updated?
69 unsigned getInitialFunctionLevel(const Function &F) const;
70
71 const AnalysisOptions &Opts;
72 std::unique_ptr<MLModelRunner> ModelRunner;
73 std::function<bool(CallBase &)> GetDefaultAdvice;
74 std::vector<TensorSpec> FeatureMap;
75
76private:
77 int64_t getModuleIRSize() const;
78 std::unique_ptr<InlineAdvice>
79 getSkipAdviceIfUnreachableCallsite(CallBase &CB);
80 void print(raw_ostream &OS) const override;
81
82 // Using std::map to benefit from its iterator / reference non-invalidating
83 // semantics, which make it easy to use `getCachedFPI` results from multiple
84 // calls without needing to copy to avoid invalidation effects.
85 mutable std::map<const Function *, FunctionPropertiesInfo> FPICache;
86
87 LazyCallGraph &CG;
88
89 int64_t NodeCount = 0;
90 int64_t EdgeCount = 0;
91 int64_t EdgesOfLastSeenNodes = 0;
92 const bool UseIR2Vec;
93
94 std::map<const LazyCallGraph::Node *, unsigned> FunctionLevels;
95 const int32_t InitialIRSize = 0;
96 int32_t CurrentIRSize = 0;
99 DenseSet<Function *> DeadFunctions;
100 bool ForceStop = false;
102};
103
104/// InlineAdvice that tracks changes post inlining. For that reason, it only
105/// overrides the "successful inlining" extension points.
107public:
109 OptimizationRemarkEmitter &ORE, bool Recommendation);
110 ~MLInlineAdvice() override = default;
111
112 void recordInliningImpl() override;
114 void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
115 void recordUnattemptedInliningImpl() override;
116
117 Function *getCaller() const { return Caller; }
118 Function *getCallee() const { return Callee; }
119
120 const int64_t CallerIRSize;
121 const int64_t CalleeIRSize;
122 const int64_t CallerAndCalleeEdges;
124
125private:
126 void reportContextForRemark(DiagnosticInfoOptimizationBase &OR);
127 MLInlineAdvisor *getAdvisor() const {
128 return static_cast<MLInlineAdvisor *>(Advisor);
129 };
130 // Make a copy of the FPI of the caller right before inlining. If inlining
131 // fails, we can just update the cache with that value.
132 const FunctionPropertiesInfo PreInlineCallerFPI;
133 std::optional<FunctionPropertiesUpdater> FPU;
134};
135
136} // namespace llvm
137
138#endif // LLVM_ANALYSIS_MLINLINEADVISOR_H
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition Compiler.h:211
This header defines various interfaces for pass management in LLVM.
Implements a lazy call graph analysis and related passes for the new pass manager.
#define F(x, y, z)
Definition MD5.cpp:54
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Function *const Callee
Function *const Caller
Caller and Callee are pre-inlining.
OptimizationRemarkEmitter & ORE
InlineAdvisor *const Advisor
LLVM_ABI InlineAdvice(InlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool IsInliningRecommended)
InlineAdvisor(InlineAdvisor &&)=delete
InlineResult is basically true or false.
Definition InlineCost.h:181
An SCC of the call graph.
A lazily constructed view of the call graph of a module.
InlineAdvice that tracks changes post inlining.
~MLInlineAdvice() override=default
void updateCachedCallerFPI(FunctionAnalysisManager &FAM) const
const int64_t CallerIRSize
MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool Recommendation)
const int64_t CalleeIRSize
void recordInliningImpl() override
Function * getCaller() const
const int64_t CallerAndCalleeEdges
void recordUnsuccessfulInliningImpl(const InlineResult &Result) override
Function * getCallee() const
void recordInliningWithCalleeDeletedImpl() override
void recordUnattemptedInliningImpl() override
const std::vector< TensorSpec > & getFeatureMap() const
const AnalysisOptions & Opts
std::unique_ptr< MLModelRunner > ModelRunner
FunctionPropertiesInfo & getCachedFPI(Function &) const
void onPassExit(LazyCallGraph::SCC *SCC) override
This must be called when the Inliner pass is exited, as function passes may be run subsequently.
const MLModelRunner & getModelRunner() const
void onPassEntry(LazyCallGraph::SCC *SCC) override
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
~MLInlineAdvisor() override=default
MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM, std::function< std::unique_ptr< MLModelRunner >(const std::vector< TensorSpec > &)> GetModelRunner, std::function< bool(CallBase &)> GetDefaultAdvice)
std::vector< TensorSpec > FeatureMap
int64_t getIRSize(Function &F) const
std::function< bool(CallBase &)> GetDefaultAdvice
bool isForcedToStop() const
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
The optimization diagnostic interface.
Analysis providing profile information.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39