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/IR/DebugInfoMetadata.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/Support/LineIterator.h"
20 #include <memory>
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "replay-inline"
25 
26 ReplayInlineAdvisor::ReplayInlineAdvisor(
27     Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context,
28     std::unique_ptr<InlineAdvisor> OriginalAdvisor, StringRef RemarksFile,
29     ReplayInlineScope Scope, bool EmitRemarks)
30     : InlineAdvisor(M, FAM), OriginalAdvisor(std::move(OriginalAdvisor)),
31       HasReplayRemarks(false), Scope(Scope), EmitRemarks(EmitRemarks) {
32 
33   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(RemarksFile);
34   std::error_code EC = BufferOrErr.getError();
35   if (EC) {
36     Context.emitError("Could not open remarks file: " + EC.message());
37     return;
38   }
39 
40   // Example for inline remarks to parse:
41   //   main:3:1.1: '_Z3subii' inlined into 'main' at callsite sum:1 @
42   //   main:3:1.1;
43   // We use the callsite string after `at callsite` to replay inlining.
44   line_iterator LineIt(*BufferOrErr.get(), /*SkipBlanks=*/true);
45   for (; !LineIt.is_at_eof(); ++LineIt) {
46     StringRef Line = *LineIt;
47     auto Pair = Line.split(" at callsite ");
48 
49     auto CalleeCaller = Pair.first.split("' inlined into '");
50 
51     StringRef Callee = CalleeCaller.first.rsplit(": '").second;
52     StringRef Caller = CalleeCaller.second.rsplit("'").first;
53 
54     auto CallSite = Pair.second.split(";").first;
55 
56     if (Callee.empty() || Caller.empty() || CallSite.empty()) {
57       Context.emitError("Invalid remark format: " + Line);
58       return;
59     }
60 
61     std::string Combined = (Callee + CallSite).str();
62     InlineSitesFromRemarks[Combined] = false;
63     if (Scope == ReplayInlineScope::Function)
64       CallersToReplay.insert(Caller);
65   }
66 
67   HasReplayRemarks = true;
68 }
69 
70 std::unique_ptr<InlineAdvisor> llvm::getReplayInlineAdvisor(
71     Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context,
72     std::unique_ptr<InlineAdvisor> OriginalAdvisor, StringRef RemarksFile,
73     ReplayInlineScope Scope, bool EmitRemarks) {
74   auto Advisor = std::make_unique<ReplayInlineAdvisor>(
75       M, FAM, Context, std::move(OriginalAdvisor), RemarksFile, Scope,
76       EmitRemarks);
77   if (!Advisor->areReplayRemarksLoaded())
78     Advisor.reset();
79   return Advisor;
80 }
81 
82 std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdviceImpl(CallBase &CB) {
83   assert(HasReplayRemarks);
84 
85   Function &Caller = *CB.getCaller();
86   auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
87 
88   Optional<InlineCost> InlineRecommended;
89 
90   if (Scope == ReplayInlineScope::Module ||
91       CallersToReplay.count(CB.getFunction()->getName())) {
92     std::string CallSiteLoc = getCallSiteLocation(CB.getDebugLoc());
93     StringRef Callee = CB.getCalledFunction()->getName();
94     std::string Combined = (Callee + CallSiteLoc).str();
95 
96     auto Iter = InlineSitesFromRemarks.find(Combined);
97     if (Iter != InlineSitesFromRemarks.end()) {
98       InlineSitesFromRemarks[Combined] = true;
99       InlineRecommended = llvm::InlineCost::getAlways("previously inlined");
100     }
101   } else if (Scope == ReplayInlineScope::Function) {
102     if (OriginalAdvisor)
103       return OriginalAdvisor->getAdvice(CB);
104     return {};
105   }
106 
107   return std::make_unique<DefaultInlineAdvice>(this, CB, InlineRecommended, ORE,
108                                                EmitRemarks);
109 }
110