LLVM: include/llvm/Transforms/IPO/ProfiledCallGraph.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9#ifndef LLVM_TRANSFORMS_IPO_PROFILEDCALLGRAPH_H
10#define LLVM_TRANSFORMS_IPO_PROFILEDCALLGRAPH_H
11
16#include
17#include
18
19namespace llvm {
21
23
36
38
39
40
41
45 return L.Target->Name < R.Target->Name;
46 }
47 };
48
50 using edges = std::set<edge, ProfiledCallGraphEdgeComparer>;
53
56
59};
60
62public:
64
65
67 uint64_t IgnoreColdCallThreshold = 0) {
69 "CS flat profile is not handled here");
70 for (const auto &Samples : ProfileMap) {
71 addProfiledCalls(Samples.second);
72 }
73
74
75
76 trimColdEges(IgnoreColdCallThreshold);
77 }
78
79
81 uint64_t IgnoreColdCallThreshold = 0) {
82
83
84 std::queue<ContextTrieNode *> Queue;
88 Queue.push(Callee);
89 }
90
91 while (!Queue.empty()) {
93 Queue.pop();
94 FunctionSamples *CallerSamples = Caller->getFunctionSamples();
95
96
97
98
99
100
101
102 for (auto &Child : Caller->getAllChildContext()) {
105 Queue.push(Callee);
106
107
109 FunctionSamples *CalleeSamples = Callee->getFunctionSamples();
110 if (!CalleeSamples || !CallerSamples) {
111 Weight = 0;
112 } else {
115 LineLocation Callsite = Callee->getCallSiteLoc();
117 auto It = CallTargets->find(CalleeSamples->getFunction());
118 if (It != CallTargets->end())
119 CallsiteCount = It->second;
120 }
121 Weight = std::max(CallsiteCount, CalleeEntryCount);
122 }
123
124 addProfiledCall(Caller->getFuncName(), Callee->getFuncName(), Weight);
125 }
126 }
127
128
129
130 trimColdEges(IgnoreColdCallThreshold);
131 }
132
136
138 auto [It, Inserted] = ProfiledFunctions.try_emplace(Name);
139 if (Inserted) {
140
141
142
145 It->second = &Node;
146 Root.Edges.emplace(&Root, It->second, 0);
147 }
148 }
149
150private:
153 auto CalleeIt = ProfiledFunctions.find(CalleeName);
154 if (CalleeIt == ProfiledFunctions.end())
155 return;
156 auto CallerIt = ProfiledFunctions.find(CallerName);
157 assert(CallerIt != ProfiledFunctions.end());
159 auto &Edges = CallerIt->second->Edges;
160 auto [EdgeIt, Inserted] = Edges.insert(Edge);
161 if (!Inserted) {
162
163 Edge.Weight += EdgeIt->Weight;
164 Edges.erase(EdgeIt);
165 Edges.insert(Edge);
166 }
167 }
168
169 void addProfiledCalls(const FunctionSamples &Samples) {
171
172 for (const auto &Sample : Samples.getBodySamples()) {
173 for (const auto &[Target, Frequency] : Sample.second.getCallTargets()) {
175 addProfiledCall(Samples.getFunction(), Target, Frequency);
176 }
177 }
178
179 for (const auto &CallsiteSamples : Samples.getCallsiteSamples()) {
180 for (const auto &InlinedSamples : CallsiteSamples.second) {
182 addProfiledCall(Samples.getFunction(), InlinedSamples.first,
183 InlinedSamples.second.getHeadSamplesEstimate());
184 addProfiledCalls(InlinedSamples.second);
185 }
186 }
187 }
188
189
190
191 void trimColdEges(uint64_t Threshold = 0) {
192 if (!Threshold)
193 return;
194
195 for (auto &Node : ProfiledFunctions) {
196 auto &Edges = Node.second->Edges;
197 auto I = Edges.begin();
198 while (I != Edges.end()) {
199 if (I->Weight <= Threshold)
201 else
202 I++;
203 }
204 }
205 }
206
207 ProfiledCallGraphNode Root;
208
209 std::list ProfiledCallGraphNodeList;
210 HashKeyMap<llvm::DenseMap, FunctionId, ProfiledCallGraphNode*>
211 ProfiledFunctions;
212};
213
214}
215
226
227template <>
233
235 return PCG->begin();
236 }
237
239 return PCG->end();
240 }
241};
242
243}
244
245#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the little GraphTraits template class that should be specialized by classes that...
std::pair< BasicBlock *, BasicBlock * > Edge
This file provides the interface for context-sensitive profile tracker used by CSSPGO.
LLVM_ABI std::map< uint64_t, ContextTrieNode > & getAllChildContext()
LLVM_ABI ContextTrieNode & getRootContext()
This class represents a function that is read from a sample profile.
Representation of the samples collected for a function.
static LLVM_ABI bool ProfileIsCS
FunctionId getFunction() const
Return the function name.
ErrorOr< const SampleRecord::CallTargetMap & > findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const
Returns the call target map collected at a given location.
uint64_t getHeadSamplesEstimate() const
Return an estimate of the sample count of the function entry basic block.
Definition ProfiledCallGraph.h:61
ProfiledCallGraphNode * getEntryNode()
Definition ProfiledCallGraph.h:135
ProfiledCallGraphNode::iterator iterator
Definition ProfiledCallGraph.h:63
void addProfiledFunction(FunctionId Name)
Definition ProfiledCallGraph.h:137
ProfiledCallGraph(SampleContextTracker &ContextTracker, uint64_t IgnoreColdCallThreshold=0)
Definition ProfiledCallGraph.h:80
iterator end()
Definition ProfiledCallGraph.h:134
iterator begin()
Definition ProfiledCallGraph.h:133
ProfiledCallGraph(SampleProfileMap &ProfileMap, uint64_t IgnoreColdCallThreshold=0)
Definition ProfiledCallGraph.h:66
This class provides operator overloads to the map container using MD5 as the key type,...
NodeAddr< NodeBase * > Node
This is an optimization pass for GlobalISel generic memory operations.
NodeType::edge EdgeType
Definition ProfiledCallGraph.h:219
ProfiledCallGraphNode NodeType
Definition ProfiledCallGraph.h:217
static ChildIteratorType child_end(NodeRef N)
Definition ProfiledCallGraph.h:224
ProfiledCallGraphNode * NodeRef
Definition ProfiledCallGraph.h:218
static NodeRef getEntryNode(NodeRef PCGN)
Definition ProfiledCallGraph.h:222
NodeType::const_iterator ChildIteratorType
Definition ProfiledCallGraph.h:220
static ChildIteratorType child_begin(NodeRef N)
Definition ProfiledCallGraph.h:223
static NodeRef getEntryNode(ProfiledCallGraph *PCG)
Definition ProfiledCallGraph.h:230
static ChildIteratorType nodes_end(ProfiledCallGraph *PCG)
Definition ProfiledCallGraph.h:238
static ChildIteratorType nodes_begin(ProfiledCallGraph *PCG)
Definition ProfiledCallGraph.h:234
typename ProfiledCallGraph *::UnknownGraphTypeError NodeRef
Represents the relative location of an instruction.
Definition ProfiledCallGraph.h:24
ProfiledCallGraphNode * Target
Definition ProfiledCallGraph.h:29
ProfiledCallGraphEdge(ProfiledCallGraphNode *Source, ProfiledCallGraphNode *Target, uint64_t Weight)
Definition ProfiledCallGraph.h:25
ProfiledCallGraphNode * Source
Definition ProfiledCallGraph.h:28
uint64_t Weight
Definition ProfiledCallGraph.h:30
Definition ProfiledCallGraph.h:42
bool operator()(const ProfiledCallGraphEdge &L, const ProfiledCallGraphEdge &R) const
Definition ProfiledCallGraph.h:43
Definition ProfiledCallGraph.h:37
ProfiledCallGraphEdge edge
Definition ProfiledCallGraph.h:49
edges::iterator iterator
Definition ProfiledCallGraph.h:51
FunctionId Name
Definition ProfiledCallGraph.h:57
edges::const_iterator const_iterator
Definition ProfiledCallGraph.h:52
edges Edges
Definition ProfiledCallGraph.h:58
ProfiledCallGraphNode(FunctionId FName=FunctionId())
Definition ProfiledCallGraph.h:54
std::set< edge, ProfiledCallGraphEdgeComparer > edges
Definition ProfiledCallGraph.h:50