[llvm] r271129 - ValueMapper: fix assertion when null-mapping a constant for linking metadata

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Sat May 28 10:26:07 PDT 2016


Author: mehdi_amini
Date: Sat May 28 12:26:03 2016
New Revision: 271129

URL: http://llvm.org/viewvc/llvm-project?rev=271129&view=rev
Log:
ValueMapper: fix assertion when null-mapping a constant for linking metadata

Summary:
When RF_NullMapMissingGlobalValues is set, mapValue can return null
for GlobalValue. When mapping the operands of a constant that is
referenced from metadata, we need to handle this case and actually
return null instead of mapping this constant.

Reviewers: dexonsmith, rafael

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D20713

Added:
    llvm/trunk/test/Linker/null_mapping_constant.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp

Modified: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=271129&r1=271128&r2=271129&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp Sat May 28 12:26:03 2016
@@ -429,13 +429,23 @@ Value *Mapper::mapValue(const Value *V)
   if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
     return mapBlockAddress(*BA);
 
+  auto mapValueOrNull = [this](Value *V) {
+    auto Mapped = mapValue(V);
+    assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
+           "Unexpected null mapping for constant operand without "
+           "NullMapMissingGlobalValues flag");
+    return Mapped;
+  };
+
   // Otherwise, we have some other constant to remap.  Start by checking to see
   // if all operands have an identity remapping.
   unsigned OpNo = 0, NumOperands = C->getNumOperands();
   Value *Mapped = nullptr;
   for (; OpNo != NumOperands; ++OpNo) {
     Value *Op = C->getOperand(OpNo);
-    Mapped = mapValue(Op);
+    Mapped = mapValueOrNull(Op);
+    if (!Mapped)
+      return nullptr;
     if (Mapped != Op)
       break;
   }
@@ -462,8 +472,12 @@ Value *Mapper::mapValue(const Value *V)
     Ops.push_back(cast<Constant>(Mapped));
 
     // Map the rest of the operands that aren't processed yet.
-    for (++OpNo; OpNo != NumOperands; ++OpNo)
-      Ops.push_back(cast<Constant>(mapValue(C->getOperand(OpNo))));
+    for (++OpNo; OpNo != NumOperands; ++OpNo) {
+      Mapped = mapValueOrNull(C->getOperand(OpNo));
+      if (!Mapped)
+        return nullptr;
+      Ops.push_back(cast<Constant>(Mapped));
+    }
   }
   Type *NewSrcTy = nullptr;
   if (TypeMapper)

Added: llvm/trunk/test/Linker/null_mapping_constant.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/null_mapping_constant.ll?rev=271129&view=auto
==============================================================================
--- llvm/trunk/test/Linker/null_mapping_constant.ll (added)
+++ llvm/trunk/test/Linker/null_mapping_constant.ll Sat May 28 12:26:03 2016
@@ -0,0 +1,11 @@
+; RUN: llvm-link %s -S -o - | FileCheck %s
+; Check that the constant is not linked and the metadata is correctly referencing a nullptr
+; CHECK: !0 = !{!"foo", null, i64 16}
+
+
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.12.0"
+ at foo = external unnamed_addr constant { [4 x i8*], [32 x i8] }, align 32
+!llvm.bitsets = !{!0}
+!0 = !{!"foo", [4 x i8*]* getelementptr inbounds ({ [4 x i8*], [32 x i8] }, { [4 x i8*], [32 x i8] }* @foo, i32 0, i32 0), i64 16}




More information about the llvm-commits mailing list