Skip to content

Commit a605ce0

Browse files
ptersilievext01
authored andcommitted
Extend basic block address map with IR indexes.
Since LLVM reorders machine basic blocks during compilation, the order in the basic block address map doesn't match the order of LLVM IR basic blocks. This commit extends the basic block address map (SHT_LLVM_BB_ADDR_MAP) with an additional field which exposes the LLVM IR basic block index for each machine basic block. This allows us to map virtual addresses back to BasicBlocks in the IR.
1 parent 09fa4ff commit a605ce0

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
11511151
OutStreamer->emitSymbolValue(FunctionSymbol, getPointerSize());
11521152
// Emit the total number of basic blocks in this function.
11531153
OutStreamer->emitULEB128IntValue(MF.size());
1154+
const Function &F = MF.getFunction();
11541155
// Emit BB Information for each basic block in the funciton.
11551156
for (const MachineBasicBlock &MBB : MF) {
11561157
const MCSymbol *MBBSymbol =
@@ -1161,6 +1162,16 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
11611162
// always be computed from their offsets.
11621163
emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), MBBSymbol);
11631164
OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
1165+
// Emit the index of the corresponding LLVMIR basic block.
1166+
size_t i = 0;
1167+
for (auto It = F.begin(); It != F.end(); It++) {
1168+
const BasicBlock *BB = &*It;
1169+
if (BB == MBB.getBasicBlock()) {
1170+
break;
1171+
}
1172+
i++;
1173+
}
1174+
OutStreamer->emitULEB128IntValue(i);
11641175
}
11651176
OutStreamer->PopSection();
11661177
}

0 commit comments

Comments
 (0)