[llvm] r341289 - [DebugInfo] Fix bug in LiveDebugVariables.

Hsiangkai Wang via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 2 08:57:22 PDT 2018


Author: hsiangkai
Date: Sun Sep  2 08:57:22 2018
New Revision: 341289

URL: http://llvm.org/viewvc/llvm-project?rev=341289&view=rev
Log:
[DebugInfo] Fix bug in LiveDebugVariables.

In lib/CodeGen/LiveDebugVariables.cpp, it uses std::prev(MBBI) to
get DebugValue's SlotIndex. However, the previous instruction may be
also a debug instruction. It could not use a debug instruction to query
SlotIndex in mi2iMap.

Scan all debug instructions and use the first debug instruction to query
SlotIndex for following debug instructions. Only handle DBG_VALUE in
handleDebugValue().

Differential Revision: https://reviews.llvm.org/D50621

Added:
    llvm/trunk/test/DebugInfo/X86/live-debug-vars-index.mir
Modified:
    llvm/trunk/include/llvm/CodeGen/SlotIndexes.h
    llvm/trunk/lib/CodeGen/LiveDebugVariables.cpp

Modified: llvm/trunk/include/llvm/CodeGen/SlotIndexes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/SlotIndexes.h?rev=341289&r1=341288&r2=341289&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/SlotIndexes.h (original)
+++ llvm/trunk/include/llvm/CodeGen/SlotIndexes.h Sun Sep  2 08:57:22 2018
@@ -414,6 +414,8 @@ class raw_ostream;
     SlotIndex getInstructionIndex(const MachineInstr &MI) const {
       // Instructions inside a bundle have the same number as the bundle itself.
       const MachineInstr &BundleStart = *getBundleStart(MI.getIterator());
+      assert(!BundleStart.isDebugInstr() &&
+             "Could not use a debug instruction to query mi2iMap.");
       Mi2IndexMap::const_iterator itr = mi2iMap.find(&BundleStart);
       assert(itr != mi2iMap.end() && "Instruction not found in maps.");
       return itr->second;

Modified: llvm/trunk/lib/CodeGen/LiveDebugVariables.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/LiveDebugVariables.cpp?rev=341289&r1=341288&r2=341289&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/LiveDebugVariables.cpp (original)
+++ llvm/trunk/lib/CodeGen/LiveDebugVariables.cpp Sun Sep  2 08:57:22 2018
@@ -578,23 +578,28 @@ bool LDVImpl::collectDebugValues(Machine
     MachineBasicBlock *MBB = &*MFI;
     for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
          MBBI != MBBE;) {
-      if (!MBBI->isDebugValue()) {
+      // Use the first debug instruction in the sequence to get a SlotIndex
+      // for following consecutive debug instructions.
+      if (!MBBI->isDebugInstr()) {
         ++MBBI;
         continue;
       }
-      // DBG_VALUE has no slot index, use the previous instruction instead.
+      // Debug instructions has no slot index. Use the previous
+      // non-debug instruction's SlotIndex as its SlotIndex.
       SlotIndex Idx =
           MBBI == MBB->begin()
               ? LIS->getMBBStartIdx(MBB)
               : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
-      // Handle consecutive DBG_VALUE instructions with the same slot index.
+      // Handle consecutive debug instructions with the same slot index.
       do {
-        if (handleDebugValue(*MBBI, Idx)) {
+        // Only handle DBG_VALUE in handleDebugValue(). Skip all other
+        // kinds of debug instructions.
+        if (MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) {
           MBBI = MBB->erase(MBBI);
           Changed = true;
         } else
           ++MBBI;
-      } while (MBBI != MBBE && MBBI->isDebugValue());
+      } while (MBBI != MBBE && MBBI->isDebugInstr());
     }
   }
   return Changed;

Added: llvm/trunk/test/DebugInfo/X86/live-debug-vars-index.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/DebugInfo/X86/live-debug-vars-index.mir?rev=341289&view=auto
==============================================================================
--- llvm/trunk/test/DebugInfo/X86/live-debug-vars-index.mir (added)
+++ llvm/trunk/test/DebugInfo/X86/live-debug-vars-index.mir Sun Sep  2 08:57:22 2018
@@ -0,0 +1,51 @@
+# After adding new debug instruction for labels, it is possible to have
+# debug instructions before DBG_VALUE. When querying DBG_VALUE's slot
+# index using previous instruction and the previous instruction is debug
+# instruction, it will trigger an assertion as using debug instruction
+# to get slot index. This test is to emulate the case when DBG_VALUE's
+# previous instruction is DBG_LABEL in LiveDebugVariables pass.
+#
+# RUN: llc -run-pass=livedebugvars -run-pass=virtregrewriter -o - %s | FileCheck %s
+
+--- |
+  define i32 @foo(i32 %a, i32 %b) !dbg !4 {
+  entry:
+    ret i32 0, !dbg !12
+  }
+
+  !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: "debug-var-slot.c", 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 = !DILabel(scope: !4, name: "top", file: !1, line: 4)
+  !9 = !DILocation(line: 4, column: 1, scope: !4)
+  !10 = !DILocalVariable(name: "local_var", scope: !4, file: !1, line: 7, type: !7)
+  !11 = !DILocation(line: 7, column: 1, scope: !4)
+  !12 = !DILocation(line: 8, column: 3, scope: !4)
+  !13 = !DILocalVariable(name: "local_var2", scope: !4, file: !1, line: 7, type: !7)
+
+...
+---
+name:            foo
+tracksRegLiveness: true
+body:             |
+  bb.0:
+
+    DBG_VALUE debug-use $esi, debug-use $noreg, !13, !DIExpression(), debug-location !11
+    DBG_LABEL !8, debug-location !9
+    DBG_VALUE debug-use $edi, debug-use $noreg, !10, !DIExpression(), debug-location !11
+    RET 0, undef $eax, debug-location !12
+...
+
+# CHECK-LABEL: name:            foo
+# CHECK: bb.0:
+# CHECK-DAG: DBG_LABEL
+# CHECK-DAG: DBG_VALUE debug-use $esi
+# CHECK-DAG: DBG_VALUE debug-use $edi




More information about the llvm-commits mailing list