[llvm] [BOLT] Run EliminateUnreachableBlocks in parallel (PR #71299)
Vladislav Khmelevsky via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 4 15:00:23 PDT 2023
https://github.com/yota9 updated https://github.com/llvm/llvm-project/pull/71299
>From 171796eb45eac362347f6c5541ad708b6e94f4b4 Mon Sep 17 00:00:00 2001
From: Vladislav Khmelevsky <och95 at yandex.ru>
Date: Sun, 5 Nov 2023 01:57:13 +0400
Subject: [PATCH] [BOLT] Run EliminateUnreachableBlocks in parallel
The wall time for this pass decreased on my laptop from ~80 sec to 5
sec.
---
bolt/include/bolt/Core/BinaryFunction.h | 3 +-
bolt/lib/Core/BinaryFunction.cpp | 3 +-
bolt/lib/Passes/BinaryPasses.cpp | 60 ++++++++++++++-----------
3 files changed, 38 insertions(+), 28 deletions(-)
diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index c67ddccbf4892a7..771008f3b5f98e1 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -1445,7 +1445,8 @@ class BinaryFunction {
/// Rebuilds BBs layout, ignoring dead BBs. Returns the number of removed
/// BBs and the removed number of bytes of code.
- std::pair<unsigned, uint64_t> eraseInvalidBBs();
+ std::pair<unsigned, uint64_t>
+ eraseInvalidBBs(const MCCodeEmitter *Emitter = nullptr);
/// Get the relative order between two basic blocks in the original
/// layout. The result is > 0 if B occurs before A and < 0 if B
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index 2e1fa739e53f20b..361036316b524d8 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -322,7 +322,8 @@ void BinaryFunction::markUnreachableBlocks() {
// Any unnecessary fallthrough jumps revealed after calling eraseInvalidBBs
// will be cleaned up by fixBranches().
-std::pair<unsigned, uint64_t> BinaryFunction::eraseInvalidBBs() {
+std::pair<unsigned, uint64_t>
+BinaryFunction::eraseInvalidBBs(const MCCodeEmitter *Emitter) {
DenseSet<const BinaryBasicBlock *> InvalidBBs;
unsigned Count = 0;
uint64_t Bytes = 0;
diff --git a/bolt/lib/Passes/BinaryPasses.cpp b/bolt/lib/Passes/BinaryPasses.cpp
index e50fa9dea602bc7..cfd9e103a52d408 100644
--- a/bolt/lib/Passes/BinaryPasses.cpp
+++ b/bolt/lib/Passes/BinaryPasses.cpp
@@ -317,38 +317,46 @@ void NormalizeCFG::runOnFunctions(BinaryContext &BC) {
}
void EliminateUnreachableBlocks::runOnFunction(BinaryFunction &Function) {
- if (!Function.getLayout().block_empty()) {
- unsigned Count;
- uint64_t Bytes;
- Function.markUnreachableBlocks();
- LLVM_DEBUG({
- for (BinaryBasicBlock &BB : Function) {
- if (!BB.isValid()) {
- dbgs() << "BOLT-INFO: UCE found unreachable block " << BB.getName()
- << " in function " << Function << "\n";
- Function.dump();
- }
+ BinaryContext &BC = Function.getBinaryContext();
+ unsigned Count;
+ uint64_t Bytes;
+ Function.markUnreachableBlocks();
+ LLVM_DEBUG({
+ for (BinaryBasicBlock &BB : Function) {
+ if (!BB.isValid()) {
+ dbgs() << "BOLT-INFO: UCE found unreachable block " << BB.getName()
+ << " in function " << Function << "\n";
+ Function.dump();
}
- });
- std::tie(Count, Bytes) = Function.eraseInvalidBBs();
- DeletedBlocks += Count;
- DeletedBytes += Bytes;
- if (Count) {
- Modified.insert(&Function);
- if (opts::Verbosity > 0)
- outs() << "BOLT-INFO: removed " << Count
- << " dead basic block(s) accounting for " << Bytes
- << " bytes in function " << Function << '\n';
}
+ });
+ BinaryContext::IndependentCodeEmitter Emitter =
+ BC.createIndependentMCCodeEmitter();
+ std::tie(Count, Bytes) = Function.eraseInvalidBBs(Emitter.MCE.get());
+ DeletedBlocks += Count;
+ DeletedBytes += Bytes;
+ if (Count) {
+ auto L = BC.scopeLock();
+ Modified.insert(&Function);
+ if (opts::Verbosity > 0)
+ outs() << "BOLT-INFO: removed " << Count
+ << " dead basic block(s) accounting for " << Bytes
+ << " bytes in function " << Function << '\n';
}
}
void EliminateUnreachableBlocks::runOnFunctions(BinaryContext &BC) {
- for (auto &It : BC.getBinaryFunctions()) {
- BinaryFunction &Function = It.second;
- if (shouldOptimize(Function))
- runOnFunction(Function);
- }
+ ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
+ runOnFunction(BF);
+ };
+
+ ParallelUtilities::PredicateTy SkipPredicate = [&](const BinaryFunction &BF) {
+ return !shouldOptimize(BF) || BF.getLayout().block_empty();
+ };
+
+ ParallelUtilities::runOnEachFunction(
+ BC, ParallelUtilities::SchedulingPolicy::SP_CONSTANT, WorkFun,
+ SkipPredicate, "elimininate-unreachable");
if (DeletedBlocks)
outs() << "BOLT-INFO: UCE removed " << DeletedBlocks << " blocks and "
More information about the llvm-commits
mailing list