diff --git a/mlir/include/mlir/Analysis/Presburger/Simplex.h b/mlir/include/mlir/Analysis/Presburger/Simplex.h index 283f59ed1298..646598f01a78 100644 --- a/mlir/include/mlir/Analysis/Presburger/Simplex.h +++ b/mlir/include/mlir/Analysis/Presburger/Simplex.h @@ -537,8 +537,11 @@ public: void detectRedundant(); /// Returns a (min, max) pair denoting the minimum and maximum integer values - /// of the given expression. - std::pair computeIntegerBounds(ArrayRef 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> + computeIntegerBounds(ArrayRef 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 ffaf073f8116..4d9123602c69 100644 --- a/mlir/lib/Analysis/Presburger/Simplex.cpp +++ b/mlir/lib/Analysis/Presburger/Simplex.cpp @@ -1456,7 +1456,7 @@ Optional> Simplex::findIntegerSample() { llvm::to_vector<8>(basis.getRow(level)); basisCoeffs.push_back(0); - int64_t minRoundedUp, maxRoundedDown; + Optional minRoundedUp, maxRoundedDown; std::tie(minRoundedUp, maxRoundedDown) = computeIntegerBounds(basisCoeffs); @@ -1475,8 +1475,10 @@ Optional> 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> Simplex::findIntegerSample() { /// Compute the minimum and maximum integer values the expression can take. We /// compute each separately. -std::pair +std::pair, Optional> Simplex::computeIntegerBounds(ArrayRef coeffs) { - int64_t minRoundedUp; + Optional minRoundedUp; if (Optional maybeMin = computeOptimum(Simplex::Direction::Down, coeffs)) minRoundedUp = ceil(*maybeMin); - else - llvm_unreachable("Tableau should not be unbounded"); - int64_t maxRoundedDown; + Optional maxRoundedDown; if (Optional 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 62f600baf071..81ed73afc49e 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(yMin), Optional(yMax))); simplex.rollback(snapshot1); EXPECT_EQ(simplex.getNumVariables(), 1u);