[Mlir-commits] [llvm] [mlir] Reland "mlir/Presburger/MPInt: move into llvm/ADT" (PR #95254)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jun 12 07:17:06 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Ramkumar Ramachandra (artagnon)

<details>
<summary>Changes</summary>

Change: remove guards on debug-printing, to allow Release builds to pass.

MPInt is an arbitrary-precision integer library that builds on top of APInt, and has a fast-path when the number fits within 64 bits. It was originally written for the Presburger library in MLIR, but seems useful to the LLVM project in general, independently of the Presburger library or MLIR. Hence, move it into LLVM/ADT under the name DynamicAPInt.

This patch is part of a project to move the Presburger library into LLVM.

---

Patch is 243.01 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/95254.diff


40 Files Affected:

- (added) llvm/include/llvm/ADT/DynamicAPInt.h (+636) 
- (added) llvm/include/llvm/ADT/SlowDynamicAPInt.h (+136) 
- (modified) llvm/lib/Support/CMakeLists.txt (+2) 
- (added) llvm/lib/Support/DynamicAPInt.cpp (+28) 
- (added) llvm/lib/Support/SlowDynamicAPInt.cpp (+287) 
- (modified) llvm/unittests/ADT/CMakeLists.txt (+1) 
- (added) llvm/unittests/ADT/DynamicAPIntTest.cpp (+200) 
- (modified) mlir/include/mlir/Analysis/Presburger/Barvinok.h (+1-1) 
- (modified) mlir/include/mlir/Analysis/Presburger/Fraction.h (+17-14) 
- (modified) mlir/include/mlir/Analysis/Presburger/IntegerRelation.h (+59-43) 
- (modified) mlir/include/mlir/Analysis/Presburger/LinearTransform.h (+4-2) 
- (removed) mlir/include/mlir/Analysis/Presburger/MPInt.h (-611) 
- (modified) mlir/include/mlir/Analysis/Presburger/Matrix.h (+13-13) 
- (modified) mlir/include/mlir/Analysis/Presburger/PWMAFunction.h (+11-7) 
- (modified) mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h (+4-4) 
- (modified) mlir/include/mlir/Analysis/Presburger/Simplex.h (+21-20) 
- (removed) mlir/include/mlir/Analysis/Presburger/SlowMPInt.h (-135) 
- (modified) mlir/include/mlir/Analysis/Presburger/Utils.h (+34-28) 
- (modified) mlir/include/mlir/Support/LLVM.h (+2) 
- (modified) mlir/lib/Analysis/FlatLinearValueConstraints.cpp (+2-1) 
- (modified) mlir/lib/Analysis/Presburger/Barvinok.cpp (+3-3) 
- (modified) mlir/lib/Analysis/Presburger/CMakeLists.txt (-2) 
- (modified) mlir/lib/Analysis/Presburger/IntegerRelation.cpp (+100-92) 
- (modified) mlir/lib/Analysis/Presburger/LinearTransform.cpp (+6-7) 
- (removed) mlir/lib/Analysis/Presburger/MPInt.cpp (-38) 
- (modified) mlir/lib/Analysis/Presburger/Matrix.cpp (+11-12) 
- (modified) mlir/lib/Analysis/Presburger/PWMAFunction.cpp (+20-19) 
- (modified) mlir/lib/Analysis/Presburger/PresburgerRelation.cpp (+30-28) 
- (modified) mlir/lib/Analysis/Presburger/Simplex.cpp (+88-84) 
- (removed) mlir/lib/Analysis/Presburger/SlowMPInt.cpp (-290) 
- (modified) mlir/lib/Analysis/Presburger/Utils.cpp (+73-64) 
- (modified) mlir/unittests/Analysis/Presburger/CMakeLists.txt (-1) 
- (modified) mlir/unittests/Analysis/Presburger/FractionTest.cpp (+1-1) 
- (modified) mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp (+12-10) 
- (modified) mlir/unittests/Analysis/Presburger/LinearTransformTest.cpp (+2-1) 
- (removed) mlir/unittests/Analysis/Presburger/MPIntTest.cpp (-200) 
- (modified) mlir/unittests/Analysis/Presburger/MatrixTest.cpp (+1-1) 
- (modified) mlir/unittests/Analysis/Presburger/SimplexTest.cpp (+15-14) 
- (modified) mlir/unittests/Analysis/Presburger/Utils.h (+11-9) 
- (modified) mlir/unittests/Analysis/Presburger/UtilsTest.cpp (+28-19) 


``````````diff
diff --git a/llvm/include/llvm/ADT/DynamicAPInt.h b/llvm/include/llvm/ADT/DynamicAPInt.h
new file mode 100644
index 0000000000000..f312f776df971
--- /dev/null
+++ b/llvm/include/llvm/ADT/DynamicAPInt.h
@@ -0,0 +1,636 @@
+//===- DynamicAPInt.h - DynamicAPInt Class ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a simple class to represent arbitrary precision signed integers.
+// Unlike APInt, one does not have to specify a fixed maximum size, and the
+// integer can take on any arbitrary values. This is optimized for small-values
+// by providing fast-paths for the cases when the value stored fits in 64-bits.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_DYNAMICAPINT_H
+#define LLVM_ADT_DYNAMICAPINT_H
+
+#include "llvm/ADT/SlowDynamicAPInt.h"
+#include "llvm/Support/MathExtras.h"
+#include "llvm/Support/raw_ostream.h"
+#include <numeric>
+
+namespace llvm {
+/// This class provides support for dynamic arbitrary-precision arithmetic.
+///
+/// Unlike APInt, this extends the precision as necessary to prevent overflows
+/// and supports operations between objects with differing internal precisions.
+///
+/// This is optimized for small-values by providing fast-paths for the cases
+/// when the value stored fits in 64-bits. We annotate all fastpaths by using
+/// the LLVM_LIKELY/LLVM_UNLIKELY annotations. Removing these would result in
+/// a 1.2x performance slowdown.
+///
+/// We always_inline all operations; removing these results in a 1.5x
+/// performance slowdown.
+///
+/// When HoldsLarge is true, a SlowMPInt is held in the union. If it is false,
+/// the int64_t is held. Using std::variant instead would lead to significantly
+/// worse performance.
+class DynamicAPInt {
+  union {
+    int64_t ValSmall;
+    detail::SlowDynamicAPInt ValLarge;
+  };
+  unsigned HoldsLarge;
+
+  LLVM_ATTRIBUTE_ALWAYS_INLINE void initSmall(int64_t O) {
+    if (LLVM_UNLIKELY(isLarge()))
+      ValLarge.detail::SlowDynamicAPInt::~SlowDynamicAPInt();
+    ValSmall = O;
+    HoldsLarge = false;
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE void
+  initLarge(const detail::SlowDynamicAPInt &O) {
+    if (LLVM_LIKELY(isSmall())) {
+      // The data in memory could be in an arbitrary state, not necessarily
+      // corresponding to any valid state of ValLarge; we cannot call any member
+      // functions, e.g. the assignment operator on it, as they may access the
+      // invalid internal state. We instead construct a new object using
+      // placement new.
+      new (&ValLarge) detail::SlowDynamicAPInt(O);
+    } else {
+      // In this case, we need to use the assignment operator, because if we use
+      // placement-new as above we would lose track of allocated memory
+      // and leak it.
+      ValLarge = O;
+    }
+    HoldsLarge = true;
+  }
+
+  LLVM_ATTRIBUTE_ALWAYS_INLINE explicit DynamicAPInt(
+      const detail::SlowDynamicAPInt &Val)
+      : ValLarge(Val), HoldsLarge(true) {}
+  LLVM_ATTRIBUTE_ALWAYS_INLINE bool isSmall() const { return !HoldsLarge; }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE bool isLarge() const { return HoldsLarge; }
+  /// Get the stored value. For getSmall/Large,
+  /// the stored value should be small/large.
+  LLVM_ATTRIBUTE_ALWAYS_INLINE int64_t getSmall() const {
+    assert(isSmall() &&
+           "getSmall should only be called when the value stored is small!");
+    return ValSmall;
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE int64_t &getSmall() {
+    assert(isSmall() &&
+           "getSmall should only be called when the value stored is small!");
+    return ValSmall;
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE const detail::SlowDynamicAPInt &
+  getLarge() const {
+    assert(isLarge() &&
+           "getLarge should only be called when the value stored is large!");
+    return ValLarge;
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE detail::SlowDynamicAPInt &getLarge() {
+    assert(isLarge() &&
+           "getLarge should only be called when the value stored is large!");
+    return ValLarge;
+  }
+  explicit operator detail::SlowDynamicAPInt() const {
+    if (isSmall())
+      return detail::SlowDynamicAPInt(getSmall());
+    return getLarge();
+  }
+
+public:
+  LLVM_ATTRIBUTE_ALWAYS_INLINE explicit DynamicAPInt(int64_t Val)
+      : ValSmall(Val), HoldsLarge(false) {}
+  LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt() : DynamicAPInt(0) {}
+  LLVM_ATTRIBUTE_ALWAYS_INLINE ~DynamicAPInt() {
+    if (LLVM_UNLIKELY(isLarge()))
+      ValLarge.detail::SlowDynamicAPInt::~SlowDynamicAPInt();
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt(const DynamicAPInt &O)
+      : ValSmall(O.ValSmall), HoldsLarge(false) {
+    if (LLVM_UNLIKELY(O.isLarge()))
+      initLarge(O.ValLarge);
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt &operator=(const DynamicAPInt &O) {
+    if (LLVM_LIKELY(O.isSmall())) {
+      initSmall(O.ValSmall);
+      return *this;
+    }
+    initLarge(O.ValLarge);
+    return *this;
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt &operator=(int X) {
+    initSmall(X);
+    return *this;
+  }
+  LLVM_ATTRIBUTE_ALWAYS_INLINE explicit operator int64_t() const {
+    if (isSmall())
+      return getSmall();
+    return static_cast<int64_t>(getLarge());
+  }
+
+  bool operator==(const DynamicAPInt &O) const;
+  bool operator!=(const DynamicAPInt &O) const;
+  bool operator>(const DynamicAPInt &O) const;
+  bool operator<(const DynamicAPInt &O) const;
+  bool operator<=(const DynamicAPInt &O) const;
+  bool operator>=(const DynamicAPInt &O) const;
+  DynamicAPInt operator+(const DynamicAPInt &O) const;
+  DynamicAPInt operator-(const DynamicAPInt &O) const;
+  DynamicAPInt operator*(const DynamicAPInt &O) const;
+  DynamicAPInt operator/(const DynamicAPInt &O) const;
+  DynamicAPInt operator%(const DynamicAPInt &O) const;
+  DynamicAPInt &operator+=(const DynamicAPInt &O);
+  DynamicAPInt &operator-=(const DynamicAPInt &O);
+  DynamicAPInt &operator*=(const DynamicAPInt &O);
+  DynamicAPInt &operator/=(const DynamicAPInt &O);
+  DynamicAPInt &operator%=(const DynamicAPInt &O);
+  DynamicAPInt operator-() const;
+  DynamicAPInt &operator++();
+  DynamicAPInt &operator--();
+
+  // Divide by a number that is known to be positive.
+  // This is slightly more efficient because it saves an overflow check.
+  DynamicAPInt divByPositive(const DynamicAPInt &O) const;
+  DynamicAPInt &divByPositiveInPlace(const DynamicAPInt &O);
+
+  friend DynamicAPInt abs(const DynamicAPInt &X);
+  friend DynamicAPInt ceilDiv(const DynamicAPInt &LHS, const DynamicAPInt &RHS);
+  friend DynamicAPInt floorDiv(const DynamicAPInt &LHS,
+                               const DynamicAPInt &RHS);
+  // The operands must be non-negative for gcd.
+  friend DynamicAPInt gcd(const DynamicAPInt &A, const DynamicAPInt &B);
+  friend DynamicAPInt lcm(const DynamicAPInt &A, const DynamicAPInt &B);
+  friend DynamicAPInt mod(const DynamicAPInt &LHS, const DynamicAPInt &RHS);
+
+  /// ---------------------------------------------------------------------------
+  /// Convenience operator overloads for int64_t.
+  /// ---------------------------------------------------------------------------
+  friend DynamicAPInt &operator+=(DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt &operator-=(DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt &operator*=(DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt &operator/=(DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt &operator%=(DynamicAPInt &A, int64_t B);
+
+  friend bool operator==(const DynamicAPInt &A, int64_t B);
+  friend bool operator!=(const DynamicAPInt &A, int64_t B);
+  friend bool operator>(const DynamicAPInt &A, int64_t B);
+  friend bool operator<(const DynamicAPInt &A, int64_t B);
+  friend bool operator<=(const DynamicAPInt &A, int64_t B);
+  friend bool operator>=(const DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt operator+(const DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt operator-(const DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt operator*(const DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt operator/(const DynamicAPInt &A, int64_t B);
+  friend DynamicAPInt operator%(const DynamicAPInt &A, int64_t B);
+
+  friend bool operator==(int64_t A, const DynamicAPInt &B);
+  friend bool operator!=(int64_t A, const DynamicAPInt &B);
+  friend bool operator>(int64_t A, const DynamicAPInt &B);
+  friend bool operator<(int64_t A, const DynamicAPInt &B);
+  friend bool operator<=(int64_t A, const DynamicAPInt &B);
+  friend bool operator>=(int64_t A, const DynamicAPInt &B);
+  friend DynamicAPInt operator+(int64_t A, const DynamicAPInt &B);
+  friend DynamicAPInt operator-(int64_t A, const DynamicAPInt &B);
+  friend DynamicAPInt operator*(int64_t A, const DynamicAPInt &B);
+  friend DynamicAPInt operator/(int64_t A, const DynamicAPInt &B);
+  friend DynamicAPInt operator%(int64_t A, const DynamicAPInt &B);
+
+  friend hash_code hash_value(const DynamicAPInt &x); // NOLINT
+
+  raw_ostream &print(raw_ostream &OS) const;
+  LLVM_DUMP_METHOD void dump() const;
+};
+
+inline raw_ostream &operator<<(raw_ostream &OS, const DynamicAPInt &X) {
+  X.print(OS);
+  return OS;
+}
+
+/// Redeclarations of friend declaration above to
+/// make it discoverable by lookups.
+hash_code hash_value(const DynamicAPInt &X); // NOLINT
+
+/// This just calls through to the operator int64_t, but it's useful when a
+/// function pointer is required. (Although this is marked inline, it is still
+/// possible to obtain and use a function pointer to this.)
+static inline int64_t int64fromDynamicAPInt(const DynamicAPInt &X) {
+  return int64_t(X);
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt dynamicAPIntFromInt64(int64_t X) {
+  return DynamicAPInt(X);
+}
+
+// The RHS is always expected to be positive, and the result
+/// is always non-negative.
+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.
+
+/// ---------------------------------------------------------------------------
+/// Comparison operators.
+/// ---------------------------------------------------------------------------
+LLVM_ATTRIBUTE_ALWAYS_INLINE bool
+DynamicAPInt::operator==(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return getSmall() == O.getSmall();
+  return detail::SlowDynamicAPInt(*this) == detail::SlowDynamicAPInt(O);
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE bool
+DynamicAPInt::operator!=(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return getSmall() != O.getSmall();
+  return detail::SlowDynamicAPInt(*this) != detail::SlowDynamicAPInt(O);
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE bool
+DynamicAPInt::operator>(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return getSmall() > O.getSmall();
+  return detail::SlowDynamicAPInt(*this) > detail::SlowDynamicAPInt(O);
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE bool
+DynamicAPInt::operator<(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return getSmall() < O.getSmall();
+  return detail::SlowDynamicAPInt(*this) < detail::SlowDynamicAPInt(O);
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE bool
+DynamicAPInt::operator<=(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return getSmall() <= O.getSmall();
+  return detail::SlowDynamicAPInt(*this) <= detail::SlowDynamicAPInt(O);
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE bool
+DynamicAPInt::operator>=(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return getSmall() >= O.getSmall();
+  return detail::SlowDynamicAPInt(*this) >= detail::SlowDynamicAPInt(O);
+}
+
+/// ---------------------------------------------------------------------------
+/// Arithmetic operators.
+/// ---------------------------------------------------------------------------
+
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt
+DynamicAPInt::operator+(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall())) {
+    DynamicAPInt Result;
+    bool Overflow = AddOverflow(getSmall(), O.getSmall(), Result.getSmall());
+    if (LLVM_LIKELY(!Overflow))
+      return Result;
+    return DynamicAPInt(detail::SlowDynamicAPInt(*this) +
+                        detail::SlowDynamicAPInt(O));
+  }
+  return DynamicAPInt(detail::SlowDynamicAPInt(*this) +
+                      detail::SlowDynamicAPInt(O));
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt
+DynamicAPInt::operator-(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall())) {
+    DynamicAPInt Result;
+    bool Overflow = SubOverflow(getSmall(), O.getSmall(), Result.getSmall());
+    if (LLVM_LIKELY(!Overflow))
+      return Result;
+    return DynamicAPInt(detail::SlowDynamicAPInt(*this) -
+                        detail::SlowDynamicAPInt(O));
+  }
+  return DynamicAPInt(detail::SlowDynamicAPInt(*this) -
+                      detail::SlowDynamicAPInt(O));
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt
+DynamicAPInt::operator*(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall())) {
+    DynamicAPInt Result;
+    bool Overflow = MulOverflow(getSmall(), O.getSmall(), Result.getSmall());
+    if (LLVM_LIKELY(!Overflow))
+      return Result;
+    return DynamicAPInt(detail::SlowDynamicAPInt(*this) *
+                        detail::SlowDynamicAPInt(O));
+  }
+  return DynamicAPInt(detail::SlowDynamicAPInt(*this) *
+                      detail::SlowDynamicAPInt(O));
+}
+
+// Division overflows only occur when negating the minimal possible value.
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt
+DynamicAPInt::divByPositive(const DynamicAPInt &O) const {
+  assert(O > 0);
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return DynamicAPInt(getSmall() / O.getSmall());
+  return DynamicAPInt(detail::SlowDynamicAPInt(*this) /
+                      detail::SlowDynamicAPInt(O));
+}
+
+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())))
+      return -*this;
+    return DynamicAPInt(getSmall() / O.getSmall());
+  }
+  return DynamicAPInt(detail::SlowDynamicAPInt(*this) /
+                      detail::SlowDynamicAPInt(O));
+}
+
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt abs(const DynamicAPInt &X) {
+  return DynamicAPInt(X >= 0 ? X : -X);
+}
+// Division overflows only occur when negating the minimal possible value.
+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())))
+      return -LHS;
+    return DynamicAPInt(divideCeilSigned(LHS.getSmall(), RHS.getSmall()));
+  }
+  return DynamicAPInt(
+      ceilDiv(detail::SlowDynamicAPInt(LHS), detail::SlowDynamicAPInt(RHS)));
+}
+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())))
+      return -LHS;
+    return DynamicAPInt(divideFloorSigned(LHS.getSmall(), RHS.getSmall()));
+  }
+  return DynamicAPInt(
+      floorDiv(detail::SlowDynamicAPInt(LHS), detail::SlowDynamicAPInt(RHS)));
+}
+// The RHS is always expected to be positive, and the result
+/// is always non-negative.
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt mod(const DynamicAPInt &LHS,
+                                              const DynamicAPInt &RHS) {
+  if (LLVM_LIKELY(LHS.isSmall() && RHS.isSmall()))
+    return DynamicAPInt(mod(LHS.getSmall(), RHS.getSmall()));
+  return DynamicAPInt(
+      mod(detail::SlowDynamicAPInt(LHS), detail::SlowDynamicAPInt(RHS)));
+}
+
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt gcd(const DynamicAPInt &A,
+                                              const DynamicAPInt &B) {
+  assert(A >= 0 && B >= 0 && "operands must be non-negative!");
+  if (LLVM_LIKELY(A.isSmall() && B.isSmall()))
+    return DynamicAPInt(std::gcd(A.getSmall(), B.getSmall()));
+  return DynamicAPInt(
+      gcd(detail::SlowDynamicAPInt(A), detail::SlowDynamicAPInt(B)));
+}
+
+/// Returns the least common multiple of A and B.
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt lcm(const DynamicAPInt &A,
+                                              const DynamicAPInt &B) {
+  DynamicAPInt X = abs(A);
+  DynamicAPInt Y = abs(B);
+  return (X * Y) / gcd(X, Y);
+}
+
+/// This operation cannot overflow.
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt
+DynamicAPInt::operator%(const DynamicAPInt &O) const {
+  if (LLVM_LIKELY(isSmall() && O.isSmall()))
+    return DynamicAPInt(getSmall() % O.getSmall());
+  return DynamicAPInt(detail::SlowDynamicAPInt(*this) %
+                      detail::SlowDynamicAPInt(O));
+}
+
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt DynamicAPInt::operator-() const {
+  if (LLVM_LIKELY(isSmall())) {
+    if (LLVM_LIKELY(getSmall() != std::numeric_limits<int64_t>::min()))
+      return DynamicAPInt(-getSmall());
+    return DynamicAPInt(-detail::SlowDynamicAPInt(*this));
+  }
+  return DynamicAPInt(-detail::SlowDynamicAPInt(*this));
+}
+
+/// ---------------------------------------------------------------------------
+/// Assignment operators, preincrement, predecrement.
+/// ---------------------------------------------------------------------------
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt &
+DynamicAPInt::operator+=(const DynamicAPInt &O) {
+  if (LLVM_LIKELY(isSmall() && O.isSmall())) {
+    int64_t Result = getSmall();
+    bool Overflow = AddOverflow(getSmall(), O.getSmall(), Result);
+    if (LLVM_LIKELY(!Overflow)) {
+      getSmall() = Result;
+      return *this;
+    }
+    // Note: this return is not strictly required but
+    // removing it leads to a performance regression.
+    return *this = DynamicAPInt(detail::SlowDynamicAPInt(*this) +
+                                detail::SlowDynamicAPInt(O));
+  }
+  return *this = DynamicAPInt(detail::SlowDynamicAPInt(*this) +
+                              detail::SlowDynamicAPInt(O));
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt &
+DynamicAPInt::operator-=(const DynamicAPInt &O) {
+  if (LLVM_LIKELY(isSmall() && O.isSmall())) {
+    int64_t Result = getSmall();
+    bool Overflow = SubOverflow(getSmall(), O.getSmall(), Result);
+    if (LLVM_LIKELY(!Overflow)) {
+      getSmall() = Result;
+      return *this;
+    }
+    // Note: this return is not strictly required but
+    // removing it leads to a performance regression.
+    return *this = DynamicAPInt(detail::SlowDynamicAPInt(*this) -
+                                detail::SlowDynamicAPInt(O));
+  }
+  return *this = DynamicAPInt(detail::SlowDynamicAPInt(*this) -
+                              detail::SlowDynamicAPInt(O));
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt &
+DynamicAPInt::operator*=(const DynamicAPInt &O) {
+  if (LLVM_LIKELY(isSmall() && O.isSmall())) {
+    int64_t Result = getSmall();
+    bool Overflow = MulOverflow(getSmall(), O.getSmall(), Result);
+    if (LLVM_LIKELY(!Overflow)) {
+      getSmall() = Result;
+      return *this;
+    }
+    // Note: this return is not strictly required but
+    // removing it leads to a performance regression.
+    return *this = DynamicAPInt(detail::SlowDynamicAPInt(*this) *
+                                detail::SlowDynamicAPInt(O));
+  }
+  return *this = DynamicAPInt(detail::SlowDynamicAPInt(*this) *
+                              detail::SlowDynamicAPInt(O));
+}
+LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt &
+Dynam...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/95254


More information about the Mlir-commits mailing list