1 //===- VectorUtilsTest.cpp - VectorUtils tests ------------------------===// 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 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Analysis/VectorUtils.h" 10 #include "llvm/Analysis/ValueTracking.h" 11 #include "llvm/AsmParser/Parser.h" 12 #include "llvm/IR/Function.h" 13 #include "llvm/IR/InstIterator.h" 14 #include "llvm/IR/IRBuilder.h" 15 #include "llvm/IR/LLVMContext.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/IR/NoFolder.h" 18 #include "llvm/Support/ErrorHandling.h" 19 #include "llvm/Support/SourceMgr.h" 20 #include "llvm/Support/KnownBits.h" 21 #include "gtest/gtest.h" 22 23 using namespace llvm; 24 25 namespace { 26 27 class VectorUtilsTest : public testing::Test { 28 protected: 29 void parseAssembly(const char *Assembly) { 30 SMDiagnostic Error; 31 M = parseAssemblyString(Assembly, Error, Context); 32 33 std::string errMsg; 34 raw_string_ostream os(errMsg); 35 Error.print("", os); 36 37 // A failure here means that the test itself is buggy. 38 if (!M) 39 report_fatal_error(os.str()); 40 41 Function *F = M->getFunction("test"); 42 if (F == nullptr) 43 report_fatal_error("Test must have a function named @test"); 44 45 A = nullptr; 46 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { 47 if (I->hasName()) { 48 if (I->getName() == "A") 49 A = &*I; 50 } 51 } 52 if (A == nullptr) 53 report_fatal_error("@test must have an instruction %A"); 54 } 55 56 LLVMContext Context; 57 std::unique_ptr<Module> M; 58 Instruction *A; 59 }; 60 61 struct BasicTest : public testing::Test { 62 LLVMContext Ctx; 63 std::unique_ptr<Module> M; 64 Function *F; 65 BasicBlock *BB; 66 IRBuilder<NoFolder> IRB; 67 68 BasicTest() 69 : M(new Module("VectorUtils", Ctx)), 70 F(Function::Create( 71 FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false), 72 Function::ExternalLinkage, "f", M.get())), 73 BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {} 74 }; 75 76 77 } // namespace 78 79 TEST_F(BasicTest, isSplat) { 80 Value *UndefVec = UndefValue::get(FixedVectorType::get(IRB.getInt8Ty(), 4)); 81 EXPECT_TRUE(isSplatValue(UndefVec)); 82 83 Constant *UndefScalar = UndefValue::get(IRB.getInt8Ty()); 84 EXPECT_FALSE(isSplatValue(UndefScalar)); 85 86 Constant *ScalarC = IRB.getInt8(42); 87 EXPECT_FALSE(isSplatValue(ScalarC)); 88 89 Constant *OtherScalarC = IRB.getInt8(-42); 90 Constant *NonSplatC = ConstantVector::get({ScalarC, OtherScalarC}); 91 EXPECT_FALSE(isSplatValue(NonSplatC)); 92 93 Value *SplatC = IRB.CreateVectorSplat(5, ScalarC); 94 EXPECT_TRUE(isSplatValue(SplatC)); 95 96 Value *SplatC_SVE = 97 IRB.CreateVectorSplat(ElementCount::getScalable(5), ScalarC); 98 EXPECT_TRUE(isSplatValue(SplatC_SVE)); 99 100 // FIXME: Constant splat analysis does not allow undef elements. 101 Constant *SplatWithUndefC = ConstantVector::get({ScalarC, UndefScalar}); 102 EXPECT_FALSE(isSplatValue(SplatWithUndefC)); 103 } 104 105 TEST_F(BasicTest, narrowShuffleMaskElts) { 106 SmallVector<int, 16> ScaledMask; 107 narrowShuffleMaskElts(1, {3,2,0,-2}, ScaledMask); 108 EXPECT_EQ(makeArrayRef(ScaledMask), makeArrayRef({3,2,0,-2})); 109 narrowShuffleMaskElts(4, {3,2,0,-1}, ScaledMask); 110 EXPECT_EQ(makeArrayRef(ScaledMask), makeArrayRef({12,13,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1})); 111 } 112 113 TEST_F(BasicTest, widenShuffleMaskElts) { 114 SmallVector<int, 16> WideMask; 115 SmallVector<int, 16> NarrowMask; 116 117 // scale == 1 is a copy 118 EXPECT_TRUE(widenShuffleMaskElts(1, {3,2,0,-1}, WideMask)); 119 EXPECT_EQ(makeArrayRef(WideMask), makeArrayRef({3,2,0,-1})); 120 121 // back to original mask 122 narrowShuffleMaskElts(1, makeArrayRef(WideMask), NarrowMask); 123 EXPECT_EQ(makeArrayRef(NarrowMask), makeArrayRef({3,2,0,-1})); 124 125 // can't widen non-consecutive 3/2 126 EXPECT_FALSE(widenShuffleMaskElts(2, {3,2,0,-1}, WideMask)); 127 128 // can't widen if not evenly divisible 129 EXPECT_FALSE(widenShuffleMaskElts(2, {0,1,2}, WideMask)); 130 131 // can always widen identity to single element 132 EXPECT_TRUE(widenShuffleMaskElts(3, {0,1,2}, WideMask)); 133 EXPECT_EQ(makeArrayRef(WideMask), makeArrayRef({0})); 134 135 // back to original mask 136 narrowShuffleMaskElts(3, makeArrayRef(WideMask), NarrowMask); 137 EXPECT_EQ(makeArrayRef(NarrowMask), makeArrayRef({0,1,2})); 138 139 // groups of 4 must be consecutive/undef 140 EXPECT_TRUE(widenShuffleMaskElts(4, {12,13,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}, WideMask)); 141 EXPECT_EQ(makeArrayRef(WideMask), makeArrayRef({3,2,0,-1})); 142 143 // back to original mask 144 narrowShuffleMaskElts(4, makeArrayRef(WideMask), NarrowMask); 145 EXPECT_EQ(makeArrayRef(NarrowMask), makeArrayRef({12,13,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1})); 146 147 // groups of 2 must be consecutive/undef 148 EXPECT_FALSE(widenShuffleMaskElts(2, {12,12,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}, WideMask)); 149 150 // groups of 3 must be consecutive/undef 151 EXPECT_TRUE(widenShuffleMaskElts(3, {6,7,8,0,1,2,-1,-1,-1}, WideMask)); 152 EXPECT_EQ(makeArrayRef(WideMask), makeArrayRef({2,0,-1})); 153 154 // back to original mask 155 narrowShuffleMaskElts(3, makeArrayRef(WideMask), NarrowMask); 156 EXPECT_EQ(makeArrayRef(NarrowMask), makeArrayRef({6,7,8,0,1,2,-1,-1,-1})); 157 158 // groups of 3 must be consecutive/undef (partial undefs are not ok) 159 EXPECT_FALSE(widenShuffleMaskElts(3, {-1,7,8,0,-1,2,-1,-1,-1}, WideMask)); 160 161 // negative indexes must match across a wide element 162 EXPECT_FALSE(widenShuffleMaskElts(2, {-1,-2,-1,-1}, WideMask)); 163 164 // negative indexes must match across a wide element 165 EXPECT_TRUE(widenShuffleMaskElts(2, {-2,-2,-3,-3}, WideMask)); 166 EXPECT_EQ(makeArrayRef(WideMask), makeArrayRef({-2,-3})); 167 } 168 169 TEST_F(BasicTest, getSplatIndex) { 170 EXPECT_EQ(getSplatIndex({0,0,0}), 0); 171 EXPECT_EQ(getSplatIndex({1,0,0}), -1); // no splat 172 EXPECT_EQ(getSplatIndex({0,1,1}), -1); // no splat 173 EXPECT_EQ(getSplatIndex({42,42,42}), 42); // array size is independent of splat index 174 EXPECT_EQ(getSplatIndex({42,42,-1}), 42); // ignore negative 175 EXPECT_EQ(getSplatIndex({-1,42,-1}), 42); // ignore negatives 176 EXPECT_EQ(getSplatIndex({-4,42,-42}), 42); // ignore all negatives 177 EXPECT_EQ(getSplatIndex({-4,-1,-42}), -1); // all negative values map to -1 178 } 179 180 TEST_F(VectorUtilsTest, isSplatValue_00) { 181 parseAssembly( 182 "define <2 x i8> @test(<2 x i8> %x) {\n" 183 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n" 184 " ret <2 x i8> %A\n" 185 "}\n"); 186 EXPECT_TRUE(isSplatValue(A)); 187 } 188 189 TEST_F(VectorUtilsTest, isSplatValue_00_index0) { 190 parseAssembly( 191 "define <2 x i8> @test(<2 x i8> %x) {\n" 192 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n" 193 " ret <2 x i8> %A\n" 194 "}\n"); 195 EXPECT_TRUE(isSplatValue(A, 0)); 196 } 197 198 TEST_F(VectorUtilsTest, isSplatValue_00_index1) { 199 parseAssembly( 200 "define <2 x i8> @test(<2 x i8> %x) {\n" 201 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n" 202 " ret <2 x i8> %A\n" 203 "}\n"); 204 EXPECT_FALSE(isSplatValue(A, 1)); 205 } 206 207 TEST_F(VectorUtilsTest, isSplatValue_11) { 208 parseAssembly( 209 "define <2 x i8> @test(<2 x i8> %x) {\n" 210 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 211 " ret <2 x i8> %A\n" 212 "}\n"); 213 EXPECT_TRUE(isSplatValue(A)); 214 } 215 216 TEST_F(VectorUtilsTest, isSplatValue_11_index0) { 217 parseAssembly( 218 "define <2 x i8> @test(<2 x i8> %x) {\n" 219 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 220 " ret <2 x i8> %A\n" 221 "}\n"); 222 EXPECT_FALSE(isSplatValue(A, 0)); 223 } 224 225 TEST_F(VectorUtilsTest, isSplatValue_11_index1) { 226 parseAssembly( 227 "define <2 x i8> @test(<2 x i8> %x) {\n" 228 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 229 " ret <2 x i8> %A\n" 230 "}\n"); 231 EXPECT_TRUE(isSplatValue(A, 1)); 232 } 233 234 TEST_F(VectorUtilsTest, isSplatValue_01) { 235 parseAssembly( 236 "define <2 x i8> @test(<2 x i8> %x) {\n" 237 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n" 238 " ret <2 x i8> %A\n" 239 "}\n"); 240 EXPECT_FALSE(isSplatValue(A)); 241 } 242 243 TEST_F(VectorUtilsTest, isSplatValue_01_index0) { 244 parseAssembly( 245 "define <2 x i8> @test(<2 x i8> %x) {\n" 246 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n" 247 " ret <2 x i8> %A\n" 248 "}\n"); 249 EXPECT_FALSE(isSplatValue(A, 0)); 250 } 251 252 TEST_F(VectorUtilsTest, isSplatValue_01_index1) { 253 parseAssembly( 254 "define <2 x i8> @test(<2 x i8> %x) {\n" 255 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n" 256 " ret <2 x i8> %A\n" 257 "}\n"); 258 EXPECT_FALSE(isSplatValue(A, 1)); 259 } 260 261 // FIXME: Allow undef matching with Constant (mask) splat analysis. 262 263 TEST_F(VectorUtilsTest, isSplatValue_0u) { 264 parseAssembly( 265 "define <2 x i8> @test(<2 x i8> %x) {\n" 266 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n" 267 " ret <2 x i8> %A\n" 268 "}\n"); 269 EXPECT_FALSE(isSplatValue(A)); 270 } 271 272 // FIXME: Allow undef matching with Constant (mask) splat analysis. 273 274 TEST_F(VectorUtilsTest, isSplatValue_0u_index0) { 275 parseAssembly( 276 "define <2 x i8> @test(<2 x i8> %x) {\n" 277 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n" 278 " ret <2 x i8> %A\n" 279 "}\n"); 280 EXPECT_FALSE(isSplatValue(A, 0)); 281 } 282 283 TEST_F(VectorUtilsTest, isSplatValue_0u_index1) { 284 parseAssembly( 285 "define <2 x i8> @test(<2 x i8> %x) {\n" 286 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n" 287 " ret <2 x i8> %A\n" 288 "}\n"); 289 EXPECT_FALSE(isSplatValue(A, 1)); 290 } 291 292 TEST_F(VectorUtilsTest, isSplatValue_Binop) { 293 parseAssembly( 294 "define <2 x i8> @test(<2 x i8> %x) {\n" 295 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n" 296 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 297 " %A = udiv <2 x i8> %v0, %v1\n" 298 " ret <2 x i8> %A\n" 299 "}\n"); 300 EXPECT_TRUE(isSplatValue(A)); 301 } 302 303 TEST_F(VectorUtilsTest, isSplatValue_Binop_index0) { 304 parseAssembly( 305 "define <2 x i8> @test(<2 x i8> %x) {\n" 306 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n" 307 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 308 " %A = udiv <2 x i8> %v0, %v1\n" 309 " ret <2 x i8> %A\n" 310 "}\n"); 311 EXPECT_FALSE(isSplatValue(A, 0)); 312 } 313 314 TEST_F(VectorUtilsTest, isSplatValue_Binop_index1) { 315 parseAssembly( 316 "define <2 x i8> @test(<2 x i8> %x) {\n" 317 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n" 318 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 319 " %A = udiv <2 x i8> %v0, %v1\n" 320 " ret <2 x i8> %A\n" 321 "}\n"); 322 EXPECT_FALSE(isSplatValue(A, 1)); 323 } 324 325 TEST_F(VectorUtilsTest, isSplatValue_Binop_ConstantOp0) { 326 parseAssembly( 327 "define <2 x i8> @test(<2 x i8> %x) {\n" 328 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 329 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n" 330 " ret <2 x i8> %A\n" 331 "}\n"); 332 EXPECT_TRUE(isSplatValue(A)); 333 } 334 335 TEST_F(VectorUtilsTest, isSplatValue_Binop_ConstantOp0_index0) { 336 parseAssembly( 337 "define <2 x i8> @test(<2 x i8> %x) {\n" 338 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 339 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n" 340 " ret <2 x i8> %A\n" 341 "}\n"); 342 EXPECT_FALSE(isSplatValue(A, 0)); 343 } 344 345 TEST_F(VectorUtilsTest, isSplatValue_Binop_ConstantOp0_index1) { 346 parseAssembly( 347 "define <2 x i8> @test(<2 x i8> %x) {\n" 348 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 349 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n" 350 " ret <2 x i8> %A\n" 351 "}\n"); 352 EXPECT_TRUE(isSplatValue(A, 1)); 353 } 354 355 TEST_F(VectorUtilsTest, isSplatValue_Binop_Not_Op0) { 356 parseAssembly( 357 "define <2 x i8> @test(<2 x i8> %x) {\n" 358 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 0>\n" 359 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 360 " %A = add <2 x i8> %v0, %v1\n" 361 " ret <2 x i8> %A\n" 362 "}\n"); 363 EXPECT_FALSE(isSplatValue(A)); 364 } 365 366 TEST_F(VectorUtilsTest, isSplatValue_Binop_Not_Op1) { 367 parseAssembly( 368 "define <2 x i8> @test(<2 x i8> %x) {\n" 369 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 370 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n" 371 " %A = shl <2 x i8> %v0, %v1\n" 372 " ret <2 x i8> %A\n" 373 "}\n"); 374 EXPECT_FALSE(isSplatValue(A)); 375 } 376 377 TEST_F(VectorUtilsTest, isSplatValue_Select) { 378 parseAssembly( 379 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n" 380 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n" 381 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n" 382 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 383 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %v2\n" 384 " ret <2 x i8> %A\n" 385 "}\n"); 386 EXPECT_TRUE(isSplatValue(A)); 387 } 388 389 TEST_F(VectorUtilsTest, isSplatValue_Select_ConstantOp) { 390 parseAssembly( 391 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n" 392 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n" 393 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 394 " %A = select <2 x i1> %v0, <2 x i8> <i8 42, i8 42>, <2 x i8> %v2\n" 395 " ret <2 x i8> %A\n" 396 "}\n"); 397 EXPECT_TRUE(isSplatValue(A)); 398 } 399 400 TEST_F(VectorUtilsTest, isSplatValue_Select_NotCond) { 401 parseAssembly( 402 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n" 403 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n" 404 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 405 " %A = select <2 x i1> %x, <2 x i8> %v1, <2 x i8> %v2\n" 406 " ret <2 x i8> %A\n" 407 "}\n"); 408 EXPECT_FALSE(isSplatValue(A)); 409 } 410 411 TEST_F(VectorUtilsTest, isSplatValue_Select_NotOp1) { 412 parseAssembly( 413 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n" 414 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n" 415 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 416 " %A = select <2 x i1> %v0, <2 x i8> %y, <2 x i8> %v2\n" 417 " ret <2 x i8> %A\n" 418 "}\n"); 419 EXPECT_FALSE(isSplatValue(A)); 420 } 421 422 TEST_F(VectorUtilsTest, isSplatValue_Select_NotOp2) { 423 parseAssembly( 424 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n" 425 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n" 426 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n" 427 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %z\n" 428 " ret <2 x i8> %A\n" 429 "}\n"); 430 EXPECT_FALSE(isSplatValue(A)); 431 } 432 433 TEST_F(VectorUtilsTest, isSplatValue_SelectBinop) { 434 parseAssembly( 435 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n" 436 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n" 437 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n" 438 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 439 " %bo = xor <2 x i8> %v1, %v2\n" 440 " %A = select <2 x i1> %v0, <2 x i8> %bo, <2 x i8> %v2\n" 441 " ret <2 x i8> %A\n" 442 "}\n"); 443 EXPECT_TRUE(isSplatValue(A)); 444 } 445 446 TEST_F(VectorUtilsTest, getSplatValueElt0) { 447 parseAssembly( 448 "define <2 x i8> @test(i8 %x) {\n" 449 " %ins = insertelement <2 x i8> undef, i8 %x, i32 0\n" 450 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n" 451 " ret <2 x i8> %A\n" 452 "}\n"); 453 EXPECT_EQ(getSplatValue(A)->getName(), "x"); 454 } 455 456 TEST_F(VectorUtilsTest, getSplatValueEltMismatch) { 457 parseAssembly( 458 "define <2 x i8> @test(i8 %x) {\n" 459 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n" 460 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n" 461 " ret <2 x i8> %A\n" 462 "}\n"); 463 EXPECT_EQ(getSplatValue(A), nullptr); 464 } 465 466 // TODO: This is a splat, but we don't recognize it. 467 468 TEST_F(VectorUtilsTest, getSplatValueElt1) { 469 parseAssembly( 470 "define <2 x i8> @test(i8 %x) {\n" 471 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n" 472 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n" 473 " ret <2 x i8> %A\n" 474 "}\n"); 475 EXPECT_EQ(getSplatValue(A), nullptr); 476 } 477 478 //////////////////////////////////////////////////////////////////////////////// 479 // VFShape API tests. 480 //////////////////////////////////////////////////////////////////////////////// 481 482 class VFShapeAPITest : public testing::Test { 483 protected: 484 void SetUp() override { 485 M = parseAssemblyString(IR, Err, Ctx); 486 // Get the only call instruction in the block, which is the first 487 // instruction. 488 CI = dyn_cast<CallInst>(&*(instructions(M->getFunction("f")).begin())); 489 } 490 491 const char *IR = "define i32 @f(i32 %a, i64 %b, double %c) {\n" 492 " %1 = call i32 @g(i32 %a, i64 %b, double %c)\n" 493 " ret i32 %1\n" 494 "}\n" 495 "declare i32 @g(i32, i64, double)\n"; 496 LLVMContext Ctx; 497 SMDiagnostic Err; 498 std::unique_ptr<Module> M; 499 CallInst *CI; 500 // Dummy shape with no parameters, overwritten by buildShape when invoked. 501 VFShape Shape = {/*VF*/ 2, /*IsScalable*/ false, /*Parameters*/ {}}; 502 VFShape Expected; 503 SmallVector<VFParameter, 8> &ExpectedParams = Expected.Parameters; 504 505 void buildShape(unsigned VF, bool IsScalable, bool HasGlobalPred) { 506 Shape = VFShape::get(*CI, ElementCount::get(VF, IsScalable), HasGlobalPred); 507 } 508 509 bool validParams(ArrayRef<VFParameter> Parameters) { 510 Shape.Parameters = 511 SmallVector<VFParameter, 8>(Parameters.begin(), Parameters.end()); 512 return Shape.hasValidParameterList(); 513 } 514 }; 515 516 TEST_F(VFShapeAPITest, API_buildVFShape) { 517 buildShape(/*VF*/ 2, /*IsScalable*/ false, /*HasGlobalPred*/ false); 518 Expected = {/*VF*/ 2, /*IsScalable*/ false, /*Parameters*/ { 519 {0, VFParamKind::Vector}, 520 {1, VFParamKind::Vector}, 521 {2, VFParamKind::Vector}, 522 }}; 523 EXPECT_EQ(Shape, Expected); 524 525 buildShape(/*VF*/ 4, /*IsScalable*/ false, /*HasGlobalPred*/ true); 526 Expected = {/*VF*/ 4, /*IsScalable*/ false, /*Parameters*/ { 527 {0, VFParamKind::Vector}, 528 {1, VFParamKind::Vector}, 529 {2, VFParamKind::Vector}, 530 {3, VFParamKind::GlobalPredicate}, 531 }}; 532 EXPECT_EQ(Shape, Expected); 533 534 buildShape(/*VF*/ 16, /*IsScalable*/ true, /*HasGlobalPred*/ false); 535 Expected = {/*VF*/ 16, /*IsScalable*/ true, /*Parameters*/ { 536 {0, VFParamKind::Vector}, 537 {1, VFParamKind::Vector}, 538 {2, VFParamKind::Vector}, 539 }}; 540 EXPECT_EQ(Shape, Expected); 541 } 542 543 TEST_F(VFShapeAPITest, API_getScalarShape) { 544 buildShape(/*VF*/ 1, /*IsScalable*/ false, /*HasGlobalPred*/ false); 545 EXPECT_EQ(VFShape::getScalarShape(*CI), Shape); 546 } 547 548 TEST_F(VFShapeAPITest, API_getVectorizedFunction) { 549 VFShape ScalarShape = VFShape::getScalarShape(*CI); 550 EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(ScalarShape), 551 M->getFunction("g")); 552 553 buildShape(/*VF*/ 1, /*IsScalable*/ true, /*HasGlobalPred*/ false); 554 EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(Shape), nullptr); 555 buildShape(/*VF*/ 1, /*IsScalable*/ false, /*HasGlobalPred*/ true); 556 EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(Shape), nullptr); 557 buildShape(/*VF*/ 1, /*IsScalable*/ true, /*HasGlobalPred*/ true); 558 EXPECT_EQ(VFDatabase(*CI).getVectorizedFunction(Shape), nullptr); 559 } 560 561 TEST_F(VFShapeAPITest, API_updateVFShape) { 562 563 buildShape(/*VF*/ 2, /*IsScalable*/ false, /*HasGlobalPred*/ false); 564 Shape.updateParam({0 /*Pos*/, VFParamKind::OMP_Linear, 1, Align(4)}); 565 Expected = {/*VF*/ 2, /*IsScalable*/ false, /*Parameters*/ { 566 {0, VFParamKind::OMP_Linear, 1, Align(4)}, 567 {1, VFParamKind::Vector}, 568 {2, VFParamKind::Vector}, 569 }}; 570 EXPECT_EQ(Shape, Expected); 571 572 // From this point on, we update only the parameters of the VFShape, 573 // so we update only the reference of the expected Parameters. 574 Shape.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform}); 575 ExpectedParams = { 576 {0, VFParamKind::OMP_Linear, 1, Align(4)}, 577 {1, VFParamKind::OMP_Uniform}, 578 {2, VFParamKind::Vector}, 579 }; 580 EXPECT_EQ(Shape, Expected); 581 582 Shape.updateParam({2 /*Pos*/, VFParamKind::OMP_LinearRefPos, 1}); 583 ExpectedParams = { 584 {0, VFParamKind::OMP_Linear, 1, Align(4)}, 585 {1, VFParamKind::OMP_Uniform}, 586 {2, VFParamKind::OMP_LinearRefPos, 1}, 587 }; 588 EXPECT_EQ(Shape, Expected); 589 } 590 591 TEST_F(VFShapeAPITest, API_updateVFShape_GlobalPredicate) { 592 593 buildShape(/*VF*/ 2, /*IsScalable*/ true, /*HasGlobalPred*/ true); 594 Shape.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform}); 595 Expected = {/*VF*/ 2, /*IsScalable*/ true, 596 /*Parameters*/ {{0, VFParamKind::Vector}, 597 {1, VFParamKind::OMP_Uniform}, 598 {2, VFParamKind::Vector}, 599 {3, VFParamKind::GlobalPredicate}}}; 600 EXPECT_EQ(Shape, Expected); 601 } 602 603 TEST_F(VFShapeAPITest, Parameters_Valid) { 604 // ParamPos in order. 605 EXPECT_TRUE(validParams({{0, VFParamKind::Vector}})); 606 EXPECT_TRUE( 607 validParams({{0, VFParamKind::Vector}, {1, VFParamKind::Vector}})); 608 EXPECT_TRUE(validParams({{0, VFParamKind::Vector}, 609 {1, VFParamKind::Vector}, 610 {2, VFParamKind::Vector}})); 611 612 // GlocalPredicate is unique. 613 EXPECT_TRUE(validParams({{0, VFParamKind::Vector}, 614 {1, VFParamKind::Vector}, 615 {2, VFParamKind::Vector}, 616 {3, VFParamKind::GlobalPredicate}})); 617 618 EXPECT_TRUE(validParams({{0, VFParamKind::Vector}, 619 {1, VFParamKind::GlobalPredicate}, 620 {2, VFParamKind::Vector}})); 621 } 622 623 TEST_F(VFShapeAPITest, Parameters_ValidOpenMPLinear) { 624 // Valid linear constant step (>0). 625 #define __BUILD_PARAMETERS(Kind, Val) \ 626 { \ 627 { 0, Kind, Val } \ 628 } 629 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear, 1))); 630 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef, 2))); 631 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal, 4))); 632 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal, 33))); 633 #undef __BUILD_PARAMETERS 634 635 // Valid linear runtime step (the step parameter is marked uniform). 636 #define __BUILD_PARAMETERS(Kind) \ 637 { \ 638 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 0 } \ 639 } 640 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos))); 641 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos))); 642 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos))); 643 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos))); 644 #undef __BUILD_PARAMETERS 645 } 646 647 TEST_F(VFShapeAPITest, Parameters_Invalid) { 648 #ifndef NDEBUG 649 // Wrong order is checked by an assertion: make sure that the 650 // assertion is not removed. 651 EXPECT_DEATH(validParams({{1, VFParamKind::Vector}}), 652 "Broken parameter list."); 653 EXPECT_DEATH( 654 validParams({{1, VFParamKind::Vector}, {0, VFParamKind::Vector}}), 655 "Broken parameter list."); 656 #endif 657 658 // GlobalPredicate is not unique 659 EXPECT_FALSE(validParams({{0, VFParamKind::Vector}, 660 {1, VFParamKind::GlobalPredicate}, 661 {2, VFParamKind::GlobalPredicate}})); 662 EXPECT_FALSE(validParams({{0, VFParamKind::GlobalPredicate}, 663 {1, VFParamKind::Vector}, 664 {2, VFParamKind::GlobalPredicate}})); 665 } 666 667 TEST_F(VFShapeAPITest, Parameters_InvalidOpenMPLinear) { 668 // Compile time linear steps must be non-zero (compile time invariant). 669 #define __BUILD_PARAMETERS(Kind) \ 670 { \ 671 { 0, Kind, 0 } \ 672 } 673 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear))); 674 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef))); 675 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal))); 676 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal))); 677 #undef __BUILD_PARAMETERS 678 679 // The step of a runtime linear parameter must be marked 680 // as uniform (runtime invariant). 681 #define __BUILD_PARAMETERS(Kind) \ 682 { \ 683 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 1 } \ 684 } 685 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos))); 686 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos))); 687 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos))); 688 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos))); 689 #undef __BUILD_PARAMETERS 690 691 // The linear step parameter can't point at itself. 692 #define __BUILD_PARAMETERS(Kind) \ 693 { \ 694 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 2 } \ 695 } 696 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos))); 697 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos))); 698 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos))); 699 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos))); 700 #undef __BUILD_PARAMETERS 701 702 // Linear parameter (runtime) is out of range. 703 #define __BUILD_PARAMETERS(Kind) \ 704 { \ 705 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 3 } \ 706 } 707 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos))); 708 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos))); 709 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos))); 710 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos))); 711 #undef __BUILD_PARAMETERS 712 } 713