Skip to content

Commit

Permalink
[BOLT] Enhance LowerAnnotations pass. NFCI. (llvm#71847)
Browse files Browse the repository at this point in the history
After llvm#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.
  • Loading branch information
maksfb authored and zahiraam committed Nov 20, 2023
1 parent 743daf1 commit ec1ba20
Showing 1 changed file with 20 additions and 38 deletions.
58 changes: 20 additions & 38 deletions bolt/lib/Passes/BinaryPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,65 +582,47 @@ bool CheckLargeFunctions::shouldOptimize(const BinaryFunction &BF) const {
}

void LowerAnnotations::runOnFunctions(BinaryContext &BC) {
std::vector<std::pair<MCInst *, uint32_t>> PreservedOffsetAnnotations;
std::vector<std::pair<MCInst *, MCSymbol *>> PreservedLabelAnnotations;

for (auto &It : BC.getBinaryFunctions()) {
BinaryFunction &BF = It.second;

for (FunctionFragment &FF : BF.getLayout().fragments()) {
for (BinaryFunction *BF : BC.getAllBinaryFunctions()) {
for (FunctionFragment &FF : BF->getLayout().fragments()) {
// Reset at the start of the new fragment.
int64_t CurrentGnuArgsSize = 0;

for (BinaryBasicBlock *const BB : FF) {
// First convert GnuArgsSize annotations into CFIs. This may change
// instr pointers, so do it before recording ptrs for preserved
// annotations
if (BF.usesGnuArgsSize()) {
for (auto II = BB->begin(); II != BB->end(); ++II) {
if (!BC.MIB->isInvoke(*II))
continue;
for (auto II = BB->begin(); II != BB->end(); ++II) {

// Convert GnuArgsSize annotations into CFIs.
if (BF->usesGnuArgsSize() && BC.MIB->isInvoke(*II)) {
const int64_t NewGnuArgsSize = BC.MIB->getGnuArgsSize(*II);
assert(NewGnuArgsSize >= 0 &&
"expected non-negative GNU_args_size");
"Expected non-negative GNU_args_size.");
if (NewGnuArgsSize != CurrentGnuArgsSize) {
auto InsertII = BF.addCFIInstruction(
auto InsertII = BF->addCFIInstruction(
BB, II,
MCCFIInstruction::createGnuArgsSize(nullptr, NewGnuArgsSize));
CurrentGnuArgsSize = NewGnuArgsSize;
II = std::next(InsertII);
}
}
}

// Now record preserved annotations separately and then strip
// annotations.
for (auto II = BB->begin(); II != BB->end(); ++II) {
if (BF.requiresAddressTranslation() && BC.MIB->getOffset(*II))
PreservedOffsetAnnotations.emplace_back(&(*II),
*BC.MIB->getOffset(*II));
if (MCSymbol *Label = BC.MIB->getLabel(*II))
PreservedLabelAnnotations.emplace_back(&*II, Label);
// Preserve selected annotations and strip the rest.
std::optional<uint32_t> Offset = BF->requiresAddressTranslation()
? BC.MIB->getOffset(*II)
: std::nullopt;
MCSymbol *Label = BC.MIB->getLabel(*II);

BC.MIB->stripAnnotations(*II);

if (Offset)
BC.MIB->setOffset(*II, *Offset);
if (Label)
BC.MIB->setLabel(*II, Label);
}
}
}
}
for (BinaryFunction *BF : BC.getInjectedBinaryFunctions())
for (BinaryBasicBlock &BB : *BF)
for (MCInst &Instruction : BB) {
if (MCSymbol *Label = BC.MIB->getLabel(Instruction))
PreservedLabelAnnotations.emplace_back(&Instruction, Label);
BC.MIB->stripAnnotations(Instruction);
}

// Release all memory taken by annotations
BC.MIB->freeAnnotations();

// Reinsert preserved annotations we need during code emission.
for (const std::pair<MCInst *, uint32_t> &Item : PreservedOffsetAnnotations)
BC.MIB->setOffset(*Item.first, Item.second);
for (auto [Instr, Label] : PreservedLabelAnnotations)
BC.MIB->setLabel(*Instr, Label);
}

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

0 comments on commit ec1ba20

Please sign in to comment.