From 445e2b2aa06927a503232516203885cb0ed59ac5 Mon Sep 17 00:00:00 2001 From: Groverkss Date: Tue, 7 Jun 2022 22:49:34 +0530 Subject: [PATCH] [MLIR][Presburger] Fix subtract processing extra inequalities This patch fixes a bug in PresburgeRelation::subtract that made it process the inequality at index 0, multiple times. This was caused by allocating memory instead of reserving memory in llvm::SmallVector. Reviewed By: arjunp Differential Revision: https://reviews.llvm.org/D127228 --- mlir/lib/Analysis/Presburger/PresburgerRelation.cpp | 3 ++- .../Analysis/Presburger/PresburgerSetTest.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp b/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp index 375ea70..4bb726d 100644 --- a/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp +++ b/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp @@ -300,7 +300,8 @@ static PresburgerRelation getSetDifference(IntegerRelation b, canIgnoreIneq[j] = simplex.isMarkedRedundant(offset + j); simplex.rollback(snapshotBeforeIntersect); - SmallVector ineqsToProcess(totalNewSimplexInequalities); + SmallVector ineqsToProcess; + ineqsToProcess.reserve(totalNewSimplexInequalities); for (unsigned i = 0; i < totalNewSimplexInequalities; ++i) if (!canIgnoreIneq[i]) ineqsToProcess.push_back(i); diff --git a/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp b/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp index b21bcc5..fbd3e3c 100644 --- a/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp +++ b/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp @@ -746,3 +746,21 @@ TEST(SetTest, computeVolume) { /*trueVolume=*/{}, /*resultBound=*/{}); } + +TEST(SetTest, subtractOutputSizeRegression) { + PresburgerSet set1 = + parsePresburgerSetFromPolyStrings(1, {"(i) : (i >= 0, 10 - i >= 0)"}); + PresburgerSet set2 = + parsePresburgerSetFromPolyStrings(1, {"(i) : (i - 5 >= 0)"}); + + PresburgerSet set3 = + parsePresburgerSetFromPolyStrings(1, {"(i) : (i >= 0, 4 - i >= 0)"}); + + PresburgerSet result = set1.subtract(set2); + + EXPECT_TRUE(result.isEqual(set3)); + + // Previously, the subtraction result was producing an extra empty set, which + // is correct, but bad for output size. + EXPECT_EQ(result.getNumDisjuncts(), 1u); +} -- 2.7.4