[PATCH] D117300: [opt] Do not replace MD refs to destroyed constants with undef

Stephen Tozer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 14 03:47:12 PST 2022


StephenTozer created this revision.
StephenTozer added reviewers: dexonsmith, tejohnson, aprantl.
Herald added subscribers: ormris, steven_wu, hiraditya.
StephenTozer requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch fixes some incorrect behaviour in Constants.cpp, in which as part of destroying a constant we replace any references to that constant from metadata with an undef value of the same type. This is incorrect for any non-debug-info metadata, and debug info metadata already has RAUW handlers to insert an undef value if the constant is destroyed (and replaced with nullptr).

This resolves the issue introduced in rGe5d958c45629 <https://reviews.llvm.org/rGe5d958c45629ccd2f5b5f7432756be1d0fcf052c> and mentioned in D108642 <https://reviews.llvm.org/D108642>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117300

Files:
  llvm/lib/IR/Constants.cpp
  llvm/test/LTO/Resolution/X86/Inputs/no-undef-type-md.ll
  llvm/test/LTO/Resolution/X86/no-undef-type-md.ll


Index: llvm/test/LTO/Resolution/X86/no-undef-type-md.ll
===================================================================
--- /dev/null
+++ 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}
Index: llvm/test/LTO/Resolution/X86/Inputs/no-undef-type-md.ll
===================================================================
--- /dev/null
+++ 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}
Index: llvm/lib/IR/Constants.cpp
===================================================================
--- llvm/lib/IR/Constants.cpp
+++ llvm/lib/IR/Constants.cpp
@@ -739,15 +739,8 @@
       ++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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117300.399942.patch
Type: text/x-patch
Size: 2543 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220114/f301d2e9/attachment.bin>


More information about the llvm-commits mailing list