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 "gtest/gtest.h"
26 class VectorUtilsTest
: public testing::Test
{
28 void parseAssembly(const char *Assembly
) {
30 M
= parseAssemblyString(Assembly
, Error
, Context
);
33 raw_string_ostream
os(errMsg
);
36 // A failure here means that the test itself is buggy.
38 report_fatal_error(Twine(errMsg
));
40 Function
*F
= M
->getFunction("test");
42 report_fatal_error("Test must have a function named @test");
45 for (inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
) {
47 if (I
->getName() == "A")
52 report_fatal_error("@test must have an instruction %A");
56 std::unique_ptr
<Module
> M
;
60 struct BasicTest
: public testing::Test
{
62 std::unique_ptr
<Module
> M
;
65 IRBuilder
<NoFolder
> IRB
;
68 : M(new Module("VectorUtils", Ctx
)),
70 FunctionType::get(Type::getVoidTy(Ctx
), /* IsVarArg */ false),
71 Function::ExternalLinkage
, "f", M
.get())),
72 BB(BasicBlock::Create(Ctx
, "entry", F
)), IRB(BB
) {}
78 TEST_F(BasicTest
, isSplat
) {
79 Value
*UndefVec
= UndefValue::get(FixedVectorType::get(IRB
.getInt8Ty(), 4));
80 EXPECT_TRUE(isSplatValue(UndefVec
));
82 Constant
*UndefScalar
= UndefValue::get(IRB
.getInt8Ty());
83 EXPECT_FALSE(isSplatValue(UndefScalar
));
85 Constant
*ScalarC
= IRB
.getInt8(42);
86 EXPECT_FALSE(isSplatValue(ScalarC
));
88 Constant
*OtherScalarC
= IRB
.getInt8(-42);
89 Constant
*NonSplatC
= ConstantVector::get({ScalarC
, OtherScalarC
});
90 EXPECT_FALSE(isSplatValue(NonSplatC
));
92 Value
*SplatC
= IRB
.CreateVectorSplat(5, ScalarC
);
93 EXPECT_TRUE(isSplatValue(SplatC
));
96 IRB
.CreateVectorSplat(ElementCount::getScalable(5), ScalarC
);
97 EXPECT_TRUE(isSplatValue(SplatC_SVE
));
99 // FIXME: Constant splat analysis does not allow undef elements.
100 Constant
*SplatWithUndefC
= ConstantVector::get({ScalarC
, UndefScalar
});
101 EXPECT_FALSE(isSplatValue(SplatWithUndefC
));
104 TEST_F(BasicTest
, narrowShuffleMaskElts
) {
105 SmallVector
<int, 16> ScaledMask
;
106 narrowShuffleMaskElts(1, {3,2,0,-2}, ScaledMask
);
107 EXPECT_EQ(ArrayRef(ScaledMask
), ArrayRef({3, 2, 0, -2}));
108 narrowShuffleMaskElts(4, {3,2,0,-1}, ScaledMask
);
109 EXPECT_EQ(ArrayRef(ScaledMask
), ArrayRef({12, 13, 14, 15, 8, 9, 10, 11, 0, 1,
110 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(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
121 // back to original mask
122 narrowShuffleMaskElts(1, ArrayRef(WideMask
), NarrowMask
);
123 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({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(ArrayRef(WideMask
), ArrayRef({0}));
135 // back to original mask
136 narrowShuffleMaskElts(3, ArrayRef(WideMask
), NarrowMask
);
137 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({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(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
143 // back to original mask
144 narrowShuffleMaskElts(4, ArrayRef(WideMask
), NarrowMask
);
145 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({12, 13, 14, 15, 8, 9, 10, 11, 0, 1,
146 2, 3, -1, -1, -1, -1}));
148 // groups of 2 must be consecutive/undef
149 EXPECT_FALSE(widenShuffleMaskElts(2, {12,12,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}, WideMask
));
151 // groups of 3 must be consecutive/undef
152 EXPECT_TRUE(widenShuffleMaskElts(3, {6,7,8,0,1,2,-1,-1,-1}, WideMask
));
153 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({2, 0, -1}));
155 // back to original mask
156 narrowShuffleMaskElts(3, ArrayRef(WideMask
), NarrowMask
);
157 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({6, 7, 8, 0, 1, 2, -1, -1, -1}));
159 // groups of 3 must be consecutive/undef (partial undefs are not ok)
160 EXPECT_FALSE(widenShuffleMaskElts(3, {-1,7,8,0,-1,2,-1,-1,-1}, WideMask
));
162 // negative indexes must match across a wide element
163 EXPECT_FALSE(widenShuffleMaskElts(2, {-1,-2,-1,-1}, WideMask
));
165 // negative indexes must match across a wide element
166 EXPECT_TRUE(widenShuffleMaskElts(2, {-2,-2,-3,-3}, WideMask
));
167 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-2, -3}));
170 TEST_F(BasicTest
, getShuffleMaskWithWidestElts
) {
171 SmallVector
<int, 16> WideMask
;
173 // can not widen anything here.
174 getShuffleMaskWithWidestElts({3, 2, 0, -1}, WideMask
);
175 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
177 // can't widen non-consecutive 3/2
178 getShuffleMaskWithWidestElts({3, 2, 0, -1}, WideMask
);
179 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
181 // can always widen identity to single element
182 getShuffleMaskWithWidestElts({0, 1, 2}, WideMask
);
183 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({0}));
185 // groups of 4 must be consecutive/undef
186 getShuffleMaskWithWidestElts(
187 {12, 13, 14, 15, 8, 9, 10, 11, 0, 1, 2, 3, -1, -1, -1, -1}, WideMask
);
188 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
190 // groups of 2 must be consecutive/undef
191 getShuffleMaskWithWidestElts(
192 {12, 12, 14, 15, 8, 9, 10, 11, 0, 1, 2, 3, -1, -1, -1, -1}, WideMask
);
193 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({12, 12, 14, 15, 8, 9, 10, 11, 0, 1, 2,
194 3, -1, -1, -1, -1}));
196 // groups of 3 must be consecutive/undef
197 getShuffleMaskWithWidestElts({6, 7, 8, 0, 1, 2, -1, -1, -1}, WideMask
);
198 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({2, 0, -1}));
200 // groups of 3 must be consecutive/undef (partial undefs are not ok)
201 getShuffleMaskWithWidestElts({-1, 7, 8, 0, -1, 2, -1, -1, -1}, WideMask
);
202 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-1, 7, 8, 0, -1, 2, -1, -1, -1}));
204 // negative indexes must match across a wide element
205 getShuffleMaskWithWidestElts({-1, -2, -1, -1}, WideMask
);
206 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-1, -2, -1, -1}));
208 // negative indexes must match across a wide element
209 getShuffleMaskWithWidestElts({-2, -2, -3, -3}, WideMask
);
210 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-2, -3}));
213 TEST_F(BasicTest
, getShuffleDemandedElts
) {
217 EXPECT_TRUE(getShuffleDemandedElts(4, {0, 0, 0, 0}, APInt(4,0xf), LHS
, RHS
));
218 EXPECT_EQ(LHS
.getZExtValue(), 0x1U
);
219 EXPECT_EQ(RHS
.getZExtValue(), 0x0U
);
221 // broadcast zero (with non-permitted undefs)
222 EXPECT_FALSE(getShuffleDemandedElts(2, {0, -1}, APInt(2, 0x3), LHS
, RHS
));
224 // broadcast zero (with permitted undefs)
225 EXPECT_TRUE(getShuffleDemandedElts(3, {0, 0, -1}, APInt(3, 0x7), LHS
, RHS
, true));
226 EXPECT_EQ(LHS
.getZExtValue(), 0x1U
);
227 EXPECT_EQ(RHS
.getZExtValue(), 0x0U
);
229 // broadcast one in demanded
230 EXPECT_TRUE(getShuffleDemandedElts(4, {1, 1, 1, -1}, APInt(4, 0x7), LHS
, RHS
));
231 EXPECT_EQ(LHS
.getZExtValue(), 0x2U
);
232 EXPECT_EQ(RHS
.getZExtValue(), 0x0U
);
234 // broadcast 7 in demanded
235 EXPECT_TRUE(getShuffleDemandedElts(4, {7, 0, 7, 7}, APInt(4, 0xd), LHS
, RHS
));
236 EXPECT_EQ(LHS
.getZExtValue(), 0x0U
);
237 EXPECT_EQ(RHS
.getZExtValue(), 0x8U
);
240 EXPECT_TRUE(getShuffleDemandedElts(4, {4, 2, 7, 3}, APInt(4, 0xf), LHS
, RHS
));
241 EXPECT_EQ(LHS
.getZExtValue(), 0xcU
);
242 EXPECT_EQ(RHS
.getZExtValue(), 0x9U
);
245 TEST_F(BasicTest
, getHorizontalDemandedEltsForFirstOperand
) {
248 getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b0000), LHS
, RHS
);
249 EXPECT_EQ(LHS
.getZExtValue(), 0b0000U
);
250 EXPECT_EQ(RHS
.getZExtValue(), 0b0000U
);
252 getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b0001), LHS
, RHS
);
253 EXPECT_EQ(LHS
.getZExtValue(), 0b0001U
);
254 EXPECT_EQ(RHS
.getZExtValue(), 0b0000U
);
256 getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b1000), LHS
, RHS
);
257 EXPECT_EQ(LHS
.getZExtValue(), 0b0000U
);
258 EXPECT_EQ(RHS
.getZExtValue(), 0b0100U
);
260 getHorizDemandedEltsForFirstOperand(128, APInt(4, 0b0110), LHS
, RHS
);
261 EXPECT_EQ(LHS
.getZExtValue(), 0b0100U
);
262 EXPECT_EQ(RHS
.getZExtValue(), 0b0001U
);
264 getHorizDemandedEltsForFirstOperand(256, APInt(4, 0b0100), LHS
, RHS
);
265 EXPECT_EQ(LHS
.getZExtValue(), 0b0100U
);
266 EXPECT_EQ(RHS
.getZExtValue(), 0b0000U
);
269 TEST_F(BasicTest
, getSplatIndex
) {
270 EXPECT_EQ(getSplatIndex({0,0,0}), 0);
271 EXPECT_EQ(getSplatIndex({1,0,0}), -1); // no splat
272 EXPECT_EQ(getSplatIndex({0,1,1}), -1); // no splat
273 EXPECT_EQ(getSplatIndex({42,42,42}), 42); // array size is independent of splat index
274 EXPECT_EQ(getSplatIndex({42,42,-1}), 42); // ignore negative
275 EXPECT_EQ(getSplatIndex({-1,42,-1}), 42); // ignore negatives
276 EXPECT_EQ(getSplatIndex({-4,42,-42}), 42); // ignore all negatives
277 EXPECT_EQ(getSplatIndex({-4,-1,-42}), -1); // all negative values map to -1
280 TEST_F(VectorUtilsTest
, isSplatValue_00
) {
282 "define <2 x i8> @test(<2 x i8> %x) {\n"
283 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
286 EXPECT_TRUE(isSplatValue(A
));
289 TEST_F(VectorUtilsTest
, isSplatValue_00_index0
) {
291 "define <2 x i8> @test(<2 x i8> %x) {\n"
292 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
295 EXPECT_TRUE(isSplatValue(A
, 0));
298 TEST_F(VectorUtilsTest
, isSplatValue_00_index1
) {
300 "define <2 x i8> @test(<2 x i8> %x) {\n"
301 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
304 EXPECT_FALSE(isSplatValue(A
, 1));
307 TEST_F(VectorUtilsTest
, isSplatValue_11
) {
309 "define <2 x i8> @test(<2 x i8> %x) {\n"
310 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
313 EXPECT_TRUE(isSplatValue(A
));
316 TEST_F(VectorUtilsTest
, isSplatValue_11_index0
) {
318 "define <2 x i8> @test(<2 x i8> %x) {\n"
319 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
322 EXPECT_FALSE(isSplatValue(A
, 0));
325 TEST_F(VectorUtilsTest
, isSplatValue_11_index1
) {
327 "define <2 x i8> @test(<2 x i8> %x) {\n"
328 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
331 EXPECT_TRUE(isSplatValue(A
, 1));
334 TEST_F(VectorUtilsTest
, isSplatValue_01
) {
336 "define <2 x i8> @test(<2 x i8> %x) {\n"
337 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
340 EXPECT_FALSE(isSplatValue(A
));
343 TEST_F(VectorUtilsTest
, isSplatValue_01_index0
) {
345 "define <2 x i8> @test(<2 x i8> %x) {\n"
346 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
349 EXPECT_FALSE(isSplatValue(A
, 0));
352 TEST_F(VectorUtilsTest
, isSplatValue_01_index1
) {
354 "define <2 x i8> @test(<2 x i8> %x) {\n"
355 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
358 EXPECT_FALSE(isSplatValue(A
, 1));
361 // FIXME: Allow undef matching with Constant (mask) splat analysis.
363 TEST_F(VectorUtilsTest
, isSplatValue_0u
) {
365 "define <2 x i8> @test(<2 x i8> %x) {\n"
366 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
369 EXPECT_FALSE(isSplatValue(A
));
372 // FIXME: Allow undef matching with Constant (mask) splat analysis.
374 TEST_F(VectorUtilsTest
, isSplatValue_0u_index0
) {
376 "define <2 x i8> @test(<2 x i8> %x) {\n"
377 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
380 EXPECT_FALSE(isSplatValue(A
, 0));
383 TEST_F(VectorUtilsTest
, isSplatValue_0u_index1
) {
385 "define <2 x i8> @test(<2 x i8> %x) {\n"
386 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
389 EXPECT_FALSE(isSplatValue(A
, 1));
392 TEST_F(VectorUtilsTest
, isSplatValue_Binop
) {
394 "define <2 x i8> @test(<2 x i8> %x) {\n"
395 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
396 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
397 " %A = udiv <2 x i8> %v0, %v1\n"
400 EXPECT_TRUE(isSplatValue(A
));
403 TEST_F(VectorUtilsTest
, isSplatValue_Binop_index0
) {
405 "define <2 x i8> @test(<2 x i8> %x) {\n"
406 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
407 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
408 " %A = udiv <2 x i8> %v0, %v1\n"
411 EXPECT_FALSE(isSplatValue(A
, 0));
414 TEST_F(VectorUtilsTest
, isSplatValue_Binop_index1
) {
416 "define <2 x i8> @test(<2 x i8> %x) {\n"
417 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
418 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
419 " %A = udiv <2 x i8> %v0, %v1\n"
422 EXPECT_FALSE(isSplatValue(A
, 1));
425 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0
) {
427 "define <2 x i8> @test(<2 x i8> %x) {\n"
428 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
429 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
432 EXPECT_TRUE(isSplatValue(A
));
435 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0_index0
) {
437 "define <2 x i8> @test(<2 x i8> %x) {\n"
438 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
439 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
442 EXPECT_FALSE(isSplatValue(A
, 0));
445 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0_index1
) {
447 "define <2 x i8> @test(<2 x i8> %x) {\n"
448 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
449 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
452 EXPECT_TRUE(isSplatValue(A
, 1));
455 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op0
) {
457 "define <2 x i8> @test(<2 x i8> %x) {\n"
458 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 0>\n"
459 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
460 " %A = add <2 x i8> %v0, %v1\n"
463 EXPECT_FALSE(isSplatValue(A
));
466 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op1
) {
468 "define <2 x i8> @test(<2 x i8> %x) {\n"
469 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
470 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
471 " %A = shl <2 x i8> %v0, %v1\n"
474 EXPECT_FALSE(isSplatValue(A
));
477 TEST_F(VectorUtilsTest
, isSplatValue_Select
) {
479 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
480 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
481 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
482 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
483 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %v2\n"
486 EXPECT_TRUE(isSplatValue(A
));
489 TEST_F(VectorUtilsTest
, isSplatValue_Select_ConstantOp
) {
491 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
492 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
493 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
494 " %A = select <2 x i1> %v0, <2 x i8> <i8 42, i8 42>, <2 x i8> %v2\n"
497 EXPECT_TRUE(isSplatValue(A
));
500 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotCond
) {
502 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
503 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
504 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
505 " %A = select <2 x i1> %x, <2 x i8> %v1, <2 x i8> %v2\n"
508 EXPECT_FALSE(isSplatValue(A
));
511 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp1
) {
513 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
514 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
515 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
516 " %A = select <2 x i1> %v0, <2 x i8> %y, <2 x i8> %v2\n"
519 EXPECT_FALSE(isSplatValue(A
));
522 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp2
) {
524 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
525 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
526 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
527 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %z\n"
530 EXPECT_FALSE(isSplatValue(A
));
533 TEST_F(VectorUtilsTest
, isSplatValue_SelectBinop
) {
535 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
536 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
537 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
538 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
539 " %bo = xor <2 x i8> %v1, %v2\n"
540 " %A = select <2 x i1> %v0, <2 x i8> %bo, <2 x i8> %v2\n"
543 EXPECT_TRUE(isSplatValue(A
));
546 TEST_F(VectorUtilsTest
, getSplatValueElt0
) {
548 "define <2 x i8> @test(i8 %x) {\n"
549 " %ins = insertelement <2 x i8> undef, i8 %x, i32 0\n"
550 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
553 EXPECT_EQ(getSplatValue(A
)->getName(), "x");
556 TEST_F(VectorUtilsTest
, getSplatValueEltMismatch
) {
558 "define <2 x i8> @test(i8 %x) {\n"
559 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
560 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
563 EXPECT_EQ(getSplatValue(A
), nullptr);
566 // TODO: This is a splat, but we don't recognize it.
568 TEST_F(VectorUtilsTest
, getSplatValueElt1
) {
570 "define <2 x i8> @test(i8 %x) {\n"
571 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
572 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
575 EXPECT_EQ(getSplatValue(A
), nullptr);
578 ////////////////////////////////////////////////////////////////////////////////
579 // VFShape API tests.
580 ////////////////////////////////////////////////////////////////////////////////
582 class VFShapeAPITest
: public testing::Test
{
584 void SetUp() override
{
585 M
= parseAssemblyString(IR
, Err
, Ctx
);
586 // Get the only call instruction in the block, which is the first
588 CI
= dyn_cast
<CallInst
>(&*(instructions(M
->getFunction("f")).begin()));
591 const char *IR
= "define i32 @f(i32 %a, i64 %b, double %c) {\n"
592 " %1 = call i32 @g(i32 %a, i64 %b, double %c)\n"
595 "declare i32 @g(i32, i64, double)\n";
598 std::unique_ptr
<Module
> M
;
600 // Dummy shape with no parameters, overwritten by buildShape when invoked.
601 VFShape Shape
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {}};
603 SmallVector
<VFParameter
, 8> &ExpectedParams
= Expected
.Parameters
;
605 void buildShape(ElementCount VF
, bool HasGlobalPred
) {
606 Shape
= VFShape::get(CI
->getFunctionType(), VF
, HasGlobalPred
);
609 bool validParams(ArrayRef
<VFParameter
> Parameters
) {
610 Shape
.Parameters
= SmallVector
<VFParameter
, 8>(Parameters
);
611 return Shape
.hasValidParameterList();
615 TEST_F(VFShapeAPITest
, API_buildVFShape
) {
616 buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
617 Expected
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
618 {0, VFParamKind::Vector
},
619 {1, VFParamKind::Vector
},
620 {2, VFParamKind::Vector
},
622 EXPECT_EQ(Shape
, Expected
);
624 buildShape(/*VF*/ ElementCount::getFixed(4), /*HasGlobalPred*/ true);
625 Expected
= {/*VF*/ ElementCount::getFixed(4), /*Parameters*/ {
626 {0, VFParamKind::Vector
},
627 {1, VFParamKind::Vector
},
628 {2, VFParamKind::Vector
},
629 {3, VFParamKind::GlobalPredicate
},
631 EXPECT_EQ(Shape
, Expected
);
633 buildShape(/*VF*/ ElementCount::getScalable(16), /*HasGlobalPred*/ false);
634 Expected
= {/*VF*/ ElementCount::getScalable(16), /*Parameters*/ {
635 {0, VFParamKind::Vector
},
636 {1, VFParamKind::Vector
},
637 {2, VFParamKind::Vector
},
639 EXPECT_EQ(Shape
, Expected
);
642 TEST_F(VFShapeAPITest
, API_getScalarShape
) {
643 buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ false);
644 EXPECT_EQ(VFShape::getScalarShape(CI
->getFunctionType()), Shape
);
647 TEST_F(VFShapeAPITest
, API_getVectorizedFunction
) {
648 VFShape ScalarShape
= VFShape::getScalarShape(CI
->getFunctionType());
649 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(ScalarShape
),
650 M
->getFunction("g"));
652 buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ false);
653 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
654 buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ true);
655 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
656 buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ true);
657 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
660 TEST_F(VFShapeAPITest
, API_updateVFShape
) {
662 buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
663 Shape
.updateParam({0 /*Pos*/, VFParamKind::OMP_Linear
, 1, Align(4)});
664 Expected
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
665 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
666 {1, VFParamKind::Vector
},
667 {2, VFParamKind::Vector
},
669 EXPECT_EQ(Shape
, Expected
);
671 // From this point on, we update only the parameters of the VFShape,
672 // so we update only the reference of the expected Parameters.
673 Shape
.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform
});
675 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
676 {1, VFParamKind::OMP_Uniform
},
677 {2, VFParamKind::Vector
},
679 EXPECT_EQ(Shape
, Expected
);
681 Shape
.updateParam({2 /*Pos*/, VFParamKind::OMP_LinearRefPos
, 1});
683 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
684 {1, VFParamKind::OMP_Uniform
},
685 {2, VFParamKind::OMP_LinearRefPos
, 1},
687 EXPECT_EQ(Shape
, Expected
);
690 TEST_F(VFShapeAPITest
, API_updateVFShape_GlobalPredicate
) {
692 buildShape(/*VF*/ ElementCount::getScalable(2), /*HasGlobalPred*/ true);
693 Shape
.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform
});
694 Expected
= {/*VF*/ ElementCount::getScalable(2),
695 /*Parameters*/ {{0, VFParamKind::Vector
},
696 {1, VFParamKind::OMP_Uniform
},
697 {2, VFParamKind::Vector
},
698 {3, VFParamKind::GlobalPredicate
}}};
699 EXPECT_EQ(Shape
, Expected
);
702 TEST_F(VFShapeAPITest
, Parameters_Valid
) {
703 // ParamPos in order.
704 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
}}));
706 validParams({{0, VFParamKind::Vector
}, {1, VFParamKind::Vector
}}));
707 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
708 {1, VFParamKind::Vector
},
709 {2, VFParamKind::Vector
}}));
711 // GlocalPredicate is unique.
712 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
713 {1, VFParamKind::Vector
},
714 {2, VFParamKind::Vector
},
715 {3, VFParamKind::GlobalPredicate
}}));
717 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
718 {1, VFParamKind::GlobalPredicate
},
719 {2, VFParamKind::Vector
}}));
722 TEST_F(VFShapeAPITest
, Parameters_ValidOpenMPLinear
) {
723 // Valid linear constant step (>0).
724 #define __BUILD_PARAMETERS(Kind, Val) \
728 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear
, 1)));
729 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef
, 2)));
730 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal
, 4)));
731 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal
, 33)));
732 #undef __BUILD_PARAMETERS
734 // Valid linear runtime step (the step parameter is marked uniform).
735 #define __BUILD_PARAMETERS(Kind) \
737 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 0 } \
739 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos
)));
740 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos
)));
741 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos
)));
742 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos
)));
743 #undef __BUILD_PARAMETERS
746 TEST_F(VFShapeAPITest
, Parameters_Invalid
) {
748 // Wrong order is checked by an assertion: make sure that the
749 // assertion is not removed.
750 EXPECT_DEATH(validParams({{1, VFParamKind::Vector
}}),
751 "Broken parameter list.");
753 validParams({{1, VFParamKind::Vector
}, {0, VFParamKind::Vector
}}),
754 "Broken parameter list.");
757 // GlobalPredicate is not unique
758 EXPECT_FALSE(validParams({{0, VFParamKind::Vector
},
759 {1, VFParamKind::GlobalPredicate
},
760 {2, VFParamKind::GlobalPredicate
}}));
761 EXPECT_FALSE(validParams({{0, VFParamKind::GlobalPredicate
},
762 {1, VFParamKind::Vector
},
763 {2, VFParamKind::GlobalPredicate
}}));
766 TEST_F(VFShapeAPITest
, Parameters_InvalidOpenMPLinear
) {
767 // Compile time linear steps must be non-zero (compile time invariant).
768 #define __BUILD_PARAMETERS(Kind) \
772 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear
)));
773 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef
)));
774 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal
)));
775 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal
)));
776 #undef __BUILD_PARAMETERS
778 // The step of a runtime linear parameter must be marked
779 // as uniform (runtime invariant).
780 #define __BUILD_PARAMETERS(Kind) \
782 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 1 } \
784 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos
)));
785 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos
)));
786 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos
)));
787 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos
)));
788 #undef __BUILD_PARAMETERS
790 // The linear step parameter can't point at itself.
791 #define __BUILD_PARAMETERS(Kind) \
793 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 2 } \
795 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos
)));
796 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos
)));
797 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos
)));
798 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos
)));
799 #undef __BUILD_PARAMETERS
801 // Linear parameter (runtime) is out of range.
802 #define __BUILD_PARAMETERS(Kind) \
804 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 3 } \
806 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos
)));
807 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos
)));
808 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos
)));
809 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos
)));
810 #undef __BUILD_PARAMETERS