[ValueMapper] allow mapping ConstantTargetNone to its layout type
authorBing1 Yu <bing1.yu@intel.com>
Tue, 25 Apr 2023 07:47:33 +0000 (15:47 +0800)
committerBing1 Yu <bing1.yu@intel.com>
Tue, 25 Apr 2023 07:48:28 +0000 (15:48 +0800)
zeroinitializer is allowed for spirv TargetExtType.
This PR allows ValueMapper to map TargetExtType ConstantTargetNone to '0' constant of its layout type.

Reviewed By: jcranmer-intel

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

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

index 6fd6087..cad523c 100644 (file)
@@ -529,6 +529,8 @@ Value *Mapper::mapValue(const Value *V) {
     return getVM()[V] = UndefValue::get(NewTy);
   if (isa<ConstantAggregateZero>(C))
     return getVM()[V] = ConstantAggregateZero::get(NewTy);
+  if (isa<ConstantTargetNone>(C))
+    return getVM()[V] = Constant::getNullValue(NewTy);
   assert(isa<ConstantPointerNull>(C));
   return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
 }
index a4cff86..af20f5a 100644 (file)
@@ -421,4 +421,19 @@ TEST(ValueMapperTest, mapValuePoisonWithTypeRemap) {
   EXPECT_EQ(NewPoison, Mapper.mapValue(*OldPoison));
 }
 
+TEST(ValueMapperTest, mapValueConstantTargetNoneToLayoutTypeNullValue) {
+  LLVMContext C;
+  auto *OldTy = TargetExtType::get(C, "spirv.Image");
+  Type *NewTy = OldTy->getLayoutType();
+
+  TestTypeRemapper TM(NewTy);
+  ValueToValueMapTy VM;
+  ValueMapper Mapper(VM, RF_None, &TM);
+
+  // Check that ConstantTargetNone is mapped to '0' constant of its layout type.
+  auto *OldConstant = ConstantTargetNone::get(OldTy);
+  auto *NewConstant = Constant::getNullValue(NewTy);
+  EXPECT_EQ(NewConstant, Mapper.mapValue(*OldConstant));
+}
+
 } // end namespace