[Mlir-commits] [mlir] [MLIR][Presburger] Conversion between Int- and FracMatrix (PR #192822)

Yue Huang llvmlistbot at llvm.org
Thu Apr 23 02:04:28 PDT 2026


https://github.com/AdUhTkJm updated https://github.com/llvm/llvm-project/pull/192822

>From a5c6e1cf5dfd9b3da9a93ddd42df463aeabf9021 Mon Sep 17 00:00:00 2001
From: Yue Huang <yh548 at cam.ac.uk>
Date: Sun, 19 Apr 2026 12:18:03 +0800
Subject: [PATCH 1/2] [MLIR][Presburger] Conversion between Int- and FracMatrix

---
 mlir/include/mlir/Analysis/Presburger/Matrix.h | 10 ++++++++++
 mlir/lib/Analysis/Presburger/Matrix.cpp        | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 4592efda29f70..5cd97186805cc 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -254,6 +254,7 @@ extern template class Matrix<Fraction>;
 // An inherited class for integer matrices, with no new data attributes.
 // This is only used for the matrix-related methods which apply only
 // to integers (hermite normal form computation and row normalisation).
+class FracMatrix;
 class IntMatrix : public Matrix<DynamicAPInt> {
 public:
   IntMatrix(unsigned rows, unsigned columns, unsigned reservedRows = 0,
@@ -301,6 +302,9 @@ class IntMatrix : public Matrix<DynamicAPInt> {
   // M x M' = M'  M = det(M) x I.
   // Assert-fails if the matrix is not square.
   DynamicAPInt determinant(IntMatrix *inverse = nullptr) const;
+
+  // Converts the matrix into a FracMatrix as-is.
+  FracMatrix asFracMatrix() const;
 };
 
 // An inherited class for rational matrices, with no new data attributes.
@@ -339,6 +343,12 @@ class FracMatrix : public Matrix<Fraction> {
   // Multiply each row of the matrix by the LCM of the denominators, thereby
   // converting it to an integer matrix.
   IntMatrix normalizeRows() const;
+
+  // Converts the matrix to an IntMatrix as-is. If any value in the matrix
+  // is not an integer, the function triggers an assertion failure.
+  // Though equivalent to normalizeRows() when all entries are integers,
+  // the semantics are clearer.
+  IntMatrix asIntMatrix() const;
 };
 
 } // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 94e9eb5792dab..02a3b61b6cf42 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -467,6 +467,15 @@ IntMatrix IntMatrix::identity(unsigned dimension) {
   return matrix;
 }
 
+FracMatrix IntMatrix::asFracMatrix() const {
+  FracMatrix mat(nRows, nColumns);
+  for (unsigned i = 0; i < nRows; i++)
+    for (unsigned j = 0; j < nColumns; j++)
+      mat(i, j) = at(i, j);
+
+  return mat;
+}
+
 std::pair<IntMatrix, IntMatrix> IntMatrix::computeHermiteNormalForm() const {
   // We start with u as an identity matrix and perform operations on h until h
   // is in hermite normal form. We apply the same sequence of operations on u to
@@ -731,6 +740,15 @@ FracMatrix FracMatrix::identity(unsigned dimension) {
   return Matrix::identity(dimension);
 }
 
+IntMatrix FracMatrix::asIntMatrix() const {
+  IntMatrix mat(nRows, nColumns);
+  for (unsigned i = 0; i < nRows; i++)
+    for (unsigned j = 0; j < nColumns; j++)
+      mat(i, j) = at(i, j).getAsInteger();
+
+  return mat;
+}
+
 FracMatrix::FracMatrix(IntMatrix m)
     : FracMatrix(m.getNumRows(), m.getNumColumns()) {
   for (unsigned i = 0, r = m.getNumRows(); i < r; i++)

>From 39b5b422a28550a945e7f6a88521adc7eeab27be Mon Sep 17 00:00:00 2001
From: Yue Huang <yh548 at cam.ac.uk>
Date: Thu, 23 Apr 2026 17:04:08 +0800
Subject: [PATCH 2/2] follow-up commit 1

---
 mlir/include/mlir/Analysis/Presburger/Matrix.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 5cd97186805cc..1f10ea2078ad3 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -346,8 +346,6 @@ class FracMatrix : public Matrix<Fraction> {
 
   // Converts the matrix to an IntMatrix as-is. If any value in the matrix
   // is not an integer, the function triggers an assertion failure.
-  // Though equivalent to normalizeRows() when all entries are integers,
-  // the semantics are clearer.
   IntMatrix asIntMatrix() const;
 };
 



More information about the Mlir-commits mailing list