Skip to content

Commit 0fb216f

Browse files
authored
mlir/MathExtras: consolidate with llvm/MathExtras (#95087)
This patch is part of a project to move the Presburger library into LLVM.
1 parent cc04bbb commit 0fb216f

File tree

30 files changed

+138
-155
lines changed

30 files changed

+138
-155
lines changed

llvm/include/llvm/Support/MathExtras.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,43 @@ template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
424424
return (Value + Align - 1) / Align * Align;
425425
}
426426

427-
/// Returns the integer ceil(Numerator / Denominator).
427+
/// Returns the integer ceil(Numerator / Denominator). Unsigned integer version.
428428
inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
429429
return alignTo(Numerator, Denominator) / Denominator;
430430
}
431431

432+
/// Returns the integer ceil(Numerator / Denominator). Signed integer version.
433+
inline int64_t divideCeilSigned(int64_t Numerator, int64_t Denominator) {
434+
assert(Denominator && "Division by zero");
435+
if (!Numerator)
436+
return 0;
437+
// C's integer division rounds towards 0.
438+
int64_t X = (Denominator > 0) ? -1 : 1;
439+
bool SameSign = (Numerator > 0) == (Denominator > 0);
440+
return SameSign ? ((Numerator + X) / Denominator) + 1
441+
: Numerator / Denominator;
442+
}
443+
444+
/// Returns the integer floor(Numerator / Denominator). Signed integer version.
445+
inline int64_t divideFloorSigned(int64_t Numerator, int64_t Denominator) {
446+
assert(Denominator && "Division by zero");
447+
if (!Numerator)
448+
return 0;
449+
// C's integer division rounds towards 0.
450+
int64_t X = (Denominator > 0) ? -1 : 1;
451+
bool SameSign = (Numerator > 0) == (Denominator > 0);
452+
return SameSign ? Numerator / Denominator
453+
: -((-Numerator + X) / Denominator) - 1;
454+
}
455+
456+
/// Returns the remainder of the Euclidean division of LHS by RHS. Result is
457+
/// always non-negative.
458+
inline int64_t mod(int64_t Numerator, int64_t Denominator) {
459+
assert(Denominator >= 1 && "Mod by non-positive number");
460+
int64_t Mod = Numerator % Denominator;
461+
return Mod < 0 ? Mod + Denominator : Mod;
462+
}
463+
432464
/// Returns the integer nearest(Numerator / Denominator).
433465
inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) {
434466
return (Numerator + (Denominator / 2)) / Denominator;

llvm/unittests/Support/MathExtrasTest.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,39 @@ TEST(MathExtras, IsShiftedInt) {
434434
EXPECT_FALSE((isShiftedInt<6, 10>(int64_t(1) << 15)));
435435
}
436436

437-
template <typename T>
438-
class OverflowTest : public ::testing::Test { };
437+
TEST(MathExtras, DivideCeilSigned) {
438+
EXPECT_EQ(divideCeilSigned(14, 3), 5);
439+
EXPECT_EQ(divideCeilSigned(15, 3), 5);
440+
EXPECT_EQ(divideCeilSigned(14, -3), -4);
441+
EXPECT_EQ(divideCeilSigned(-14, -3), 5);
442+
EXPECT_EQ(divideCeilSigned(-14, 3), -4);
443+
EXPECT_EQ(divideCeilSigned(-15, 3), -5);
444+
EXPECT_EQ(divideCeilSigned(0, 3), 0);
445+
EXPECT_EQ(divideCeilSigned(0, -3), 0);
446+
}
447+
448+
TEST(MathExtras, DivideFloorSigned) {
449+
EXPECT_EQ(divideFloorSigned(14, 3), 4);
450+
EXPECT_EQ(divideFloorSigned(15, 3), 5);
451+
EXPECT_EQ(divideFloorSigned(14, -3), -5);
452+
EXPECT_EQ(divideFloorSigned(-14, -3), 4);
453+
EXPECT_EQ(divideFloorSigned(-14, 3), -5);
454+
EXPECT_EQ(divideFloorSigned(-15, 3), -5);
455+
EXPECT_EQ(divideFloorSigned(0, 3), 0);
456+
EXPECT_EQ(divideFloorSigned(0, -3), 0);
457+
}
458+
459+
TEST(MathExtras, Mod) {
460+
EXPECT_EQ(mod(1, 14), 1);
461+
EXPECT_EQ(mod(-1, 14), 13);
462+
EXPECT_EQ(mod(14, 3), 2);
463+
EXPECT_EQ(mod(15, 3), 0);
464+
EXPECT_EQ(mod(-14, 3), 1);
465+
EXPECT_EQ(mod(-15, 3), 0);
466+
EXPECT_EQ(mod(0, 3), 0);
467+
}
468+
469+
template <typename T> class OverflowTest : public ::testing::Test {};
439470

440471
using OverflowTestTypes = ::testing::Types<signed char, short, int, long,
441472
long long>;
@@ -560,5 +591,4 @@ TYPED_TEST(OverflowTest, MulResultZero) {
560591
EXPECT_FALSE(MulOverflow<TypeParam>(0, -5, Result));
561592
EXPECT_EQ(Result, TypeParam(0));
562593
}
563-
564594
} // namespace

