https://github.com/llvmbot created 
https://github.com/llvm/llvm-project/pull/178679

Backport 02771a3eaff153002ec544c3dc4427d56ccd4456

Requested by: @androm3da

>From 88e72452c4b15886b3cfcb87ad08d4ffcac16b09 Mon Sep 17 00:00:00 2001
From: Fateme Hosseini <[email protected]>
Date: Thu, 29 Jan 2026 09:03:10 -0600
Subject: [PATCH] Fix insert DBG_VALUE after terminator Failure for Hexagon
 (#173401)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch fixes an assertion failure on VLIW targets like Hexagon,
where a packet can contain both a terminator and a spill/copy. The
existing code did not look inside bundles, hence, it could leave a
transfer anchored on a terminator. When LiveDebugValues later
attempted to insert a DBG_VALUE after that packet, it hit:

  Assertion `!TR.TransferInst->isTerminator() && "Cannot insert
DBG_VALUE after terminator"' failed

The change switches to instr_iterator and walks each packet with
getBundleStart/getBundleEnd. Packets containing a terminator are skipped
for insertion; non‑terminator ops in other packets are still processed
normally. This avoids illegal insertion points while keeping spill/copy
tracking intact.

(cherry picked from commit 02771a3eaff153002ec544c3dc4427d56ccd4456)
---
 .../LiveDebugValues/VarLocBasedImpl.cpp       | 33 ++++++++++++++--
 .../livedebugvalues-bundle-terminator.mir     | 38 +++++++++++++++++++
 2 files changed, 68 insertions(+), 3 deletions(-)
 create mode 100644 
llvm/test/CodeGen/Hexagon/livedebugvalues-bundle-terminator.mir

diff --git a/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp 
b/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
index 1c4b2f9136857..293dfa53c3c84 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
@@ -125,6 +125,7 @@
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineInstrBundle.h"
 #include "llvm/CodeGen/MachineMemOperand.h"
 #include "llvm/CodeGen/MachineOperand.h"
 #include "llvm/CodeGen/PseudoSourceValue.h"
@@ -2347,9 +2348,35 @@ bool VarLocBasedLDV::ExtendRanges(MachineFunction &MF,
         OpenRanges.insertFromLocSet(getVarLocsInMBB(MBB, InLocs), VarLocIDs);
         LastNonDbgMI = nullptr;
         RegSetInstrs.clear();
-        for (auto &MI : *MBB)
-          process(MI, OpenRanges, VarLocIDs, Transfers, EntryValTransfers,
-                  RegSetInstrs);
+        // Iterate through instructions within each packet to handle VLIW
+        // bundles correctly; this keeps DBG_VALUE placement valid on
+        // packet-based targets.
+        for (auto I = MBB->instr_begin(), E = MBB->instr_end(); I != E;) {
+          auto BStart = llvm::getBundleStart(I);
+          auto BEnd = llvm::getBundleEnd(I);
+          bool PacketHasTerminator = false;
+          for (auto BI = BStart; BI != BEnd; ++BI) {
+            if (BI->isTerminator()) {
+              PacketHasTerminator = true;
+              break;
+            }
+          }
+          if (PacketHasTerminator) {
+            // FIXME: This drops debug info for spills in terminator bundles;
+            // DBG_VALUE instructions can't be inserted after the bundle.
+            // It may be possible to insert the DBG_VALUE elsewhere.
+            I = BEnd;
+            continue;
+          }
+          auto FirstOp = (BStart->isBundle()) ? std::next(BStart) : BStart;
+          for (auto BI = FirstOp; BI != BEnd; ++BI) {
+            if (BI->isTerminator())
+              continue;
+            process(*BI, OpenRanges, VarLocIDs, Transfers, EntryValTransfers,
+                    RegSetInstrs);
+          }
+          I = BEnd;
+        }
         OLChanged |= transferTerminator(MBB, OpenRanges, OutLocs, VarLocIDs);
 
         LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
diff --git a/llvm/test/CodeGen/Hexagon/livedebugvalues-bundle-terminator.mir 
b/llvm/test/CodeGen/Hexagon/livedebugvalues-bundle-terminator.mir
new file mode 100644
index 0000000000000..4bc3541d949dc
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/livedebugvalues-bundle-terminator.mir
@@ -0,0 +1,38 @@
+# RUN: llc -march=hexagon -run-pass=livedebugvalues -verify-machineinstrs %s 
-o - | FileCheck %s
+# CHECK: BUNDLE
+--- |
+
+  define void @foo() !dbg !5 {
+    ret void
+  }
+
+  !llvm.dbg.cu = !{!0}
+  !llvm.module.flags = !{!3, !4}
+  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: 
"clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+  !1 = !DIFile(filename: "t.c", directory: "/")
+  !3 = !{i32 2, !"Debug Info Version", i32 3}
+  !4 = !{i32 1, !"PIC Level", i32 2}
+  !5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: 
!6, isDefinition: true, unit: !0)
+  !6 = !DISubroutineType(types: !7)
+  !7 = !{null}
+  !8 = !DILocalVariable(name: "x", scope: !5, file: !1, line: 1, type: !9)
+  !9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+  !10 = !DILocation(line: 1, column: 1, scope: !5)
+
+...
+---
+name:            foo
+tracksRegLiveness: true
+stack:
+  - { id: 0, type: spill-slot, size: 4, alignment: 4 }
+body:             |
+  bb.0:
+    liveins: $r29, $r31
+    
+    DBG_VALUE $r29, $noreg, !8, !DIExpression(), debug-location !10
+    
+    BUNDLE implicit-def $pc, implicit killed $r29, implicit $r31, 
debug-location !10 :: (store (s32) into %stack.0) {
+      S2_storeri_io %stack.0, 0, killed $r29 :: (store (s32) into %stack.0)
+      PS_jmpret $r31, implicit-def $pc
+    }
+...

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to