[llvm] 8e1038b - [ValueMapper] Preserve poison types during value mapping

Carl Ritson via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 5 21:21:54 PST 2023


Author: Carl Ritson
Date: 2023-03-06T14:21:09+09:00
New Revision: 8e1038bd159638aa354144c76a07cbc1399b04dc

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

LOG: [ValueMapper] Preserve poison types during value mapping

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

Reviewed By: dexonsmith

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/ValueMapper.cpp b/llvm/lib/Transforms/Utils/ValueMapper.cpp
index a5edbb2acc6d8..2a7aba0bcee79 100644
--- a/llvm/lib/Transforms/Utils/ValueMapper.cpp
+++ b/llvm/lib/Transforms/Utils/ValueMapper.cpp
@@ -523,6 +523,8 @@ Value *Mapper::mapValue(const Value *V) {
   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))

diff  --git a/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp b/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp
index b5b785321b992..a4cff86f533cf 100644
--- a/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp
+++ b/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp
@@ -397,4 +397,28 @@ TEST(ValueMapperTest, mapValueLocalAsMetadataToConstant) {
   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


        


More information about the llvm-commits mailing list