Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BOLT] Enhance LowerAnnotations pass. NFCI. #71847

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 20 additions & 38 deletions bolt/lib/Passes/BinaryPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,65 +574,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