1//===-- VPlanVerifier.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/// \file
10/// This file defines the class VPlanVerifier, which contains utility functions
11/// to check the consistency and invariants of a VPlan.
12///
13//===----------------------------------------------------------------------===//
14
15#include "VPlanVerifier.h"
16#include "VPlan.h"
17#include "VPlanCFG.h"
18#include "VPlanDominatorTree.h"
19#include "VPlanHelpers.h"
20#include "VPlanPatternMatch.h"
21#include "VPlanUtils.h"
22#include "llvm/ADT/SmallPtrSet.h"
23
24#define DEBUG_TYPE "loop-vectorize"
25
26using namespace llvm;
27using namespace VPlanPatternMatch;
28
29namespace {
30class VPlanVerifier {
31 const VPDominatorTree &VPDT;
32
33 SmallPtrSet<BasicBlock *, 8> WrappedIRBBs;
34
35 // Verify that phi-like recipes are at the beginning of \p VPBB, with no
36 // other recipes in between. Also check that only header blocks contain
37 // VPHeaderPHIRecipes.
38 bool verifyPhiRecipes(const VPBasicBlock *VPBB);
39
40 /// Verify that \p LastActiveLane's operand is guaranteed to be a prefix-mask.
41 bool verifyLastActiveLaneRecipe(const VPInstruction &LastActiveLane) const;
42
43 bool verifyVPBasicBlock(const VPBasicBlock *VPBB);
44
45 bool verifyBlock(const VPBlockBase *VPB);
46
47 /// Helper function that verifies the CFG invariants of the VPBlockBases
48 /// within
49 /// \p Region. Checks in this function are generic for VPBlockBases. They are
50 /// not specific for VPBasicBlocks or VPRegionBlocks.
51 bool verifyBlocksInRegion(const VPRegionBlock *Region);
52
53 /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
54 /// VPBlockBases. Do not recurse inside nested VPRegionBlocks.
55 bool verifyRegion(const VPRegionBlock *Region);
56
57 /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
58 /// VPBlockBases. Recurse inside nested VPRegionBlocks.
59 bool verifyRegionRec(const VPRegionBlock *Region);
60
61public:
62 VPlanVerifier(VPDominatorTree &VPDT) : VPDT(VPDT) {}
63
64 bool verify(const VPlan &Plan);
65};
66} // namespace
67
68bool VPlanVerifier::verifyPhiRecipes(const VPBasicBlock *VPBB) {
69 auto RecipeI = VPBB->begin();
70 auto End = VPBB->end();
71 unsigned NumActiveLaneMaskPhiRecipes = 0;
72 bool IsHeaderVPBB = VPBlockUtils::isHeader(VPB: VPBB, VPDT);
73 while (RecipeI != End && RecipeI->isPhi()) {
74 if (isa<VPActiveLaneMaskPHIRecipe>(Val: RecipeI))
75 NumActiveLaneMaskPhiRecipes++;
76
77 if (IsHeaderVPBB &&
78 !isa<VPHeaderPHIRecipe, VPWidenPHIRecipe, VPPhi>(Val: *RecipeI)) {
79 errs() << "Found non-header PHI recipe in header VPBB";
80#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
81 errs() << ": ";
82 RecipeI->dump();
83#endif
84 return false;
85 }
86
87 if (!IsHeaderVPBB && isa<VPHeaderPHIRecipe>(Val: *RecipeI)) {
88 errs() << "Found header PHI recipe in non-header VPBB";
89#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
90 errs() << ": ";
91 RecipeI->dump();
92#endif
93 return false;
94 }
95
96 // In region form, VPCurrentIterationPHIRecipe must be the first header phi
97 // recipe. In a plain CFG VPlan, it must either be the first or second.
98 if (isa<VPCurrentIterationPHIRecipe>(Val: RecipeI) &&
99 (VPBB->getPlan()->getVectorLoopRegion()
100 ? RecipeI->getIterator() != VPBB->begin()
101 : RecipeI->getIterator() != VPBB->begin() &&
102 RecipeI->getIterator() != std::next(x: VPBB->begin()))) {
103 errs() << "CurrentIteration PHI is not the first/second recipe\n";
104 return false;
105 }
106
107 // Check if the recipe operands match the number of predecessors.
108 // TODO Extend to other phi-like recipes.
109 if (auto *PhiIRI = dyn_cast<VPIRPhi>(Val: &*RecipeI)) {
110 if (PhiIRI->getNumOperands() != VPBB->getNumPredecessors()) {
111 errs() << "Phi-like recipe with different number of operands and "
112 "predecessors.\n";
113 // TODO: Print broken recipe. At the moment printing an ill-formed
114 // phi-like recipe may crash.
115 return false;
116 }
117 }
118
119 RecipeI++;
120 }
121
122 if (!VPBB->getPlan()->isUnrolled() && NumActiveLaneMaskPhiRecipes > 1) {
123 errs() << "There should be no more than one VPActiveLaneMaskPHIRecipe";
124 return false;
125 }
126
127 while (RecipeI != End) {
128 if (RecipeI->isPhi() && !isa<VPBlendRecipe>(Val: &*RecipeI)) {
129 errs() << "Found phi-like recipe after non-phi recipe";
130
131#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 errs() << ": ";
133 RecipeI->dump();
134 errs() << "after\n";
135 std::prev(RecipeI)->dump();
136#endif
137 return false;
138 }
139 RecipeI++;
140 }
141 return true;
142}
143
144static bool isKnownMonotonic(VPValue *V) {
145 VPValue *X, *Y;
146 if (match(V, P: m_Add(Op0: m_VPValue(V&: X), Op1: m_VPValue(V&: Y))))
147 return cast<VPRecipeWithIRFlags>(Val: V)->hasNoUnsignedWrap() &&
148 isKnownMonotonic(V: X) && isKnownMonotonic(V: Y);
149 if (match(V, P: m_StepVector()))
150 return true;
151 // Only handle a subset of IVs until we can guarantee there's no overflow.
152 if (auto *WidenIV = dyn_cast<VPWidenIntOrFpInductionRecipe>(Val: V))
153 return WidenIV->isCanonical() || WidenIV->hasNoUnsignedWrap();
154 if (auto *Steps = dyn_cast<VPScalarIVStepsRecipe>(Val: V))
155 return match(V: Steps->getOperand(N: 0),
156 P: m_CombineOr(
157 Ps: m_CanonicalIV(),
158 Ps: m_DerivedIV(Op0: m_ZeroInt(), Op1: m_CanonicalIV(), Op2: m_One()))) &&
159 match(V: Steps->getStepValue(), P: m_One());
160 if (isa<VPWidenCanonicalIVRecipe>(Val: V))
161 return true;
162 return vputils::isUniformAcrossVFsAndUFs(V);
163}
164
165bool VPlanVerifier::verifyLastActiveLaneRecipe(
166 const VPInstruction &LastActiveLane) const {
167 assert(LastActiveLane.getOpcode() == VPInstruction::LastActiveLane &&
168 "must be called with VPInstruction::LastActiveLane");
169
170 if (LastActiveLane.getNumOperands() < 1) {
171 errs() << "LastActiveLane must have at least one operand\n";
172 return false;
173 }
174
175 // All operands must be prefix-mask. This means an icmp ult/ule LHS, RHS where
176 // the LHS is monotonically increasing and RHS is uniform across VFs and UF.
177 for (VPValue *Op : LastActiveLane.operands()) {
178 VPValue *Mask = Op;
179 VPValue *HeaderMask;
180
181 // Look through any `and`s with the incoming alias mask or a
182 // loop_dependence_war_mask, which are always prefix masks.
183 // TODO: Verify the full loop.dependence.mask chain.
184 if (match(V: Op,
185 P: m_c_BinaryAnd(
186 Op0: m_VPValue(V&: HeaderMask),
187 Op1: m_CombineOr(
188 Ps: m_c_BinaryAnd(
189 Op0: m_Intrinsic<Intrinsic::loop_dependence_war_mask>(),
190 Op1: m_VPValue()),
191 Ps: m_Intrinsic<Intrinsic::loop_dependence_war_mask>(),
192 Ps: m_VPInstruction<VPInstruction::IncomingAliasMask>()))))
193 Mask = HeaderMask;
194
195 // The header mask is a prefix mask. Before being materialized it is the
196 // loop region's abstract header mask; afterwards it is an active lane mask
197 // (an intrinsic, phi, or an extract from a phi), or the icmp checked below.
198 if (match(V: Mask, P: m_CombineOr(
199 Ps: m_HeaderMask(), Ps: m_Isa<VPActiveLaneMaskPHIRecipe>(),
200 Ps: m_VPInstruction<VPInstruction::ActiveLaneMask>(),
201 Ps: m_ExtractVectorForPart(
202 Op: m_Isa<VPActiveLaneMaskPHIRecipe>(), Op: m_VPValue()))))
203 continue;
204
205 CmpPredicate Pred;
206 VPValue *LHS, *RHS;
207 if (match(V: Mask, P: m_ICmp(Pred, Op0: m_VPValue(V&: LHS), Op1: m_VPValue(V&: RHS))) &&
208 (Pred == CmpInst::ICMP_ULE || Pred == CmpInst::ICMP_ULT) &&
209 isKnownMonotonic(V: LHS) &&
210 (vputils::isUniformAcrossVFsAndUFs(V: RHS) ||
211 match(V: RHS, P: m_EVL(Op0: m_VPValue()))))
212 continue;
213
214 errs() << "LastActiveLane operand ";
215#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
216 VPSlotTracker Tracker(LastActiveLane.getParent()->getPlan());
217 Op->printAsOperand(errs(), Tracker);
218#endif
219 errs() << " must be prefix mask (a header mask or an "
220 "EVL-derived mask currently)\n";
221 return false;
222 }
223
224 return true;
225}
226
227bool VPlanVerifier::verifyVPBasicBlock(const VPBasicBlock *VPBB) {
228 if (!verifyPhiRecipes(VPBB))
229 return false;
230
231 // Verify that defs in VPBB dominate all their uses.
232 DenseMap<const VPRecipeBase *, unsigned> RecipeNumbering;
233 unsigned Cnt = 0;
234 for (const VPRecipeBase &R : *VPBB)
235 RecipeNumbering[&R] = Cnt++;
236
237 for (const VPRecipeBase &R : *VPBB) {
238 if (isa<VPIRInstruction>(Val: &R) && !isa<VPIRBasicBlock>(Val: VPBB)) {
239 errs() << "VPIRInstructions ";
240#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
241 R.dump();
242 errs() << " ";
243#endif
244 errs() << "not in a VPIRBasicBlock!\n";
245 return false;
246 }
247 for (const VPValue *V : R.definedValues()) {
248 // Verify that each defined value has a scalar type.
249 if (!V->getScalarType()) {
250 errs() << "VPValue without scalar type!\n";
251 return false;
252 }
253
254 for (const VPUser *U : V->users()) {
255 auto *UI = cast<VPRecipeBase>(Val: U);
256 if (isa<VPIRPhi>(Val: UI) &&
257 UI->getNumOperands() != UI->getParent()->getNumPredecessors()) {
258 errs() << "Phi-like recipe with different number of operands and "
259 "predecessors.\n";
260 return false;
261 }
262
263 if (auto *Phi = dyn_cast<VPPhiAccessors>(Val: UI)) {
264 for (const auto &[IncomingVPV, IncomingVPBB] :
265 Phi->incoming_values_and_blocks()) {
266 if (IncomingVPV != V)
267 continue;
268
269 if (VPDT.dominates(A: VPBB, B: IncomingVPBB))
270 continue;
271
272 errs() << "Incoming def does not dominate incoming block!\n";
273#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
274 VPSlotTracker Tracker(VPBB->getPlan());
275 IncomingVPV->getDefiningRecipe()->print(errs(), " ", Tracker);
276 errs() << "\n does not dominate " << IncomingVPBB->getName()
277 << " for\n";
278 UI->print(errs(), " ", Tracker);
279#endif
280 return false;
281 }
282 continue;
283 }
284 // TODO: Also verify VPPredInstPHIRecipe.
285 if (isa<VPPredInstPHIRecipe>(Val: UI))
286 continue;
287
288 // If the user is in the same block, check it comes after R in the
289 // block.
290 if (UI->getParent() == VPBB) {
291 if (RecipeNumbering[UI] >= RecipeNumbering[&R])
292 continue;
293 } else {
294 // MaskedCond may be used from blocks it don't dominate; the block
295 // will be linearized and it will dominate its users after
296 // linearization.
297 if (match(V: &R, P: m_VPInstruction<VPInstruction::MaskedCond>()) ||
298 VPDT.dominates(A: VPBB, B: UI->getParent()))
299 continue;
300 }
301
302 // Recipes in blocks with a MaskedCond may be used in exit blocks; the
303 // block will be linearized and its recipes will dominate their users
304 // after linearization.
305 bool BlockHasMaskedCond = any_of(Range: *VPBB, P: [](const VPRecipeBase &R) {
306 return match(V: &R, P: m_VPInstruction<VPInstruction::MaskedCond>());
307 });
308 if (BlockHasMaskedCond &&
309 any_of(Range: VPBB->getPlan()->getExitBlocks(), P: [UI](VPIRBasicBlock *EB) {
310 return is_contained(Range&: EB->getPredecessors(), Element: UI->getParent());
311 })) {
312 continue;
313 }
314
315 errs() << "Use before def!\n";
316#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
317 VPSlotTracker Tracker(VPBB->getPlan());
318 UI->print(errs(), " ", Tracker);
319 errs() << "\n before\n";
320 R.print(errs(), " ", Tracker);
321 errs() << "\n";
322#endif
323 return false;
324 }
325 }
326 if (const auto *VPI = dyn_cast<VPInstruction>(Val: &R)) {
327 switch (VPI->getOpcode()) {
328 case VPInstruction::LastActiveLane:
329 if (!verifyLastActiveLaneRecipe(LastActiveLane: *VPI))
330 return false;
331 break;
332 default:
333 break;
334 }
335 }
336 if (const auto *DIV = dyn_cast<VPDerivedIVRecipe>(Val: &R)) {
337 if (!DIV->getStartValue()->isDefinedOutsideLoopRegions()) {
338 errs() << "VPDerivedIVRecipe must have start value defined outside "
339 "loop regions\n";
340 return false;
341 }
342 }
343 if (const auto *ScalarIVSteps = dyn_cast<VPScalarIVStepsRecipe>(Val: &R)) {
344 unsigned NumOps = ScalarIVSteps->getNumOperands();
345 if (NumOps != 3 && NumOps != 4) {
346 errs() << "VPScalarIVStepsRecipe must have 3 or 4 operands\n";
347 return false;
348 }
349 }
350 }
351
352 auto *IRBB = dyn_cast<VPIRBasicBlock>(Val: VPBB);
353 if (!IRBB)
354 return true;
355
356 if (!WrappedIRBBs.insert(Ptr: IRBB->getIRBasicBlock()).second) {
357 errs() << "Same IR basic block used by multiple wrapper blocks!\n";
358 return false;
359 }
360
361 return true;
362}
363
364bool VPlanVerifier::verifyBlock(const VPBlockBase *VPB) {
365 auto *VPBB = dyn_cast<VPBasicBlock>(Val: VPB);
366 // Check block's condition bit.
367 if (VPBB && !isa<VPIRBasicBlock>(Val: VPB)) {
368 // For plain CFG VPlans, verify header and latch block structure.
369 if (!VPBB->getParent()) {
370 if (VPBlockUtils::isHeader(VPB: VPBB, VPDT)) {
371 if (VPB->getNumPredecessors() != 2) {
372 errs()
373 << "Header block in plain CFG VPlan must have 2 predecessors!\n";
374 return false;
375 }
376 // Predecessor 0 is preheader, predecessor 1 is latch.
377 if (!VPBlockUtils::isLatch(VPB: VPB->getPredecessors()[1], VPDT)) {
378 errs() << "Header's second predecessor must be the latch!\n";
379 return false;
380 }
381 }
382
383 if (VPBlockUtils::isLatch(VPB: VPBB, VPDT)) {
384 if (!match(V: VPBB->getTerminator(), P: m_Branch())) {
385 errs() << "Latch block must have a branch terminator!\n";
386 return false;
387 }
388 // Successor 0 is middle block, successor 1 is header.
389 if (VPBlockUtils::isHeader(VPB: VPB->getSuccessors()[0], VPDT)) {
390 errs() << "Latch's first successor must not be the header (must be "
391 "middle block)!\n";
392 return false;
393 }
394 }
395 } else if (VPB->getNumSuccessors() > 1 ||
396 (VPBB->isExiting() && !VPBB->getParent()->isReplicator())) {
397 if (!VPBB->getTerminator()) {
398 errs() << "Block has multiple successors but doesn't "
399 "have a proper branch recipe!\n";
400 return false;
401 }
402 } else if (VPBB->getTerminator()) {
403 errs() << "Unexpected branch recipe!\n";
404 return false;
405 }
406 }
407
408 // Check block's successors.
409 const auto &Successors = VPB->getSuccessors();
410 for (const VPBlockBase *Succ : Successors) {
411 // There must be a bi-directional link between block and successor.
412 const auto &SuccPreds = Succ->getPredecessors();
413 if (!is_contained(Range: SuccPreds, Element: VPB)) {
414 errs() << "Missing predecessor link.\n";
415 return false;
416 }
417 }
418
419 // Check block's predecessors.
420 const auto &Predecessors = VPB->getPredecessors();
421
422 for (const VPBlockBase *Pred : Predecessors) {
423 // Block and predecessor must be inside the same region.
424 if (Pred->getParent() != VPB->getParent()) {
425 errs() << "Predecessor is not in the same region.\n";
426 return false;
427 }
428
429 // There must be a bi-directional link between block and predecessor.
430 const auto &PredSuccs = Pred->getSuccessors();
431 if (!is_contained(Range: PredSuccs, Element: VPB)) {
432 errs() << "Missing successor link.\n";
433 return false;
434 }
435 }
436 return !VPBB || verifyVPBasicBlock(VPBB);
437}
438
439bool VPlanVerifier::verifyBlocksInRegion(const VPRegionBlock *Region) {
440 for (const VPBlockBase *VPB : vp_depth_first_shallow(G: Region->getEntry())) {
441 // Check block's parent.
442 if (VPB->getParent() != Region) {
443 errs() << "VPBlockBase has wrong parent\n";
444 return false;
445 }
446
447 if (!verifyBlock(VPB))
448 return false;
449 }
450 return true;
451}
452
453bool VPlanVerifier::verifyRegion(const VPRegionBlock *Region) {
454 const VPBlockBase *Entry = Region->getEntry();
455 const VPBlockBase *Exiting = Region->getExiting();
456
457 // Entry and Exiting shouldn't have any predecessor/successor, respectively.
458 if (Entry->hasPredecessors()) {
459 errs() << "region entry block has predecessors\n";
460 return false;
461 }
462 if (Exiting->getNumSuccessors() != 0) {
463 errs() << "region exiting block has successors\n";
464 return false;
465 }
466
467 return verifyBlocksInRegion(Region);
468}
469
470bool VPlanVerifier::verifyRegionRec(const VPRegionBlock *Region) {
471 // Recurse inside nested regions and check all blocks inside the region.
472 return verifyRegion(Region) &&
473 all_of(Range: vp_depth_first_shallow(G: Region->getEntry()),
474 P: [this](const VPBlockBase *VPB) {
475 const auto *SubRegion = dyn_cast<VPRegionBlock>(Val: VPB);
476 return !SubRegion || verifyRegionRec(Region: SubRegion);
477 });
478}
479
480bool VPlanVerifier::verify(const VPlan &Plan) {
481 if (any_of(Range: vp_depth_first_shallow(G: Plan.getEntry()),
482 P: [this](const VPBlockBase *VPB) { return !verifyBlock(VPB); }))
483 return false;
484
485 // Check that the plan has a single loop region reachable from entry, and it
486 // matches the one returned by getVectorLoopRegion.
487 const VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
488 if (any_of(Range: VPBlockUtils::blocksOnly<const VPRegionBlock>(
489 Range: vp_depth_first_shallow(G: Plan.getEntry())),
490 P: [TopRegion](const VPRegionBlock *R) {
491 return !R->isReplicator() && R != TopRegion;
492 })) {
493 errs() << "VPlan must have a single top-level loop region, reachable from "
494 "the entry by following the last successor of each block\n";
495 return false;
496 }
497
498 // TODO: Verify all blocks using vp_depth_first_deep iterators.
499 if (!TopRegion)
500 return true;
501
502 if (!verifyRegionRec(Region: TopRegion))
503 return false;
504
505 if (TopRegion->getParent()) {
506 errs() << "VPlan Top Region should have no parent.\n";
507 return false;
508 }
509
510 const VPBasicBlock *Entry = dyn_cast<VPBasicBlock>(Val: TopRegion->getEntry());
511 if (!Entry) {
512 errs() << "VPlan entry block is not a VPBasicBlock\n";
513 return false;
514 }
515
516 const VPBasicBlock *Exiting = dyn_cast<VPBasicBlock>(Val: TopRegion->getExiting());
517 if (!Exiting) {
518 errs() << "VPlan exiting block is not a VPBasicBlock\n";
519 return false;
520 }
521
522 if (Exiting->empty()) {
523 errs() << "VPlan vector loop exiting block must end with BranchOnCount, "
524 "BranchOnCond, or BranchOnTwoConds VPInstruction but is empty\n";
525 return false;
526 }
527
528 auto *LastInst = dyn_cast<VPInstruction>(Val: std::prev(x: Exiting->end()));
529 if (!match(V: LastInst, P: m_Branch())) {
530 errs() << "VPlan vector loop exit must end with BranchOnCount, "
531 "BranchOnCond, or BranchOnTwoConds VPInstruction\n";
532 return false;
533 }
534
535 return true;
536}
537
538bool llvm::verifyVPlanIsValid(const VPlan &Plan) {
539 // The entry must be the root of the plan's top-level CFG: the dominator tree
540 // constructed below and the verifier's block walks all start there.
541 if (Plan.getEntry()->hasPredecessors()) {
542 errs() << "VPlan entry block has predecessors\n";
543 return false;
544 }
545
546 VPDominatorTree VPDT(const_cast<VPlan &>(Plan));
547 VPlanVerifier Verifier(VPDT);
548 return Verifier.verify(Plan);
549}
550