[MLIR] Simplex: fix a bug when rolling back a Simplex with no solutions

Previously, when adding a constraint to a Simplex that is already marked
as having no solutions (marked empty), the Simplex would be marked empty again,
and a second UnmarkEmpty entry would be pushed to the undo log. When rolling
back, Simplex should be unmarked empty only after rolling back past the
creation of the first constraint that made it empty.

Reviewed By: Groverkss

Differential Revision: https://reviews.llvm.org/D114613
This commit is contained in:
Arjun P 2021-11-26 20:37:04 +05:30
parent 715d2dc126
commit ad34ce94d5
2 changed files with 21 additions and 4 deletions

View File

@ -357,6 +357,11 @@ void Simplex::swapColumns(unsigned i, unsigned j) {
/// Mark this tableau empty and push an entry to the undo stack.
void Simplex::markEmpty() {
// If the set is already empty, then we shouldn't add another UnmarkEmpty log
// entry, since in that case the Simplex will be erroneously marked as
// non-empty when rolling back past this point.
if (empty)
return;
undoLog.push_back(UndoLogEntry::UnmarkEmpty);
empty = true;
}

View File

@ -14,19 +14,31 @@
namespace mlir {
/// Take a snapshot, add constraints making the set empty, and rollback.
/// The set should not be empty after rolling back.
/// The set should not be empty after rolling back. We add additional
/// constraints after the set is already empty and roll back the addition
/// of these. The set should be marked non-empty only once we rollback
/// past the addition of the first constraint that made it empty.
TEST(SimplexTest, emptyRollback) {
Simplex simplex(2);
// (u - v) >= 0
simplex.addInequality({1, -1, 0});
EXPECT_FALSE(simplex.isEmpty());
ASSERT_FALSE(simplex.isEmpty());
unsigned snapshot = simplex.getSnapshot();
// (u - v) <= -1
simplex.addInequality({-1, 1, -1});
EXPECT_TRUE(simplex.isEmpty());
ASSERT_TRUE(simplex.isEmpty());
unsigned snapshot2 = simplex.getSnapshot();
// (u - v) <= -3
simplex.addInequality({-1, 1, -3});
ASSERT_TRUE(simplex.isEmpty());
simplex.rollback(snapshot2);
ASSERT_TRUE(simplex.isEmpty());
simplex.rollback(snapshot);
EXPECT_FALSE(simplex.isEmpty());
ASSERT_FALSE(simplex.isEmpty());
}
/// Check that the set gets marked as empty when we add contradictory