[Mlir-commits] [mlir] [mlir][scf]: Expose emitNormalizedLoopBounds/denormalizeInductionVariable util functions (NFC) (PR #94429)

Aviad Cohen llvmlistbot at llvm.org
Sat Jun 8 07:01:24 PDT 2024


================
@@ -501,26 +483,27 @@ static LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
   if (isZeroBased && isStepOne)
     return {lb, ub, step};
 
-  Value diff = isZeroBased ? ub : rewriter.create<arith::SubIOp>(loc, ub, lb);
+  Value diff =
+      isZeroBased ? ub : rewriter.createOrFold<arith::SubIOp>(loc, ub, lb);
   Value newUpperBound =
-      isStepOne ? diff : rewriter.create<arith::CeilDivSIOp>(loc, diff, step);
+      isStepOne ? diff
+                : rewriter.createOrFold<arith::CeilDivSIOp>(loc, diff, step);
 
   Value newLowerBound = isZeroBased
                             ? lb
-                            : rewriter.create<arith::ConstantOp>(
+                            : rewriter.createOrFold<arith::ConstantOp>(
----------------
AviadCo wrote:

Once I moved it to attribute, it relevealed that it may lead to this error:
`'scf.for' op failed to verify that all of {lowerBound, upperBound, step} have same type`

This is because we now use `getValueOrCreateConstantIndexOp`, and the lowerBound / upperBound type is i32, but the new step is created from attribute into index type.

The error from the lit test:

```
// -----// IR Dump After InterpreterPass Failed (transform-interpreter) //----- //
"builtin.module"() ({
  "func.func"() <{function_type = () -> (), sym_name = "coalesce_i32_loops"}> ({
    %3 = "arith.constant"() <{value = 0 : i32}> : () -> i32
    %4 = "arith.constant"() <{value = 128 : i32}> : () -> i32
    %5 = "arith.constant"() <{value = 2 : i32}> : () -> i32
    %6 = "arith.constant"() <{value = 64 : i32}> : () -> i32
    %7 = "arith.constant"() <{value = 64 : i32}> : () -> i32
    %8 = "arith.constant"() <{value = 1 : index}> : () -> index
    %9 = "arith.constant"() <{value = 32 : i32}> : () -> i32
    %10 = "arith.constant"() <{value = 1 : index}> : () -> index
    %11 = "arith.muli"(%7, %9) <{overflowFlags = #arith.overflow<none>}> : (i32, i32) -> i32
    "scf.for"(%3, %11, %8) ({
    ^bb0(%arg1: i32):
      %12 = "arith.remsi"(%arg1, %9) : (i32, i32) -> i32
      %13 = "arith.divsi"(%arg1, %9) : (i32, i32) -> i32
      %14 = "arith.muli"(%12, %5) <{overflowFlags = #arith.overflow<none>}> : (i32, i32) -> i32
      %15 = "arith.muli"(%13, %5) <{overflowFlags = #arith.overflow<none>}> : (i32, i32) -> i32
      %16 = "arith.addi"(%15, %14) <{overflowFlags = #arith.overflow<none>}> : (i32, i32) -> i32
      "scf.yield"() : () -> ()
    }) {coalesce} : (i32, i32, index) -> ()
    "func.return"() : () -> ()
  }) : () -> ()
  "builtin.module"() ({
    "transform.named_sequence"() <{arg_attrs = [{transform.readonly}], function_type = (!transform.any_op) -> (), sym_name = "__transform_main"}> ({
    ^bb0(%arg0: !transform.any_op):
      %0 = "transform.structured.match"(%arg0) <{op_attrs = {coalesce}, ops = ["scf.for"]}> : (!transform.any_op) -> !transform.any_op
      %1 = "transform.cast"(%0) : (!transform.any_op) -> !transform.op<"scf.for">
      %2 = "transform.loop.coalesce"(%1) : (!transform.op<"scf.for">) -> !transform.op<"scf.for">
      "transform.yield"() : () -> ()
    }) : () -> ()
  }) {transform.with_named_sequence} : () -> ()
}) : () -> ()
```

lit test is:

`mlir/test/Dialect/SCF/transform-ops.mlir` line 279

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


More information about the Mlir-commits mailing list