- Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathReorderFunctions.cpp
524 lines (468 loc) · 19.2 KB
/
ReorderFunctions.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//===- bolt/Passes/ReorderFunctions.cpp - Function reordering pass --------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements ReorderFunctions class.
//
//===----------------------------------------------------------------------===//
#include"bolt/Passes/ReorderFunctions.h"
#include"bolt/Passes/HFSort.h"
#include"bolt/Utils/Utils.h"
#include"llvm/ADT/STLExtras.h"
#include"llvm/Support/CommandLine.h"
#include"llvm/Transforms/Utils/CodeLayout.h"
#include<fstream>
#defineDEBUG_TYPE"hfsort"
usingnamespacellvm;
namespaceopts {
extern cl::OptionCategory BoltOptCategory;
extern cl::opt<unsigned> Verbosity;
extern cl::opt<uint32_t> RandomSeed;
externsize_tpadFunctionBefore(const bolt::BinaryFunction &Function);
externsize_tpadFunctionAfter(const bolt::BinaryFunction &Function);
extern cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions;
cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions(
"reorder-functions",
cl::desc("reorder and cluster functions (works only with relocations)"),
cl::init(bolt::ReorderFunctions::RT_NONE),
cl::values(clEnumValN(bolt::ReorderFunctions::RT_NONE, "none",
"do not reorder functions"),
clEnumValN(bolt::ReorderFunctions::RT_EXEC_COUNT, "exec-count",
"order by execution count"),
clEnumValN(bolt::ReorderFunctions::RT_HFSORT, "hfsort",
"use hfsort algorithm"),
clEnumValN(bolt::ReorderFunctions::RT_HFSORT_PLUS, "hfsort+",
"use cache-directed sort"),
clEnumValN(bolt::ReorderFunctions::RT_CDSORT, "cdsort",
"use cache-directed sort"),
clEnumValN(bolt::ReorderFunctions::RT_PETTIS_HANSEN,
"pettis-hansen", "use Pettis-Hansen algorithm"),
clEnumValN(bolt::ReorderFunctions::RT_RANDOM, "random",
"reorder functions randomly"),
clEnumValN(bolt::ReorderFunctions::RT_USER, "user",
"use function order specified by -function-order")),
cl::ZeroOrMore, cl::cat(BoltOptCategory),
cl::callback([](const bolt::ReorderFunctions::ReorderType &option) {
if (option == bolt::ReorderFunctions::RT_HFSORT_PLUS) {
errs() << "BOLT-WARNING: '-reorder-functions=hfsort+' is deprecated,"
<< " please use '-reorder-functions=cdsort' instead\n";
ReorderFunctions = bolt::ReorderFunctions::RT_CDSORT;
}
}));
static cl::opt<bool> ReorderFunctionsUseHotSize(
"reorder-functions-use-hot-size",
cl::desc("use a function's hot size when doing clustering"), cl::init(true),
cl::cat(BoltOptCategory));
static cl::opt<std::string> FunctionOrderFile(
"function-order",
cl::desc("file containing an ordered list of functions to use for function "
"reordering"),
cl::cat(BoltOptCategory));
static cl::opt<std::string> GenerateFunctionOrderFile(
"generate-function-order",
cl::desc("file to dump the ordered list of functions to use for function "
"reordering"),
cl::cat(BoltOptCategory));
static cl::opt<std::string> LinkSectionsFile(
"generate-link-sections",
cl::desc("generate a list of function sections in a format suitable for "
"inclusion in a linker script"),
cl::cat(BoltOptCategory));
static cl::opt<bool>
UseEdgeCounts("use-edge-counts",
cl::desc("use edge count data when doing clustering"),
cl::init(true), cl::cat(BoltOptCategory));
static cl::opt<bool> CgFromPerfData(
"cg-from-perf-data",
cl::desc("use perf data directly when constructing the call graph"
" for stale functions"),
cl::init(true), cl::ZeroOrMore, cl::cat(BoltOptCategory));
static cl::opt<bool> CgIgnoreRecursiveCalls(
"cg-ignore-recursive-calls",
cl::desc("ignore recursive calls when constructing the call graph"),
cl::init(true), cl::cat(BoltOptCategory));
static cl::opt<bool> CgUseSplitHotSize(
"cg-use-split-hot-size",
cl::desc("use hot/cold data on basic blocks to determine hot sizes for "
"call graph functions"),
cl::init(false), cl::ZeroOrMore, cl::cat(BoltOptCategory));
} // namespace opts
namespacellvm {
namespacebolt {
using NodeId = CallGraph::NodeId;
using Arc = CallGraph::Arc;
using Node = CallGraph::Node;
voidReorderFunctions::reorder(BinaryContext &BC,
std::vector<Cluster> &&Clusters,
std::map<uint64_t, BinaryFunction> &BFs) {
std::vector<uint64_t> FuncAddr(Cg.numNodes()); // Just for computing stats
uint64_t TotalSize = 0;
uint32_t Index = 0;
// Set order of hot functions based on clusters.
for (const Cluster &Cluster : Clusters) {
for (const NodeId FuncId : Cluster.targets()) {
Cg.nodeIdToFunc(FuncId)->setIndex(Index++);
FuncAddr[FuncId] = TotalSize;
TotalSize += Cg.size(FuncId);
}
}
// Assign valid index for functions with valid profile.
for (auto &It : BFs) {
BinaryFunction &BF = It.second;
if (!BF.hasValidIndex() && BF.hasValidProfile())
BF.setIndex(Index++);
}
if (opts::ReorderFunctions == RT_NONE)
return;
printStats(BC, Clusters, FuncAddr);
}
voidReorderFunctions::printStats(BinaryContext &BC,
const std::vector<Cluster> &Clusters,
const std::vector<uint64_t> &FuncAddr) {
if (opts::Verbosity == 0) {
#ifndef NDEBUG
if (!DebugFlag || !isCurrentDebugType("hfsort"))
return;
#else
return;
#endif
}
bool PrintDetailed = opts::Verbosity > 1;
#ifndef NDEBUG
PrintDetailed |=
(DebugFlag && isCurrentDebugType("hfsort") && opts::Verbosity > 0);
#endif
uint64_t TotalSize = 0;
uint64_t CurPage = 0;
uint64_t Hotfuncs = 0;
double TotalDistance = 0;
double TotalCalls = 0;
double TotalCalls64B = 0;
double TotalCalls4KB = 0;
double TotalCalls2MB = 0;
if (PrintDetailed)
BC.outs() << "BOLT-INFO: Function reordering page layout\n"
<< "BOLT-INFO: ============== page 0 ==============\n";
for (const Cluster &Cluster : Clusters) {
if (PrintDetailed)
BC.outs() << format(
"BOLT-INFO: -------- density = %.3lf (%u / %u) --------\n",
Cluster.density(), Cluster.samples(), Cluster.size());
for (NodeId FuncId : Cluster.targets()) {
if (Cg.samples(FuncId) > 0) {
Hotfuncs++;
if (PrintDetailed)
BC.outs() << "BOLT-INFO: hot func " << *Cg.nodeIdToFunc(FuncId)
<< " (" << Cg.size(FuncId) << ")\n";
uint64_t Dist = 0;
uint64_t Calls = 0;
for (NodeId Dst : Cg.successors(FuncId)) {
if (FuncId == Dst) // ignore recursive calls in stats
continue;
const Arc &Arc = *Cg.findArc(FuncId, Dst);
constauto D = std::abs(FuncAddr[Arc.dst()] -
(FuncAddr[FuncId] + Arc.avgCallOffset()));
constdouble W = Arc.weight();
if (D < 64 && PrintDetailed && opts::Verbosity > 2)
BC.outs() << "BOLT-INFO: short (" << D << "B) call:\n"
<< "BOLT-INFO: Src: " << *Cg.nodeIdToFunc(FuncId)
<< "\n"
<< "BOLT-INFO: Dst: " << *Cg.nodeIdToFunc(Dst) << "\n"
<< "BOLT-INFO: Weight = " << W << "\n"
<< "BOLT-INFO: AvgOffset = " << Arc.avgCallOffset()
<< "\n";
Calls += W;
if (D < 64)
TotalCalls64B += W;
if (D < 4096)
TotalCalls4KB += W;
if (D < (2 << 20))
TotalCalls2MB += W;
Dist += Arc.weight() * D;
if (PrintDetailed)
BC.outs() << format("BOLT-INFO: arc: %u [@%lu+%.1lf] -> %u [@%lu]: "
"weight = %.0lf, callDist = %f\n",
Arc.src(), FuncAddr[Arc.src()],
Arc.avgCallOffset(), Arc.dst(),
FuncAddr[Arc.dst()], Arc.weight(), D);
}
TotalCalls += Calls;
TotalDistance += Dist;
TotalSize += Cg.size(FuncId);
if (PrintDetailed) {
BC.outs() << format("BOLT-INFO: start = %6u : avgCallDist = %lu : ",
TotalSize, Calls ? Dist / Calls : 0)
<< Cg.nodeIdToFunc(FuncId)->getPrintName() << '\n';
constuint64_t NewPage = TotalSize / HugePageSize;
if (NewPage != CurPage) {
CurPage = NewPage;
BC.outs() << format(
"BOLT-INFO: ============== page %u ==============\n", CurPage);
}
}
}
}
}
BC.outs() << "BOLT-INFO: Function reordering stats\n"
<< format("BOLT-INFO: Number of hot functions: %u\n"
"BOLT-INFO: Number of clusters: %lu\n",
Hotfuncs, Clusters.size())
<< format("BOLT-INFO: Final average call distance = %.1lf "
"(%.0lf / %.0lf)\n",
TotalCalls ? TotalDistance / TotalCalls : 0,
TotalDistance, TotalCalls)
<< format("BOLT-INFO: Total Calls = %.0lf\n", TotalCalls);
if (TotalCalls)
BC.outs()
<< format("BOLT-INFO: Total Calls within 64B = %.0lf (%.2lf%%)\n",
TotalCalls64B, 100 * TotalCalls64B / TotalCalls)
<< format("BOLT-INFO: Total Calls within 4KB = %.0lf (%.2lf%%)\n",
TotalCalls4KB, 100 * TotalCalls4KB / TotalCalls)
<< format("BOLT-INFO: Total Calls within 2MB = %.0lf (%.2lf%%)\n",
TotalCalls2MB, 100 * TotalCalls2MB / TotalCalls);
}
Error ReorderFunctions::readFunctionOrderFile(
std::vector<std::string> &FunctionNames) {
std::ifstream FuncsFile(opts::FunctionOrderFile, std::ios::in);
if (!FuncsFile)
returncreateFatalBOLTError(Twine("Ordered functions file \"") +
Twine(opts::FunctionOrderFile) +
Twine("\" can't be opened."));
std::string FuncName;
while (std::getline(FuncsFile, FuncName))
FunctionNames.push_back(FuncName);
returnError::success();
}
Error ReorderFunctions::runOnFunctions(BinaryContext &BC) {
auto &BFs = BC.getBinaryFunctions();
if (opts::ReorderFunctions != RT_NONE &&
opts::ReorderFunctions != RT_EXEC_COUNT &&
opts::ReorderFunctions != RT_USER) {
Cg = buildCallGraph(
BC,
[](const BinaryFunction &BF) {
if (!BF.hasProfile())
returntrue;
if (BF.getState() != BinaryFunction::State::CFG)
returntrue;
returnfalse;
},
opts::CgFromPerfData,
/*IncludeSplitCalls=*/false, opts::ReorderFunctionsUseHotSize,
opts::CgUseSplitHotSize, opts::UseEdgeCounts,
opts::CgIgnoreRecursiveCalls);
Cg.normalizeArcWeights();
}
std::vector<Cluster> Clusters;
switch (opts::ReorderFunctions) {
case RT_NONE:
break;
case RT_EXEC_COUNT: {
std::vector<BinaryFunction *> SortedFunctions(BFs.size());
llvm::transform(llvm::make_second_range(BFs), SortedFunctions.begin(),
[](BinaryFunction &BF) { return &BF; });
llvm::stable_sort(SortedFunctions,
[&](const BinaryFunction *A, const BinaryFunction *B) {
if (A->isIgnored())
returnfalse;
if (B->isIgnored())
returntrue;
constsize_t PadA = opts::padFunctionBefore(*A) +
opts::padFunctionAfter(*A);
constsize_t PadB = opts::padFunctionBefore(*B) +
opts::padFunctionAfter(*B);
if (!PadA || !PadB) {
if (PadA)
returntrue;
if (PadB)
returnfalse;
}
if (!A->hasProfile())
returnfalse;
if (!B->hasProfile())
returntrue;
return A->getExecutionCount() > B->getExecutionCount();
});
uint32_t Index = 0;
for (BinaryFunction *BF : SortedFunctions)
if (BF->hasProfile()) {
BF->setIndex(Index++);
LLVM_DEBUG(if (opts::Verbosity > 1) {
dbgs() << "BOLT-INFO: hot func " << BF->getPrintName() << " ("
<< BF->getExecutionCount() << ")\n";
});
}
} break;
case RT_HFSORT:
Clusters = clusterize(Cg);
break;
case RT_CDSORT: {
// It is required that the sum of incoming arc weights is not greater
// than the number of samples for every function. Ensuring the call graph
// obeys the property before running the algorithm.
Cg.adjustArcWeights();
// Initialize CFG nodes and their data
std::vector<uint64_t> FuncSizes;
std::vector<uint64_t> FuncCounts;
std::vector<codelayout::EdgeCount> CallCounts;
std::vector<uint64_t> CallOffsets;
for (NodeId F = 0; F < Cg.numNodes(); ++F) {
FuncSizes.push_back(Cg.size(F));
FuncCounts.push_back(Cg.samples(F));
for (NodeId Succ : Cg.successors(F)) {
const Arc &Arc = *Cg.findArc(F, Succ);
CallCounts.push_back({F, Succ, uint64_t(Arc.weight())});
CallOffsets.push_back(uint64_t(Arc.avgCallOffset()));
}
}
// Run the layout algorithm.
std::vector<uint64_t> Result = codelayout::computeCacheDirectedLayout(
FuncSizes, FuncCounts, CallCounts, CallOffsets);
// Create a single cluster from the computed order of hot functions.
std::vector<CallGraph::NodeId> NodeOrder(Result.begin(), Result.end());
Clusters.emplace_back(Cluster(NodeOrder, Cg));
} break;
case RT_PETTIS_HANSEN:
Clusters = pettisAndHansen(Cg);
break;
case RT_RANDOM:
std::srand(opts::RandomSeed);
Clusters = randomClusters(Cg);
break;
case RT_USER: {
// Build LTOCommonNameMap
StringMap<std::vector<uint64_t>> LTOCommonNameMap;
for (const BinaryFunction &BF : llvm::make_second_range(BFs))
for (StringRef Name : BF.getNames())
if (std::optional<StringRef> LTOCommonName = getLTOCommonName(Name))
LTOCommonNameMap[*LTOCommonName].push_back(BF.getAddress());
uint32_t Index = 0;
uint32_t InvalidEntries = 0;
std::vector<std::string> FunctionNames;
if (Error E = readFunctionOrderFile(FunctionNames))
returnError(std::move(E));
for (const std::string &Function : FunctionNames) {
std::vector<uint64_t> FuncAddrs;
BinaryData *BD = BC.getBinaryDataByName(Function);
if (!BD) {
// If we can't find the main symbol name, look for alternates.
uint32_t LocalID = 1;
while (true) {
const std::string FuncName = Function + "/" + std::to_string(LocalID);
BD = BC.getBinaryDataByName(FuncName);
if (BD)
FuncAddrs.push_back(BD->getAddress());
else
break;
LocalID++;
}
// Strip LTO suffixes
if (std::optional<StringRef> CommonName = getLTOCommonName(Function))
if (LTOCommonNameMap.contains(*CommonName))
llvm::append_range(FuncAddrs, LTOCommonNameMap[*CommonName]);
} else {
FuncAddrs.push_back(BD->getAddress());
}
if (FuncAddrs.empty()) {
if (opts::Verbosity >= 1)
BC.errs() << "BOLT-WARNING: Reorder functions: can't find function "
<< "for " << Function << "\n";
++InvalidEntries;
continue;
}
for (constuint64_t FuncAddr : FuncAddrs) {
const BinaryData *FuncBD = BC.getBinaryDataAtAddress(FuncAddr);
assert(FuncBD);
BinaryFunction *BF = BC.getFunctionForSymbol(FuncBD->getSymbol());
if (!BF) {
if (opts::Verbosity >= 1)
BC.errs() << "BOLT-WARNING: Reorder functions: can't find function "
<< "for " << Function << "\n";
++InvalidEntries;
break;
}
if (!BF->hasValidIndex())
BF->setIndex(Index++);
elseif (opts::Verbosity > 0)
BC.errs() << "BOLT-WARNING: Duplicate reorder entry for " << Function
<< "\n";
}
}
if (InvalidEntries)
BC.errs() << "BOLT-WARNING: Reorder functions: can't find functions for "
<< InvalidEntries << " entries in -function-order list\n";
} break;
default:
llvm_unreachable("unexpected layout type");
}
reorder(BC, std::move(Clusters), BFs);
BC.HasFinalizedFunctionOrder = true;
std::unique_ptr<std::ofstream> FuncsFile;
if (!opts::GenerateFunctionOrderFile.empty()) {
FuncsFile = std::make_unique<std::ofstream>(opts::GenerateFunctionOrderFile,
std::ios::out);
if (!FuncsFile) {
BC.errs() << "BOLT-ERROR: ordered functions file "
<< opts::GenerateFunctionOrderFile << " cannot be opened\n";
returncreateFatalBOLTError("");
}
}
std::unique_ptr<std::ofstream> LinkSectionsFile;
if (!opts::LinkSectionsFile.empty()) {
LinkSectionsFile =
std::make_unique<std::ofstream>(opts::LinkSectionsFile, std::ios::out);
if (!LinkSectionsFile) {
BC.errs() << "BOLT-ERROR: link sections file " << opts::LinkSectionsFile
<< " cannot be opened\n";
returncreateFatalBOLTError("");
}
}
if (FuncsFile || LinkSectionsFile) {
std::vector<BinaryFunction *> SortedFunctions(BFs.size());
llvm::transform(llvm::make_second_range(BFs), SortedFunctions.begin(),
[](BinaryFunction &BF) { return &BF; });
// Sort functions by index.
llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
for (const BinaryFunction *Func : SortedFunctions) {
if (!Func->hasValidIndex())
break;
if (Func->isPLTFunction())
continue;
if (FuncsFile)
*FuncsFile << Func->getOneName().str() << '\n';
if (LinkSectionsFile) {
constchar *Indent = "";
std::vector<StringRef> AllNames = Func->getNames();
llvm::sort(AllNames);
for (StringRef Name : AllNames) {
constsize_t SlashPos = Name.find('/');
if (SlashPos != std::string::npos) {
// Avoid duplicates for local functions.
if (Name.find('/', SlashPos + 1) != std::string::npos)
continue;
Name = Name.substr(0, SlashPos);
}
*LinkSectionsFile << Indent << ".text." << Name.str() << '\n';
Indent = "";
}
}
}
if (FuncsFile) {
FuncsFile->close();
BC.outs() << "BOLT-INFO: dumped function order to "
<< opts::GenerateFunctionOrderFile << '\n';
}
if (LinkSectionsFile) {
LinkSectionsFile->close();
BC.outs() << "BOLT-INFO: dumped linker section order to "
<< opts::LinkSectionsFile << '\n';
}
}
returnError::success();
}
} // namespace bolt
} // namespace llvm