Skip to content

Commit 1794046

Browse files
committed
[VPlan] Move verifier to class to reduce need to pass via args. (NFC)
Move VPlan verification functions to avoid the need to pass VPDT across multiple calls. This also allows easier extensions in the future.
1 parent 815250b commit 1794046

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed

llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,41 @@
2323

2424
using namespace llvm;
2525

26-
// Verify that phi-like recipes are at the beginning of \p VPBB, with no
27-
// other recipes in between. Also check that only header blocks contain
28-
// VPHeaderPHIRecipes.
29-
static bool verifyPhiRecipes(const VPBasicBlock *VPBB) {
26+
namespace {
27+
class VPlanVerifier {
28+
const VPDominatorTree &VPDT;
29+
30+
// Verify that phi-like recipes are at the beginning of \p VPBB, with no
31+
// other recipes in between. Also check that only header blocks contain
32+
// VPHeaderPHIRecipes.
33+
bool verifyPhiRecipes(const VPBasicBlock *VPBB);
34+
35+
bool verifyVPBasicBlock(const VPBasicBlock *VPBB);
36+
37+
bool verifyBlock(const VPBlockBase *VPB);
38+
39+
/// Helper function that verifies the CFG invariants of the VPBlockBases
40+
/// within
41+
/// \p Region. Checks in this function are generic for VPBlockBases. They are
42+
/// not specific for VPBasicBlocks or VPRegionBlocks.
43+
bool verifyBlocksInRegion(const VPRegionBlock *Region);
44+
45+
/// Verify the CFG invariants of VPRegionBlock \p Region and its nested
46+
/// VPBlockBases. Do not recurse inside nested VPRegionBlocks.
47+
bool verifyRegion(const VPRegionBlock *Region);
48+
49+
/// Verify the CFG invariants of VPRegionBlock \p Region and its nested
50+
/// VPBlockBases. Recurse inside nested VPRegionBlocks.
51+
bool verifyRegionRec(const VPRegionBlock *Region);
52+
53+
public:
54+
VPlanVerifier(VPDominatorTree &VPDT) : VPDT(VPDT) {}
55+
56+
bool verify(const VPlan &Plan);
57+
};
58+
} // namespace
59+
60+
bool VPlanVerifier::verifyPhiRecipes(const VPBasicBlock *VPBB) {
3061
auto RecipeI = VPBB->begin();
3162
auto End = VPBB->end();
3263
unsigned NumActiveLaneMaskPhiRecipes = 0;
@@ -80,8 +111,7 @@ static bool verifyPhiRecipes(const VPBasicBlock *VPBB) {
80111
return true;
81112
}
82113

83-
static bool verifyVPBasicBlock(const VPBasicBlock *VPBB,
84-
const VPDominatorTree &VPDT) {
114+
bool VPlanVerifier::verifyVPBasicBlock(const VPBasicBlock *VPBB) {
85115
if (!verifyPhiRecipes(VPBB))
86116
return false;
87117

@@ -133,7 +163,7 @@ static bool hasDuplicates(const SmallVectorImpl<VPBlockBase *> &VPBlockVec) {
133163
return false;
134164
}
135165

136-
static bool verifyBlock(const VPBlockBase *VPB, const VPDominatorTree &VPDT) {
166+
bool VPlanVerifier::verifyBlock(const VPBlockBase *VPB) {
137167
auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
138168
// Check block's condition bit.
139169
if (VPB->getNumSuccessors() > 1 ||
@@ -193,31 +223,24 @@ static bool verifyBlock(const VPBlockBase *VPB, const VPDominatorTree &VPDT) {
193223
return false;
194224
}
195225
}
196-
return !VPBB || verifyVPBasicBlock(VPBB, VPDT);
226+
return !VPBB || verifyVPBasicBlock(VPBB);
197227
}
198228

199-
/// Helper function that verifies the CFG invariants of the VPBlockBases within
200-
/// \p Region. Checks in this function are generic for VPBlockBases. They are
201-
/// not specific for VPBasicBlocks or VPRegionBlocks.
202-
static bool verifyBlocksInRegion(const VPRegionBlock *Region,
203-
const VPDominatorTree &VPDT) {
229+
bool VPlanVerifier::verifyBlocksInRegion(const VPRegionBlock *Region) {
204230
for (const VPBlockBase *VPB : vp_depth_first_shallow(Region->getEntry())) {
205231
// Check block's parent.
206232
if (VPB->getParent() != Region) {
207233
errs() << "VPBlockBase has wrong parent\n";
208234
return false;
209235
}
210236

211-
if (!verifyBlock(VPB, VPDT))
237+
if (!verifyBlock(VPB))
212238
return false;
213239
}
214240
return true;
215241
}
216242

217-
/// Verify the CFG invariants of VPRegionBlock \p Region and its nested
218-
/// VPBlockBases. Do not recurse inside nested VPRegionBlocks.
219-
static bool verifyRegion(const VPRegionBlock *Region,
220-
const VPDominatorTree &VPDT) {
243+
bool VPlanVerifier::verifyRegion(const VPRegionBlock *Region) {
221244
const VPBlockBase *Entry = Region->getEntry();
222245
const VPBlockBase *Exiting = Region->getExiting();
223246

@@ -231,33 +254,26 @@ static bool verifyRegion(const VPRegionBlock *Region,
231254
return false;
232255
}
233256

234-
return verifyBlocksInRegion(Region, VPDT);
257+
return verifyBlocksInRegion(Region);
235258
}
236259

237-
/// Verify the CFG invariants of VPRegionBlock \p Region and its nested
238-
/// VPBlockBases. Recurse inside nested VPRegionBlocks.
239-
static bool verifyRegionRec(const VPRegionBlock *Region,
240-
const VPDominatorTree &VPDT) {
260+
bool VPlanVerifier::verifyRegionRec(const VPRegionBlock *Region) {
241261
// Recurse inside nested regions and check all blocks inside the region.
242-
return verifyRegion(Region, VPDT) &&
262+
return verifyRegion(Region) &&
243263
all_of(vp_depth_first_shallow(Region->getEntry()),
244-
[&VPDT](const VPBlockBase *VPB) {
264+
[this](const VPBlockBase *VPB) {
245265
const auto *SubRegion = dyn_cast<VPRegionBlock>(VPB);
246-
return !SubRegion || verifyRegionRec(SubRegion, VPDT);
266+
return !SubRegion || verifyRegionRec(SubRegion);
247267
});
248268
}
249269

250-
bool llvm::verifyVPlanIsValid(const VPlan &Plan) {
251-
VPDominatorTree VPDT;
252-
VPDT.recalculate(const_cast<VPlan &>(Plan));
253-
254-
if (any_of(
255-
vp_depth_first_shallow(Plan.getEntry()),
256-
[&VPDT](const VPBlockBase *VPB) { return !verifyBlock(VPB, VPDT); }))
270+
bool VPlanVerifier::verify(const VPlan &Plan) {
271+
if (any_of(vp_depth_first_shallow(Plan.getEntry()),
272+
[this](const VPBlockBase *VPB) { return !verifyBlock(VPB); }))
257273
return false;
258274

259275
const VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
260-
if (!verifyRegionRec(TopRegion, VPDT))
276+
if (!verifyRegionRec(TopRegion))
261277
return false;
262278

263279
if (TopRegion->getParent()) {
@@ -305,3 +321,10 @@ bool llvm::verifyVPlanIsValid(const VPlan &Plan) {
305321

306322
return true;
307323
}
324+
325+
bool llvm::verifyVPlanIsValid(const VPlan &Plan) {
326+
VPDominatorTree VPDT;
327+
VPDT.recalculate(const_cast<VPlan &>(Plan));
328+
VPlanVerifier Verifier(VPDT);
329+
return Verifier.verify(Plan);
330+
}

0 commit comments

Comments
 (0)