Skip to content

Commit 551cd1b

Browse files
wsmosesjoker-eph
andauthored
[MLIR][NFC] Speed up is valid symbol check (#154924)
This removes the closure indirection, and removes the recursion on isValidSymbol. The rewriting of the recursion is particularly helpful to avoid redundant checks of isPure and checking the isValidSymbol of the operands for each parent region check --------- Co-authored-by: Mehdi Amini <[email protected]>
1 parent b6753de commit 551cd1b

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,21 @@ bool mlir::affine::isValidSymbol(Value value) {
427427
return false;
428428
}
429429

430+
/// A utility function to check if a value is defined at the top level of
431+
/// `region` or is an argument of `region` or is defined above the region.
432+
static bool isTopLevelValueOrAbove(Value value, Region *region) {
433+
Region *parentRegion = value.getParentRegion();
434+
do {
435+
if (parentRegion == region)
436+
return true;
437+
Operation *regionOp = region->getParentOp();
438+
if (regionOp->hasTrait<OpTrait::IsIsolatedFromAbove>())
439+
break;
440+
region = region->getParentOp()->getParentRegion();
441+
} while (region);
442+
return false;
443+
}
444+
430445
/// A value can be used as a symbol for `region` iff it meets one of the
431446
/// following conditions:
432447
/// *) It is a constant.
@@ -445,19 +460,12 @@ bool mlir::affine::isValidSymbol(Value value, Region *region) {
445460
return false;
446461

447462
// A top-level value is a valid symbol.
448-
if (region && ::isTopLevelValue(value, region))
463+
if (region && isTopLevelValueOrAbove(value, region))
449464
return true;
450465

451466
auto *defOp = value.getDefiningOp();
452-
if (!defOp) {
453-
// A block argument that is not a top-level value is a valid symbol if it
454-
// dominates region's parent op.
455-
Operation *regionOp = region ? region->getParentOp() : nullptr;
456-
if (regionOp && !regionOp->hasTrait<OpTrait::IsIsolatedFromAbove>())
457-
if (auto *parentOpRegion = region->getParentOp()->getParentRegion())
458-
return isValidSymbol(value, parentOpRegion);
467+
if (!defOp)
459468
return false;
460-
}
461469

462470
// Constant operation is ok.
463471
Attribute operandCst;
@@ -475,12 +483,6 @@ bool mlir::affine::isValidSymbol(Value value, Region *region) {
475483
if (auto dimOp = dyn_cast<ShapedDimOpInterface>(defOp))
476484
return isDimOpValidSymbol(dimOp, region);
477485

478-
// Check for values dominating `region`'s parent op.
479-
Operation *regionOp = region ? region->getParentOp() : nullptr;
480-
if (regionOp && !regionOp->hasTrait<OpTrait::IsIsolatedFromAbove>())
481-
if (auto *parentRegion = region->getParentOp()->getParentRegion())
482-
return isValidSymbol(value, parentRegion);
483-
484486
return false;
485487
}
486488

0 commit comments

Comments
 (0)