xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/ReplayInlineAdvisor.cpp (revision ae7e8a02e6e93455e026036132c4d053b2c12ad9)
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(
26     Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context,
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());
71   StringRef Callee = CB.getCalledFunction()->getName();
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 }
83