Skip to content

Commit 290cdcb

Browse files
committed
Switch to section start/stop markers for the .llvm_bb_addr_map section.
Prior to this change, each *function* would get a start/stop symbol pair and the runtime would need to know the names of all the functions in order to load each map from the `.llvm_bb_addr_map` section. The problem with that approach is that to know the names of all of the functions, you need to also have the LLVM IR in order to iterate over the functions in the module. To do that, you need to have the LLVM IR parser, which means linking LLVM. In upcoming work (for the fast ykpt trace decoder), the block map parser will need to be moved out of `yk` and into `hwtracer`, so that hwtracer can pick up the decoding hints we are going to add to the block map. It seems unreasonable and cumbersome for `hwtracer` to have to link LLVM just to load the blockmap. This change uses *section* start and stop symbols (instead of per-function start/stop symbols), so that `hwtracer` will be able to load the whole blockmap section without any knowledge of LLVM IR. It is probably also a bit quicker at runtime, since we no longer need to iterate over the functions in the IR and load their maps individually. (The approach assumes that LTO is being used, which is already a requirement of Yk).
1 parent ca22613 commit 290cdcb

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ class AsmPrinter : public MachineFunctionPass {
226226
/// split stack prologue.
227227
bool HasNoSplitStack = false;
228228

229+
/// `true` when the `.llvm_bb_addr_map` section is going to be loaded at
230+
/// runtime and we've emitted at least one entry.
231+
bool YkEmittedFirstBBAddrMap = false;
232+
233+
/// The last `.llvm_bb_addr_map` section fragment that we handled (if any).
234+
MCSection *YkLastBBAddrMapSection = nullptr;
235+
229236
protected:
230237
explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
231238

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,16 +1326,24 @@ static unsigned getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
13261326
(const_cast<MachineBasicBlock &>(MBB).canFallThrough() << 3);
13271327
}
13281328

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

1338-
MCSymbol *Sym = MF.getContext().getOrCreateSymbol(SymName);
1346+
MCSymbol *Sym = MCtxt.getOrCreateSymbol(SymName);
13391347
OutStreamer.emitSymbolAttribute(Sym, llvm::MCSA_Global);
13401348
OutStreamer.emitLabel(Sym);
13411349
}
@@ -1350,9 +1358,16 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
13501358
OutStreamer->pushSection();
13511359
OutStreamer->switchSection(BBAddrMapSection);
13521360

1353-
// Add the `ykllvm.bbaddrmap.<func>.start` symbol.
1354-
if (YkAllocLLVMBBAddrMapSection)
1355-
emitYkBBAddrMapSymbol(MF, *OutStreamer, true);
1361+
if (YkAllocLLVMBBAddrMapSection) {
1362+
if (!YkEmittedFirstBBAddrMap) {
1363+
// Add the `ykllvm.bbaddrmaps.start` symbol.
1364+
emitYkBBAddrMapSymbol(MF.getContext(), *OutStreamer, true);
1365+
YkEmittedFirstBBAddrMap = true;
1366+
}
1367+
// We cache the last seen bbaddrmap section fragment so that we can insert
1368+
// the stop symbol when the asmprinter is finalising.
1369+
YkLastBBAddrMapSection = BBAddrMapSection;
1370+
}
13561371

13571372
OutStreamer->AddComment("version");
13581373
OutStreamer->emitInt8(OutStreamer->getContext().getBBAddrMapVersion());
@@ -1443,10 +1458,6 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14431458
}
14441459
}
14451460

1446-
// Add the `ykllvm.bbaddrmap.<func>.end` symbol.
1447-
if (YkAllocLLVMBBAddrMapSection)
1448-
emitYkBBAddrMapSymbol(MF, *OutStreamer, false);
1449-
14501461
OutStreamer->popSection();
14511462
}
14521463

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

20322043
bool AsmPrinter::doFinalization(Module &M) {
2044+
if (YkAllocLLVMBBAddrMapSection && YkEmittedFirstBBAddrMap) {
2045+
// Add the `ykllvm.bbaddrmaps.stop` symbol.
2046+
OutStreamer->pushSection();
2047+
OutStreamer->switchSection(YkLastBBAddrMapSection);
2048+
emitYkBBAddrMapSymbol(OutContext, *OutStreamer, false);
2049+
OutStreamer->popSection();
2050+
}
2051+
20332052
// The `embed-bitcode` flag serialises the IR after only architecture
20342053
// agnostic optimisations have been run, but then proceeds to apply other
20352054
// optimisations and transformations afterwards. Sometimes this final version

0 commit comments

Comments
 (0)