VPlanVerifier.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include "VPlanVerifier.h"
  15. #include "VPlan.h"
  16. #include "VPlanCFG.h"
  17. #include "llvm/ADT/DepthFirstIterator.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #define DEBUG_TYPE "loop-vectorize"
  20. using namespace llvm;
  21. static cl::opt<bool> EnableHCFGVerifier("vplan-verify-hcfg", cl::init(false),
  22. cl::Hidden,
  23. cl::desc("Verify VPlan H-CFG."));
  24. #ifndef NDEBUG
  25. /// Utility function that checks whether \p VPBlockVec has duplicate
  26. /// VPBlockBases.
  27. static bool hasDuplicates(const SmallVectorImpl<VPBlockBase *> &VPBlockVec) {
  28. SmallDenseSet<const VPBlockBase *, 8> VPBlockSet;
  29. for (const auto *Block : VPBlockVec) {
  30. if (VPBlockSet.count(Block))
  31. return true;
  32. VPBlockSet.insert(Block);
  33. }
  34. return false;
  35. }
  36. #endif
  37. /// Helper function that verifies the CFG invariants of the VPBlockBases within
  38. /// \p Region. Checks in this function are generic for VPBlockBases. They are
  39. /// not specific for VPBasicBlocks or VPRegionBlocks.
  40. static void verifyBlocksInRegion(const VPRegionBlock *Region) {
  41. for (const VPBlockBase *VPB : vp_depth_first_shallow(Region->getEntry())) {
  42. // Check block's parent.
  43. assert(VPB->getParent() == Region && "VPBlockBase has wrong parent");
  44. auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
  45. // Check block's condition bit.
  46. if (VPB->getNumSuccessors() > 1 || (VPBB && VPBB->isExiting()))
  47. assert(VPBB && VPBB->getTerminator() &&
  48. "Block has multiple successors but doesn't "
  49. "have a proper branch recipe!");
  50. else
  51. assert((!VPBB || !VPBB->getTerminator()) && "Unexpected branch recipe!");
  52. // Check block's successors.
  53. const auto &Successors = VPB->getSuccessors();
  54. // There must be only one instance of a successor in block's successor list.
  55. // TODO: This won't work for switch statements.
  56. assert(!hasDuplicates(Successors) &&
  57. "Multiple instances of the same successor.");
  58. for (const VPBlockBase *Succ : Successors) {
  59. // There must be a bi-directional link between block and successor.
  60. const auto &SuccPreds = Succ->getPredecessors();
  61. assert(llvm::is_contained(SuccPreds, VPB) && "Missing predecessor link.");
  62. (void)SuccPreds;
  63. }
  64. // Check block's predecessors.
  65. const auto &Predecessors = VPB->getPredecessors();
  66. // There must be only one instance of a predecessor in block's predecessor
  67. // list.
  68. // TODO: This won't work for switch statements.
  69. assert(!hasDuplicates(Predecessors) &&
  70. "Multiple instances of the same predecessor.");
  71. for (const VPBlockBase *Pred : Predecessors) {
  72. // Block and predecessor must be inside the same region.
  73. assert(Pred->getParent() == VPB->getParent() &&
  74. "Predecessor is not in the same region.");
  75. // There must be a bi-directional link between block and predecessor.
  76. const auto &PredSuccs = Pred->getSuccessors();
  77. assert(llvm::is_contained(PredSuccs, VPB) && "Missing successor link.");
  78. (void)PredSuccs;
  79. }
  80. }
  81. }
  82. /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
  83. /// VPBlockBases. Do not recurse inside nested VPRegionBlocks.
  84. static void verifyRegion(const VPRegionBlock *Region) {
  85. const VPBlockBase *Entry = Region->getEntry();
  86. const VPBlockBase *Exiting = Region->getExiting();
  87. // Entry and Exiting shouldn't have any predecessor/successor, respectively.
  88. assert(!Entry->getNumPredecessors() && "Region entry has predecessors.");
  89. assert(!Exiting->getNumSuccessors() &&
  90. "Region exiting block has successors.");
  91. (void)Entry;
  92. (void)Exiting;
  93. verifyBlocksInRegion(Region);
  94. }
  95. /// Verify the CFG invariants of VPRegionBlock \p Region and its nested
  96. /// VPBlockBases. Recurse inside nested VPRegionBlocks.
  97. static void verifyRegionRec(const VPRegionBlock *Region) {
  98. verifyRegion(Region);
  99. // Recurse inside nested regions.
  100. for (const VPBlockBase *VPB : make_range(
  101. df_iterator<const VPBlockBase *>::begin(Region->getEntry()),
  102. df_iterator<const VPBlockBase *>::end(Region->getExiting()))) {
  103. if (const auto *SubRegion = dyn_cast<VPRegionBlock>(VPB))
  104. verifyRegionRec(SubRegion);
  105. }
  106. }
  107. void VPlanVerifier::verifyHierarchicalCFG(
  108. const VPRegionBlock *TopRegion) const {
  109. if (!EnableHCFGVerifier)
  110. return;
  111. LLVM_DEBUG(dbgs() << "Verifying VPlan H-CFG.\n");
  112. assert(!TopRegion->getParent() && "VPlan Top Region should have no parent.");
  113. verifyRegionRec(TopRegion);
  114. }
  115. // Verify that phi-like recipes are at the beginning of \p VPBB, with no
  116. // other recipes in between. Also check that only header blocks contain
  117. // VPHeaderPHIRecipes.
  118. static bool verifyPhiRecipes(const VPBasicBlock *VPBB) {
  119. auto RecipeI = VPBB->begin();
  120. auto End = VPBB->end();
  121. unsigned NumActiveLaneMaskPhiRecipes = 0;
  122. const VPRegionBlock *ParentR = VPBB->getParent();
  123. bool IsHeaderVPBB = ParentR && !ParentR->isReplicator() &&
  124. ParentR->getEntryBasicBlock() == VPBB;
  125. while (RecipeI != End && RecipeI->isPhi()) {
  126. if (isa<VPActiveLaneMaskPHIRecipe>(RecipeI))
  127. NumActiveLaneMaskPhiRecipes++;
  128. if (IsHeaderVPBB && !isa<VPHeaderPHIRecipe>(*RecipeI)) {
  129. errs() << "Found non-header PHI recipe in header VPBB";
  130. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  131. errs() << ": ";
  132. RecipeI->dump();
  133. #endif
  134. return false;
  135. }
  136. if (!IsHeaderVPBB && isa<VPHeaderPHIRecipe>(*RecipeI)) {
  137. errs() << "Found header PHI recipe in non-header VPBB";
  138. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  139. errs() << ": ";
  140. RecipeI->dump();
  141. #endif
  142. return false;
  143. }
  144. RecipeI++;
  145. }
  146. if (NumActiveLaneMaskPhiRecipes > 1) {
  147. errs() << "There should be no more than one VPActiveLaneMaskPHIRecipe";
  148. return false;
  149. }
  150. while (RecipeI != End) {
  151. if (RecipeI->isPhi() && !isa<VPBlendRecipe>(&*RecipeI)) {
  152. errs() << "Found phi-like recipe after non-phi recipe";
  153. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  154. errs() << ": ";
  155. RecipeI->dump();
  156. errs() << "after\n";
  157. std::prev(RecipeI)->dump();
  158. #endif
  159. return false;
  160. }
  161. RecipeI++;
  162. }
  163. return true;
  164. }
  165. static bool
  166. verifyVPBasicBlock(const VPBasicBlock *VPBB,
  167. DenseMap<const VPBlockBase *, unsigned> &BlockNumbering) {
  168. if (!verifyPhiRecipes(VPBB))
  169. return false;
  170. // Verify that defs in VPBB dominate all their uses. The current
  171. // implementation is still incomplete.
  172. DenseMap<const VPRecipeBase *, unsigned> RecipeNumbering;
  173. unsigned Cnt = 0;
  174. for (const VPRecipeBase &R : *VPBB)
  175. RecipeNumbering[&R] = Cnt++;
  176. for (const VPRecipeBase &R : *VPBB) {
  177. for (const VPValue *V : R.definedValues()) {
  178. for (const VPUser *U : V->users()) {
  179. auto *UI = dyn_cast<VPRecipeBase>(U);
  180. if (!UI || isa<VPHeaderPHIRecipe>(UI))
  181. continue;
  182. // If the user is in the same block, check it comes after R in the
  183. // block.
  184. if (UI->getParent() == VPBB) {
  185. if (RecipeNumbering[UI] < RecipeNumbering[&R]) {
  186. errs() << "Use before def!\n";
  187. return false;
  188. }
  189. continue;
  190. }
  191. // Skip blocks outside any region for now and blocks outside
  192. // replicate-regions.
  193. auto *ParentR = VPBB->getParent();
  194. if (!ParentR || !ParentR->isReplicator())
  195. continue;
  196. // For replicators, verify that VPPRedInstPHIRecipe defs are only used
  197. // in subsequent blocks.
  198. if (isa<VPPredInstPHIRecipe>(&R)) {
  199. auto I = BlockNumbering.find(UI->getParent());
  200. unsigned BlockNumber = I == BlockNumbering.end() ? std::numeric_limits<unsigned>::max() : I->second;
  201. if (BlockNumber < BlockNumbering[ParentR]) {
  202. errs() << "Use before def!\n";
  203. return false;
  204. }
  205. continue;
  206. }
  207. // All non-VPPredInstPHIRecipe recipes in the block must be used in
  208. // the replicate region only.
  209. if (UI->getParent()->getParent() != ParentR) {
  210. errs() << "Use before def!\n";
  211. return false;
  212. }
  213. }
  214. }
  215. }
  216. return true;
  217. }
  218. bool VPlanVerifier::verifyPlanIsValid(const VPlan &Plan) {
  219. DenseMap<const VPBlockBase *, unsigned> BlockNumbering;
  220. unsigned Cnt = 0;
  221. auto Iter = vp_depth_first_deep(Plan.getEntry());
  222. for (const VPBlockBase *VPB : Iter) {
  223. BlockNumbering[VPB] = Cnt++;
  224. auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
  225. if (!VPBB)
  226. continue;
  227. if (!verifyVPBasicBlock(VPBB, BlockNumbering))
  228. return false;
  229. }
  230. const VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
  231. const VPBasicBlock *Entry = dyn_cast<VPBasicBlock>(TopRegion->getEntry());
  232. if (!Entry) {
  233. errs() << "VPlan entry block is not a VPBasicBlock\n";
  234. return false;
  235. }
  236. if (!isa<VPCanonicalIVPHIRecipe>(&*Entry->begin())) {
  237. errs() << "VPlan vector loop header does not start with a "
  238. "VPCanonicalIVPHIRecipe\n";
  239. return false;
  240. }
  241. const VPBasicBlock *Exiting = dyn_cast<VPBasicBlock>(TopRegion->getExiting());
  242. if (!Exiting) {
  243. errs() << "VPlan exiting block is not a VPBasicBlock\n";
  244. return false;
  245. }
  246. if (Exiting->empty()) {
  247. errs() << "VPlan vector loop exiting block must end with BranchOnCount or "
  248. "BranchOnCond VPInstruction but is empty\n";
  249. return false;
  250. }
  251. auto *LastInst = dyn_cast<VPInstruction>(std::prev(Exiting->end()));
  252. if (!LastInst || (LastInst->getOpcode() != VPInstruction::BranchOnCount &&
  253. LastInst->getOpcode() != VPInstruction::BranchOnCond)) {
  254. errs() << "VPlan vector loop exit must end with BranchOnCount or "
  255. "BranchOnCond VPInstruction\n";
  256. return false;
  257. }
  258. for (const VPRegionBlock *Region :
  259. VPBlockUtils::blocksOnly<const VPRegionBlock>(
  260. vp_depth_first_deep(Plan.getEntry()))) {
  261. if (Region->getEntry()->getNumPredecessors() != 0) {
  262. errs() << "region entry block has predecessors\n";
  263. return false;
  264. }
  265. if (Region->getExiting()->getNumSuccessors() != 0) {
  266. errs() << "region exiting block has successors\n";
  267. return false;
  268. }
  269. }
  270. for (const auto &KV : Plan.getLiveOuts())
  271. if (KV.second->getNumOperands() != 1) {
  272. errs() << "live outs must have a single operand\n";
  273. return false;
  274. }
  275. return true;
  276. }