From ad34ce94d5a0eb507de6b53e4fff296830d88c1a Mon Sep 17 00:00:00 2001 From: Arjun P Date: Fri, 26 Nov 2021 20:37:04 +0530 Subject: [PATCH] [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 --- mlir/lib/Analysis/Presburger/Simplex.cpp | 5 +++++ .../Analysis/Presburger/SimplexTest.cpp | 20 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/mlir/lib/Analysis/Presburger/Simplex.cpp b/mlir/lib/Analysis/Presburger/Simplex.cpp index b04060109f2c..dbfc1186db4e 100644 --- a/mlir/lib/Analysis/Presburger/Simplex.cpp +++ b/mlir/lib/Analysis/Presburger/Simplex.cpp @@ -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; } diff --git a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp index d1d725476cc8..cf67193ad8b6 100644 --- a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp +++ b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp @@ -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