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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun May 3 10:12:54 PDT 2026


Author: Yue Huang
Date: 2026-05-03T17:12:49Z
New Revision: 35c7191f3a1a4e0fec44fa14331f03f2d8df2fc2

URL: https://github.com/llvm/llvm-project/commit/35c7191f3a1a4e0fec44fa14331f03f2d8df2fc2
DIFF: https://github.com/llvm/llvm-project/commit/35c7191f3a1a4e0fec44fa14331f03f2d8df2fc2.diff

LOG: [MLIR][Presburger] Conversion between Int- and FracMatrix (#192822)

A straightforward conversion between `IntMatrix` and `FracMatrix`. This
is one further preparation PR.

The next step for upstreaming is to find a particular solution `x` to
the system `Ax = Bp + C`, which might contain fractions while `A`, `B`
and `C` are IntMatrices. That's the reason we need these conversion
helpers.

---------

Co-authored-by: Arjun Pitchanathan <arjunpitchanathan at gmail.com>

Added: 
    

Modified: 
    mlir/include/mlir/Analysis/Presburger/Matrix.h
    mlir/lib/Analysis/Presburger/Matrix.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 4592efda29f70..1f10ea2078ad3 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,10 @@ 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.
+  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++)


        


More information about the Mlir-commits mailing list