1 //===- VectorUtilsTest.cpp - VectorUtils 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/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"
27 class VectorUtilsTest
: public testing::Test
{
29 void parseAssembly(const char *Assembly
) {
31 M
= parseAssemblyString(Assembly
, Error
, Context
);
34 raw_string_ostream
os(errMsg
);
37 // A failure here means that the test itself is buggy.
39 report_fatal_error(Twine(os
.str()));
41 Function
*F
= M
->getFunction("test");
43 report_fatal_error("Test must have a function named @test");
46 for (inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
) {
48 if (I
->getName() == "A")
53 report_fatal_error("@test must have an instruction %A");
57 std::unique_ptr
<Module
> M
;
61 struct BasicTest
: public testing::Test
{
63 std::unique_ptr
<Module
> M
;
66 IRBuilder
<NoFolder
> IRB
;
69 : M(new Module("VectorUtils", Ctx
)),
71 FunctionType::get(Type::getVoidTy(Ctx
), /* IsVarArg */ false),
72 Function::ExternalLinkage
, "f", M
.get())),
73 BB(BasicBlock::Create(Ctx
, "entry", F
)), IRB(BB
) {}
79 TEST_F(BasicTest
, isSplat
) {
80 Value
*UndefVec
= UndefValue::get(FixedVectorType::get(IRB
.getInt8Ty(), 4));
81 EXPECT_TRUE(isSplatValue(UndefVec
));
83 Constant
*UndefScalar
= UndefValue::get(IRB
.getInt8Ty());
84 EXPECT_FALSE(isSplatValue(UndefScalar
));
86 Constant
*ScalarC
= IRB
.getInt8(42);
87 EXPECT_FALSE(isSplatValue(ScalarC
));
89 Constant
*OtherScalarC
= IRB
.getInt8(-42);
90 Constant
*NonSplatC
= ConstantVector::get({ScalarC
, OtherScalarC
});
91 EXPECT_FALSE(isSplatValue(NonSplatC
));
93 Value
*SplatC
= IRB
.CreateVectorSplat(5, ScalarC
);
94 EXPECT_TRUE(isSplatValue(SplatC
));
97 IRB
.CreateVectorSplat(ElementCount::getScalable(5), ScalarC
);
98 EXPECT_TRUE(isSplatValue(SplatC_SVE
));
100 // FIXME: Constant splat analysis does not allow undef elements.
101 Constant
*SplatWithUndefC
= ConstantVector::get({ScalarC
, UndefScalar
});
102 EXPECT_FALSE(isSplatValue(SplatWithUndefC
));
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}));
113 TEST_F(BasicTest
, widenShuffleMaskElts
) {
114 SmallVector
<int, 16> WideMask
;
115 SmallVector
<int, 16> NarrowMask
;
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}));
121 // back to original mask
122 narrowShuffleMaskElts(1, makeArrayRef(WideMask
), NarrowMask
);
123 EXPECT_EQ(makeArrayRef(NarrowMask
), makeArrayRef({3,2,0,-1}));
125 // can't widen non-consecutive 3/2
126 EXPECT_FALSE(widenShuffleMaskElts(2, {3,2,0,-1}, WideMask
));
128 // can't widen if not evenly divisible
129 EXPECT_FALSE(widenShuffleMaskElts(2, {0,1,2}, WideMask
));
131 // can always widen identity to single element
132 EXPECT_TRUE(widenShuffleMaskElts(3, {0,1,2}, WideMask
));
133 EXPECT_EQ(makeArrayRef(WideMask
), makeArrayRef({0}));
135 // back to original mask
136 narrowShuffleMaskElts(3, makeArrayRef(WideMask
), NarrowMask
);
137 EXPECT_EQ(makeArrayRef(NarrowMask
), makeArrayRef({0,1,2}));
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}));
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}));
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
));
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}));
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}));
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
));
161 // negative indexes must match across a wide element
162 EXPECT_FALSE(widenShuffleMaskElts(2, {-1,-2,-1,-1}, WideMask
));
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}));
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
180 TEST_F(VectorUtilsTest
, isSplatValue_00
) {
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"
186 EXPECT_TRUE(isSplatValue(A
));
189 TEST_F(VectorUtilsTest
, isSplatValue_00_index0
) {
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"
195 EXPECT_TRUE(isSplatValue(A
, 0));
198 TEST_F(VectorUtilsTest
, isSplatValue_00_index1
) {
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"
204 EXPECT_FALSE(isSplatValue(A
, 1));
207 TEST_F(VectorUtilsTest
, isSplatValue_11
) {
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"
213 EXPECT_TRUE(isSplatValue(A
));
216 TEST_F(VectorUtilsTest
, isSplatValue_11_index0
) {
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"
222 EXPECT_FALSE(isSplatValue(A
, 0));
225 TEST_F(VectorUtilsTest
, isSplatValue_11_index1
) {
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"
231 EXPECT_TRUE(isSplatValue(A
, 1));
234 TEST_F(VectorUtilsTest
, isSplatValue_01
) {
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"
240 EXPECT_FALSE(isSplatValue(A
));
243 TEST_F(VectorUtilsTest
, isSplatValue_01_index0
) {
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"
249 EXPECT_FALSE(isSplatValue(A
, 0));
252 TEST_F(VectorUtilsTest
, isSplatValue_01_index1
) {
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"
258 EXPECT_FALSE(isSplatValue(A
, 1));
261 // FIXME: Allow undef matching with Constant (mask) splat analysis.
263 TEST_F(VectorUtilsTest
, isSplatValue_0u
) {
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"
269 EXPECT_FALSE(isSplatValue(A
));
272 // FIXME: Allow undef matching with Constant (mask) splat analysis.
274 TEST_F(VectorUtilsTest
, isSplatValue_0u_index0
) {
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"
280 EXPECT_FALSE(isSplatValue(A
, 0));
283 TEST_F(VectorUtilsTest
, isSplatValue_0u_index1
) {
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"
289 EXPECT_FALSE(isSplatValue(A
, 1));
292 TEST_F(VectorUtilsTest
, isSplatValue_Binop
) {
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"
300 EXPECT_TRUE(isSplatValue(A
));
303 TEST_F(VectorUtilsTest
, isSplatValue_Binop_index0
) {
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"
311 EXPECT_FALSE(isSplatValue(A
, 0));
314 TEST_F(VectorUtilsTest
, isSplatValue_Binop_index1
) {
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"
322 EXPECT_FALSE(isSplatValue(A
, 1));
325 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0
) {
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"
332 EXPECT_TRUE(isSplatValue(A
));
335 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0_index0
) {
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"
342 EXPECT_FALSE(isSplatValue(A
, 0));
345 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0_index1
) {
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"
352 EXPECT_TRUE(isSplatValue(A
, 1));
355 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op0
) {
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"
363 EXPECT_FALSE(isSplatValue(A
));
366 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op1
) {
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"
374 EXPECT_FALSE(isSplatValue(A
));
377 TEST_F(VectorUtilsTest
, isSplatValue_Select
) {
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"
386 EXPECT_TRUE(isSplatValue(A
));
389 TEST_F(VectorUtilsTest
, isSplatValue_Select_ConstantOp
) {
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"
397 EXPECT_TRUE(isSplatValue(A
));
400 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotCond
) {
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"
408 EXPECT_FALSE(isSplatValue(A
));
411 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp1
) {
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"
419 EXPECT_FALSE(isSplatValue(A
));
422 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp2
) {
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"
430 EXPECT_FALSE(isSplatValue(A
));
433 TEST_F(VectorUtilsTest
, isSplatValue_SelectBinop
) {
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"
443 EXPECT_TRUE(isSplatValue(A
));
446 TEST_F(VectorUtilsTest
, getSplatValueElt0
) {
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"
453 EXPECT_EQ(getSplatValue(A
)->getName(), "x");
456 TEST_F(VectorUtilsTest
, getSplatValueEltMismatch
) {
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"
463 EXPECT_EQ(getSplatValue(A
), nullptr);
466 // TODO: This is a splat, but we don't recognize it.
468 TEST_F(VectorUtilsTest
, getSplatValueElt1
) {
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"
475 EXPECT_EQ(getSplatValue(A
), nullptr);
478 ////////////////////////////////////////////////////////////////////////////////
479 // VFShape API tests.
480 ////////////////////////////////////////////////////////////////////////////////
482 class VFShapeAPITest
: public testing::Test
{
484 void SetUp() override
{
485 M
= parseAssemblyString(IR
, Err
, Ctx
);
486 // Get the only call instruction in the block, which is the first
488 CI
= dyn_cast
<CallInst
>(&*(instructions(M
->getFunction("f")).begin()));
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"
495 "declare i32 @g(i32, i64, double)\n";
498 std::unique_ptr
<Module
> M
;
500 // Dummy shape with no parameters, overwritten by buildShape when invoked.
501 VFShape Shape
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {}};
503 SmallVector
<VFParameter
, 8> &ExpectedParams
= Expected
.Parameters
;
505 void buildShape(ElementCount VF
, bool HasGlobalPred
) {
506 Shape
= VFShape::get(*CI
, VF
, HasGlobalPred
);
509 bool validParams(ArrayRef
<VFParameter
> Parameters
) {
511 SmallVector
<VFParameter
, 8>(Parameters
.begin(), Parameters
.end());
512 return Shape
.hasValidParameterList();
516 TEST_F(VFShapeAPITest
, API_buildVFShape
) {
517 buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
518 Expected
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
519 {0, VFParamKind::Vector
},
520 {1, VFParamKind::Vector
},
521 {2, VFParamKind::Vector
},
523 EXPECT_EQ(Shape
, Expected
);
525 buildShape(/*VF*/ ElementCount::getFixed(4), /*HasGlobalPred*/ true);
526 Expected
= {/*VF*/ ElementCount::getFixed(4), /*Parameters*/ {
527 {0, VFParamKind::Vector
},
528 {1, VFParamKind::Vector
},
529 {2, VFParamKind::Vector
},
530 {3, VFParamKind::GlobalPredicate
},
532 EXPECT_EQ(Shape
, Expected
);
534 buildShape(/*VF*/ ElementCount::getScalable(16), /*HasGlobalPred*/ false);
535 Expected
= {/*VF*/ ElementCount::getScalable(16), /*Parameters*/ {
536 {0, VFParamKind::Vector
},
537 {1, VFParamKind::Vector
},
538 {2, VFParamKind::Vector
},
540 EXPECT_EQ(Shape
, Expected
);
543 TEST_F(VFShapeAPITest
, API_getScalarShape
) {
544 buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ false);
545 EXPECT_EQ(VFShape::getScalarShape(*CI
), Shape
);
548 TEST_F(VFShapeAPITest
, API_getVectorizedFunction
) {
549 VFShape ScalarShape
= VFShape::getScalarShape(*CI
);
550 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(ScalarShape
),
551 M
->getFunction("g"));
553 buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ false);
554 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
555 buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ true);
556 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
557 buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ true);
558 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
561 TEST_F(VFShapeAPITest
, API_updateVFShape
) {
563 buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
564 Shape
.updateParam({0 /*Pos*/, VFParamKind::OMP_Linear
, 1, Align(4)});
565 Expected
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
566 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
567 {1, VFParamKind::Vector
},
568 {2, VFParamKind::Vector
},
570 EXPECT_EQ(Shape
, Expected
);
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
});
576 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
577 {1, VFParamKind::OMP_Uniform
},
578 {2, VFParamKind::Vector
},
580 EXPECT_EQ(Shape
, Expected
);
582 Shape
.updateParam({2 /*Pos*/, VFParamKind::OMP_LinearRefPos
, 1});
584 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
585 {1, VFParamKind::OMP_Uniform
},
586 {2, VFParamKind::OMP_LinearRefPos
, 1},
588 EXPECT_EQ(Shape
, Expected
);
591 TEST_F(VFShapeAPITest
, API_updateVFShape_GlobalPredicate
) {
593 buildShape(/*VF*/ ElementCount::getScalable(2), /*HasGlobalPred*/ true);
594 Shape
.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform
});
595 Expected
= {/*VF*/ ElementCount::getScalable(2),
596 /*Parameters*/ {{0, VFParamKind::Vector
},
597 {1, VFParamKind::OMP_Uniform
},
598 {2, VFParamKind::Vector
},
599 {3, VFParamKind::GlobalPredicate
}}};
600 EXPECT_EQ(Shape
, Expected
);
603 TEST_F(VFShapeAPITest
, Parameters_Valid
) {
604 // ParamPos in order.
605 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
}}));
607 validParams({{0, VFParamKind::Vector
}, {1, VFParamKind::Vector
}}));
608 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
609 {1, VFParamKind::Vector
},
610 {2, VFParamKind::Vector
}}));
612 // GlocalPredicate is unique.
613 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
614 {1, VFParamKind::Vector
},
615 {2, VFParamKind::Vector
},
616 {3, VFParamKind::GlobalPredicate
}}));
618 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
619 {1, VFParamKind::GlobalPredicate
},
620 {2, VFParamKind::Vector
}}));
623 TEST_F(VFShapeAPITest
, Parameters_ValidOpenMPLinear
) {
624 // Valid linear constant step (>0).
625 #define __BUILD_PARAMETERS(Kind, Val) \
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
635 // Valid linear runtime step (the step parameter is marked uniform).
636 #define __BUILD_PARAMETERS(Kind) \
638 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 0 } \
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
647 TEST_F(VFShapeAPITest
, Parameters_Invalid
) {
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.");
654 validParams({{1, VFParamKind::Vector
}, {0, VFParamKind::Vector
}}),
655 "Broken parameter list.");
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
}}));
667 TEST_F(VFShapeAPITest
, Parameters_InvalidOpenMPLinear
) {
668 // Compile time linear steps must be non-zero (compile time invariant).
669 #define __BUILD_PARAMETERS(Kind) \
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
679 // The step of a runtime linear parameter must be marked
680 // as uniform (runtime invariant).
681 #define __BUILD_PARAMETERS(Kind) \
683 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 1 } \
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
691 // The linear step parameter can't point at itself.
692 #define __BUILD_PARAMETERS(Kind) \
694 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 2 } \
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
702 // Linear parameter (runtime) is out of range.
703 #define __BUILD_PARAMETERS(Kind) \
705 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 3 } \
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