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 //===----------------------------------------------------------------------===// 14 #include "llvm/Analysis/MLInlineAdvisor.h" 15 #include "llvm/ADT/SCCIterator.h" 16 #include "llvm/Analysis/AssumptionCache.h" 17 #include "llvm/Analysis/CallGraph.h" 18 #include "llvm/Analysis/FunctionPropertiesAnalysis.h" 19 #include "llvm/Analysis/InlineCost.h" 20 #include "llvm/Analysis/InlineModelFeatureMaps.h" 21 #include "llvm/Analysis/LazyCallGraph.h" 22 #include "llvm/Analysis/LoopInfo.h" 23 #include "llvm/Analysis/MLModelRunner.h" 24 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 25 #include "llvm/Analysis/TargetTransformInfo.h" 26 #include "llvm/IR/Dominators.h" 27 #include "llvm/IR/InstIterator.h" 28 #include "llvm/IR/PassManager.h" 29 #include "llvm/Support/CommandLine.h" 30 31 using namespace llvm; 32 33 #if defined(LLVM_HAVE_TF_AOT_INLINERSIZEMODEL) 34 #include "llvm/Analysis/ReleaseModeModelRunner.h" 35 // codegen-ed file 36 #include "InlinerSizeModel.h" // NOLINT 37 38 std::unique_ptr<InlineAdvisor> 39 llvm::getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM) { 40 auto AOTRunner = 41 std::make_unique<ReleaseModeModelRunner<llvm::InlinerSizeModel>>( 42 M.getContext(), FeatureMap, DecisionName); 43 return std::make_unique<MLInlineAdvisor>(M, MAM, std::move(AOTRunner)); 44 } 45 #endif 46 47 #define DEBUG_TYPE "inline-ml" 48 49 static cl::opt<float> SizeIncreaseThreshold( 50 "ml-advisor-size-increase-threshold", cl::Hidden, 51 cl::desc("Maximum factor by which expected native size may increase before " 52 "blocking any further inlining."), 53 cl::init(2.0)); 54 55 static cl::opt<bool> KeepFPICache( 56 "ml-advisor-keep-fpi-cache", cl::Hidden, 57 cl::desc( 58 "For test - keep the ML Inline advisor's FunctionPropertiesInfo cache"), 59 cl::init(false)); 60 61 // clang-format off 62 const std::array<TensorSpec, NumberOfFeatures> llvm::FeatureMap{ 63 #define POPULATE_NAMES(_, NAME) TensorSpec::createSpec<int64_t>(NAME, {1} ), 64 // InlineCost features - these must come first 65 INLINE_COST_FEATURE_ITERATOR(POPULATE_NAMES) 66 #undef POPULATE_NAMES 67 68 // Non-cost features 69 #define POPULATE_NAMES(_, NAME, __) TensorSpec::createSpec<int64_t>(NAME, {1} ), 70 INLINE_FEATURE_ITERATOR(POPULATE_NAMES) 71 #undef POPULATE_NAMES 72 }; 73 // clang-format on 74 75 const char *const llvm::DecisionName = "inlining_decision"; 76 const char *const llvm::DefaultDecisionName = "inlining_default"; 77 const char *const llvm::RewardName = "delta_size"; 78 79 CallBase *getInlinableCS(Instruction &I) { 80 if (auto *CS = dyn_cast<CallBase>(&I)) 81 if (Function *Callee = CS->getCalledFunction()) { 82 if (!Callee->isDeclaration()) { 83 return CS; 84 } 85 } 86 return nullptr; 87 } 88 89 MLInlineAdvisor::MLInlineAdvisor(Module &M, ModuleAnalysisManager &MAM, 90 std::unique_ptr<MLModelRunner> Runner) 91 : InlineAdvisor( 92 M, MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()), 93 ModelRunner(std::move(Runner)), 94 CG(MAM.getResult<LazyCallGraphAnalysis>(M)), 95 InitialIRSize(getModuleIRSize()), CurrentIRSize(InitialIRSize) { 96 assert(ModelRunner); 97 98 // Extract the 'call site height' feature - the position of a call site 99 // relative to the farthest statically reachable SCC node. We don't mutate 100 // this value while inlining happens. Empirically, this feature proved 101 // critical in behavioral cloning - i.e. training a model to mimic the manual 102 // heuristic's decisions - and, thus, equally important for training for 103 // improvement. 104 CallGraph CGraph(M); 105 for (auto I = scc_begin(&CGraph); !I.isAtEnd(); ++I) { 106 const std::vector<CallGraphNode *> &CGNodes = *I; 107 unsigned Level = 0; 108 for (auto *CGNode : CGNodes) { 109 Function *F = CGNode->getFunction(); 110 if (!F || F->isDeclaration()) 111 continue; 112 for (auto &I : instructions(F)) { 113 if (auto *CS = getInlinableCS(I)) { 114 auto *Called = CS->getCalledFunction(); 115 auto Pos = FunctionLevels.find(&CG.get(*Called)); 116 // In bottom up traversal, an inlinable callee is either in the 117 // same SCC, or to a function in a visited SCC. So not finding its 118 // level means we haven't visited it yet, meaning it's in this SCC. 119 if (Pos == FunctionLevels.end()) 120 continue; 121 Level = std::max(Level, Pos->second + 1); 122 } 123 } 124 } 125 for (auto *CGNode : CGNodes) { 126 Function *F = CGNode->getFunction(); 127 if (F && !F->isDeclaration()) 128 FunctionLevels[&CG.get(*F)] = Level; 129 } 130 } 131 for (auto KVP : FunctionLevels) { 132 AllNodes.insert(KVP.first); 133 EdgeCount += getLocalCalls(KVP.first->getFunction()); 134 } 135 NodeCount = AllNodes.size(); 136 } 137 138 unsigned MLInlineAdvisor::getInitialFunctionLevel(const Function &F) const { 139 return CG.lookup(F) ? FunctionLevels.at(CG.lookup(F)) : 0; 140 } 141 142 void MLInlineAdvisor::onPassEntry(LazyCallGraph::SCC *LastSCC) { 143 if (!LastSCC || ForceStop) 144 return; 145 FPICache.clear(); 146 // Function passes executed between InlinerPass runs may have changed the 147 // module-wide features. 148 // The cgscc pass manager rules are such that: 149 // - if a pass leads to merging SCCs, then the pipeline is restarted on the 150 // merged SCC 151 // - if a pass leads to splitting the SCC, then we continue with one of the 152 // splits 153 // This means that the NodesInLastSCC is a superset (not strict) of the nodes 154 // that subsequent passes would have processed 155 // - in addition, if new Nodes were created by a pass (e.g. CoroSplit), 156 // they'd be adjacent to Nodes in the last SCC. So we just need to check the 157 // boundary of Nodes in NodesInLastSCC for Nodes we haven't seen. We don't 158 // care about the nature of the Edge (call or ref). 159 NodeCount -= static_cast<int64_t>(NodesInLastSCC.size()); 160 while (!NodesInLastSCC.empty()) { 161 const auto *N = *NodesInLastSCC.begin(); 162 NodesInLastSCC.erase(N); 163 // The Function wrapped by N could have been deleted since we last saw it. 164 if (N->isDead()) { 165 assert(!N->getFunction().isDeclaration()); 166 continue; 167 } 168 ++NodeCount; 169 EdgeCount += getLocalCalls(N->getFunction()); 170 for (const auto &E : *(*N)) { 171 const auto *AdjNode = &E.getNode(); 172 assert(!AdjNode->isDead() && !AdjNode->getFunction().isDeclaration()); 173 auto I = AllNodes.insert(AdjNode); 174 if (I.second) 175 NodesInLastSCC.insert(AdjNode); 176 } 177 } 178 179 EdgeCount -= EdgesOfLastSeenNodes; 180 EdgesOfLastSeenNodes = 0; 181 182 // (Re)use NodesInLastSCC to remember the nodes in the SCC right now, 183 // in case the SCC is split before onPassExit and some nodes are split out 184 assert(NodesInLastSCC.empty()); 185 for (const auto &N : *LastSCC) 186 NodesInLastSCC.insert(&N); 187 } 188 189 void MLInlineAdvisor::onPassExit(LazyCallGraph::SCC *LastSCC) { 190 // No need to keep this around - function passes will invalidate it. 191 if (!KeepFPICache) 192 FPICache.clear(); 193 if (!LastSCC || ForceStop) 194 return; 195 // Keep track of the nodes and edges we last saw. Then, in onPassEntry, 196 // we update the node count and edge count from the subset of these nodes that 197 // survived. 198 EdgesOfLastSeenNodes = 0; 199 200 // Check on nodes that were in SCC onPassEntry 201 for (auto I = NodesInLastSCC.begin(); I != NodesInLastSCC.end();) { 202 if ((*I)->isDead()) 203 NodesInLastSCC.erase(*I++); 204 else 205 EdgesOfLastSeenNodes += getLocalCalls((*I++)->getFunction()); 206 } 207 208 // Check on nodes that may have got added to SCC 209 for (const auto &N : *LastSCC) { 210 assert(!N.isDead()); 211 auto I = NodesInLastSCC.insert(&N); 212 if (I.second) 213 EdgesOfLastSeenNodes += getLocalCalls(N.getFunction()); 214 } 215 assert(NodeCount >= NodesInLastSCC.size()); 216 assert(EdgeCount >= EdgesOfLastSeenNodes); 217 } 218 219 int64_t MLInlineAdvisor::getLocalCalls(Function &F) { 220 return getCachedFPI(F).DirectCallsToDefinedFunctions; 221 } 222 223 // Update the internal state of the advisor, and force invalidate feature 224 // analysis. Currently, we maintain minimal (and very simple) global state - the 225 // number of functions and the number of static calls. We also keep track of the 226 // total IR size in this module, to stop misbehaving policies at a certain bloat 227 // factor (SizeIncreaseThreshold) 228 void MLInlineAdvisor::onSuccessfulInlining(const MLInlineAdvice &Advice, 229 bool CalleeWasDeleted) { 230 assert(!ForceStop); 231 Function *Caller = Advice.getCaller(); 232 Function *Callee = Advice.getCallee(); 233 // The caller features aren't valid anymore. 234 { 235 PreservedAnalyses PA = PreservedAnalyses::none(); 236 FAM.invalidate(*Caller, PA); 237 } 238 Advice.updateCachedCallerFPI(FAM); 239 int64_t IRSizeAfter = 240 getIRSize(*Caller) + (CalleeWasDeleted ? 0 : Advice.CalleeIRSize); 241 CurrentIRSize += IRSizeAfter - (Advice.CallerIRSize + Advice.CalleeIRSize); 242 if (CurrentIRSize > SizeIncreaseThreshold * InitialIRSize) 243 ForceStop = true; 244 245 // We can delta-update module-wide features. We know the inlining only changed 246 // the caller, and maybe the callee (by deleting the latter). 247 // Nodes are simple to update. 248 // For edges, we 'forget' the edges that the caller and callee used to have 249 // before inlining, and add back what they currently have together. 250 int64_t NewCallerAndCalleeEdges = 251 getCachedFPI(*Caller).DirectCallsToDefinedFunctions; 252 253 if (CalleeWasDeleted) 254 --NodeCount; 255 else 256 NewCallerAndCalleeEdges += 257 getCachedFPI(*Callee).DirectCallsToDefinedFunctions; 258 EdgeCount += (NewCallerAndCalleeEdges - Advice.CallerAndCalleeEdges); 259 assert(CurrentIRSize >= 0 && EdgeCount >= 0 && NodeCount >= 0); 260 } 261 262 int64_t MLInlineAdvisor::getModuleIRSize() const { 263 int64_t Ret = 0; 264 for (auto &F : M) 265 if (!F.isDeclaration()) 266 Ret += getIRSize(F); 267 return Ret; 268 } 269 270 FunctionPropertiesInfo &MLInlineAdvisor::getCachedFPI(Function &F) const { 271 auto InsertPair = 272 FPICache.insert(std::make_pair(&F, FunctionPropertiesInfo())); 273 if (!InsertPair.second) 274 return InsertPair.first->second; 275 InsertPair.first->second = FAM.getResult<FunctionPropertiesAnalysis>(F); 276 return InsertPair.first->second; 277 } 278 279 std::unique_ptr<InlineAdvice> MLInlineAdvisor::getAdviceImpl(CallBase &CB) { 280 if (auto Skip = getSkipAdviceIfUnreachableCallsite(CB)) 281 return Skip; 282 283 auto &Caller = *CB.getCaller(); 284 auto &Callee = *CB.getCalledFunction(); 285 286 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & { 287 return FAM.getResult<AssumptionAnalysis>(F); 288 }; 289 auto &TIR = FAM.getResult<TargetIRAnalysis>(Callee); 290 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller); 291 292 auto MandatoryKind = InlineAdvisor::getMandatoryKind(CB, FAM, ORE); 293 // If this is a "never inline" case, there won't be any changes to internal 294 // state we need to track, so we can just return the base InlineAdvice, which 295 // will do nothing interesting. 296 // Same thing if this is a recursive case. 297 if (MandatoryKind == InlineAdvisor::MandatoryInliningKind::Never || 298 &Caller == &Callee) 299 return getMandatoryAdvice(CB, false); 300 301 bool Mandatory = 302 MandatoryKind == InlineAdvisor::MandatoryInliningKind::Always; 303 304 // If we need to stop, we won't want to track anymore any state changes, so 305 // we just return the base InlineAdvice, which acts as a noop. 306 if (ForceStop) { 307 ORE.emit([&] { 308 return OptimizationRemarkMissed(DEBUG_TYPE, "ForceStop", &CB) 309 << "Won't attempt inlining because module size grew too much."; 310 }); 311 return std::make_unique<InlineAdvice>(this, CB, ORE, Mandatory); 312 } 313 314 int CostEstimate = 0; 315 if (!Mandatory) { 316 auto IsCallSiteInlinable = 317 llvm::getInliningCostEstimate(CB, TIR, GetAssumptionCache); 318 if (!IsCallSiteInlinable) { 319 // We can't inline this for correctness reasons, so return the base 320 // InlineAdvice, as we don't care about tracking any state changes (which 321 // won't happen). 322 return std::make_unique<InlineAdvice>(this, CB, ORE, false); 323 } 324 CostEstimate = *IsCallSiteInlinable; 325 } 326 327 const auto CostFeatures = 328 llvm::getInliningCostFeatures(CB, TIR, GetAssumptionCache); 329 if (!CostFeatures) { 330 return std::make_unique<InlineAdvice>(this, CB, ORE, false); 331 } 332 333 if (Mandatory) 334 return getMandatoryAdvice(CB, true); 335 336 auto NrCtantParams = 0; 337 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) { 338 NrCtantParams += (isa<Constant>(*I)); 339 } 340 341 auto &CallerBefore = getCachedFPI(Caller); 342 auto &CalleeBefore = getCachedFPI(Callee); 343 344 *ModelRunner->getTensor<int64_t>(FeatureIndex::CalleeBasicBlockCount) = 345 CalleeBefore.BasicBlockCount; 346 *ModelRunner->getTensor<int64_t>(FeatureIndex::CallSiteHeight) = 347 getInitialFunctionLevel(Caller); 348 *ModelRunner->getTensor<int64_t>(FeatureIndex::NodeCount) = NodeCount; 349 *ModelRunner->getTensor<int64_t>(FeatureIndex::NrCtantParams) = NrCtantParams; 350 *ModelRunner->getTensor<int64_t>(FeatureIndex::EdgeCount) = EdgeCount; 351 *ModelRunner->getTensor<int64_t>(FeatureIndex::CallerUsers) = 352 CallerBefore.Uses; 353 *ModelRunner->getTensor<int64_t>( 354 FeatureIndex::CallerConditionallyExecutedBlocks) = 355 CallerBefore.BlocksReachedFromConditionalInstruction; 356 *ModelRunner->getTensor<int64_t>(FeatureIndex::CallerBasicBlockCount) = 357 CallerBefore.BasicBlockCount; 358 *ModelRunner->getTensor<int64_t>( 359 FeatureIndex::CalleeConditionallyExecutedBlocks) = 360 CalleeBefore.BlocksReachedFromConditionalInstruction; 361 *ModelRunner->getTensor<int64_t>(FeatureIndex::CalleeUsers) = 362 CalleeBefore.Uses; 363 *ModelRunner->getTensor<int64_t>(FeatureIndex::CostEstimate) = CostEstimate; 364 365 // Add the cost features 366 for (size_t I = 0; 367 I < static_cast<size_t>(InlineCostFeatureIndex::NumberOfFeatures); ++I) { 368 *ModelRunner->getTensor<int64_t>(inlineCostFeatureToMlFeature( 369 static_cast<InlineCostFeatureIndex>(I))) = CostFeatures->at(I); 370 } 371 372 return getAdviceFromModel(CB, ORE); 373 } 374 375 std::unique_ptr<MLInlineAdvice> 376 MLInlineAdvisor::getAdviceFromModel(CallBase &CB, 377 OptimizationRemarkEmitter &ORE) { 378 return std::make_unique<MLInlineAdvice>( 379 this, CB, ORE, static_cast<bool>(ModelRunner->evaluate<int64_t>())); 380 } 381 382 std::unique_ptr<InlineAdvice> 383 MLInlineAdvisor::getSkipAdviceIfUnreachableCallsite(CallBase &CB) { 384 if (!FAM.getResult<DominatorTreeAnalysis>(*CB.getCaller()) 385 .isReachableFromEntry(CB.getParent())) 386 return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), false); 387 return nullptr; 388 } 389 390 std::unique_ptr<InlineAdvice> MLInlineAdvisor::getMandatoryAdvice(CallBase &CB, 391 bool Advice) { 392 // Make sure we track inlinings in all cases - mandatory or not. 393 if (auto Skip = getSkipAdviceIfUnreachableCallsite(CB)) 394 return Skip; 395 if (Advice && !ForceStop) 396 return getMandatoryAdviceImpl(CB); 397 398 // If this is a "never inline" case, there won't be any changes to internal 399 // state we need to track, so we can just return the base InlineAdvice, which 400 // will do nothing interesting. 401 // Same if we are forced to stop - we don't track anymore. 402 return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), Advice); 403 } 404 405 std::unique_ptr<MLInlineAdvice> 406 MLInlineAdvisor::getMandatoryAdviceImpl(CallBase &CB) { 407 return std::make_unique<MLInlineAdvice>(this, CB, getCallerORE(CB), true); 408 } 409 410 void MLInlineAdvisor::print(raw_ostream &OS) const { 411 OS << "[MLInlineAdvisor] Nodes: " << NodeCount << " Edges: " << EdgeCount 412 << " EdgesOfLastSeenNodes: " << EdgesOfLastSeenNodes << "\n"; 413 OS << "[MLInlineAdvisor] FPI:\n"; 414 for (auto I : FPICache) { 415 OS << I.getFirst()->getName() << ":\n"; 416 I.getSecond().print(OS); 417 OS << "\n"; 418 } 419 OS << "\n"; 420 } 421 422 MLInlineAdvice::MLInlineAdvice(MLInlineAdvisor *Advisor, CallBase &CB, 423 OptimizationRemarkEmitter &ORE, 424 bool Recommendation) 425 : InlineAdvice(Advisor, CB, ORE, Recommendation), 426 CallerIRSize(Advisor->isForcedToStop() ? 0 : Advisor->getIRSize(*Caller)), 427 CalleeIRSize(Advisor->isForcedToStop() ? 0 : Advisor->getIRSize(*Callee)), 428 CallerAndCalleeEdges(Advisor->isForcedToStop() 429 ? 0 430 : (Advisor->getLocalCalls(*Caller) + 431 Advisor->getLocalCalls(*Callee))), 432 PreInlineCallerFPI(Advisor->getCachedFPI(*Caller)) { 433 if (Recommendation) 434 FPU.emplace(Advisor->getCachedFPI(*getCaller()), CB); 435 } 436 437 void MLInlineAdvice::reportContextForRemark( 438 DiagnosticInfoOptimizationBase &OR) { 439 using namespace ore; 440 OR << NV("Callee", Callee->getName()); 441 for (size_t I = 0; I < NumberOfFeatures; ++I) 442 OR << NV(FeatureMap[I].name(), 443 *getAdvisor()->getModelRunner().getTensor<int64_t>(I)); 444 OR << NV("ShouldInline", isInliningRecommended()); 445 } 446 447 void MLInlineAdvice::updateCachedCallerFPI(FunctionAnalysisManager &FAM) const { 448 FPU->finish(FAM); 449 } 450 451 void MLInlineAdvice::recordInliningImpl() { 452 ORE.emit([&]() { 453 OptimizationRemark R(DEBUG_TYPE, "InliningSuccess", DLoc, Block); 454 reportContextForRemark(R); 455 return R; 456 }); 457 getAdvisor()->onSuccessfulInlining(*this, /*CalleeWasDeleted*/ false); 458 } 459 460 void MLInlineAdvice::recordInliningWithCalleeDeletedImpl() { 461 ORE.emit([&]() { 462 OptimizationRemark R(DEBUG_TYPE, "InliningSuccessWithCalleeDeleted", DLoc, 463 Block); 464 reportContextForRemark(R); 465 return R; 466 }); 467 getAdvisor()->onSuccessfulInlining(*this, /*CalleeWasDeleted*/ true); 468 } 469 470 void MLInlineAdvice::recordUnsuccessfulInliningImpl( 471 const InlineResult &Result) { 472 getAdvisor()->getCachedFPI(*Caller) = PreInlineCallerFPI; 473 ORE.emit([&]() { 474 OptimizationRemarkMissed R(DEBUG_TYPE, "InliningAttemptedAndUnsuccessful", 475 DLoc, Block); 476 reportContextForRemark(R); 477 return R; 478 }); 479 } 480 void MLInlineAdvice::recordUnattemptedInliningImpl() { 481 assert(!FPU); 482 ORE.emit([&]() { 483 OptimizationRemarkMissed R(DEBUG_TYPE, "IniningNotAttempted", DLoc, Block); 484 reportContextForRemark(R); 485 return R; 486 }); 487 } 488