forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHFSort.cpp
289 lines (237 loc) · 8.44 KB
/
HFSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//===- bolt/Passes/HFSort.cpp - Cluster functions by hotness --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implementation of HFSort algorithm for function ordering:
// https://research.fb.com/wp-content/uploads/2017/01/cgo2017-hfsort-final1.pdf
//
//===----------------------------------------------------------------------===//
#include "bolt/Passes/HFSort.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#include <unordered_set>
#define DEBUG_TYPE "hfsort"
namespace opts {
extern llvm::cl::opt<unsigned> Verbosity;
}
namespace llvm {
namespace bolt {
using NodeId = CallGraph::NodeId;
using Arc = CallGraph::Arc;
using Node = CallGraph::Node;
namespace {
// The number of pages to reserve for the functions with highest
// density (samples / size). The functions put in these pages are not
// considered for clustering.
constexpr uint32_t FrozenPages = 0;
// The minimum approximate probability of a callee being called from a
// particular arc to consider merging with the caller's cluster.
constexpr double MinArcProbability = 0.1;
// This is a factor to determine by how much a caller cluster is
// willing to degrade it's density by merging a callee.
constexpr int CallerDegradeFactor = 8;
} // namespace
////////////////////////////////////////////////////////////////////////////////
Cluster::Cluster(NodeId Id, const Node &Func)
: Samples(Func.samples()), Size(Func.size()),
Density((double)Samples / Size) {
Targets.push_back(Id);
}
Cluster::Cluster(const std::vector<NodeId> &Nodes, const CallGraph &Cg) {
Samples = 0;
Size = 0;
for (NodeId TargetId : Nodes) {
Targets.push_back(TargetId);
Samples += Cg.samples(TargetId);
Size += Cg.size(TargetId);
}
Density = (double)Samples / Size;
}
std::string Cluster::toString() const {
std::string Str;
raw_string_ostream CS(Str);
bool PrintComma = false;
CS << "funcs = [";
for (const NodeId &Target : Targets) {
if (PrintComma)
CS << ", ";
CS << Target;
PrintComma = true;
}
CS << "]";
return CS.str();
}
namespace {
void freezeClusters(const CallGraph &Cg, std::vector<Cluster> &Clusters) {
uint32_t TotalSize = 0;
std::sort(Clusters.begin(), Clusters.end(), compareClustersDensity);
for (Cluster &C : Clusters) {
uint32_t NewSize = TotalSize + C.size();
if (NewSize > FrozenPages * HugePageSize)
break;
C.freeze();
TotalSize = NewSize;
LLVM_DEBUG(NodeId Fid = C.target(0);
dbgs() << format(
"freezing cluster for func %d, size = %u, samples = %lu)\n",
Fid, Cg.size(Fid), Cg.samples(Fid)););
}
}
} // namespace
void Cluster::reverseTargets() { std::reverse(Targets.begin(), Targets.end()); }
void Cluster::merge(const Cluster &Other, const double Aw) {
Targets.insert(Targets.end(), Other.Targets.begin(), Other.Targets.end());
Size += Other.Size;
Samples += Other.Samples;
Density = (double)Samples / Size;
}
void Cluster::merge(const Cluster &Other,
const std::vector<CallGraph::NodeId> &Targets_) {
Targets = Targets_;
Size += Other.Size;
Samples += Other.Samples;
Density = (double)Samples / Size;
}
void Cluster::clear() {
Id = -1u;
Size = 0;
Samples = 0;
Density = 0.0;
Targets.clear();
Frozen = false;
}
std::vector<Cluster> clusterize(const CallGraph &Cg) {
std::vector<NodeId> SortedFuncs;
// indexed by NodeId, keeps it's current cluster
std::vector<Cluster *> FuncCluster(Cg.numNodes(), nullptr);
std::vector<Cluster> Clusters;
Clusters.reserve(Cg.numNodes());
for (NodeId F = 0; F < Cg.numNodes(); F++) {
if (Cg.samples(F) == 0)
continue;
Clusters.emplace_back(F, Cg.getNode(F));
SortedFuncs.push_back(F);
}
freezeClusters(Cg, Clusters);
// The size and order of Clusters is fixed until we reshuffle it immediately
// before returning.
for (Cluster &Cluster : Clusters)
FuncCluster[Cluster.targets().front()] = &Cluster;
std::sort(SortedFuncs.begin(), SortedFuncs.end(),
[&](const NodeId F1, const NodeId F2) {
const CallGraph::Node &Func1 = Cg.getNode(F1);
const CallGraph::Node &Func2 = Cg.getNode(F2);
return Func1.samples() * Func2.size() > // TODO: is this correct?
Func2.samples() * Func1.size();
});
// Process each function, and consider merging its cluster with the
// one containing its most likely predecessor.
for (const NodeId Fid : SortedFuncs) {
Cluster *Cluster = FuncCluster[Fid];
if (Cluster->frozen())
continue;
// Find best predecessor.
NodeId BestPred = CallGraph::InvalidId;
double BestProb = 0;
for (const NodeId Src : Cg.predecessors(Fid)) {
const Arc &Arc = *Cg.findArc(Src, Fid);
if (BestPred == CallGraph::InvalidId ||
Arc.normalizedWeight() > BestProb) {
BestPred = Arc.src();
BestProb = Arc.normalizedWeight();
}
}
// Check if the merge is good for the callee.
// Don't merge if the probability of getting to the callee from the
// caller is too low.
if (BestProb < MinArcProbability)
continue;
assert(BestPred != CallGraph::InvalidId);
class Cluster *PredCluster = FuncCluster[BestPred];
// Skip if no predCluster (predecessor w/ no samples), or if same
// as cluster, of it's frozen.
if (PredCluster == nullptr || PredCluster == Cluster ||
PredCluster->frozen())
continue;
// Skip if merged cluster would be bigger than the threshold.
if (Cluster->size() + PredCluster->size() > MaxClusterSize)
continue;
// Check if the merge is good for the caller.
// Don't merge if the caller's density is significantly better
// than the density resulting from the merge.
const double NewDensity =
((double)PredCluster->samples() + Cluster->samples()) /
(PredCluster->size() + Cluster->size());
if (PredCluster->density() > NewDensity * CallerDegradeFactor) {
continue;
}
LLVM_DEBUG(if (opts::Verbosity > 1) {
dbgs() << format("merging %s -> %s: %u\n",
PredCluster->toString().c_str(),
Cluster->toString().c_str(), Cg.samples(Fid));
});
for (NodeId F : Cluster->targets())
FuncCluster[F] = PredCluster;
PredCluster->merge(*Cluster);
Cluster->clear();
}
// Return the set of Clusters that are left, which are the ones that
// didn't get merged (so their first func is its original func).
std::vector<Cluster> SortedClusters;
std::unordered_set<Cluster *> Visited;
for (const NodeId Func : SortedFuncs) {
Cluster *Cluster = FuncCluster[Func];
if (!Cluster || Visited.count(Cluster) == 1 || Cluster->target(0) != Func)
continue;
SortedClusters.emplace_back(std::move(*Cluster));
Visited.insert(Cluster);
}
std::sort(SortedClusters.begin(), SortedClusters.end(),
compareClustersDensity);
return SortedClusters;
}
std::vector<Cluster> randomClusters(const CallGraph &Cg) {
std::vector<NodeId> FuncIds(Cg.numNodes(), 0);
std::vector<Cluster> Clusters;
Clusters.reserve(Cg.numNodes());
for (NodeId F = 0; F < Cg.numNodes(); F++) {
if (Cg.samples(F) == 0)
continue;
Clusters.emplace_back(F, Cg.getNode(F));
}
std::sort(
Clusters.begin(), Clusters.end(),
[](const Cluster &A, const Cluster &B) { return A.size() < B.size(); });
auto pickMergeCluster = [&Clusters](const size_t Idx) {
size_t MaxIdx = Idx + 1;
while (MaxIdx < Clusters.size() &&
Clusters[Idx].size() + Clusters[MaxIdx].size() <= MaxClusterSize)
++MaxIdx;
if (MaxIdx - Idx > 1) {
size_t MergeIdx = (std::rand() % (MaxIdx - Idx - 1)) + Idx + 1;
assert(Clusters[MergeIdx].size() + Clusters[Idx].size() <=
MaxClusterSize);
return MergeIdx;
}
return Clusters.size();
};
size_t Idx = 0;
while (Idx < Clusters.size()) {
size_t MergeIdx = pickMergeCluster(Idx);
if (MergeIdx == Clusters.size()) {
++Idx;
} else {
Clusters[Idx].merge(Clusters[MergeIdx]);
Clusters.erase(Clusters.begin() + MergeIdx);
}
}
return Clusters;
}
} // namespace bolt
} // namespace llvm