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"
18 #define EXPECT_VTY_EQ(LHS, RHS) \
20 ASSERT_NE(LHS, nullptr) << #LHS << " must not be null"; \
21 ASSERT_NE(RHS, nullptr) << #RHS << " must not be null"; \
22 EXPECT_EQ(LHS, RHS) << "Expect that " << #LHS << " == " << #RHS \
23 << " where " << #LHS << " = " << *LHS << " and " \
24 << #RHS << " = " << *RHS; \
27 #define EXPECT_VTY_NE(LHS, RHS) \
29 ASSERT_NE(LHS, nullptr) << #LHS << " must not be null"; \
30 ASSERT_NE(RHS, nullptr) << #RHS << " must not be null"; \
31 EXPECT_NE(LHS, RHS) << "Expect that " << #LHS << " != " << #RHS \
32 << " where " << #LHS << " = " << *LHS << " and " \
33 << #RHS << " = " << *RHS; \
36 TEST(VectorTypesTest
, FixedLength
) {
39 Type
*Int8Ty
= Type::getInt8Ty(Ctx
);
40 Type
*Int16Ty
= Type::getInt16Ty(Ctx
);
41 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
42 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
43 Type
*Float64Ty
= Type::getDoubleTy(Ctx
);
45 auto *V16Int8Ty
= FixedVectorType::get(Int8Ty
, 16);
46 ASSERT_NE(nullptr, V16Int8Ty
);
47 EXPECT_EQ(V16Int8Ty
->getNumElements(), 16U);
48 EXPECT_EQ(V16Int8Ty
->getElementType()->getScalarSizeInBits(), 8U);
51 dyn_cast
<FixedVectorType
>(VectorType::get(Int32Ty
, 8, false));
52 ASSERT_NE(nullptr, V8Int32Ty
);
53 EXPECT_EQ(V8Int32Ty
->getNumElements(), 8U);
54 EXPECT_EQ(V8Int32Ty
->getElementType()->getScalarSizeInBits(), 32U);
57 dyn_cast
<FixedVectorType
>(VectorType::get(Int8Ty
, V8Int32Ty
));
58 EXPECT_VTY_NE(V8Int32Ty
, V8Int8Ty
);
59 EXPECT_EQ(V8Int8Ty
->getElementCount(), V8Int32Ty
->getElementCount());
60 EXPECT_EQ(V8Int8Ty
->getElementType()->getScalarSizeInBits(), 8U);
63 dyn_cast
<FixedVectorType
>(VectorType::get(Int32Ty
, V8Int32Ty
));
64 EXPECT_VTY_EQ(V8Int32Ty
, V8Int32Ty2
);
66 auto *V8Int16Ty
= dyn_cast
<FixedVectorType
>(
67 VectorType::get(Int16Ty
, ElementCount::getFixed(8)));
68 ASSERT_NE(nullptr, V8Int16Ty
);
69 EXPECT_EQ(V8Int16Ty
->getNumElements(), 8U);
70 EXPECT_EQ(V8Int16Ty
->getElementType()->getScalarSizeInBits(), 16U);
72 auto EltCnt
= ElementCount::getFixed(4);
73 auto *V4Int64Ty
= dyn_cast
<FixedVectorType
>(VectorType::get(Int64Ty
, EltCnt
));
74 ASSERT_NE(nullptr, V4Int64Ty
);
75 EXPECT_EQ(V4Int64Ty
->getNumElements(), 4U);
76 EXPECT_EQ(V4Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
78 auto *V2Int64Ty
= dyn_cast
<FixedVectorType
>(
79 VectorType::get(Int64Ty
, EltCnt
.divideCoefficientBy(2)));
80 ASSERT_NE(nullptr, V2Int64Ty
);
81 EXPECT_EQ(V2Int64Ty
->getNumElements(), 2U);
82 EXPECT_EQ(V2Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
85 dyn_cast
<FixedVectorType
>(VectorType::get(Int64Ty
, EltCnt
* 2));
86 ASSERT_NE(nullptr, V8Int64Ty
);
87 EXPECT_EQ(V8Int64Ty
->getNumElements(), 8U);
88 EXPECT_EQ(V8Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
91 dyn_cast
<FixedVectorType
>(VectorType::get(Float64Ty
, EltCnt
));
92 ASSERT_NE(nullptr, V4Float64Ty
);
93 EXPECT_EQ(V4Float64Ty
->getNumElements(), 4U);
94 EXPECT_EQ(V4Float64Ty
->getElementType()->getScalarSizeInBits(), 64U);
96 auto *ExtTy
= dyn_cast
<FixedVectorType
>(
97 VectorType::getExtendedElementVectorType(V8Int16Ty
));
98 EXPECT_VTY_EQ(ExtTy
, V8Int32Ty
);
99 EXPECT_EQ(ExtTy
->getNumElements(), 8U);
100 EXPECT_EQ(ExtTy
->getElementType()->getScalarSizeInBits(), 32U);
102 auto *TruncTy
= dyn_cast
<FixedVectorType
>(
103 VectorType::getTruncatedElementVectorType(V8Int32Ty
));
104 EXPECT_VTY_EQ(TruncTy
, V8Int16Ty
);
105 EXPECT_EQ(TruncTy
->getNumElements(), 8U);
106 EXPECT_EQ(TruncTy
->getElementType()->getScalarSizeInBits(), 16U);
108 auto *HalvedTy
= dyn_cast
<FixedVectorType
>(
109 VectorType::getHalfElementsVectorType(V4Int64Ty
));
110 EXPECT_VTY_EQ(HalvedTy
, V2Int64Ty
);
111 EXPECT_EQ(HalvedTy
->getNumElements(), 2U);
112 EXPECT_EQ(HalvedTy
->getElementType()->getScalarSizeInBits(), 64U);
114 auto *DoubledTy
= dyn_cast
<FixedVectorType
>(
115 VectorType::getDoubleElementsVectorType(V4Int64Ty
));
116 EXPECT_VTY_EQ(DoubledTy
, V8Int64Ty
);
117 EXPECT_EQ(DoubledTy
->getNumElements(), 8U);
118 EXPECT_EQ(DoubledTy
->getElementType()->getScalarSizeInBits(), 64U);
120 auto *ConvTy
= dyn_cast
<FixedVectorType
>(VectorType::getInteger(V4Float64Ty
));
121 EXPECT_VTY_EQ(ConvTy
, V4Int64Ty
);
122 EXPECT_EQ(ConvTy
->getNumElements(), 4U);
123 EXPECT_EQ(ConvTy
->getElementType()->getScalarSizeInBits(), 64U);
125 EltCnt
= V8Int64Ty
->getElementCount();
126 EXPECT_EQ(EltCnt
.getKnownMinValue(), 8U);
127 ASSERT_FALSE(EltCnt
.isScalable());
130 TEST(VectorTypesTest
, Scalable
) {
133 Type
*Int8Ty
= Type::getInt8Ty(Ctx
);
134 Type
*Int16Ty
= Type::getInt16Ty(Ctx
);
135 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
136 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
137 Type
*Float64Ty
= Type::getDoubleTy(Ctx
);
139 auto *ScV16Int8Ty
= ScalableVectorType::get(Int8Ty
, 16);
140 ASSERT_NE(nullptr, ScV16Int8Ty
);
141 EXPECT_EQ(ScV16Int8Ty
->getMinNumElements(), 16U);
142 EXPECT_EQ(ScV16Int8Ty
->getScalarSizeInBits(), 8U);
145 dyn_cast
<ScalableVectorType
>(VectorType::get(Int32Ty
, 8, true));
146 ASSERT_NE(nullptr, ScV8Int32Ty
);
147 EXPECT_EQ(ScV8Int32Ty
->getMinNumElements(), 8U);
148 EXPECT_EQ(ScV8Int32Ty
->getElementType()->getScalarSizeInBits(), 32U);
151 dyn_cast
<ScalableVectorType
>(VectorType::get(Int8Ty
, ScV8Int32Ty
));
152 EXPECT_VTY_NE(ScV8Int32Ty
, ScV8Int8Ty
);
153 EXPECT_EQ(ScV8Int8Ty
->getElementCount(), ScV8Int32Ty
->getElementCount());
154 EXPECT_EQ(ScV8Int8Ty
->getElementType()->getScalarSizeInBits(), 8U);
157 dyn_cast
<ScalableVectorType
>(VectorType::get(Int32Ty
, ScV8Int32Ty
));
158 EXPECT_VTY_EQ(ScV8Int32Ty
, ScV8Int32Ty2
);
160 auto *ScV8Int16Ty
= dyn_cast
<ScalableVectorType
>(
161 VectorType::get(Int16Ty
, ElementCount::getScalable(8)));
162 ASSERT_NE(nullptr, ScV8Int16Ty
);
163 EXPECT_EQ(ScV8Int16Ty
->getMinNumElements(), 8U);
164 EXPECT_EQ(ScV8Int16Ty
->getElementType()->getScalarSizeInBits(), 16U);
166 auto EltCnt
= ElementCount::getScalable(4);
168 dyn_cast
<ScalableVectorType
>(VectorType::get(Int64Ty
, EltCnt
));
169 ASSERT_NE(nullptr, ScV4Int64Ty
);
170 EXPECT_EQ(ScV4Int64Ty
->getMinNumElements(), 4U);
171 EXPECT_EQ(ScV4Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
173 auto *ScV2Int64Ty
= dyn_cast
<ScalableVectorType
>(
174 VectorType::get(Int64Ty
, EltCnt
.divideCoefficientBy(2)));
175 ASSERT_NE(nullptr, ScV2Int64Ty
);
176 EXPECT_EQ(ScV2Int64Ty
->getMinNumElements(), 2U);
177 EXPECT_EQ(ScV2Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
180 dyn_cast
<ScalableVectorType
>(VectorType::get(Int64Ty
, EltCnt
* 2));
181 ASSERT_NE(nullptr, ScV8Int64Ty
);
182 EXPECT_EQ(ScV8Int64Ty
->getMinNumElements(), 8U);
183 EXPECT_EQ(ScV8Int64Ty
->getElementType()->getScalarSizeInBits(), 64U);
185 auto *ScV4Float64Ty
=
186 dyn_cast
<ScalableVectorType
>(VectorType::get(Float64Ty
, EltCnt
));
187 ASSERT_NE(nullptr, ScV4Float64Ty
);
188 EXPECT_EQ(ScV4Float64Ty
->getMinNumElements(), 4U);
189 EXPECT_EQ(ScV4Float64Ty
->getElementType()->getScalarSizeInBits(), 64U);
191 auto *ExtTy
= dyn_cast
<ScalableVectorType
>(
192 VectorType::getExtendedElementVectorType(ScV8Int16Ty
));
193 EXPECT_VTY_EQ(ExtTy
, ScV8Int32Ty
);
194 EXPECT_EQ(ExtTy
->getMinNumElements(), 8U);
195 EXPECT_EQ(ExtTy
->getElementType()->getScalarSizeInBits(), 32U);
197 auto *TruncTy
= dyn_cast
<ScalableVectorType
>(
198 VectorType::getTruncatedElementVectorType(ScV8Int32Ty
));
199 EXPECT_VTY_EQ(TruncTy
, ScV8Int16Ty
);
200 EXPECT_EQ(TruncTy
->getMinNumElements(), 8U);
201 EXPECT_EQ(TruncTy
->getElementType()->getScalarSizeInBits(), 16U);
203 auto *HalvedTy
= dyn_cast
<ScalableVectorType
>(
204 VectorType::getHalfElementsVectorType(ScV4Int64Ty
));
205 EXPECT_VTY_EQ(HalvedTy
, ScV2Int64Ty
);
206 EXPECT_EQ(HalvedTy
->getMinNumElements(), 2U);
207 EXPECT_EQ(HalvedTy
->getElementType()->getScalarSizeInBits(), 64U);
209 auto *DoubledTy
= dyn_cast
<ScalableVectorType
>(
210 VectorType::getDoubleElementsVectorType(ScV4Int64Ty
));
211 EXPECT_VTY_EQ(DoubledTy
, ScV8Int64Ty
);
212 EXPECT_EQ(DoubledTy
->getMinNumElements(), 8U);
213 EXPECT_EQ(DoubledTy
->getElementType()->getScalarSizeInBits(), 64U);
216 dyn_cast
<ScalableVectorType
>(VectorType::getInteger(ScV4Float64Ty
));
217 EXPECT_VTY_EQ(ConvTy
, ScV4Int64Ty
);
218 EXPECT_EQ(ConvTy
->getMinNumElements(), 4U);
219 EXPECT_EQ(ConvTy
->getElementType()->getScalarSizeInBits(), 64U);
221 EltCnt
= ScV8Int64Ty
->getElementCount();
222 EXPECT_EQ(EltCnt
.getKnownMinValue(), 8U);
223 ASSERT_TRUE(EltCnt
.isScalable());
226 TEST(VectorTypesTest
, BaseVectorType
) {
229 Type
*Int16Ty
= Type::getInt16Ty(Ctx
);
230 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
232 std::array
<VectorType
*, 8> VTys
= {
233 VectorType::get(Int16Ty
, ElementCount::getScalable(4)),
234 VectorType::get(Int16Ty
, ElementCount::getFixed(4)),
235 VectorType::get(Int16Ty
, ElementCount::getScalable(2)),
236 VectorType::get(Int16Ty
, ElementCount::getFixed(2)),
237 VectorType::get(Int32Ty
, ElementCount::getScalable(4)),
238 VectorType::get(Int32Ty
, ElementCount::getFixed(4)),
239 VectorType::get(Int32Ty
, ElementCount::getScalable(2)),
240 VectorType::get(Int32Ty
, ElementCount::getFixed(2))};
243 The comparison matrix is symmetric, so we only check the upper triangle:
245 (0,0) (0,1) (0,2) ... (0,7)
253 for (size_t I
= 0, IEnd
= VTys
.size(); I
< IEnd
; ++I
) {
255 VectorType
*VI
= VTys
[I
];
256 ElementCount ECI
= VI
->getElementCount();
257 EXPECT_EQ(isa
<ScalableVectorType
>(VI
), ECI
.isScalable());
259 for (size_t J
= I
+ 1, JEnd
= VTys
.size(); J
< JEnd
; ++J
) {
261 VectorType
*VJ
= VTys
[J
];
262 EXPECT_VTY_NE(VI
, VJ
);
264 VectorType
*VJPrime
= VectorType::get(VI
->getElementType(), VJ
);
265 if (VI
->getElementType() == VJ
->getElementType()) {
266 EXPECT_VTY_EQ(VJ
, VJPrime
);
268 EXPECT_VTY_NE(VJ
, VJPrime
);
271 EXPECT_EQ(VJ
->getTypeID(), VJPrime
->getTypeID())
272 << "VJ and VJPrime are the same sort of vector";
277 TEST(VectorTypesTest
, FixedLenComparisons
) {
281 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
282 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
284 auto *V2Int32Ty
= FixedVectorType::get(Int32Ty
, 2);
285 auto *V4Int32Ty
= FixedVectorType::get(Int32Ty
, 4);
287 auto *V2Int64Ty
= FixedVectorType::get(Int64Ty
, 2);
289 TypeSize V2I32Len
= V2Int32Ty
->getPrimitiveSizeInBits();
290 EXPECT_EQ(V2I32Len
.getKnownMinValue(), 64U);
291 EXPECT_FALSE(V2I32Len
.isScalable());
293 EXPECT_LT(V2Int32Ty
->getPrimitiveSizeInBits().getFixedValue(),
294 V4Int32Ty
->getPrimitiveSizeInBits().getFixedValue());
295 EXPECT_GT(V2Int64Ty
->getPrimitiveSizeInBits().getFixedValue(),
296 V2Int32Ty
->getPrimitiveSizeInBits().getFixedValue());
297 EXPECT_EQ(V4Int32Ty
->getPrimitiveSizeInBits(),
298 V2Int64Ty
->getPrimitiveSizeInBits());
299 EXPECT_NE(V2Int32Ty
->getPrimitiveSizeInBits(),
300 V2Int64Ty
->getPrimitiveSizeInBits());
302 // Check that a fixed-only comparison works for fixed size vectors.
303 EXPECT_EQ(V2Int64Ty
->getPrimitiveSizeInBits().getFixedValue(),
304 V4Int32Ty
->getPrimitiveSizeInBits().getFixedValue());
306 // Check the DataLayout interfaces.
307 EXPECT_EQ(DL
.getTypeSizeInBits(V2Int64Ty
), DL
.getTypeSizeInBits(V4Int32Ty
));
308 EXPECT_EQ(DL
.getTypeSizeInBits(V2Int32Ty
), 64U);
309 EXPECT_EQ(DL
.getTypeSizeInBits(V2Int64Ty
), 128U);
310 EXPECT_EQ(DL
.getTypeStoreSize(V2Int64Ty
), DL
.getTypeStoreSize(V4Int32Ty
));
311 EXPECT_NE(DL
.getTypeStoreSizeInBits(V2Int32Ty
),
312 DL
.getTypeStoreSizeInBits(V2Int64Ty
));
313 EXPECT_EQ(DL
.getTypeStoreSizeInBits(V2Int32Ty
), 64U);
314 EXPECT_EQ(DL
.getTypeStoreSize(V2Int64Ty
), 16U);
315 EXPECT_EQ(DL
.getTypeAllocSize(V4Int32Ty
), DL
.getTypeAllocSize(V2Int64Ty
));
316 EXPECT_NE(DL
.getTypeAllocSizeInBits(V2Int32Ty
),
317 DL
.getTypeAllocSizeInBits(V2Int64Ty
));
318 EXPECT_EQ(DL
.getTypeAllocSizeInBits(V4Int32Ty
), 128U);
319 EXPECT_EQ(DL
.getTypeAllocSize(V2Int32Ty
), 8U);
320 ASSERT_TRUE(DL
.typeSizeEqualsStoreSize(V4Int32Ty
));
323 TEST(VectorTypesTest
, ScalableComparisons
) {
327 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
328 Type
*Int64Ty
= Type::getInt64Ty(Ctx
);
330 auto *ScV2Int32Ty
= ScalableVectorType::get(Int32Ty
, 2);
331 auto *ScV4Int32Ty
= ScalableVectorType::get(Int32Ty
, 4);
333 auto *ScV2Int64Ty
= ScalableVectorType::get(Int64Ty
, 2);
335 TypeSize ScV2I32Len
= ScV2Int32Ty
->getPrimitiveSizeInBits();
336 EXPECT_EQ(ScV2I32Len
.getKnownMinValue(), 64U);
337 EXPECT_TRUE(ScV2I32Len
.isScalable());
339 EXPECT_LT(ScV2Int32Ty
->getPrimitiveSizeInBits().getKnownMinValue(),
340 ScV4Int32Ty
->getPrimitiveSizeInBits().getKnownMinValue());
341 EXPECT_GT(ScV2Int64Ty
->getPrimitiveSizeInBits().getKnownMinValue(),
342 ScV2Int32Ty
->getPrimitiveSizeInBits().getKnownMinValue());
343 EXPECT_EQ(ScV4Int32Ty
->getPrimitiveSizeInBits().getKnownMinValue(),
344 ScV2Int64Ty
->getPrimitiveSizeInBits().getKnownMinValue());
345 EXPECT_NE(ScV2Int32Ty
->getPrimitiveSizeInBits().getKnownMinValue(),
346 ScV2Int64Ty
->getPrimitiveSizeInBits().getKnownMinValue());
348 // Check the DataLayout interfaces.
349 EXPECT_EQ(DL
.getTypeSizeInBits(ScV2Int64Ty
),
350 DL
.getTypeSizeInBits(ScV4Int32Ty
));
351 EXPECT_EQ(DL
.getTypeSizeInBits(ScV2Int32Ty
).getKnownMinValue(), 64U);
352 EXPECT_EQ(DL
.getTypeStoreSize(ScV2Int64Ty
), DL
.getTypeStoreSize(ScV4Int32Ty
));
353 EXPECT_NE(DL
.getTypeStoreSizeInBits(ScV2Int32Ty
),
354 DL
.getTypeStoreSizeInBits(ScV2Int64Ty
));
355 EXPECT_EQ(DL
.getTypeStoreSizeInBits(ScV2Int32Ty
).getKnownMinValue(), 64U);
356 EXPECT_EQ(DL
.getTypeStoreSize(ScV2Int64Ty
).getKnownMinValue(), 16U);
357 EXPECT_EQ(DL
.getTypeAllocSize(ScV4Int32Ty
), DL
.getTypeAllocSize(ScV2Int64Ty
));
358 EXPECT_NE(DL
.getTypeAllocSizeInBits(ScV2Int32Ty
),
359 DL
.getTypeAllocSizeInBits(ScV2Int64Ty
));
360 EXPECT_EQ(DL
.getTypeAllocSizeInBits(ScV4Int32Ty
).getKnownMinValue(), 128U);
361 EXPECT_EQ(DL
.getTypeAllocSize(ScV2Int32Ty
).getKnownMinValue(), 8U);
362 ASSERT_TRUE(DL
.typeSizeEqualsStoreSize(ScV4Int32Ty
));
365 TEST(VectorTypesTest
, CrossComparisons
) {
368 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
370 auto *V4Int32Ty
= FixedVectorType::get(Int32Ty
, 4);
371 auto *ScV4Int32Ty
= ScalableVectorType::get(Int32Ty
, 4);
373 // Even though the minimum size is the same, a scalable vector could be
374 // larger so we don't consider them to be the same size.
375 EXPECT_NE(V4Int32Ty
->getPrimitiveSizeInBits(),
376 ScV4Int32Ty
->getPrimitiveSizeInBits());
377 // If we are only checking the minimum, then they are the same size.
378 EXPECT_EQ(V4Int32Ty
->getPrimitiveSizeInBits().getKnownMinValue(),
379 ScV4Int32Ty
->getPrimitiveSizeInBits().getKnownMinValue());
381 // We can't use ordering comparisons (<,<=,>,>=) between scalable and
382 // non-scalable vector sizes.
385 } // end anonymous namespace