Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

krasimirgg
Copy link
Contributor

@krasimirgg krasimirgg commented Sep 15, 2023

(this is just for demonstration, let's keep the discussion on #66266)

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

…rg/D159485

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
```
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2023

@llvm/pr-subscribers-llvm-ir

Changes ```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)
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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants