Skip to content

Commit 6a2190d

Browse files
author
Hendrik Greving
committed
[mlir] Make division unsigned.
Uses arith.divui where it is safe to do so. Adjusts the tests for above. Differential Revision: https://reviews.llvm.org/D132701
1 parent 3785234 commit 6a2190d

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static Value ceilDivPositive(OpBuilder &builder, Location loc, Value dividend,
297297
builder.create<arith::ConstantIndexOp>(loc, divisor - 1);
298298
Value divisorCst = builder.create<arith::ConstantIndexOp>(loc, divisor);
299299
Value sum = builder.create<arith::AddIOp>(loc, dividend, divisorMinusOneCst);
300-
return builder.create<arith::DivSIOp>(loc, sum, divisorCst);
300+
return builder.create<arith::DivUIOp>(loc, sum, divisorCst);
301301
}
302302

303303
// Build the IR that performs ceil division of a positive value by another
@@ -311,7 +311,7 @@ static Value ceilDivPositive(OpBuilder &builder, Location loc, Value dividend,
311311
Value cstOne = builder.create<arith::ConstantIndexOp>(loc, 1);
312312
Value divisorMinusOne = builder.create<arith::SubIOp>(loc, divisor, cstOne);
313313
Value sum = builder.create<arith::AddIOp>(loc, dividend, divisorMinusOne);
314-
return builder.create<arith::DivSIOp>(loc, sum, divisor);
314+
return builder.create<arith::DivUIOp>(loc, sum, divisor);
315315
}
316316

