LLVM  13.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 
18 #include "llvm/IR/Instructions.h"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "inline-replay"
24 
27  std::unique_ptr<InlineAdvisor> OriginalAdvisor, StringRef RemarksFile,
28  bool EmitRemarks)
29  : InlineAdvisor(M, FAM), OriginalAdvisor(std::move(OriginalAdvisor)),
30  HasReplayRemarks(false), EmitRemarks(EmitRemarks) {
31  auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(RemarksFile);
32  std::error_code EC = BufferOrErr.getError();
33  if (EC) {
34  Context.emitError("Could not open remarks file: " + EC.message());
35  return;
36  }
37 
38  // Example for inline remarks to parse:
39  // main:3:1.1: _Z3subii inlined into main at callsite sum:1 @ main:3:1.1
40  // We use the callsite string after `at callsite` to replay inlining.
41  line_iterator LineIt(*BufferOrErr.get(), /*SkipBlanks=*/true);
42  for (; !LineIt.is_at_eof(); ++LineIt) {
43  StringRef Line = *LineIt;
44  auto Pair = Line.split(" at callsite ");
45 
46  auto Callee = Pair.first.split(" inlined into").first.rsplit(": ").second;
47 
48  auto CallSite = Pair.second.split(";").first;
49 
50  if (Callee.empty() || CallSite.empty())
51  continue;
52 
53  std::string Combined = (Callee + CallSite).str();
54  InlineSitesFromRemarks.insert(Combined);
55  }
56 
57  HasReplayRemarks = true;
58 }
59 
60 std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdviceImpl(CallBase &CB) {
61  assert(HasReplayRemarks);
62 
63  Function &Caller = *CB.getCaller();
64  auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
65 
66  if (InlineSitesFromRemarks.empty())
67  return std::make_unique<DefaultInlineAdvice>(this, CB, None, ORE,
68  EmitRemarks);
69 
70  std::string CallSiteLoc = getCallSiteLocation(CB.getDebugLoc());
72  std::string Combined = (Callee + CallSiteLoc).str();
73  auto Iter = InlineSitesFromRemarks.find(Combined);
74 
75  Optional<InlineCost> InlineRecommended = None;
76  if (Iter != InlineSitesFromRemarks.end()) {
77  InlineRecommended = llvm::InlineCost::getAlways("found in replay");
78  }
79 
80  return std::make_unique<DefaultInlineAdvice>(this, CB, InlineRecommended, ORE,
81  EmitRemarks);
82 }
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::getCallSiteLocation
std::string getCallSiteLocation(DebugLoc DLoc)
get call site location as string
Definition: InlineAdvisor.cpp:383
llvm::line_iterator
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:769
DebugInfoMetadata.h
llvm::Function
Definition: Function.h:61
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::StringMap::end
iterator end()
Definition: StringMap.h:205
llvm::Optional
Definition: APInt.h:33
llvm::StringSet::insert
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:33
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1396
llvm::ReplayInlineAdvisor::ReplayInlineAdvisor
ReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context, std::unique_ptr< InlineAdvisor > OriginalAdvisor, StringRef RemarksFile, bool EmitRemarks)
Definition: ReplayInlineAdvisor.cpp:25
false
Definition: StackSlotColoring.cpp:142
LineIterator.h
llvm::None
const NoneType None
Definition: None.h:23
llvm::CallBase::getCaller
Function * getCaller()
Helper to get the caller (the parent function).
Definition: Instructions.cpp:278
llvm::LLVMContext::emitError
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
Definition: LLVMContext.cpp:251
ReplayInlineAdvisor.h
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:144
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
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:1540
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::InlineAdvisor
Interface for deciding whether to inline a call site or not.
Definition: InlineAdvisor.h:140
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:294
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:206
llvm::InlineAdvisor::FAM
FunctionAnalysisManager & FAM
Definition: InlineAdvisor.h:171
std
Definition: BitVector.h:838
llvm::InlineCost::getAlways
static InlineCost getAlways(const char *Reason)
Definition: InlineCost.h:92
llvm::line_iterator::is_at_eof
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
Instructions.h
llvm::StringMapImpl::empty
bool empty() const
Definition: StringMap.h:92
llvm::Instruction::getDebugLoc
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:370
llvm::ReplayInlineAdvisor::getAdviceImpl
std::unique_ptr< InlineAdvice > getAdviceImpl(CallBase &CB) override
Definition: ReplayInlineAdvisor.cpp:60
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1164
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::OptimizationRemarkEmitterAnalysis
Definition: OptimizationRemarkEmitter.h:164