1577e58bcSWenlei He //===- ReplayInlineAdvisor.cpp - Replay InlineAdvisor ---------------------===//
2577e58bcSWenlei He //
3577e58bcSWenlei He // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4577e58bcSWenlei He // See https://llvm.org/LICENSE.txt for license information.
5577e58bcSWenlei He // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6577e58bcSWenlei He //
7577e58bcSWenlei He //===----------------------------------------------------------------------===//
8577e58bcSWenlei He //
92a49b7c6Smodimo // This file implements ReplayInlineAdvisor that replays inline decisions based
102a49b7c6Smodimo // on previous inline remarks from optimization remark log. This is a best
112a49b7c6Smodimo // effort approach useful for testing compiler/source changes while holding
122a49b7c6Smodimo // inlining steady.
13577e58bcSWenlei He //
14577e58bcSWenlei He //===----------------------------------------------------------------------===//
15577e58bcSWenlei He
16577e58bcSWenlei He #include "llvm/Analysis/ReplayInlineAdvisor.h"
1771c3a551Sserge-sans-paille #include "llvm/Analysis/OptimizationRemarkEmitter.h"
18577e58bcSWenlei He #include "llvm/Support/LineIterator.h"
1971c3a551Sserge-sans-paille #include "llvm/Support/MemoryBuffer.h"
20313c657fSmodimo #include <memory>
21577e58bcSWenlei He
22577e58bcSWenlei He using namespace llvm;
23577e58bcSWenlei He
24313c657fSmodimo #define DEBUG_TYPE "replay-inline"
25577e58bcSWenlei He
ReplayInlineAdvisor(Module & M,FunctionAnalysisManager & FAM,LLVMContext & Context,std::unique_ptr<InlineAdvisor> OriginalAdvisor,const ReplayInlinerSettings & ReplaySettings,bool EmitRemarks,InlineContext IC)26ce7f9cdbSmodimo ReplayInlineAdvisor::ReplayInlineAdvisor(
27ce7f9cdbSmodimo Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context,
285caad9b5Smodimo std::unique_ptr<InlineAdvisor> OriginalAdvisor,
29*e0d06959SMingming Liu const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks,
30*e0d06959SMingming Liu InlineContext IC)
31*e0d06959SMingming Liu : InlineAdvisor(M, FAM, IC), OriginalAdvisor(std::move(OriginalAdvisor)),
32b752eb88SKazu Hirata ReplaySettings(ReplaySettings), EmitRemarks(EmitRemarks) {
33313c657fSmodimo
345caad9b5Smodimo auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(ReplaySettings.ReplayFile);
35577e58bcSWenlei He std::error_code EC = BufferOrErr.getError();
36577e58bcSWenlei He if (EC) {
37577e58bcSWenlei He Context.emitError("Could not open remarks file: " + EC.message());
38577e58bcSWenlei He return;
39577e58bcSWenlei He }
40577e58bcSWenlei He
41577e58bcSWenlei He // Example for inline remarks to parse:
42313c657fSmodimo // main:3:1.1: '_Z3subii' inlined into 'main' at callsite sum:1 @
43313c657fSmodimo // main:3:1.1;
44577e58bcSWenlei He // We use the callsite string after `at callsite` to replay inlining.
45577e58bcSWenlei He line_iterator LineIt(*BufferOrErr.get(), /*SkipBlanks=*/true);
465caad9b5Smodimo const std::string PositiveRemark = "' inlined into '";
475caad9b5Smodimo const std::string NegativeRemark = "' will not be inlined into '";
485caad9b5Smodimo
49577e58bcSWenlei He for (; !LineIt.is_at_eof(); ++LineIt) {
50577e58bcSWenlei He StringRef Line = *LineIt;
51577e58bcSWenlei He auto Pair = Line.split(" at callsite ");
522a49b7c6Smodimo
535caad9b5Smodimo bool IsPositiveRemark = true;
545caad9b5Smodimo if (Pair.first.contains(NegativeRemark))
555caad9b5Smodimo IsPositiveRemark = false;
565caad9b5Smodimo
575caad9b5Smodimo auto CalleeCaller =
585caad9b5Smodimo Pair.first.split(IsPositiveRemark ? PositiveRemark : NegativeRemark);
59313c657fSmodimo
60313c657fSmodimo StringRef Callee = CalleeCaller.first.rsplit(": '").second;
61313c657fSmodimo StringRef Caller = CalleeCaller.second.rsplit("'").first;
62313c657fSmodimo
632a49b7c6Smodimo auto CallSite = Pair.second.split(";").first;
642a49b7c6Smodimo
65313c657fSmodimo if (Callee.empty() || Caller.empty() || CallSite.empty()) {
66313c657fSmodimo Context.emitError("Invalid remark format: " + Line);
67313c657fSmodimo return;
68313c657fSmodimo }
692a49b7c6Smodimo
702a49b7c6Smodimo std::string Combined = (Callee + CallSite).str();
715caad9b5Smodimo InlineSitesFromRemarks[Combined] = IsPositiveRemark;
725caad9b5Smodimo if (ReplaySettings.ReplayScope == ReplayInlinerSettings::Scope::Function)
73313c657fSmodimo CallersToReplay.insert(Caller);
74577e58bcSWenlei He }
752a49b7c6Smodimo
76577e58bcSWenlei He HasReplayRemarks = true;
77577e58bcSWenlei He }
78577e58bcSWenlei He
79*e0d06959SMingming Liu std::unique_ptr<InlineAdvisor>
getReplayInlineAdvisor(Module & M,FunctionAnalysisManager & FAM,LLVMContext & Context,std::unique_ptr<InlineAdvisor> OriginalAdvisor,const ReplayInlinerSettings & ReplaySettings,bool EmitRemarks,InlineContext IC)80*e0d06959SMingming Liu llvm::getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
81*e0d06959SMingming Liu LLVMContext &Context,
825caad9b5Smodimo std::unique_ptr<InlineAdvisor> OriginalAdvisor,
83*e0d06959SMingming Liu const ReplayInlinerSettings &ReplaySettings,
84*e0d06959SMingming Liu bool EmitRemarks, InlineContext IC) {
85313c657fSmodimo auto Advisor = std::make_unique<ReplayInlineAdvisor>(
86*e0d06959SMingming Liu M, FAM, Context, std::move(OriginalAdvisor), ReplaySettings, EmitRemarks,
87*e0d06959SMingming Liu IC);
88313c657fSmodimo if (!Advisor->areReplayRemarksLoaded())
89313c657fSmodimo Advisor.reset();
90313c657fSmodimo return Advisor;
91313c657fSmodimo }
92313c657fSmodimo
getAdviceImpl(CallBase & CB)93e8049dc3SMircea Trofin std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdviceImpl(CallBase &CB) {
94577e58bcSWenlei He assert(HasReplayRemarks);
95577e58bcSWenlei He
96577e58bcSWenlei He Function &Caller = *CB.getCaller();
97577e58bcSWenlei He auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
98577e58bcSWenlei He
995caad9b5Smodimo // Decision not made by replay system
1005caad9b5Smodimo if (!hasInlineAdvice(*CB.getFunction())) {
1015caad9b5Smodimo // If there's a registered original advisor, return its decision
102313c657fSmodimo if (OriginalAdvisor)
103313c657fSmodimo return OriginalAdvisor->getAdvice(CB);
1045caad9b5Smodimo
1055caad9b5Smodimo // If no decision is made above, return non-decision
106313c657fSmodimo return {};
1072a49b7c6Smodimo }
1082a49b7c6Smodimo
1095caad9b5Smodimo std::string CallSiteLoc =
1105caad9b5Smodimo formatCallSiteLocation(CB.getDebugLoc(), ReplaySettings.ReplayFormat);
1115caad9b5Smodimo StringRef Callee = CB.getCalledFunction()->getName();
1125caad9b5Smodimo std::string Combined = (Callee + CallSiteLoc).str();
1135caad9b5Smodimo
1145caad9b5Smodimo // Replay decision, if it has one
1155caad9b5Smodimo auto Iter = InlineSitesFromRemarks.find(Combined);
1165caad9b5Smodimo if (Iter != InlineSitesFromRemarks.end()) {
1175caad9b5Smodimo if (InlineSitesFromRemarks[Combined]) {
1185caad9b5Smodimo LLVM_DEBUG(dbgs() << "Replay Inliner: Inlined " << Callee << " @ "
1195caad9b5Smodimo << CallSiteLoc << "\n");
1205caad9b5Smodimo return std::make_unique<DefaultInlineAdvice>(
1215caad9b5Smodimo this, CB, llvm::InlineCost::getAlways("previously inlined"), ORE,
1222a49b7c6Smodimo EmitRemarks);
1235caad9b5Smodimo } else {
1245caad9b5Smodimo LLVM_DEBUG(dbgs() << "Replay Inliner: Not Inlined " << Callee << " @ "
1255caad9b5Smodimo << CallSiteLoc << "\n");
1265caad9b5Smodimo // A negative inline is conveyed by "None" Optional<InlineCost>
1275caad9b5Smodimo return std::make_unique<DefaultInlineAdvice>(this, CB, None, ORE,
1285caad9b5Smodimo EmitRemarks);
1295caad9b5Smodimo }
1305caad9b5Smodimo }
1315caad9b5Smodimo
1325caad9b5Smodimo // Fallback decisions
1335caad9b5Smodimo if (ReplaySettings.ReplayFallback ==
1345caad9b5Smodimo ReplayInlinerSettings::Fallback::AlwaysInline)
1355caad9b5Smodimo return std::make_unique<DefaultInlineAdvice>(
1365caad9b5Smodimo this, CB, llvm::InlineCost::getAlways("AlwaysInline Fallback"), ORE,
1375caad9b5Smodimo EmitRemarks);
1385caad9b5Smodimo else if (ReplaySettings.ReplayFallback ==
1395caad9b5Smodimo ReplayInlinerSettings::Fallback::NeverInline)
1405caad9b5Smodimo // A negative inline is conveyed by "None" Optional<InlineCost>
1415caad9b5Smodimo return std::make_unique<DefaultInlineAdvice>(this, CB, None, ORE,
1425caad9b5Smodimo EmitRemarks);
1435caad9b5Smodimo else {
1445caad9b5Smodimo assert(ReplaySettings.ReplayFallback ==
1455caad9b5Smodimo ReplayInlinerSettings::Fallback::Original);
1465caad9b5Smodimo // If there's a registered original advisor, return its decision
1475caad9b5Smodimo if (OriginalAdvisor)
1485caad9b5Smodimo return OriginalAdvisor->getAdvice(CB);
1495caad9b5Smodimo }
1505caad9b5Smodimo
1515caad9b5Smodimo // If no decision is made above, return non-decision
1525caad9b5Smodimo return {};
153577e58bcSWenlei He }
154