[Mlir-commits] [mlir] [MLIR][SideEffects][MemoryEffects] Modified LICM to be more aggressive when checking movability of ops with MemWrite effects (PR #155344)

Mo Bagherbeik llvmlistbot at llvm.org
Wed Oct 8 13:51:02 PDT 2025


================
@@ -318,6 +318,35 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
           $_op->operand_begin() + firstOperandIndex + initsMutable.size());
     }
 
+    /// Return whether the loop is known to have zero iterations.
+    /// Returns std::nullopt if not enough constant information is available.
+    ::std::optional<bool> isZeroTrip() {
+      auto lbs = $_op.getLoopLowerBounds();
+      auto ubs = $_op.getLoopUpperBounds();
+      auto steps = $_op.getLoopSteps();
+
+      if (!lbs || !ubs || !steps)
+        return ::std::nullopt;
+
+      if (lbs->size() != ubs->size() || ubs->size() != steps->size())
+        return ::std::nullopt;
+
+      for (size_t i = 0; i < steps->size(); ++i) {
+        auto lb = ::mlir::getConstantIntValue((*lbs)[i]);
+        auto ub = ::mlir::getConstantIntValue((*ubs)[i]);
+        auto st = ::mlir::getConstantIntValue((*steps)[i]);
+
+        if (!lb || !ub || !st)
+          return ::std::nullopt; // non-constant -> unknown
+
+        if (*st >= 0 && *lb >= *ub)
+          return true;
+        if (*st < 0 && *lb <= *ub)
+          return true;
+      }
+      return false;
+    }
----------------
mbagherbeikTT wrote:

I used it in addition to isZeroTrip() to confirm liveness

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


More information about the Mlir-commits mailing list