[Mlir-commits] [mlir] b4149a0 - [MLIR][Presburger] Fix Gaussian elimination (#164437)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Dec 2 07:35:22 PST 2025
Author: Yue Huang
Date: 2025-12-02T15:35:18Z
New Revision: b4149a013d8ea93c3a34fe88a1eb0a80a8c8b6b9
URL: https://github.com/llvm/llvm-project/commit/b4149a013d8ea93c3a34fe88a1eb0a80a8c8b6b9
DIFF: https://github.com/llvm/llvm-project/commit/b4149a013d8ea93c3a34fe88a1eb0a80a8c8b6b9.diff
LOG: [MLIR][Presburger] Fix Gaussian elimination (#164437)
In the Presburger library, there are two minor bugs of Gaussian
elimination.
In Barvinok.cpp, the `if (equations(i, i) != 0) continue;` is intended
to skip only the row-swapping, but it in fact skipped the whole loop
body altogether, including the elimination parts.
In IntegerRelation.cpp, the Gaussian elimination forgets to advance
`firstVar` (the number of finished columns) when it finishes a column.
Moreover, when it checks the pivot row of each column, it didn't ignore
the rows considered.
As an example, suppose the constraints are
```
1 0 0 1 2 = 0
0 1 0 0 3 = 0
0 0 0 1 4 = 0
...
```
For the 4th column, it will think the pivot is the first row `1 0 0 1 2
= 0`, rather than the correct 3rd row `0 0 0 1 4 = 0`.
(This bug is left undiscovered, because if we don't advance `firstVar`
then this Gaussian elimination process will simply do nothing. Moreover,
it is called only in `simplify()`, and the existing test cases doesn't
care whether a set has been simplified.)
Added:
Modified:
mlir/lib/Analysis/Presburger/Barvinok.cpp
mlir/lib/Analysis/Presburger/IntegerRelation.cpp
mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
Removed:
################################################################################
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
index 75d592e976edf..c31b27794f01e 100644
--- a/mlir/lib/Analysis/Presburger/Barvinok.cpp
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -178,13 +178,13 @@ mlir::presburger::detail::solveParametricEquations(FracMatrix equations) {
for (unsigned i = 0; i < d; ++i) {
// First ensure that the diagonal element is nonzero, by swapping
// it with a row that is non-zero at column i.
- if (equations(i, i) != 0)
- continue;
- for (unsigned j = i + 1; j < d; ++j) {
- if (equations(j, i) == 0)
- continue;
- equations.swapRows(j, i);
- break;
+ if (equations(i, i) == 0) {
+ for (unsigned j = i + 1; j < d; ++j) {
+ if (equations(j, i) == 0)
+ continue;
+ equations.swapRows(j, i);
+ break;
+ }
}
Fraction diagElement = equations(i, i);
diff --git a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
index 949fc2db79809..188ee0bb91b5c 100644
--- a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
+++ b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
@@ -1112,15 +1112,29 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
return posLimit - posStart;
}
+static std::optional<unsigned>
+findEqualityWithNonZeroAfterRow(IntegerRelation &rel, unsigned fromRow,
+ unsigned colIdx) {
+ assert(fromRow < rel.getNumEqualities() && colIdx < rel.getNumCols() &&
+ "position out of bounds");
+ for (unsigned rowIdx = fromRow, e = rel.getNumEqualities(); rowIdx < e;
+ ++rowIdx) {
+ if (rel.atEq(rowIdx, colIdx) != 0)
+ return rowIdx;
+ }
+ return std::nullopt;
+}
+
bool IntegerRelation::gaussianEliminate() {
gcdTightenInequalities();
unsigned firstVar = 0, vars = getNumVars();
unsigned nowDone, eqs;
std::optional<unsigned> pivotRow;
for (nowDone = 0, eqs = getNumEqualities(); nowDone < eqs; ++nowDone) {
- // Finds the first non-empty column.
+ // Finds the first non-empty column that we haven't dealt with.
for (; firstVar < vars; ++firstVar) {
- if ((pivotRow = findConstraintWithNonZeroAt(firstVar, /*isEq=*/true)))
+ if ((pivotRow =
+ findEqualityWithNonZeroAfterRow(*this, nowDone, firstVar)))
break;
}
// The matrix has been normalized to row echelon form.
@@ -1143,6 +1157,10 @@ bool IntegerRelation::gaussianEliminate() {
inequalities.normalizeRow(i);
}
gcdTightenInequalities();
+
+ // The column is finished. Tell the next iteration to start at the next
+ // column.
+ firstVar++;
}
// No redundant rows.
diff --git a/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp b/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
index eaf04379cb529..4ca999878df2c 100644
--- a/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
@@ -301,3 +301,12 @@ TEST(BarvinokTest, computeNumTermsPolytope) {
gf = count[0].second;
EXPECT_EQ(gf.getNumerators().size(), 24u);
}
+
+TEST(BarvinokTest, solveParametricEquations) {
+ FracMatrix equations = makeFracMatrix(2, 3, {{2, 3, -4}, {2, 6, -7}});
+ auto maybeSolution = solveParametricEquations(equations);
+ ASSERT_TRUE(maybeSolution.has_value());
+ FracMatrix solution = *maybeSolution;
+ EXPECT_EQ(solution.at(0, 0), Fraction(1, 2));
+ EXPECT_EQ(solution.at(1, 0), 1);
+}
diff --git a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
index 9ae90a4841f3c..599db4cc74983 100644
--- a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
@@ -725,3 +725,18 @@ TEST(IntegerRelationTest, addLocalModulo) {
EXPECT_TRUE(rel.containsPointNoLocal({x, x % 32}));
}
}
+
+TEST(IntegerRelationTest, simplify) {
+ IntegerRelation rel =
+ parseRelationFromSet("(x, y, z): (2*x + y - 4*z - 3 == 0, "
+ "3*x - y - 3*z + 2 == 0, x + 3*y - 5*z - 8 == 0,"
+ "x - y + z >= 0)",
+ 2);
+ IntegerRelation copy = rel;
+ rel.simplify();
+
+ EXPECT_TRUE(rel.isEqual(copy));
+ // The third equality is redundant and should be removed.
+ // It can be obtained from 2 times the first equality minus the second.
+ EXPECT_TRUE(rel.getNumEqualities() == 2);
+}
More information about the Mlir-commits
mailing list