[Mlir-commits] [mlir] [MLIR][Presburger] Fix a bug with determinant of IntMatrix (PR #76622)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Dec 30 10:07:50 PST 2023


https://github.com/Abhinav271828 created https://github.com/llvm/llvm-project/pull/76622

IntMatrix determinant() had a bug where it would try to assign to a null pointer.
A check to avoid this case has been added.

>From d7b066f6dece4fcc96695fe97c13ac275961759b Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 23:33:53 +0530
Subject: [PATCH 1/2] Add condition for null inverse

---
 mlir/lib/Analysis/Presburger/Matrix.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 25300f84cfc047..1f1188c115a8b5 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -452,6 +452,9 @@ MPInt IntMatrix::determinant(IntMatrix *inverse) const {
   if (detM == 0)
     return MPInt(0);
 
+  if (!inverse)
+    return detM;
+
   *inverse = IntMatrix(nRows, nColumns);
   for (unsigned i = 0; i < nRows; i++)
     for (unsigned j = 0; j < nColumns; j++)

>From c14d68968a37debbc45310d4b5a521915ad730e0 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 23:35:54 +0530
Subject: [PATCH 2/2] Add condition for null inverse

---
 mlir/unittests/Analysis/Presburger/MatrixTest.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index e6e452790f82d1..103619518c15a2 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -251,6 +251,9 @@ TEST(MatrixTest, computeHermiteNormalForm) {
 }
 
 TEST(MatrixTest, inverse) {
+  IntMatrix mat1 = makeIntMatrix(2, 2, {{2, 1}, {7, 0}});
+  EXPECT_EQ(mat1.determinant(), -7);
+
   FracMatrix mat = makeFracMatrix(
       2, 2, {{Fraction(2), Fraction(1)}, {Fraction(7), Fraction(0)}});
   FracMatrix inverse = makeFracMatrix(



More information about the Mlir-commits mailing list