Skip to content

Commit f99bd29

Browse files
authored
[BOLT][NFC] Run ADRRelaxationPass in parallel (#67831)
To do this: 1. Protect BC.Ctx with mutex 2. Don't call exit from thread, please check the reason comment near PassFailed variable definition. The other option would be call _Exit instead of exit, but I think we shall call destructors properly.
1 parent 061adc1 commit f99bd29

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

bolt/lib/Passes/ADRRelaxationPass.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ static cl::opt<bool>
2929
namespace llvm {
3030
namespace bolt {
3131

32+
// We don't exit directly from runOnFunction since it would call ThreadPool
33+
// destructor which might result in internal assert if we're not finished
34+
// creating async jobs on the moment of exit. So we're finishing all parallel
35+
// jobs and checking the exit flag after it.
36+
static bool PassFailed = false;
37+
3238
void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
39+
if (PassFailed)
40+
return;
41+
3342
BinaryContext &BC = BF.getBinaryContext();
3443
for (BinaryBasicBlock &BB : BF) {
3544
for (auto It = BB.begin(); It != BB.end(); ++It) {
@@ -54,8 +63,12 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
5463
MCPhysReg Reg;
5564
BC.MIB->getADRReg(Inst, Reg);
5665
int64_t Addend = BC.MIB->getTargetAddend(Inst);
57-
InstructionListType Addr =
58-
BC.MIB->materializeAddress(Symbol, BC.Ctx.get(), Reg, Addend);
66+
InstructionListType Addr;
67+
68+
{
69+
auto L = BC.scopeLock();
70+
Addr = BC.MIB->materializeAddress(Symbol, BC.Ctx.get(), Reg, Addend);
71+
}
5972

6073
if (It != BB.begin() && BC.MIB->isNoop(*std::prev(It))) {
6174
It = BB.eraseInstruction(std::prev(It));
@@ -68,7 +81,8 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
6881
errs() << formatv("BOLT-ERROR: Cannot relax adr in non-simple function "
6982
"{0}. Can't proceed in current mode.\n",
7083
BF.getOneName());
71-
exit(1);
84+
PassFailed = true;
85+
return;
7286
}
7387
It = BB.replaceInstruction(It, Addr);
7488
}
@@ -85,7 +99,10 @@ void ADRRelaxationPass::runOnFunctions(BinaryContext &BC) {
8599

86100
ParallelUtilities::runOnEachFunction(
87101
BC, ParallelUtilities::SchedulingPolicy::SP_TRIVIAL, WorkFun, nullptr,
88-
"ADRRelaxationPass", /* ForceSequential */ true);
102+
"ADRRelaxationPass");
103+
104+
if (PassFailed)
105+
exit(1);
89106
}
90107

91108
} // end namespace bolt

0 commit comments

Comments
 (0)