diff --git a/mlir/lib/Analysis/Presburger/Simplex.cpp b/mlir/lib/Analysis/Presburger/Simplex.cpp --- a/mlir/lib/Analysis/Presburger/Simplex.cpp +++ b/mlir/lib/Analysis/Presburger/Simplex.cpp @@ -240,7 +240,7 @@ } normalizeRow(pivotRow); - for (unsigned row = nRedundant; row < nRow; ++row) { + for (unsigned row = 0; row < nRow; ++row) { if (row == pivotRow) continue; if (tableau(row, pivotCol) == 0) // Nothing to do. diff --git a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp --- a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp +++ b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp @@ -11,6 +11,8 @@ #include #include +#include + namespace mlir { /// Take a snapshot, add constraints making the set empty, and rollback. @@ -373,6 +375,60 @@ EXPECT_FALSE(simplex.isMarkedRedundant(5)); } +TEST(Simplextest, pivotRedundantRegressionTest) { + Simplex simplex(2); + simplex.addInequality({-1, 0, -1}); // x <= -1. + unsigned snapshot = simplex.getSnapshot(); + + simplex.addInequality({-1, 0, -2}); // x <= -2. + simplex.addInequality({-3, 0, -6}); + + // This first marks x <= -1 as redundant. Then it performs some more pivots + // to check if the other constraints are redundant. Pivot must update the + // non-redundant rows as well, otherwise these pivots result in an incorrect + // tableau state. + simplex.detectRedundant(); + + // After the rollback, the only remaining constraint is x <= -1. + // The maximum value of x should be -1. + simplex.rollback(snapshot); + Optional maxX = + simplex.computeOptimum(Simplex::Direction::Up, {1, 0, 0}); + EXPECT_TRUE(maxX.hasValue() && *maxX == Fraction(-1, 1)); +} + +// TEST(FlatAffineConstraintsTest, emptinessRandomTests) { +// std::mt19937 rng(0); +// std::uniform_int_distribution distCoeff(-9,9); +// std::uniform_int_distribution distNum(3, 5); + +// for (unsigned it = 0; it <= 100000; ++it) { +// // if (it % 100 == 99) +// // llvm::errs() << it << ' '; +// // (d0, d1, d2, d3) : (d0 >= 0 and d1 + 32 >= 0 and d2 - 2 >= 0 and 30d0 +// + d2 + 32d3 + 29 >= 0 and -30d0 - d2 - 32d3 >= 0 and -2d0 - d1 - d2 - 2 +// >= 0) unsigned dim = distNum(rng); + +// FlatAffineConstraints fac8 = makeFACFromConstraints(dim, {}, {}); +// for (unsigned j = 0; j < numIneq; ++j) { +// SmallVector coeffs; +// for (unsigned k = 0; k <= dim; k++) { +// coeffs.push_back(distCoeff(rng)); +// if (it == 53333 || it == 73294) +// llvm::errs() << coeffs.back() << ' '; +// } +// if (it == 53333 || it == 73294) +// llvm::errs() << '\n'; +// fac8.addInequality(coeffs); +// } + +// if (it == 53333 || it == 73294) +// llvm::errs() << '\n'; +// bool res = fac8.isIntegerEmpty(); +// res = res; +// } +// } + TEST(SimplexTest, addInequality_already_redundant) { Simplex simplex(1); simplex.addInequality({1, -1}); // x >= 1.