Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/CodeGen/GlobalISel/LowLevelTypeTest.cpp --------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
NAKAMURA Takumi | d45fae6 | 2023-04-23 21:46:09 +0900 | [diff] [blame] | 9 | #include "llvm/CodeGen/LowLevelTypeUtils.h" |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 10 | #include "llvm/IR/DataLayout.h" |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 11 | #include "llvm/IR/DerivedTypes.h" |
| 12 | #include "llvm/IR/LLVMContext.h" |
| 13 | #include "llvm/IR/Type.h" |
Sander de Smalen | bd7f7e2 | 2021-06-22 08:05:55 +0100 | [diff] [blame] | 14 | #include "llvm/Support/TypeSize.h" |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | |
Sameer Sahasrabuddhe | 32a067c | 2024-03-15 10:17:50 +0530 | [diff] [blame] | 21 | TEST(LowLevelTypeTest, Token) { |
| 22 | LLVMContext C; |
Sameer Sahasrabuddhe | 32a067c | 2024-03-15 10:17:50 +0530 | [diff] [blame] | 23 | |
| 24 | const LLT TTy = LLT::token(); |
| 25 | |
| 26 | // Test kind. |
| 27 | EXPECT_TRUE(TTy.isValid()); |
| 28 | EXPECT_TRUE(TTy.isScalar()); |
| 29 | EXPECT_TRUE(TTy.isToken()); |
| 30 | |
| 31 | EXPECT_FALSE(TTy.isPointer()); |
| 32 | EXPECT_FALSE(TTy.isVector()); |
| 33 | |
| 34 | const LLT STy = LLT::scalar(0); |
| 35 | EXPECT_EQ(STy, TTy); |
| 36 | } |
| 37 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 38 | TEST(LowLevelTypeTest, Scalar) { |
| 39 | LLVMContext C; |
Sergei Barannikov | 6cf3e7d | 2024-08-14 15:02:47 +0300 | [diff] [blame] | 40 | DataLayout DL; |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 41 | |
Paulo Matos | 842e718 | 2021-07-16 10:34:44 +0200 | [diff] [blame] | 42 | for (unsigned S : {0U, 1U, 17U, 32U, 64U, 0xfffffU}) { |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 43 | const LLT Ty = LLT::scalar(S); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 44 | |
| 45 | // Test kind. |
Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 46 | ASSERT_TRUE(Ty.isValid()); |
| 47 | ASSERT_TRUE(Ty.isScalar()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 48 | |
Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 49 | ASSERT_FALSE(Ty.isPointer()); |
| 50 | ASSERT_FALSE(Ty.isVector()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 51 | |
Sameer Sahasrabuddhe | 32a067c | 2024-03-15 10:17:50 +0530 | [diff] [blame] | 52 | EXPECT_TRUE(S != 0 || Ty.isToken()); |
| 53 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 54 | // Test sizes. |
| 55 | EXPECT_EQ(S, Ty.getSizeInBits()); |
| 56 | EXPECT_EQ(S, Ty.getScalarSizeInBits()); |
| 57 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 58 | // Test equality operators. |
| 59 | EXPECT_TRUE(Ty == Ty); |
Ahmed Bougacha | 9d95557 | 2016-07-29 16:11:04 +0000 | [diff] [blame] | 60 | EXPECT_FALSE(Ty != Ty); |
| 61 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 62 | // Test Type->LLT conversion. |
Paulo Matos | 842e718 | 2021-07-16 10:34:44 +0200 | [diff] [blame] | 63 | if (S != 0) { |
| 64 | Type *IRTy = IntegerType::get(C, S); |
| 65 | EXPECT_EQ(Ty, getLLTForType(*IRTy, DL)); |
| 66 | } |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | TEST(LowLevelTypeTest, Vector) { |
| 71 | LLVMContext C; |
Sergei Barannikov | 6cf3e7d | 2024-08-14 15:02:47 +0300 | [diff] [blame] | 72 | DataLayout DL; |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 73 | |
Paulo Matos | 842e718 | 2021-07-16 10:34:44 +0200 | [diff] [blame] | 74 | for (unsigned S : {0U, 1U, 17U, 32U, 64U, 0xfffU}) { |
Sander de Smalen | bd7f7e2 | 2021-06-22 08:05:55 +0100 | [diff] [blame] | 75 | for (auto EC : |
| 76 | {ElementCount::getFixed(2), ElementCount::getFixed(3), |
| 77 | ElementCount::getFixed(4), ElementCount::getFixed(32), |
| 78 | ElementCount::getFixed(0xff), ElementCount::getScalable(2), |
| 79 | ElementCount::getScalable(3), ElementCount::getScalable(4), |
| 80 | ElementCount::getScalable(32), ElementCount::getScalable(0xff)}) { |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 81 | const LLT STy = LLT::scalar(S); |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 82 | const LLT VTy = LLT::vector(EC, S); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 83 | |
| 84 | // Test the alternative vector(). |
| 85 | { |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 86 | const LLT VSTy = LLT::vector(EC, STy); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 87 | EXPECT_EQ(VTy, VSTy); |
| 88 | } |
| 89 | |
| 90 | // Test getElementType(). |
| 91 | EXPECT_EQ(STy, VTy.getElementType()); |
| 92 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 93 | // Test kind. |
Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 94 | ASSERT_TRUE(VTy.isValid()); |
| 95 | ASSERT_TRUE(VTy.isVector()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 96 | |
Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 97 | ASSERT_FALSE(VTy.isScalar()); |
| 98 | ASSERT_FALSE(VTy.isPointer()); |
Sameer Sahasrabuddhe | 32a067c | 2024-03-15 10:17:50 +0530 | [diff] [blame] | 99 | ASSERT_FALSE(VTy.isToken()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 100 | |
| 101 | // Test sizes. |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 102 | EXPECT_EQ(S, VTy.getScalarSizeInBits()); |
Sander de Smalen | bd7f7e2 | 2021-06-22 08:05:55 +0100 | [diff] [blame] | 103 | EXPECT_EQ(EC, VTy.getElementCount()); |
| 104 | if (!EC.isScalable()) |
| 105 | EXPECT_EQ(S * EC.getFixedValue(), VTy.getSizeInBits()); |
Sander de Smalen | 0e09d18 | 2021-06-27 16:07:19 +0100 | [diff] [blame] | 106 | else |
Sander de Smalen | 81b7f11 | 2023-11-22 08:52:53 +0000 | [diff] [blame] | 107 | EXPECT_EQ(TypeSize::getScalable(S * EC.getKnownMinValue()), |
Sander de Smalen | 0e09d18 | 2021-06-27 16:07:19 +0100 | [diff] [blame] | 108 | VTy.getSizeInBits()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 109 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 110 | // Test equality operators. |
| 111 | EXPECT_TRUE(VTy == VTy); |
Ahmed Bougacha | 9d95557 | 2016-07-29 16:11:04 +0000 | [diff] [blame] | 112 | EXPECT_FALSE(VTy != VTy); |
| 113 | |
| 114 | // Test inequality operators on.. |
| 115 | // ..different kind. |
| 116 | EXPECT_NE(VTy, STy); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 117 | |
| 118 | // Test Type->LLT conversion. |
Paulo Matos | 842e718 | 2021-07-16 10:34:44 +0200 | [diff] [blame] | 119 | if (S != 0) { |
| 120 | Type *IRSTy = IntegerType::get(C, S); |
| 121 | Type *IRTy = VectorType::get(IRSTy, EC); |
| 122 | EXPECT_EQ(VTy, getLLTForType(*IRTy, DL)); |
| 123 | } |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
Matt Arsenault | 7ba2d82 | 2019-01-25 00:10:49 +0000 | [diff] [blame] | 128 | TEST(LowLevelTypeTest, ScalarOrVector) { |
| 129 | // Test version with number of bits for scalar type. |
Sander de Smalen | 968980e | 2021-06-25 08:25:41 +0100 | [diff] [blame] | 130 | EXPECT_EQ(LLT::scalar(32), |
| 131 | LLT::scalarOrVector(ElementCount::getFixed(1), 32)); |
| 132 | EXPECT_EQ(LLT::fixed_vector(2, 32), |
| 133 | LLT::scalarOrVector(ElementCount::getFixed(2), 32)); |
| 134 | EXPECT_EQ(LLT::scalable_vector(1, 32), |
| 135 | LLT::scalarOrVector(ElementCount::getScalable(1), 32)); |
Matt Arsenault | 7ba2d82 | 2019-01-25 00:10:49 +0000 | [diff] [blame] | 136 | |
| 137 | // Test version with LLT for scalar type. |
Sander de Smalen | 968980e | 2021-06-25 08:25:41 +0100 | [diff] [blame] | 138 | EXPECT_EQ(LLT::scalar(32), |
| 139 | LLT::scalarOrVector(ElementCount::getFixed(1), LLT::scalar(32))); |
| 140 | EXPECT_EQ(LLT::fixed_vector(2, 32), |
| 141 | LLT::scalarOrVector(ElementCount::getFixed(2), LLT::scalar(32))); |
Matt Arsenault | 7ba2d82 | 2019-01-25 00:10:49 +0000 | [diff] [blame] | 142 | |
| 143 | // Test with pointer elements. |
Sander de Smalen | 968980e | 2021-06-25 08:25:41 +0100 | [diff] [blame] | 144 | EXPECT_EQ(LLT::pointer(1, 32), LLT::scalarOrVector(ElementCount::getFixed(1), |
| 145 | LLT::pointer(1, 32))); |
| 146 | EXPECT_EQ( |
| 147 | LLT::fixed_vector(2, LLT::pointer(1, 32)), |
| 148 | LLT::scalarOrVector(ElementCount::getFixed(2), LLT::pointer(1, 32))); |
Matt Arsenault | 7ba2d82 | 2019-01-25 00:10:49 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Matt Arsenault | 91be65b | 2019-02-07 17:25:51 +0000 | [diff] [blame] | 151 | TEST(LowLevelTypeTest, ChangeElementType) { |
| 152 | const LLT P0 = LLT::pointer(0, 32); |
| 153 | const LLT P1 = LLT::pointer(1, 64); |
| 154 | |
| 155 | const LLT S32 = LLT::scalar(32); |
| 156 | const LLT S64 = LLT::scalar(64); |
| 157 | |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 158 | const LLT V2S32 = LLT::fixed_vector(2, 32); |
| 159 | const LLT V2S64 = LLT::fixed_vector(2, 64); |
Matt Arsenault | 91be65b | 2019-02-07 17:25:51 +0000 | [diff] [blame] | 160 | |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 161 | const LLT V2P0 = LLT::fixed_vector(2, P0); |
| 162 | const LLT V2P1 = LLT::fixed_vector(2, P1); |
Matt Arsenault | 91be65b | 2019-02-07 17:25:51 +0000 | [diff] [blame] | 163 | |
| 164 | EXPECT_EQ(S64, S32.changeElementType(S64)); |
| 165 | EXPECT_EQ(S32, S32.changeElementType(S32)); |
| 166 | |
| 167 | EXPECT_EQ(S32, S64.changeElementSize(32)); |
| 168 | EXPECT_EQ(S32, S32.changeElementSize(32)); |
| 169 | |
| 170 | EXPECT_EQ(V2S64, V2S32.changeElementType(S64)); |
| 171 | EXPECT_EQ(V2S32, V2S64.changeElementType(S32)); |
| 172 | |
| 173 | EXPECT_EQ(V2S64, V2S32.changeElementSize(64)); |
| 174 | EXPECT_EQ(V2S32, V2S64.changeElementSize(32)); |
| 175 | |
| 176 | EXPECT_EQ(P0, S32.changeElementType(P0)); |
| 177 | EXPECT_EQ(S32, P0.changeElementType(S32)); |
| 178 | |
| 179 | EXPECT_EQ(V2P1, V2P0.changeElementType(P1)); |
| 180 | EXPECT_EQ(V2S32, V2P0.changeElementType(S32)); |
Sander de Smalen | bd7f7e2 | 2021-06-22 08:05:55 +0100 | [diff] [blame] | 181 | |
Fangrui Song | 111fcb0 | 2023-09-01 18:25:16 -0700 | [diff] [blame] | 182 | // Similar tests for scalable vectors. |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 183 | const LLT NXV2S32 = LLT::scalable_vector(2, 32); |
| 184 | const LLT NXV2S64 = LLT::scalable_vector(2, 64); |
Sander de Smalen | bd7f7e2 | 2021-06-22 08:05:55 +0100 | [diff] [blame] | 185 | |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 186 | const LLT NXV2P0 = LLT::scalable_vector(2, P0); |
| 187 | const LLT NXV2P1 = LLT::scalable_vector(2, P1); |
Sander de Smalen | bd7f7e2 | 2021-06-22 08:05:55 +0100 | [diff] [blame] | 188 | |
| 189 | EXPECT_EQ(NXV2S64, NXV2S32.changeElementType(S64)); |
| 190 | EXPECT_EQ(NXV2S32, NXV2S64.changeElementType(S32)); |
| 191 | |
| 192 | EXPECT_EQ(NXV2S64, NXV2S32.changeElementSize(64)); |
| 193 | EXPECT_EQ(NXV2S32, NXV2S64.changeElementSize(32)); |
| 194 | |
| 195 | EXPECT_EQ(NXV2P1, NXV2P0.changeElementType(P1)); |
| 196 | EXPECT_EQ(NXV2S32, NXV2P0.changeElementType(S32)); |
Matt Arsenault | 91be65b | 2019-02-07 17:25:51 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Matt Arsenault | 24ab761 | 2020-01-28 13:14:06 -0500 | [diff] [blame] | 199 | TEST(LowLevelTypeTest, ChangeNumElements) { |
| 200 | const LLT P0 = LLT::pointer(0, 32); |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 201 | const LLT V2P0 = LLT::fixed_vector(2, P0); |
| 202 | const LLT V3P0 = LLT::fixed_vector(3, P0); |
Matt Arsenault | 24ab761 | 2020-01-28 13:14:06 -0500 | [diff] [blame] | 203 | |
| 204 | const LLT S64 = LLT::scalar(64); |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 205 | const LLT V2S64 = LLT::fixed_vector(2, 64); |
| 206 | const LLT V3S64 = LLT::fixed_vector(3, 64); |
Matt Arsenault | 24ab761 | 2020-01-28 13:14:06 -0500 | [diff] [blame] | 207 | |
| 208 | // Vector to scalar |
Sander de Smalen | c9acd2f | 2021-06-25 11:27:41 +0100 | [diff] [blame] | 209 | EXPECT_EQ(S64, V2S64.changeElementCount(ElementCount::getFixed(1))); |
Matt Arsenault | 24ab761 | 2020-01-28 13:14:06 -0500 | [diff] [blame] | 210 | |
| 211 | // Vector to vector |
Sander de Smalen | c9acd2f | 2021-06-25 11:27:41 +0100 | [diff] [blame] | 212 | EXPECT_EQ(V3S64, V2S64.changeElementCount(ElementCount::getFixed(3))); |
Matt Arsenault | 24ab761 | 2020-01-28 13:14:06 -0500 | [diff] [blame] | 213 | |
| 214 | // Scalar to vector |
Sander de Smalen | c9acd2f | 2021-06-25 11:27:41 +0100 | [diff] [blame] | 215 | EXPECT_EQ(V2S64, S64.changeElementCount(ElementCount::getFixed(2))); |
Matt Arsenault | 24ab761 | 2020-01-28 13:14:06 -0500 | [diff] [blame] | 216 | |
Sander de Smalen | c9acd2f | 2021-06-25 11:27:41 +0100 | [diff] [blame] | 217 | EXPECT_EQ(P0, V2P0.changeElementCount(ElementCount::getFixed(1))); |
| 218 | EXPECT_EQ(V3P0, V2P0.changeElementCount(ElementCount::getFixed(3))); |
| 219 | EXPECT_EQ(V2P0, P0.changeElementCount(ElementCount::getFixed(2))); |
| 220 | |
| 221 | const LLT NXV2S64 = LLT::scalable_vector(2, 64); |
| 222 | const LLT NXV3S64 = LLT::scalable_vector(3, 64); |
| 223 | const LLT NXV2P0 = LLT::scalable_vector(2, P0); |
| 224 | |
| 225 | // Scalable vector to scalar |
| 226 | EXPECT_EQ(S64, NXV2S64.changeElementCount(ElementCount::getFixed(1))); |
| 227 | EXPECT_EQ(P0, NXV2P0.changeElementCount(ElementCount::getFixed(1))); |
| 228 | |
| 229 | // Fixed-width vector to scalable vector |
| 230 | EXPECT_EQ(NXV3S64, V2S64.changeElementCount(ElementCount::getScalable(3))); |
| 231 | |
| 232 | // Scalable vector to fixed-width vector |
| 233 | EXPECT_EQ(V3P0, NXV2P0.changeElementCount(ElementCount::getFixed(3))); |
| 234 | |
| 235 | // Scalar to scalable vector |
| 236 | EXPECT_EQ(NXV2S64, S64.changeElementCount(ElementCount::getScalable(2))); |
| 237 | EXPECT_EQ(NXV2P0, P0.changeElementCount(ElementCount::getScalable(2))); |
Matt Arsenault | 24ab761 | 2020-01-28 13:14:06 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Matt Arsenault | 91be65b | 2019-02-07 17:25:51 +0000 | [diff] [blame] | 240 | #ifdef GTEST_HAS_DEATH_TEST |
| 241 | #ifndef NDEBUG |
| 242 | |
| 243 | // Invalid to directly change the element size for pointers. |
| 244 | TEST(LowLevelTypeTest, ChangeElementTypeDeath) { |
| 245 | const LLT P0 = LLT::pointer(0, 32); |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 246 | const LLT V2P0 = LLT::fixed_vector(2, P0); |
Matt Arsenault | 91be65b | 2019-02-07 17:25:51 +0000 | [diff] [blame] | 247 | |
| 248 | EXPECT_DEATH(P0.changeElementSize(64), |
| 249 | "invalid to directly change element size for pointers"); |
| 250 | EXPECT_DEATH(V2P0.changeElementSize(64), |
| 251 | "invalid to directly change element size for pointers"); |
| 252 | |
| 253 | // Make sure this still fails even without a change in size. |
| 254 | EXPECT_DEATH(P0.changeElementSize(32), |
| 255 | "invalid to directly change element size for pointers"); |
| 256 | EXPECT_DEATH(V2P0.changeElementSize(32), |
| 257 | "invalid to directly change element size for pointers"); |
| 258 | } |
| 259 | |
| 260 | #endif |
| 261 | #endif |
| 262 | |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 263 | TEST(LowLevelTypeTest, Pointer) { |
| 264 | LLVMContext C; |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 265 | DataLayout DL("p64:64:64-p127:512:512:512-p16777215:65528:8"); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 266 | |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 267 | for (unsigned AS : {0U, 1U, 127U, 0xffffU, |
| 268 | static_cast<unsigned>(maxUIntN(23)), |
| 269 | static_cast<unsigned>(maxUIntN(24))}) { |
Sander de Smalen | bd7f7e2 | 2021-06-22 08:05:55 +0100 | [diff] [blame] | 270 | for (ElementCount EC : |
| 271 | {ElementCount::getFixed(2), ElementCount::getFixed(3), |
| 272 | ElementCount::getFixed(4), ElementCount::getFixed(256), |
| 273 | ElementCount::getFixed(65535), ElementCount::getScalable(2), |
| 274 | ElementCount::getScalable(3), ElementCount::getScalable(4), |
| 275 | ElementCount::getScalable(256), ElementCount::getScalable(65535)}) { |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 276 | const LLT Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS)); |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 277 | const LLT VTy = LLT::vector(EC, Ty); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 278 | |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 279 | // Test kind. |
| 280 | ASSERT_TRUE(Ty.isValid()); |
| 281 | ASSERT_TRUE(Ty.isPointer()); |
Jay Foad | d57515bd | 2024-02-13 08:21:35 +0000 | [diff] [blame] | 282 | ASSERT_TRUE(Ty.isPointerOrPointerVector()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 283 | |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 284 | ASSERT_FALSE(Ty.isScalar()); |
| 285 | ASSERT_FALSE(Ty.isVector()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 286 | |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 287 | ASSERT_TRUE(VTy.isValid()); |
| 288 | ASSERT_TRUE(VTy.isVector()); |
| 289 | ASSERT_TRUE(VTy.getElementType().isPointer()); |
Jay Foad | d57515bd | 2024-02-13 08:21:35 +0000 | [diff] [blame] | 290 | ASSERT_TRUE(VTy.isPointerVector()); |
| 291 | ASSERT_TRUE(VTy.isPointerOrPointerVector()); |
Kristof Beyls | 0f36e68 | 2017-04-19 07:23:57 +0000 | [diff] [blame] | 292 | |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 293 | EXPECT_EQ(Ty, VTy.getElementType()); |
| 294 | EXPECT_EQ(Ty.getSizeInBits(), VTy.getScalarSizeInBits()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 295 | |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 296 | // Test address space. |
| 297 | EXPECT_EQ(AS, Ty.getAddressSpace()); |
| 298 | EXPECT_EQ(AS, VTy.getElementType().getAddressSpace()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 299 | |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 300 | // Test equality operators. |
| 301 | EXPECT_TRUE(Ty == Ty); |
| 302 | EXPECT_FALSE(Ty != Ty); |
| 303 | EXPECT_TRUE(VTy == VTy); |
| 304 | EXPECT_FALSE(VTy != VTy); |
| 305 | |
| 306 | // Test Type->LLT conversion. |
Mats Jun Larsen | 97d691b | 2025-01-21 17:40:18 +0900 | [diff] [blame] | 307 | Type *IRTy = PointerType::get(C, AS); |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 308 | EXPECT_EQ(Ty, getLLTForType(*IRTy, DL)); |
Mats Jun Larsen | 97d691b | 2025-01-21 17:40:18 +0900 | [diff] [blame] | 309 | Type *IRVTy = VectorType::get(PointerType::get(C, AS), EC); |
Matt Arsenault | cdc201f | 2019-01-26 01:42:13 +0000 | [diff] [blame] | 310 | EXPECT_EQ(VTy, getLLTForType(*IRVTy, DL)); |
| 311 | } |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | TEST(LowLevelTypeTest, Invalid) { |
| 316 | const LLT Ty; |
| 317 | |
| 318 | ASSERT_FALSE(Ty.isValid()); |
| 319 | ASSERT_FALSE(Ty.isScalar()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 320 | ASSERT_FALSE(Ty.isPointer()); |
| 321 | ASSERT_FALSE(Ty.isVector()); |
Sameer Sahasrabuddhe | 32a067c | 2024-03-15 10:17:50 +0530 | [diff] [blame] | 322 | ASSERT_FALSE(Ty.isToken()); |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Matt Arsenault | f48fe2c | 2020-04-11 13:57:15 -0400 | [diff] [blame] | 325 | TEST(LowLevelTypeTest, Divide) { |
| 326 | // Test basic scalar->scalar cases. |
| 327 | EXPECT_EQ(LLT::scalar(16), LLT::scalar(32).divide(2)); |
| 328 | EXPECT_EQ(LLT::scalar(8), LLT::scalar(32).divide(4)); |
| 329 | EXPECT_EQ(LLT::scalar(8), LLT::scalar(32).divide(4)); |
| 330 | |
| 331 | // Test pointer->scalar |
| 332 | EXPECT_EQ(LLT::scalar(32), LLT::pointer(0, 64).divide(2)); |
| 333 | |
| 334 | // Test dividing vectors. |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 335 | EXPECT_EQ(LLT::scalar(32), LLT::fixed_vector(2, 32).divide(2)); |
| 336 | EXPECT_EQ(LLT::fixed_vector(2, 32), LLT::fixed_vector(4, 32).divide(2)); |
Matt Arsenault | f48fe2c | 2020-04-11 13:57:15 -0400 | [diff] [blame] | 337 | |
| 338 | // Test vector of pointers |
| 339 | EXPECT_EQ(LLT::pointer(1, 64), |
Sander de Smalen | d5e14ba | 2021-06-24 09:58:21 +0100 | [diff] [blame] | 340 | LLT::fixed_vector(4, LLT::pointer(1, 64)).divide(4)); |
| 341 | EXPECT_EQ(LLT::fixed_vector(2, LLT::pointer(1, 64)), |
| 342 | LLT::fixed_vector(4, LLT::pointer(1, 64)).divide(2)); |
Matt Arsenault | f48fe2c | 2020-04-11 13:57:15 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Matt Arsenault | 12d79b1 | 2022-04-09 10:45:31 -0400 | [diff] [blame] | 345 | TEST(LowLevelTypeTest, MultiplyElements) { |
| 346 | // Basic scalar->vector cases |
| 347 | EXPECT_EQ(LLT::fixed_vector(2, 16), LLT::scalar(16).multiplyElements(2)); |
| 348 | EXPECT_EQ(LLT::fixed_vector(3, 16), LLT::scalar(16).multiplyElements(3)); |
| 349 | EXPECT_EQ(LLT::fixed_vector(4, 32), LLT::scalar(32).multiplyElements(4)); |
| 350 | EXPECT_EQ(LLT::fixed_vector(4, 7), LLT::scalar(7).multiplyElements(4)); |
| 351 | |
| 352 | // Basic vector to vector cases |
| 353 | EXPECT_EQ(LLT::fixed_vector(4, 32), |
| 354 | LLT::fixed_vector(2, 32).multiplyElements(2)); |
| 355 | EXPECT_EQ(LLT::fixed_vector(9, 32), |
| 356 | LLT::fixed_vector(3, 32).multiplyElements(3)); |
| 357 | |
| 358 | // Pointer to vector of pointers |
| 359 | EXPECT_EQ(LLT::fixed_vector(2, LLT::pointer(0, 32)), |
| 360 | LLT::pointer(0, 32).multiplyElements(2)); |
| 361 | EXPECT_EQ(LLT::fixed_vector(3, LLT::pointer(1, 32)), |
| 362 | LLT::pointer(1, 32).multiplyElements(3)); |
| 363 | EXPECT_EQ(LLT::fixed_vector(4, LLT::pointer(1, 64)), |
| 364 | LLT::pointer(1, 64).multiplyElements(4)); |
| 365 | |
| 366 | // Vector of pointers to vector of pointers |
| 367 | EXPECT_EQ(LLT::fixed_vector(8, LLT::pointer(1, 64)), |
| 368 | LLT::fixed_vector(2, LLT::pointer(1, 64)).multiplyElements(4)); |
| 369 | EXPECT_EQ(LLT::fixed_vector(9, LLT::pointer(1, 32)), |
| 370 | LLT::fixed_vector(3, LLT::pointer(1, 32)).multiplyElements(3)); |
| 371 | |
| 372 | // Scalable vectors |
| 373 | EXPECT_EQ(LLT::scalable_vector(4, 16), |
| 374 | LLT::scalable_vector(2, 16).multiplyElements(2)); |
| 375 | EXPECT_EQ(LLT::scalable_vector(6, 16), |
| 376 | LLT::scalable_vector(2, 16).multiplyElements(3)); |
| 377 | EXPECT_EQ(LLT::scalable_vector(9, 16), |
| 378 | LLT::scalable_vector(3, 16).multiplyElements(3)); |
| 379 | EXPECT_EQ(LLT::scalable_vector(4, 32), |
| 380 | LLT::scalable_vector(2, 32).multiplyElements(2)); |
| 381 | EXPECT_EQ(LLT::scalable_vector(256, 32), |
| 382 | LLT::scalable_vector(8, 32).multiplyElements(32)); |
| 383 | |
| 384 | // Scalable vectors of pointers |
| 385 | EXPECT_EQ(LLT::scalable_vector(4, LLT::pointer(0, 32)), |
| 386 | LLT::scalable_vector(2, LLT::pointer(0, 32)).multiplyElements(2)); |
| 387 | EXPECT_EQ(LLT::scalable_vector(32, LLT::pointer(1, 64)), |
| 388 | LLT::scalable_vector(8, LLT::pointer(1, 64)).multiplyElements(4)); |
| 389 | } |
Matt Arsenault | ecfed0a | 2022-12-18 19:29:59 -0500 | [diff] [blame] | 390 | |
| 391 | constexpr LLT CELLT = LLT(); |
| 392 | constexpr LLT CES32 = LLT::scalar(32); |
| 393 | constexpr LLT CEV2S32 = LLT::fixed_vector(2, 32); |
| 394 | constexpr LLT CESV2S32 = LLT::scalable_vector(2, 32); |
| 395 | constexpr LLT CEP0 = LLT::pointer(0, 32); |
| 396 | constexpr LLT CEV2P1 = LLT::fixed_vector(2, LLT::pointer(1, 64)); |
| 397 | |
| 398 | static_assert(!CELLT.isValid()); |
| 399 | static_assert(CES32.isValid()); |
| 400 | static_assert(CEV2S32.isValid()); |
| 401 | static_assert(CESV2S32.isValid()); |
| 402 | static_assert(CEP0.isValid()); |
| 403 | static_assert(CEV2P1.isValid()); |
| 404 | static_assert(CEV2P1.isVector()); |
| 405 | static_assert(CEV2P1.getElementCount() == ElementCount::getFixed(2)); |
| 406 | static_assert(CEV2P1.getElementCount() != ElementCount::getFixed(1)); |
| 407 | static_assert(CEV2S32.getElementCount() == ElementCount::getFixed(2)); |
Sander de Smalen | 81b7f11 | 2023-11-22 08:52:53 +0000 | [diff] [blame] | 408 | static_assert(CEV2S32.getSizeInBits() == TypeSize::getFixed(64)); |
| 409 | static_assert(CEV2P1.getSizeInBits() == TypeSize::getFixed(128)); |
Matt Arsenault | ecfed0a | 2022-12-18 19:29:59 -0500 | [diff] [blame] | 410 | static_assert(CEV2P1.getScalarType() == LLT::pointer(1, 64)); |
| 411 | static_assert(CES32.getScalarType() == CES32); |
| 412 | static_assert(CEV2S32.getScalarType() == CES32); |
| 413 | static_assert(CEV2S32.changeElementType(CEP0) == LLT::fixed_vector(2, CEP0)); |
| 414 | static_assert(CEV2S32.changeElementSize(16) == LLT::fixed_vector(2, 16)); |
| 415 | static_assert(CEV2S32.changeElementCount(ElementCount::getFixed(4)) == |
| 416 | LLT::fixed_vector(4, 32)); |
| 417 | static_assert(CES32.isByteSized()); |
| 418 | static_assert(!LLT::scalar(7).isByteSized()); |
| 419 | static_assert(CES32.getScalarSizeInBits() == 32); |
| 420 | static_assert(CEP0.getAddressSpace() == 0); |
| 421 | static_assert(LLT::pointer(1, 64).getAddressSpace() == 1); |
| 422 | static_assert(CEV2S32.multiplyElements(2) == LLT::fixed_vector(4, 32)); |
| 423 | static_assert(CEV2S32.divide(2) == LLT::scalar(32)); |
| 424 | static_assert(LLT::scalarOrVector(ElementCount::getFixed(1), LLT::scalar(32)) == |
| 425 | LLT::scalar(32)); |
| 426 | static_assert(LLT::scalarOrVector(ElementCount::getFixed(2), LLT::scalar(32)) == |
| 427 | LLT::fixed_vector(2, 32)); |
| 428 | static_assert(LLT::scalarOrVector(ElementCount::getFixed(2), CEP0) == |
| 429 | LLT::fixed_vector(2, CEP0)); |
| 430 | |
| 431 | TEST(LowLevelTypeTest, ConstExpr) { |
| 432 | EXPECT_EQ(LLT(), CELLT); |
| 433 | EXPECT_EQ(LLT::scalar(32), CES32); |
| 434 | EXPECT_EQ(LLT::fixed_vector(2, 32), CEV2S32); |
| 435 | EXPECT_EQ(LLT::pointer(0, 32), CEP0); |
| 436 | EXPECT_EQ(LLT::scalable_vector(2, 32), CESV2S32); |
| 437 | } |
Michael Maitland | bede010 | 2023-11-09 14:31:38 -0500 | [diff] [blame] | 438 | |
| 439 | TEST(LowLevelTypeTest, IsFixedVector) { |
| 440 | EXPECT_FALSE(LLT::scalar(32).isFixedVector()); |
| 441 | EXPECT_TRUE(LLT::fixed_vector(2, 32).isFixedVector()); |
| 442 | EXPECT_FALSE(LLT::scalable_vector(2, 32).isFixedVector()); |
| 443 | EXPECT_FALSE(LLT::scalable_vector(1, 32).isFixedVector()); |
| 444 | } |
| 445 | |
| 446 | TEST(LowLevelTypeTest, IsScalableVector) { |
| 447 | EXPECT_FALSE(LLT::scalar(32).isScalableVector()); |
| 448 | EXPECT_FALSE(LLT::fixed_vector(2, 32).isScalableVector()); |
| 449 | EXPECT_TRUE(LLT::scalable_vector(2, 32).isScalableVector()); |
| 450 | EXPECT_TRUE(LLT::scalable_vector(1, 32).isScalableVector()); |
| 451 | } |
Ahmed Bougacha | 9f986bf | 2016-07-29 16:10:57 +0000 | [diff] [blame] | 452 | } |