[llvm] add a unit test for the segfault in rust after https://reviews.llvm.org/D159485 (PR #66491)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 03:51:29 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir
            
<details>
<summary>Changes</summary>
```sh
ninja unittests/IR/IRTests ; ./unittests/IR/IRTests --gtest_filter='*GetSetInsertionPointWithEmptyBasicBlock*'
```

```
[ RUN      ] IRBuilder.GetSetInsertionPointWithEmptyBasicBlock
; ModuleID = 'module'
source_filename = "module"

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare void @llvm.dbg.declare(metadata %0, metadata %1, metadata %2) #0

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

start:                                            ; No predecessors!
  call addrspace(0) void @llvm.dbg.declare(metadata <0x56430425ec90>, metadata <0x56430425ec90>, metadata <0x56430425ec90>)

error: getStableDebugLoc:getNextNonDebugInstruction is NULL
```
--
Full diff: https://github.com/llvm/llvm-project/pull/66491.diff

2 Files Affected:

- (modified) llvm/lib/IR/Instruction.cpp (+9-2) 
- (modified) llvm/unittests/IR/DebugInfoTest.cpp (+18) 


<pre>
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 6b0348f8f691fd4..a4982f14222b960 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -886,8 +886,15 @@ Instruction::getPrevNonDebugInstruction(bool SkipPseudoOp) const {
 }
 
 const DebugLoc &amp;Instruction::getStableDebugLoc() const {
-  if (isa&lt;DbgInfoIntrinsic&gt;(this))
-    return getNextNonDebugInstruction()-&gt;getDebugLoc();
+  if (isa&lt;DbgInfoIntrinsic&gt;(this)) {
+    if (const Instruction* Next = getNextNonDebugInstruction()) {
+      return Next-&gt;getDebugLoc();
+    }
+
+    // FIXME: remove, just to demonstrate the test causes Next above to be null.
+    llvm::errs() &lt;&lt; &quot;error: getStableDebugLoc:getNextNonDebugInstruction is NULL\n&quot;;
+    exit(1);
+  }
   return getDebugLoc();
 }
 
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index a22c7be73f49fe1..3a91bd39d1bde07 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -566,6 +566,24 @@ TEST(AssignmentTrackingTest, Utils) {
   EXPECT_FALSE(at::getAssignmentMarkers(&amp;Fun2Alloca).empty());
 }
 
+TEST(IRBuilder, GetSetInsertionPointWithEmptyBasicBlock) {
+  LLVMContext C;
+  std::unique_ptr&lt;BasicBlock&gt; BB(BasicBlock::Create(C, &quot;start&quot;));
+  Module *M = new Module(&quot;module&quot;, C);
+  IRBuilder&lt;&gt; Builder(BB.get());
+  Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare);
+  Value *DIV = MetadataAsValue::get(C, (Metadata *)nullptr);
+  SmallVector&lt;Value *, 3&gt; Args = {DIV, DIV, DIV};
+  Builder.CreateCall(DbgDeclare, Args);
+
+  // FIXME: remove the dump(), just for debugging.
+  M-&gt;dump();
+  BB-&gt;dump();
+
+  auto IP = BB-&gt;getFirstInsertionPt();
+  Builder.SetInsertPoint(BB.get(), IP);
+}
+
 TEST(AssignmentTrackingTest, InstrMethods) {
   // Test the assignment tracking Instruction methods.
   // This includes:
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66491


More information about the llvm-commits mailing list