Skip to content

Commit ec4a03c

Browse files
authored
[BOLT] Enhance LowerAnnotations pass. NFCI. (#71847)
After #70147, all primary annotation types are stored directly in the instruction and hence there's no need for the temporary storage we've used previously for repopulating preserved annotations.
1 parent edc38a6 commit ec4a03c

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -582,65 +582,47 @@ bool CheckLargeFunctions::shouldOptimize(const BinaryFunction &BF) const {
582582
}
583583

584584
void LowerAnnotations::runOnFunctions(BinaryContext &BC) {
585-
std::vector<std::pair<MCInst *, uint32_t>> PreservedOffsetAnnotations;
586-
std::vector<std::pair<MCInst *, MCSymbol *>> PreservedLabelAnnotations;
587-
588-
for (auto &It : BC.getBinaryFunctions()) {
589-
BinaryFunction &BF = It.second;
590-
591-
for (FunctionFragment &FF : BF.getLayout().fragments()) {
585+
for (BinaryFunction *BF : BC.getAllBinaryFunctions()) {
586+
for (FunctionFragment &FF : BF->getLayout().fragments()) {
587+
// Reset at the start of the new fragment.
592588
int64_t CurrentGnuArgsSize = 0;
593589

594590
for (BinaryBasicBlock *const BB : FF) {
595-
// First convert GnuArgsSize annotations into CFIs. This may change
596-
// instr pointers, so do it before recording ptrs for preserved
597-
// annotations
598-
if (BF.usesGnuArgsSize()) {
599-
for (auto II = BB->begin(); II != BB->end(); ++II) {
600-
if (!BC.MIB->isInvoke(*II))
601-
continue;
591+
for (auto II = BB->begin(); II != BB->end(); ++II) {
592+
593+
// Convert GnuArgsSize annotations into CFIs.
594+
if (BF->usesGnuArgsSize() && BC.MIB->isInvoke(*II)) {
602595
const int64_t NewGnuArgsSize = BC.MIB->getGnuArgsSize(*II);
603596
assert(NewGnuArgsSize >= 0 &&
604-
"expected non-negative GNU_args_size");
597+
"Expected non-negative GNU_args_size.");
605598
if (NewGnuArgsSize != CurrentGnuArgsSize) {
606-
auto InsertII = BF.addCFIInstruction(
599+
auto InsertII = BF->addCFIInstruction(
607600
BB, II,
608601
MCCFIInstruction::createGnuArgsSize(nullptr, NewGnuArgsSize));
609602
CurrentGnuArgsSize = NewGnuArgsSize;
610603
II = std::next(InsertII);
611604
}
612605
}
613-
}
614606

615-
// Now record preserved annotations separately and then strip
616-
// annotations.
617-
for (auto II = BB->begin(); II != BB->end(); ++II) {
618-
if (BF.requiresAddressTranslation() && BC.MIB->getOffset(*II))
619-
PreservedOffsetAnnotations.emplace_back(&(*II),
620-
*BC.MIB->getOffset(*II));
621-
if (MCSymbol *Label = BC.MIB->getLabel(*II))
622-
PreservedLabelAnnotations.emplace_back(&*II, Label);
607+
// Preserve selected annotations and strip the rest.
608+
std::optional<uint32_t> Offset = BF->requiresAddressTranslation()
609+
? BC.MIB->getOffset(*II)
610+
: std::nullopt;
611+
MCSymbol *Label = BC.MIB->getLabel(*II);
612+
623613
BC.MIB->stripAnnotations(*II);
614+
615+
if (Offset)
616+
BC.MIB->setOffset(*II, *Offset);
617+
if (Label)
618+
BC.MIB->setLabel(*II, Label);
624619
}
625620
}
626621
}
627622
}
628-
for (BinaryFunction *BF : BC.getInjectedBinaryFunctions())
629-
for (BinaryBasicBlock &BB : *BF)
630-
for (MCInst &Instruction : BB) {
631-
if (MCSymbol *Label = BC.MIB->getLabel(Instruction))
632-
PreservedLabelAnnotations.emplace_back(&Instruction, Label);
633-
BC.MIB->stripAnnotations(Instruction);
634-
}
635623

636624
// Release all memory taken by annotations
637625
BC.MIB->freeAnnotations();
638-
639-
// Reinsert preserved annotations we need during code emission.
640-
for (const std::pair<MCInst *, uint32_t> &Item : PreservedOffsetAnnotations)
641-
BC.MIB->setOffset(*Item.first, Item.second);
642-
for (auto [Instr, Label] : PreservedLabelAnnotations)
643-
BC.MIB->setLabel(*Instr, Label);
644626
}
645627

646628
// Check for dirty state in MCSymbol objects that might be a consequence

0 commit comments

Comments
 (0)