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/DataLayout.h"
10 #include "llvm/IR/DerivedTypes.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/Support/TypeSize.h"
13 #include "gtest/gtest.h"
17 TEST(VectorTypesTest
, FixedLength
) {
20 Type
*Int16Ty
= Type::getInt16Ty(Ctx
);
21 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
22 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
23 Type
*Float64Ty
= Type::getDoubleTy(Ctx
);
25 VectorType
*V8Int32Ty
= VectorType::get(Int32Ty
, 8);
26 ASSERT_FALSE(V8Int32Ty
->isScalable());
27 EXPECT_EQ(V8Int32Ty
->getNumElements(), 8U);
28 EXPECT_EQ(V8Int32Ty
->getElementType()->getScalarSizeInBits(), 32U);
30 VectorType
*V8Int16Ty
= VectorType::get(Int16Ty
, {8, false});
31 ASSERT_FALSE(V8Int16Ty
->isScalable());
32 EXPECT_EQ(V8Int16Ty
->getNumElements(), 8U);
33 EXPECT_EQ(V8Int16Ty
->getElementType()->getScalarSizeInBits(), 16U);
35 ElementCount
EltCnt(4, false);
36 VectorType
*V4Int64Ty
= VectorType::get(Int64Ty
, EltCnt
);
37 ASSERT_FALSE(V4Int64Ty
->isScalable());
38 EXPECT_EQ(V4Int64Ty
->getNumElements(), 4U);
39 EXPECT_EQ(V4Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
41 VectorType
*V2Int64Ty
= VectorType::get(Int64Ty
, EltCnt
/2);
42 ASSERT_FALSE(V2Int64Ty
->isScalable());
43 EXPECT_EQ(V2Int64Ty
->getNumElements(), 2U);
44 EXPECT_EQ(V2Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
46 VectorType
*V8Int64Ty
= VectorType::get(Int64Ty
, EltCnt
*2);
47 ASSERT_FALSE(V8Int64Ty
->isScalable());
48 EXPECT_EQ(V8Int64Ty
->getNumElements(), 8U);
49 EXPECT_EQ(V8Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
51 VectorType
*V4Float64Ty
= VectorType::get(Float64Ty
, EltCnt
);
52 ASSERT_FALSE(V4Float64Ty
->isScalable());
53 EXPECT_EQ(V4Float64Ty
->getNumElements(), 4U);
54 EXPECT_EQ(V4Float64Ty
->getElementType()->getScalarSizeInBits(), 64U);
56 VectorType
*ExtTy
= VectorType::getExtendedElementVectorType(V8Int16Ty
);
57 EXPECT_EQ(ExtTy
, V8Int32Ty
);
58 ASSERT_FALSE(ExtTy
->isScalable());
59 EXPECT_EQ(ExtTy
->getNumElements(), 8U);
60 EXPECT_EQ(ExtTy
->getElementType()->getScalarSizeInBits(), 32U);
62 VectorType
*TruncTy
= VectorType::getTruncatedElementVectorType(V8Int32Ty
);
63 EXPECT_EQ(TruncTy
, V8Int16Ty
);
64 ASSERT_FALSE(TruncTy
->isScalable());
65 EXPECT_EQ(TruncTy
->getNumElements(), 8U);
66 EXPECT_EQ(TruncTy
->getElementType()->getScalarSizeInBits(), 16U);
68 VectorType
*HalvedTy
= VectorType::getHalfElementsVectorType(V4Int64Ty
);
69 EXPECT_EQ(HalvedTy
, V2Int64Ty
);
70 ASSERT_FALSE(HalvedTy
->isScalable());
71 EXPECT_EQ(HalvedTy
->getNumElements(), 2U);
72 EXPECT_EQ(HalvedTy
->getElementType()->getScalarSizeInBits(), 64U);
74 VectorType
*DoubledTy
= VectorType::getDoubleElementsVectorType(V4Int64Ty
);
75 EXPECT_EQ(DoubledTy
, V8Int64Ty
);
76 ASSERT_FALSE(DoubledTy
->isScalable());
77 EXPECT_EQ(DoubledTy
->getNumElements(), 8U);
78 EXPECT_EQ(DoubledTy
->getElementType()->getScalarSizeInBits(), 64U);
80 VectorType
*ConvTy
= VectorType::getInteger(V4Float64Ty
);
81 EXPECT_EQ(ConvTy
, V4Int64Ty
);
82 ASSERT_FALSE(ConvTy
->isScalable());
83 EXPECT_EQ(ConvTy
->getNumElements(), 4U);
84 EXPECT_EQ(ConvTy
->getElementType()->getScalarSizeInBits(), 64U);
86 EltCnt
= V8Int64Ty
->getElementCount();
87 EXPECT_EQ(EltCnt
.Min
, 8U);
88 ASSERT_FALSE(EltCnt
.Scalable
);
91 TEST(VectorTypesTest
, Scalable
) {
94 Type
*Int16Ty
= Type::getInt16Ty(Ctx
);
95 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
96 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
97 Type
*Float64Ty
= Type::getDoubleTy(Ctx
);
99 VectorType
*ScV8Int32Ty
= VectorType::get(Int32Ty
, 8, true);
100 ASSERT_TRUE(ScV8Int32Ty
->isScalable());
101 EXPECT_EQ(ScV8Int32Ty
->getNumElements(), 8U);
102 EXPECT_EQ(ScV8Int32Ty
->getElementType()->getScalarSizeInBits(), 32U);
104 VectorType
*ScV8Int16Ty
= VectorType::get(Int16Ty
, {8, true});
105 ASSERT_TRUE(ScV8Int16Ty
->isScalable());
106 EXPECT_EQ(ScV8Int16Ty
->getNumElements(), 8U);
107 EXPECT_EQ(ScV8Int16Ty
->getElementType()->getScalarSizeInBits(), 16U);
109 ElementCount
EltCnt(4, true);
110 VectorType
*ScV4Int64Ty
= VectorType::get(Int64Ty
, EltCnt
);
111 ASSERT_TRUE(ScV4Int64Ty
->isScalable());
112 EXPECT_EQ(ScV4Int64Ty
->getNumElements(), 4U);
113 EXPECT_EQ(ScV4Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
115 VectorType
*ScV2Int64Ty
= VectorType::get(Int64Ty
, EltCnt
/2);
116 ASSERT_TRUE(ScV2Int64Ty
->isScalable());
117 EXPECT_EQ(ScV2Int64Ty
->getNumElements(), 2U);
118 EXPECT_EQ(ScV2Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
120 VectorType
*ScV8Int64Ty
= VectorType::get(Int64Ty
, EltCnt
*2);
121 ASSERT_TRUE(ScV8Int64Ty
->isScalable());
122 EXPECT_EQ(ScV8Int64Ty
->getNumElements(), 8U);
123 EXPECT_EQ(ScV8Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
125 VectorType
*ScV4Float64Ty
= VectorType::get(Float64Ty
, EltCnt
);
126 ASSERT_TRUE(ScV4Float64Ty
->isScalable());
127 EXPECT_EQ(ScV4Float64Ty
->getNumElements(), 4U);
128 EXPECT_EQ(ScV4Float64Ty
->getElementType()->getScalarSizeInBits(), 64U);
130 VectorType
*ExtTy
= VectorType::getExtendedElementVectorType(ScV8Int16Ty
);
131 EXPECT_EQ(ExtTy
, ScV8Int32Ty
);
132 ASSERT_TRUE(ExtTy
->isScalable());
133 EXPECT_EQ(ExtTy
->getNumElements(), 8U);
134 EXPECT_EQ(ExtTy
->getElementType()->getScalarSizeInBits(), 32U);
136 VectorType
*TruncTy
= VectorType::getTruncatedElementVectorType(ScV8Int32Ty
);
137 EXPECT_EQ(TruncTy
, ScV8Int16Ty
);
138 ASSERT_TRUE(TruncTy
->isScalable());
139 EXPECT_EQ(TruncTy
->getNumElements(), 8U);
140 EXPECT_EQ(TruncTy
->getElementType()->getScalarSizeInBits(), 16U);
142 VectorType
*HalvedTy
= VectorType::getHalfElementsVectorType(ScV4Int64Ty
);
143 EXPECT_EQ(HalvedTy
, ScV2Int64Ty
);
144 ASSERT_TRUE(HalvedTy
->isScalable());
145 EXPECT_EQ(HalvedTy
->getNumElements(), 2U);
146 EXPECT_EQ(HalvedTy
->getElementType()->getScalarSizeInBits(), 64U);
148 VectorType
*DoubledTy
= VectorType::getDoubleElementsVectorType(ScV4Int64Ty
);
149 EXPECT_EQ(DoubledTy
, ScV8Int64Ty
);
150 ASSERT_TRUE(DoubledTy
->isScalable());
151 EXPECT_EQ(DoubledTy
->getNumElements(), 8U);
152 EXPECT_EQ(DoubledTy
->getElementType()->getScalarSizeInBits(), 64U);
154 VectorType
*ConvTy
= VectorType::getInteger(ScV4Float64Ty
);
155 EXPECT_EQ(ConvTy
, ScV4Int64Ty
);
156 ASSERT_TRUE(ConvTy
->isScalable());
157 EXPECT_EQ(ConvTy
->getNumElements(), 4U);
158 EXPECT_EQ(ConvTy
->getElementType()->getScalarSizeInBits(), 64U);
160 EltCnt
= ScV8Int64Ty
->getElementCount();
161 EXPECT_EQ(EltCnt
.Min
, 8U);
162 ASSERT_TRUE(EltCnt
.Scalable
);
165 TEST(VectorTypesTest
, FixedLenComparisons
) {
169 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
170 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
172 VectorType
*V2Int32Ty
= VectorType::get(Int32Ty
, 2);
173 VectorType
*V4Int32Ty
= VectorType::get(Int32Ty
, 4);
175 VectorType
*V2Int64Ty
= VectorType::get(Int64Ty
, 2);
177 TypeSize V2I32Len
= V2Int32Ty
->getPrimitiveSizeInBits();
178 EXPECT_EQ(V2I32Len
.getKnownMinSize(), 64U);
179 EXPECT_FALSE(V2I32Len
.isScalable());
181 EXPECT_LT(V2Int32Ty
->getPrimitiveSizeInBits(),
182 V4Int32Ty
->getPrimitiveSizeInBits());
183 EXPECT_GT(V2Int64Ty
->getPrimitiveSizeInBits(),
184 V2Int32Ty
->getPrimitiveSizeInBits());
185 EXPECT_EQ(V4Int32Ty
->getPrimitiveSizeInBits(),
186 V2Int64Ty
->getPrimitiveSizeInBits());
187 EXPECT_NE(V2Int32Ty
->getPrimitiveSizeInBits(),
188 V2Int64Ty
->getPrimitiveSizeInBits());
190 // Check that a fixed-only comparison works for fixed size vectors.
191 EXPECT_EQ(V2Int64Ty
->getPrimitiveSizeInBits().getFixedSize(),
192 V4Int32Ty
->getPrimitiveSizeInBits().getFixedSize());
194 // Check the DataLayout interfaces.
195 EXPECT_EQ(DL
.getTypeSizeInBits(V2Int64Ty
),
196 DL
.getTypeSizeInBits(V4Int32Ty
));
197 EXPECT_EQ(DL
.getTypeSizeInBits(V2Int32Ty
), 64U);
198 EXPECT_EQ(DL
.getTypeSizeInBits(V2Int64Ty
), 128U);
199 EXPECT_EQ(DL
.getTypeStoreSize(V2Int64Ty
),
200 DL
.getTypeStoreSize(V4Int32Ty
));
201 EXPECT_NE(DL
.getTypeStoreSizeInBits(V2Int32Ty
),
202 DL
.getTypeStoreSizeInBits(V2Int64Ty
));
203 EXPECT_EQ(DL
.getTypeStoreSizeInBits(V2Int32Ty
), 64U);
204 EXPECT_EQ(DL
.getTypeStoreSize(V2Int64Ty
), 16U);
205 EXPECT_EQ(DL
.getTypeAllocSize(V4Int32Ty
),
206 DL
.getTypeAllocSize(V2Int64Ty
));
207 EXPECT_NE(DL
.getTypeAllocSizeInBits(V2Int32Ty
),
208 DL
.getTypeAllocSizeInBits(V2Int64Ty
));
209 EXPECT_EQ(DL
.getTypeAllocSizeInBits(V4Int32Ty
), 128U);
210 EXPECT_EQ(DL
.getTypeAllocSize(V2Int32Ty
), 8U);
211 ASSERT_TRUE(DL
.typeSizeEqualsStoreSize(V4Int32Ty
));
214 TEST(VectorTypesTest
, ScalableComparisons
) {
218 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
219 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
221 VectorType
*ScV2Int32Ty
= VectorType::get(Int32Ty
, {2, true});
222 VectorType
*ScV4Int32Ty
= VectorType::get(Int32Ty
, {4, true});
224 VectorType
*ScV2Int64Ty
= VectorType::get(Int64Ty
, {2, true});
226 TypeSize ScV2I32Len
= ScV2Int32Ty
->getPrimitiveSizeInBits();
227 EXPECT_EQ(ScV2I32Len
.getKnownMinSize(), 64U);
228 EXPECT_TRUE(ScV2I32Len
.isScalable());
230 EXPECT_LT(ScV2Int32Ty
->getPrimitiveSizeInBits(),
231 ScV4Int32Ty
->getPrimitiveSizeInBits());
232 EXPECT_GT(ScV2Int64Ty
->getPrimitiveSizeInBits(),
233 ScV2Int32Ty
->getPrimitiveSizeInBits());
234 EXPECT_EQ(ScV4Int32Ty
->getPrimitiveSizeInBits(),
235 ScV2Int64Ty
->getPrimitiveSizeInBits());
236 EXPECT_NE(ScV2Int32Ty
->getPrimitiveSizeInBits(),
237 ScV2Int64Ty
->getPrimitiveSizeInBits());
239 // Check the DataLayout interfaces.
240 EXPECT_EQ(DL
.getTypeSizeInBits(ScV2Int64Ty
),
241 DL
.getTypeSizeInBits(ScV4Int32Ty
));
242 EXPECT_EQ(DL
.getTypeSizeInBits(ScV2Int32Ty
).getKnownMinSize(), 64U);
243 EXPECT_EQ(DL
.getTypeStoreSize(ScV2Int64Ty
),
244 DL
.getTypeStoreSize(ScV4Int32Ty
));
245 EXPECT_NE(DL
.getTypeStoreSizeInBits(ScV2Int32Ty
),
246 DL
.getTypeStoreSizeInBits(ScV2Int64Ty
));
247 EXPECT_EQ(DL
.getTypeStoreSizeInBits(ScV2Int32Ty
).getKnownMinSize(), 64U);
248 EXPECT_EQ(DL
.getTypeStoreSize(ScV2Int64Ty
).getKnownMinSize(), 16U);
249 EXPECT_EQ(DL
.getTypeAllocSize(ScV4Int32Ty
),
250 DL
.getTypeAllocSize(ScV2Int64Ty
));
251 EXPECT_NE(DL
.getTypeAllocSizeInBits(ScV2Int32Ty
),
252 DL
.getTypeAllocSizeInBits(ScV2Int64Ty
));
253 EXPECT_EQ(DL
.getTypeAllocSizeInBits(ScV4Int32Ty
).getKnownMinSize(), 128U);
254 EXPECT_EQ(DL
.getTypeAllocSize(ScV2Int32Ty
).getKnownMinSize(), 8U);
255 ASSERT_TRUE(DL
.typeSizeEqualsStoreSize(ScV4Int32Ty
));
258 TEST(VectorTypesTest
, CrossComparisons
) {
261 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
263 VectorType
*V4Int32Ty
= VectorType::get(Int32Ty
, {4, false});
264 VectorType
*ScV4Int32Ty
= VectorType::get(Int32Ty
, {4, true});
266 // Even though the minimum size is the same, a scalable vector could be
267 // larger so we don't consider them to be the same size.
268 EXPECT_NE(V4Int32Ty
->getPrimitiveSizeInBits(),
269 ScV4Int32Ty
->getPrimitiveSizeInBits());
270 // If we are only checking the minimum, then they are the same size.
271 EXPECT_EQ(V4Int32Ty
->getPrimitiveSizeInBits().getKnownMinSize(),
272 ScV4Int32Ty
->getPrimitiveSizeInBits().getKnownMinSize());
274 // We can't use ordering comparisons (<,<=,>,>=) between scalable and
275 // non-scalable vector sizes.
278 } // end anonymous namespace