[llvm] 1f33911 - IRBuilder: avoid crash when seeking to start of a BasicBlock with only DebugInfo (#66266)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 09:52:11 PDT 2023


Author: Augie Fackler
Date: 2023-09-15T12:52:07-04:00
New Revision: 1f33911f50294c07f672a49e311776693823d0bc

URL: https://github.com/llvm/llvm-project/commit/1f33911f50294c07f672a49e311776693823d0bc
DIFF: https://github.com/llvm/llvm-project/commit/1f33911f50294c07f672a49e311776693823d0bc.diff

LOG: 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 at 1ce1732f82aec29ec27d6de58153d516bca1d633).

This was more or less pair-programmed with @krasimirgg - I can't claim
full credit.

Added: 
    

Modified: 
    llvm/lib/IR/Instruction.cpp
    llvm/unittests/IR/DebugInfoTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 6b0348f8f691fd4..b497951a598cc50 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 a22c7be73f49fe1..84ed73333416c11 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:


        


More information about the llvm-commits mailing list