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 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "inline-replay"
24 
25 ReplayInlineAdvisor::ReplayInlineAdvisor(FunctionAnalysisManager &FAM,
26                                          LLVMContext &Context,
27                                          StringRef RemarksFile,
28                                          bool EmitRemarks)
29     : InlineAdvisor(FAM), HasReplayRemarks(false), EmitRemarks(EmitRemarks) {
30   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(RemarksFile);
31   std::error_code EC = BufferOrErr.getError();
32   if (EC) {
33     Context.emitError("Could not open remarks file: " + EC.message());
34     return;
35   }
36 
37   // Example for inline remarks to parse:
38   //   main:3:1.1: _Z3subii inlined into main at callsite sum:1 @ main:3:1.1
39   // We use the callsite string after `at callsite` to replay inlining.
40   line_iterator LineIt(*BufferOrErr.get(), /*SkipBlanks=*/true);
41   for (; !LineIt.is_at_eof(); ++LineIt) {
42     StringRef Line = *LineIt;
43     auto Pair = Line.split(" at callsite ");
44 
45     auto Callee = Pair.first.split(" inlined into").first.rsplit(": ").second;
46 
47     auto CallSite = Pair.second.split(";").first;
48 
49     if (Callee.empty() || CallSite.empty())
50       continue;
51 
52     std::string Combined = (Callee + CallSite).str();
53     InlineSitesFromRemarks.insert(Combined);
54   }
55 
56   HasReplayRemarks = true;
57 }
58 
59 std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdvice(CallBase &CB) {
60   assert(HasReplayRemarks);
61 
62   Function &Caller = *CB.getCaller();
63   auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
64 
65   if (InlineSitesFromRemarks.empty())
66     return std::make_unique<DefaultInlineAdvice>(this, CB, None, ORE,
67                                                  EmitRemarks);
68 
69   std::string CallSiteLoc = getCallSiteLocation(CB.getDebugLoc());
70   StringRef Callee = CB.getCalledFunction()->getName();
71   std::string Combined = (Callee + CallSiteLoc).str();
72   auto Iter = InlineSitesFromRemarks.find(Combined);
73 
74   Optional<InlineCost> InlineRecommended = None;
75   if (Iter != InlineSitesFromRemarks.end()) {
76     InlineRecommended = llvm::InlineCost::getAlways("found in replay");
77   }
78 
79   return std::make_unique<DefaultInlineAdvice>(this, CB, InlineRecommended, ORE,
80                                                EmitRemarks);
81 }
82