Skip to content

Commit c6c04a8

Browse files
authored
[BOLT] Run EliminateUnreachableBlocks in parallel (llvm#71299)
The wall time for this pass decreased on my laptop from ~80 sec to 5 sec processing the clang.
1 parent 73519ba commit c6c04a8

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,8 @@ class BinaryFunction {
14451445

14461446
/// Rebuilds BBs layout, ignoring dead BBs. Returns the number of removed
14471447
/// BBs and the removed number of bytes of code.
1448-
std::pair<unsigned, uint64_t> eraseInvalidBBs();
1448+
std::pair<unsigned, uint64_t>
1449+
eraseInvalidBBs(const MCCodeEmitter *Emitter = nullptr);
14491450

14501451
/// Get the relative order between two basic blocks in the original
14511452
/// layout. The result is > 0 if B occurs before A and < 0 if B

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ void BinaryFunction::markUnreachableBlocks() {
322322

323323
// Any unnecessary fallthrough jumps revealed after calling eraseInvalidBBs
324324
// will be cleaned up by fixBranches().
325-
std::pair<unsigned, uint64_t> BinaryFunction::eraseInvalidBBs() {
325+
std::pair<unsigned, uint64_t>
326+
BinaryFunction::eraseInvalidBBs(const MCCodeEmitter *Emitter) {
326327
DenseSet<const BinaryBasicBlock *> InvalidBBs;
327328
unsigned Count = 0;
328329
uint64_t Bytes = 0;
@@ -331,7 +332,7 @@ std::pair<unsigned, uint64_t> BinaryFunction::eraseInvalidBBs() {
331332
assert(!isEntryPoint(*BB) && "all entry blocks must be valid");
332333
InvalidBBs.insert(BB);
333334
++Count;
334-
Bytes += BC.computeCodeSize(BB->begin(), BB->end());
335+
Bytes += BC.computeCodeSize(BB->begin(), BB->end(), Emitter);
335336
}
336337
}
337338

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -317,38 +317,46 @@ void NormalizeCFG::runOnFunctions(BinaryContext &BC) {
317317
}
318318

319319
void EliminateUnreachableBlocks::runOnFunction(BinaryFunction &Function) {
320-
if (!Function.getLayout().block_empty()) {
321-
unsigned Count;
322-
uint64_t Bytes;
323-
Function.markUnreachableBlocks();
324-
LLVM_DEBUG({
325-
for (BinaryBasicBlock &BB : Function) {
326-
if (!BB.isValid()) {
327-
dbgs() << "BOLT-INFO: UCE found unreachable block " << BB.getName()
328-
<< " in function " << Function << "\n";
329-
Function.dump();
330-
}
320+
BinaryContext &BC = Function.getBinaryContext();
321+
unsigned Count;
322+
uint64_t Bytes;
323+
Function.markUnreachableBlocks();
324+
LLVM_DEBUG({
325+
for (BinaryBasicBlock &BB : Function) {
326+
if (!BB.isValid()) {
327+
dbgs() << "BOLT-INFO: UCE found unreachable block " << BB.getName()
328+
<< " in function " << Function << "\n";
329+
Function.dump();
331330
}
332-
});
333-
std::tie(Count, Bytes) = Function.eraseInvalidBBs();
334-
DeletedBlocks += Count;
335-
DeletedBytes += Bytes;
336-
if (Count) {
337-
Modified.insert(&Function);
338-
if (opts::Verbosity > 0)
339-
outs() << "BOLT-INFO: removed " << Count
340-
<< " dead basic block(s) accounting for " << Bytes
341-
<< " bytes in function " << Function << '\n';
342331
}
332+
});
333+
BinaryContext::IndependentCodeEmitter Emitter =
334+
BC.createIndependentMCCodeEmitter();
335+
std::tie(Count, Bytes) = Function.eraseInvalidBBs(Emitter.MCE.get());
336+
DeletedBlocks += Count;
337+
DeletedBytes += Bytes;
338+
if (Count) {
339+
auto L = BC.scopeLock();
340+
Modified.insert(&Function);
341+
if (opts::Verbosity > 0)
342+
outs() << "BOLT-INFO: removed " << Count
343+
<< " dead basic block(s) accounting for " << Bytes
344+
<< " bytes in function " << Function << '\n';
343345
}
344346
}
345347

346348
void EliminateUnreachableBlocks::runOnFunctions(BinaryContext &BC) {
347-
for (auto &It : BC.getBinaryFunctions()) {
348-
BinaryFunction &Function = It.second;
349-
if (shouldOptimize(Function))
350-
runOnFunction(Function);
351-
}
349+
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
350+
runOnFunction(BF);
351+
};
352+
353+
ParallelUtilities::PredicateTy SkipPredicate = [&](const BinaryFunction &BF) {
354+
return !shouldOptimize(BF) || BF.getLayout().block_empty();
355+
};
356+
357+
ParallelUtilities::runOnEachFunction(
358+
BC, ParallelUtilities::SchedulingPolicy::SP_CONSTANT, WorkFun,
359+
SkipPredicate, "elimininate-unreachable");
352360

353361
if (DeletedBlocks)
354362
outs() << "BOLT-INFO: UCE removed " << DeletedBlocks << " blocks and "

0 commit comments

Comments
 (0)