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
9 changes: 9 additions & 0 deletions mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ mlir::intrange::inferCeilDivS(ArrayRef<ConstantIntRanges> argRanges) {
result.sadd_ov(APInt(result.getBitWidth(), 1), overflowed);
return overflowed ? std::optional<APInt>() : corrected;
}
// Special case where the usual implementation of ceilDiv causes
// INT_MIN / [positive number] to be positive. This doesn't match the
// definition of signed ceiling division mathematically, but it prevents
// inconsistent constant-folding results. This arises because (-int_min) is
// still negative, so -(-int_min / b) is -(int_min / b), which is
// positive See #115293.
if (lhs.isMinSignedValue() && rhs.sgt(1)) {
return -result;
}
return result;
};
return inferDivSRange(lhs, rhs, ceilDivSIFix);
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/Dialect/Arith/int-range-interface.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ func.func @ceil_divsi(%arg0 : index) -> i1 {
func.return %10 : i1
}

// CHECK-LABEL: func @ceil_divsi_intmin_bug_115293
// CHECK: %[[ret:.*]] = arith.constant true
// CHECK: return %[[ret]]
func.func @ceil_divsi_intmin_bug_115293() -> i1 {
%intMin_i64 = test.with_bounds { smin = -9223372036854775808 : si64, smax = -9223372036854775808 : si64, umin = 9223372036854775808 : ui64, umax = 9223372036854775808 : ui64 } : i64
%denom_i64 = test.with_bounds { smin = 1189465982 : si64, smax = 1189465982 : si64, umin = 1189465982 : ui64, umax = 1189465982 : ui64 } : i64
%res_i64 = test.with_bounds { smin = 7754212542 : si64, smax = 7754212542 : si64, umin = 7754212542 : ui64, umax = 7754212542 : ui64 } : i64

%0 = arith.ceildivsi %intMin_i64, %denom_i64 : i64
%1 = arith.cmpi eq, %0, %res_i64 : i64
func.return %1 : i1
}

// CHECK-LABEL: func @floor_divsi
// CHECK: %[[true:.*]] = arith.constant true
// CHECK: return %[[true]]
Expand Down
Loading