forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAligner.cpp
200 lines (159 loc) · 6.03 KB
/
Aligner.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
//===- bolt/Passes/Aligner.cpp - Pass for optimal code alignment ----------===//
//
// 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 the AlignerPass class.
//
//===----------------------------------------------------------------------===//
#include "bolt/Passes/Aligner.h"
#include "bolt/Core/ParallelUtilities.h"
#define DEBUG_TYPE "bolt-aligner"
using namespace llvm;
namespace opts {
extern cl::OptionCategory BoltOptCategory;
extern cl::opt<bool> AlignBlocks;
extern cl::opt<bool> PreserveBlocksAlignment;
cl::opt<unsigned>
AlignBlocksMinSize("align-blocks-min-size",
cl::desc("minimal size of the basic block that should be aligned"),
cl::init(0),
cl::ZeroOrMore,
cl::Hidden,
cl::cat(BoltOptCategory));
cl::opt<unsigned>
AlignBlocksThreshold("align-blocks-threshold",
cl::desc("align only blocks with frequency larger than containing function "
"execution frequency specified in percent. E.g. 1000 means aligning "
"blocks that are 10 times more frequently executed than the "
"containing function."),
cl::init(800),
cl::ZeroOrMore,
cl::Hidden,
cl::cat(BoltOptCategory));
cl::opt<unsigned>
AlignFunctions("align-functions",
cl::desc("align functions at a given value (relocation mode)"),
cl::init(64),
cl::ZeroOrMore,
cl::cat(BoltOptCategory));
cl::opt<unsigned>
AlignFunctionsMaxBytes("align-functions-max-bytes",
cl::desc("maximum number of bytes to use to align functions"),
cl::init(32),
cl::ZeroOrMore,
cl::cat(BoltOptCategory));
cl::opt<unsigned>
BlockAlignment("block-alignment",
cl::desc("boundary to use for alignment of basic blocks"),
cl::init(16),
cl::ZeroOrMore,
cl::cat(BoltOptCategory));
cl::opt<bool>
UseCompactAligner("use-compact-aligner",
cl::desc("Use compact approach for aligning functions"),
cl::init(true),
cl::ZeroOrMore,
cl::cat(BoltOptCategory));
} // end namespace opts
namespace llvm {
namespace bolt {
namespace {
// Align function to the specified byte-boundary (typically, 64) offsetting
// the fuction by not more than the corresponding value
void alignMaxBytes(BinaryFunction &Function) {
Function.setAlignment(opts::AlignFunctions);
Function.setMaxAlignmentBytes(opts::AlignFunctionsMaxBytes);
Function.setMaxColdAlignmentBytes(opts::AlignFunctionsMaxBytes);
}
// Align function to the specified byte-boundary (typically, 64) offsetting
// the fuction by not more than the minimum over
// -- the size of the function
// -- the specified number of bytes
void alignCompact(BinaryFunction &Function, const MCCodeEmitter *Emitter) {
const BinaryContext &BC = Function.getBinaryContext();
size_t HotSize = 0;
size_t ColdSize = 0;
for (const BinaryBasicBlock *BB : Function.layout())
if (BB->isCold())
ColdSize += BC.computeCodeSize(BB->begin(), BB->end(), Emitter);
else
HotSize += BC.computeCodeSize(BB->begin(), BB->end(), Emitter);
Function.setAlignment(opts::AlignFunctions);
if (HotSize > 0)
Function.setMaxAlignmentBytes(
std::min(size_t(opts::AlignFunctionsMaxBytes), HotSize));
// using the same option, max-align-bytes, both for cold and hot parts of the
// functions, as aligning cold functions typically does not affect performance
if (ColdSize > 0)
Function.setMaxColdAlignmentBytes(
std::min(size_t(opts::AlignFunctionsMaxBytes), ColdSize));
}
} // end anonymous namespace
void AlignerPass::alignBlocks(BinaryFunction &Function,
const MCCodeEmitter *Emitter) {
if (!Function.hasValidProfile() || !Function.isSimple())
return;
const BinaryContext &BC = Function.getBinaryContext();
const uint64_t FuncCount =
std::max<uint64_t>(1, Function.getKnownExecutionCount());
BinaryBasicBlock *PrevBB = nullptr;
for (BinaryBasicBlock *BB : Function.layout()) {
uint64_t Count = BB->getKnownExecutionCount();
if (Count <= FuncCount * opts::AlignBlocksThreshold / 100) {
PrevBB = BB;
continue;
}
uint64_t FTCount = 0;
if (PrevBB && PrevBB->getFallthrough() == BB)
FTCount = PrevBB->getBranchInfo(*BB).Count;
PrevBB = BB;
if (Count < FTCount * 2)
continue;
const uint64_t BlockSize =
BC.computeCodeSize(BB->begin(), BB->end(), Emitter);
const uint64_t BytesToUse =
std::min<uint64_t>(opts::BlockAlignment - 1, BlockSize);
if (opts::AlignBlocksMinSize && BlockSize < opts::AlignBlocksMinSize)
continue;
BB->setAlignment(opts::BlockAlignment);
BB->setAlignmentMaxBytes(BytesToUse);
// Update stats.
LLVM_DEBUG(
std::unique_lock<std::shared_timed_mutex> Lock(AlignHistogramMtx);
AlignHistogram[BytesToUse]++;
AlignedBlocksCount += BB->getKnownExecutionCount();
);
}
}
void AlignerPass::runOnFunctions(BinaryContext &BC) {
if (!BC.HasRelocations)
return;
AlignHistogram.resize(opts::BlockAlignment);
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
// Create a separate MCCodeEmitter to allow lock free execution
BinaryContext::IndependentCodeEmitter Emitter =
BC.createIndependentMCCodeEmitter();
if (opts::UseCompactAligner)
alignCompact(BF, Emitter.MCE.get());
else
alignMaxBytes(BF);
if (opts::AlignBlocks && !opts::PreserveBlocksAlignment)
alignBlocks(BF, Emitter.MCE.get());
};
ParallelUtilities::runOnEachFunction(
BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun,
ParallelUtilities::PredicateTy(nullptr), "AlignerPass");
LLVM_DEBUG(
dbgs() << "BOLT-DEBUG: max bytes per basic block alignment distribution:\n";
for (unsigned I = 1; I < AlignHistogram.size(); ++I)
dbgs() << " " << I << " : " << AlignHistogram[I] << '\n';
dbgs() << "BOLT-DEBUG: total execution count of aligned blocks: "
<< AlignedBlocksCount << '\n';
);
}
} // end namespace bolt
} // end namespace llvm