[Mlir-commits] [mlir] e85e44c - [MLIR][Affine] Fix crash in affine.for empty loop folder (#130372)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Mar 8 00:06:54 PST 2025


Author: Uday Bondhugula
Date: 2025-03-08T13:36:51+05:30
New Revision: e85e44ca9cca70990a0b6a96a11014f2ac56aca5

URL: https://github.com/llvm/llvm-project/commit/e85e44ca9cca70990a0b6a96a11014f2ac56aca5
DIFF: https://github.com/llvm/llvm-project/commit/e85e44ca9cca70990a0b6a96a11014f2ac56aca5.diff

LOG: [MLIR][Affine] Fix crash in affine.for empty loop folder (#130372)

A yield value can be the loop IV itself.

Fixes: https://github.com/llvm/llvm-project/issues/74301

Added: 
    

Modified: 
    mlir/lib/Dialect/Affine/IR/AffineOps.cpp
    mlir/test/Dialect/Affine/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index 18e980cd8b88e..8acb21d5074b4 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -2314,6 +2314,10 @@ struct AffineForEmptyLoopFolder : public OpRewritePattern<AffineForOp> {
     for (unsigned i = 0, e = yieldOp->getNumOperands(); i < e; ++i) {
       Value val = yieldOp.getOperand(i);
       auto *iterArgIt = llvm::find(iterArgs, val);
+      // TODO: It should be possible to perform a replacement by computing the
+      // last value of the IV based on the bounds and the step.
+      if (val == forOp.getInductionVar())
+        return failure();
       if (iterArgIt == iterArgs.end()) {
         // `val` is defined outside of the loop.
         assert(forOp.isDefinedOutsideOfLoop(val) &&

diff  --git a/mlir/test/Dialect/Affine/canonicalize.mlir b/mlir/test/Dialect/Affine/canonicalize.mlir
index a9ac13ad71624..d39c0c6e41df2 100644
--- a/mlir/test/Dialect/Affine/canonicalize.mlir
+++ b/mlir/test/Dialect/Affine/canonicalize.mlir
@@ -2262,3 +2262,12 @@ func.func @cst_value_to_cst_attr_basis_linearize_index(%arg0 : index, %arg1 : in
   %0 = affine.linearize_index disjoint [%arg0, %arg1, %arg2] by  (%c2, 3, %c4) : index
   return %0 : index
 }
+
+// CHECK-LABEL: func @for_empty_body_folder_iv_yield
+func.func @for_empty_body_folder_iv_yield() -> index {
+  %c18 = arith.constant 18 : index
+  %10 = affine.for %arg3 = 0 to 114 iter_args(%arg4 = %c18) -> (index) {
+    affine.yield %arg3 : index
+  }
+  return %10 : index
+}


        


More information about the Mlir-commits mailing list