[Mlir-commits] [mlir] [MLIR][NFC] Speed up is valid symbol check (PR #154924)

Mehdi Amini llvmlistbot at llvm.org
Fri Aug 22 04:53:28 PDT 2025


================
@@ -445,42 +460,35 @@ bool mlir::affine::isValidSymbol(Value value, Region *region) {
     return false;
 
   // A top-level value is a valid symbol.
-  if (region && ::isTopLevelValue(value, region))
+  if (region && isTopLevelValueOrAbove(value, region))
     return true;
 
   auto *defOp = value.getDefiningOp();
-  if (!defOp) {
-    // A block argument that is not a top-level value is a valid symbol if it
-    // dominates region's parent op.
-    Operation *regionOp = region ? region->getParentOp() : nullptr;
-    if (regionOp && !regionOp->hasTrait<OpTrait::IsIsolatedFromAbove>())
-      if (auto *parentOpRegion = region->getParentOp()->getParentRegion())
-        return isValidSymbol(value, parentOpRegion);
+  if (!defOp)
     return false;
-  }
 
   // Constant operation is ok.
   Attribute operandCst;
   if (matchPattern(defOp, m_Constant(&operandCst)))
     return true;
 
   // `Pure` operation that whose operands are valid symbolic identifiers.
-  if (isPure(defOp) && llvm::all_of(defOp->getOperands(), [&](Value operand) {
-        return affine::isValidSymbol(operand, region);
-      })) {
-    return true;
+  if (isPure(defOp)) {
+    bool allValid = true;
+    for (auto operand : defOp->getOperands()) {
+      if (!affine::isValidSymbol(operand, region)) {
+        allValid = false;
+        break;
+      }
+    }
+    if (allValid)
+      return true;
   }
 
   // Dim op results could be valid symbols at any level.
   if (auto dimOp = dyn_cast<ShapedDimOpInterface>(defOp))
     return isDimOpValidSymbol(dimOp, region);
 
-  // Check for values dominating `region`'s parent op.
-  Operation *regionOp = region ? region->getParentOp() : nullptr;
-  if (regionOp && !regionOp->hasTrait<OpTrait::IsIsolatedFromAbove>())
-    if (auto *parentRegion = region->getParentOp()->getParentRegion())
-      return isValidSymbol(value, parentRegion);
----------------
joker-eph wrote:

> `if is symbol in parent region`

This is the condition I mentioned, which does not match the API doc somehow (line 438 I mentioned before).

I don't see where you're checking on this "`if is symbol in parent region`" after your change?
I don't follow when you write:

> So essentially we rewrite the is top level in region to be is top level in region or above and do the recursive check in a single call there.

Because I don't see anymore the part about:

> the check for "value is constant and/or is pure" is invariant to the region specifier, and has to be done for each recursive region check.


That is: it seems to me that your change is conforming to the API documentation, but the existing code was enforcing more checks. I guess I need to write a unit-test to figure out.



https://github.com/llvm/llvm-project/pull/154924


More information about the Mlir-commits mailing list