Skip to content

Commit

Permalink
[LLVM][IR] Fix assert in ConstantExpr::getPtrToInt so all vector type…
Browse files Browse the repository at this point in the history
…s are supported.

Fixes: #55410
  • Loading branch information
paulwalker-arm committed May 24, 2022
1 parent a9e354c commit 9426df9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions llvm/lib/IR/Constants.cpp
Expand Up @@ -2235,8 +2235,8 @@ Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
"PtrToInt destination must be integer or integer vector");
assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
if (isa<VectorType>(C->getType()))
assert(cast<FixedVectorType>(C->getType())->getNumElements() ==
cast<FixedVectorType>(DstTy)->getNumElements() &&
assert(cast<VectorType>(C->getType())->getElementCount() ==
cast<VectorType>(DstTy)->getElementCount() &&
"Invalid cast between a different number of vector elements");
return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
}
Expand Down
15 changes: 15 additions & 0 deletions llvm/unittests/IR/ConstantsTest.cpp
Expand Up @@ -134,6 +134,9 @@ TEST(ConstantsTest, PointerCast) {
VectorType *Int8PtrVecTy = FixedVectorType::get(Int8PtrTy, 4);
VectorType *Int32PtrVecTy = FixedVectorType::get(Int32PtrTy, 4);
VectorType *Int64VecTy = FixedVectorType::get(Int64Ty, 4);
VectorType *Int8PtrScalableVecTy = ScalableVectorType::get(Int8PtrTy, 4);
VectorType *Int32PtrScalableVecTy = ScalableVectorType::get(Int32PtrTy, 4);
VectorType *Int64ScalableVecTy = ScalableVectorType::get(Int64Ty, 4);

// ptrtoint i8* to i64
EXPECT_EQ(
Expand All @@ -150,11 +153,23 @@ TEST(ConstantsTest, PointerCast) {
ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy),
Int64VecTy));

// ptrtoint <vscale x 4 x i8*> to <vscale x 4 x i64>
EXPECT_EQ(
Constant::getNullValue(Int64ScalableVecTy),
ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrScalableVecTy),
Int64ScalableVecTy));

// bitcast <4 x i8*> to <4 x i32*>
EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy),
Int32PtrVecTy));

// bitcast <vscale x 4 x i8*> to <vscale x 4 x i32*>
EXPECT_EQ(
Constant::getNullValue(Int32PtrScalableVecTy),
ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrScalableVecTy),
Int32PtrScalableVecTy));

Type *Int32Ptr1Ty = Type::getInt32PtrTy(C, 1);
ConstantInt *K = ConstantInt::get(Type::getInt64Ty(C), 1234);

Expand Down

0 comments on commit 9426df9

Please sign in to comment.