317317
/// Helper to replace uses of loop carried values (iter_args) and loop

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ func.func @unnormalized_loops() {
9191
// CHECK: %[[c1:.*]] = arith.constant 1
9292
// CHECK: %[[step_minus_c1:.*]] = arith.subi %[[orig_step_i]], %[[c1]]
9393
// CHECK: %[[dividend:.*]] = arith.addi %[[diff_i]], %[[step_minus_c1]]
94-
// CHECK: %[[numiter_i:.*]] = arith.divsi %[[dividend]], %[[orig_step_i]]
94+
// CHECK: %[[numiter_i:.*]] = arith.divui %[[dividend]], %[[orig_step_i]]
9595

9696
// Normalized lower bound and step for the outer scf.
9797
// CHECK: %[[lb_i:.*]] = arith.constant 0
9898
// CHECK: %[[step_i:.*]] = arith.constant 1
9999

100100
// Number of iterations in the inner loop, the pattern is the same as above,
101101
// only capture the final result.
102-
// CHECK: %[[numiter_j:.*]] = arith.divsi {{.*}}, %[[orig_step_j]]
102+
// CHECK: %[[numiter_j:.*]] = arith.divui {{.*}}, %[[orig_step_j]]
103103

104104
// New bounds of the outer scf.
105105
// CHECK: %[[range:.*]] = arith.muli %[[numiter_i]], %[[numiter_j]]
@@ -137,11 +137,11 @@ func.func @parametric(%lb1 : index, %ub1 : index, %step1 : index,
137137
// CHECK: %[[range1:.*]] = arith.subi %[[orig_ub1]], %[[orig_lb1]]
138138
// CHECK: %[[orig_step1_minus_1:.*]] = arith.subi %[[orig_step1]], %c1
139139
// CHECK: %[[dividend1:.*]] = arith.addi %[[range1]], %[[orig_step1_minus_1]]
140-
// CHECK: %[[numiter1:.*]] = arith.divsi %[[dividend1]], %[[orig_step1]]
140+
// CHECK: %[[numiter1:.*]] = arith.divui %[[dividend1]], %[[orig_step1]]
141141
// CHECK: %[[range2:.*]] = arith.subi %[[orig_ub2]], %[[orig_lb2]]
142142
// CHECK: %[[orig_step2_minus_1:.*]] = arith.subi %arg5, %c1
143143
// CHECK: %[[dividend2:.*]] = arith.addi %[[range2]], %[[orig_step2_minus_1]]
144-
// CHECK: %[[numiter2:.*]] = arith.divsi %[[dividend2]], %[[orig_step2]]
144+
// CHECK: %[[numiter2:.*]] = arith.divui %[[dividend2]], %[[orig_step2]]
145145
// CHECK: %[[range:.*]] = arith.muli %[[numiter1]], %[[numiter2]] : index
146146

147147
// Check that the outer loop is updated.

mlir/test/Dialect/SCF/loop-unroll.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func.func @dynamic_loop_unroll(%arg0 : index, %arg1 : index, %arg2 : index,
2525
// UNROLL-BY-2-DAG: %[[V1:.*]] = arith.subi %[[STEP]], %[[C1]] : index
2626
// UNROLL-BY-2-DAG: %[[V2:.*]] = arith.addi %[[V0]], %[[V1]] : index
2727
// Compute trip count in V3.
28-
// UNROLL-BY-2-DAG: %[[V3:.*]] = arith.divsi %[[V2]], %[[STEP]] : index
28+
// UNROLL-BY-2-DAG: %[[V3:.*]] = arith.divui %[[V2]], %[[STEP]] : index
2929
// Store unroll factor in C2.
3030
// UNROLL-BY-2-DAG: %[[C2:.*]] = arith.constant 2 : index
3131
// UNROLL-BY-2-DAG: %[[V4:.*]] = arith.remsi %[[V3]], %[[C2]] : index
@@ -58,7 +58,7 @@ func.func @dynamic_loop_unroll(%arg0 : index, %arg1 : index, %arg2 : index,
5858
// UNROLL-BY-3-DAG: %[[V1:.*]] = arith.subi %[[STEP]], %[[C1]] : index
5959
// UNROLL-BY-3-DAG: %[[V2:.*]] = arith.addi %[[V0]], %[[V1]] : index
6060
// Compute trip count in V3.
61-
// UNROLL-BY-3-DAG: %[[V3:.*]] = arith.divsi %[[V2]], %[[STEP]] : index
61+
// UNROLL-BY-3-DAG: %[[V3:.*]] = arith.divui %[[V2]], %[[STEP]] : index
6262
// Store unroll factor in C3.
6363
// UNROLL-BY-3-DAG: %[[C3:.*]] = arith.constant 3 : index
6464
// UNROLL-BY-3-DAG: %[[V4:.*]] = arith.remsi %[[V3]], %[[C3]] : index

mlir/test/Transforms/parametric-tiling.mlir

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ func.func @rectangular(%arg0: memref<?x?xf32>) {
1212
// COMMON: %[[diff:.*]] = arith.subi %c44, %c2
1313
// COMMON: %[[adjustment:.*]] = arith.subi %c1, %c1_{{.*}}
1414
// COMMON-NEXT: %[[diff_adj:.*]] = arith.addi %[[diff]], %[[adjustment]]
15-
// COMMON-NEXT: %[[range:.*]] = arith.divsi %[[diff_adj]], %c1
15+
// COMMON-NEXT: %[[range:.*]] = arith.divui %[[diff_adj]], %c1
1616

1717
// Ceildiv to get the parametric tile size.
1818
// COMMON: %[[sum:.*]] = arith.addi %[[range]], %c6
19-
// COMMON-NEXT: %[[size:.*]] = arith.divsi %[[sum]], %c7
19+
// COMMON-NEXT: %[[size:.*]] = arith.divui %[[sum]], %c7
2020
// New outer step (original is %c1).
2121
// COMMON-NEXT: %[[step:.*]] = arith.muli %c1, %[[size]]
2222

@@ -26,11 +26,11 @@ func.func @rectangular(%arg0: memref<?x?xf32>) {
2626
// TILE_74: %[[diff2:.*]] = arith.subi %c44, %c1
2727
// TILE_74: %[[adjustment2:.*]] = arith.subi %c2, %c1_{{.*}}
2828
// TILE_74-NEXT: %[[diff2_adj:.*]] = arith.addi %[[diff2]], %[[adjustment2]]
29-
// TILE_74-NEXT: %[[range2:.*]] = arith.divsi %[[diff2_adj]], %c2
29+
// TILE_74-NEXT: %[[range2:.*]] = arith.divui %[[diff2_adj]], %c2
3030

3131
// Ceildiv to get the parametric tile size for the second original scf.
3232
// TILE_74: %[[sum2:.*]] = arith.addi %[[range2]], %c3
33-
// TILE_74-NEXT: %[[size2:.*]] = arith.divsi %[[sum2]], %c4
33+
// TILE_74-NEXT: %[[size2:.*]] = arith.divui %[[sum2]], %c4
3434
// New inner step (original is %c2).
3535
// TILE_74-NEXT: %[[step2:.*]] = arith.muli %c2, %[[size2]]
3636

@@ -76,11 +76,11 @@ func.func @triangular(%arg0: memref<?x?xf32>) {
7676
// COMMON: %[[diff:.*]] = arith.subi %c44, %c2
7777
// COMMON: %[[adjustment:.*]] = arith.subi %c1, %c1_{{.*}}
7878
// COMMON-NEXT: %[[diff_adj:.*]] = arith.addi %[[diff]], %[[adjustment]]
79-
// COMMON-NEXT: %[[range:.*]] = arith.divsi %[[diff_adj]], %c1
79+
// COMMON-NEXT: %[[range:.*]] = arith.divui %[[diff_adj]], %c1
8080

8181
// Ceildiv to get the parametric tile size.
8282
// COMMON: %[[sum:.*]] = arith.addi %[[range]], %c6
83-
// COMMON-NEXT: %[[size:.*]] = arith.divsi %[[sum]], %c7
83+
// COMMON-NEXT: %[[size:.*]] = arith.divui %[[sum]], %c7
8484
// New outer step (original is %c1).
8585
// COMMON-NEXT: %[[step:.*]] = arith.muli %c1, %[[size]]
8686

@@ -95,11 +95,11 @@ func.func @triangular(%arg0: memref<?x?xf32>) {
9595
// where step is known to be %c2.
9696
// TILE_74: %[[diff2:.*]] = arith.subi %[[i]], %c1
9797
// TILE_74-NEXT: %[[diff2_adj:.*]] = arith.addi %[[diff2]], %[[adjustment2]]
98-
// TILE_74-NEXT: %[[range2:.*]] = arith.divsi %[[diff2_adj]], %c2
98+
// TILE_74-NEXT: %[[range2:.*]] = arith.divui %[[diff2_adj]], %c2
9999

100100
// Ceildiv to get the parametric tile size for the second original scf.
101101
// TILE_74: %[[sum2:.*]] = arith.addi %[[range2]], %c3
102-
// TILE_74-NEXT: %[[size2:.*]] = arith.divsi %[[sum2]], %c4
102+
// TILE_74-NEXT: %[[size2:.*]] = arith.divui %[[sum2]], %c4
103103
// New inner step (original is %c2).
104104
// TILE_74-NEXT: %[[step2:.*]] = arith.muli %c2, %[[size2]]
105105

0 commit comments

Comments
 (0)