Skip to content

Commit 9b740c0

Browse files
committed
Update normalizeAffineFor to canonicalize maps/operands before using them
Update normalizeAffineFor utility to canonicalize maps and operands before using them. Differential Revision: https://reviews.llvm.org/D121086
1 parent af4ddd5 commit 9b740c0

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

mlir/lib/Dialect/Affine/Utils/Utils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ LogicalResult mlir::normalizeAffineFor(AffineForOp op) {
618618
AffineMap::get(origLbMap.getNumDims() + origUbMap.getNumDims(),
619619
origLbMap.getNumSymbols() + origUbMap.getNumSymbols(),
620620
newUbExprs, opBuilder.getContext());
621+
canonicalizeMapAndOperands(&newUbMap, &ubOperands);
621622

622623
// Normalize the loop.
623624
op.setUpperBound(ubOperands, newUbMap);
@@ -640,6 +641,7 @@ LogicalResult mlir::normalizeAffineFor(AffineForOp op) {
640641
AffineExpr newIVExpr = origIVExpr * origLoopStep + origLbMap.getResult(0);
641642
AffineMap ivMap = AffineMap::get(origLbMap.getNumDims() + 1,
642643
origLbMap.getNumSymbols(), newIVExpr);
644+
canonicalizeMapAndOperands(&ivMap, &lbOperands);
643645
Operation *newIV = opBuilder.create<AffineApplyOp>(loc, ivMap, lbOperands);
644646
op.getInductionVar().replaceAllUsesExcept(newIV->getResult(0), newIV);
645647
return success();

mlir/test/Dialect/Affine/affine-loop-normalize.mlir

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ func @normalize_parallel() {
2626

2727
// -----
2828

29+
// CHECK-LABEL: func @relative_bounds
30+
func @relative_bounds(%arg: index) {
31+
// CHECK: affine.for %{{.*}} = 0 to 4
32+
affine.for %i = affine_map<(d0) -> (d0)>(%arg) to affine_map<(d0) -> (d0 + 4)>(%arg) {
33+
}
34+
return
35+
}
36+
37+
// -----
38+
2939
// Check that single iteration loop is removed and its body is promoted to the
3040
// parent block.
3141

@@ -103,15 +113,15 @@ func @loop_with_unknown_upper_bound(%arg0: memref<?x?xf32>, %arg1: index) {
103113
// CHECK-DAG: [[$OUTERIV:#map[0-9]+]] = affine_map<(d0) -> (d0 * 32 + 2)>
104114
// CHECK-DAG: [[$INNERIV:#map[0-9]+]] = affine_map<(d0) -> (d0 + 2)>
105115
// CHECK-DAG: [[$OUTERUB:#map[0-9]+]] = affine_map<()[s0] -> ((s0 - 2) ceildiv 32)>
106-
// CHECK-DAG: [[$INNERUB:#map[0-9]+]] = affine_map<(d0) -> (d0 - 2, 510)>
116+
// CHECK-DAG: [[$INNERUB:#map[0-9]+]] = affine_map<()[s0] -> (s0 - 2, 510)>
107117

108118
// CHECK-LABEL: func @loop_with_multiple_upper_bounds
109119
// CHECK-SAME: (%[[ARG0:.*]]: memref<?x?xf32>, %[[ARG1:.*]]: index)
110120
// CHECK-NEXT: %{{.*}} = arith.constant 0 : index
111121
// CHECK-NEXT: %[[DIM:.*]] = memref.dim %arg0, %c0 : memref<?x?xf32>
112122
// CHECK-NEXT: affine.for %[[I:.*]] = 0 to [[$OUTERUB]]()[%[[DIM]]] {
113123
// CHECK-NEXT: %[[IIV:.*]] = affine.apply [[$OUTERIV]](%[[I]])
114-
// CHECK-NEXT: affine.for %[[II:.*]] = 0 to min [[$INNERUB]](%[[ARG1]]) {
124+
// CHECK-NEXT: affine.for %[[II:.*]] = 0 to min [[$INNERUB]]()[%[[ARG1]]] {
115125
// CHECK-NEXT: %[[IIIV:.*]] = affine.apply [[$INNERIV]](%[[II]])
116126
// CHECK-NEXT: "test.foo"(%[[IIV]], %[[IIIV]])
117127
// CHECK-NEXT: }
@@ -133,7 +143,7 @@ func @loop_with_multiple_upper_bounds(%arg0: memref<?x?xf32>, %arg1 : index) {
133143

134144
// CHECK-DAG: [[$INTERUB:#map[0-9]+]] = affine_map<()[s0] -> (s0 ceildiv 32)>
135145
// CHECK-DAG: [[$INTERIV:#map[0-9]+]] = affine_map<(d0) -> (d0 * 32)>
136-
// CHECK-DAG: [[$INTRAUB:#map[0-9]+]] = affine_map<(d0, d1)[s0] -> (32, -d0 + s0)>
146+
// CHECK-DAG: [[$INTRAUB:#map[0-9]+]] = affine_map<(d0)[s0] -> (32, -d0 + s0)>
137147
// CHECK-DAG: [[$INTRAIV:#map[0-9]+]] = affine_map<(d0, d1) -> (d1 + d0)>
138148

139149
// CHECK-LABEL: func @tiled_matmul
@@ -149,11 +159,11 @@ func @loop_with_multiple_upper_bounds(%arg0: memref<?x?xf32>, %arg1 : index) {
149159
// CHECK-NEXT: %[[JIV:.*]] = affine.apply [[$INTERIV]](%[[J]])
150160
// CHECK-NEXT: affine.for %[[K:.*]] = 0 to [[$INTERUB]]()[%[[DIM2]]] {
151161
// CHECK-NEXT: %[[KIV:.*]] = affine.apply [[$INTERIV]](%[[K]])
152-
// CHECK-NEXT: affine.for %[[II:.*]] = 0 to min [[$INTRAUB]](%[[IIV]], %[[IIV]])[%[[DIM0]]] {
162+
// CHECK-NEXT: affine.for %[[II:.*]] = 0 to min [[$INTRAUB]](%[[IIV]])[%[[DIM0]]] {
153163
// CHECK-NEXT: %[[IIIV:.*]] = affine.apply [[$INTRAIV]](%[[IIV]], %[[II]])
154-
// CHECK-NEXT: affine.for %[[JJ:.*]] = 0 to min [[$INTRAUB]](%[[JIV]], %[[JIV]])[%[[DIM1]]] {
164+
// CHECK-NEXT: affine.for %[[JJ:.*]] = 0 to min [[$INTRAUB]](%[[JIV]])[%[[DIM1]]] {
155165
// CHECK-NEXT: %[[JJIV:.*]] = affine.apply [[$INTRAIV]](%[[JIV]], %[[JJ]])
156-
// CHECK-NEXT: affine.for %[[KK:.*]] = 0 to min [[$INTRAUB]](%[[KIV]], %[[KIV]])[%[[DIM2]]] {
166+
// CHECK-NEXT: affine.for %[[KK:.*]] = 0 to min [[$INTRAUB]](%[[KIV]])[%[[DIM2]]] {
157167
// CHECK-NEXT: %[[KKIV:.*]] = affine.apply [[$INTRAIV]](%[[KIV]], %[[KK]])
158168
// CHECK-NEXT: %{{.*}} = affine.load %[[ARG0]][%[[IIIV]], %[[KKIV]]] : memref<1024x1024xf32>
159169
// CHECK-NEXT: %{{.*}} = affine.load %[[ARG1]][%[[KKIV]], %[[JJIV]]] : memref<1024x1024xf32>

0 commit comments

Comments
 (0)