Skip to content

Commit 8ea7f1d

Browse files
authored
[BOLT][NFCI] Keep instruction annotations (#80382)
We used to delete most instruction annotations before code emission. It was done to release memory taken by annotations and to reduce overall memory consumption. However, since the implementation of annotations has moved to using existing instruction operands, the memory overhead associated with them has reduced drastically. I measured that savings are less than 0.5% on large binaries and processing time is just slightly reduced if we keep them. Additionally, I plan to use annotations in pre-emission passes for the Linux kernel rewriter.
1 parent c7fa25f commit 8ea7f1d

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -582,50 +582,31 @@ bool CheckLargeFunctions::shouldOptimize(const BinaryFunction &BF) const {
582582
}
583583

584584
void LowerAnnotations::runOnFunctions(BinaryContext &BC) {
585+
// Convert GnuArgsSize annotations into CFIs.
585586
for (BinaryFunction *BF : BC.getAllBinaryFunctions()) {
586587
for (FunctionFragment &FF : BF->getLayout().fragments()) {
587588
// Reset at the start of the new fragment.
588589
int64_t CurrentGnuArgsSize = 0;
589590

590591
for (BinaryBasicBlock *const BB : FF) {
591592
for (auto II = BB->begin(); II != BB->end(); ++II) {
592-
593-
// Convert GnuArgsSize annotations into CFIs.
594-
if (BF->usesGnuArgsSize() && BC.MIB->isInvoke(*II)) {
595-
const int64_t NewGnuArgsSize = BC.MIB->getGnuArgsSize(*II);
596-
assert(NewGnuArgsSize >= 0 &&
597-
"Expected non-negative GNU_args_size.");
598-
if (NewGnuArgsSize != CurrentGnuArgsSize) {
599-
auto InsertII = BF->addCFIInstruction(
600-
BB, II,
601-
MCCFIInstruction::createGnuArgsSize(nullptr, NewGnuArgsSize));
602-
CurrentGnuArgsSize = NewGnuArgsSize;
603-
II = std::next(InsertII);
604-
}
605-
}
606-
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-
std::optional<uint32_t> Size = BC.MIB->getSize(*II);
612-
MCSymbol *Label = BC.MIB->getLabel(*II);
613-
614-
BC.MIB->stripAnnotations(*II);
615-
616-
if (Offset)
617-
BC.MIB->setOffset(*II, *Offset);
618-
if (Size)
619-
BC.MIB->setSize(*II, *Size);
620-
if (Label)
621-
BC.MIB->setLabel(*II, Label);
593+
if (!BF->usesGnuArgsSize() || !BC.MIB->isInvoke(*II))
594+
continue;
595+
596+
const int64_t NewGnuArgsSize = BC.MIB->getGnuArgsSize(*II);
597+
assert(NewGnuArgsSize >= 0 && "Expected non-negative GNU_args_size.");
598+
if (NewGnuArgsSize == CurrentGnuArgsSize)
599+
continue;
600+
601+
auto InsertII = BF->addCFIInstruction(
602+
BB, II,
603+
MCCFIInstruction::createGnuArgsSize(nullptr, NewGnuArgsSize));
604+
CurrentGnuArgsSize = NewGnuArgsSize;
605+
II = std::next(InsertII);
622606
}
623607
}
624608
}
625609
}
626-
627-
// Release all memory taken by annotations
628-
BC.MIB->freeAnnotations();
629610
}
630611

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

0 commit comments

Comments
 (0)