[Mlir-commits] [mlir] 099775c - [mlir] Use std::lcm (NFC)

Kazu Hirata llvmlistbot at llvm.org
Sat Aug 27 09:53:39 PDT 2022


Author: Kazu Hirata
Date: 2022-08-27T09:53:15-07:00
New Revision: 099775c2d6609cb03f459393d5bfa4b34e183bc9

URL: https://github.com/llvm/llvm-project/commit/099775c2d6609cb03f459393d5bfa4b34e183bc9
DIFF: https://github.com/llvm/llvm-project/commit/099775c2d6609cb03f459393d5bfa4b34e183bc9.diff

LOG: [mlir] Use std::lcm (NFC)

This patch replaces mlir::lcm with std::lcm, a C++17 feature.

Note that all the arguments to mlir::lcm are of int64_t with no
implicit type conversion as they are passed to mlir::lcm, which I've
verified by modifying mlir::lcm as:

  template <typename TA, typename TB>
  inline int64_t lcm(TA a, TB b) {
    static_assert(std::is_same_v<TA, int64_t>);
    static_assert(std::is_same_v<TB, int64_t>);
    :

Added: 
    

Modified: 
    mlir/include/mlir/Support/MathExtras.h
    mlir/lib/Analysis/Presburger/IntegerRelation.cpp
    mlir/lib/Analysis/Presburger/Simplex.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Support/MathExtras.h b/mlir/include/mlir/Support/MathExtras.h
index ca1d431c9718..17a747393d26 100644
--- a/mlir/include/mlir/Support/MathExtras.h
+++ b/mlir/include/mlir/Support/MathExtras.h
@@ -46,15 +46,6 @@ inline int64_t mod(int64_t lhs, int64_t rhs) {
   assert(rhs >= 1);
   return lhs % rhs < 0 ? lhs % rhs + rhs : lhs % rhs;
 }
-
-/// Returns the least common multiple of 'a' and 'b'.
-inline int64_t lcm(int64_t a, int64_t b) {
-  uint64_t x = std::abs(a);
-  uint64_t y = std::abs(b);
-  int64_t lcm = (x * y) / llvm::GreatestCommonDivisor64(x, y);
-  assert((lcm >= a && lcm >= b) && "LCM overflow");
-  return lcm;
-}
 } // namespace mlir
 
 #endif // MLIR_SUPPORT_MATHEXTRAS_H_

diff  --git a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
index b3fe22c50351..e0c0acdd8f8d 100644
--- a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
+++ b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
@@ -21,6 +21,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/Support/Debug.h"
+#include <numeric>
 
 #define DEBUG_TYPE "presburger"
 
@@ -537,7 +538,7 @@ static void eliminateFromConstraint(IntegerRelation *constraints,
     return;
   int64_t pivotCoeff = constraints->atEq(pivotRow, pivotCol);
   int64_t sign = (leadCoeff * pivotCoeff > 0) ? -1 : 1;
-  int64_t lcm = mlir::lcm(pivotCoeff, leadCoeff);
+  int64_t lcm = std::lcm(pivotCoeff, leadCoeff);
   int64_t pivotMultiplier = sign * (lcm / std::abs(pivotCoeff));
   int64_t rowMultiplier = lcm / std::abs(leadCoeff);
 
@@ -1827,7 +1828,7 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
         if (l == pos)
           continue;
         assert(lbCoeff >= 1 && ubCoeff >= 1 && "bounds wrongly identified");
-        int64_t lcm = mlir::lcm(lbCoeff, ubCoeff);
+        int64_t lcm = std::lcm(lbCoeff, ubCoeff);
         ineq.push_back(atIneq(ubPos, l) * (lcm / ubCoeff) +
                        atIneq(lbPos, l) * (lcm / lbCoeff));
         assert(lcm > 0 && "lcm should be positive!");

diff  --git a/mlir/lib/Analysis/Presburger/Simplex.cpp b/mlir/lib/Analysis/Presburger/Simplex.cpp
index 67fb608ddf82..b856c8931114 100644
--- a/mlir/lib/Analysis/Presburger/Simplex.cpp
+++ b/mlir/lib/Analysis/Presburger/Simplex.cpp
@@ -11,6 +11,7 @@
 #include "mlir/Support/MathExtras.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/Support/Compiler.h"
+#include <numeric>
 
 using namespace mlir;
 using namespace presburger;
@@ -149,7 +150,7 @@ unsigned SimplexBase::addRow(ArrayRef<int64_t> coeffs, bool makeRestricted) {
     // row, scaled by the coefficient for the variable, accounting for the two
     // rows potentially having 
diff erent denominators. The new denominator is
     // the lcm of the two.
-    int64_t lcm = mlir::lcm(tableau(newRow, 0), tableau(pos, 0));
+    int64_t lcm = std::lcm(tableau(newRow, 0), tableau(pos, 0));
     int64_t nRowCoeff = lcm / tableau(newRow, 0);
     int64_t idxRowCoeff = coeffs[i] * (lcm / tableau(pos, 0));
     tableau(newRow, 0) = lcm;


        


More information about the Mlir-commits mailing list