aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAugie Fackler <augie@google.com>2023-09-15 12:52:07 -0400
committerGitHub <noreply@github.com>2023-09-15 12:52:07 -0400
commit1f33911f50294c07f672a49e311776693823d0bc (patch)
tree0ffe4532d20288f9e153a8a36fc1eb5dc0d8f09e
parent4a030f5b245b63fe09c29686c50d40796c987d96 (diff)
downloadllvm-1f33911f50294c07f672a49e311776693823d0bc.zip
llvm-1f33911f50294c07f672a49e311776693823d0bc.tar.gz
llvm-1f33911f50294c07f672a49e311776693823d0bc.tar.bz2
IRBuilder: avoid crash when seeking to start of a BasicBlock with only DebugInfo (#66266)
This fixes a crash in `rustc` that was triggered by https://reviews.llvm.org/D159485 (aka llvm/llvm-project@1ce1732f82aec29ec27d6de58153d516bca1d633). This was more or less pair-programmed with @krasimirgg - I can't claim full credit.
-rw-r--r--llvm/lib/IR/Instruction.cpp3
-rw-r--r--llvm/unittests/IR/DebugInfoTest.cpp13
2 files changed, 15 insertions, 1 deletions
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 6b0348f..b497951 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -887,7 +887,8 @@ Instruction::getPrevNonDebugInstruction(bool SkipPseudoOp) const {
const DebugLoc &Instruction::getStableDebugLoc() const {
if (isa<DbgInfoIntrinsic>(this))
- return getNextNonDebugInstruction()->getDebugLoc();
+ if (const Instruction *Next = getNextNonDebugInstruction())
+ return Next->getDebugLoc();
return getDebugLoc();
}
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index a22c7be..84ed733 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -566,6 +566,19 @@ TEST(AssignmentTrackingTest, Utils) {
EXPECT_FALSE(at::getAssignmentMarkers(&Fun2Alloca).empty());
}
+TEST(IRBuilder, GetSetInsertionPointWithEmptyBasicBlock) {
+ LLVMContext C;
+ std::unique_ptr<BasicBlock> BB(BasicBlock::Create(C, "start"));
+ Module *M = new Module("module", C);
+ IRBuilder<> Builder(BB.get());
+ Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare);
+ Value *DIV = MetadataAsValue::get(C, (Metadata *)nullptr);
+ SmallVector<Value *, 3> Args = {DIV, DIV, DIV};
+ Builder.CreateCall(DbgDeclare, Args);
+ auto IP = BB->getFirstInsertionPt();
+ Builder.SetInsertPoint(BB.get(), IP);
+}
+
TEST(AssignmentTrackingTest, InstrMethods) {
// Test the assignment tracking Instruction methods.
// This includes: