[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / llvm / unittests / CodeGen / ScalableVectorMVTsTest.cpp
blobc343465ae4c72d7c9730d83e13e09a916e276857
1 //===-------- llvm/unittest/CodeGen/ScalableVectorMVTsTest.cpp ------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/ValueTypes.h"
10 #include "llvm/CodeGenTypes/MachineValueType.h"
11 #include "llvm/IR/DerivedTypes.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/Support/TypeSize.h"
14 #include "gtest/gtest.h"
16 using namespace llvm;
18 namespace {
20 TEST(ScalableVectorMVTsTest, IntegerMVTs) {
21 for (MVT VecTy : MVT::integer_scalable_vector_valuetypes()) {
22 ASSERT_TRUE(VecTy.isValid());
23 ASSERT_TRUE(VecTy.isInteger());
24 ASSERT_TRUE(VecTy.isVector());
25 ASSERT_TRUE(VecTy.isScalableVector());
26 ASSERT_TRUE(VecTy.getScalarType().isValid());
28 ASSERT_FALSE(VecTy.isFloatingPoint());
32 TEST(ScalableVectorMVTsTest, FloatMVTs) {
33 for (MVT VecTy : MVT::fp_scalable_vector_valuetypes()) {
34 ASSERT_TRUE(VecTy.isValid());
35 ASSERT_TRUE(VecTy.isFloatingPoint());
36 ASSERT_TRUE(VecTy.isVector());
37 ASSERT_TRUE(VecTy.isScalableVector());
38 ASSERT_TRUE(VecTy.getScalarType().isValid());
40 ASSERT_FALSE(VecTy.isInteger());
44 TEST(ScalableVectorMVTsTest, HelperFuncs) {
45 LLVMContext Ctx;
47 // Create with scalable flag
48 EVT Vnx4i32 = EVT::getVectorVT(Ctx, MVT::i32, 4, /*Scalable=*/true);
49 ASSERT_TRUE(Vnx4i32.isScalableVector());
51 // Create with separate llvm::ElementCount
52 auto EltCnt = ElementCount::getScalable(2);
53 EVT Vnx2i32 = EVT::getVectorVT(Ctx, MVT::i32, EltCnt);
54 ASSERT_TRUE(Vnx2i32.isScalableVector());
56 // Create with inline llvm::ElementCount
57 EVT Vnx2i64 = EVT::getVectorVT(Ctx, MVT::i64, ElementCount::getScalable(2));
58 ASSERT_TRUE(Vnx2i64.isScalableVector());
60 // Check that changing scalar types/element count works
61 EXPECT_EQ(Vnx2i32.widenIntegerVectorElementType(Ctx), Vnx2i64);
62 EXPECT_EQ(Vnx4i32.getHalfNumVectorElementsVT(Ctx), Vnx2i32);
64 // Check that operators work
65 EXPECT_EQ(EVT::getVectorVT(Ctx, MVT::i64, EltCnt * 2), MVT::nxv4i64);
66 EXPECT_EQ(EVT::getVectorVT(Ctx, MVT::i64, EltCnt.divideCoefficientBy(2)),
67 MVT::nxv1i64);
69 // Check that float->int conversion works
70 EVT Vnx2f64 = EVT::getVectorVT(Ctx, MVT::f64, ElementCount::getScalable(2));
71 EXPECT_EQ(Vnx2f64.changeTypeToInteger(), Vnx2i64);
73 // Check fields inside llvm::ElementCount
74 EltCnt = Vnx4i32.getVectorElementCount();
75 EXPECT_EQ(EltCnt.getKnownMinValue(), 4U);
76 ASSERT_TRUE(EltCnt.isScalable());
78 // Check that fixed-length vector types aren't scalable.
79 EVT V8i32 = EVT::getVectorVT(Ctx, MVT::i32, 8);
80 ASSERT_FALSE(V8i32.isScalableVector());
81 EVT V4f64 = EVT::getVectorVT(Ctx, MVT::f64, ElementCount::getFixed(4));
82 ASSERT_FALSE(V4f64.isScalableVector());
84 // Check that llvm::ElementCount works for fixed-length types.
85 EltCnt = V8i32.getVectorElementCount();
86 EXPECT_EQ(EltCnt.getKnownMinValue(), 8U);
87 ASSERT_FALSE(EltCnt.isScalable());
90 TEST(ScalableVectorMVTsTest, IRToVTTranslation) {
91 LLVMContext Ctx;
93 Type *Int64Ty = Type::getInt64Ty(Ctx);
94 VectorType *ScV8Int64Ty =
95 VectorType::get(Int64Ty, ElementCount::getScalable(8));
97 // Check that we can map a scalable IR type to an MVT
98 MVT Mnxv8i64 = MVT::getVT(ScV8Int64Ty);
99 ASSERT_TRUE(Mnxv8i64.isScalableVector());
100 ASSERT_EQ(ScV8Int64Ty->getElementCount(), Mnxv8i64.getVectorElementCount());
101 ASSERT_EQ(MVT::getVT(ScV8Int64Ty->getElementType()),
102 Mnxv8i64.getScalarType());
104 // Check that we can map a scalable IR type to an EVT
105 EVT Enxv8i64 = EVT::getEVT(ScV8Int64Ty);
106 ASSERT_TRUE(Enxv8i64.isScalableVector());
107 ASSERT_EQ(ScV8Int64Ty->getElementCount(), Enxv8i64.getVectorElementCount());
108 ASSERT_EQ(EVT::getEVT(ScV8Int64Ty->getElementType()),
109 Enxv8i64.getScalarType());
112 TEST(ScalableVectorMVTsTest, VTToIRTranslation) {
113 LLVMContext Ctx;
115 EVT Enxv4f64 = EVT::getVectorVT(Ctx, MVT::f64, ElementCount::getScalable(4));
117 Type *Ty = Enxv4f64.getTypeForEVT(Ctx);
118 VectorType *ScV4Float64Ty = cast<VectorType>(Ty);
119 ASSERT_TRUE(isa<ScalableVectorType>(ScV4Float64Ty));
120 ASSERT_EQ(Enxv4f64.getVectorElementCount(), ScV4Float64Ty->getElementCount());
121 ASSERT_EQ(Enxv4f64.getScalarType().getTypeForEVT(Ctx),
122 ScV4Float64Ty->getElementType());
125 TEST(ScalableVectorMVTsTest, SizeQueries) {
126 LLVMContext Ctx;
128 EVT nxv4i32 = EVT::getVectorVT(Ctx, MVT::i32, 4, /*Scalable=*/ true);
129 EVT nxv2i32 = EVT::getVectorVT(Ctx, MVT::i32, 2, /*Scalable=*/ true);
130 EVT nxv2i64 = EVT::getVectorVT(Ctx, MVT::i64, 2, /*Scalable=*/ true);
131 EVT nxv2f64 = EVT::getVectorVT(Ctx, MVT::f64, 2, /*Scalable=*/ true);
133 EVT v4i32 = EVT::getVectorVT(Ctx, MVT::i32, 4);
134 EVT v2i32 = EVT::getVectorVT(Ctx, MVT::i32, 2);
135 EVT v2i64 = EVT::getVectorVT(Ctx, MVT::i64, 2);
136 EVT v2f64 = EVT::getVectorVT(Ctx, MVT::f64, 2);
138 EVT nxv5i32 = EVT::getVectorVT(Ctx, MVT::i32, 5, /*Scalable=*/true);
139 ASSERT_FALSE(nxv5i32.is16BitVector());
140 ASSERT_FALSE(nxv5i32.is32BitVector());
141 ASSERT_FALSE(nxv5i32.is64BitVector());
142 ASSERT_FALSE(nxv5i32.is128BitVector());
143 ASSERT_FALSE(nxv5i32.is256BitVector());
144 ASSERT_FALSE(nxv5i32.is512BitVector());
145 ASSERT_FALSE(nxv5i32.is1024BitVector());
146 ASSERT_FALSE(nxv5i32.is2048BitVector());
148 // Check equivalence and ordering on scalable types.
149 EXPECT_EQ(nxv4i32.getSizeInBits(), nxv2i64.getSizeInBits());
150 EXPECT_EQ(nxv2f64.getSizeInBits(), nxv2i64.getSizeInBits());
151 EXPECT_NE(nxv2i32.getSizeInBits(), nxv4i32.getSizeInBits());
152 EXPECT_LT(nxv2i32.getSizeInBits().getKnownMinValue(),
153 nxv2i64.getSizeInBits().getKnownMinValue());
154 EXPECT_LE(nxv4i32.getSizeInBits().getKnownMinValue(),
155 nxv2i64.getSizeInBits().getKnownMinValue());
156 EXPECT_GT(nxv4i32.getSizeInBits().getKnownMinValue(),
157 nxv2i32.getSizeInBits().getKnownMinValue());
158 EXPECT_GE(nxv2i64.getSizeInBits().getKnownMinValue(),
159 nxv4i32.getSizeInBits().getKnownMinValue());
161 // Check equivalence and ordering on fixed types.
162 EXPECT_EQ(v4i32.getSizeInBits(), v2i64.getSizeInBits());
163 EXPECT_EQ(v2f64.getSizeInBits(), v2i64.getSizeInBits());
164 EXPECT_NE(v2i32.getSizeInBits(), v4i32.getSizeInBits());
165 EXPECT_LT(v2i32.getFixedSizeInBits(), v2i64.getFixedSizeInBits());
166 EXPECT_LE(v4i32.getFixedSizeInBits(), v2i64.getFixedSizeInBits());
167 EXPECT_GT(v4i32.getFixedSizeInBits(), v2i32.getFixedSizeInBits());
168 EXPECT_GE(v2i64.getFixedSizeInBits(), v4i32.getFixedSizeInBits());
170 // Check that scalable and non-scalable types with the same minimum size
171 // are not considered equal.
172 ASSERT_TRUE(v4i32.getSizeInBits() != nxv4i32.getSizeInBits());
173 ASSERT_FALSE(v2i64.getSizeInBits() == nxv2f64.getSizeInBits());
175 // Check that we can obtain a known-exact size from a non-scalable type.
176 EXPECT_EQ(v4i32.getFixedSizeInBits(), 128U);
177 EXPECT_EQ(v2i64.getFixedSizeInBits(), 128U);
179 // Check that we can query the known minimum size for both scalable and
180 // fixed length types.
181 EXPECT_EQ(nxv2i32.getSizeInBits().getKnownMinValue(), 64U);
182 EXPECT_EQ(nxv2f64.getSizeInBits().getKnownMinValue(), 128U);
183 EXPECT_EQ(v2i32.getSizeInBits().getKnownMinValue(),
184 nxv2i32.getSizeInBits().getKnownMinValue());
186 // Check scalable property.
187 ASSERT_FALSE(v4i32.getSizeInBits().isScalable());
188 ASSERT_TRUE(nxv4i32.getSizeInBits().isScalable());
190 // Check convenience size scaling methods.
191 EXPECT_EQ(v2i32.getSizeInBits() * 2, v4i32.getSizeInBits());
192 EXPECT_EQ(2 * nxv2i32.getSizeInBits(), nxv4i32.getSizeInBits());
193 EXPECT_EQ(nxv2f64.getSizeInBits().divideCoefficientBy(2),
194 nxv2i32.getSizeInBits());
197 } // end anonymous namespace