[Mlir-commits] [mlir] [mlir][scf] Specialize loops with constant affine.min operands (PR #228909)
Matthias Springer
llvmlistbot at llvm.org
Sun Oct 4 16:07:44 PDT 2026
https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/228909
>From 9fcafc7df7a5850fc30b17d4cfb1951decdecb17 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Sun, 4 Oct 2026 23:32:33 +0800
Subject: [PATCH] [mlir][scf] Specialize loops with constant affine.min
operands
---
.../SCF/Transforms/LoopSpecialization.cpp | 5 +++-
.../Dialect/SCF/for-loop-specialization.mlir | 26 +++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp b/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
index a0b5ae9b66091..685c57319a216 100644
--- a/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
@@ -84,8 +84,11 @@ static void specializeForLoopForUnrolling(ForOp op) {
auto minOp = bound.getDefiningOp<AffineMinOp>();
if (!minOp)
return;
+ AffineMap canonicalizedMap = minOp.getMap();
+ SmallVector<Value> operands(minOp.getMapOperands());
+ affine::canonicalizeMapAndOperands(&canonicalizedMap, &operands);
int64_t minConstant = std::numeric_limits<int64_t>::max();
- for (AffineExpr expr : minOp.getMap().getResults()) {
+ for (AffineExpr expr : canonicalizedMap.getResults()) {
if (auto constantIndex = dyn_cast<AffineConstantExpr>(expr))
minConstant = std::min(minConstant, constantIndex.getValue());
}
diff --git a/mlir/test/Dialect/SCF/for-loop-specialization.mlir b/mlir/test/Dialect/SCF/for-loop-specialization.mlir
index ff66c6c1e47f5..59e522f621963 100644
--- a/mlir/test/Dialect/SCF/for-loop-specialization.mlir
+++ b/mlir/test/Dialect/SCF/for-loop-specialization.mlir
@@ -37,3 +37,29 @@ func.func @for(%outer: index, %A: memref<?xf32>, %B: memref<?xf32>,
// CHECK: }
// CHECK: return
// CHECK: }
+
+// -----
+
+func.func @for_constant_operand(%n: index) {
+ %c0 = arith.constant 0 : index
+ %c1 = arith.constant 1 : index
+ %c8 = arith.constant 8 : index
+ %ub = affine.min affine_map<(d0)[s0] -> (d0, s0)>(%n)[%c8]
+ scf.for %i = %c0 to %ub step %c1 {
+ }
+ return
+}
+
+// CHECK-LABEL: func.func @for_constant_operand(
+// CHECK-SAME: [[N:%.*]]: index)
+// CHECK: [[C8:%.*]] = arith.constant 8 : index
+// CHECK: [[MIN:%.*]] = affine.min #{{.*}}([[N]]){{\[}}[[C8]]]
+// CHECK: [[SPECIALIZED_C8:%.*]] = arith.constant 8 : index
+// CHECK: [[PRED:%.*]] = arith.cmpi eq, [[MIN]], [[SPECIALIZED_C8]]
+// CHECK: scf.if [[PRED]] {
+// CHECK: scf.for {{%.*}} = {{%.*}} to [[SPECIALIZED_C8]] step {{%.*}} {
+// CHECK: }
+// CHECK: } else {
+// CHECK: scf.for {{%.*}} = {{%.*}} to [[MIN]] step {{%.*}} {
+// CHECK: }
+// CHECK: }
More information about the Mlir-commits
mailing list