[llvm] Globalopt pass produces invalid debug info (PR #100654)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 26 12:27:42 PDT 2024


https://github.com/ykhatav updated https://github.com/llvm/llvm-project/pull/100654

>From 7e2ae7c85e49e01daac227459c1fd3d38647da0d Mon Sep 17 00:00:00 2001
From: "Khatavkar, Yashasvi" <yashasvi.khatavkar at intel.com>
Date: Thu, 25 Jul 2024 13:28:03 -0700
Subject: [PATCH 1/3] Handle dbg.value in SalvageDebugInfo()

---
 llvm/lib/IR/Metadata.cpp                 |  5 ++++
 llvm/lib/Transforms/IPO/GlobalOpt.cpp    |  1 +
 llvm/test/DebugInfo/X86/undef-dbg-val.ll | 35 ++++++++++++++++++++++++
 3 files changed, 41 insertions(+)
 create mode 100644 llvm/test/DebugInfo/X86/undef-dbg-val.ll

diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index ae5f5de142328..768008828e783 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -346,6 +346,11 @@ void ReplaceableMetadataImpl::SalvageDebugInfo(const Constant &C) {
     MetadataTracking::OwnerTy Owner = Pair.second.first;
     if (!Owner)
       continue;
+    // Check for MetadataAsValue.
+    if (isa<MetadataAsValue *>(Owner)) {
+      cast<MetadataAsValue *>(Owner)->handleChangedMetadata(ValueAsMetadata::get(UndefValue::get(C.getType())));
+      continue;
+    }
     if (!isa<Metadata *>(Owner))
       continue;
     auto *OwnerMD = dyn_cast_if_present<MDNode>(cast<Metadata *>(Owner));
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index ab1e41ebf9a9d..5293a777496bc 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -1338,6 +1338,7 @@ deleteIfDead(GlobalValue &GV,
     if (DeleteFnCallback)
       DeleteFnCallback(*F);
   }
+  ReplaceableMetadataImpl::SalvageDebugInfo(GV);
   GV.eraseFromParent();
   ++NumDeleted;
   return true;
diff --git a/llvm/test/DebugInfo/X86/undef-dbg-val.ll b/llvm/test/DebugInfo/X86/undef-dbg-val.ll
new file mode 100644
index 0000000000000..be386cb14c85f
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/undef-dbg-val.ll
@@ -0,0 +1,35 @@
+; RUN:  opt -S -passes=globalopt <%s | FileCheck %s
+; CHECK: #dbg_value(ptr poison, 
+; CHECK-SAME:    [[VAR:![0-9]+]],
+; CHECK-SAME:    !DIExpression()
+; CHECK: [[VAR]] = !DILocalVariable(name: "_format"
+
+
+; ModuleID = '<stdin>'
+source_filename = "test.cpp"
+
+ at _ZZZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_ENKUlvE_clEvE7_format = internal constant [24 x i8] c"Result1: Hello, World!\0A\00", align 16, !dbg !9
+
+; Function Attrs: convergent mustprogress norecurse
+define void  @foo() align 2 !dbg !5 {
+entry:
+  call void @llvm.dbg.value(metadata ptr @_ZZZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_ENKUlvE_clEvE7_format, metadata !11, metadata !DIExpression()), !dbg !12
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.cpp", directory: "/path/to")
+!2 = !{}
+!3 = !{i32 7, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 27, type: !6, scopeLine: 27, spFlags: DISPFlagDefinition, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{null}
+!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression())
+!10 = distinct !DIGlobalVariable(name: "_format", scope: !5, file: !1, line: 49, type: !8, isLocal: true, isDefinition: true)
+!11 = !DILocalVariable(name: "_format", arg: 1, scope: !5, file: !1, line: 79, type: !8)
+!12 = !DILocation(line: 0, scope: !5)

>From 31556e78f4e0a7e908ed047ac50e838619214fb9 Mon Sep 17 00:00:00 2001
From: "Khatavkar, Yashasvi" <yashasvi.khatavkar at intel.com>
Date: Thu, 25 Jul 2024 14:08:08 -0700
Subject: [PATCH 2/3] Apply clang-format

---
 llvm/lib/IR/Metadata.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index 768008828e783..a91b073f17348 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -348,7 +348,8 @@ void ReplaceableMetadataImpl::SalvageDebugInfo(const Constant &C) {
       continue;
     // Check for MetadataAsValue.
     if (isa<MetadataAsValue *>(Owner)) {
-      cast<MetadataAsValue *>(Owner)->handleChangedMetadata(ValueAsMetadata::get(UndefValue::get(C.getType())));
+      cast<MetadataAsValue *>(Owner)->handleChangedMetadata(
+          ValueAsMetadata::get(UndefValue::get(C.getType())));
       continue;
     }
     if (!isa<Metadata *>(Owner))

>From 605fbd970ff4568ab4a4dc59a8304cc7cbc3a562 Mon Sep 17 00:00:00 2001
From: "Khatavkar, Yashasvi" <yashasvi.khatavkar at intel.com>
Date: Fri, 26 Jul 2024 12:12:24 -0700
Subject: [PATCH 3/3] Update test

---
 llvm/test/DebugInfo/X86/undef-dbg-val.ll | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/DebugInfo/X86/undef-dbg-val.ll b/llvm/test/DebugInfo/X86/undef-dbg-val.ll
index be386cb14c85f..1324936a2a9cf 100644
--- a/llvm/test/DebugInfo/X86/undef-dbg-val.ll
+++ b/llvm/test/DebugInfo/X86/undef-dbg-val.ll
@@ -1,5 +1,5 @@
-; RUN:  opt -S -passes=globalopt <%s | FileCheck %s
-; CHECK: #dbg_value(ptr poison, 
+; RUN:  opt -S -passes=globalopt --experimental-debuginfo-iterators=false <%s | FileCheck %s
+; CHECK: #dbg_value(ptr undef, 
 ; CHECK-SAME:    [[VAR:![0-9]+]],
 ; CHECK-SAME:    !DIExpression()
 ; CHECK: [[VAR]] = !DILocalVariable(name: "_format"



More information about the llvm-commits mailing list