Skip to content

Commit 0f60ec3

Browse files
committed
[MLIR][Presburger] Fix Gaussian elimination
1 parent 02976f5 commit 0f60ec3

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

mlir/lib/Analysis/Presburger/Barvinok.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ mlir::presburger::detail::solveParametricEquations(FracMatrix equations) {
178178
for (unsigned i = 0; i < d; ++i) {
179179
// First ensure that the diagonal element is nonzero, by swapping
180180
// it with a row that is non-zero at column i.
181-
if (equations(i, i) != 0)
182-
continue;
183-
for (unsigned j = i + 1; j < d; ++j) {
184-
if (equations(j, i) == 0)
185-
continue;
186-
equations.swapRows(j, i);
187-
break;
181+
if (equations(i, i) == 0) {
182+
for (unsigned j = i + 1; j < d; ++j) {
183+
if (equations(j, i) == 0)
184+
continue;
185+
equations.swapRows(j, i);
186+
break;
187+
}
188188
}
189189

190190
Fraction diagElement = equations(i, i);

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,15 +1112,28 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
11121112
return posLimit - posStart;
11131113
}
11141114

1115+
static std::optional<unsigned>
1116+
findEqualityWithNonZeroAfterRow(IntegerRelation &rel, unsigned fromRow,
1117+
unsigned colIdx) {
1118+
assert(fromRow < rel.getNumVars() && colIdx < rel.getNumCols() &&
1119+
"position out of bounds");
1120+
for (unsigned rowIdx = fromRow; rowIdx < rel.getNumEqualities(); ++rowIdx) {
1121+
if (rel.atEq(rowIdx, colIdx) != 0)
1122+
return rowIdx;
1123+
}
1124+
return std::nullopt;
1125+
}
1126+
11151127
bool IntegerRelation::gaussianEliminate() {
11161128
gcdTightenInequalities();
11171129
unsigned firstVar = 0, vars = getNumVars();
11181130
unsigned nowDone, eqs;
11191131
std::optional<unsigned> pivotRow;
11201132
for (nowDone = 0, eqs = getNumEqualities(); nowDone < eqs; ++nowDone) {
1121-
// Finds the first non-empty column.
1133+
// Finds the first non-empty column that we haven't dealt with.
11221134
for (; firstVar < vars; ++firstVar) {
1123-
if ((pivotRow = findConstraintWithNonZeroAt(firstVar, /*isEq=*/true)))
1135+
if ((pivotRow =
1136+
findEqualityWithNonZeroAfterRow(*this, nowDone, firstVar)))
11241137
break;
11251138
}
11261139
// The matrix has been normalized to row echelon form.
@@ -1143,6 +1156,10 @@ bool IntegerRelation::gaussianEliminate() {
11431156
inequalities.normalizeRow(i);
11441157
}
11451158
gcdTightenInequalities();
1159+
1160+
// The column is finished. Tell the next iteration to start at the next
1161+
// column.
1162+
firstVar++;
11461163
}
11471164

11481165
// No redundant rows.

mlir/unittests/Analysis/Presburger/BarvinokTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,10 @@ TEST(BarvinokTest, computeNumTermsPolytope) {
301301
gf = count[0].second;
302302
EXPECT_EQ(gf.getNumerators().size(), 24u);
303303
}
304+
305+
TEST(BarvinokTest, solveParametricEquations) {
306+
FracMatrix equations = makeFracMatrix(2, 3, {{2, 3, -4}, {2, 6, -7}});
307+
FracMatrix solution = *solveParametricEquations(equations);
308+
EXPECT_EQ(solution.at(0, 0), Fraction(1, 2));
309+
EXPECT_EQ(solution.at(1, 0), 1);
310+
}

mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,17 @@ TEST(IntegerRelationTest, addLocalModulo) {
725725
EXPECT_TRUE(rel.containsPointNoLocal({x, x % 32}));
726726
}
727727
}
728+
729+
TEST(IntegerRelationTest, simplify) {
730+
IntegerRelation rel =
731+
parseRelationFromSet("(x, y, z): (2*x + y - 4*z - 3 == 0, "
732+
"3*x - y - 3*z + 2 == 0, x + 3*y - 5*N - 8 == 0,"
733+
"x - y + N >= 0)",
734+
2);
735+
IntegerRelation copy = rel;
736+
rel.simplify();
737+
738+
EXPECT_TRUE(rel.isEqual(copy));
739+
// The third equality is redundant and should be removed.
740+
EXPECT_TRUE(rel.getNumEqualities() == 2);
741+
}

0 commit comments

Comments
 (0)