Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,14 @@ static ConstantIntRanges inferDivURange(const ConstantIntRanges &lhs,
return minMaxBy(udiv, {lhsMin, lhsMax}, {rhsMin, rhsMax},
/*isSigned=*/false);
}
// Otherwise, it's possible we might divide by 0.
return ConstantIntRanges::maxRange(rhsMin.getBitWidth());

APInt umin = APInt::getZero(rhsMin.getBitWidth());
if (lhsMin.uge(rhsMax) && !rhsMax.isZero())
umin = lhsMin.udiv(rhsMax);

// X u/ Y u<= X.
APInt umax = lhsMax;
return ConstantIntRanges::fromUnsigned(umin, umax);
}

ConstantIntRanges
Expand Down
21 changes: 17 additions & 4 deletions mlir/test/Dialect/Arith/int-range-interface.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func.func @div_bounds_negative(%arg0 : index) -> i1 {
}

// CHECK-LABEL: func @div_zero_undefined
// CHECK: %[[ret:.*]] = arith.cmpi ule
// CHECK: return %[[ret]]
// CHECK: %[[true:.*]] = arith.constant true
// CHECK: return %[[true]]
func.func @div_zero_undefined(%arg0 : index) -> i1 {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
Expand All @@ -190,6 +190,19 @@ func.func @div_zero_undefined(%arg0 : index) -> i1 {
func.return %2 : i1
}

// CHECK-LABEL: func @div_refine_min
// CHECK: %[[true:.*]] = arith.constant true
// CHECK: return %[[true]]
func.func @div_refine_min(%arg0 : index) -> i1 {
%c0 = arith.constant 1 : index
%c1 = arith.constant 2 : index
%c4 = arith.constant 4 : index
%0 = arith.andi %arg0, %c1 : index
%1 = arith.divui %c4, %0 : index
%2 = arith.cmpi uge, %1, %c0 : index
func.return %2 : i1
}

// CHECK-LABEL: func @ceil_divui
// CHECK: %[[ret:.*]] = arith.cmpi eq
// CHECK: return %[[ret]]
Expand Down Expand Up @@ -271,13 +284,13 @@ func.func @remui_base(%arg0 : index, %arg1 : index ) -> i1 {
// CHECK: return %[[true]]
func.func @remui_base_maybe_zero(%arg0 : index, %arg1 : index ) -> i1 {
%c4 = arith.constant 4 : index
%c5 = arith.constant 5 : index
%c5 = arith.constant 5 : index

%0 = arith.minui %arg1, %c4 : index
%1 = arith.remui %arg0, %0 : index
%2 = arith.cmpi ult, %1, %c5 : index
func.return %2 : i1
}
}

// CHECK-LABEL: func @remsi_base
// CHECK: %[[ret:.*]] = arith.cmpi sge
Expand Down
Loading