Skip to content

Commit b9092ed

Browse files
committed
Make finding the corresponding BasicBlock more robust.
In a previous PR we modified the .llvm_bb_addr_map section, adding the original basic block index. This commit improves that code in two ways: 1) Catch the case where MachineBasicBlock::getBasicBlock() returns a null pointer. To quote the documentation: > Note that this may be NULL if this instance does not correspond > directly to an LLVM basic block. We use the block index u64::MAX to encode this condition (note that although the field in the section is uleb128, LLVM only allows block indices that fit in a u64). An upcoming change to yk will check for this condition at runtime. 2) Currently if a block isn't found, then the resulting block index is incorrect: The last index used in the loop is erroneously used.This commit causes an error at compile time instead.
1 parent a0e458b commit b9092ed

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ const char PPTimerDescription[] = "Pseudo Probe Emission";
156156
const char PPGroupName[] = "pseudo probe";
157157
const char PPGroupDescription[] = "Pseudo Probe Emission";
158158

159+
// A basic block index value used in the bb_addr_map to indicate that there
160+
// is no correspoinding IR block for the given machine basic block.
161+
const uint64_t NO_BB = UINT64_MAX;
162+
159163
STATISTIC(EmittedInsts, "Number of machine instrs printed");
160164

161165
char AsmPrinter::ID = 0;
@@ -1163,15 +1167,26 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
11631167
emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), MBBSymbol);
11641168
OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
11651169
// 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;
1170+
size_t BBIdx = 0;
1171+
bool found = false;
1172+
const BasicBlock *FindBB = MBB.getBasicBlock();
1173+
if (FindBB == nullptr) {
1174+
found = true;
1175+
BBIdx = NO_BB;
1176+
} else {
1177+
for (auto It = F.begin(); It != F.end(); It++) {
1178+
const BasicBlock *BB = &*It;
1179+
if (BB == FindBB) {
1180+
found = true;
1181+
break;
1182+
}
1183+
BBIdx++;
1184+
assert(BBIdx != NO_BB); // Or we are out of encoding space.
11711185
}
1172-
i++;
11731186
}
1174-
OutStreamer->emitULEB128IntValue(i);
1187+
if (!found)
1188+
OutContext.reportError(SMLoc(), "Couldn't find the block's index");
1189+
OutStreamer->emitULEB128IntValue(BBIdx);
11751190
}
11761191
OutStreamer->PopSection();
11771192
}

0 commit comments

Comments
 (0)