Skip to content

Commit eb5910d

Browse files
committed
[BOLT] Simplify
As fully unknown blocks (stubs) don't have correct unwind info, guessing their RA state is also futile. Remove code implementing this.
1 parent b623c64 commit eb5910d

File tree

1 file changed

+11
-96
lines changed

1 file changed

+11
-96
lines changed

bolt/lib/Passes/InsertNegateRAStatePass.cpp

Lines changed: 11 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
8181
for (BinaryBasicBlock &BB : BF) {
8282
fillUnknownStateInBB(BC, BB);
8383
}
84-
// Some stubs have no predecessors. For those, we iterate once in the layout
85-
// order to fill their RAState.
84+
// BasicBlocks which are made entirely of "new instructions" (instructions
85+
// without RAState annotation) are stubs, and do not have correct unwind info.
86+
// We should iterate in layout order and fill them based on previous known
87+
// RAState.
8688
fillUnknownStubs(BF);
87-
88-
fillUnknownBlocksInCFG(BF);
8989
}
9090

9191
void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
@@ -206,24 +206,21 @@ void InsertNegateRAState::fillUnknownStubs(BinaryFunction &BF) {
206206
for (FunctionFragment &FF : BF.getLayout().fragments()) {
207207
for (BinaryBasicBlock *BB : FF) {
208208
if (!FirstIter && isUnknownBlock(BC, *BB)) {
209-
// If we have no predecessors or successors, current BB is a Stub called
210-
// from another BinaryFunction. As of #160989, we have to copy the
209+
// As of #160989, we have to copy the
211210
// PrevInst's RAState, because CFIs are already incorrect here.
212-
if (BB->pred_size() == 0 && BB->succ_size() == 0) {
213-
auto PrevRAState = BC.MIB->getRAState(PrevInst);
214-
if (!PrevRAState) {
215-
llvm_unreachable(
216-
"Previous Instruction has no RAState in fillUnknownStubs.");
217-
continue;
218-
}
211+
auto PrevRAState = BC.MIB->getRAState(PrevInst);
212+
if (!PrevRAState) {
213+
llvm_unreachable(
214+
"Previous Instruction has no RAState in fillUnknownStubs.");
215+
continue;
216+
}
219217

220218
if (BC.MIB->isPSignOnLR(PrevInst)) {
221219
PrevRAState = true;
222220
} else if (BC.MIB->isPAuthOnLR(PrevInst)) {
223221
PrevRAState = false;
224222
}
225223
markUnknownBlock(BC, *BB, *PrevRAState);
226-
}
227224
}
228225
if (FirstIter) {
229226
FirstIter = false;
@@ -240,88 +237,6 @@ void InsertNegateRAState::fillUnknownStubs(BinaryFunction &BF) {
240237
}
241238
}
242239

243-
std::optional<bool> InsertNegateRAState::getRAStateByCFG(BinaryBasicBlock &BB,
244-
BinaryFunction &BF) {
245-
BinaryContext &BC = BF.getBinaryContext();
246-
247-
auto checkRAState = [&](std::optional<bool> &NeighborRAState, MCInst &Inst) {
248-
auto RAState = BC.MIB->getRAState(Inst);
249-
if (!RAState)
250-
return;
251-
if (!NeighborRAState) {
252-
NeighborRAState = *RAState;
253-
return;
254-
}
255-
if (NeighborRAState != *RAState) {
256-
BC.outs() << "BOLT-WARNING: Conflicting RAState found in function "
257-
<< BF.getPrintName() << ". Function will not be optimized.\n";
258-
BF.setIgnored();
259-
}
260-
};
261-
262-
// Holds the first found RAState from CFG neighbors.
263-
std::optional<bool> NeighborRAState = std::nullopt;
264-
if (BB.pred_size() != 0) {
265-
for (BinaryBasicBlock *PredBB : BB.predecessors()) {
266-
// find last inst of Predecessor with known RA State.
267-
auto LI = PredBB->getLastNonPseudo();
268-
if (LI == PredBB->rend())
269-
continue;
270-
MCInst &LastInst = *LI;
271-
checkRAState(NeighborRAState, LastInst);
272-
}
273-
} else if (BB.succ_size() != 0) {
274-
for (BinaryBasicBlock *SuccBB : BB.successors()) {
275-
// find first inst of Successor with known RA State.
276-
auto FI = SuccBB->getFirstNonPseudo();
277-
if (FI == SuccBB->end())
278-
continue;
279-
MCInst &FirstInst = *FI;
280-
checkRAState(NeighborRAState, FirstInst);
281-
}
282-
} else {
283-
llvm_unreachable("Called getRAStateByCFG on a BB with no preds or succs.");
284-
}
285-
286-
return NeighborRAState;
287-
}
288-
289-
void InsertNegateRAState::fillUnknownBlocksInCFG(BinaryFunction &BF) {
290-
BinaryContext &BC = BF.getBinaryContext();
291-
292-
auto fillUnknowns = [&](BinaryFunction &BF) -> std::pair<int, bool> {
293-
int Unknowns = 0;
294-
bool Updated = false;
295-
for (BinaryBasicBlock &BB : BF) {
296-
// Only try to iterate if the BB has either predecessors or successors.
297-
if (isUnknownBlock(BC, BB) &&
298-
(BB.pred_size() != 0 || BB.succ_size() != 0)) {
299-
auto RAStateOpt = getRAStateByCFG(BB, BF);
300-
if (RAStateOpt) {
301-
markUnknownBlock(BC, BB, *RAStateOpt);
302-
Updated = true;
303-
} else {
304-
Unknowns++;
305-
}
306-
}
307-
}
308-
return std::pair<int, bool>{Unknowns, Updated};
309-
};
310-
311-
while (true) {
312-
std::pair<int, bool> Iter = fillUnknowns(BF);
313-
if (Iter.first == 0)
314-
break;
315-
if (!Iter.second) {
316-
BC.errs() << "BOLT-WARNING: Could not infer RAState for " << Iter.first
317-
<< " BBs in function " << BF.getPrintName()
318-
<< ". Function will not be optimized.\n";
319-
BF.setIgnored();
320-
break;
321-
}
322-
}
323-
}
324-
325240
Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
326241
std::atomic<uint64_t> FunctionsModified{0};
327242
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {

0 commit comments

Comments
 (0)