1//===- ReplayInlineAdvisor.cpp - Replay 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 ReplayInlineAdvisor that replays inline decisions based
10// on previous inline remarks from optimization remark log. This is a best
11// effort approach useful for testing compiler/source changes while holding
12// inlining steady.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/ReplayInlineAdvisor.h"
17#include "llvm/Analysis/OptimizationRemarkEmitter.h"
18#include "llvm/Support/ErrorHandling.h"
19#include "llvm/Support/LineIterator.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include <memory>
22
23using namespace llvm;
24
25#define DEBUG_TYPE "replay-inline"
26
27ReplayInlineAdvisor::ReplayInlineAdvisor(
28 Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context,
29 std::unique_ptr<InlineAdvisor> OriginalAdvisor,
30 const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks,
31 InlineContext IC)
32 : InlineAdvisor(M, FAM, IC), OriginalAdvisor(std::move(OriginalAdvisor)),
33 ReplaySettings(ReplaySettings), EmitRemarks(EmitRemarks) {
34
35 auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename: ReplaySettings.ReplayFile);
36 std::error_code EC = BufferOrErr.getError();
37 if (EC)
38 reportFatalUsageError(reason: "could not open remarks file: " +
39 Twine(EC.message()));
40
41 // Example for inline remarks to parse:
42 // main:3:1.1: '_Z3subii' inlined into 'main' at callsite sum:1 @
43 // main:3:1.1;
44 // We use the callsite string after `at callsite` to replay inlining.
45 line_iterator LineIt(*BufferOrErr.get(), /*SkipBlanks=*/true);
46 const std::string PositiveRemark = "' inlined into '";
47 const std::string NegativeRemark = "' will not be inlined into '";
48
49 for (; !LineIt.is_at_eof(); ++LineIt) {
50 StringRef Line = *LineIt;
51 auto Pair = Line.split(Separator: " at callsite ");
52
53 bool IsPositiveRemark = true;
54 if (Pair.first.contains(Other: NegativeRemark))
55 IsPositiveRemark = false;
56
57 auto CalleeCaller =
58 Pair.first.split(Separator: IsPositiveRemark ? PositiveRemark : NegativeRemark);
59
60 StringRef Callee = CalleeCaller.first.rsplit(Separator: ": '").second;
61 StringRef Caller = CalleeCaller.second.rsplit(Separator: "'").first;
62
63 auto CallSite = Pair.second.split(Separator: ";").first;
64
65 if (Callee.empty() || Caller.empty() || CallSite.empty())
66 reportFatalUsageError(reason: "invalid remark format: " + Twine(Line));
67
68 std::string Combined = (Callee + CallSite).str();
69 InlineSitesFromRemarks[Combined] = IsPositiveRemark;
70 if (ReplaySettings.ReplayScope == ReplayInlinerSettings::Scope::Function)
71 CallersToReplay.insert(key: Caller);
72 }
73
74 HasReplayRemarks = true;
75}
76
77std::unique_ptr<InlineAdvisor>
78llvm::getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
79 LLVMContext &Context,
80 std::unique_ptr<InlineAdvisor> OriginalAdvisor,
81 const ReplayInlinerSettings &ReplaySettings,
82 bool EmitRemarks, InlineContext IC) {
83 auto Advisor = std::make_unique<ReplayInlineAdvisor>(
84 args&: M, args&: FAM, args&: Context, args: std::move(OriginalAdvisor), args: ReplaySettings, args&: EmitRemarks,
85 args&: IC);
86 if (!Advisor->areReplayRemarksLoaded())
87 Advisor.reset();
88 return Advisor;
89}
90
91std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdviceImpl(CallBase &CB) {
92 assert(HasReplayRemarks);
93
94 Function &Caller = *CB.getCaller();
95 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(IR&: Caller);
96
97 // Decision not made by replay system. Remarks only name direct callees, so
98 // there is no replay decision for an indirect call either.
99 if (!hasInlineAdvice(F&: *CB.getFunction()) || !CB.getCalledFunction()) {
100 // If there's a registered original advisor, return its decision
101 if (OriginalAdvisor)
102 return OriginalAdvisor->getAdvice(CB);
103
104 // If no decision is made above, return non-decision
105 return {};
106 }
107
108 std::string CallSiteLoc =
109 formatCallSiteLocation(DLoc: CB.getDebugLoc(), Format: ReplaySettings.ReplayFormat);
110 StringRef Callee = CB.getCalledFunction()->getName();
111 std::string Combined = (Callee + CallSiteLoc).str();
112
113 // Replay decision, if it has one
114 auto Iter = InlineSitesFromRemarks.find(Key: Combined);
115 if (Iter != InlineSitesFromRemarks.end()) {
116 if (Iter->second) {
117 LLVM_DEBUG(dbgs() << "Replay Inliner: Inlined " << Callee << " @ "
118 << CallSiteLoc << "\n");
119 return std::make_unique<DefaultInlineAdvice>(
120 args: this, args&: CB, args: llvm::InlineCost::getAlways(Reason: "previously inlined"), args&: ORE,
121 args&: EmitRemarks);
122 } else {
123 LLVM_DEBUG(dbgs() << "Replay Inliner: Not Inlined " << Callee << " @ "
124 << CallSiteLoc << "\n");
125 // A negative inline is conveyed by "None" std::optional<InlineCost>
126 return std::make_unique<DefaultInlineAdvice>(args: this, args&: CB, args: std::nullopt, args&: ORE,
127 args&: EmitRemarks);
128 }
129 }
130
131 // Fallback decisions
132 if (ReplaySettings.ReplayFallback ==
133 ReplayInlinerSettings::Fallback::AlwaysInline)
134 return std::make_unique<DefaultInlineAdvice>(
135 args: this, args&: CB, args: llvm::InlineCost::getAlways(Reason: "AlwaysInline Fallback"), args&: ORE,
136 args&: EmitRemarks);
137 else if (ReplaySettings.ReplayFallback ==
138 ReplayInlinerSettings::Fallback::NeverInline)
139 // A negative inline is conveyed by "None" std::optional<InlineCost>
140 return std::make_unique<DefaultInlineAdvice>(args: this, args&: CB, args: std::nullopt, args&: ORE,
141 args&: EmitRemarks);
142 else {
143 assert(ReplaySettings.ReplayFallback ==
144 ReplayInlinerSettings::Fallback::Original);
145 // If there's a registered original advisor, return its decision
146 if (OriginalAdvisor)
147 return OriginalAdvisor->getAdvice(CB);
148 }
149
150 // If no decision is made above, return non-decision
151 return {};
152}
153