[Mlir-commits] [mlir] 9623f43 - [mlir][SCF]: promote one-iteration loops with equal ub and step values (#205826)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Jun 26 09:10:59 PDT 2026
Author: Ege Beysel
Date: 2026-06-26T18:10:53+02:00
New Revision: 9623f43b8bc667dd2b76067a4fb4f4353f985f95
URL: https://github.com/llvm/llvm-project/commit/9623f43b8bc667dd2b76067a4fb4f4353f985f95
DIFF: https://github.com/llvm/llvm-project/commit/9623f43b8bc667dd2b76067a4fb4f4353f985f95.diff
LOG: [mlir][SCF]: promote one-iteration loops with equal ub and step values (#205826)
Adds a fast-path to `constantTripCount` to return 1 on and enables
promotion of single-iteration loops of the form:
```
scf.for %j = %c0 to %val step %val ... { ... }
```
Signed-off-by: Ege Beysel <beyselege at gmail.com>
Added:
Modified:
mlir/lib/Dialect/Utils/StaticValueUtils.cpp
mlir/test/Dialect/SCF/transform-ops.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
index a826f56a69d92..4e30693353440 100644
--- a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
+++ b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
@@ -322,6 +322,12 @@ std::optional<APInt> constantTripCount(
// SSA values. That case cannot be detected here.
return APInt(bitwidth, 0);
}
+ if (isZeroInteger(lb) && ub == step) {
+ // Fast path: LB == 0 && UB == step. The loop has a single iteration.
+ // Note: LB and UB could match at runtime, even though they are
diff erent
+ // SSA values. That case cannot be detected here.
+ return APInt(bitwidth, 1);
+ }
std::optional<std::pair<APInt, bool>> maybeStepCst =
getConstantAPIntValue(step);
diff --git a/mlir/test/Dialect/SCF/transform-ops.mlir b/mlir/test/Dialect/SCF/transform-ops.mlir
index 0e5a4a98a98ea..c8be2d2b3a883 100644
--- a/mlir/test/Dialect/SCF/transform-ops.mlir
+++ b/mlir/test/Dialect/SCF/transform-ops.mlir
@@ -449,6 +449,20 @@ func.func @test_promote_if_one_iteration(%a: index) -> index {
return %0 : index
}
+// CHECK-LABEL: func @test_promote_if_one_iteration_dynamic(
+// CHECK-NOT: scf.for
+// CHECK: %[[r:.*]] = "test.foo"
+// CHECK: return %[[r]]
+func.func @test_promote_if_one_iteration_dynamic(%a: index, %val: index) -> index {
+ %c0 = arith.constant 0 : index
+ %c1 = arith.constant 1 : index
+ %0 = scf.for %j = %c0 to %val step %val iter_args(%arg0 = %a) -> index {
+ %1 = "test.foo"(%a) : (index) -> (index)
+ scf.yield %1 : index
+ }
+ return %0 : index
+}
+
module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
%0 = transform.structured.match ops{["scf.for"]} in %arg1 : (!transform.any_op) -> !transform.any_op
More information about the Mlir-commits
mailing list