1097a140dSpatrick //===- MLInlineAdvisor.cpp - machine learned InlineAdvisor ----------------===//
2097a140dSpatrick //
3097a140dSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4097a140dSpatrick // See https://llvm.org/LICENSE.txt for license information.
5097a140dSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6097a140dSpatrick //
7097a140dSpatrick //===----------------------------------------------------------------------===//
8097a140dSpatrick //
9097a140dSpatrick // This file implements the interface between the inliner and a learned model.
10097a140dSpatrick // It delegates model evaluation to either the AOT compiled model (the
11097a140dSpatrick // 'release' mode) or a runtime-loaded model (the 'development' case).
12097a140dSpatrick //
13097a140dSpatrick //===----------------------------------------------------------------------===//
14*d415bd75Srobert #include "llvm/Analysis/MLInlineAdvisor.h"
15097a140dSpatrick #include "llvm/ADT/SCCIterator.h"
16*d415bd75Srobert #include "llvm/Analysis/AssumptionCache.h"
17097a140dSpatrick #include "llvm/Analysis/CallGraph.h"
1873471bf0Spatrick #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
19097a140dSpatrick #include "llvm/Analysis/InlineCost.h"
20*d415bd75Srobert #include "llvm/Analysis/InlineModelFeatureMaps.h"
21*d415bd75Srobert #include "llvm/Analysis/LazyCallGraph.h"
22*d415bd75Srobert #include "llvm/Analysis/LoopInfo.h"
23097a140dSpatrick #include "llvm/Analysis/MLModelRunner.h"
24097a140dSpatrick #include "llvm/Analysis/OptimizationRemarkEmitter.h"
25097a140dSpatrick #include "llvm/Analysis/TargetTransformInfo.h"
26*d415bd75Srobert #include "llvm/IR/Dominators.h"
27097a140dSpatrick #include "llvm/IR/InstIterator.h"
28097a140dSpatrick #include "llvm/IR/PassManager.h"
29097a140dSpatrick #include "llvm/Support/CommandLine.h"
30097a140dSpatrick
31097a140dSpatrick using namespace llvm;
32097a140dSpatrick
33*d415bd75Srobert #if defined(LLVM_HAVE_TF_AOT_INLINERSIZEMODEL)
34*d415bd75Srobert #include "llvm/Analysis/ReleaseModeModelRunner.h"
35*d415bd75Srobert // codegen-ed file
36*d415bd75Srobert #include "InlinerSizeModel.h" // NOLINT
37*d415bd75Srobert
38*d415bd75Srobert std::unique_ptr<InlineAdvisor>
getReleaseModeAdvisor(Module & M,ModuleAnalysisManager & MAM)39*d415bd75Srobert llvm::getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM) {
40*d415bd75Srobert auto AOTRunner =
41*d415bd75Srobert std::make_unique<ReleaseModeModelRunner<llvm::InlinerSizeModel>>(
42*d415bd75Srobert M.getContext(), FeatureMap, DecisionName);
43*d415bd75Srobert return std::make_unique<MLInlineAdvisor>(M, MAM, std::move(AOTRunner));
44*d415bd75Srobert }
45*d415bd75Srobert #endif
46*d415bd75Srobert
47097a140dSpatrick #define DEBUG_TYPE "inline-ml"
48097a140dSpatrick
49097a140dSpatrick static cl::opt<float> SizeIncreaseThreshold(
50097a140dSpatrick "ml-advisor-size-increase-threshold", cl::Hidden,
51097a140dSpatrick cl::desc("Maximum factor by which expected native size may increase before "
52097a140dSpatrick "blocking any further inlining."),
53097a140dSpatrick cl::init(2.0));
54097a140dSpatrick
55*d415bd75Srobert static cl::opt<bool> KeepFPICache(
56*d415bd75Srobert "ml-advisor-keep-fpi-cache", cl::Hidden,
57*d415bd75Srobert cl::desc(
58*d415bd75Srobert "For test - keep the ML Inline advisor's FunctionPropertiesInfo cache"),
59*d415bd75Srobert cl::init(false));
60*d415bd75Srobert
6173471bf0Spatrick // clang-format off
62*d415bd75Srobert const std::array<TensorSpec, NumberOfFeatures> llvm::FeatureMap{
63*d415bd75Srobert #define POPULATE_NAMES(_, NAME) TensorSpec::createSpec<int64_t>(NAME, {1} ),
6473471bf0Spatrick // InlineCost features - these must come first
6573471bf0Spatrick INLINE_COST_FEATURE_ITERATOR(POPULATE_NAMES)
6673471bf0Spatrick #undef POPULATE_NAMES
6773471bf0Spatrick
6873471bf0Spatrick // Non-cost features
69*d415bd75Srobert #define POPULATE_NAMES(_, NAME, __) TensorSpec::createSpec<int64_t>(NAME, {1} ),
70097a140dSpatrick INLINE_FEATURE_ITERATOR(POPULATE_NAMES)
71097a140dSpatrick #undef POPULATE_NAMES
72097a140dSpatrick };
7373471bf0Spatrick // clang-format on
74097a140dSpatrick
75097a140dSpatrick const char *const llvm::DecisionName = "inlining_decision";
76097a140dSpatrick const char *const llvm::DefaultDecisionName = "inlining_default";
77097a140dSpatrick const char *const llvm::RewardName = "delta_size";
78097a140dSpatrick
getInlinableCS(Instruction & I)79097a140dSpatrick CallBase *getInlinableCS(Instruction &I) {
80097a140dSpatrick if (auto *CS = dyn_cast<CallBase>(&I))
81097a140dSpatrick if (Function *Callee = CS->getCalledFunction()) {
82097a140dSpatrick if (!Callee->isDeclaration()) {
83097a140dSpatrick return CS;
84097a140dSpatrick }
85097a140dSpatrick }
86097a140dSpatrick return nullptr;
87097a140dSpatrick }
88097a140dSpatrick
MLInlineAdvisor(Module & M,ModuleAnalysisManager & MAM,std::unique_ptr<MLModelRunner> Runner)89097a140dSpatrick MLInlineAdvisor::MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM,
90097a140dSpatrick std::unique_ptr<MLModelRunner> Runner)
91097a140dSpatrick : InlineAdvisor(
9273471bf0Spatrick M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
93*d415bd75Srobert ModelRunner(std::move(Runner)),
94*d415bd75Srobert CG(MAM.getResult<LazyCallGraphAnalysis>(M)),
95097a140dSpatrick InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize) {
96097a140dSpatrick assert(ModelRunner);
97097a140dSpatrick
98097a140dSpatrick // Extract the 'call site height' feature - the position of a call site
99097a140dSpatrick // relative to the farthest statically reachable SCC node. We don't mutate
100097a140dSpatrick // this value while inlining happens. Empirically, this feature proved
101097a140dSpatrick // critical in behavioral cloning - i.e. training a model to mimic the manual
102097a140dSpatrick // heuristic's decisions - and, thus, equally important for training for
103097a140dSpatrick // improvement.
104*d415bd75Srobert CallGraph CGraph(M);
105*d415bd75Srobert for (auto I = scc_begin(&CGraph); !I.isAtEnd(); ++I) {
106097a140dSpatrick const std::vector<CallGraphNode *> &CGNodes = *I;
107097a140dSpatrick unsigned Level = 0;
108097a140dSpatrick for (auto *CGNode : CGNodes) {
109097a140dSpatrick Function *F = CGNode->getFunction();
110097a140dSpatrick if (!F || F->isDeclaration())
111097a140dSpatrick continue;
112097a140dSpatrick for (auto &I : instructions(F)) {
113097a140dSpatrick if (auto *CS = getInlinableCS(I)) {
114097a140dSpatrick auto *Called = CS->getCalledFunction();
115*d415bd75Srobert auto Pos = FunctionLevels.find(&CG.get(*Called));
116097a140dSpatrick // In bottom up traversal, an inlinable callee is either in the
117097a140dSpatrick // same SCC, or to a function in a visited SCC. So not finding its
118097a140dSpatrick // level means we haven't visited it yet, meaning it's in this SCC.
119097a140dSpatrick if (Pos == FunctionLevels.end())
120097a140dSpatrick continue;
121097a140dSpatrick Level = std::max(Level, Pos->second + 1);
122097a140dSpatrick }
123097a140dSpatrick }
124097a140dSpatrick }
125097a140dSpatrick for (auto *CGNode : CGNodes) {
126097a140dSpatrick Function *F = CGNode->getFunction();
127097a140dSpatrick if (F && !F->isDeclaration())
128*d415bd75Srobert FunctionLevels[&CG.get(*F)] = Level;
129097a140dSpatrick }
130097a140dSpatrick }
131*d415bd75Srobert for (auto KVP : FunctionLevels) {
132*d415bd75Srobert AllNodes.insert(KVP.first);
133*d415bd75Srobert EdgeCount += getLocalCalls(KVP.first->getFunction());
134*d415bd75Srobert }
135*d415bd75Srobert NodeCount = AllNodes.size();
136*d415bd75Srobert }
137*d415bd75Srobert
getInitialFunctionLevel(const Function & F) const138*d415bd75Srobert unsigned MLInlineAdvisor::getInitialFunctionLevel(const Function &F) const {
139*d415bd75Srobert return CG.lookup(F) ? FunctionLevels.at(CG.lookup(F)) : 0;
140*d415bd75Srobert }
141*d415bd75Srobert
onPassEntry(LazyCallGraph::SCC * LastSCC)142*d415bd75Srobert void MLInlineAdvisor::onPassEntry(LazyCallGraph::SCC *LastSCC) {
143*d415bd75Srobert if (!LastSCC || ForceStop)
144*d415bd75Srobert return;
145*d415bd75Srobert FPICache.clear();
146*d415bd75Srobert // Function passes executed between InlinerPass runs may have changed the
147*d415bd75Srobert // module-wide features.
148*d415bd75Srobert // The cgscc pass manager rules are such that:
149*d415bd75Srobert // - if a pass leads to merging SCCs, then the pipeline is restarted on the
150*d415bd75Srobert // merged SCC
151*d415bd75Srobert // - if a pass leads to splitting the SCC, then we continue with one of the
152*d415bd75Srobert // splits
153*d415bd75Srobert // This means that the NodesInLastSCC is a superset (not strict) of the nodes
154*d415bd75Srobert // that subsequent passes would have processed
155*d415bd75Srobert // - in addition, if new Nodes were created by a pass (e.g. CoroSplit),
156*d415bd75Srobert // they'd be adjacent to Nodes in the last SCC. So we just need to check the
157*d415bd75Srobert // boundary of Nodes in NodesInLastSCC for Nodes we haven't seen. We don't
158*d415bd75Srobert // care about the nature of the Edge (call or ref).
159*d415bd75Srobert NodeCount -= static_cast<int64_t>(NodesInLastSCC.size());
160*d415bd75Srobert while (!NodesInLastSCC.empty()) {
161*d415bd75Srobert const auto *N = *NodesInLastSCC.begin();
162*d415bd75Srobert NodesInLastSCC.erase(N);
163*d415bd75Srobert // The Function wrapped by N could have been deleted since we last saw it.
164*d415bd75Srobert if (N->isDead()) {
165*d415bd75Srobert assert(!N->getFunction().isDeclaration());
166*d415bd75Srobert continue;
167*d415bd75Srobert }
168*d415bd75Srobert ++NodeCount;
169*d415bd75Srobert EdgeCount += getLocalCalls(N->getFunction());
170*d415bd75Srobert for (const auto &E : *(*N)) {
171*d415bd75Srobert const auto *AdjNode = &E.getNode();
172*d415bd75Srobert assert(!AdjNode->isDead() && !AdjNode->getFunction().isDeclaration());
173*d415bd75Srobert auto I = AllNodes.insert(AdjNode);
174*d415bd75Srobert if (I.second)
175*d415bd75Srobert NodesInLastSCC.insert(AdjNode);
176*d415bd75Srobert }
177097a140dSpatrick }
178097a140dSpatrick
179*d415bd75Srobert EdgeCount -= EdgesOfLastSeenNodes;
180*d415bd75Srobert EdgesOfLastSeenNodes = 0;
181*d415bd75Srobert
182*d415bd75Srobert // (Re)use NodesInLastSCC to remember the nodes in the SCC right now,
183*d415bd75Srobert // in case the SCC is split before onPassExit and some nodes are split out
184*d415bd75Srobert assert(NodesInLastSCC.empty());
185*d415bd75Srobert for (const auto &N : *LastSCC)
186*d415bd75Srobert NodesInLastSCC.insert(&N);
187097a140dSpatrick }
188*d415bd75Srobert
onPassExit(LazyCallGraph::SCC * LastSCC)189*d415bd75Srobert void MLInlineAdvisor::onPassExit(LazyCallGraph::SCC *LastSCC) {
190*d415bd75Srobert // No need to keep this around - function passes will invalidate it.
191*d415bd75Srobert if (!KeepFPICache)
192*d415bd75Srobert FPICache.clear();
193*d415bd75Srobert if (!LastSCC || ForceStop)
194*d415bd75Srobert return;
195*d415bd75Srobert // Keep track of the nodes and edges we last saw. Then, in onPassEntry,
196*d415bd75Srobert // we update the node count and edge count from the subset of these nodes that
197*d415bd75Srobert // survived.
198*d415bd75Srobert EdgesOfLastSeenNodes = 0;
199*d415bd75Srobert
200*d415bd75Srobert // Check on nodes that were in SCC onPassEntry
201*d415bd75Srobert for (auto I = NodesInLastSCC.begin(); I != NodesInLastSCC.end();) {
202*d415bd75Srobert if ((*I)->isDead())
203*d415bd75Srobert NodesInLastSCC.erase(*I++);
204*d415bd75Srobert else
205*d415bd75Srobert EdgesOfLastSeenNodes += getLocalCalls((*I++)->getFunction());
206*d415bd75Srobert }
207*d415bd75Srobert
208*d415bd75Srobert // Check on nodes that may have got added to SCC
209*d415bd75Srobert for (const auto &N : *LastSCC) {
210*d415bd75Srobert assert(!N.isDead());
211*d415bd75Srobert auto I = NodesInLastSCC.insert(&N);
212*d415bd75Srobert if (I.second)
213*d415bd75Srobert EdgesOfLastSeenNodes += getLocalCalls(N.getFunction());
214*d415bd75Srobert }
215*d415bd75Srobert assert(NodeCount >= NodesInLastSCC.size());
216*d415bd75Srobert assert(EdgeCount >= EdgesOfLastSeenNodes);
217097a140dSpatrick }
218097a140dSpatrick
getLocalCalls(Function & F)219097a140dSpatrick int64_t MLInlineAdvisor::getLocalCalls(Function &F) {
220*d415bd75Srobert return getCachedFPI(F).DirectCallsToDefinedFunctions;
221097a140dSpatrick }
222097a140dSpatrick
223097a140dSpatrick // Update the internal state of the advisor, and force invalidate feature
224097a140dSpatrick // analysis. Currently, we maintain minimal (and very simple) global state - the
225097a140dSpatrick // number of functions and the number of static calls. We also keep track of the
226097a140dSpatrick // total IR size in this module, to stop misbehaving policies at a certain bloat
227097a140dSpatrick // factor (SizeIncreaseThreshold)
onSuccessfulInlining(const MLInlineAdvice & Advice,bool CalleeWasDeleted)228097a140dSpatrick void MLInlineAdvisor::onSuccessfulInlining(const MLInlineAdvice &Advice,
229097a140dSpatrick bool CalleeWasDeleted) {
230097a140dSpatrick assert(!ForceStop);
231097a140dSpatrick Function *Caller = Advice.getCaller();
232097a140dSpatrick Function *Callee = Advice.getCallee();
233097a140dSpatrick // The caller features aren't valid anymore.
23473471bf0Spatrick {
23573471bf0Spatrick PreservedAnalyses PA = PreservedAnalyses::all();
23673471bf0Spatrick PA.abandon<FunctionPropertiesAnalysis>();
237*d415bd75Srobert PA.abandon<DominatorTreeAnalysis>();
238*d415bd75Srobert PA.abandon<LoopAnalysis>();
23973471bf0Spatrick FAM.invalidate(*Caller, PA);
24073471bf0Spatrick }
241*d415bd75Srobert Advice.updateCachedCallerFPI(FAM);
242097a140dSpatrick int64_t IRSizeAfter =
243097a140dSpatrick getIRSize(*Caller) + (CalleeWasDeleted ? 0 : Advice.CalleeIRSize);
244097a140dSpatrick CurrentIRSize += IRSizeAfter - (Advice.CallerIRSize + Advice.CalleeIRSize);
245097a140dSpatrick if (CurrentIRSize > SizeIncreaseThreshold * InitialIRSize)
246097a140dSpatrick ForceStop = true;
247097a140dSpatrick
248097a140dSpatrick // We can delta-update module-wide features. We know the inlining only changed
249097a140dSpatrick // the caller, and maybe the callee (by deleting the latter).
250097a140dSpatrick // Nodes are simple to update.
251097a140dSpatrick // For edges, we 'forget' the edges that the caller and callee used to have
252097a140dSpatrick // before inlining, and add back what they currently have together.
253097a140dSpatrick int64_t NewCallerAndCalleeEdges =
254*d415bd75Srobert getCachedFPI(*Caller).DirectCallsToDefinedFunctions;
255097a140dSpatrick
256097a140dSpatrick if (CalleeWasDeleted)
257097a140dSpatrick --NodeCount;
258097a140dSpatrick else
25973471bf0Spatrick NewCallerAndCalleeEdges +=
260*d415bd75Srobert getCachedFPI(*Callee).DirectCallsToDefinedFunctions;
261097a140dSpatrick EdgeCount += (NewCallerAndCalleeEdges - Advice.CallerAndCalleeEdges);
262097a140dSpatrick assert(CurrentIRSize >= 0 && EdgeCount >= 0 && NodeCount >= 0);
263097a140dSpatrick }
264097a140dSpatrick
getModuleIRSize() const265097a140dSpatrick int64_t MLInlineAdvisor::getModuleIRSize() const {
266097a140dSpatrick int64_t Ret = 0;
267*d415bd75Srobert for (auto &F : M)
268097a140dSpatrick if (!F.isDeclaration())
269097a140dSpatrick Ret += getIRSize(F);
270097a140dSpatrick return Ret;
271097a140dSpatrick }
272097a140dSpatrick
getCachedFPI(Function & F) const273*d415bd75Srobert FunctionPropertiesInfo &MLInlineAdvisor::getCachedFPI(Function &F) const {
274*d415bd75Srobert auto InsertPair =
275*d415bd75Srobert FPICache.insert(std::make_pair(&F, FunctionPropertiesInfo()));
276*d415bd75Srobert if (!InsertPair.second)
277*d415bd75Srobert return InsertPair.first->second;
278*d415bd75Srobert InsertPair.first->second = FAM.getResult<FunctionPropertiesAnalysis>(F);
279*d415bd75Srobert return InsertPair.first->second;
280*d415bd75Srobert }
281*d415bd75Srobert
getAdviceImpl(CallBase & CB)28273471bf0Spatrick std::unique_ptr<InlineAdvice> MLInlineAdvisor::getAdviceImpl(CallBase &CB) {
283*d415bd75Srobert if (auto Skip = getSkipAdviceIfUnreachableCallsite(CB))
284*d415bd75Srobert return Skip;
285*d415bd75Srobert
286097a140dSpatrick auto &Caller = *CB.getCaller();
287097a140dSpatrick auto &Callee = *CB.getCalledFunction();
288097a140dSpatrick
289097a140dSpatrick auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
290097a140dSpatrick return FAM.getResult<AssumptionAnalysis>(F);
291097a140dSpatrick };
292097a140dSpatrick auto &TIR = FAM.getResult<TargetIRAnalysis>(Callee);
293097a140dSpatrick auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
294097a140dSpatrick
29573471bf0Spatrick auto MandatoryKind = InlineAdvisor::getMandatoryKind(CB, FAM, ORE);
296097a140dSpatrick // If this is a "never inline" case, there won't be any changes to internal
297097a140dSpatrick // state we need to track, so we can just return the base InlineAdvice, which
298097a140dSpatrick // will do nothing interesting.
299097a140dSpatrick // Same thing if this is a recursive case.
30073471bf0Spatrick if (MandatoryKind == InlineAdvisor::MandatoryInliningKind::Never ||
301097a140dSpatrick &Caller == &Callee)
30273471bf0Spatrick return getMandatoryAdvice(CB, false);
303097a140dSpatrick
30473471bf0Spatrick bool Mandatory =
30573471bf0Spatrick MandatoryKind == InlineAdvisor::MandatoryInliningKind::Always;
306097a140dSpatrick
307097a140dSpatrick // If we need to stop, we won't want to track anymore any state changes, so
308097a140dSpatrick // we just return the base InlineAdvice, which acts as a noop.
309097a140dSpatrick if (ForceStop) {
310097a140dSpatrick ORE.emit([&] {
311097a140dSpatrick return OptimizationRemarkMissed(DEBUG_TYPE, "ForceStop", &CB)
312097a140dSpatrick << "Won't attempt inlining because module size grew too much.";
313097a140dSpatrick });
314097a140dSpatrick return std::make_unique<InlineAdvice>(this, CB, ORE, Mandatory);
315097a140dSpatrick }
316097a140dSpatrick
317097a140dSpatrick int CostEstimate = 0;
318097a140dSpatrick if (!Mandatory) {
319097a140dSpatrick auto IsCallSiteInlinable =
320097a140dSpatrick llvm::getInliningCostEstimate(CB, TIR, GetAssumptionCache);
321097a140dSpatrick if (!IsCallSiteInlinable) {
322097a140dSpatrick // We can't inline this for correctness reasons, so return the base
323097a140dSpatrick // InlineAdvice, as we don't care about tracking any state changes (which
324097a140dSpatrick // won't happen).
325097a140dSpatrick return std::make_unique<InlineAdvice>(this, CB, ORE, false);
326097a140dSpatrick }
327097a140dSpatrick CostEstimate = *IsCallSiteInlinable;
328097a140dSpatrick }
329097a140dSpatrick
33073471bf0Spatrick const auto CostFeatures =
33173471bf0Spatrick llvm::getInliningCostFeatures(CB, TIR, GetAssumptionCache);
33273471bf0Spatrick if (!CostFeatures) {
33373471bf0Spatrick return std::make_unique<InlineAdvice>(this, CB, ORE, false);
33473471bf0Spatrick }
33573471bf0Spatrick
336097a140dSpatrick if (Mandatory)
33773471bf0Spatrick return getMandatoryAdvice(CB, true);
338097a140dSpatrick
339097a140dSpatrick auto NrCtantParams = 0;
340097a140dSpatrick for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
341097a140dSpatrick NrCtantParams += (isa<Constant>(*I));
342097a140dSpatrick }
343097a140dSpatrick
344*d415bd75Srobert auto &CallerBefore = getCachedFPI(Caller);
345*d415bd75Srobert auto &CalleeBefore = getCachedFPI(Callee);
346097a140dSpatrick
347*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::CalleeBasicBlockCount) =
348*d415bd75Srobert CalleeBefore.BasicBlockCount;
349*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::CallSiteHeight) =
350*d415bd75Srobert getInitialFunctionLevel(Caller);
351*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::NodeCount) = NodeCount;
352*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::NrCtantParams) = NrCtantParams;
353*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::EdgeCount) = EdgeCount;
354*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::CallerUsers) =
355*d415bd75Srobert CallerBefore.Uses;
356*d415bd75Srobert *ModelRunner->getTensor<int64_t>(
357*d415bd75Srobert FeatureIndex::CallerConditionallyExecutedBlocks) =
358*d415bd75Srobert CallerBefore.BlocksReachedFromConditionalInstruction;
359*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::CallerBasicBlockCount) =
360*d415bd75Srobert CallerBefore.BasicBlockCount;
361*d415bd75Srobert *ModelRunner->getTensor<int64_t>(
362*d415bd75Srobert FeatureIndex::CalleeConditionallyExecutedBlocks) =
363*d415bd75Srobert CalleeBefore.BlocksReachedFromConditionalInstruction;
364*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::CalleeUsers) =
365*d415bd75Srobert CalleeBefore.Uses;
366*d415bd75Srobert *ModelRunner->getTensor<int64_t>(FeatureIndex::CostEstimate) = CostEstimate;
36773471bf0Spatrick
36873471bf0Spatrick // Add the cost features
36973471bf0Spatrick for (size_t I = 0;
37073471bf0Spatrick I < static_cast<size_t>(InlineCostFeatureIndex::NumberOfFeatures); ++I) {
371*d415bd75Srobert *ModelRunner->getTensor<int64_t>(inlineCostFeatureToMlFeature(
372*d415bd75Srobert static_cast<InlineCostFeatureIndex>(I))) = CostFeatures->at(I);
37373471bf0Spatrick }
37473471bf0Spatrick
375097a140dSpatrick return getAdviceFromModel(CB, ORE);
376097a140dSpatrick }
377097a140dSpatrick
378097a140dSpatrick std::unique_ptr<MLInlineAdvice>
getAdviceFromModel(CallBase & CB,OptimizationRemarkEmitter & ORE)379097a140dSpatrick MLInlineAdvisor::getAdviceFromModel(CallBase &CB,
380097a140dSpatrick OptimizationRemarkEmitter &ORE) {
381*d415bd75Srobert return std::make_unique<MLInlineAdvice>(
382*d415bd75Srobert this, CB, ORE, static_cast<bool>(ModelRunner->evaluate<int64_t>()));
383*d415bd75Srobert }
384*d415bd75Srobert
385*d415bd75Srobert std::unique_ptr<InlineAdvice>
getSkipAdviceIfUnreachableCallsite(CallBase & CB)386*d415bd75Srobert MLInlineAdvisor::getSkipAdviceIfUnreachableCallsite(CallBase &CB) {
387*d415bd75Srobert if (!FAM.getResult<DominatorTreeAnalysis>(*CB.getCaller())
388*d415bd75Srobert .isReachableFromEntry(CB.getParent()))
389*d415bd75Srobert return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), false);
390*d415bd75Srobert return nullptr;
391097a140dSpatrick }
392097a140dSpatrick
getMandatoryAdvice(CallBase & CB,bool Advice)39373471bf0Spatrick std::unique_ptr<InlineAdvice> MLInlineAdvisor::getMandatoryAdvice(CallBase &CB,
39473471bf0Spatrick bool Advice) {
39573471bf0Spatrick // Make sure we track inlinings in all cases - mandatory or not.
396*d415bd75Srobert if (auto Skip = getSkipAdviceIfUnreachableCallsite(CB))
397*d415bd75Srobert return Skip;
39873471bf0Spatrick if (Advice && !ForceStop)
39973471bf0Spatrick return getMandatoryAdviceImpl(CB);
40073471bf0Spatrick
40173471bf0Spatrick // If this is a "never inline" case, there won't be any changes to internal
40273471bf0Spatrick // state we need to track, so we can just return the base InlineAdvice, which
40373471bf0Spatrick // will do nothing interesting.
40473471bf0Spatrick // Same if we are forced to stop - we don't track anymore.
40573471bf0Spatrick return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), Advice);
40673471bf0Spatrick }
40773471bf0Spatrick
408097a140dSpatrick std::unique_ptr<MLInlineAdvice>
getMandatoryAdviceImpl(CallBase & CB)40973471bf0Spatrick MLInlineAdvisor::getMandatoryAdviceImpl(CallBase &CB) {
41073471bf0Spatrick return std::make_unique<MLInlineAdvice>(this, CB, getCallerORE(CB), true);
411097a140dSpatrick }
412097a140dSpatrick
print(raw_ostream & OS) const413*d415bd75Srobert void MLInlineAdvisor::print(raw_ostream &OS) const {
414*d415bd75Srobert OS << "[MLInlineAdvisor] Nodes: " << NodeCount << " Edges: " << EdgeCount
415*d415bd75Srobert << " EdgesOfLastSeenNodes: " << EdgesOfLastSeenNodes << "\n";
416*d415bd75Srobert OS << "[MLInlineAdvisor] FPI:\n";
417*d415bd75Srobert for (auto I : FPICache) {
418*d415bd75Srobert OS << I.first->getName() << ":\n";
419*d415bd75Srobert I.second.print(OS);
420*d415bd75Srobert OS << "\n";
421*d415bd75Srobert }
422*d415bd75Srobert OS << "\n";
423*d415bd75Srobert }
424*d415bd75Srobert
MLInlineAdvice(MLInlineAdvisor * Advisor,CallBase & CB,OptimizationRemarkEmitter & ORE,bool Recommendation)425*d415bd75Srobert MLInlineAdvice::MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB,
426*d415bd75Srobert OptimizationRemarkEmitter &ORE,
427*d415bd75Srobert bool Recommendation)
428*d415bd75Srobert : InlineAdvice(Advisor, CB, ORE, Recommendation),
429*d415bd75Srobert CallerIRSize(Advisor->isForcedToStop() ? 0 : Advisor->getIRSize(*Caller)),
430*d415bd75Srobert CalleeIRSize(Advisor->isForcedToStop() ? 0 : Advisor->getIRSize(*Callee)),
431*d415bd75Srobert CallerAndCalleeEdges(Advisor->isForcedToStop()
432*d415bd75Srobert ? 0
433*d415bd75Srobert : (Advisor->getLocalCalls(*Caller) +
434*d415bd75Srobert Advisor->getLocalCalls(*Callee))),
435*d415bd75Srobert PreInlineCallerFPI(Advisor->getCachedFPI(*Caller)) {
436*d415bd75Srobert if (Recommendation)
437*d415bd75Srobert FPU.emplace(Advisor->getCachedFPI(*getCaller()), CB);
438*d415bd75Srobert }
439*d415bd75Srobert
reportContextForRemark(DiagnosticInfoOptimizationBase & OR)440097a140dSpatrick void MLInlineAdvice::reportContextForRemark(
441097a140dSpatrick DiagnosticInfoOptimizationBase &OR) {
442097a140dSpatrick using namespace ore;
443097a140dSpatrick OR << NV("Callee", Callee->getName());
444097a140dSpatrick for (size_t I = 0; I < NumberOfFeatures; ++I)
445*d415bd75Srobert OR << NV(FeatureMap[I].name(),
446*d415bd75Srobert *getAdvisor()->getModelRunner().getTensor<int64_t>(I));
447097a140dSpatrick OR << NV("ShouldInline", isInliningRecommended());
448097a140dSpatrick }
449097a140dSpatrick
updateCachedCallerFPI(FunctionAnalysisManager & FAM) const450*d415bd75Srobert void MLInlineAdvice::updateCachedCallerFPI(FunctionAnalysisManager &FAM) const {
451*d415bd75Srobert FPU->finish(FAM);
452*d415bd75Srobert }
453*d415bd75Srobert
recordInliningImpl()454097a140dSpatrick void MLInlineAdvice::recordInliningImpl() {
455097a140dSpatrick ORE.emit([&]() {
456097a140dSpatrick OptimizationRemark R(DEBUG_TYPE, "InliningSuccess", DLoc, Block);
457097a140dSpatrick reportContextForRemark(R);
458097a140dSpatrick return R;
459097a140dSpatrick });
460097a140dSpatrick getAdvisor()->onSuccessfulInlining(*this, /*CalleeWasDeleted*/ false);
461097a140dSpatrick }
462097a140dSpatrick
recordInliningWithCalleeDeletedImpl()463097a140dSpatrick void MLInlineAdvice::recordInliningWithCalleeDeletedImpl() {
464097a140dSpatrick ORE.emit([&]() {
465097a140dSpatrick OptimizationRemark R(DEBUG_TYPE, "InliningSuccessWithCalleeDeleted", DLoc,
466097a140dSpatrick Block);
467097a140dSpatrick reportContextForRemark(R);
468097a140dSpatrick return R;
469097a140dSpatrick });
470097a140dSpatrick getAdvisor()->onSuccessfulInlining(*this, /*CalleeWasDeleted*/ true);
471097a140dSpatrick }
472097a140dSpatrick
recordUnsuccessfulInliningImpl(const InlineResult & Result)473097a140dSpatrick void MLInlineAdvice::recordUnsuccessfulInliningImpl(
474097a140dSpatrick const InlineResult &Result) {
475*d415bd75Srobert getAdvisor()->getCachedFPI(*Caller) = PreInlineCallerFPI;
476097a140dSpatrick ORE.emit([&]() {
477097a140dSpatrick OptimizationRemarkMissed R(DEBUG_TYPE, "InliningAttemptedAndUnsuccessful",
478097a140dSpatrick DLoc, Block);
479097a140dSpatrick reportContextForRemark(R);
480097a140dSpatrick return R;
481097a140dSpatrick });
482097a140dSpatrick }
recordUnattemptedInliningImpl()483097a140dSpatrick void MLInlineAdvice::recordUnattemptedInliningImpl() {
484*d415bd75Srobert assert(!FPU);
485097a140dSpatrick ORE.emit([&]() {
486097a140dSpatrick OptimizationRemarkMissed R(DEBUG_TYPE, "IniningNotAttempted", DLoc, Block);
487097a140dSpatrick reportContextForRemark(R);
488097a140dSpatrick return R;
489097a140dSpatrick });
490097a140dSpatrick }
491