Skip to content

Commit

Permalink
Merge rust-lang#49
Browse files Browse the repository at this point in the history
49: Switch to section start/stop markers for the `.llvm_bb_addr_map` section. r=ltratt a=vext01



Co-authored-by: Edd Barrett <vext01@gmail.com>
  • Loading branch information
bors[bot] and vext01 authored Nov 9, 2022
2 parents ca22613 + 290cdcb commit 9ea6491
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
7 changes: 7 additions & 0 deletions llvm/include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ class AsmPrinter : public MachineFunctionPass {
/// split stack prologue.
bool HasNoSplitStack = false;

/// `true` when the `.llvm_bb_addr_map` section is going to be loaded at
/// runtime and we've emitted at least one entry.
bool YkEmittedFirstBBAddrMap = false;

/// The last `.llvm_bb_addr_map` section fragment that we handled (if any).
MCSection *YkLastBBAddrMapSection = nullptr;

protected:
explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);

Expand Down
43 changes: 31 additions & 12 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,16 +1326,24 @@ static unsigned getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
(const_cast<MachineBasicBlock &>(MBB).canFallThrough() << 3);
}

void emitYkBBAddrMapSymbol(const MachineFunction &MF, MCStreamer &OutStreamer,
/// Emit a start (or stop) marker symbol into the `.llvm_bb_addr_map` section
/// so that we can find the extent of the section at runtime.
///
/// The `MCStreamer` should be primed to output to the `.llvm_bb_addr_map`
/// section prior to calling this function.
///
/// This assumes that LTO is being used (as is required for the Yk JIT), and
/// thus that there is only a single `Module` in play, and in turn that no
/// symbol clashes can occur.
void emitYkBBAddrMapSymbol(MCContext &MCtxt, MCStreamer &OutStreamer,
bool Start) {
std::string SymName("ykllvm.bbaddrmap.");
SymName.append(MF.getName().str());
std::string SymName("ykllvm.bbaddrmaps");
if (Start)
SymName.append(".start");
else
SymName.append(".end");
SymName.append(".stop");

MCSymbol *Sym = MF.getContext().getOrCreateSymbol(SymName);
MCSymbol *Sym = MCtxt.getOrCreateSymbol(SymName);
OutStreamer.emitSymbolAttribute(Sym, llvm::MCSA_Global);
OutStreamer.emitLabel(Sym);
}
Expand All @@ -1350,9 +1358,16 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
OutStreamer->pushSection();
OutStreamer->switchSection(BBAddrMapSection);

// Add the `ykllvm.bbaddrmap.<func>.start` symbol.
if (YkAllocLLVMBBAddrMapSection)
emitYkBBAddrMapSymbol(MF, *OutStreamer, true);
if (YkAllocLLVMBBAddrMapSection) {
if (!YkEmittedFirstBBAddrMap) {
// Add the `ykllvm.bbaddrmaps.start` symbol.
emitYkBBAddrMapSymbol(MF.getContext(), *OutStreamer, true);
YkEmittedFirstBBAddrMap = true;
}
// We cache the last seen bbaddrmap section fragment so that we can insert
// the stop symbol when the asmprinter is finalising.
YkLastBBAddrMapSection = BBAddrMapSection;
}

OutStreamer->AddComment("version");
OutStreamer->emitInt8(OutStreamer->getContext().getBBAddrMapVersion());
Expand Down Expand Up @@ -1443,10 +1458,6 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
}
}

// Add the `ykllvm.bbaddrmap.<func>.end` symbol.
if (YkAllocLLVMBBAddrMapSection)
emitYkBBAddrMapSymbol(MF, *OutStreamer, false);

OutStreamer->popSection();
}

Expand Down Expand Up @@ -2030,6 +2041,14 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
}

bool AsmPrinter::doFinalization(Module &M) {
if (YkAllocLLVMBBAddrMapSection && YkEmittedFirstBBAddrMap) {
// Add the `ykllvm.bbaddrmaps.stop` symbol.
OutStreamer->pushSection();
OutStreamer->switchSection(YkLastBBAddrMapSection);
emitYkBBAddrMapSymbol(OutContext, *OutStreamer, false);
OutStreamer->popSection();
}

// The `embed-bitcode` flag serialises the IR after only architecture
// agnostic optimisations have been run, but then proceeds to apply other
// optimisations and transformations afterwards. Sometimes this final version
Expand Down

0 comments on commit 9ea6491

Please sign in to comment.