LLVM 20.0.0git
ReplayInlineAdvisor.cpp
Go to the documentation of this file.
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
20#include <memory>
21
22using namespace llvm;
23
24#define DEBUG_TYPE "replay-inline"
25
28 std::unique_ptr<InlineAdvisor> OriginalAdvisor,
29 const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks,
31 : InlineAdvisor(M, FAM, IC), OriginalAdvisor(std::move(OriginalAdvisor)),
32 ReplaySettings(ReplaySettings), EmitRemarks(EmitRemarks) {
33
34 auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(ReplaySettings.ReplayFile);
35 std::error_code EC = BufferOrErr.getError();
36 if (EC) {
37 Context.emitError("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 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(" at callsite ");
52
53 bool IsPositiveRemark = true;
54 if (Pair.first.contains(NegativeRemark))
55 IsPositiveRemark = false;
56
57 auto CalleeCaller =
58 Pair.first.split(IsPositiveRemark ? PositiveRemark : NegativeRemark);
59
60 StringRef Callee = CalleeCaller.first.rsplit(": '").second;
61 StringRef Caller = CalleeCaller.second.rsplit("'").first;
62
63 auto CallSite = Pair.second.split(";").first;
64
65 if (Callee.empty() || Caller.empty() || CallSite.empty()) {
66 Context.emitError("Invalid remark format: " + Line);
67 return;
68 }
69
70 std::string Combined = (Callee + CallSite).str();
71 InlineSitesFromRemarks[Combined] = IsPositiveRemark;
73 CallersToReplay.insert(Caller);
74 }
75
76 HasReplayRemarks = true;
77}
78
79std::unique_ptr<InlineAdvisor>
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 M, FAM, Context, std::move(OriginalAdvisor), ReplaySettings, EmitRemarks,
87 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();
98
99 // Decision not made by replay system
100 if (!hasInlineAdvice(*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(CB.getDebugLoc(), 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(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 this, CB, llvm::InlineCost::getAlways("previously inlined"), ORE,
122 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>(this, CB, std::nullopt, ORE,
128 EmitRemarks);
129 }
130 }
131
132 // Fallback decisions
133 if (ReplaySettings.ReplayFallback ==
135 return std::make_unique<DefaultInlineAdvice>(
136 this, CB, llvm::InlineCost::getAlways("AlwaysInline Fallback"), ORE,
137 EmitRemarks);
138 else if (ReplaySettings.ReplayFallback ==
140 // A negative inline is conveyed by "None" std::optional<InlineCost>
141 return std::make_unique<DefaultInlineAdvice>(this, CB, std::nullopt, ORE,
142 EmitRemarks);
143 else {
144 assert(ReplaySettings.ReplayFallback ==
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}
#define LLVM_DEBUG(X)
Definition: Debug.h:101
FunctionAnalysisManager FAM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1465
Function * getCaller()
Helper to get the caller (the parent function).
Interface for deciding whether to inline a call site or not.
FunctionAnalysisManager & FAM
static InlineCost getAlways(const char *Reason, std::optional< CostBenefitPair > CostBenefit=std::nullopt)
Definition: InlineCost.h:126
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:466
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
void emitError(uint64_t LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
ReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context, std::unique_ptr< InlineAdvisor > OriginalAdvisor, const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks, InlineContext IC)
std::unique_ptr< InlineAdvice > getAdviceImpl(CallBase &CB) override
iterator end()
Definition: StringMap.h:220
iterator find(StringRef Key)
Definition: StringMap.h:233
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:38
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
std::unique_ptr< InlineAdvisor > getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context, std::unique_ptr< InlineAdvisor > OriginalAdvisor, const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks, InlineContext IC)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1856
std::string formatCallSiteLocation(DebugLoc DLoc, const CallSiteFormat &Format)
Get call site location as a string with the given format.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Provides context on when an inline advisor is constructed in the pipeline (e.g., link phase,...
Definition: InlineAdvisor.h:58