Skip to content

Conversation

slinder1
Copy link
Contributor

Most of the pass works in terms of MachineBasicBlock::iterator (MachineInstrBundleIterator), but here one is constructed from an arbitrary instruction which may be within a bundle, causing an assertion.

I broke out the regression test in this review as the initial commit, but I plan to squash before landing.

Most of the pass works in terms of MachineBasicBlock::iterator
(MachineInstrBundleIterator), but here one is constructed from an
arbitrary instruction which may be within a bundle, causing an
assertion.
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-debuginfo

Author: Scott Linder (slinder1)

Changes

Most of the pass works in terms of MachineBasicBlock::iterator (MachineInstrBundleIterator), but here one is constructed from an arbitrary instruction which may be within a bundle, causing an assertion.

I broke out the regression test in this review as the initial commit, but I plan to squash before landing.


Full diff: https://github.com/llvm/llvm-project/pull/159471.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/LiveDebugVariables.cpp (+2-2)
  • (added) llvm/test/DebugInfo/X86/live-debug-vars-bundle.mir (+48)
diff --git a/llvm/lib/CodeGen/LiveDebugVariables.cpp b/llvm/lib/CodeGen/LiveDebugVariables.cpp
index b049491b531fe..001ba5250f77e 100644
--- a/llvm/lib/CodeGen/LiveDebugVariables.cpp
+++ b/llvm/lib/CodeGen/LiveDebugVariables.cpp
@@ -1973,8 +1973,8 @@ void LiveDebugVariables::LDVImpl::emitDebugValues(VirtRegMap *VRM) {
 
     if (MachineInstr *Pos = Slots->getInstructionFromIndex(Idx)) {
       // Insert at the end of any debug instructions.
-      auto PostDebug = std::next(Pos->getIterator());
-      PostDebug = skipDebugInstructionsForward(PostDebug, MBB->instr_end());
+      auto PostDebug = std::next(MachineBasicBlock::iterator(Pos));
+      PostDebug = skipDebugInstructionsForward(PostDebug, MBB->end());
       EmitInstsHere(PostDebug);
     } else {
       // Insert position disappeared; walk forwards through slots until we
diff --git a/llvm/test/DebugInfo/X86/live-debug-vars-bundle.mir b/llvm/test/DebugInfo/X86/live-debug-vars-bundle.mir
new file mode 100644
index 0000000000000..8a7eff5519869
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/live-debug-vars-bundle.mir
@@ -0,0 +1,48 @@
+# RUN: llc -run-pass=livedebugvars -run-pass=virtregrewriter -o - %s | FileCheck %s
+
+# Regression test for a bug where LiveDebugVariables used the wrong instruction
+# iterator type when debugInstrRef=true, and triggered an assertion.
+
+--- |
+  target triple = "x86_64-unknown-linux-gnu"
+
+  define i32 @foo(i32 %a, i32 %b) !dbg !4 {
+  entry:
+    ret i32 0, !dbg !10
+  }
+
+  !llvm.dbg.cu = !{!0}
+  !llvm.module.flags = !{!3}
+
+  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+  !1 = !DIFile(filename: "-", directory: "./")
+  !2 = !{}
+  !3 = !{i32 2, !"Debug Info Version", i32 3}
+  !4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 2, isOptimized: true, unit: !0, retainedNodes: !2)
+  !5 = !DISubroutineType(types: !6)
+  !6 = !{!7, !7, !7}
+  !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+  !8 = !DILocalVariable(name: "local_var", scope: !4, file: !1, line: 7, type: !7)
+  !9 = !DILocation(line: 7, column: 1, scope: !4)
+  !10 = !DILocation(line: 8, column: 3, scope: !4)
+
+...
+---
+name:            foo
+tracksRegLiveness: true
+debugInstrRef: true
+body:             |
+  bb.0:
+    $esi = MOV32ri 2, debug-instr-number 1
+    BUNDLE {
+      NOOP
+    }
+    DBG_INSTR_REF !8, !DIExpression(), dbg-instr-ref(1, 0), debug-location !9
+    RET 0, undef $eax, debug-location !10
+...
+
+# CHECK-LABEL: name:            foo
+# CHECK: bb.0:
+# CHECK: BUNDLE {
+# CHECK: }
+# CHECK: DBG_INSTR_REF

Copy link
Contributor

@jayfoad jayfoad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Member

@jmorse jmorse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@slinder1 slinder1 merged commit ad68e5d into llvm:main Sep 18, 2025
12 checks passed
@slinder1 slinder1 deleted the live-debug-vars-bundle branch September 18, 2025 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants