Skip to content

Commit 1f33911

Browse files
authored
IRBuilder: avoid crash when seeking to start of a BasicBlock with only DebugInfo (llvm#66266)
This fixes a crash in `rustc` that was triggered by https://reviews.llvm.org/D159485 (aka llvm/llvm-project@1ce1732). This was more or less pair-programmed with @krasimirgg - I can't claim full credit.
1 parent 4a030f5 commit 1f33911

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

llvm/lib/IR/Instruction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,8 @@ Instruction::getPrevNonDebugInstruction(bool SkipPseudoOp) const {
887887

888888
const DebugLoc &Instruction::getStableDebugLoc() const {
889889
if (isa<DbgInfoIntrinsic>(this))
890-
return getNextNonDebugInstruction()->getDebugLoc();
890+
if (const Instruction *Next = getNextNonDebugInstruction())
891+
return Next->getDebugLoc();
891892
return getDebugLoc();
892893
}
893894

llvm/unittests/IR/DebugInfoTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,19 @@ TEST(AssignmentTrackingTest, Utils) {
566566
EXPECT_FALSE(at::getAssignmentMarkers(&Fun2Alloca).empty());
567567
}
568568

569+
TEST(IRBuilder, GetSetInsertionPointWithEmptyBasicBlock) {
570+
LLVMContext C;
571+
std::unique_ptr<BasicBlock> BB(BasicBlock::Create(C, "start"));
572+
Module *M = new Module("module", C);
573+
IRBuilder<> Builder(BB.get());
574+
Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare);
575+
Value *DIV = MetadataAsValue::get(C, (Metadata *)nullptr);
576+
SmallVector<Value *, 3> Args = {DIV, DIV, DIV};
577+
Builder.CreateCall(DbgDeclare, Args);
578+
auto IP = BB->getFirstInsertionPt();
579+
Builder.SetInsertPoint(BB.get(), IP);
580+
}
581+
569582
TEST(AssignmentTrackingTest, InstrMethods) {
570583
// Test the assignment tracking Instruction methods.
571584
// This includes:

0 commit comments

Comments
 (0)