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

0 commit comments

Comments
 (0)