Skip to content

[BOLT] Refactor interface for instruction labels. NFCI #83209

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

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions bolt/include/bolt/Core/MCPlusBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1183,11 +1183,16 @@ class MCPlusBuilder {
bool clearOffset(MCInst &Inst) const;

/// Return the label of \p Inst, if available.
MCSymbol *getLabel(const MCInst &Inst) const;
MCSymbol *getInstLabel(const MCInst &Inst) const;

/// Set the label of \p Inst or return the existing label for the instruction.
/// This label will be emitted right before \p Inst is emitted to MCStreamer.
MCSymbol *getOrCreateInstLabel(MCInst &Inst, const Twine &Name,
MCContext *Ctx) const;

/// Set the label of \p Inst. This label will be emitted right before \p Inst
/// is emitted to MCStreamer.
bool setLabel(MCInst &Inst, MCSymbol *Label) const;
void setInstLabel(MCInst &Inst, MCSymbol *Label) const;

/// Get instruction size specified via annotation.
std::optional<uint32_t> getSize(const MCInst &Inst) const;
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Core/BinaryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
OS << " # Offset: " << *Offset;
if (std::optional<uint32_t> Size = MIB->getSize(Instruction))
OS << " # Size: " << *Size;
if (MCSymbol *Label = MIB->getLabel(Instruction))
if (MCSymbol *Label = MIB->getInstLabel(Instruction))
OS << " # Label: " << *Label;

MIB->printAnnotations(Instruction, OS);
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Core/BinaryEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ void BinaryEmitter::emitFunctionBody(BinaryFunction &BF, FunctionFragment &FF,

if (!EmitCodeOnly) {
// A symbol to be emitted before the instruction to mark its location.
MCSymbol *InstrLabel = BC.MIB->getLabel(Instr);
MCSymbol *InstrLabel = BC.MIB->getInstLabel(Instr);

if (opts::UpdateDebugSections && BF.getDWARFUnit()) {
LastLocSeen = emitLineInfo(BF, Instr.getLoc(), LastLocSeen,
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Core/BinaryFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ Error BinaryFunction::disassemble() {
InstrMapType::iterator II = Instructions.find(Offset);
assert(II != Instructions.end() && "reference to non-existing instruction");

BC.MIB->setLabel(II->second, Label);
BC.MIB->setInstLabel(II->second, Label);
}

// Reset symbolizer for the disassembler.
Expand Down
5 changes: 2 additions & 3 deletions bolt/lib/Core/Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,11 @@ void BinaryFunction::updateEHRanges() {

// Same symbol is used for the beginning and the end of the range.
MCSymbol *EHSymbol;
if (MCSymbol *InstrLabel = BC.MIB->getLabel(Instr)) {
if (MCSymbol *InstrLabel = BC.MIB->getInstLabel(Instr)) {
EHSymbol = InstrLabel;
} else {
std::unique_lock<llvm::sys::RWMutex> Lock(BC.CtxMutex);
EHSymbol = BC.Ctx->createNamedTempSymbol("EH");
BC.MIB->setLabel(Instr, EHSymbol);
EHSymbol = BC.MIB->getOrCreateInstLabel(Instr, "EH", BC.Ctx.get());
}

// At this point we could be in one of the following states:
Expand Down
19 changes: 16 additions & 3 deletions bolt/lib/Core/MCPlusBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "bolt/Core/MCPlusBuilder.h"
#include "bolt/Core/MCPlus.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstrAnalysis.h"
#include "llvm/MC/MCInstrDesc.h"
Expand Down Expand Up @@ -266,17 +267,29 @@ bool MCPlusBuilder::clearOffset(MCInst &Inst) const {
return true;
}

MCSymbol *MCPlusBuilder::getLabel(const MCInst &Inst) const {
MCSymbol *MCPlusBuilder::getInstLabel(const MCInst &Inst) const {
if (std::optional<int64_t> Label =
getAnnotationOpValue(Inst, MCAnnotation::kLabel))
return reinterpret_cast<MCSymbol *>(*Label);
return nullptr;
}

bool MCPlusBuilder::setLabel(MCInst &Inst, MCSymbol *Label) const {
MCSymbol *MCPlusBuilder::getOrCreateInstLabel(MCInst &Inst, const Twine &Name,
MCContext *Ctx) const {
MCSymbol *Label = getInstLabel(Inst);
if (Label)
return Label;

Label = Ctx->createNamedTempSymbol(Name);
setAnnotationOpValue(Inst, MCAnnotation::kLabel,
reinterpret_cast<int64_t>(Label));
return Label;
}

void MCPlusBuilder::setInstLabel(MCInst &Inst, MCSymbol *Label) const {
assert(!getInstLabel(Inst) && "Instruction already has assigned label.");
setAnnotationOpValue(Inst, MCAnnotation::kLabel,
reinterpret_cast<int64_t>(Label));
return true;
}

std::optional<uint32_t> MCPlusBuilder::getSize(const MCInst &Inst) const {
Expand Down
14 changes: 4 additions & 10 deletions bolt/lib/Rewrite/LinuxKernelRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,11 +770,8 @@ Error LinuxKernelRewriter::rewriteORCTables() {
continue;

// Issue label for the instruction.
MCSymbol *Label = BC.MIB->getLabel(Inst);
if (!Label) {
Label = BC.Ctx->createTempSymbol("__ORC_");
BC.MIB->setLabel(Inst, Label);
}
MCSymbol *Label =
BC.MIB->getOrCreateInstLabel(Inst, "__ORC_", BC.Ctx.get());

if (Error E = emitORCEntry(0, *ErrorOrState, Label))
return E;
Expand Down Expand Up @@ -908,11 +905,8 @@ Error LinuxKernelRewriter::readStaticCalls() {

BC.MIB->addAnnotation(*Inst, "StaticCall", EntryID);

MCSymbol *Label = BC.MIB->getLabel(*Inst);
if (!Label) {
Label = BC.Ctx->createTempSymbol("__SC_");
BC.MIB->setLabel(*Inst, Label);
}
MCSymbol *Label =
BC.MIB->getOrCreateInstLabel(*Inst, "__SC_", BC.Ctx.get());

StaticCallEntries.push_back({EntryID, BF, Label});
}
Expand Down