Debian Codegraph @

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/InstrMapsTest.cpp

raw
1 //===- InstrMapsTest.cpp --------------------------------------------------===//
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/Transforms/Vectorize/SandboxVectorizer/InstrMaps.h"
10 #include "llvm/ADT/SmallSet.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/SandboxIR/Function.h"
13 #include "llvm/SandboxIR/Instruction.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17
18 using namespace llvm;
19
20 struct InstrMapsTest : public testing::Test {
21 LLVMContext C;
22 std::unique_ptr<Module> M;
23
24 void parseIR(LLVMContext &C, const char *IR) {
25 SMDiagnostic Err;
26 M = parseAssemblyString(IR, Err, C);
27 if (!M)
28 Err.print("InstrMapsTest", errs());
29 }
30 };
31
32 TEST_F(InstrMapsTest, Basic) {
33 parseIR(C, R"IR(
34 define void @foo(i8 %v0, i8 %v1, i8 %v2, i8 %v3, <2 x i8> %vec) {
35 %add0 = add i8 %v0, %v0
36 %add1 = add i8 %v1, %v1
37 %add2 = add i8 %v2, %v2
38 %add3 = add i8 %v3, %v3
39 %vadd0 = add <2 x i8> %vec, %vec
40 ret void
41 }
42 )IR");
43 llvm::Function *LLVMF = &*M->getFunction("foo");
44 sandboxir::Context Ctx(C);
45 auto *F = Ctx.createFunction(LLVMF);
46 auto *BB = &*F->begin();
47 auto It = BB->begin();
48
49 auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
50 auto *Add1 = cast<sandboxir::BinaryOperator>(&*It++);
51 auto *Add2 = cast<sandboxir::BinaryOperator>(&*It++);
52 auto *Add3 = cast<sandboxir::BinaryOperator>(&*It++);
53 auto *VAdd0 = cast<sandboxir::BinaryOperator>(&*It++);
54 [[maybe_unused]] auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
55
56 sandboxir::InstrMaps IMaps;
57 {
58 // Check with empty IMaps.
59 sandboxir::Action A(nullptr, {Add0}, {}, 0);
60 EXPECT_EQ(IMaps.getVectorForOrig(Add0), nullptr);
61 EXPECT_EQ(IMaps.getVectorForOrig(Add1), nullptr);
62 EXPECT_FALSE(IMaps.getOrigLane(&A, Add0));
63 }
64 {
65 // Check with 1 match.
66 sandboxir::Action A(nullptr, {Add0, Add1}, {}, 0);
67 sandboxir::Action OtherA(nullptr, {}, {}, 0);
68 IMaps.registerVector({Add0, Add1}, &A);
69 EXPECT_EQ(IMaps.getVectorForOrig(Add0), &A);
70 EXPECT_EQ(IMaps.getVectorForOrig(Add1), &A);
71 EXPECT_FALSE(IMaps.getOrigLane(&A, VAdd0)); // Bad Orig value
72 EXPECT_FALSE(IMaps.getOrigLane(&OtherA, Add0)); // Bad Vector value
73 EXPECT_EQ(*IMaps.getOrigLane(&A, Add0), 0U);
74 EXPECT_EQ(*IMaps.getOrigLane(&A, Add1), 1U);
75 }
76 {
77 // Check when the same vector maps to different original values (which is
78 // common for vector constants).
79 sandboxir::Action A(nullptr, {Add2, Add3}, {}, 0);
80 IMaps.registerVector({Add2, Add3}, &A);
81 EXPECT_EQ(*IMaps.getOrigLane(&A, Add2), 0U);
82 EXPECT_EQ(*IMaps.getOrigLane(&A, Add3), 1U);
83 }
84 {
85 // Check when we register for a second time.
86 sandboxir::Action A(nullptr, {Add2, Add3}, {}, 0);
87 #ifndef NDEBUG
88 EXPECT_DEATH(IMaps.registerVector({Add1, Add0}, &A), ".*exists.*");
89 #endif // NDEBUG
90 }
91 }
92
93 TEST_F(InstrMapsTest, VectorLanes) {
94 parseIR(C, R"IR(
95 define void @foo(<2 x i8> %v0, <2 x i8> %v1, <4 x i8> %v2, <4 x i8> %v3) {
96 %vadd0 = add <2 x i8> %v0, %v1
97 %vadd1 = add <2 x i8> %v0, %v1
98 ret void
99 }
100 )IR");
101 llvm::Function *LLVMF = &*M->getFunction("foo");
102 sandboxir::Context Ctx(C);
103 auto *F = Ctx.createFunction(LLVMF);
104 auto *BB = &*F->begin();
105 auto It = BB->begin();
106
107 auto *VAdd0 = cast<sandboxir::BinaryOperator>(&*It++);
108 auto *VAdd1 = cast<sandboxir::BinaryOperator>(&*It++);
109
110 sandboxir::InstrMaps IMaps;
111
112 {
113 // Check that the vector lanes are calculated correctly.
114 sandboxir::Action A(nullptr, {VAdd0, VAdd1}, {}, 0);
115 IMaps.registerVector({VAdd0, VAdd1}, &A);
116 EXPECT_EQ(*IMaps.getOrigLane(&A, VAdd0), 0U);
117 EXPECT_EQ(*IMaps.getOrigLane(&A, VAdd1), 2U);
118 }
119 }