LLVM: lib/Analysis/ReplayInlineAdvisor.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

20#include

21

22using namespace llvm;

23

24#define DEBUG_TYPE "replay-inline"

25

28 std::unique_ptr OriginalAdvisor,

32 ReplaySettings(ReplaySettings), EmitRemarks(EmitRemarks) {

33

35 std::error_code EC = BufferOrErr.getError();

36 if (EC) {

37 Context.emitError("Could not open remarks file: " + EC.message());

38 return;

39 }

40

41

42

43

44

45 line_iterator LineIt(*BufferOrErr.get(), true);

46 const std::string PositiveRemark = "' inlined into '";

47 const std::string NegativeRemark = "' will not be inlined into '";

48

49 for (; !LineIt.is_at_eof(); ++LineIt) {

51 auto Pair = Line.split(" at callsite ");

52

53 bool IsPositiveRemark = true;

54 if (Pair.first.contains(NegativeRemark))

55 IsPositiveRemark = false;

56

57 auto CalleeCaller =

58 Pair.first.split(IsPositiveRemark ? PositiveRemark : NegativeRemark);

59

60 StringRef Callee = CalleeCaller.first.rsplit(": '").second;

61 StringRef Caller = CalleeCaller.second.rsplit("'").first;

62

63 auto CallSite = Pair.second.split(";").first;

64

65 if (Callee.empty() || Caller.empty() || CallSite.empty()) {

66 Context.emitError("Invalid remark format: " + Line);

67 return;

68 }

69

70 std::string Combined = (Callee + CallSite).str();

71 InlineSitesFromRemarks[Combined] = IsPositiveRemark;

73 CallersToReplay.insert(Caller);

74 }

75

76 HasReplayRemarks = true;

77}

78

79std::unique_ptr

82 std::unique_ptr OriginalAdvisor,

85 auto Advisor = std::make_unique(

86 M, FAM, Context, std::move(OriginalAdvisor), ReplaySettings, EmitRemarks,

87 IC);

88 if (!Advisor->areReplayRemarksLoaded())

89 Advisor.reset();

90 return Advisor;

91}

92

94 assert(HasReplayRemarks);

95

98

99

100 if (!hasInlineAdvice(*CB.getFunction())) {

101

102 if (OriginalAdvisor)

103 return OriginalAdvisor->getAdvice(CB);

104

105

106 return {};

107 }

108

109 std::string CallSiteLoc =

112 std::string Combined = (Callee + CallSiteLoc).str();

113

114

115 auto Iter = InlineSitesFromRemarks.find(Combined);

116 if (Iter != InlineSitesFromRemarks.end()) {

117 if (Iter->second) {

118 LLVM_DEBUG(dbgs() << "Replay Inliner: Inlined " << Callee << " @ "

119 << CallSiteLoc << "\n");

120 return std::make_unique(

122 EmitRemarks);

123 } else {

124 LLVM_DEBUG(dbgs() << "Replay Inliner: Not Inlined " << Callee << " @ "

125 << CallSiteLoc << "\n");

126

127 return std::make_unique(this, CB, std::nullopt, ORE,

128 EmitRemarks);

129 }

130 }

131

132

133 if (ReplaySettings.ReplayFallback ==

135 return std::make_unique(

137 EmitRemarks);

138 else if (ReplaySettings.ReplayFallback ==

140

141 return std::make_unique(this, CB, std::nullopt, ORE,

142 EmitRemarks);

143 else {

144 assert(ReplaySettings.ReplayFallback ==

146

147 if (OriginalAdvisor)

148 return OriginalAdvisor->getAdvice(CB);

149 }

150

151

152 return {};

153}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

FunctionAnalysisManager FAM

Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...

Function * getCalledFunction() const

Returns the function called, or null if this is an indirect function invocation or the function signa...

LLVM_ABI Function * getCaller()

Helper to get the caller (the parent function).

const std::optional< InlineContext > IC

FunctionAnalysisManager & FAM

InlineAdvisor(InlineAdvisor &&)=delete

static InlineCost getAlways(const char *Reason, std::optional< CostBenefitPair > CostBenefit=std::nullopt)

const DebugLoc & getDebugLoc() const

Return the debug location for this node as a DebugLoc.

LLVM_ABI const Function * getFunction() const

Return the function this instruction belongs to.

This is an important class for using LLVM in a threaded context.

static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)

Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".

A Module instance is used to store all the information related to an LLVM module.

ReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context, std::unique_ptr< InlineAdvisor > OriginalAdvisor, const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks, InlineContext IC)

Definition ReplayInlineAdvisor.cpp:26

std::unique_ptr< InlineAdvice > getAdviceImpl(CallBase &CB) override

Definition ReplayInlineAdvisor.cpp:93

StringRef - Represent a constant reference to a string, i.e.

size_t find(char C, size_t From=0) const

Search for the first character C in the string.

LLVM_ABI StringRef getName() const

Return a constant reference to the value's name.

A forward iterator which reads text lines from a buffer.

bool is_at_eof() const

Return true if we've reached EOF or are an "end" iterator.

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

std::unique_ptr< InlineAdvisor > getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM, LLVMContext &Context, std::unique_ptr< InlineAdvisor > OriginalAdvisor, const ReplayInlinerSettings &ReplaySettings, bool EmitRemarks, InlineContext IC)

Definition ReplayInlineAdvisor.cpp:80

OutputIt move(R &&Range, OutputIt Out)

Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.

AnalysisManager< Function > FunctionAnalysisManager

Convenience typedef for the Function analysis manager.

std::string formatCallSiteLocation(DebugLoc DLoc, const CallSiteFormat &Format)

Get call site location as a string with the given format.

Implement std::hash so that hash_code can be used in STL containers.

Provides context on when an inline advisor is constructed in the pipeline (e.g., link phase,...