LLVM 19.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
17#include "llvm/IR/PassManager.h"
18
19#include <deque>
20#include <map>
21#include <memory>
22#include <optional>
23
24namespace llvm {
25class DiagnosticInfoOptimizationBase;
26class Module;
27class MLInlineAdvice;
28
30public:
32 std::unique_ptr<MLModelRunner> ModelRunner,
33 std::function<bool(CallBase &)> GetDefaultAdvice);
34
35 virtual ~MLInlineAdvisor() = default;
36
37 void onPassEntry(LazyCallGraph::SCC *SCC) override;
38 void onPassExit(LazyCallGraph::SCC *SCC) override;
39
40 int64_t getIRSize(Function &F) const {
42 }
43 void onSuccessfulInlining(const MLInlineAdvice &Advice,
44 bool CalleeWasDeleted);
45
46 bool isForcedToStop() const { return ForceStop; }
47 int64_t getLocalCalls(Function &F);
48 const MLModelRunner &getModelRunner() const { return *ModelRunner.get(); }
50
51protected:
52 std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
53
54 std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
55 bool Advice) override;
56
57 virtual std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB);
58
59 virtual std::unique_ptr<MLInlineAdvice>
61
62 // Get the initial 'level' of the function, or 0 if the function has been
63 // introduced afterwards.
64 // TODO: should we keep this updated?
65 unsigned getInitialFunctionLevel(const Function &F) const;
66
67 std::unique_ptr<MLModelRunner> ModelRunner;
68 std::function<bool(CallBase &)> GetDefaultAdvice;
69
70private:
71 int64_t getModuleIRSize() const;
72 std::unique_ptr<InlineAdvice>
73 getSkipAdviceIfUnreachableCallsite(CallBase &CB);
74 void print(raw_ostream &OS) const override;
75
76 // Using std::map to benefit from its iterator / reference non-invalidating
77 // semantics, which make it easy to use `getCachedFPI` results from multiple
78 // calls without needing to copy to avoid invalidation effects.
79 mutable std::map<const Function *, FunctionPropertiesInfo> FPICache;
80
81 LazyCallGraph &CG;
82
83 int64_t NodeCount = 0;
84 int64_t EdgeCount = 0;
85 int64_t EdgesOfLastSeenNodes = 0;
86
87 std::map<const LazyCallGraph::Node *, unsigned> FunctionLevels;
88 const int32_t InitialIRSize = 0;
89 int32_t CurrentIRSize = 0;
92 bool ForceStop = false;
94};
95
96/// InlineAdvice that tracks changes post inlining. For that reason, it only
97/// overrides the "successful inlining" extension points.
99public:
101 OptimizationRemarkEmitter &ORE, bool Recommendation);
102 virtual ~MLInlineAdvice() = default;
103
104 void recordInliningImpl() override;
106 void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
107 void recordUnattemptedInliningImpl() override;
108
109 Function *getCaller() const { return Caller; }
110 Function *getCallee() const { return Callee; }
111
112 const int64_t CallerIRSize;
113 const int64_t CalleeIRSize;
114 const int64_t CallerAndCalleeEdges;
116
117private:
118 void reportContextForRemark(DiagnosticInfoOptimizationBase &OR);
119 MLInlineAdvisor *getAdvisor() const {
120 return static_cast<MLInlineAdvisor *>(Advisor);
121 };
122 // Make a copy of the FPI of the caller right before inlining. If inlining
123 // fails, we can just update the cache with that value.
124 const FunctionPropertiesInfo PreInlineCallerFPI;
125 std::optional<FunctionPropertiesUpdater> FPU;
126};
127
128} // namespace llvm
129
130#endif // LLVM_ANALYSIS_MLINLINEADVISOR_H
Implements a lazy call graph analysis and related passes for the new pass manager.
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This header defines various interfaces for pass management in LLVM.
raw_pwrite_stream & OS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:242
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Capture state between an inlining decision having had been made, and its impact being observable.
Definition: InlineAdvisor.h:75
Function *const Callee
Function *const Caller
Caller and Callee are pre-inlining.
OptimizationRemarkEmitter & ORE
InlineAdvisor *const Advisor
Interface for deciding whether to inline a call site or not.
InlineResult is basically true or false.
Definition: InlineCost.h:180
An SCC of the call graph.
A lazily constructed view of the call graph of a module.
InlineAdvice that tracks changes post inlining.
void updateCachedCallerFPI(FunctionAnalysisManager &FAM) const
virtual ~MLInlineAdvice()=default
const int64_t CallerIRSize
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
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 onSuccessfulInlining(const MLInlineAdvice &Advice, bool CalleeWasDeleted)
virtual std::unique_ptr< MLInlineAdvice > getMandatoryAdviceImpl(CallBase &CB)
void onPassEntry(LazyCallGraph::SCC *SCC) override
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
int64_t getLocalCalls(Function &F)
virtual std::unique_ptr< MLInlineAdvice > getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE)
int64_t getIRSize(Function &F) const
std::function< bool(CallBase &)> GetDefaultAdvice
std::unique_ptr< InlineAdvice > getAdviceImpl(CallBase &CB) override
std::unique_ptr< InlineAdvice > getMandatoryAdvice(CallBase &CB, bool Advice) override
virtual ~MLInlineAdvisor()=default
bool isForcedToStop() const
unsigned getInitialFunctionLevel(const Function &F) const
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
Definition: MLModelRunner.h:26
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
The optimization diagnostic interface.
Analysis providing profile information.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18