Skip to content

MathExtras: add overflow query for signed-div #97901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2024
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
17 changes: 6 additions & 11 deletions llvm/include/llvm/ADT/DynamicAPInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,6 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt dynamicAPIntFromInt64(int64_t X) {
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt mod(const DynamicAPInt &LHS,
const DynamicAPInt &RHS);

namespace detail {
// Division overflows only when trying to negate the minimal signed value.
LLVM_ATTRIBUTE_ALWAYS_INLINE bool divWouldOverflow(int64_t X, int64_t Y) {
return X == std::numeric_limits<int64_t>::min() && Y == -1;
}
} // namespace detail

/// We define the operations here in the header to facilitate inlining.

/// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -338,7 +331,7 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt
DynamicAPInt::operator/(const DynamicAPInt &O) const {
if (LLVM_LIKELY(isSmall() && O.isSmall())) {
// Division overflows only occur when negating the minimal possible value.
if (LLVM_UNLIKELY(detail::divWouldOverflow(getSmall(), O.getSmall())))
if (LLVM_UNLIKELY(divideSignedWouldOverflow(getSmall(), O.getSmall())))
return -*this;
return DynamicAPInt(getSmall() / O.getSmall());
}
Expand All @@ -353,7 +346,8 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt abs(const DynamicAPInt &X) {
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt ceilDiv(const DynamicAPInt &LHS,
const DynamicAPInt &RHS) {
if (LLVM_LIKELY(LHS.isSmall() && RHS.isSmall())) {
if (LLVM_UNLIKELY(detail::divWouldOverflow(LHS.getSmall(), RHS.getSmall())))
if (LLVM_UNLIKELY(
divideSignedWouldOverflow(LHS.getSmall(), RHS.getSmall())))
return -LHS;
return DynamicAPInt(divideCeilSigned(LHS.getSmall(), RHS.getSmall()));
}
Expand All @@ -363,7 +357,8 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt ceilDiv(const DynamicAPInt &LHS,
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt floorDiv(const DynamicAPInt &LHS,
const DynamicAPInt &RHS) {
if (LLVM_LIKELY(LHS.isSmall() && RHS.isSmall())) {
if (LLVM_UNLIKELY(detail::divWouldOverflow(LHS.getSmall(), RHS.getSmall())))
if (LLVM_UNLIKELY(
divideSignedWouldOverflow(LHS.getSmall(), RHS.getSmall())))
return -LHS;
return DynamicAPInt(divideFloorSigned(LHS.getSmall(), RHS.getSmall()));
}
Expand Down Expand Up @@ -473,7 +468,7 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt &
DynamicAPInt::operator/=(const DynamicAPInt &O) {
if (LLVM_LIKELY(isSmall() && O.isSmall())) {
// Division overflows only occur when negating the minimal possible value.
if (LLVM_UNLIKELY(detail::divWouldOverflow(getSmall(), O.getSmall())))
if (LLVM_UNLIKELY(divideSignedWouldOverflow(getSmall(), O.getSmall())))
return *this = -*this;
getSmall() /= O.getSmall();
return *this;
Expand Down
17 changes: 13 additions & 4 deletions llvm/include/llvm/Support/MathExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,20 @@ constexpr uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
return (Numerator - Bias) / Denominator + Bias;
}

// Check whether divideCeilSigned or divideFloorSigned would overflow. This
// happens only when Numerator = INT_MIN and Denominator = -1.
template <typename U, typename V>
constexpr bool divideSignedWouldOverflow(U Numerator, V Denominator) {
return Numerator == std::numeric_limits<U>::min() && Denominator == -1;
}

/// Returns the integer ceil(Numerator / Denominator). Signed version.
/// Guaranteed to never overflow, unless Numerator is INT64_MIN and Denominator
/// is -1.
/// Overflow is explicitly forbidden with an assert.
template <typename U, typename V, typename T = common_sint<U, V>>
constexpr T divideCeilSigned(U Numerator, V Denominator) {
assert(Denominator && "Division by zero");
assert(!divideSignedWouldOverflow(Numerator, Denominator) &&
"Divide would overflow");
if (!Numerator)
return 0;
// C's integer division rounds towards 0.
Expand All @@ -429,11 +437,12 @@ constexpr T divideCeilSigned(U Numerator, V Denominator) {
}

/// Returns the integer floor(Numerator / Denominator). Signed version.
/// Guaranteed to never overflow, unless Numerator is INT64_MIN and Denominator
/// is -1.
/// Overflow is explicitly forbidden with an assert.
template <typename U, typename V, typename T = common_sint<U, V>>
constexpr T divideFloorSigned(U Numerator, V Denominator) {
assert(Denominator && "Division by zero");
assert(!divideSignedWouldOverflow(Numerator, Denominator) &&
"Divide would overflow");
if (!Numerator)
return 0;
// C's integer division rounds towards 0.
Expand Down
8 changes: 8 additions & 0 deletions llvm/unittests/Support/MathExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ TEST(MathExtras, DivideCeil) {
std::numeric_limits<int64_t>::min() / 2 + 1);
EXPECT_EQ(divideCeilSigned(std::numeric_limits<int64_t>::min(), 1),
std::numeric_limits<int64_t>::min());

// Overflow.
EXPECT_TRUE(
divideSignedWouldOverflow(std::numeric_limits<int8_t>::min(), -1));
EXPECT_TRUE(
divideSignedWouldOverflow(std::numeric_limits<int64_t>::min(), -1));
}

TEST(MathExtras, DivideFloorSigned) {
Expand All @@ -544,6 +550,8 @@ TEST(MathExtras, DivideFloorSigned) {
std::numeric_limits<int64_t>::min() / 2);
EXPECT_EQ(divideFloorSigned(std::numeric_limits<int64_t>::min(), 1),
std::numeric_limits<int64_t>::min());

// Same overflow condition, divideSignedWouldOverflow, applies.
}

TEST(MathExtras, Mod) {
Expand Down
11 changes: 3 additions & 8 deletions mlir/lib/IR/AffineExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using namespace mlir::detail;

using llvm::divideCeilSigned;
using llvm::divideFloorSigned;
using llvm::divideSignedWouldOverflow;
using llvm::mod;

MLIRContext *AffineExpr::getContext() const { return expr->context; }
Expand Down Expand Up @@ -859,11 +860,8 @@ static AffineExpr simplifyFloorDiv(AffineExpr lhs, AffineExpr rhs) {
return nullptr;

if (lhsConst) {
// divideFloorSigned can only overflow in this case:
if (lhsConst.getValue() == std::numeric_limits<int64_t>::min() &&
rhsConst.getValue() == -1) {
if (divideSignedWouldOverflow(lhsConst.getValue(), rhsConst.getValue()))
return nullptr;
}
return getAffineConstantExpr(
divideFloorSigned(lhsConst.getValue(), rhsConst.getValue()),
lhs.getContext());
Expand Down Expand Up @@ -921,11 +919,8 @@ static AffineExpr simplifyCeilDiv(AffineExpr lhs, AffineExpr rhs) {
return nullptr;

if (lhsConst) {
// divideCeilSigned can only overflow in this case:
if (lhsConst.getValue() == std::numeric_limits<int64_t>::min() &&
rhsConst.getValue() == -1) {
if (divideSignedWouldOverflow(lhsConst.getValue(), rhsConst.getValue()))
return nullptr;
}
return getAffineConstantExpr(
divideCeilSigned(lhsConst.getValue(), rhsConst.getValue()),
lhs.getContext());
Expand Down
Loading