Debian Codegraph @

llvm/unittests/Transforms/Vectorize/VPlanVerifierTest.cpp

raw
1 //===- llvm/unittests/Transforms/Vectorize/VPlanVerifierTest.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 "../lib/Transforms/Vectorize/VPlanVerifier.h"
10 #include "../lib/Transforms/Vectorize/VPlan.h"
11 #include "../lib/Transforms/Vectorize/VPlanCFG.h"
12 #include "VPlanTestBase.h"
13 #include "llvm/IR/Instruction.h"
14 #include "llvm/IR/Instructions.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18
19 using VPVerifierTest = VPlanTestBase;
20
21 namespace {
22 TEST_F(VPVerifierTest, VPInstructionUseBeforeDefSameBB) {
23 VPlan &Plan = getPlan();
24 VPValue *Zero = Plan.getOrAddLiveIn(ConstantInt::get(Type::getInt32Ty(C), 0));
25 VPInstruction *DefI = new VPInstruction(Instruction::Add, {Zero});
26 VPInstruction *UseI = new VPInstruction(Instruction::Sub, {DefI});
27 auto *CanIV = new VPCanonicalIVPHIRecipe(Zero, {});
28
29 VPBasicBlock *VPBB1 = Plan.getEntry();
30 VPBB1->appendRecipe(UseI);
31 VPBB1->appendRecipe(DefI);
32
33 VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
34 VPBB2->appendRecipe(CanIV);
35 VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB2, "R1");
36 VPBlockUtils::connectBlocks(VPBB1, R1);
37 VPBlockUtils::connectBlocks(R1, Plan.getScalarHeader());
38
39 #if GTEST_HAS_STREAM_REDIRECTION
40 ::testing::internal::CaptureStderr();
41 #endif
42 EXPECT_FALSE(verifyVPlanIsValid(Plan));
43 #if GTEST_HAS_STREAM_REDIRECTION
44 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
45 EXPECT_STREQ("Use before def!\n"
46 " EMIT vp<%1> = sub vp<%2>\n"
47 " before\n"
48 " EMIT vp<%2> = add ir<0>\n",
49 ::testing::internal::GetCapturedStderr().c_str());
50 #else
51 EXPECT_STREQ("Use before def!\n",
52 ::testing::internal::GetCapturedStderr().c_str());
53 #endif
54 #endif
55 }
56
57 TEST_F(VPVerifierTest, VPInstructionUseBeforeDefDifferentBB) {
58 VPlan &Plan = getPlan();
59 VPValue *Zero = Plan.getOrAddLiveIn(ConstantInt::get(Type::getInt32Ty(C), 0));
60 VPInstruction *DefI = new VPInstruction(Instruction::Add, {Zero});
61 VPInstruction *UseI = new VPInstruction(Instruction::Sub, {DefI});
62 auto *CanIV = new VPCanonicalIVPHIRecipe(Zero, {});
63 VPInstruction *BranchOnCond =
64 new VPInstruction(VPInstruction::BranchOnCond, {CanIV});
65
66 VPBasicBlock *VPBB1 = Plan.getEntry();
67 VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
68
69 VPBB1->appendRecipe(UseI);
70 VPBB2->appendRecipe(CanIV);
71 VPBB2->appendRecipe(DefI);
72 VPBB2->appendRecipe(BranchOnCond);
73
74 VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB2, "R1");
75 VPBlockUtils::connectBlocks(VPBB1, R1);
76 VPBlockUtils::connectBlocks(R1, Plan.getScalarHeader());
77
78 #if GTEST_HAS_STREAM_REDIRECTION
79 ::testing::internal::CaptureStderr();
80 #endif
81 EXPECT_FALSE(verifyVPlanIsValid(Plan));
82 #if GTEST_HAS_STREAM_REDIRECTION
83 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
84 EXPECT_STREQ("Use before def!\n"
85 " EMIT vp<%1> = sub vp<%3>\n"
86 " before\n"
87 " EMIT vp<%3> = add ir<0>\n",
88 ::testing::internal::GetCapturedStderr().c_str());
89 #else
90 EXPECT_STREQ("Use before def!\n",
91 ::testing::internal::GetCapturedStderr().c_str());
92 #endif
93 #endif
94 }
95
96 TEST_F(VPVerifierTest, VPBlendUseBeforeDefDifferentBB) {
97 VPlan &Plan = getPlan();
98 IntegerType *Int32 = IntegerType::get(C, 32);
99 auto *Phi = PHINode::Create(Int32, 1);
100 VPValue *Zero = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 0));
101
102 VPInstruction *DefI = new VPInstruction(Instruction::Add, {Zero});
103 auto *CanIV = new VPCanonicalIVPHIRecipe(Zero, {});
104 VPInstruction *BranchOnCond =
105 new VPInstruction(VPInstruction::BranchOnCond, {CanIV});
106 auto *Blend = new VPBlendRecipe(Phi, {DefI});
107
108 VPBasicBlock *VPBB1 = Plan.getEntry();
109 VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
110 VPBasicBlock *VPBB3 = Plan.createVPBasicBlock("");
111 VPBasicBlock *VPBB4 = Plan.createVPBasicBlock("");
112
113 VPBB2->appendRecipe(CanIV);
114 VPBB3->appendRecipe(Blend);
115 VPBB4->appendRecipe(DefI);
116 VPBB4->appendRecipe(BranchOnCond);
117
118 VPBlockUtils::connectBlocks(VPBB2, VPBB3);
119 VPBlockUtils::connectBlocks(VPBB3, VPBB4);
120 VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB4, "R1");
121 VPBlockUtils::connectBlocks(VPBB1, R1);
122 VPBB3->setParent(R1);
123
124 VPBlockUtils::connectBlocks(R1, Plan.getScalarHeader());
125
126 #if GTEST_HAS_STREAM_REDIRECTION
127 ::testing::internal::CaptureStderr();
128 #endif
129 EXPECT_FALSE(verifyVPlanIsValid(Plan));
130 #if GTEST_HAS_STREAM_REDIRECTION
131 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 EXPECT_STREQ("Use before def!\n"
133 " BLEND ir<<badref>> = vp<%2>\n"
134 " before\n"
135 " EMIT vp<%2> = add ir<0>\n",
136 ::testing::internal::GetCapturedStderr().c_str());
137 #else
138 EXPECT_STREQ("Use before def!\n",
139 ::testing::internal::GetCapturedStderr().c_str());
140 #endif
141 #endif
142
143 delete Phi;
144 }
145
146 TEST_F(VPVerifierTest, VPPhiIncomingValueDoesntDominateIncomingBlock) {
147 VPlan &Plan = getPlan();
148 IntegerType *Int32 = IntegerType::get(C, 32);
149 VPValue *Zero = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 0));
150
151 VPBasicBlock *VPBB1 = Plan.getEntry();
152 VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
153 VPBasicBlock *VPBB3 = Plan.createVPBasicBlock("");
154 VPBasicBlock *VPBB4 = Plan.createVPBasicBlock("");
155
156 VPInstruction *DefI = new VPInstruction(Instruction::Add, {Zero});
157 VPPhi *Phi = new VPPhi({DefI}, {});
158 VPBB2->appendRecipe(Phi);
159 VPBB2->appendRecipe(DefI);
160 auto *CanIV = new VPCanonicalIVPHIRecipe(Zero, {});
161 VPBB3->appendRecipe(CanIV);
162
163 VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB3, VPBB3, "R1");
164 VPBlockUtils::connectBlocks(VPBB1, VPBB2);
165 VPBlockUtils::connectBlocks(VPBB2, R1);
166 VPBlockUtils::connectBlocks(VPBB4, Plan.getScalarHeader());
167 #if GTEST_HAS_STREAM_REDIRECTION
168 ::testing::internal::CaptureStderr();
169 #endif
170 EXPECT_FALSE(verifyVPlanIsValid(Plan));
171 #if GTEST_HAS_STREAM_REDIRECTION
172 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
173 EXPECT_STREQ("Incoming def at index 0 does not dominate incoming block!\n"
174 " EMIT vp<%2> = add ir<0>\n"
175 " does not dominate preheader for\n"
176 " EMIT-SCALAR vp<%1> = phi [ vp<%2>, preheader ]",
177 ::testing::internal::GetCapturedStderr().c_str());
178 #else
179 EXPECT_STREQ("Incoming def at index 0 does not dominate incoming block!\n", ::
180 testing::internal::GetCapturedStderr()
181 .c_str());
182 #endif
183 #endif
184 }
185
186 TEST_F(VPVerifierTest, DuplicateSuccessorsOutsideRegion) {
187 VPlan &Plan = getPlan();
188 VPValue *Zero = Plan.getOrAddLiveIn(ConstantInt::get(Type::getInt32Ty(C), 0));
189 VPInstruction *I1 = new VPInstruction(Instruction::Add, {Zero});
190 auto *CanIV = new VPCanonicalIVPHIRecipe(Zero, {});
191 VPInstruction *BranchOnCond =
192 new VPInstruction(VPInstruction::BranchOnCond, {CanIV});
193 VPInstruction *BranchOnCond2 =
194 new VPInstruction(VPInstruction::BranchOnCond, {I1});
195
196 VPBasicBlock *VPBB1 = Plan.getEntry();
197 VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
198
199 VPBB1->appendRecipe(I1);
200 VPBB1->appendRecipe(BranchOnCond2);
201 VPBB2->appendRecipe(CanIV);
202 VPBB2->appendRecipe(BranchOnCond);
203
204 VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB2, "R1");
205 VPBlockUtils::connectBlocks(VPBB1, R1);
206 VPBlockUtils::connectBlocks(VPBB1, R1);
207
208 VPBlockUtils::connectBlocks(R1, Plan.getScalarHeader());
209
210 #if GTEST_HAS_STREAM_REDIRECTION
211 ::testing::internal::CaptureStderr();
212 #endif
213 EXPECT_FALSE(verifyVPlanIsValid(Plan));
214 #if GTEST_HAS_STREAM_REDIRECTION
215 EXPECT_STREQ("Multiple instances of the same successor.\n",
216 ::testing::internal::GetCapturedStderr().c_str());
217 #endif
218 }
219
220 TEST_F(VPVerifierTest, DuplicateSuccessorsInsideRegion) {
221 VPlan &Plan = getPlan();
222 VPValue *Zero = Plan.getOrAddLiveIn(ConstantInt::get(Type::getInt32Ty(C), 0));
223 VPInstruction *I1 = new VPInstruction(Instruction::Add, {Zero});
224 auto *CanIV = new VPCanonicalIVPHIRecipe(Zero, {});
225 VPInstruction *BranchOnCond =
226 new VPInstruction(VPInstruction::BranchOnCond, {CanIV});
227 VPInstruction *BranchOnCond2 =
228 new VPInstruction(VPInstruction::BranchOnCond, {I1});
229
230 VPBasicBlock *VPBB1 = Plan.getEntry();
231 VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
232 VPBasicBlock *VPBB3 = Plan.createVPBasicBlock("");
233
234 VPBB1->appendRecipe(I1);
235 VPBB2->appendRecipe(CanIV);
236 VPBB2->appendRecipe(BranchOnCond2);
237 VPBB3->appendRecipe(BranchOnCond);
238
239 VPBlockUtils::connectBlocks(VPBB2, VPBB3);
240 VPBlockUtils::connectBlocks(VPBB2, VPBB3);
241 VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB3, "R1");
242 VPBlockUtils::connectBlocks(VPBB1, R1);
243 VPBB3->setParent(R1);
244
245 VPBlockUtils::connectBlocks(R1, Plan.getScalarHeader());
246
247 #if GTEST_HAS_STREAM_REDIRECTION
248 ::testing::internal::CaptureStderr();
249 #endif
250 EXPECT_FALSE(verifyVPlanIsValid(Plan));
251 #if GTEST_HAS_STREAM_REDIRECTION
252 EXPECT_STREQ("Multiple instances of the same successor.\n",
253 ::testing::internal::GetCapturedStderr().c_str());
254 #endif
255 }
256
257 TEST_F(VPVerifierTest, BlockOutsideRegionWithParent) {
258 VPlan &Plan = getPlan();
259
260 VPBasicBlock *VPBB1 = Plan.getEntry();
261 VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("");
262
263 VPValue *Zero = Plan.getOrAddLiveIn(ConstantInt::get(Type::getInt32Ty(C), 0));
264 auto *CanIV = new VPCanonicalIVPHIRecipe(Zero, {});
265 VPBB2->appendRecipe(CanIV);
266
267 VPInstruction *DefI = new VPInstruction(Instruction::Add, {Zero});
268 VPInstruction *BranchOnCond =
269 new VPInstruction(VPInstruction::BranchOnCond, {DefI});
270
271 VPBB1->appendRecipe(DefI);
272 VPBB2->appendRecipe(BranchOnCond);
273
274 VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB2, "R1");
275 VPBlockUtils::connectBlocks(VPBB1, R1);
276
277 VPBlockUtils::connectBlocks(R1, Plan.getScalarHeader());
278 VPBB1->setParent(R1);
279
280 #if GTEST_HAS_STREAM_REDIRECTION
281 ::testing::internal::CaptureStderr();
282 #endif
283 EXPECT_FALSE(verifyVPlanIsValid(Plan));
284 #if GTEST_HAS_STREAM_REDIRECTION
285 EXPECT_STREQ("Predecessor is not in the same region.\n",
286 ::testing::internal::GetCapturedStderr().c_str());
287 #endif
288 }
289
290 class VPIRVerifierTest : public VPlanTestIRBase {};
291
292 TEST_F(VPIRVerifierTest, testVerifyIRPhi) {
293 const char *ModuleString =
294 "define void @f(ptr %A, i64 %N) {\n"
295 "entry:\n"
296 " br label %loop\n"
297 "loop:\n"
298 " %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]\n"
299 " %arr.idx = getelementptr inbounds i32, ptr %A, i64 %iv\n"
300 " %l1 = load i32, ptr %arr.idx, align 4\n"
301 " %res = add i32 %l1, 10\n"
302 " store i32 %res, ptr %arr.idx, align 4\n"
303 " %iv.next = add i64 %iv, 1\n"
304 " %exitcond = icmp ne i64 %iv.next, %N\n"
305 " br i1 %exitcond, label %loop, label %for.end\n"
306 "for.end:\n"
307 " %p = phi i32 [ %l1, %loop ]\n"
308 " ret void\n"
309 "}\n";
310
311 Module &M = parseModule(ModuleString);
312
313 Function *F = M.getFunction("f");
314 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
315 auto Plan = buildVPlan(LoopHeader);
316
317 Plan->getExitBlocks()[0]->front().addOperand(
318 Plan->getOrAddLiveIn(ConstantInt::get(Type::getInt32Ty(*Ctx), 0)));
319
320 #if GTEST_HAS_STREAM_REDIRECTION
321 ::testing::internal::CaptureStderr();
322 #endif
323 EXPECT_FALSE(verifyVPlanIsValid(*Plan));
324 #if GTEST_HAS_STREAM_REDIRECTION
325 EXPECT_STREQ(
326 "Phi-like recipe with different number of operands and predecessors.\n",
327 ::testing::internal::GetCapturedStderr().c_str());
328 #endif
329 }
330 } // namespace