[llvm] 421b27c - [AsmParser] Apply deferred debug locations before intrinsic upgrade. (#200779)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 04:33:07 PDT 2026


Author: Florian Hahn
Date: 2026-06-01T11:33:02Z
New Revision: 421b27c4ab4b789bc5084f5f3ff9c008f69846df

URL: https://github.com/llvm/llvm-project/commit/421b27c4ab4b789bc5084f5f3ff9c008f69846df
DIFF: https://github.com/llvm/llvm-project/commit/421b27c4ab4b789bc5084f5f3ff9c008f69846df.diff

LOG: [AsmParser] Apply deferred debug locations before intrinsic upgrade. (#200779)

Intrinsic upgrades may delete instructions, leaving dangling pointers
that may be accessed when applying deferred debug locations after
91b77dc (#200649).

Fix by applying deferred debug locations before intrinsic upgrade.

PR: https://github.com/llvm/llvm-project/pull/200779

Added: 
    llvm/test/Assembler/dbg-intrinsic-forward-ref-upgrade.ll

Modified: 
    llvm/lib/AsmParser/LLParser.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 848132538b450..8202385ec2c90 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -331,6 +331,30 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
                  "use of undefined comdat '$" +
                      ForwardRefComdats.begin()->first + "'");
 
+  if (AllowIncompleteIR && !ForwardRefMDNodes.empty())
+    dropUnknownMetadataReferences();
+
+  if (!ForwardRefMDNodes.empty())
+    return error(ForwardRefMDNodes.begin()->second.second,
+                 "use of undefined metadata '!" +
+                     Twine(ForwardRefMDNodes.begin()->first) + "'");
+
+  // Set debug locations.
+  for (auto [Loc, DR, MD] : PendingDbgRecords) {
+    if (auto *DI = dyn_cast<DILocation>(MD))
+      DR->setDebugLoc(DebugLoc(DI));
+    else
+      return error(Loc, "invalid debug location");
+  }
+  PendingDbgRecords.clear();
+  for (auto [Loc, I, MD] : PendingDbgInsts) {
+    if (auto *DI = dyn_cast<DILocation>(MD))
+      I->setDebugLoc(DebugLoc(DI));
+    else
+      return error(Loc, "invalid !dbg metadata");
+  }
+  PendingDbgInsts.clear();
+
   for (const auto &[Name, Info] : make_early_inc_range(ForwardRefVals)) {
     if (StringRef(Name).starts_with("llvm.")) {
       Intrinsic::ID IID = Intrinsic::lookupIntrinsicID(Name);
@@ -422,28 +446,6 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
                  "use of undefined value '@" +
                      Twine(ForwardRefValIDs.begin()->first) + "'");
 
-  if (AllowIncompleteIR && !ForwardRefMDNodes.empty())
-    dropUnknownMetadataReferences();
-
-  if (!ForwardRefMDNodes.empty())
-    return error(ForwardRefMDNodes.begin()->second.second,
-                 "use of undefined metadata '!" +
-                     Twine(ForwardRefMDNodes.begin()->first) + "'");
-
-  // Set debug locations.
-  for (auto [Loc, DR, MD] : PendingDbgRecords) {
-    if (auto *DI = dyn_cast<DILocation>(MD))
-      DR->setDebugLoc(DebugLoc(DI));
-    else
-      return error(Loc, "invalid debug location");
-  }
-  for (auto [Loc, I, MD] : PendingDbgInsts) {
-    if (auto *DI = dyn_cast<DILocation>(MD))
-      I->setDebugLoc(DebugLoc(DI));
-    else
-      return error(Loc, "invalid !dbg metadata");
-  }
-
   // Resolve metadata cycles.
   for (auto &N : NumberedMetadata) {
     if (N.second && !N.second->isResolved())

diff  --git a/llvm/test/Assembler/dbg-intrinsic-forward-ref-upgrade.ll b/llvm/test/Assembler/dbg-intrinsic-forward-ref-upgrade.ll
new file mode 100644
index 0000000000000..05a50014aa4ac
--- /dev/null
+++ b/llvm/test/Assembler/dbg-intrinsic-forward-ref-upgrade.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt %s -S 2>&1 | FileCheck %s
+;
+define void @f(i32 %x) !dbg !6 {
+; CHECK-LABEL: define void @f(
+; CHECK-SAME: i32 [[X:%.*]]) !dbg [[DBG4:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:      #dbg_value(i32 [[X]], [[META8:![0-9]+]], !DIExpression(), [[META9:![0-9]+]])
+; CHECK-NEXT:    ret void, !dbg [[META9]]
+;
+entry:
+  tail call void @llvm.dbg.value(metadata i32 %x, i64 0, metadata !10, metadata !DIExpression()), !dbg !11
+  ret void, !dbg !11
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "")
+!1 = !DIFile(filename: "t.c", directory: "/")
+!3 = !{i32 2, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0)
+!7 = !DISubroutineType(types: !8)
+!8 = !{!9}
+!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!10 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !1, line: 1, type: !9)
+!11 = !DILocation(line: 1, column: 1, scope: !6)
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C99, file: [[META1:![0-9]+]], isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
+; CHECK: [[META1]] = !DIFile(filename: "{{.*}}t.c", directory: {{.*}})
+; CHECK: [[DBG4]] = distinct !DISubprogram(name: "f", scope: [[META1]], file: [[META1]], line: 1, type: [[META5:![0-9]+]], scopeLine: 1, spFlags: DISPFlagDefinition, unit: [[META0]])
+; CHECK: [[META5]] = !DISubroutineType(types: [[META6:![0-9]+]])
+; CHECK: [[META6]] = !{[[META7:![0-9]+]]}
+; CHECK: [[META7]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+; CHECK: [[META8]] = !DILocalVariable(name: "x", arg: 1, scope: [[DBG4]], file: [[META1]], line: 1, type: [[META7]])
+; CHECK: [[META9]] = !DILocation(line: 1, column: 1, scope: [[DBG4]])
+;.


        


More information about the llvm-commits mailing list