mlir/include/mlir/Analysis/Presburger/Fraction.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#define MLIR_ANALYSIS_PRESBURGER_FRACTION_H
1616

1717
#include "mlir/Analysis/Presburger/MPInt.h"
18-
#include "mlir/Support/MathExtras.h"
1918

2019
namespace mlir {
2120
namespace presburger {

mlir/include/mlir/Analysis/Presburger/MPInt.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,17 @@
1717
#define MLIR_ANALYSIS_PRESBURGER_MPINT_H
1818

1919
#include "mlir/Analysis/Presburger/SlowMPInt.h"
20-
#include "mlir/Support/MathExtras.h"
20+
#include "llvm/ADT/ArrayRef.h"
21+
#include "llvm/Support/MathExtras.h"
2122
#include "llvm/Support/raw_ostream.h"
2223
#include <numeric>
2324

2425
namespace mlir {
2526
namespace presburger {
26-
27-
/// Redefine these functions, which operate on 64-bit ints, to also be part of
28-
/// the mlir::presburger namespace. This is useful because this file defines
29-
/// identically-named functions that operate on MPInts, which would otherwie
30-
/// become the only candidates of overload resolution when calling e.g. ceilDiv
31-
/// from the mlir::presburger namespace. So to access the 64-bit overloads, an
32-
/// explict call to mlir::ceilDiv would be required. These using declarations
33-
/// allow overload resolution to transparently call the right function.
34-
using ::mlir::ceilDiv;
35-
using ::mlir::floorDiv;
36-
using ::mlir::mod;
27+
using ::llvm::ArrayRef;
28+
using ::llvm::divideCeilSigned;
29+
using ::llvm::divideFloorSigned;
30+
using ::llvm::mod;
3731

3832
namespace detail {
3933
/// If builtin intrinsics for overflow-checked arithmetic are available,
@@ -375,7 +369,7 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE MPInt ceilDiv(const MPInt &lhs, const MPInt &rhs) {
375369
if (LLVM_LIKELY(lhs.isSmall() && rhs.isSmall())) {
376370
if (LLVM_UNLIKELY(detail::divWouldOverflow(lhs.getSmall(), rhs.getSmall())))
377371
return -lhs;
378-
return MPInt(ceilDiv(lhs.getSmall(), rhs.getSmall()));
372+
return MPInt(divideCeilSigned(lhs.getSmall(), rhs.getSmall()));
379373
}
380374
return MPInt(ceilDiv(detail::SlowMPInt(lhs), detail::SlowMPInt(rhs)));
381375
}
@@ -384,7 +378,7 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE MPInt floorDiv(const MPInt &lhs,
384378
if (LLVM_LIKELY(lhs.isSmall() && rhs.isSmall())) {
385379
if (LLVM_UNLIKELY(detail::divWouldOverflow(lhs.getSmall(), rhs.getSmall())))
386380
return -lhs;
387-
return MPInt(floorDiv(lhs.getSmall(), rhs.getSmall()));
381+
return MPInt(divideFloorSigned(lhs.getSmall(), rhs.getSmall()));
388382
}
389383
return MPInt(floorDiv(detail::SlowMPInt(lhs), detail::SlowMPInt(rhs)));
390384
}

mlir/include/mlir/Analysis/Presburger/SlowMPInt.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#ifndef MLIR_ANALYSIS_PRESBURGER_SLOWMPINT_H
1919
#define MLIR_ANALYSIS_PRESBURGER_SLOWMPINT_H
2020

21-
#include "mlir/Support/MathExtras.h"
2221
#include "llvm/ADT/APInt.h"
2322
#include "llvm/ADT/Hashing.h"
2423
#include "llvm/Support/raw_ostream.h"

mlir/include/mlir/Dialect/Mesh/IR/MeshOps.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "mlir/IR/SymbolTable.h"
1818
#include "mlir/Interfaces/InferTypeOpInterface.h"
1919
#include "mlir/Interfaces/SideEffectInterfaces.h"
20-
#include "mlir/Support/MathExtras.h"
20+
#include "llvm/Support/MathExtras.h"
2121

2222
namespace mlir {
2323
namespace mesh {
@@ -114,7 +114,7 @@ inline int64_t shardDimension(int64_t dimSize, int64_t shardCount) {
114114
return ShapedType::kDynamic;
115115

116116
assert(dimSize % shardCount == 0);
117-
return ceilDiv(dimSize, shardCount);
117+
return llvm::divideCeilSigned(dimSize, shardCount);
118118
}
119119

120120
// Get the size of an unsharded dimension.

mlir/include/mlir/Support/MathExtras.h

Lines changed: 0 additions & 51 deletions
This file was deleted.

mlir/lib/Analysis/FlatLinearValueConstraints.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "mlir/IR/Builders.h"
1717
#include "mlir/IR/IntegerSet.h"
1818
#include "mlir/Support/LLVM.h"
19-
#include "mlir/Support/MathExtras.h"
2019
#include "llvm/ADT/STLExtras.h"
2120
#include "llvm/ADT/SmallPtrSet.h"
2221
#include "llvm/ADT/SmallVector.h"

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "mlir/IR/SymbolTable.h"
3737
#include "mlir/IR/TypeUtilities.h"
3838
#include "mlir/Support/LogicalResult.h"
39-
#include "mlir/Support/MathExtras.h"
4039
#include "mlir/Transforms/DialectConversion.h"
4140
#include "mlir/Transforms/Passes.h"
4241
#include "llvm/ADT/SmallVector.h"

mlir/lib/Conversion/LLVMCommon/MemRefBuilder.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1313
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
1414
#include "mlir/IR/Builders.h"
15-
#include "mlir/Support/MathExtras.h"
15+
#include "llvm/Support/MathExtras.h"
1616

1717
using namespace mlir;
1818

@@ -363,9 +363,9 @@ void UnrankedMemRefDescriptor::computeSizes(
363363
// Initialize shared constants.
364364
Value one = createIndexAttrConstant(builder, loc, indexType, 1);
365365
Value two = createIndexAttrConstant(builder, loc, indexType, 2);
366-
Value indexSize =
367-
createIndexAttrConstant(builder, loc, indexType,
368-
ceilDiv(typeConverter.getIndexTypeBitwidth(), 8));
366+
Value indexSize = createIndexAttrConstant(
367+
builder, loc, indexType,
368+
llvm::divideCeilSigned(typeConverter.getIndexTypeBitwidth(), 8));
369369

370370
sizes.reserve(sizes.size() + values.size());
371371
for (auto [desc, addressSpace] : llvm::zip(values, addressSpaces)) {
@@ -378,7 +378,8 @@ void UnrankedMemRefDescriptor::computeSizes(
378378
// to data layout) into the unranked descriptor.
379379
Value pointerSize = createIndexAttrConstant(
380380
builder, loc, indexType,
381-
ceilDiv(typeConverter.getPointerBitwidth(addressSpace), 8));
381+
llvm::divideCeilSigned(typeConverter.getPointerBitwidth(addressSpace),
382+
8));
382383
Value doublePointerSize =
383384
builder.create<LLVM::MulOp>(loc, indexType, two, pointerSize);
384385

mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include "mlir/IR/BuiltinTypes.h"
2626
#include "mlir/IR/IRMapping.h"
2727
#include "mlir/Pass/Pass.h"
28-
#include "mlir/Support/MathExtras.h"
2928
#include "llvm/ADT/SmallBitVector.h"
29+
#include "llvm/Support/MathExtras.h"
3030
#include <optional>
3131

3232
namespace mlir {
@@ -971,8 +971,8 @@ struct MemorySpaceCastOpLowering
971971
resultUnderlyingDesc, resultElemPtrType);
972972

973973
int64_t bytesToSkip =
974-
2 *
975-
ceilDiv(getTypeConverter()->getPointerBitwidth(resultAddrSpace), 8);
974+
2 * llvm::divideCeilSigned(
975+
getTypeConverter()->getPointerBitwidth(resultAddrSpace), 8);
976976
Value bytesToSkipConst = rewriter.create<LLVM::ConstantOp>(
977977
loc, getIndexType(), rewriter.getIndexAttr(bytesToSkip));
978978
Value copySize = rewriter.create<LLVM::SubOp>(

mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "mlir/IR/AffineExprVisitor.h"
2222
#include "mlir/IR/IntegerSet.h"
2323
#include "mlir/Support/LLVM.h"
24-
#include "mlir/Support/MathExtras.h"
2524
#include "llvm/ADT/STLExtras.h"
2625
#include "llvm/ADT/SmallPtrSet.h"
2726
#include "llvm/ADT/SmallVector.h"

mlir/lib/Dialect/Affine/Analysis/LoopAnalysis.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "mlir/Dialect/Affine/Analysis/NestedMatcher.h"
1919
#include "mlir/Dialect/Affine/IR/AffineOps.h"
2020
#include "mlir/Dialect/Affine/IR/AffineValueMap.h"
21-
#include "mlir/Support/MathExtras.h"
21+
#include "llvm/Support/MathExtras.h"
2222

2323
#include "llvm/ADT/DenseSet.h"
2424
#include "llvm/ADT/SmallPtrSet.h"
@@ -47,7 +47,8 @@ void mlir::affine::getTripCountMapAndOperands(
4747
loopSpan = ub - lb;
4848
if (loopSpan < 0)
4949
loopSpan = 0;
50-
*tripCountMap = AffineMap::getConstantMap(ceilDiv(loopSpan, step), context);
50+
*tripCountMap = AffineMap::getConstantMap(
51+
llvm::divideCeilSigned(loopSpan, step), context);
5152
tripCountOperands->clear();
5253
return;
5354
}

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818
#include "mlir/IR/PatternMatch.h"
1919
#include "mlir/Interfaces/ShapedOpInterfaces.h"
2020
#include "mlir/Interfaces/ValueBoundsOpInterface.h"
21-
#include "mlir/Support/MathExtras.h"
2221
#include "mlir/Transforms/InliningUtils.h"
2322
#include "llvm/ADT/ScopeExit.h"
2423
#include "llvm/ADT/SmallBitVector.h"
2524
#include "llvm/ADT/SmallVectorExtras.h"
2625
#include "llvm/ADT/TypeSwitch.h"
2726
#include "llvm/Support/Debug.h"
27+
#include "llvm/Support/MathExtras.h"
2828
#include <numeric>
2929
#include <optional>
3030

3131
using namespace mlir;
3232
using namespace mlir::affine;
3333

34+
using llvm::divideCeilSigned;
35+
using llvm::divideFloorSigned;
36+
using llvm::mod;
37+
3438
#define DEBUG_TYPE "affine-ops"
3539

3640
#include "mlir/Dialect/Affine/IR/AffineOpsDialect.cpp.inc"
@@ -824,19 +828,19 @@ static void simplifyExprAndOperands(AffineExpr &expr, unsigned numDims,
824828
// lhs floordiv c is a single value lhs is bounded in a range `c` that has
825829
// the same quotient.
826830
if (binExpr.getKind() == AffineExprKind::FloorDiv &&
827-
floorDiv(lhsLbConstVal, rhsConstVal) ==
828-
floorDiv(lhsUbConstVal, rhsConstVal)) {
829-
expr =
830-
getAffineConstantExpr(floorDiv(lhsLbConstVal, rhsConstVal), context);
831+
divideFloorSigned(lhsLbConstVal, rhsConstVal) ==
832+
divideFloorSigned(lhsUbConstVal, rhsConstVal)) {
833+
expr = getAffineConstantExpr(
834+
divideFloorSigned(lhsLbConstVal, rhsConstVal), context);
831835
return;
832836
}
833837
// lhs ceildiv c is a single value if the entire range has the same ceil
834838
// quotient.
835839
if (binExpr.getKind() == AffineExprKind::CeilDiv &&
836-
ceilDiv(lhsLbConstVal, rhsConstVal) ==
837-
ceilDiv(lhsUbConstVal, rhsConstVal)) {
838-
expr =
839-
getAffineConstantExpr(ceilDiv(lhsLbConstVal, rhsConstVal), context);
840+
divideCeilSigned(lhsLbConstVal, rhsConstVal) ==
841+
divideCeilSigned(lhsUbConstVal, rhsConstVal)) {
842+
expr = getAffineConstantExpr(divideCeilSigned(lhsLbConstVal, rhsConstVal),
843+
context);
840844
return;
841845
}
842846
// lhs mod c is lhs if the entire range has quotient 0 w.r.t the rhs.

mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "mlir/Dialect/SCF/IR/SCF.h"
2424
#include "mlir/IR/IRMapping.h"
2525
#include "mlir/IR/IntegerSet.h"
26-
#include "mlir/Support/MathExtras.h"
2726
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
2827
#include "llvm/ADT/MapVector.h"
2928
#include "llvm/ADT/SmallPtrSet.h"

mlir/lib/Dialect/ControlFlow/IR/ControlFlowOps.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "mlir/IR/PatternMatch.h"
2424
#include "mlir/IR/TypeUtilities.h"
2525
#include "mlir/IR/Value.h"
26-
#include "mlir/Support/MathExtras.h"
2726
#include "mlir/Transforms/InliningUtils.h"
2827
#include "llvm/ADT/APFloat.h"
2928
#include "llvm/ADT/STLExtras.h"

mlir/lib/Dialect/Func/IR/FuncOps.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "mlir/IR/TypeUtilities.h"
2020
#include "mlir/IR/Value.h"
2121
#include "mlir/Interfaces/FunctionImplementation.h"
22-
#include "mlir/Support/MathExtras.h"
2322
#include "mlir/Transforms/InliningUtils.h"
2423
#include "llvm/ADT/APFloat.h"
2524
#include "llvm/ADT/MapVector.h"

0 commit comments

Comments
 (0)