LLVM 22.0.0git
MLInlineAdvisor.cpp
Go to the documentation of this file.
1//===- MLInlineAdvisor.cpp - machine learned InlineAdvisor ----------------===//
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// This file implements the interface between the inliner and a learned model.
10// It delegates model evaluation to either the AOT compiled model (the
11// 'release' mode) or a runtime-loaded model (the 'development' case).
12//
13//===----------------------------------------------------------------------===//
31#include "llvm/IR/Dominators.h"
33#include "llvm/IR/Module.h"
34#include "llvm/IR/PassManager.h"
36
37using namespace llvm;
38
40 "inliner-interactive-channel-base", cl::Hidden,
42 "Base file path for the interactive mode. The incoming filename should "
43 "have the name <inliner-interactive-channel-base>.in, while the "
44 "outgoing name should be <inliner-interactive-channel-base>.out"));
45static const std::string InclDefaultMsg =
46 (Twine("In interactive mode, also send the default policy decision: ") +
48 .str();
49static cl::opt<bool>
50 InteractiveIncludeDefault("inliner-interactive-include-default", cl::Hidden,
52
54
56 "ml-inliner-skip-policy", cl::Hidden, cl::init(SkipMLPolicyCriteria::Never),
59 "if-caller-not-cold", "if the caller is not cold")));
60
61static cl::opt<std::string> ModelSelector("ml-inliner-model-selector",
62 cl::Hidden, cl::init(""));
63
64static cl::opt<bool> StopImmediatelyForTest("ml-inliner-stop-immediately",
66
67#if defined(LLVM_HAVE_TF_AOT_INLINERSIZEMODEL)
68// codegen-ed file
69#include "InlinerSizeModel.h" // NOLINT
70using CompiledModelType = llvm::InlinerSizeModel;
71#else
73#endif
74
75std::unique_ptr<InlineAdvisor>
77 std::function<bool(CallBase &)> GetDefaultAdvice) {
80 return nullptr;
81 auto RunnerFactory = [&](const std::vector<TensorSpec> &InputFeatures)
82 -> std::unique_ptr<MLModelRunner> {
83 std::unique_ptr<MLModelRunner> AOTRunner;
85 AOTRunner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
86 M.getContext(), InputFeatures, DecisionName,
87 EmbeddedModelRunnerOptions().setModelSelector(ModelSelector));
88 else {
89 AOTRunner = std::make_unique<InteractiveModelRunner>(
90 M.getContext(), InputFeatures, InlineDecisionSpec,
93 }
94 return AOTRunner;
95 };
96 return std::make_unique<MLInlineAdvisor>(M, MAM, RunnerFactory,
97 GetDefaultAdvice);
98}
99
100#define DEBUG_TYPE "inline-ml"
101
103 "ml-advisor-size-increase-threshold", cl::Hidden,
104 cl::desc("Maximum factor by which expected native size may increase before "
105 "blocking any further inlining."),
106 cl::init(2.0));
107
109 "ml-advisor-keep-fpi-cache", cl::Hidden,
110 cl::desc(
111 "For test - keep the ML Inline advisor's FunctionPropertiesInfo cache"),
112 cl::init(false));
113
114const std::vector<TensorSpec> &MLInlineAdvisor::getInitialFeatureMap() {
115 // clang-format off
116static std::vector<TensorSpec> FeatureMap{
117#define POPULATE_NAMES(DTYPE, SHAPE, NAME, __) TensorSpec::createSpec<DTYPE>(#NAME, SHAPE),
118// InlineCost features - these must come first
120
121// Non-cost features
123#undef POPULATE_NAMES
124};
125 // clang-format on
126 return FeatureMap;
127}
128
129const char *const llvm::DecisionName = "inlining_decision";
132const char *const llvm::DefaultDecisionName = "inlining_default";
135const char *const llvm::RewardName = "delta_size";
136
138 if (auto *CS = dyn_cast<CallBase>(&I))
139 if (Function *Callee = CS->getCalledFunction()) {
140 if (!Callee->isDeclaration()) {
141 return CS;
142 }
143 }
144 return nullptr;
145}
146
149 std::function<
150 std::unique_ptr<MLModelRunner>(const std::vector<TensorSpec> &)>
151 GetModelRunner,
152 std::function<bool(CallBase &)> GetDefaultAdvice)
154 M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
156 CG(MAM.getResult<LazyCallGraphAnalysis>(M)),
157 UseIR2Vec(MAM.getCachedResult<IR2VecVocabAnalysis>(M) != nullptr),
158 InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize),
159 PSI(MAM.getResult<ProfileSummaryAnalysis>(M)) {
160 // Extract the 'call site height' feature - the position of a call site
161 // relative to the farthest statically reachable SCC node. We don't mutate
162 // this value while inlining happens. Empirically, this feature proved
163 // critical in behavioral cloning - i.e. training a model to mimic the manual
164 // heuristic's decisions - and, thus, equally important for training for
165 // improvement.
166 CallGraph CGraph(M);
167 for (auto I = scc_begin(&CGraph); !I.isAtEnd(); ++I) {
168 const std::vector<CallGraphNode *> &CGNodes = *I;
169 unsigned Level = 0;
170 for (auto *CGNode : CGNodes) {
171 Function *F = CGNode->getFunction();
172 if (!F || F->isDeclaration())
173 continue;
174 for (auto &I : instructions(F)) {
175 if (auto *CS = getInlinableCS(I)) {
176 auto *Called = CS->getCalledFunction();
177 auto Pos = FunctionLevels.find(&CG.get(*Called));
178 // In bottom up traversal, an inlinable callee is either in the
179 // same SCC, or to a function in a visited SCC. So not finding its
180 // level means we haven't visited it yet, meaning it's in this SCC.
181 if (Pos == FunctionLevels.end())
182 continue;
183 Level = std::max(Level, Pos->second + 1);
184 }
185 }
186 }
187 for (auto *CGNode : CGNodes) {
188 Function *F = CGNode->getFunction();
189 if (F && !F->isDeclaration())
190 FunctionLevels[&CG.get(*F)] = Level;
191 }
192 }
193 for (auto KVP : FunctionLevels) {
194 AllNodes.insert(KVP.first);
195 EdgeCount += getLocalCalls(KVP.first->getFunction());
196 }
197 NodeCount = AllNodes.size();
198
199 if (auto *IR2VecVocabResult = MAM.getCachedResult<IR2VecVocabAnalysis>(M)) {
200 if (!IR2VecVocabResult->isValid()) {
201 M.getContext().emitError("IR2VecVocabAnalysis is not valid");
202 return;
203 }
204 // Add the IR2Vec features to the feature map
205 auto IR2VecDim = IR2VecVocabResult->getDimension();
206 FeatureMap.push_back(
207 TensorSpec::createSpec<float>("callee_embedding", {IR2VecDim}));
208 FeatureMap.push_back(
209 TensorSpec::createSpec<float>("caller_embedding", {IR2VecDim}));
210 }
213
214 ModelRunner = GetModelRunner(getFeatureMap());
215 if (!ModelRunner) {
216 M.getContext().emitError("Could not create model runner");
217 return;
218 }
219 ModelRunner->switchContext("");
220 ForceStop = StopImmediatelyForTest;
221}
222
224 return CG.lookup(F) ? FunctionLevels.at(CG.lookup(F)) : 0;
225}
226
228 if (!CurSCC || ForceStop)
229 return;
230 FPICache.clear();
231 // Function passes executed between InlinerPass runs may have changed the
232 // module-wide features.
233 // The cgscc pass manager rules are such that:
234 // - if a pass leads to merging SCCs, then the pipeline is restarted on the
235 // merged SCC
236 // - if a pass leads to splitting the SCC, then we continue with one of the
237 // splits
238 // This means that the NodesInLastSCC is a superset (not strict) of the nodes
239 // that subsequent passes would have processed
240 // - in addition, if new Nodes were created by a pass (e.g. CoroSplit),
241 // they'd be adjacent to Nodes in the last SCC. So we just need to check the
242 // boundary of Nodes in NodesInLastSCC for Nodes we haven't seen. We don't
243 // care about the nature of the Edge (call or ref). `FunctionLevels`-wise, we
244 // record them at the same level as the original node (this is a choice, may
245 // need revisiting).
246 // - nodes are only deleted at the end of a call graph walk where they are
247 // batch deleted, so we shouldn't see any dead nodes here.
248 while (!NodesInLastSCC.empty()) {
249 const auto *N = *NodesInLastSCC.begin();
250 assert(!N->isDead());
251 NodesInLastSCC.erase(N);
252 EdgeCount += getLocalCalls(N->getFunction());
253 const auto NLevel = FunctionLevels.at(N);
254 for (const auto &E : *(*N)) {
255 const auto *AdjNode = &E.getNode();
256 assert(!AdjNode->isDead() && !AdjNode->getFunction().isDeclaration());
257 auto I = AllNodes.insert(AdjNode);
258 // We've discovered a new function.
259 if (I.second) {
260 ++NodeCount;
261 NodesInLastSCC.insert(AdjNode);
262 FunctionLevels[AdjNode] = NLevel;
263 }
264 }
265 }
266
267 EdgeCount -= EdgesOfLastSeenNodes;
268 EdgesOfLastSeenNodes = 0;
269
270 // (Re)use NodesInLastSCC to remember the nodes in the SCC right now,
271 // in case the SCC is split before onPassExit and some nodes are split out
272 assert(NodesInLastSCC.empty());
273 for (const auto &N : *CurSCC)
274 NodesInLastSCC.insert(&N);
275}
276
278 // No need to keep this around - function passes will invalidate it.
279 if (!KeepFPICache)
280 FPICache.clear();
281 if (!CurSCC || ForceStop)
282 return;
283 // Keep track of the nodes and edges we last saw. Then, in onPassEntry,
284 // we update the node count and edge count from the subset of these nodes that
285 // survived.
286 EdgesOfLastSeenNodes = 0;
287
288 // Check on nodes that were in SCC onPassEntry
289 for (const LazyCallGraph::Node *N : NodesInLastSCC) {
290 assert(!N->isDead());
291 EdgesOfLastSeenNodes += getLocalCalls(N->getFunction());
292 }
293
294 // Check on nodes that may have got added to SCC
295 for (const auto &N : *CurSCC) {
296 assert(!N.isDead());
297 auto I = NodesInLastSCC.insert(&N);
298 if (I.second)
299 EdgesOfLastSeenNodes += getLocalCalls(N.getFunction());
300 }
301 assert(NodeCount >= NodesInLastSCC.size());
302 assert(EdgeCount >= EdgesOfLastSeenNodes);
303}
304
308
309// Update the internal state of the advisor, and force invalidate feature
310// analysis. Currently, we maintain minimal (and very simple) global state - the
311// number of functions and the number of static calls. We also keep track of the
312// total IR size in this module, to stop misbehaving policies at a certain bloat
313// factor (SizeIncreaseThreshold)
315 bool CalleeWasDeleted) {
316 assert(!ForceStop);
317 Function *Caller = Advice.getCaller();
318 Function *Callee = Advice.getCallee();
319 // The caller features aren't valid anymore.
320 {
323 PA.abandon<LoopAnalysis>();
324 FAM.invalidate(*Caller, PA);
325 }
327 int64_t IRSizeAfter =
328 getIRSize(*Caller) + (CalleeWasDeleted ? 0 : Advice.CalleeIRSize);
329 CurrentIRSize += IRSizeAfter - (Advice.CallerIRSize + Advice.CalleeIRSize);
330 if (CurrentIRSize > SizeIncreaseThreshold * InitialIRSize)
331 ForceStop = true;
332
333 // We can delta-update module-wide features. We know the inlining only changed
334 // the caller, and maybe the callee (by deleting the latter).
335 // Nodes are simple to update.
336 // For edges, we 'forget' the edges that the caller and callee used to have
337 // before inlining, and add back what they currently have together.
338 int64_t NewCallerAndCalleeEdges =
340
341 // A dead function's node is not actually removed from the call graph until
342 // the end of the call graph walk, but the node no longer belongs to any valid
343 // SCC.
344 if (CalleeWasDeleted) {
345 --NodeCount;
346 NodesInLastSCC.erase(CG.lookup(*Callee));
347 DeadFunctions.insert(Callee);
348 } else {
349 NewCallerAndCalleeEdges +=
351 }
352 EdgeCount += (NewCallerAndCalleeEdges - Advice.CallerAndCalleeEdges);
353 assert(CurrentIRSize >= 0 && EdgeCount >= 0 && NodeCount >= 0);
354}
355
356int64_t MLInlineAdvisor::getModuleIRSize() const {
357 int64_t Ret = 0;
358 for (auto &F : M)
359 if (!F.isDeclaration())
360 Ret += getIRSize(F);
361 return Ret;
362}
363
365 auto InsertPair = FPICache.try_emplace(&F);
366 if (!InsertPair.second)
367 return InsertPair.first->second;
368 InsertPair.first->second = FAM.getResult<FunctionPropertiesAnalysis>(F);
369 return InsertPair.first->second;
370}
371
372std::unique_ptr<InlineAdvice> MLInlineAdvisor::getAdviceImpl(CallBase &CB) {
373 if (auto Skip = getSkipAdviceIfUnreachableCallsite(CB))
374 return Skip;
375
376 auto &Caller = *CB.getCaller();
377 auto &Callee = *CB.getCalledFunction();
378
379 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
380 return FAM.getResult<AssumptionAnalysis>(F);
381 };
382 auto &TIR = FAM.getResult<TargetIRAnalysis>(Callee);
383 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
384
386 if (!PSI.isFunctionEntryCold(&Caller)) {
387 // Return a MLInlineAdvice, despite delegating to the default advice,
388 // because we need to keep track of the internal state. This is different
389 // from the other instances where we return a "default" InlineAdvice,
390 // which happen at points we won't come back to the MLAdvisor for
391 // decisions requiring that state.
392 return ForceStop ? std::make_unique<InlineAdvice>(this, CB, ORE,
394 : std::make_unique<MLInlineAdvice>(this, CB, ORE,
395 GetDefaultAdvice(CB));
396 }
397 }
398 auto MandatoryKind = InlineAdvisor::getMandatoryKind(CB, FAM, ORE);
399 // If this is a "never inline" case, there won't be any changes to internal
400 // state we need to track, so we can just return the base InlineAdvice, which
401 // will do nothing interesting.
402 // Same thing if this is a recursive case.
403 if (MandatoryKind == InlineAdvisor::MandatoryInliningKind::Never ||
404 &Caller == &Callee)
405 return getMandatoryAdvice(CB, false);
406
407 bool Mandatory =
409
410 // If we need to stop, we won't want to track anymore any state changes, so
411 // we just return the base InlineAdvice, which acts as a noop.
412 if (ForceStop) {
413 ORE.emit([&] {
414 return OptimizationRemarkMissed(DEBUG_TYPE, "ForceStop", &CB)
415 << "Won't attempt inlining because module size grew too much.";
416 });
417 return std::make_unique<InlineAdvice>(this, CB, ORE, Mandatory);
418 }
419
420 int CostEstimate = 0;
421 if (!Mandatory) {
422 auto IsCallSiteInlinable =
423 llvm::getInliningCostEstimate(CB, TIR, GetAssumptionCache);
424 if (!IsCallSiteInlinable) {
425 // We can't inline this for correctness reasons, so return the base
426 // InlineAdvice, as we don't care about tracking any state changes (which
427 // won't happen).
428 return std::make_unique<InlineAdvice>(this, CB, ORE, false);
429 }
430 CostEstimate = *IsCallSiteInlinable;
431 }
432
433 const auto CostFeatures =
434 llvm::getInliningCostFeatures(CB, TIR, GetAssumptionCache);
435 if (!CostFeatures) {
436 return std::make_unique<InlineAdvice>(this, CB, ORE, false);
437 }
438
439 if (Mandatory)
440 return getMandatoryAdvice(CB, true);
441
442 auto NumCtantParams = 0;
443 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
444 NumCtantParams += (isa<Constant>(*I));
445 }
446
447 auto &CallerBefore = getCachedFPI(Caller);
448 auto &CalleeBefore = getCachedFPI(Callee);
449
450 *ModelRunner->getTensor<int64_t>(FeatureIndex::callee_basic_block_count) =
451 CalleeBefore.BasicBlockCount;
452 *ModelRunner->getTensor<int64_t>(FeatureIndex::callsite_height) =
454 *ModelRunner->getTensor<int64_t>(FeatureIndex::node_count) = NodeCount;
455 *ModelRunner->getTensor<int64_t>(FeatureIndex::nr_ctant_params) =
456 NumCtantParams;
457 *ModelRunner->getTensor<int64_t>(FeatureIndex::edge_count) = EdgeCount;
458 *ModelRunner->getTensor<int64_t>(FeatureIndex::caller_users) =
459 CallerBefore.Uses;
460 *ModelRunner->getTensor<int64_t>(
461 FeatureIndex::caller_conditionally_executed_blocks) =
462 CallerBefore.BlocksReachedFromConditionalInstruction;
463 *ModelRunner->getTensor<int64_t>(FeatureIndex::caller_basic_block_count) =
464 CallerBefore.BasicBlockCount;
465 *ModelRunner->getTensor<int64_t>(
466 FeatureIndex::callee_conditionally_executed_blocks) =
467 CalleeBefore.BlocksReachedFromConditionalInstruction;
468 *ModelRunner->getTensor<int64_t>(FeatureIndex::callee_users) =
469 CalleeBefore.Uses;
470 *ModelRunner->getTensor<int64_t>(FeatureIndex::cost_estimate) = CostEstimate;
471 *ModelRunner->getTensor<int64_t>(FeatureIndex::is_callee_avail_external) =
472 Callee.hasAvailableExternallyLinkage();
473 *ModelRunner->getTensor<int64_t>(FeatureIndex::is_caller_avail_external) =
474 Caller.hasAvailableExternallyLinkage();
475
476 if (UseIR2Vec) {
477 // Python side expects float embeddings. The IR2Vec embeddings are doubles
478 // as of now due to the restriction of fromJSON method used by the
479 // readVocabulary method in ir2vec::Embeddings.
480 auto setEmbedding = [&](const ir2vec::Embedding &Embedding,
481 FeatureIndex Index) {
482 llvm::transform(Embedding, ModelRunner->getTensor<float>(Index),
483 [](double Val) { return static_cast<float>(Val); });
484 };
485
486 setEmbedding(CalleeBefore.getFunctionEmbedding(),
488 setEmbedding(CallerBefore.getFunctionEmbedding(),
490 }
491
492 // Add the cost features
493 for (size_t I = 0;
495 *ModelRunner->getTensor<int64_t>(inlineCostFeatureToMlFeature(
496 static_cast<InlineCostFeatureIndex>(I))) = CostFeatures->at(I);
497 }
498 // This one would have been set up to be right at the end.
500 *ModelRunner->getTensor<int64_t>(getFeatureMap().size() - 1) =
502 return getAdviceFromModel(CB, ORE);
503}
504
505std::unique_ptr<MLInlineAdvice>
508 return std::make_unique<MLInlineAdvice>(
509 this, CB, ORE, static_cast<bool>(ModelRunner->evaluate<int64_t>()));
510}
511
512std::unique_ptr<InlineAdvice>
513MLInlineAdvisor::getSkipAdviceIfUnreachableCallsite(CallBase &CB) {
514 if (!FAM.getResult<DominatorTreeAnalysis>(*CB.getCaller())
515 .isReachableFromEntry(CB.getParent()))
516 return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), false);
517 return nullptr;
518}
519
520std::unique_ptr<InlineAdvice> MLInlineAdvisor::getMandatoryAdvice(CallBase &CB,
521 bool Advice) {
522 // Make sure we track inlinings in all cases - mandatory or not.
523 if (auto Skip = getSkipAdviceIfUnreachableCallsite(CB))
524 return Skip;
525 if (Advice && !ForceStop)
526 return getMandatoryAdviceImpl(CB);
527
528 // If this is a "never inline" case, there won't be any changes to internal
529 // state we need to track, so we can just return the base InlineAdvice, which
530 // will do nothing interesting.
531 // Same if we are forced to stop - we don't track anymore.
532 return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), Advice);
533}
534
535std::unique_ptr<MLInlineAdvice>
537 return std::make_unique<MLInlineAdvice>(this, CB, getCallerORE(CB), true);
538}
539
540void MLInlineAdvisor::print(raw_ostream &OS) const {
541 OS << "[MLInlineAdvisor] Nodes: " << NodeCount << " Edges: " << EdgeCount
542 << " EdgesOfLastSeenNodes: " << EdgesOfLastSeenNodes << "\n";
543 OS << "[MLInlineAdvisor] FPI:\n";
544 for (auto I : FPICache) {
545 OS << I.first->getName() << ":\n";
546 I.second.print(OS);
547 OS << "\n";
548 }
549 OS << "\n";
550 OS << "[MLInlineAdvisor] FuncLevels:\n";
551 for (auto I : FunctionLevels)
552 OS << (DeadFunctions.contains(&I.first->getFunction())
553 ? "<deleted>"
554 : I.first->getFunction().getName())
555 << " : " << I.second << "\n";
556
557 OS << "\n";
558}
559
562 bool Recommendation)
563 : InlineAdvice(Advisor, CB, ORE, Recommendation),
564 CallerIRSize(Advisor->isForcedToStop() ? 0 : Advisor->getIRSize(*Caller)),
565 CalleeIRSize(Advisor->isForcedToStop() ? 0 : Advisor->getIRSize(*Callee)),
566 CallerAndCalleeEdges(Advisor->isForcedToStop()
567 ? 0
568 : (Advisor->getLocalCalls(*Caller) +
569 Advisor->getLocalCalls(*Callee))),
570 PreInlineCallerFPI(Advisor->getCachedFPI(*Caller)) {
571 if (Recommendation)
572 FPU.emplace(Advisor->getCachedFPI(*getCaller()), CB);
573}
574
575void MLInlineAdvice::reportContextForRemark(
577 using namespace ore;
578 OR << NV("Callee", Callee->getName());
579 for (size_t I = 0; I < getAdvisor()->getFeatureMap().size(); ++I)
580 OR << NV(getAdvisor()->getFeatureMap()[I].name(),
581 *getAdvisor()->getModelRunner().getTensor<int64_t>(I));
582 OR << NV("ShouldInline", isInliningRecommended());
583}
584
588
590 ORE.emit([&]() {
591 OptimizationRemark R(DEBUG_TYPE, "InliningSuccess", DLoc, Block);
592 reportContextForRemark(R);
593 return R;
594 });
595 getAdvisor()->onSuccessfulInlining(*this, /*CalleeWasDeleted*/ false);
596}
597
599 ORE.emit([&]() {
600 OptimizationRemark R(DEBUG_TYPE, "InliningSuccessWithCalleeDeleted", DLoc,
601 Block);
602 reportContextForRemark(R);
603 return R;
604 });
605 getAdvisor()->onSuccessfulInlining(*this, /*CalleeWasDeleted*/ true);
606}
607
609 const InlineResult &Result) {
610 getAdvisor()->getCachedFPI(*Caller) = PreInlineCallerFPI;
611 ORE.emit([&]() {
612 OptimizationRemarkMissed R(DEBUG_TYPE, "InliningAttemptedAndUnsuccessful",
613 DLoc, Block);
614 reportContextForRemark(R);
615 return R;
616 });
617}
619 assert(!FPU);
620 ORE.emit([&]() {
621 OptimizationRemarkMissed R(DEBUG_TYPE, "IniningNotAttempted", DLoc, Block);
622 reportContextForRemark(R);
623 return R;
624 });
625}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define INLINE_COST_FEATURE_ITERATOR(M)
#define INLINE_FEATURE_ITERATOR(M)
Implements a lazy call graph analysis and related passes for the new pass manager.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
static cl::opt< bool > KeepFPICache("ml-advisor-keep-fpi-cache", cl::Hidden, cl::desc("For test - keep the ML Inline advisor's FunctionPropertiesInfo cache"), cl::init(false))
static cl::opt< std::string > ModelSelector("ml-inliner-model-selector", cl::Hidden, cl::init(""))
CallBase * getInlinableCS(Instruction &I)
NoopSavedModelImpl CompiledModelType
SkipMLPolicyCriteria
static cl::opt< std::string > InteractiveChannelBaseName("inliner-interactive-channel-base", cl::Hidden, cl::desc("Base file path for the interactive mode. The incoming filename should " "have the name <inliner-interactive-channel-base>.in, while the " "outgoing name should be <inliner-interactive-channel-base>.out"))
#define POPULATE_NAMES(DTYPE, SHAPE, NAME, __)
static cl::opt< bool > StopImmediatelyForTest("ml-inliner-stop-immediately", cl::Hidden)
static cl::opt< float > SizeIncreaseThreshold("ml-advisor-size-increase-threshold", cl::Hidden, cl::desc("Maximum factor by which expected native size may increase before " "blocking any further inlining."), cl::init(2.0))
static const std::string InclDefaultMsg
static cl::opt< SkipMLPolicyCriteria > SkipPolicy("ml-inliner-skip-policy", cl::Hidden, cl::init(SkipMLPolicyCriteria::Never), cl::values(clEnumValN(SkipMLPolicyCriteria::Never, "never", "never"), clEnumValN(SkipMLPolicyCriteria::IfCallerIsNotCold, "if-caller-not-cold", "if the caller is not cold")))
static cl::opt< bool > InteractiveIncludeDefault("inliner-interactive-include-default", cl::Hidden, cl::desc(InclDefaultMsg))
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
static const char * name
This pass exposes codegen information to IR-level passes.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
The basic data container for the call graph of a Module of IR.
Definition CallGraph.h:72
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Analysis pass which computes a DominatorTree.
Definition Dominators.h:284
int64_t DirectCallsToDefinedFunctions
Number of direct calls made from this function to other functions defined in this module.
This analysis provides the vocabulary for IR2Vec.
Definition IR2Vec.h:614
Function *const Callee
Function *const Caller
Caller and Callee are pre-inlining.
const BasicBlock *const Block
OptimizationRemarkEmitter & ORE
InlineAdvisor *const Advisor
LLVM_ABI InlineAdvice(InlineAdvisor *Advisor, CallBase &CB, OptimizationRemarkEmitter &ORE, bool IsInliningRecommended)
const DebugLoc DLoc
bool isInliningRecommended() const
Get the inlining recommendation.
OptimizationRemarkEmitter & getCallerORE(CallBase &CB)
FunctionAnalysisManager & FAM
static MandatoryInliningKind getMandatoryKind(CallBase &CB, FunctionAnalysisManager &FAM, OptimizationRemarkEmitter &ORE)
InlineAdvisor(InlineAdvisor &&)=delete
InlineResult is basically true or false.
Definition InlineCost.h:181
An analysis pass which computes the call graph for a module.
A node in the call graph.
An SCC of the call graph.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:569
InlineAdvice that tracks changes post inlining.
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
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.
void onSuccessfulInlining(const MLInlineAdvice &Advice, bool CalleeWasDeleted)
static const std::vector< TensorSpec > & getInitialFeatureMap()
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...
MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM, std::function< std::unique_ptr< MLModelRunner >(const std::vector< TensorSpec > &)> GetModelRunner, std::function< bool(CallBase &)> GetDefaultAdvice)
int64_t getLocalCalls(Function &F)
std::vector< TensorSpec > FeatureMap
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
unsigned getInitialFunctionLevel(const Function &F) const
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A mock class satisfying the interface expected by ReleaseModeModelRunner for its TGen parameter.
The optimization diagnostic interface.
Diagnostic information for missed-optimization remarks.
Diagnostic information for applied optimization remarks.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & abandon()
Mark an analysis as abandoned.
Definition Analysis.h:171
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Analysis pass providing the TargetTransformInfo.
static TensorSpec createSpec(const std::string &Name, const std::vector< int64_t > &Shape, int Port=0)
Definition TensorSpec.h:66
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
const ParentTy * getParent() const
Definition ilist_node.h:34
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
This is an optimization pass for GlobalISel generic memory operations.
constexpr FeatureIndex inlineCostFeatureToMlFeature(InlineCostFeatureIndex Feature)
LLVM_ABI const char *const DefaultDecisionName
bool isEmbeddedModelEvaluatorValid()
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
LLVM_ABI std::unique_ptr< InlineAdvisor > getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM, std::function< bool(CallBase &)> GetDefaultAdvice)
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:1968
LLVM_ABI const TensorSpec DefaultDecisionSpec
LLVM_ABI const char *const DecisionName
static const std::vector< TensorSpec > InputFeatures
LLVM_ABI std::optional< InlineCostFeatures > getInliningCostFeatures(CallBase &Call, TargetTransformInfo &CalleeTTI, function_ref< AssumptionCache &(Function &)> GetAssumptionCache, function_ref< BlockFrequencyInfo &(Function &)> GetBFI=nullptr, function_ref< const TargetLibraryInfo &(Function &)> GetTLI=nullptr, ProfileSummaryInfo *PSI=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
Get the expanded cost features.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI const TensorSpec InlineDecisionSpec
LLVM_ABI const char *const RewardName
LLVM_ABI std::optional< int > getInliningCostEstimate(CallBase &Call, TargetTransformInfo &CalleeTTI, function_ref< AssumptionCache &(Function &)> GetAssumptionCache, function_ref< BlockFrequencyInfo &(Function &)> GetBFI=nullptr, function_ref< const TargetLibraryInfo &(Function &)> GetTLI=nullptr, ProfileSummaryInfo *PSI=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
Get the cost estimate ignoring thresholds.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
#define N
ReleaseModeModelRunner - production mode implementation of the MLModelRunner.
Embedding is a datatype that wraps std::vector<double>.
Definition IR2Vec.h:87