1 //===--- llvm/unittest/IR/VectorTypesTest.cpp - vector types unit tests ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/DerivedTypes.h"
10 #include "llvm/IR/LLVMContext.h"
11 #include "llvm/Support/ScalableSize.h"
12 #include "gtest/gtest.h"
16 TEST(VectorTypesTest
, FixedLength
) {
19 Type
*Int16Ty
= Type::getInt16Ty(Ctx
);
20 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
21 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
22 Type
*Float64Ty
= Type::getDoubleTy(Ctx
);
24 VectorType
*V8Int32Ty
= VectorType::get(Int32Ty
, 8);
25 ASSERT_FALSE(V8Int32Ty
->isScalable());
26 EXPECT_EQ(V8Int32Ty
->getNumElements(), 8U);
27 EXPECT_EQ(V8Int32Ty
->getElementType()->getScalarSizeInBits(), 32U);
29 VectorType
*V8Int16Ty
= VectorType::get(Int16Ty
, {8, false});
30 ASSERT_FALSE(V8Int16Ty
->isScalable());
31 EXPECT_EQ(V8Int16Ty
->getNumElements(), 8U);
32 EXPECT_EQ(V8Int16Ty
->getElementType()->getScalarSizeInBits(), 16U);
34 ElementCount
EltCnt(4, false);
35 VectorType
*V4Int64Ty
= VectorType::get(Int64Ty
, EltCnt
);
36 ASSERT_FALSE(V4Int64Ty
->isScalable());
37 EXPECT_EQ(V4Int64Ty
->getNumElements(), 4U);
38 EXPECT_EQ(V4Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
40 VectorType
*V2Int64Ty
= VectorType::get(Int64Ty
, EltCnt
/2);
41 ASSERT_FALSE(V2Int64Ty
->isScalable());
42 EXPECT_EQ(V2Int64Ty
->getNumElements(), 2U);
43 EXPECT_EQ(V2Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
45 VectorType
*V8Int64Ty
= VectorType::get(Int64Ty
, EltCnt
*2);
46 ASSERT_FALSE(V8Int64Ty
->isScalable());
47 EXPECT_EQ(V8Int64Ty
->getNumElements(), 8U);
48 EXPECT_EQ(V8Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
50 VectorType
*V4Float64Ty
= VectorType::get(Float64Ty
, EltCnt
);
51 ASSERT_FALSE(V4Float64Ty
->isScalable());
52 EXPECT_EQ(V4Float64Ty
->getNumElements(), 4U);
53 EXPECT_EQ(V4Float64Ty
->getElementType()->getScalarSizeInBits(), 64U);
55 VectorType
*ExtTy
= VectorType::getExtendedElementVectorType(V8Int16Ty
);
56 EXPECT_EQ(ExtTy
, V8Int32Ty
);
57 ASSERT_FALSE(ExtTy
->isScalable());
58 EXPECT_EQ(ExtTy
->getNumElements(), 8U);
59 EXPECT_EQ(ExtTy
->getElementType()->getScalarSizeInBits(), 32U);
61 VectorType
*TruncTy
= VectorType::getTruncatedElementVectorType(V8Int32Ty
);
62 EXPECT_EQ(TruncTy
, V8Int16Ty
);
63 ASSERT_FALSE(TruncTy
->isScalable());
64 EXPECT_EQ(TruncTy
->getNumElements(), 8U);
65 EXPECT_EQ(TruncTy
->getElementType()->getScalarSizeInBits(), 16U);
67 VectorType
*HalvedTy
= VectorType::getHalfElementsVectorType(V4Int64Ty
);
68 EXPECT_EQ(HalvedTy
, V2Int64Ty
);
69 ASSERT_FALSE(HalvedTy
->isScalable());
70 EXPECT_EQ(HalvedTy
->getNumElements(), 2U);
71 EXPECT_EQ(HalvedTy
->getElementType()->getScalarSizeInBits(), 64U);
73 VectorType
*DoubledTy
= VectorType::getDoubleElementsVectorType(V4Int64Ty
);
74 EXPECT_EQ(DoubledTy
, V8Int64Ty
);
75 ASSERT_FALSE(DoubledTy
->isScalable());
76 EXPECT_EQ(DoubledTy
->getNumElements(), 8U);
77 EXPECT_EQ(DoubledTy
->getElementType()->getScalarSizeInBits(), 64U);
79 VectorType
*ConvTy
= VectorType::getInteger(V4Float64Ty
);
80 EXPECT_EQ(ConvTy
, V4Int64Ty
);
81 ASSERT_FALSE(ConvTy
->isScalable());
82 EXPECT_EQ(ConvTy
->getNumElements(), 4U);
83 EXPECT_EQ(ConvTy
->getElementType()->getScalarSizeInBits(), 64U);
85 EltCnt
= V8Int64Ty
->getElementCount();
86 EXPECT_EQ(EltCnt
.Min
, 8U);
87 ASSERT_FALSE(EltCnt
.Scalable
);
90 TEST(VectorTypesTest
, Scalable
) {
93 Type
*Int16Ty
= Type::getInt16Ty(Ctx
);
94 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
95 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
96 Type
*Float64Ty
= Type::getDoubleTy(Ctx
);
98 VectorType
*ScV8Int32Ty
= VectorType::get(Int32Ty
, 8, true);
99 ASSERT_TRUE(ScV8Int32Ty
->isScalable());
100 EXPECT_EQ(ScV8Int32Ty
->getNumElements(), 8U);
101 EXPECT_EQ(ScV8Int32Ty
->getElementType()->getScalarSizeInBits(), 32U);
103 VectorType
*ScV8Int16Ty
= VectorType::get(Int16Ty
, {8, true});
104 ASSERT_TRUE(ScV8Int16Ty
->isScalable());
105 EXPECT_EQ(ScV8Int16Ty
->getNumElements(), 8U);
106 EXPECT_EQ(ScV8Int16Ty
->getElementType()->getScalarSizeInBits(), 16U);
108 ElementCount
EltCnt(4, true);
109 VectorType
*ScV4Int64Ty
= VectorType::get(Int64Ty
, EltCnt
);
110 ASSERT_TRUE(ScV4Int64Ty
->isScalable());
111 EXPECT_EQ(ScV4Int64Ty
->getNumElements(), 4U);
112 EXPECT_EQ(ScV4Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
114 VectorType
*ScV2Int64Ty
= VectorType::get(Int64Ty
, EltCnt
/2);
115 ASSERT_TRUE(ScV2Int64Ty
->isScalable());
116 EXPECT_EQ(ScV2Int64Ty
->getNumElements(), 2U);
117 EXPECT_EQ(ScV2Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
119 VectorType
*ScV8Int64Ty
= VectorType::get(Int64Ty
, EltCnt
*2);
120 ASSERT_TRUE(ScV8Int64Ty
->isScalable());
121 EXPECT_EQ(ScV8Int64Ty
->getNumElements(), 8U);
122 EXPECT_EQ(ScV8Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
124 VectorType
*ScV4Float64Ty
= VectorType::get(Float64Ty
, EltCnt
);
125 ASSERT_TRUE(ScV4Float64Ty
->isScalable());
126 EXPECT_EQ(ScV4Float64Ty
->getNumElements(), 4U);
127 EXPECT_EQ(ScV4Float64Ty
->getElementType()->getScalarSizeInBits(), 64U);
129 VectorType
*ExtTy
= VectorType::getExtendedElementVectorType(ScV8Int16Ty
);
130 EXPECT_EQ(ExtTy
, ScV8Int32Ty
);
131 ASSERT_TRUE(ExtTy
->isScalable());
132 EXPECT_EQ(ExtTy
->getNumElements(), 8U);
133 EXPECT_EQ(ExtTy
->getElementType()->getScalarSizeInBits(), 32U);
135 VectorType
*TruncTy
= VectorType::getTruncatedElementVectorType(ScV8Int32Ty
);
136 EXPECT_EQ(TruncTy
, ScV8Int16Ty
);
137 ASSERT_TRUE(TruncTy
->isScalable());
138 EXPECT_EQ(TruncTy
->getNumElements(), 8U);
139 EXPECT_EQ(TruncTy
->getElementType()->getScalarSizeInBits(), 16U);
141 VectorType
*HalvedTy
= VectorType::getHalfElementsVectorType(ScV4Int64Ty
);
142 EXPECT_EQ(HalvedTy
, ScV2Int64Ty
);
143 ASSERT_TRUE(HalvedTy
->isScalable());
144 EXPECT_EQ(HalvedTy
->getNumElements(), 2U);
145 EXPECT_EQ(HalvedTy
->getElementType()->getScalarSizeInBits(), 64U);
147 VectorType
*DoubledTy
= VectorType::getDoubleElementsVectorType(ScV4Int64Ty
);
148 EXPECT_EQ(DoubledTy
, ScV8Int64Ty
);
149 ASSERT_TRUE(DoubledTy
->isScalable());
150 EXPECT_EQ(DoubledTy
->getNumElements(), 8U);
151 EXPECT_EQ(DoubledTy
->getElementType()->getScalarSizeInBits(), 64U);
153 VectorType
*ConvTy
= VectorType::getInteger(ScV4Float64Ty
);
154 EXPECT_EQ(ConvTy
, ScV4Int64Ty
);
155 ASSERT_TRUE(ConvTy
->isScalable());
156 EXPECT_EQ(ConvTy
->getNumElements(), 4U);
157 EXPECT_EQ(ConvTy
->getElementType()->getScalarSizeInBits(), 64U);
159 EltCnt
= ScV8Int64Ty
->getElementCount();
160 EXPECT_EQ(EltCnt
.Min
, 8U);
161 ASSERT_TRUE(EltCnt
.Scalable
);
164 } // end anonymous namespace