[llvm] e83543f - Don't replace Undef with null value for Constants Differential Revision:https://reviews.llvm.org/D124098

YASHASVI KHATAVKAR via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 25 17:50:22 PDT 2022


Author: YASHASVI KHATAVKAR
Date: 2022-04-25T20:50:00-04:00
New Revision: e83543f8c2ef79f82f2e291072a956e23ab08efa

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

LOG: Don't replace Undef with null value for Constants Differential Revision:https://reviews.llvm.org/D124098

Added: 
    llvm/test/DebugInfo/X86/undef-type-md.ll

Modified: 
    llvm/include/llvm/IR/Metadata.h
    llvm/lib/IR/Constants.cpp
    llvm/lib/IR/Metadata.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h
index eeeeb905506a7..31583ac1eb535 100644
--- a/llvm/include/llvm/IR/Metadata.h
+++ b/llvm/include/llvm/IR/Metadata.h
@@ -302,7 +302,8 @@ class ReplaceableMetadataImpl {
   ///
   /// Replace all uses of this with \c MD, which is allowed to be null.
   void replaceAllUsesWith(Metadata *MD);
-
+   /// Replace all uses of the constant with Undef in debug info metadata
+  static void SalvageDebugInfo(const Constant &C); 
   /// Returns the list of all DIArgList users of this.
   SmallVector<Metadata *> getAllArgListUsers();
 

diff  --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 1a7c9d379e04c..5dcf1bab1dbb5 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -741,9 +741,13 @@ static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) {
       ++I;
   }
 
-  if (RemoveDeadUsers)
+  if (RemoveDeadUsers) {
+    // If C is only used by metadata, it should not be preserved but should
+    // have its uses replaced.
+    ReplaceableMetadataImpl::SalvageDebugInfo(*C);
     const_cast<Constant *>(C)->destroyConstant();
-
+  }
+  
   return true;
 }
 

diff  --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index 226718ecac28b..cb003885d712c 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -245,6 +245,36 @@ void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
          "Reference without owner must be direct");
 }
 
+void ReplaceableMetadataImpl::SalvageDebugInfo(const Constant &C) {
+  if (!C.isUsedByMetadata()) {
+    return;
+  }
+
+  LLVMContext &Context = C.getType()->getContext();
+  auto &Store = Context.pImpl->ValuesAsMetadata;
+  auto I = Store.find(&C);
+  ValueAsMetadata *MD = I->second;
+  using UseTy =
+      std::pair<void *, std::pair<MetadataTracking::OwnerTy, uint64_t>>;
+  // Copy out uses and update value of Constant used by debug info metadata with undef below
+  SmallVector<UseTy, 8> Uses(MD->UseMap.begin(), MD->UseMap.end());
+
+  for (const auto &Pair : Uses) {
+    MetadataTracking::OwnerTy Owner = Pair.second.first;
+    if (!Owner)
+      continue;
+    if (!Owner.is<Metadata *>())
+      continue;
+    auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
+    if (!OwnerMD)
+      continue;
+    if (isa<DINode>(OwnerMD)) {
+      OwnerMD->handleChangedOperand(
+          Pair.first, ValueAsMetadata::get(UndefValue::get(C.getType())));
+    }
+  }
+}
+
 void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) {
   if (UseMap.empty())
     return;

diff  --git a/llvm/test/DebugInfo/X86/undef-type-md.ll b/llvm/test/DebugInfo/X86/undef-type-md.ll
new file mode 100644
index 0000000000000..c99f4570f77b0
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/undef-type-md.ll
@@ -0,0 +1,36 @@
+; RUN:  opt -S -ipsccp %S/undef-type-md.ll | FileCheck %s
+; CHECK: llvm.nondebug.metadata = !{[[NONDEBUG_METADATA:![0-9]+]]}
+; CHECK: [[NONDEBUG_METADATA]] =  distinct !{null} 
+; CHECK: !DITemplateValueParameter({{.*}} value: %class.1 addrspace(1)* undef)
+
+; ModuleID = '<stdin>'
+source_filename = "test.cpp"
+
+%"struct.1" = type <{ float, i32, i8, [3 x i8] }>
+%"class.1" = type { %"struct.1" }
+
+ at extern_const = external addrspace(1) constant { { float, i32, i8 } }
+
+; Function Attrs: convergent mustprogress norecurse
+define linkonce_odr spir_func void @foo() align 2 !dbg !5 {
+entry:
+  %0 = alloca %"class.1", align 8
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+!llvm.nondebug.metadata= !{!11}
+
+!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<param>", scope: !1, file: !1, line: 27, type: !6, scopeLine: 27, spFlags: DISPFlagDefinition, unit: !0, templateParams: !8)
+!6 = !DISubroutineType(types: !7)
+!7 = !{null}
+!8 = !{!9}
+!9 = !DITemplateValueParameter(name: "S", type: !10, value: %"class.1" addrspace(1)* bitcast ({ { float, i32, i8 } } addrspace(1)* @extern_const to %"class.1" addrspace(1)*))
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!11 = !{%"class.1" addrspace(1)* bitcast ({ { float, i32, i8 } } addrspace(1)* @extern_const to %"class.1" addrspace(1)*)}


        


More information about the llvm-commits mailing list