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/LineIterator.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include <memory>
21
22using namespace llvm;
23
24#define DEBUG_TYPE "replay-inline"
25
26ReplayInlineAdvisor::ReplayInlineAdvisor(
27 Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context,
28 std::unique_ptr<InlineAdvisor> OriginalAdvisor,
29 const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks,
30 InlineContext IC)
31 : InlineAdvisor(M, FAM, IC), OriginalAdvisor(std::move(OriginalAdvisor)),
32 ReplaySettings(ReplaySettings), EmitRemarks(EmitRemarks) {
33
34 auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename: ReplaySettings.ReplayFile);
35 std::error_code EC = BufferOrErr.getError();
36 if (EC) {
37 Context.emitError(ErrorStr: "Could not open remarks file: " + EC.message());
38 return;
39 }
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 static const std::string PositiveRemark = "' inlined into '";
47 static 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 Context.emitError(ErrorStr: "Invalid remark format: " + Line);
67 return;
68 }
69
70 std::string Combined = (Callee + CallSite).str();
71 InlineSitesFromRemarks[Combined] = IsPositiveRemark;
72 if (ReplaySettings.ReplayScope == ReplayInlinerSettings::Scope::Function)
73 CallersToReplay.insert(key: Caller);
74 }
75
76 HasReplayRemarks = true;
77}
78
79std::unique_ptr<InlineAdvisor>
80llvm::getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
81 LLVMContext &Context,
82 std::unique_ptr<InlineAdvisor> OriginalAdvisor,
83 const ReplayInlinerSettings &ReplaySettings,
84 bool EmitRemarks, InlineContext IC) {
85 auto Advisor = std::make_unique<ReplayInlineAdvisor>(
86 args&: M, args&: FAM, args&: Context, args: std::move(OriginalAdvisor), args: ReplaySettings, args&: EmitRemarks,
87 args&: IC);
88 if (!Advisor->areReplayRemarksLoaded())
89 Advisor.reset();
90 return Advisor;
91}
92
93std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdviceImpl(CallBase &CB) {
94 assert(HasReplayRemarks);
95
96 Function &Caller = *CB.getCaller();
97 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(IR&: Caller);
98
99 // Decision not made by replay system
100 if (!hasInlineAdvice(F&: *CB.getFunction())) {
101 // If there's a registered original advisor, return its decision
102 if (OriginalAdvisor)
103 return OriginalAdvisor->getAdvice(CB);
104
105 // If no decision is made above, return non-decision
106 return {};
107 }
108
109 std::string CallSiteLoc =
110 formatCallSiteLocation(DLoc: CB.getDebugLoc(), Format: ReplaySettings.ReplayFormat);
111 StringRef Callee = CB.getCalledFunction()->getName();
112 std::string Combined = (Callee + CallSiteLoc).str();
113
114 // Replay decision, if it has one
115 auto Iter = InlineSitesFromRemarks.find(Key: Combined);
116 if (Iter != InlineSitesFromRemarks.end()) {
117 if (InlineSitesFromRemarks[Combined]) {
118 LLVM_DEBUG(dbgs() << "Replay Inliner: Inlined " << Callee << " @ "
119 << CallSiteLoc << "\n");
120 return std::make_unique<DefaultInlineAdvice>(
121 args: this, args&: CB, args: llvm::InlineCost::getAlways(Reason: "previously inlined"), args&: ORE,
122 args&: EmitRemarks);
123 } else {
124 LLVM_DEBUG(dbgs() << "Replay Inliner: Not Inlined " << Callee << " @ "
125 << CallSiteLoc << "\n");
126 // A negative inline is conveyed by "None" std::optional<InlineCost>
127 return std::make_unique<DefaultInlineAdvice>(args: this, args&: CB, args: std::nullopt, args&: ORE,
128 args&: EmitRemarks);
129 }
130 }
131
132 // Fallback decisions
133 if (ReplaySettings.ReplayFallback ==
134 ReplayInlinerSettings::Fallback::AlwaysInline)
135 return std::make_unique<DefaultInlineAdvice>(
136 args: this, args&: CB, args: llvm::InlineCost::getAlways(Reason: "AlwaysInline Fallback"), args&: ORE,
137 args&: EmitRemarks);
138 else if (ReplaySettings.ReplayFallback ==
139 ReplayInlinerSettings::Fallback::NeverInline)
140 // A negative inline is conveyed by "None" std::optional<InlineCost>
141 return std::make_unique<DefaultInlineAdvice>(args: this, args&: CB, args: std::nullopt, args&: ORE,
142 args&: EmitRemarks);
143 else {
144 assert(ReplaySettings.ReplayFallback ==
145 ReplayInlinerSettings::Fallback::Original);
146 // If there's a registered original advisor, return its decision
147 if (OriginalAdvisor)
148 return OriginalAdvisor->getAdvice(CB);
149 }
150
151 // If no decision is made above, return non-decision
152 return {};
153}
154

source code of llvm/lib/Analysis/ReplayInlineAdvisor.cpp