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(ArrayRef(ScaledMask
), ArrayRef({3, 2, 0, -2}));
109 narrowShuffleMaskElts(4, {3,2,0,-1}, ScaledMask
);
110 EXPECT_EQ(ArrayRef(ScaledMask
), ArrayRef({12, 13, 14, 15, 8, 9, 10, 11, 0, 1,
111 2, 3, -1, -1, -1, -1}));
114 TEST_F(BasicTest
, widenShuffleMaskElts
) {
115 SmallVector
<int, 16> WideMask
;
116 SmallVector
<int, 16> NarrowMask
;
118 // scale == 1 is a copy
119 EXPECT_TRUE(widenShuffleMaskElts(1, {3,2,0,-1}, WideMask
));
120 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
122 // back to original mask
123 narrowShuffleMaskElts(1, ArrayRef(WideMask
), NarrowMask
);
124 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({3, 2, 0, -1}));
126 // can't widen non-consecutive 3/2
127 EXPECT_FALSE(widenShuffleMaskElts(2, {3,2,0,-1}, WideMask
));
129 // can't widen if not evenly divisible
130 EXPECT_FALSE(widenShuffleMaskElts(2, {0,1,2}, WideMask
));
132 // can always widen identity to single element
133 EXPECT_TRUE(widenShuffleMaskElts(3, {0,1,2}, WideMask
));
134 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({0}));
136 // back to original mask
137 narrowShuffleMaskElts(3, ArrayRef(WideMask
), NarrowMask
);
138 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({0, 1, 2}));
140 // groups of 4 must be consecutive/undef
141 EXPECT_TRUE(widenShuffleMaskElts(4, {12,13,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}, WideMask
));
142 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
144 // back to original mask
145 narrowShuffleMaskElts(4, ArrayRef(WideMask
), NarrowMask
);
146 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({12, 13, 14, 15, 8, 9, 10, 11, 0, 1,
147 2, 3, -1, -1, -1, -1}));
149 // groups of 2 must be consecutive/undef
150 EXPECT_FALSE(widenShuffleMaskElts(2, {12,12,14,15,8,9,10,11,0,1,2,3,-1,-1,-1,-1}, WideMask
));
152 // groups of 3 must be consecutive/undef
153 EXPECT_TRUE(widenShuffleMaskElts(3, {6,7,8,0,1,2,-1,-1,-1}, WideMask
));
154 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({2, 0, -1}));
156 // back to original mask
157 narrowShuffleMaskElts(3, ArrayRef(WideMask
), NarrowMask
);
158 EXPECT_EQ(ArrayRef(NarrowMask
), ArrayRef({6, 7, 8, 0, 1, 2, -1, -1, -1}));
160 // groups of 3 must be consecutive/undef (partial undefs are not ok)
161 EXPECT_FALSE(widenShuffleMaskElts(3, {-1,7,8,0,-1,2,-1,-1,-1}, WideMask
));
163 // negative indexes must match across a wide element
164 EXPECT_FALSE(widenShuffleMaskElts(2, {-1,-2,-1,-1}, WideMask
));
166 // negative indexes must match across a wide element
167 EXPECT_TRUE(widenShuffleMaskElts(2, {-2,-2,-3,-3}, WideMask
));
168 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-2, -3}));
171 TEST_F(BasicTest
, getShuffleMaskWithWidestElts
) {
172 SmallVector
<int, 16> WideMask
;
174 // can not widen anything here.
175 getShuffleMaskWithWidestElts({3, 2, 0, -1}, WideMask
);
176 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
178 // can't widen non-consecutive 3/2
179 getShuffleMaskWithWidestElts({3, 2, 0, -1}, WideMask
);
180 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
182 // can always widen identity to single element
183 getShuffleMaskWithWidestElts({0, 1, 2}, WideMask
);
184 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({0}));
186 // groups of 4 must be consecutive/undef
187 getShuffleMaskWithWidestElts(
188 {12, 13, 14, 15, 8, 9, 10, 11, 0, 1, 2, 3, -1, -1, -1, -1}, WideMask
);
189 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({3, 2, 0, -1}));
191 // groups of 2 must be consecutive/undef
192 getShuffleMaskWithWidestElts(
193 {12, 12, 14, 15, 8, 9, 10, 11, 0, 1, 2, 3, -1, -1, -1, -1}, WideMask
);
194 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({12, 12, 14, 15, 8, 9, 10, 11, 0, 1, 2,
195 3, -1, -1, -1, -1}));
197 // groups of 3 must be consecutive/undef
198 getShuffleMaskWithWidestElts({6, 7, 8, 0, 1, 2, -1, -1, -1}, WideMask
);
199 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({2, 0, -1}));
201 // groups of 3 must be consecutive/undef (partial undefs are not ok)
202 getShuffleMaskWithWidestElts({-1, 7, 8, 0, -1, 2, -1, -1, -1}, WideMask
);
203 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-1, 7, 8, 0, -1, 2, -1, -1, -1}));
205 // negative indexes must match across a wide element
206 getShuffleMaskWithWidestElts({-1, -2, -1, -1}, WideMask
);
207 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-1, -2, -1, -1}));
209 // negative indexes must match across a wide element
210 getShuffleMaskWithWidestElts({-2, -2, -3, -3}, WideMask
);
211 EXPECT_EQ(ArrayRef(WideMask
), ArrayRef({-2, -3}));
214 TEST_F(BasicTest
, getShuffleDemandedElts
) {
218 EXPECT_TRUE(getShuffleDemandedElts(4, {0, 0, 0, 0}, APInt(4,0xf), LHS
, RHS
));
219 EXPECT_EQ(LHS
.getZExtValue(), 0x1U
);
220 EXPECT_EQ(RHS
.getZExtValue(), 0x0U
);
222 // broadcast zero (with non-permitted undefs)
223 EXPECT_FALSE(getShuffleDemandedElts(2, {0, -1}, APInt(2, 0x3), LHS
, RHS
));
225 // broadcast zero (with permitted undefs)
226 EXPECT_TRUE(getShuffleDemandedElts(3, {0, 0, -1}, APInt(3, 0x7), LHS
, RHS
, true));
227 EXPECT_EQ(LHS
.getZExtValue(), 0x1U
);
228 EXPECT_EQ(RHS
.getZExtValue(), 0x0U
);
230 // broadcast one in demanded
231 EXPECT_TRUE(getShuffleDemandedElts(4, {1, 1, 1, -1}, APInt(4, 0x7), LHS
, RHS
));
232 EXPECT_EQ(LHS
.getZExtValue(), 0x2U
);
233 EXPECT_EQ(RHS
.getZExtValue(), 0x0U
);
235 // broadcast 7 in demanded
236 EXPECT_TRUE(getShuffleDemandedElts(4, {7, 0, 7, 7}, APInt(4, 0xd), LHS
, RHS
));
237 EXPECT_EQ(LHS
.getZExtValue(), 0x0U
);
238 EXPECT_EQ(RHS
.getZExtValue(), 0x8U
);
241 EXPECT_TRUE(getShuffleDemandedElts(4, {4, 2, 7, 3}, APInt(4, 0xf), LHS
, RHS
));
242 EXPECT_EQ(LHS
.getZExtValue(), 0xcU
);
243 EXPECT_EQ(RHS
.getZExtValue(), 0x9U
);
246 TEST_F(BasicTest
, getSplatIndex
) {
247 EXPECT_EQ(getSplatIndex({0,0,0}), 0);
248 EXPECT_EQ(getSplatIndex({1,0,0}), -1); // no splat
249 EXPECT_EQ(getSplatIndex({0,1,1}), -1); // no splat
250 EXPECT_EQ(getSplatIndex({42,42,42}), 42); // array size is independent of splat index
251 EXPECT_EQ(getSplatIndex({42,42,-1}), 42); // ignore negative
252 EXPECT_EQ(getSplatIndex({-1,42,-1}), 42); // ignore negatives
253 EXPECT_EQ(getSplatIndex({-4,42,-42}), 42); // ignore all negatives
254 EXPECT_EQ(getSplatIndex({-4,-1,-42}), -1); // all negative values map to -1
257 TEST_F(VectorUtilsTest
, isSplatValue_00
) {
259 "define <2 x i8> @test(<2 x i8> %x) {\n"
260 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
263 EXPECT_TRUE(isSplatValue(A
));
266 TEST_F(VectorUtilsTest
, isSplatValue_00_index0
) {
268 "define <2 x i8> @test(<2 x i8> %x) {\n"
269 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
272 EXPECT_TRUE(isSplatValue(A
, 0));
275 TEST_F(VectorUtilsTest
, isSplatValue_00_index1
) {
277 "define <2 x i8> @test(<2 x i8> %x) {\n"
278 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> zeroinitializer\n"
281 EXPECT_FALSE(isSplatValue(A
, 1));
284 TEST_F(VectorUtilsTest
, isSplatValue_11
) {
286 "define <2 x i8> @test(<2 x i8> %x) {\n"
287 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
290 EXPECT_TRUE(isSplatValue(A
));
293 TEST_F(VectorUtilsTest
, isSplatValue_11_index0
) {
295 "define <2 x i8> @test(<2 x i8> %x) {\n"
296 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
299 EXPECT_FALSE(isSplatValue(A
, 0));
302 TEST_F(VectorUtilsTest
, isSplatValue_11_index1
) {
304 "define <2 x i8> @test(<2 x i8> %x) {\n"
305 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
308 EXPECT_TRUE(isSplatValue(A
, 1));
311 TEST_F(VectorUtilsTest
, isSplatValue_01
) {
313 "define <2 x i8> @test(<2 x i8> %x) {\n"
314 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
317 EXPECT_FALSE(isSplatValue(A
));
320 TEST_F(VectorUtilsTest
, isSplatValue_01_index0
) {
322 "define <2 x i8> @test(<2 x i8> %x) {\n"
323 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
326 EXPECT_FALSE(isSplatValue(A
, 0));
329 TEST_F(VectorUtilsTest
, isSplatValue_01_index1
) {
331 "define <2 x i8> @test(<2 x i8> %x) {\n"
332 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
335 EXPECT_FALSE(isSplatValue(A
, 1));
338 // FIXME: Allow undef matching with Constant (mask) splat analysis.
340 TEST_F(VectorUtilsTest
, isSplatValue_0u
) {
342 "define <2 x i8> @test(<2 x i8> %x) {\n"
343 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
346 EXPECT_FALSE(isSplatValue(A
));
349 // FIXME: Allow undef matching with Constant (mask) splat analysis.
351 TEST_F(VectorUtilsTest
, isSplatValue_0u_index0
) {
353 "define <2 x i8> @test(<2 x i8> %x) {\n"
354 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
357 EXPECT_FALSE(isSplatValue(A
, 0));
360 TEST_F(VectorUtilsTest
, isSplatValue_0u_index1
) {
362 "define <2 x i8> @test(<2 x i8> %x) {\n"
363 " %A = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 undef>\n"
366 EXPECT_FALSE(isSplatValue(A
, 1));
369 TEST_F(VectorUtilsTest
, isSplatValue_Binop
) {
371 "define <2 x i8> @test(<2 x i8> %x) {\n"
372 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
373 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
374 " %A = udiv <2 x i8> %v0, %v1\n"
377 EXPECT_TRUE(isSplatValue(A
));
380 TEST_F(VectorUtilsTest
, isSplatValue_Binop_index0
) {
382 "define <2 x i8> @test(<2 x i8> %x) {\n"
383 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
384 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
385 " %A = udiv <2 x i8> %v0, %v1\n"
388 EXPECT_FALSE(isSplatValue(A
, 0));
391 TEST_F(VectorUtilsTest
, isSplatValue_Binop_index1
) {
393 "define <2 x i8> @test(<2 x i8> %x) {\n"
394 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
395 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
396 " %A = udiv <2 x i8> %v0, %v1\n"
399 EXPECT_FALSE(isSplatValue(A
, 1));
402 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0
) {
404 "define <2 x i8> @test(<2 x i8> %x) {\n"
405 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
406 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
409 EXPECT_TRUE(isSplatValue(A
));
412 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0_index0
) {
414 "define <2 x i8> @test(<2 x i8> %x) {\n"
415 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
416 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
419 EXPECT_FALSE(isSplatValue(A
, 0));
422 TEST_F(VectorUtilsTest
, isSplatValue_Binop_ConstantOp0_index1
) {
424 "define <2 x i8> @test(<2 x i8> %x) {\n"
425 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
426 " %A = ashr <2 x i8> <i8 42, i8 42>, %v1\n"
429 EXPECT_TRUE(isSplatValue(A
, 1));
432 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op0
) {
434 "define <2 x i8> @test(<2 x i8> %x) {\n"
435 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 0>\n"
436 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
437 " %A = add <2 x i8> %v0, %v1\n"
440 EXPECT_FALSE(isSplatValue(A
));
443 TEST_F(VectorUtilsTest
, isSplatValue_Binop_Not_Op1
) {
445 "define <2 x i8> @test(<2 x i8> %x) {\n"
446 " %v0 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
447 " %v1 = shufflevector <2 x i8> %x, <2 x i8> undef, <2 x i32> <i32 0, i32 1>\n"
448 " %A = shl <2 x i8> %v0, %v1\n"
451 EXPECT_FALSE(isSplatValue(A
));
454 TEST_F(VectorUtilsTest
, isSplatValue_Select
) {
456 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
457 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
458 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
459 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
460 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %v2\n"
463 EXPECT_TRUE(isSplatValue(A
));
466 TEST_F(VectorUtilsTest
, isSplatValue_Select_ConstantOp
) {
468 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
469 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
470 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
471 " %A = select <2 x i1> %v0, <2 x i8> <i8 42, i8 42>, <2 x i8> %v2\n"
474 EXPECT_TRUE(isSplatValue(A
));
477 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotCond
) {
479 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
480 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
481 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
482 " %A = select <2 x i1> %x, <2 x i8> %v1, <2 x i8> %v2\n"
485 EXPECT_FALSE(isSplatValue(A
));
488 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp1
) {
490 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
491 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
492 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
493 " %A = select <2 x i1> %v0, <2 x i8> %y, <2 x i8> %v2\n"
496 EXPECT_FALSE(isSplatValue(A
));
499 TEST_F(VectorUtilsTest
, isSplatValue_Select_NotOp2
) {
501 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
502 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
503 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
504 " %A = select <2 x i1> %v0, <2 x i8> %v1, <2 x i8> %z\n"
507 EXPECT_FALSE(isSplatValue(A
));
510 TEST_F(VectorUtilsTest
, isSplatValue_SelectBinop
) {
512 "define <2 x i8> @test(<2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {\n"
513 " %v0 = shufflevector <2 x i1> %x, <2 x i1> undef, <2 x i32> <i32 1, i32 1>\n"
514 " %v1 = shufflevector <2 x i8> %y, <2 x i8> undef, <2 x i32> <i32 0, i32 0>\n"
515 " %v2 = shufflevector <2 x i8> %z, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
516 " %bo = xor <2 x i8> %v1, %v2\n"
517 " %A = select <2 x i1> %v0, <2 x i8> %bo, <2 x i8> %v2\n"
520 EXPECT_TRUE(isSplatValue(A
));
523 TEST_F(VectorUtilsTest
, getSplatValueElt0
) {
525 "define <2 x i8> @test(i8 %x) {\n"
526 " %ins = insertelement <2 x i8> undef, i8 %x, i32 0\n"
527 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
530 EXPECT_EQ(getSplatValue(A
)->getName(), "x");
533 TEST_F(VectorUtilsTest
, getSplatValueEltMismatch
) {
535 "define <2 x i8> @test(i8 %x) {\n"
536 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
537 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> zeroinitializer\n"
540 EXPECT_EQ(getSplatValue(A
), nullptr);
543 // TODO: This is a splat, but we don't recognize it.
545 TEST_F(VectorUtilsTest
, getSplatValueElt1
) {
547 "define <2 x i8> @test(i8 %x) {\n"
548 " %ins = insertelement <2 x i8> undef, i8 %x, i32 1\n"
549 " %A = shufflevector <2 x i8> %ins, <2 x i8> undef, <2 x i32> <i32 1, i32 1>\n"
552 EXPECT_EQ(getSplatValue(A
), nullptr);
555 ////////////////////////////////////////////////////////////////////////////////
556 // VFShape API tests.
557 ////////////////////////////////////////////////////////////////////////////////
559 class VFShapeAPITest
: public testing::Test
{
561 void SetUp() override
{
562 M
= parseAssemblyString(IR
, Err
, Ctx
);
563 // Get the only call instruction in the block, which is the first
565 CI
= dyn_cast
<CallInst
>(&*(instructions(M
->getFunction("f")).begin()));
568 const char *IR
= "define i32 @f(i32 %a, i64 %b, double %c) {\n"
569 " %1 = call i32 @g(i32 %a, i64 %b, double %c)\n"
572 "declare i32 @g(i32, i64, double)\n";
575 std::unique_ptr
<Module
> M
;
577 // Dummy shape with no parameters, overwritten by buildShape when invoked.
578 VFShape Shape
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {}};
580 SmallVector
<VFParameter
, 8> &ExpectedParams
= Expected
.Parameters
;
582 void buildShape(ElementCount VF
, bool HasGlobalPred
) {
583 Shape
= VFShape::get(*CI
, VF
, HasGlobalPred
);
586 bool validParams(ArrayRef
<VFParameter
> Parameters
) {
588 SmallVector
<VFParameter
, 8>(Parameters
.begin(), Parameters
.end());
589 return Shape
.hasValidParameterList();
593 TEST_F(VFShapeAPITest
, API_buildVFShape
) {
594 buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
595 Expected
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
596 {0, VFParamKind::Vector
},
597 {1, VFParamKind::Vector
},
598 {2, VFParamKind::Vector
},
600 EXPECT_EQ(Shape
, Expected
);
602 buildShape(/*VF*/ ElementCount::getFixed(4), /*HasGlobalPred*/ true);
603 Expected
= {/*VF*/ ElementCount::getFixed(4), /*Parameters*/ {
604 {0, VFParamKind::Vector
},
605 {1, VFParamKind::Vector
},
606 {2, VFParamKind::Vector
},
607 {3, VFParamKind::GlobalPredicate
},
609 EXPECT_EQ(Shape
, Expected
);
611 buildShape(/*VF*/ ElementCount::getScalable(16), /*HasGlobalPred*/ false);
612 Expected
= {/*VF*/ ElementCount::getScalable(16), /*Parameters*/ {
613 {0, VFParamKind::Vector
},
614 {1, VFParamKind::Vector
},
615 {2, VFParamKind::Vector
},
617 EXPECT_EQ(Shape
, Expected
);
620 TEST_F(VFShapeAPITest
, API_getScalarShape
) {
621 buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ false);
622 EXPECT_EQ(VFShape::getScalarShape(*CI
), Shape
);
625 TEST_F(VFShapeAPITest
, API_getVectorizedFunction
) {
626 VFShape ScalarShape
= VFShape::getScalarShape(*CI
);
627 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(ScalarShape
),
628 M
->getFunction("g"));
630 buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ false);
631 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
632 buildShape(/*VF*/ ElementCount::getFixed(1), /*HasGlobalPred*/ true);
633 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
634 buildShape(/*VF*/ ElementCount::getScalable(1), /*HasGlobalPred*/ true);
635 EXPECT_EQ(VFDatabase(*CI
).getVectorizedFunction(Shape
), nullptr);
638 TEST_F(VFShapeAPITest
, API_updateVFShape
) {
640 buildShape(/*VF*/ ElementCount::getFixed(2), /*HasGlobalPred*/ false);
641 Shape
.updateParam({0 /*Pos*/, VFParamKind::OMP_Linear
, 1, Align(4)});
642 Expected
= {/*VF*/ ElementCount::getFixed(2), /*Parameters*/ {
643 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
644 {1, VFParamKind::Vector
},
645 {2, VFParamKind::Vector
},
647 EXPECT_EQ(Shape
, Expected
);
649 // From this point on, we update only the parameters of the VFShape,
650 // so we update only the reference of the expected Parameters.
651 Shape
.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform
});
653 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
654 {1, VFParamKind::OMP_Uniform
},
655 {2, VFParamKind::Vector
},
657 EXPECT_EQ(Shape
, Expected
);
659 Shape
.updateParam({2 /*Pos*/, VFParamKind::OMP_LinearRefPos
, 1});
661 {0, VFParamKind::OMP_Linear
, 1, Align(4)},
662 {1, VFParamKind::OMP_Uniform
},
663 {2, VFParamKind::OMP_LinearRefPos
, 1},
665 EXPECT_EQ(Shape
, Expected
);
668 TEST_F(VFShapeAPITest
, API_updateVFShape_GlobalPredicate
) {
670 buildShape(/*VF*/ ElementCount::getScalable(2), /*HasGlobalPred*/ true);
671 Shape
.updateParam({1 /*Pos*/, VFParamKind::OMP_Uniform
});
672 Expected
= {/*VF*/ ElementCount::getScalable(2),
673 /*Parameters*/ {{0, VFParamKind::Vector
},
674 {1, VFParamKind::OMP_Uniform
},
675 {2, VFParamKind::Vector
},
676 {3, VFParamKind::GlobalPredicate
}}};
677 EXPECT_EQ(Shape
, Expected
);
680 TEST_F(VFShapeAPITest
, Parameters_Valid
) {
681 // ParamPos in order.
682 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
}}));
684 validParams({{0, VFParamKind::Vector
}, {1, VFParamKind::Vector
}}));
685 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
686 {1, VFParamKind::Vector
},
687 {2, VFParamKind::Vector
}}));
689 // GlocalPredicate is unique.
690 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
691 {1, VFParamKind::Vector
},
692 {2, VFParamKind::Vector
},
693 {3, VFParamKind::GlobalPredicate
}}));
695 EXPECT_TRUE(validParams({{0, VFParamKind::Vector
},
696 {1, VFParamKind::GlobalPredicate
},
697 {2, VFParamKind::Vector
}}));
700 TEST_F(VFShapeAPITest
, Parameters_ValidOpenMPLinear
) {
701 // Valid linear constant step (>0).
702 #define __BUILD_PARAMETERS(Kind, Val) \
706 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear
, 1)));
707 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef
, 2)));
708 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal
, 4)));
709 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal
, 33)));
710 #undef __BUILD_PARAMETERS
712 // Valid linear runtime step (the step parameter is marked uniform).
713 #define __BUILD_PARAMETERS(Kind) \
715 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 0 } \
717 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos
)));
718 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos
)));
719 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos
)));
720 EXPECT_TRUE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos
)));
721 #undef __BUILD_PARAMETERS
724 TEST_F(VFShapeAPITest
, Parameters_Invalid
) {
726 // Wrong order is checked by an assertion: make sure that the
727 // assertion is not removed.
728 EXPECT_DEATH(validParams({{1, VFParamKind::Vector
}}),
729 "Broken parameter list.");
731 validParams({{1, VFParamKind::Vector
}, {0, VFParamKind::Vector
}}),
732 "Broken parameter list.");
735 // GlobalPredicate is not unique
736 EXPECT_FALSE(validParams({{0, VFParamKind::Vector
},
737 {1, VFParamKind::GlobalPredicate
},
738 {2, VFParamKind::GlobalPredicate
}}));
739 EXPECT_FALSE(validParams({{0, VFParamKind::GlobalPredicate
},
740 {1, VFParamKind::Vector
},
741 {2, VFParamKind::GlobalPredicate
}}));
744 TEST_F(VFShapeAPITest
, Parameters_InvalidOpenMPLinear
) {
745 // Compile time linear steps must be non-zero (compile time invariant).
746 #define __BUILD_PARAMETERS(Kind) \
750 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_Linear
)));
751 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRef
)));
752 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearVal
)));
753 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUVal
)));
754 #undef __BUILD_PARAMETERS
756 // The step of a runtime linear parameter must be marked
757 // as uniform (runtime invariant).
758 #define __BUILD_PARAMETERS(Kind) \
760 {0, VFParamKind::OMP_Uniform}, {1, VFParamKind::Vector}, { 2, Kind, 1 } \
762 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos
)));
763 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos
)));
764 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos
)));
765 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos
)));
766 #undef __BUILD_PARAMETERS
768 // The linear step parameter can't point at itself.
769 #define __BUILD_PARAMETERS(Kind) \
771 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 2 } \
773 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearPos
)));
774 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearRefPos
)));
775 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearValPos
)));
776 EXPECT_FALSE(validParams(__BUILD_PARAMETERS(VFParamKind::OMP_LinearUValPos
)));
777 #undef __BUILD_PARAMETERS
779 // Linear parameter (runtime) is out of range.
780 #define __BUILD_PARAMETERS(Kind) \
782 {0, VFParamKind::Vector}, {1, VFParamKind::Vector}, { 2, Kind, 3 } \
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