Debian Codegraph @

llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp

raw
1 //===- llvm/unittest/Transforms/Vectorize/VPlanHCFGTest.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/VPlan.h"
10 #include "../lib/Transforms/Vectorize/VPlanTransforms.h"
11 #include "VPlanTestBase.h"
12 #include "llvm/Analysis/TargetLibraryInfo.h"
13 #include "llvm/TargetParser/Triple.h"
14 #include "gtest/gtest.h"
15 #include <string>
16
17 namespace llvm {
18 namespace {
19
20 class VPlanHCFGTest : public VPlanTestIRBase {};
21
22 TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
23 const char *ModuleString =
24 "define void @f(ptr %A, i64 %N) {\n"
25 "entry:\n"
26 " br label %for.body\n"
27 "for.body:\n"
28 " %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
29 " %arr.idx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv\n"
30 " %l1 = load i32, ptr %arr.idx, align 4\n"
31 " %res = add i32 %l1, 10\n"
32 " store i32 %res, ptr %arr.idx, align 4\n"
33 " %indvars.iv.next = add i64 %indvars.iv, 1\n"
34 " %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
35 " br i1 %exitcond, label %for.body, label %for.end\n"
36 "for.end:\n"
37 " ret void\n"
38 "}\n";
39
40 Module &M = parseModule(ModuleString);
41
42 Function *F = M.getFunction("f");
43 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
44 auto Plan = buildVPlan(LoopHeader);
45
46 VPBasicBlock *Entry = Plan->getEntry()->getEntryBasicBlock();
47 EXPECT_EQ(0u, Entry->getNumPredecessors());
48 EXPECT_EQ(2u, Entry->getNumSuccessors());
49
50 // Check that the region following the preheader consists of a block for the
51 // original header and a separate latch.
52 VPBasicBlock *VecBB = Plan->getVectorLoopRegion()->getEntryBasicBlock();
53 EXPECT_EQ(11u, VecBB->size());
54 EXPECT_EQ(0u, VecBB->getNumPredecessors());
55 EXPECT_EQ(0u, VecBB->getNumSuccessors());
56 EXPECT_EQ(VecBB->getParent()->getEntryBasicBlock(), VecBB);
57 EXPECT_EQ(&*Plan, VecBB->getPlan());
58
59 auto Iter = VecBB->begin();
60 auto *CanIV = dyn_cast<VPCanonicalIVPHIRecipe>(&*Iter++);
61 EXPECT_NE(nullptr, CanIV);
62 VPWidenPHIRecipe *Phi = dyn_cast<VPWidenPHIRecipe>(&*Iter++);
63 EXPECT_NE(nullptr, Phi);
64
65 VPInstruction *Idx = dyn_cast<VPInstruction>(&*Iter++);
66 EXPECT_EQ(Instruction::GetElementPtr, Idx->getOpcode());
67 EXPECT_EQ(2u, Idx->getNumOperands());
68 EXPECT_EQ(Phi, Idx->getOperand(1));
69
70 VPInstruction *Load = dyn_cast<VPInstruction>(&*Iter++);
71 EXPECT_EQ(Instruction::Load, Load->getOpcode());
72 EXPECT_EQ(1u, Load->getNumOperands());
73 EXPECT_EQ(Idx, Load->getOperand(0));
74
75 VPInstruction *Add = dyn_cast<VPInstruction>(&*Iter++);
76 EXPECT_EQ(Instruction::Add, Add->getOpcode());
77 EXPECT_EQ(2u, Add->getNumOperands());
78 EXPECT_EQ(Load, Add->getOperand(0));
79
80 VPInstruction *Store = dyn_cast<VPInstruction>(&*Iter++);
81 EXPECT_EQ(Instruction::Store, Store->getOpcode());
82 EXPECT_EQ(2u, Store->getNumOperands());
83 EXPECT_EQ(Add, Store->getOperand(0));
84 EXPECT_EQ(Idx, Store->getOperand(1));
85
86 VPInstruction *IndvarAdd = dyn_cast<VPInstruction>(&*Iter++);
87 EXPECT_EQ(Instruction::Add, IndvarAdd->getOpcode());
88 EXPECT_EQ(2u, IndvarAdd->getNumOperands());
89 EXPECT_EQ(Phi, IndvarAdd->getOperand(0));
90
91 VPInstruction *ICmp = dyn_cast<VPInstruction>(&*Iter++);
92 EXPECT_EQ(Instruction::ICmp, ICmp->getOpcode());
93 EXPECT_EQ(2u, ICmp->getNumOperands());
94 EXPECT_EQ(IndvarAdd, ICmp->getOperand(0));
95
96 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
97 // Add an external value to check we do not print the list of external values,
98 // as this is not required with the new printing.
99 Plan->getOrAddLiveIn(&*F->arg_begin());
100 std::string FullDump;
101 raw_string_ostream OS(FullDump);
102 Plan->printDOT(OS);
103 const char *ExpectedStr = R"(digraph VPlan {
104 graph [labelloc=t, fontsize=30; label="Vectorization Plan\n for UF\>=1\nLive-in vp\<%0\> = VF * UF\nLive-in vp\<%1\> = vector-trip-count\nLive-in ir\<%N\> = original trip-count\n"]
105 node [shape=rect, fontname=Courier, fontsize=30]
106 edge [fontname=Courier, fontsize=30]
107 compound=true
108 N0 [label =
109 "ir-bb\<entry\>:\l" +
110 "Successor(s): scalar.ph, vector.ph\l"
111 ]
112 N0 -> N1 [ label="T"]
113 N0 -> N2 [ label="F"]
114 N1 [label =
115 "scalar.ph:\l" +
116 "Successor(s): ir-bb\<for.body\>\l"
117 ]
118 N1 -> N3 [ label=""]
119 N3 [label =
120 "ir-bb\<for.body\>:\l" +
121 " IR %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\l" +
122 " IR %arr.idx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv\l" +
123 " IR %l1 = load i32, ptr %arr.idx, align 4\l" +
124 " IR %res = add i32 %l1, 10\l" +
125 " IR store i32 %res, ptr %arr.idx, align 4\l" +
126 " IR %indvars.iv.next = add i64 %indvars.iv, 1\l" +
127 " IR %exitcond = icmp ne i64 %indvars.iv.next, %N\l" +
128 "No successors\l"
129 ]
130 N2 [label =
131 "vector.ph:\l" +
132 "Successor(s): vector loop\l"
133 ]
134 N2 -> N4 [ label="" lhead=cluster_N5]
135 subgraph cluster_N5 {
136 fontname=Courier
137 label="\<x1\> vector loop"
138 N4 [label =
139 "vector.body:\l" +
140 " EMIT vp\<%2\> = CANONICAL-INDUCTION ir\<0\>, vp\<%index.next\>\l" +
141 " WIDEN-PHI ir\<%indvars.iv\> = phi [ ir\<0\>, vector.ph ], [ ir\<%indvars.iv.next\>, vector.body ]\l" +
142 " EMIT ir\<%arr.idx\> = getelementptr ir\<%A\>, ir\<%indvars.iv\>\l" +
143 " EMIT ir\<%l1\> = load ir\<%arr.idx\>\l" +
144 " EMIT ir\<%res\> = add ir\<%l1\>, ir\<10\>\l" +
145 " EMIT store ir\<%res\>, ir\<%arr.idx\>\l" +
146 " EMIT ir\<%indvars.iv.next\> = add ir\<%indvars.iv\>, ir\<1\>\l" +
147 " EMIT ir\<%exitcond\> = icmp ir\<%indvars.iv.next\>, ir\<%N\>\l" +
148 " EMIT vp\<%3\> = not ir\<%exitcond\>\l" +
149 " EMIT vp\<%index.next\> = add nuw vp\<%2\>, vp\<%0\>\l" +
150 " EMIT branch-on-count vp\<%index.next\>, vp\<%1\>\l" +
151 "No successors\l"
152 ]
153 }
154 N4 -> N6 [ label="" ltail=cluster_N5]
155 N6 [label =
156 "middle.block:\l" +
157 " EMIT vp\<%cmp.n\> = icmp eq ir\<%N\>, vp\<%1\>\l" +
158 " EMIT branch-on-cond vp\<%cmp.n\>\l" +
159 "Successor(s): ir-bb\<for.end\>, scalar.ph\l"
160 ]
161 N6 -> N7 [ label="T"]
162 N6 -> N1 [ label="F"]
163 N7 [label =
164 "ir-bb\<for.end\>:\l" +
165 "No successors\l"
166 ]
167 }
168 )";
169 EXPECT_EQ(ExpectedStr, FullDump);
170 #endif
171 }
172
173 TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) {
174 const char *ModuleString =
175 "define void @f(ptr %A, i64 %N) {\n"
176 "entry:\n"
177 " br label %for.body\n"
178 "for.body:\n"
179 " %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\n"
180 " %arr.idx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv\n"
181 " %l1 = load i32, ptr %arr.idx, align 4\n"
182 " %res = add i32 %l1, 10\n"
183 " store i32 %res, ptr %arr.idx, align 4\n"
184 " %indvars.iv.next = add i64 %indvars.iv, 1\n"
185 " %exitcond = icmp ne i64 %indvars.iv.next, %N\n"
186 " br i1 %exitcond, label %for.body, label %for.end\n"
187 "for.end:\n"
188 " ret void\n"
189 "}\n";
190
191 Module &M = parseModule(ModuleString);
192
193 Function *F = M.getFunction("f");
194 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
195 auto Plan = buildVPlan(LoopHeader);
196
197 TargetLibraryInfoImpl TLII(M.getTargetTriple());
198 TargetLibraryInfo TLI(TLII);
199 // Current VPlan construction doesn't add a terminator for top-level loop
200 // latches. Add it before running transform.
201 cast<VPBasicBlock>(Plan->getVectorLoopRegion()->getExiting())
202 ->appendRecipe(new VPInstruction(
203 VPInstruction::BranchOnCond,
204 {Plan->getOrAddLiveIn(ConstantInt::getTrue(F->getContext()))}));
205 VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(
206 Plan, [](PHINode *P) { return nullptr; }, *SE, TLI);
207
208 VPBlockBase *Entry = Plan->getEntry()->getEntryBasicBlock();
209 EXPECT_EQ(0u, Entry->getNumPredecessors());
210 EXPECT_EQ(2u, Entry->getNumSuccessors());
211
212 // Check that the region following the preheader consists of a block for the
213 // original header and a separate latch.
214 VPBasicBlock *VecBB = Plan->getVectorLoopRegion()->getEntryBasicBlock();
215 EXPECT_EQ(12u, VecBB->size());
216 EXPECT_EQ(0u, VecBB->getNumPredecessors());
217 EXPECT_EQ(0u, VecBB->getNumSuccessors());
218 EXPECT_EQ(VecBB->getParent()->getEntryBasicBlock(), VecBB);
219
220 auto Iter = VecBB->begin();
221 EXPECT_NE(nullptr, dyn_cast<VPCanonicalIVPHIRecipe>(&*Iter++));
222 EXPECT_NE(nullptr, dyn_cast<VPWidenPHIRecipe>(&*Iter++));
223 EXPECT_NE(nullptr, dyn_cast<VPWidenGEPRecipe>(&*Iter++));
224 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryRecipe>(&*Iter++));
225 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
226 EXPECT_NE(nullptr, dyn_cast<VPWidenMemoryRecipe>(&*Iter++));
227 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
228 EXPECT_NE(nullptr, dyn_cast<VPWidenRecipe>(&*Iter++));
229 EXPECT_NE(nullptr, dyn_cast<VPInstruction>(&*Iter++));
230 EXPECT_NE(nullptr, dyn_cast<VPInstruction>(&*Iter++));
231 EXPECT_NE(nullptr, dyn_cast<VPInstruction>(&*Iter++));
232 EXPECT_NE(nullptr, dyn_cast<VPInstruction>(&*Iter++));
233 EXPECT_EQ(VecBB->end(), Iter);
234 }
235
236 TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoopMultiExit) {
237 const char *ModuleString =
238 "define void @f(ptr %A, i64 %N) {\n"
239 "entry:\n"
240 " br label %loop.header\n"
241 "loop.header:\n"
242 " %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]\n"
243 " %arr.idx = getelementptr inbounds i32, ptr %A, i64 %iv\n"
244 " %l1 = load i32, ptr %arr.idx, align 4\n"
245 " %c = icmp eq i32 %l1, 0\n"
246 " br i1 %c, label %exit.1, label %loop.latch\n"
247 "loop.latch:\n"
248 " %res = add i32 %l1, 10\n"
249 " store i32 %res, ptr %arr.idx, align 4\n"
250 " %iv.next = add i64 %iv, 1\n"
251 " %exitcond = icmp ne i64 %iv.next, %N\n"
252 " br i1 %exitcond, label %loop.header, label %exit.2\n"
253 "exit.1:\n"
254 " ret void\n"
255 "exit.2:\n"
256 " ret void\n"
257 "}\n";
258
259 Module &M = parseModule(ModuleString);
260
261 Function *F = M.getFunction("f");
262 BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
263 auto Plan = buildVPlan(LoopHeader);
264
265 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
266 // Add an external value to check we do not print the list of external values,
267 // as this is not required with the new printing.
268 Plan->getOrAddLiveIn(&*F->arg_begin());
269 std::string FullDump;
270 raw_string_ostream OS(FullDump);
271 Plan->printDOT(OS);
272 const char *ExpectedStr = R"(digraph VPlan {
273 graph [labelloc=t, fontsize=30; label="Vectorization Plan\n for UF\>=1\nLive-in vp\<%0\> = VF * UF\nLive-in vp\<%1\> = vector-trip-count\nLive-in ir\<%N\> = original trip-count\n"]
274 node [shape=rect, fontname=Courier, fontsize=30]
275 edge [fontname=Courier, fontsize=30]
276 compound=true
277 N0 [label =
278 "ir-bb\<entry\>:\l" +
279 "Successor(s): scalar.ph, vector.ph\l"
280 ]
281 N0 -> N1 [ label="T"]
282 N0 -> N2 [ label="F"]
283 N1 [label =
284 "scalar.ph:\l" +
285 "Successor(s): ir-bb\<loop.header\>\l"
286 ]
287 N1 -> N3 [ label=""]
288 N3 [label =
289 "ir-bb\<loop.header\>:\l" +
290 " IR %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]\l" +
291 " IR %arr.idx = getelementptr inbounds i32, ptr %A, i64 %iv\l" +
292 " IR %l1 = load i32, ptr %arr.idx, align 4\l" +
293 " IR %c = icmp eq i32 %l1, 0\l" +
294 "No successors\l"
295 ]
296 N2 [label =
297 "vector.ph:\l" +
298 "Successor(s): vector loop\l"
299 ]
300 N2 -> N4 [ label="" lhead=cluster_N5]
301 subgraph cluster_N5 {
302 fontname=Courier
303 label="\<x1\> vector loop"
304 N4 [label =
305 "vector.body:\l" +
306 " EMIT vp\<%2\> = CANONICAL-INDUCTION ir\<0\>, vp\<%index.next\>\l" +
307 " WIDEN-PHI ir\<%iv\> = phi [ ir\<0\>, vector.ph ], [ ir\<%iv.next\>, loop.latch ]\l" +
308 " EMIT ir\<%arr.idx\> = getelementptr ir\<%A\>, ir\<%iv\>\l" +
309 " EMIT ir\<%l1\> = load ir\<%arr.idx\>\l" +
310 " EMIT ir\<%c\> = icmp ir\<%l1\>, ir\<0\>\l" +
311 "Successor(s): loop.latch\l"
312 ]
313 N4 -> N6 [ label=""]
314 N6 [label =
315 "loop.latch:\l" +
316 " EMIT ir\<%res\> = add ir\<%l1\>, ir\<10\>\l" +
317 " EMIT store ir\<%res\>, ir\<%arr.idx\>\l" +
318 " EMIT ir\<%iv.next\> = add ir\<%iv\>, ir\<1\>\l" +
319 " EMIT ir\<%exitcond\> = icmp ir\<%iv.next\>, ir\<%N\>\l" +
320 " EMIT vp\<%3\> = not ir\<%exitcond\>\l" +
321 " EMIT vp\<%index.next\> = add nuw vp\<%2\>, vp\<%0\>\l" +
322 " EMIT branch-on-count vp\<%index.next\>, vp\<%1\>\l" +
323 "No successors\l"
324 ]
325 }
326 N6 -> N7 [ label="" ltail=cluster_N5]
327 N7 [label =
328 "middle.block:\l" +
329 " EMIT vp\<%cmp.n\> = icmp eq ir\<%N\>, vp\<%1\>\l" +
330 " EMIT branch-on-cond vp\<%cmp.n\>\l" +
331 "Successor(s): ir-bb\<exit.2\>, scalar.ph\l"
332 ]
333 N7 -> N8 [ label="T"]
334 N7 -> N1 [ label="F"]
335 N8 [label =
336 "ir-bb\<exit.2\>:\l" +
337 "No successors\l"
338 ]
339 }
340 )";
341 EXPECT_EQ(ExpectedStr, FullDump);
342 #endif
343 }
344
345 } // namespace
346 } // namespace llvm