Skip to content

Commit e9b7a09

Browse files
authored
[MLIR] Unconditionally take min of max lhs/rhs value in inferRemU (#110169)
- **[MLIR] Add test for inferring range of remu; NFC** - **[MLIR] Unconditionally take min of max lhs/rhs value in inferRemU** `arith.remu` cannot be larger than (rhs - 1) or lhs.
1 parent 3590068 commit e9b7a09

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,10 @@ mlir::intrange::inferRemU(ArrayRef<ConstantIntRanges> argRanges) {
444444

445445
unsigned width = rhsMin.getBitWidth();
446446
APInt umin = APInt::getZero(width);
447-
APInt umax = APInt::getMaxValue(width);
447+
// Remainder can't be larger than either of its arguments.
448+
APInt umax = llvm::APIntOps::umin((rhsMax - 1), lhs.umax());
448449

449450
if (!rhsMin.isZero()) {
450-
umax = rhsMax - 1;
451451
// Special case: sweeping out a contiguous range in N/[modulus]
452452
if (rhsMin == rhsMax) {
453453
const APInt &lhsMin = lhs.umin(), &lhsMax = lhs.umax();

mlir/test/Dialect/Arith/int-range-interface.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,19 @@ func.func @remui_base(%arg0 : index, %arg1 : index ) -> i1 {
266266
func.return %3 : i1
267267
}
268268

269+
// CHECK-LABEL: func @remui_base_maybe_zero
270+
// CHECK: %[[true:.*]] = arith.constant true
271+
// CHECK: return %[[true]]
272+
func.func @remui_base_maybe_zero(%arg0 : index, %arg1 : index ) -> i1 {
273+
%c4 = arith.constant 4 : index
274+
%c5 = arith.constant 5 : index
275+
276+
%0 = arith.minui %arg1, %c4 : index
277+
%1 = arith.remui %arg0, %0 : index
278+
%2 = arith.cmpi ult, %1, %c5 : index
279+
func.return %2 : i1
280+
}
281+
269282
// CHECK-LABEL: func @remsi_base
270283
// CHECK: %[[ret:.*]] = arith.cmpi sge
271284
// CHECK: return %[[ret]]

0 commit comments

Comments
 (0)