diff --git a/mlir/include/mlir/Analysis/Presburger/Simplex.h b/mlir/include/mlir/Analysis/Presburger/Simplex.h index ba974c0ccbb325..66d408dbf8b69c 100644 --- a/mlir/include/mlir/Analysis/Presburger/Simplex.h +++ b/mlir/include/mlir/Analysis/Presburger/Simplex.h @@ -484,6 +484,14 @@ class LexSimplex : public LexSimplexBase { /// any integer sample, use Simplex::findIntegerSample as that is more robust. MaybeOptimum> findIntegerLexMin(); + /// Return whether the specified inequality is redundant/separate for the + /// polytope. Redundant means every point satisfies the given inequality, and + /// separate means no point satisfies it. + /// + /// These checks are integer-exact. + bool isSeparateInequality(ArrayRef coeffs); + bool isRedundantInequality(ArrayRef coeffs); + private: /// Returns the current sample point, which may contain non-integer (rational) /// coordinates. Returns an empty optimum when the tableau is empty. @@ -685,7 +693,7 @@ class Simplex : public SimplexBase { /// which get rolled back on scope exit. class SimplexRollbackScopeExit { public: - SimplexRollbackScopeExit(Simplex &simplex) : simplex(simplex) { + SimplexRollbackScopeExit(SimplexBase &simplex) : simplex(simplex) { snapshot = simplex.getSnapshot(); }; ~SimplexRollbackScopeExit() { simplex.rollback(snapshot); } diff --git a/mlir/lib/Analysis/Presburger/Simplex.cpp b/mlir/lib/Analysis/Presburger/Simplex.cpp index fab9594fbc21e2..57e8f485742d2e 100644 --- a/mlir/lib/Analysis/Presburger/Simplex.cpp +++ b/mlir/lib/Analysis/Presburger/Simplex.cpp @@ -219,6 +219,15 @@ MaybeOptimum> LexSimplex::findIntegerLexMin() { return OptimumKind::Empty; } +bool LexSimplex::isSeparateInequality(ArrayRef coeffs) { + SimplexRollbackScopeExit scopeExit(*this); + addInequality(coeffs); + return findIntegerLexMin().isEmpty(); +} + +bool LexSimplex::isRedundantInequality(ArrayRef coeffs) { + return isSeparateInequality(getComplementIneq(coeffs)); +} bool LexSimplex::rowIsViolated(unsigned row) const { if (tableau(row, 2) < 0) return true; diff --git a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp index e605c89b469222..4c55c922a91b46 100644 --- a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp +++ b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp @@ -548,3 +548,20 @@ TEST(SimplexTest, addDivisionVariable) { ASSERT_TRUE(sample.hasValue()); EXPECT_EQ((*sample)[0] / 2, (*sample)[1]); } + +TEST(SimplexTest, LexIneqType) { + LexSimplex simplex(/*nVar=*/1); + simplex.addInequality({2, -1}); // x >= 1/2. + + // Redundant inequality x >= 2/3. + EXPECT_TRUE(simplex.isRedundantInequality({3, -2})); + EXPECT_FALSE(simplex.isSeparateInequality({3, -2})); + + // Separate inequality x <= 2/3. + EXPECT_FALSE(simplex.isRedundantInequality({-3, 2})); + EXPECT_TRUE(simplex.isSeparateInequality({-3, 2})); + + // Cut inequality x <= 1. + EXPECT_FALSE(simplex.isRedundantInequality({-1, 1})); + EXPECT_FALSE(simplex.isSeparateInequality({-1, 1})); +}