Skip to content

Commit 0f6a2cd

Browse files
author
Hendrik Greving
committed
[mlir] Fix signed ceildiv, loop normalization.
Fixes using the signed ceildiv op instead of incorrectly assuming positive loop bounds. Adjusts the tests for above. Differential Revision: https://reviews.llvm.org/D132953
1 parent 6a2190d commit 0f6a2cd

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

mlir/lib/Dialect/SCF/Utils/Utils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,13 @@ static LoopParams normalizeLoop(OpBuilder &boundsBuilder,
555555
// Compute the number of iterations the loop executes: ceildiv(ub - lb, step)
556556
// assuming the step is strictly positive. Update the bounds and the step
557557
// of the loop to go from 0 to the number of iterations, if necessary.
558-
// TODO: introduce support for negative steps or emit dynamic asserts
559-
// on step positivity, whatever gets implemented first.
560558
if (isZeroBased && isStepOne)
561559
return {/*lowerBound=*/lowerBound, /*upperBound=*/upperBound,
562560
/*step=*/step};
563561

564562
Value diff = boundsBuilder.create<arith::SubIOp>(loc, upperBound, lowerBound);
565-
Value newUpperBound = ceilDivPositive(boundsBuilder, loc, diff, step);
563+
Value newUpperBound =
564+
boundsBuilder.create<arith::CeilDivSIOp>(loc, diff, step);
566565

567566
Value newLowerBound =
568567
isZeroBased ? lowerBound

mlir/test/Dialect/Affine/loop-coalescing.mlir

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,15 @@ func.func @unnormalized_loops() {
8888

8989
// Number of iterations in the outer scf.
9090
// CHECK: %[[diff_i:.*]] = arith.subi %[[orig_ub_i]], %[[orig_lb_i]]
91-
// CHECK: %[[c1:.*]] = arith.constant 1
92-
// CHECK: %[[step_minus_c1:.*]] = arith.subi %[[orig_step_i]], %[[c1]]
93-
// CHECK: %[[dividend:.*]] = arith.addi %[[diff_i]], %[[step_minus_c1]]
94-
// CHECK: %[[numiter_i:.*]] = arith.divui %[[dividend]], %[[orig_step_i]]
91+
// CHECK: %[[numiter_i:.*]] = arith.ceildivsi %[[diff_i]], %[[orig_step_i]]
9592

9693
// Normalized lower bound and step for the outer scf.
9794
// CHECK: %[[lb_i:.*]] = arith.constant 0
9895
// CHECK: %[[step_i:.*]] = arith.constant 1
9996

10097
// Number of iterations in the inner loop, the pattern is the same as above,
10198
// only capture the final result.
102-
// CHECK: %[[numiter_j:.*]] = arith.divui {{.*}}, %[[orig_step_j]]
99+
// CHECK: %[[numiter_j:.*]] = arith.ceildivsi {{.*}}, %[[orig_step_j]]
103100

104101
// New bounds of the outer scf.
105102
// CHECK: %[[range:.*]] = arith.muli %[[numiter_i]], %[[numiter_j]]
@@ -135,13 +132,9 @@ func.func @parametric(%lb1 : index, %ub1 : index, %step1 : index,
135132
// Compute the number of iterations for each of the loops and the total
136133
// number of iterations.
137134
// CHECK: %[[range1:.*]] = arith.subi %[[orig_ub1]], %[[orig_lb1]]
138-
// CHECK: %[[orig_step1_minus_1:.*]] = arith.subi %[[orig_step1]], %c1
139-
// CHECK: %[[dividend1:.*]] = arith.addi %[[range1]], %[[orig_step1_minus_1]]
140-
// CHECK: %[[numiter1:.*]] = arith.divui %[[dividend1]], %[[orig_step1]]
135+
// CHECK: %[[numiter1:.*]] = arith.ceildivsi %[[range1]], %[[orig_step1]]
141136
// CHECK: %[[range2:.*]] = arith.subi %[[orig_ub2]], %[[orig_lb2]]
142-
// CHECK: %[[orig_step2_minus_1:.*]] = arith.subi %arg5, %c1
143-
// CHECK: %[[dividend2:.*]] = arith.addi %[[range2]], %[[orig_step2_minus_1]]
144-
// CHECK: %[[numiter2:.*]] = arith.divui %[[dividend2]], %[[orig_step2]]
137+
// CHECK: %[[numiter2:.*]] = arith.ceildivsi %[[range2]], %[[orig_step2]]
145138
// CHECK: %[[range:.*]] = arith.muli %[[numiter1]], %[[numiter2]] : index
146139

147140
// Check that the outer loop is updated.

0 commit comments

Comments
 (0)