Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
35266 views
//===-- VPlanVerifier.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file defines the class VPlanVerifier, which contains utility functions10/// to check the consistency and invariants of a VPlan.11///12//===----------------------------------------------------------------------===//1314#include "VPlanVerifier.h"15#include "VPlan.h"16#include "VPlanCFG.h"17#include "VPlanDominatorTree.h"18#include "llvm/ADT/DepthFirstIterator.h"19#include "llvm/ADT/SmallPtrSet.h"20#include "llvm/Support/CommandLine.h"2122#define DEBUG_TYPE "loop-vectorize"2324using namespace llvm;2526namespace {27class VPlanVerifier {28const VPDominatorTree &VPDT;2930SmallPtrSet<BasicBlock *, 8> WrappedIRBBs;3132// Verify that phi-like recipes are at the beginning of \p VPBB, with no33// other recipes in between. Also check that only header blocks contain34// VPHeaderPHIRecipes.35bool verifyPhiRecipes(const VPBasicBlock *VPBB);3637bool verifyVPBasicBlock(const VPBasicBlock *VPBB);3839bool verifyBlock(const VPBlockBase *VPB);4041/// Helper function that verifies the CFG invariants of the VPBlockBases42/// within43/// \p Region. Checks in this function are generic for VPBlockBases. They are44/// not specific for VPBasicBlocks or VPRegionBlocks.45bool verifyBlocksInRegion(const VPRegionBlock *Region);4647/// Verify the CFG invariants of VPRegionBlock \p Region and its nested48/// VPBlockBases. Do not recurse inside nested VPRegionBlocks.49bool verifyRegion(const VPRegionBlock *Region);5051/// Verify the CFG invariants of VPRegionBlock \p Region and its nested52/// VPBlockBases. Recurse inside nested VPRegionBlocks.53bool verifyRegionRec(const VPRegionBlock *Region);5455public:56VPlanVerifier(VPDominatorTree &VPDT) : VPDT(VPDT) {}5758bool verify(const VPlan &Plan);59};60} // namespace6162bool VPlanVerifier::verifyPhiRecipes(const VPBasicBlock *VPBB) {63auto RecipeI = VPBB->begin();64auto End = VPBB->end();65unsigned NumActiveLaneMaskPhiRecipes = 0;66const VPRegionBlock *ParentR = VPBB->getParent();67bool IsHeaderVPBB = ParentR && !ParentR->isReplicator() &&68ParentR->getEntryBasicBlock() == VPBB;69while (RecipeI != End && RecipeI->isPhi()) {70if (isa<VPActiveLaneMaskPHIRecipe>(RecipeI))71NumActiveLaneMaskPhiRecipes++;7273if (IsHeaderVPBB && !isa<VPHeaderPHIRecipe, VPWidenPHIRecipe>(*RecipeI)) {74errs() << "Found non-header PHI recipe in header VPBB";75#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)76errs() << ": ";77RecipeI->dump();78#endif79return false;80}8182if (!IsHeaderVPBB && isa<VPHeaderPHIRecipe>(*RecipeI)) {83errs() << "Found header PHI recipe in non-header VPBB";84#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)85errs() << ": ";86RecipeI->dump();87#endif88return false;89}9091RecipeI++;92}9394if (NumActiveLaneMaskPhiRecipes > 1) {95errs() << "There should be no more than one VPActiveLaneMaskPHIRecipe";96return false;97}9899while (RecipeI != End) {100if (RecipeI->isPhi() && !isa<VPBlendRecipe>(&*RecipeI)) {101errs() << "Found phi-like recipe after non-phi recipe";102103#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)104errs() << ": ";105RecipeI->dump();106errs() << "after\n";107std::prev(RecipeI)->dump();108#endif109return false;110}111RecipeI++;112}113return true;114}115116bool VPlanVerifier::verifyVPBasicBlock(const VPBasicBlock *VPBB) {117if (!verifyPhiRecipes(VPBB))118return false;119120// Verify that defs in VPBB dominate all their uses. The current121// implementation is still incomplete.122DenseMap<const VPRecipeBase *, unsigned> RecipeNumbering;123unsigned Cnt = 0;124for (const VPRecipeBase &R : *VPBB)125RecipeNumbering[&R] = Cnt++;126127for (const VPRecipeBase &R : *VPBB) {128for (const VPValue *V : R.definedValues()) {129for (const VPUser *U : V->users()) {130auto *UI = dyn_cast<VPRecipeBase>(U);131// TODO: check dominance of incoming values for phis properly.132if (!UI ||133isa<VPHeaderPHIRecipe, VPWidenPHIRecipe, VPPredInstPHIRecipe>(UI))134continue;135136// If the user is in the same block, check it comes after R in the137// block.138if (UI->getParent() == VPBB) {139if (RecipeNumbering[UI] < RecipeNumbering[&R]) {140errs() << "Use before def!\n";141return false;142}143continue;144}145146if (!VPDT.dominates(VPBB, UI->getParent())) {147errs() << "Use before def!\n";148return false;149}150}151}152}153154auto *IRBB = dyn_cast<VPIRBasicBlock>(VPBB);155if (!IRBB)156return true;157158if (!WrappedIRBBs.insert(IRBB->getIRBasicBlock()).second) {159errs() << "Same IR basic block used by multiple wrapper blocks!\n";160return false;161}162163VPBlockBase *MiddleBB =164IRBB->getPlan()->getVectorLoopRegion()->getSingleSuccessor();165if (IRBB != IRBB->getPlan()->getPreheader() &&166IRBB->getSinglePredecessor() != MiddleBB) {167errs() << "VPIRBasicBlock can only be used as pre-header or a successor of "168"middle-block at the moment!\n";169return false;170}171return true;172}173174/// Utility function that checks whether \p VPBlockVec has duplicate175/// VPBlockBases.176static bool hasDuplicates(const SmallVectorImpl<VPBlockBase *> &VPBlockVec) {177SmallDenseSet<const VPBlockBase *, 8> VPBlockSet;178for (const auto *Block : VPBlockVec) {179if (VPBlockSet.count(Block))180return true;181VPBlockSet.insert(Block);182}183return false;184}185186bool VPlanVerifier::verifyBlock(const VPBlockBase *VPB) {187auto *VPBB = dyn_cast<VPBasicBlock>(VPB);188// Check block's condition bit.189if (VPB->getNumSuccessors() > 1 ||190(VPBB && VPBB->getParent() && VPBB->isExiting() &&191!VPBB->getParent()->isReplicator())) {192if (!VPBB || !VPBB->getTerminator()) {193errs() << "Block has multiple successors but doesn't "194"have a proper branch recipe!\n";195return false;196}197} else {198if (VPBB && VPBB->getTerminator()) {199errs() << "Unexpected branch recipe!\n";200return false;201}202}203204// Check block's successors.205const auto &Successors = VPB->getSuccessors();206// There must be only one instance of a successor in block's successor list.207// TODO: This won't work for switch statements.208if (hasDuplicates(Successors)) {209errs() << "Multiple instances of the same successor.\n";210return false;211}212213for (const VPBlockBase *Succ : Successors) {214// There must be a bi-directional link between block and successor.215const auto &SuccPreds = Succ->getPredecessors();216if (!is_contained(SuccPreds, VPB)) {217errs() << "Missing predecessor link.\n";218return false;219}220}221222// Check block's predecessors.223const auto &Predecessors = VPB->getPredecessors();224// There must be only one instance of a predecessor in block's predecessor225// list.226// TODO: This won't work for switch statements.227if (hasDuplicates(Predecessors)) {228errs() << "Multiple instances of the same predecessor.\n";229return false;230}231232for (const VPBlockBase *Pred : Predecessors) {233// Block and predecessor must be inside the same region.234if (Pred->getParent() != VPB->getParent()) {235errs() << "Predecessor is not in the same region.\n";236return false;237}238239// There must be a bi-directional link between block and predecessor.240const auto &PredSuccs = Pred->getSuccessors();241if (!is_contained(PredSuccs, VPB)) {242errs() << "Missing successor link.\n";243return false;244}245}246return !VPBB || verifyVPBasicBlock(VPBB);247}248249bool VPlanVerifier::verifyBlocksInRegion(const VPRegionBlock *Region) {250for (const VPBlockBase *VPB : vp_depth_first_shallow(Region->getEntry())) {251// Check block's parent.252if (VPB->getParent() != Region) {253errs() << "VPBlockBase has wrong parent\n";254return false;255}256257if (!verifyBlock(VPB))258return false;259}260return true;261}262263bool VPlanVerifier::verifyRegion(const VPRegionBlock *Region) {264const VPBlockBase *Entry = Region->getEntry();265const VPBlockBase *Exiting = Region->getExiting();266267// Entry and Exiting shouldn't have any predecessor/successor, respectively.268if (Entry->getNumPredecessors() != 0) {269errs() << "region entry block has predecessors\n";270return false;271}272if (Exiting->getNumSuccessors() != 0) {273errs() << "region exiting block has successors\n";274return false;275}276277return verifyBlocksInRegion(Region);278}279280bool VPlanVerifier::verifyRegionRec(const VPRegionBlock *Region) {281// Recurse inside nested regions and check all blocks inside the region.282return verifyRegion(Region) &&283all_of(vp_depth_first_shallow(Region->getEntry()),284[this](const VPBlockBase *VPB) {285const auto *SubRegion = dyn_cast<VPRegionBlock>(VPB);286return !SubRegion || verifyRegionRec(SubRegion);287});288}289290bool VPlanVerifier::verify(const VPlan &Plan) {291if (any_of(vp_depth_first_shallow(Plan.getEntry()),292[this](const VPBlockBase *VPB) { return !verifyBlock(VPB); }))293return false;294295const VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();296if (!verifyRegionRec(TopRegion))297return false;298299if (TopRegion->getParent()) {300errs() << "VPlan Top Region should have no parent.\n";301return false;302}303304const VPBasicBlock *Entry = dyn_cast<VPBasicBlock>(TopRegion->getEntry());305if (!Entry) {306errs() << "VPlan entry block is not a VPBasicBlock\n";307return false;308}309310if (!isa<VPCanonicalIVPHIRecipe>(&*Entry->begin())) {311errs() << "VPlan vector loop header does not start with a "312"VPCanonicalIVPHIRecipe\n";313return false;314}315316const VPBasicBlock *Exiting = dyn_cast<VPBasicBlock>(TopRegion->getExiting());317if (!Exiting) {318errs() << "VPlan exiting block is not a VPBasicBlock\n";319return false;320}321322if (Exiting->empty()) {323errs() << "VPlan vector loop exiting block must end with BranchOnCount or "324"BranchOnCond VPInstruction but is empty\n";325return false;326}327328auto *LastInst = dyn_cast<VPInstruction>(std::prev(Exiting->end()));329if (!LastInst || (LastInst->getOpcode() != VPInstruction::BranchOnCount &&330LastInst->getOpcode() != VPInstruction::BranchOnCond)) {331errs() << "VPlan vector loop exit must end with BranchOnCount or "332"BranchOnCond VPInstruction\n";333return false;334}335336for (const auto &KV : Plan.getLiveOuts())337if (KV.second->getNumOperands() != 1) {338errs() << "live outs must have a single operand\n";339return false;340}341342return true;343}344345bool llvm::verifyVPlanIsValid(const VPlan &Plan) {346VPDominatorTree VPDT;347VPDT.recalculate(const_cast<VPlan &>(Plan));348VPlanVerifier Verifier(VPDT);349return Verifier.verify(Plan);350}351352353