Skip to content

Commit 08664d0

Browse files
committed
[Verifier] Speed up and parallelize dominance checking. NFC
One of the key algorithms used in the "mlir::verify(op)" method is the dominance checker, which ensures that operand values properly dominate the operations that use them. The MLIR dominance implementation has a number of algorithmic problems, and is not really set up in general to answer dense queries: it's constant factors are really slow with multiple map lookups and scans, even in the easy cases. Furthermore, when calling mlir::verify(module) or some other high level operation, it makes sense to parallelize the dominator verification of all the functions within the module. This patch has a few changes to enact this: 1) It splits dominance checking into "IsolatedFromAbove" units. Instead of building a monolithic DominanceInfo for everything in a module, for example, it checks dominance for the module to all the functions within it (noop, since there are no operands at this level) then each function gets their own DominanceInfo for each of their scope. 2) It adds the ability for mlir::DominanceInfo (and post dom) to be constrained to an IsolatedFromAbove region. There is no reason to recurse into IsolatedFromAbove regions since use/def relationships can't span this region anyway. This is already checked by the time the verifier gets here. 3) It avoids querying DominanceInfo for trivial checks (e.g. intra Block references) to eliminate constant factor issues). 4) It switches to lazily constructing DominanceInfo because the trivial check case handles the vast majority of the cases and avoids constructing DominanceInfo entirely in some cases (e.g. at the module level or for many Regions's that contain a single Block). 5) It parallelizes analysis of collections IsolatedFromAbove operations, e.g. each of the functions within a Module. All together this is more than a 10% speedup on `firtool` in circt on a large design when run in -verify-each mode (our default) since the verifier is invoked after each pass. Still todo is to parallelize the main verifier pass. I decided to split this out to its own thing since this patch is already large-ish. Differential Revision: https://reviews.llvm.org/D103373
1 parent 8b4c80d commit 08664d0

File tree

2 files changed

+82
-33
lines changed

2 files changed

+82
-33
lines changed

mlir/include/mlir/IR/Dominance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DominanceInfoBase {
3535
using DomTree = llvm::DominatorTreeBase<Block, IsPostDom>;
3636

3737
public:
38-
DominanceInfoBase(Operation *op) {}
38+
DominanceInfoBase(Operation *op = nullptr) {}
3939
DominanceInfoBase(DominanceInfoBase &&) = default;
4040
DominanceInfoBase &operator=(DominanceInfoBase &&) = default;
4141
~DominanceInfoBase();

mlir/lib/IR/Verifier.cpp

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131
#include "mlir/IR/Operation.h"
3232
#include "mlir/IR/RegionKindInterface.h"
3333
#include "llvm/ADT/StringMap.h"
34-
#include "llvm/Support/FormatVariadic.h"
34+
#include "llvm/Support/Parallel.h"
3535
#include "llvm/Support/PrettyStackTrace.h"
36-
#include "llvm/Support/Regex.h"
3736

3837
using namespace mlir;
3938

4039
namespace {
4140
/// This class encapsulates all the state used to verify an operation region.
4241
class OperationVerifier {
4342
public:
44-
explicit OperationVerifier() {}
43+
explicit OperationVerifier(MLIRContext *context)
44+
: parallelismEnabled(context->isMultithreadingEnabled()) {}
4545

4646
/// Verify the given operation.
4747
LogicalResult verifyOpAndDominance(Operation &op);
@@ -54,7 +54,8 @@ class OperationVerifier {
5454

5555
/// Verify the dominance property of regions contained within the given
5656
/// Operation.
57-
LogicalResult verifyDominanceOfContainedRegions(Operation &op);
57+
LogicalResult verifyDominanceOfContainedRegions(Operation &op,
58+
DominanceInfo &domInfo);
5859

5960
/// Emit an error for the given block.
6061
InFlightDiagnostic emitError(Block &bb, const Twine &message) {
@@ -66,8 +67,8 @@ class OperationVerifier {
6667
return mlir::emitError(bb.getParent()->getLoc(), message);
6768
}
6869

69-
/// Dominance information for this operation, when checking dominance.
70-
DominanceInfo *domInfo = nullptr;
70+
/// This is true if parallelism is enabled on the MLIRContext.
71+
const bool parallelismEnabled;
7172
};
7273
} // end anonymous namespace
7374

@@ -81,12 +82,11 @@ LogicalResult OperationVerifier::verifyOpAndDominance(Operation &op) {
8182
// check for any nested regions. We do this as a second pass since malformed
8283
// CFG's can cause dominator analysis constructure to crash and we want the
8384
// verifier to be resilient to malformed code.
84-
DominanceInfo theDomInfo(&op);
85-
domInfo = &theDomInfo;
86-
if (failed(verifyDominanceOfContainedRegions(op)))
87-
return failure();
88-
89-
domInfo = nullptr;
85+
if (op.getNumRegions() != 0) {
86+
DominanceInfo domInfo;
87+
if (failed(verifyDominanceOfContainedRegions(op, /*domInfo*/ domInfo)))
88+
return failure();
89+
}
9090
return success();
9191
}
9292

@@ -299,34 +299,83 @@ static void diagnoseInvalidOperandDominance(Operation &op, unsigned operandNo) {
299299
note << " neither in a parent nor in a child region)";
300300
}
301301

302-
/// Verify the dominance of each of the nested blocks within the given operation
302+
/// Verify the dominance of each of the nested blocks within the given
303+
/// operation. domInfo may be present or absent (null), depending on whether
304+
/// the caller computed it for a higher level.
303305
LogicalResult
304-
OperationVerifier::verifyDominanceOfContainedRegions(Operation &op) {
305-
for (Region &region : op.getRegions()) {
306-
// Verify the dominance of each of the held operations.
306+
OperationVerifier::verifyDominanceOfContainedRegions(Operation &opWithRegions,
307+
DominanceInfo &domInfo) {
308+
// This vector keeps track of ops that have regions which should be checked
309+
// in parallel.
310+
SmallVector<Operation *> opsWithRegionsToCheckInParallel;
311+
312+
// Get information about the requirements on the regions in this op.
313+
for (Region &region : opWithRegions.getRegions()) {
307314
for (Block &block : region) {
308315
// Dominance is only meaningful inside reachable blocks.
309-
bool isReachable = domInfo->isReachableFromEntry(&block);
316+
bool isReachable = domInfo.isReachableFromEntry(&block);
317+
318+
// Check each operation in this block, and any operations in regions
319+
// that these operations contain.
320+
opsWithRegionsToCheckInParallel.clear();
310321

311322
for (Operation &op : block) {
312323
if (isReachable) {
313324
// Check that operands properly dominate this use.
314-
for (unsigned operandNo = 0, e = op.getNumOperands(); operandNo != e;
315-
++operandNo) {
316-
if (domInfo->properlyDominates(op.getOperand(operandNo), &op))
317-
continue;
318-
319-
diagnoseInvalidOperandDominance(op, operandNo);
320-
return failure();
325+
for (auto &operand : op.getOpOperands()) {
326+
// If the operand doesn't dominate the user, then emit an error.
327+
if (!domInfo.properlyDominates(operand.get(), &op)) {
328+
diagnoseInvalidOperandDominance(op, operand.getOperandNumber());
329+
return failure();
330+
}
321331
}
322332
}
323333

324-
// Recursively verify dominance within each operation in the
325-
// block, even if the block itself is not reachable, or we are in
326-
// a region which doesn't respect dominance.
327-
if (op.getNumRegions() != 0)
328-
if (failed(verifyDominanceOfContainedRegions(op)))
334+
// If this operation has any regions, we need to recursively verify
335+
// dominance of the ops within it.
336+
if (op.getNumRegions() == 0)
337+
continue;
338+
339+
// If this is a non-isolated region (e.g. an affine for loop), pass down
340+
// the current dominator information.
341+
if (!op.hasTrait<OpTrait::IsIsolatedFromAbove>()) {
342+
if (failed(verifyDominanceOfContainedRegions(op, domInfo)))
343+
return failure();
344+
} else if (parallelismEnabled) {
345+
// If this is an IsolatedFromAbove op and parallelism is enabled, then
346+
// enqueue this for processing later.
347+
opsWithRegionsToCheckInParallel.push_back(&op);
348+
} else {
349+
// If not, just verify inline with a local dom scope.
350+
DominanceInfo localDomInfo;
351+
if (failed(verifyDominanceOfContainedRegions(op, localDomInfo)))
329352
return failure();
353+
}
354+
}
355+
356+
// If we have multiple parallelizable subregions, check them in parallel.
357+
if (opsWithRegionsToCheckInParallel.size() == 1) {
358+
// Each isolated operation gets its own dom info.
359+
Operation *op = opsWithRegionsToCheckInParallel[0];
360+
DominanceInfo localDomInfo;
361+
if (failed(verifyDominanceOfContainedRegions(*op, localDomInfo)))
362+
return failure();
363+
} else if (!opsWithRegionsToCheckInParallel.empty()) {
364+
ParallelDiagnosticHandler handler(opWithRegions.getContext());
365+
std::atomic<bool> passFailed(false);
366+
llvm::parallelForEachN(
367+
0, opsWithRegionsToCheckInParallel.size(), [&](size_t opIdx) {
368+
handler.setOrderIDForThread(opIdx);
369+
Operation *op = opsWithRegionsToCheckInParallel[opIdx];
370+
371+
// Each isolated operation gets its own dom info.
372+
DominanceInfo localDomInfo;
373+
if (failed(verifyDominanceOfContainedRegions(*op, localDomInfo)))
374+
passFailed = true;
375+
handler.eraseOrderIDForThread();
376+
});
377+
if (passFailed)
378+
return failure();
330379
}
331380
}
332381
}
@@ -338,8 +387,8 @@ OperationVerifier::verifyDominanceOfContainedRegions(Operation &op) {
338387
//===----------------------------------------------------------------------===//
339388

340389
/// Perform (potentially expensive) checks of invariants, used to detect
341-
/// compiler bugs. On error, this reports the error through the MLIRContext and
342-
/// returns failure.
390+
/// compiler bugs. On error, this reports the error through the MLIRContext
391+
/// and returns failure.
343392
LogicalResult mlir::verify(Operation *op) {
344-
return OperationVerifier().verifyOpAndDominance(*op);
393+
return OperationVerifier(op->getContext()).verifyOpAndDominance(*op);
345394
}

0 commit comments

Comments
 (0)