[PATCH] D145317: [ValueMapper] Preserve poison types during value mapping

Carl Ritson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 4 22:25:35 PST 2023


critson created this revision.
critson added reviewers: dexonsmith, ruiling.
Herald added a subscriber: hiraditya.
Herald added a project: All.
critson requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Poison needs to be treated directly during type remap otherwise
it will be considered an instance of undef.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145317

Files:
  llvm/lib/Transforms/Utils/ValueMapper.cpp
  llvm/unittests/Transforms/Utils/ValueMapperTest.cpp


Index: llvm/unittests/Transforms/Utils/ValueMapperTest.cpp
===================================================================
--- llvm/unittests/Transforms/Utils/ValueMapperTest.cpp
+++ llvm/unittests/Transforms/Utils/ValueMapperTest.cpp
@@ -397,4 +397,28 @@
   EXPECT_EQ(MDC, ValueMapper(VM).mapValue(*MDA));
 }
 
+// Type remapper which remaps all types to same destination.
+class TestTypeRemapper : public ValueMapTypeRemapper {
+public:
+  TestTypeRemapper(Type *Ty) : DstTy(Ty) { }
+  Type *remapType(Type *srcTy) { return DstTy; }
+private:
+  Type *DstTy;
+};
+
+TEST(ValueMapperTest, mapValuePoisonWithTypeRemap) {
+  LLVMContext C;
+  Type *OldTy = Type::getInt8Ty(C);
+  Type *NewTy = Type::getInt32Ty(C);
+
+  TestTypeRemapper TM(NewTy);
+  ValueToValueMapTy VM;
+  ValueMapper Mapper(VM, RF_None, &TM);
+
+  // Check that poison is still poison and has not been converted to undef.
+  auto *OldPoison = PoisonValue::get(OldTy);
+  auto *NewPoison = PoisonValue::get(NewTy);
+  EXPECT_EQ(NewPoison, Mapper.mapValue(*OldPoison));
+}
+
 } // end namespace
Index: llvm/lib/Transforms/Utils/ValueMapper.cpp
===================================================================
--- llvm/lib/Transforms/Utils/ValueMapper.cpp
+++ llvm/lib/Transforms/Utils/ValueMapper.cpp
@@ -523,6 +523,8 @@
   if (isa<ConstantVector>(C))
     return getVM()[V] = ConstantVector::get(Ops);
   // If this is a no-operand constant, it must be because the type was remapped.
+  if (isa<PoisonValue>(C))
+    return getVM()[V] = PoisonValue::get(NewTy);
   if (isa<UndefValue>(C))
     return getVM()[V] = UndefValue::get(NewTy);
   if (isa<ConstantAggregateZero>(C))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145317.502404.patch
Type: text/x-patch
Size: 1652 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230305/ddfa18f7/attachment.bin>


More information about the llvm-commits mailing list