[mlir][SCF] Fix crash in loop peeling
authorMatthias Springer <springerm@google.com>
Thu, 19 Jan 2023 18:01:22 +0000 (19:01 +0100)
committerMatthias Springer <springerm@google.com>
Thu, 19 Jan 2023 18:06:28 +0000 (19:06 +0100)
Upper bound and step size should be symbols instead of dims.

Differential Revision: https://reviews.llvm.org/D142136

mlir/lib/Dialect/SCF/Utils/AffineCanonicalizationUtils.cpp
mlir/test/Dialect/SCF/for-loop-peeling.mlir

index 0b09dc5..4ee27e4 100644 (file)
@@ -169,7 +169,8 @@ LogicalResult scf::rewritePeeledMinMaxOp(RewriterBase &rewriter, Operation *op,
                                          Value iv, Value ub, Value step,
                                          bool insideLoop) {
   FlatAffineValueConstraints constraints;
-  constraints.appendDimVar({iv, ub, step});
+  constraints.appendDimVar({iv});
+  constraints.appendSymbolVar({ub, step});
   if (auto constUb = getConstantIntValue(ub))
     constraints.addBound(IntegerPolyhedron::EQ, 1, *constUb);
   if (auto constStep = getConstantIntValue(step))
index b1cf1f2..9a6d1c8 100644 (file)
@@ -275,3 +275,17 @@ func.func @nested_loops(%lb0: index, %lb1 : index, %ub0: index, %ub1: index,
   }
   return %r0 : i32
 }
+
+// -----
+
+// CHECK-LABEL: func @regression
+func.func @regression(%arg0: memref<i64>, %arg1: index) {
+  %c0 = arith.constant 0 : index
+  %0 = affine.apply affine_map<()[s0] -> (s0 * s0)>()[%arg1]
+  scf.for %arg2 = %c0 to %0 step %arg1 {
+    %1 = affine.min affine_map<(d0)[s0] -> (s0, -d0 + s0 * s0)>(%arg2)[%arg1]
+    %2 = arith.index_cast %0 : index to i64
+    memref.store %2, %arg0[] : memref<i64>
+  }
+  return
+}