Skip to content

Commit 3c763c9

Browse files
committed
Make it so we can read the .llvm_bb_addr_map section from memory at runtime.
We make the section loadable and emit symbols to help us find the data at runtime. LLVM streams the BB data for each function (independently) out to the binary on the fly. This makes it hard to provide the runtime with a simple `(address, length)` pair that it needs to make a Rust slice of the data. We work around this like so: for each function we emit a start and end symbol marking the bbaddrmap data for the function. The name of the symbol encodes the function for which the BB data corresponds. The runtime knows what functions are available (from the LTO LLVM IR) and can therefore enumerate the symbols to find all of the BB data.
1 parent 1cb9a4a commit 3c763c9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ static cl::opt<bool>
133133
cl::desc("Embed final IR as bitcode after all "
134134
"optimisations and transformations have run."));
135135

136+
extern bool YkAllocLLVMBBAddrMapSection;
137+
136138
const char DWARFGroupName[] = "dwarf";
137139
const char DWARFGroupDescription[] = "DWARF Emission";
138140
const char DbgTimerName[] = "emit";
@@ -1324,6 +1326,20 @@ static unsigned getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
13241326
(const_cast<MachineBasicBlock &>(MBB).canFallThrough() << 3);
13251327
}
13261328

1329+
void emitYkBBAddrMapSymbol(const MachineFunction &MF, MCStreamer &OutStreamer,
1330+
bool Start) {
1331+
std::string SymName("ykllvm.bbaddrmap.");
1332+
SymName.append(MF.getName().str());
1333+
if (Start)
1334+
SymName.append(".start");
1335+
else
1336+
SymName.append(".end");
1337+
1338+
MCSymbol *Sym = MF.getContext().getOrCreateSymbol(SymName);
1339+
OutStreamer.emitSymbolAttribute(Sym, llvm::MCSA_Global);
1340+
OutStreamer.emitLabel(Sym);
1341+
}
1342+
13271343
void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
13281344
MCSection *BBAddrMapSection =
13291345
getObjFileLowering().getBBAddrMapSection(*MF.getSection());
@@ -1333,6 +1349,11 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
13331349

13341350
OutStreamer->pushSection();
13351351
OutStreamer->switchSection(BBAddrMapSection);
1352+
1353+
// Add the `ykllvm.bbaddrmap.<func>.start` symbol.
1354+
if (YkAllocLLVMBBAddrMapSection)
1355+
emitYkBBAddrMapSymbol(MF, *OutStreamer, true);
1356+
13361357
OutStreamer->AddComment("version");
13371358
OutStreamer->emitInt8(OutStreamer->getContext().getBBAddrMapVersion());
13381359
OutStreamer->AddComment("feature");
@@ -1421,6 +1442,11 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14211442
OutStreamer->emitULEB128IntValue(I);
14221443
}
14231444
}
1445+
1446+
// Add the `ykllvm.bbaddrmap.<func>.end` symbol.
1447+
if (YkAllocLLVMBBAddrMapSection)
1448+
emitYkBBAddrMapSymbol(MF, *OutStreamer, false);
1449+
14241450
OutStreamer->popSection();
14251451
}
14261452

llvm/lib/MC/MCObjectFileInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,8 @@ MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
11201120
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
11211121
}
11221122

1123+
extern bool YkAllocLLVMBBAddrMapSection;
1124+
11231125
MCSection *
11241126
MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
11251127
if (Ctx->getObjectFileType() != MCContext::IsELF)
@@ -1133,6 +1135,9 @@ MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
11331135
Flags |= ELF::SHF_GROUP;
11341136
}
11351137

1138+
if (YkAllocLLVMBBAddrMapSection)
1139+
Flags |= ELF::SHF_ALLOC;
1140+
11361141
// Use the text section's begin symbol and unique ID to create a separate
11371142
// .llvm_bb_addr_map section associated with every unique text section.
11381143
return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,

llvm/lib/Support/Yk.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ bool YkAllocLLVMBCSection;
66
static cl::opt<bool, true> YkAllocLLVMBCSectionParser(
77
"yk-alloc-llvmbc-section", cl::desc("Make the `.llvmbc` section loadable"),
88
cl::NotHidden, cl::location(YkAllocLLVMBCSection));
9+
10+
bool YkAllocLLVMBBAddrMapSection;
11+
static cl::opt<bool, true> YkAllocLLVMBBAddrMapSectionParser(
12+
"yk-alloc-llvmbbaddrmap-section",
13+
cl::desc("Make the `.llvmbbaddrmap` section loadable"), cl::NotHidden,
14+
cl::location(YkAllocLLVMBBAddrMapSection));

0 commit comments

Comments
 (0)