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 decision based 10 // on previous inline remarks from optimization remark log. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/ReplayInlineAdvisor.h" 15 #include "llvm/IR/DebugInfoMetadata.h" 16 #include "llvm/IR/Instructions.h" 17 #include "llvm/Support/LineIterator.h" 18 19 using namespace llvm; 20 21 #define DEBUG_TYPE "inline-replay" 22 23 ReplayInlineAdvisor::ReplayInlineAdvisor(FunctionAnalysisManager &FAM, 24 LLVMContext &Context, 25 StringRef RemarksFile) 26 : InlineAdvisor(FAM), HasReplayRemarks(false) { 27 auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(RemarksFile); 28 std::error_code EC = BufferOrErr.getError(); 29 if (EC) { 30 Context.emitError("Could not open remarks file: " + EC.message()); 31 return; 32 } 33 34 // Example for inline remarks to parse: 35 // _Z3subii inlined into main [details] at callsite sum:1 @ main:3.1 36 // We use the callsite string after `at callsite` to replay inlining. 37 line_iterator LineIt(*BufferOrErr.get(), /*SkipBlanks=*/true); 38 for (; !LineIt.is_at_eof(); ++LineIt) { 39 StringRef Line = *LineIt; 40 auto Pair = Line.split(" at callsite "); 41 if (Pair.second.empty()) 42 continue; 43 InlineSitesFromRemarks.insert(Pair.second); 44 } 45 HasReplayRemarks = true; 46 } 47 48 std::unique_ptr<InlineAdvice> ReplayInlineAdvisor::getAdvice(CallBase &CB) { 49 assert(HasReplayRemarks); 50 51 Function &Caller = *CB.getCaller(); 52 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller); 53 54 if (InlineSitesFromRemarks.empty()) 55 return std::make_unique<InlineAdvice>(this, CB, ORE, false); 56 57 std::string CallSiteLoc = getCallSiteLocation(CB.getDebugLoc()); 58 bool InlineRecommended = InlineSitesFromRemarks.count(CallSiteLoc) > 0; 59 return std::make_unique<InlineAdvice>(this, CB, ORE, InlineRecommended); 60 } 61