[llvm] ea17d29 - [llvm] Do not replace dead constant references in metadata with undef

Stephen Tozer via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 24 09:39:16 PST 2022


Author: Stephen Tozer
Date: 2022-01-24T17:36:33Z
New Revision: ea17d29a6c834a34a698c87193a86eeab04922d2

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

LOG: [llvm] Do not replace dead constant references in metadata with undef

This patch removes an incorrect behaviour in Constants.cpp, which would
replace dead constant references in metadata with an undef value. This
blanket replacement resulted in undef values being inserted into
metadata that would not accept them. The replacement was intended for
debug info metadata, but this is now instead handled in the RAUW
handler.

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

Added: 
    llvm/test/LTO/Resolution/X86/Inputs/no-undef-type-md.ll
    llvm/test/LTO/Resolution/X86/no-undef-type-md.ll

Modified: 
    llvm/lib/IR/Constants.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index e031f889caf69..c13990af360ec 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -739,15 +739,8 @@ static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) {
       ++I;
   }
 
-  if (RemoveDeadUsers) {
-    // If C is only used by metadata, it should not be preserved but should
-    // have its uses replaced.
-    if (C->isUsedByMetadata()) {
-      const_cast<Constant *>(C)->replaceAllUsesWith(
-          UndefValue::get(C->getType()));
-    }
+  if (RemoveDeadUsers)
     const_cast<Constant *>(C)->destroyConstant();
-  }
 
   return true;
 }

diff  --git a/llvm/test/LTO/Resolution/X86/Inputs/no-undef-type-md.ll b/llvm/test/LTO/Resolution/X86/Inputs/no-undef-type-md.ll
new file mode 100644
index 0000000000000..94166f39e2ae3
--- /dev/null
+++ b/llvm/test/LTO/Resolution/X86/Inputs/no-undef-type-md.ll
@@ -0,0 +1,13 @@
+; ModuleID = 'test.cpp.o'
+source_filename = "test.cpp"
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+declare void @a()
+
+!llvm.module.flags = !{!9, !39}
+
+!9 = !{i32 1, !"EnableSplitLTOUnit", i32 1}
+!39 = !{i32 5, !"CG Profile", !40}
+!40 = !{!41}
+!41 = distinct !{null, i32 ()* bitcast (void ()* @a to i32 ()*), i64 2594092}

diff  --git a/llvm/test/LTO/Resolution/X86/no-undef-type-md.ll b/llvm/test/LTO/Resolution/X86/no-undef-type-md.ll
new file mode 100644
index 0000000000000..afee5e656df17
--- /dev/null
+++ b/llvm/test/LTO/Resolution/X86/no-undef-type-md.ll
@@ -0,0 +1,37 @@
+; RUN: opt <%s -o %t0.o -thinlto-bc -thinlto-split-lto-unit
+; RUN: llvm-as -o %t1.o %S/Inputs/no-undef-type-md.ll
+; RUN: llvm-lto2 run -o a.out \
+; RUN: %t0.o \
+; RUN: -r=%t0.o,a, \
+; RUN: -r=%t0.o,b,pl \
+; RUN: %t1.o \
+; RUN: -r=%t1.o,a,pl \
+; RUN: | FileCheck --allow-empty --check-prefix=ERROR %s
+; RUN llvm-nm a.out.0 a.out.1 -S | FileCheck %s
+
+; ERROR-NOT: expected a Function or null
+; ERROR-NOT: i32 (%0*, i32*)* undef
+
+; CHECK: a.out.0:
+; CHECK: a.out.1:
+
+; ModuleID = 'test.cpp.o'
+source_filename = "test.cpp"
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @a() {
+entry:
+  ret i32 0
+}
+
+define i32 @b() {
+entry:
+  ret i32 0
+}
+
+!llvm.module.flags = !{!39}
+
+!39 = !{i32 5, !"CG Profile", !40}
+!40 = !{!41}
+!41 = !{i32 ()* @b, i32 ()* @a, i64 2594092}


        


More information about the llvm-commits mailing list