[Mlir-commits] [mlir] 738c738 - [MLIR][Presburger] Simplex::computeIntegerBounds: support unbounded directions by returning Optionals

Arjun P llvmlistbot at llvm.org
Tue Feb 8 07:27:36 PST 2022


Author: Arjun P
Date: 2022-02-08T20:57:18+05:30
New Revision: 738c738b4492773d7f35b814c016fa512a9c6e74

URL: https://github.com/llvm/llvm-project/commit/738c738b4492773d7f35b814c016fa512a9c6e74
DIFF: https://github.com/llvm/llvm-project/commit/738c738b4492773d7f35b814c016fa512a9c6e74.diff

LOG: [MLIR][Presburger] Simplex::computeIntegerBounds: support unbounded directions by returning Optionals

Added: 
    

Modified: 
    mlir/include/mlir/Analysis/Presburger/Simplex.h
    mlir/lib/Analysis/Presburger/Simplex.cpp
    mlir/unittests/Analysis/Presburger/SimplexTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Analysis/Presburger/Simplex.h b/mlir/include/mlir/Analysis/Presburger/Simplex.h
index 283f59ed12984..646598f01a788 100644
--- a/mlir/include/mlir/Analysis/Presburger/Simplex.h
+++ b/mlir/include/mlir/Analysis/Presburger/Simplex.h
@@ -537,8 +537,11 @@ class Simplex : public SimplexBase {
   void detectRedundant();
 
   /// Returns a (min, max) pair denoting the minimum and maximum integer values
-  /// of the given expression.
-  std::pair<int64_t, int64_t> computeIntegerBounds(ArrayRef<int64_t> coeffs);
+  /// of the given expression. If either of the values is unbounded, an empty
+  /// optional is returned in its place. If the result has min > max then no
+  /// integer value exists.
+  std::pair<Optional<int64_t>, Optional<int64_t>>
+  computeIntegerBounds(ArrayRef<int64_t> coeffs);
 
   /// Returns true if the polytope is unbounded, i.e., extends to infinity in
   /// some direction. Otherwise, returns false.

diff  --git a/mlir/lib/Analysis/Presburger/Simplex.cpp b/mlir/lib/Analysis/Presburger/Simplex.cpp
index ffaf073f81168..4d9123602c69f 100644
--- a/mlir/lib/Analysis/Presburger/Simplex.cpp
+++ b/mlir/lib/Analysis/Presburger/Simplex.cpp
@@ -1456,7 +1456,7 @@ Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() {
           llvm::to_vector<8>(basis.getRow(level));
       basisCoeffs.push_back(0);
 
-      int64_t minRoundedUp, maxRoundedDown;
+      Optional<int64_t> minRoundedUp, maxRoundedDown;
       std::tie(minRoundedUp, maxRoundedDown) =
           computeIntegerBounds(basisCoeffs);
 
@@ -1475,8 +1475,10 @@ Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() {
 
       snapshotStack.push_back(getSnapshot());
       // The smallest value in the range is the next value to try.
-      nextValueStack.push_back(minRoundedUp);
-      upperBoundStack.push_back(maxRoundedDown);
+      // The values in the optionals are guaranteed to exist since we know the
+      // polytope is bounded.
+      nextValueStack.push_back(*minRoundedUp);
+      upperBoundStack.push_back(*maxRoundedDown);
     }
 
     assert((snapshotStack.size() - 1 == level &&
@@ -1513,21 +1515,17 @@ Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() {
 
 /// Compute the minimum and maximum integer values the expression can take. We
 /// compute each separately.
-std::pair<int64_t, int64_t>
+std::pair<Optional<int64_t>, Optional<int64_t>>
 Simplex::computeIntegerBounds(ArrayRef<int64_t> coeffs) {
-  int64_t minRoundedUp;
+  Optional<int64_t> minRoundedUp;
   if (Optional<Fraction> maybeMin =
           computeOptimum(Simplex::Direction::Down, coeffs))
     minRoundedUp = ceil(*maybeMin);
-  else
-    llvm_unreachable("Tableau should not be unbounded");
 
-  int64_t maxRoundedDown;
+  Optional<int64_t> maxRoundedDown;
   if (Optional<Fraction> maybeMax =
           computeOptimum(Simplex::Direction::Up, coeffs))
     maxRoundedDown = floor(*maybeMax);
-  else
-    llvm_unreachable("Tableau should not be unbounded");
 
   return {minRoundedUp, maxRoundedDown};
 }

diff  --git a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp
index 62f600baf071e..81ed73afc49e3 100644
--- a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp
@@ -441,7 +441,7 @@ TEST(SimplexTest, appendVariable) {
   EXPECT_EQ(simplex.getNumVariables(), 2u);
   EXPECT_EQ(simplex.getNumConstraints(), 2u);
   EXPECT_EQ(simplex.computeIntegerBounds({0, 1, 0}),
-            std::make_pair(yMin, yMax));
+            std::make_pair(Optional<int64_t>(yMin), Optional<int64_t>(yMax)));
 
   simplex.rollback(snapshot1);
   EXPECT_EQ(simplex.getNumVariables(), 1u);


        


More information about the Mlir-commits mailing list