[Mlir-commits] [mlir] [MLIR][Presburger] Fix Gaussian elimination (PR #164437)

Yue Huang llvmlistbot at llvm.org
Tue Oct 21 08:02:59 PDT 2025


https://github.com/AdUhTkJm created https://github.com/llvm/llvm-project/pull/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 with the elimination parts.

In IntegerRelation.cpp, the Gaussian elimination forgets to advance `firstVar` (the number of finished columns) when it finishes a column.

P.S. I have tested that the implementation works, but how might I add test cases for it? I don't think we have related tests in `mlir/test`.

>From 01f6c4af4095f89a906b8505a5947398c35550d2 Mon Sep 17 00:00:00 2001
From: Yue Huang <yh548 at cam.ac.uk>
Date: Tue, 21 Oct 2025 15:58:58 +0100
Subject: [PATCH] [MLIR][Presburger] Fix Gaussian elimination

---
 mlir/lib/Analysis/Presburger/Barvinok.cpp        | 14 +++++++-------
 mlir/lib/Analysis/Presburger/IntegerRelation.cpp |  4 ++++
 2 files changed, 11 insertions(+), 7 deletions(-)

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 0dcdd5bb97bc8..7bafd070d13d3 100644
--- a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
+++ b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
@@ -1142,6 +1142,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.



More information about the Mlir-commits mailing list