[Mlir-commits] [mlir] [MLIR][Presburger] Add LLL basis reduction (PR #75565)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Dec 14 23:08:05 PST 2023


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

Add a method for LLL basis reduction to the FracMatrix class.
This needs an abs() method for Fractions, which is added to Fraction.h.

>From 4ddb05a281bacc8c066f7d1ee76063a8a6fd0d26 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Fri, 15 Dec 2023 12:31:09 +0530
Subject: [PATCH 1/4] Add abs method

---
 mlir/include/mlir/Analysis/Presburger/Fraction.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index afcbed84c66bc3..5c440425e429e1 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -101,6 +101,10 @@ inline bool operator>=(const Fraction &x, const Fraction &y) {
   return compare(x, y) >= 0;
 }
 
+inline Fraction abs(const Fraction &f) {
+  return Fraction(abs(f.num), f.den);
+}
+
 inline Fraction reduce(const Fraction &f) {
   if (f == Fraction(0))
     return Fraction(0, 1);

>From 8b18de26e1dfccfa2e031bb4da498a485f71452d Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Fri, 15 Dec 2023 12:31:18 +0530
Subject: [PATCH 2/4] Add lll

---
 .../include/mlir/Analysis/Presburger/Matrix.h |  5 +++
 mlir/lib/Analysis/Presburger/Matrix.cpp       | 35 +++++++++++++++-
 .../Analysis/Presburger/MatrixTest.cpp        | 40 ++++++++++++++++++-
 3 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 89fad85c0c3374..c9d5c5be180c56 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -270,6 +270,11 @@ class FracMatrix : public Matrix<Fraction> {
   // of the rows of matrix (cubic time).
   // The rows of the matrix must be linearly independent.
   FracMatrix gramSchmidt() const;
+
+  // Run LLL basis reduction on the matrix, modifying it in-place.
+  // The parameter is delta.
+  void LLL(Fraction delta);
+
 };
 
 } // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 1fcc6d072b44b7..c2fed286d37bad 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -576,4 +576,37 @@ FracMatrix FracMatrix::gramSchmidt() const {
     }
   }
   return orth;
-}
\ No newline at end of file
+}
+
+void FracMatrix::LLL(Fraction delta)
+{
+    MPInt nearest;
+    Fraction mu;
+
+    FracMatrix bStar = gramSchmidt();
+
+    unsigned k = 1;
+    while (k < getNumRows())
+    {
+        for (unsigned j = k-1; j < k; j--)
+        {
+            mu = dotProduct(getRow(k), bStar.getRow(j)) / dotProduct(bStar.getRow(j), bStar.getRow(j));
+            if (abs(mu) > Fraction(1, 2))
+            {
+                nearest = floor(mu + Fraction(1, 2));
+                addToRow(k, getRow(j), -Fraction(nearest, 1));
+                bStar = gramSchmidt();
+            }
+        }
+        mu = dotProduct(getRow(k), bStar.getRow(k-1)) / dotProduct(bStar.getRow(k-1), bStar.getRow(k-1));
+        if (dotProduct(bStar.getRow(k), bStar.getRow(k)) > (delta - mu*mu) * dotProduct(bStar.getRow(k-1), bStar.getRow(k-1)))
+            k += 1;
+        else
+        {
+            swapRows(k, k-1);
+            bStar = gramSchmidt();
+            k = k > 1 ? k-1 : 1;
+        }
+    }
+    return;
+}
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 508d4fa369c14c..9c5cfb643119e2 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -377,4 +377,42 @@ TEST(MatrixTest, gramSchmidt) {
   gs = mat.gramSchmidt();
 
   EXPECT_EQ_FRAC_MATRIX(gs, FracMatrix::identity(10));
-}
\ No newline at end of file
+}
+
+TEST(MatrixTest, LLL) {
+    FracMatrix mat = makeFracMatrix(3, 3, {{Fraction(1, 1), Fraction(1, 1), Fraction(1, 1)},
+                                                 {Fraction(-1, 1), Fraction(0, 1), Fraction(2, 1)},
+                                                 {Fraction(3, 1), Fraction(5, 1), Fraction(6, 1)}});
+    mat.LLL(Fraction(3, 4));
+    
+    FracMatrix LLL = makeFracMatrix(3, 3, {{Fraction(0, 1), Fraction(1, 1), Fraction(0, 1)},
+                                                 {Fraction(1, 1), Fraction(0, 1), Fraction(1, 1)},
+                                                 {Fraction(-1, 1), Fraction(0, 1), Fraction(2, 1)}});
+
+    for (unsigned row = 0; row < 3; row++)
+      for (unsigned col = 0; col < 3; col++)
+        EXPECT_EQ(mat(row, col), LLL(row, col));
+
+
+    mat = makeFracMatrix(2, 2, {{Fraction(12, 1), Fraction(2, 1)}, {Fraction(13, 1), Fraction(4, 1)}});
+    LLL = makeFracMatrix(2, 2, {{Fraction(1, 1),  Fraction(2, 1)}, {Fraction(9, 1),  Fraction(-4, 1)}});
+
+    mat.LLL(Fraction(3, 4));
+
+    for (unsigned row = 0; row < 2; row++)
+      for (unsigned col = 0; col < 2; col++)
+        EXPECT_EQ(mat(row, col), LLL(row, col));
+
+    mat = makeFracMatrix(3, 3, {{Fraction(1, 1), Fraction(0, 1), Fraction(2, 1)},
+                                {Fraction(0, 1), Fraction(1, 3), -Fraction(5, 3)},
+                                {Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)}});
+    LLL = makeFracMatrix(3, 3, {{Fraction(0, 1), Fraction(1, 3), Fraction(1, 3)},
+                                {Fraction(0, 1), Fraction(1, 3), -Fraction(2, 3)},
+                                {Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)}});
+
+    mat.LLL(Fraction(3, 4));
+
+    for (unsigned row = 0; row < 3; row++)
+      for (unsigned col = 0; col < 3; col++)
+        EXPECT_EQ(mat(row, col), LLL(row, col));
+}

>From e1760faf1aea972223664b050448dfdfad440372 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Fri, 15 Dec 2023 12:32:24 +0530
Subject: [PATCH 3/4] Formatting

---
 .../mlir/Analysis/Presburger/Fraction.h       |  4 +-
 .../include/mlir/Analysis/Presburger/Matrix.h |  1 -
 mlir/lib/Analysis/Presburger/Matrix.cpp       | 59 ++++++++-------
 .../Analysis/Presburger/MatrixTest.cpp        | 71 +++++++++++--------
 4 files changed, 70 insertions(+), 65 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index 5c440425e429e1..e95056ae5fc961 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -101,9 +101,7 @@ inline bool operator>=(const Fraction &x, const Fraction &y) {
   return compare(x, y) >= 0;
 }
 
-inline Fraction abs(const Fraction &f) {
-  return Fraction(abs(f.num), f.den);
-}
+inline Fraction abs(const Fraction &f) { return Fraction(abs(f.num), f.den); }
 
 inline Fraction reduce(const Fraction &f) {
   if (f == Fraction(0))
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index c9d5c5be180c56..fca3164bda6278 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -274,7 +274,6 @@ class FracMatrix : public Matrix<Fraction> {
   // Run LLL basis reduction on the matrix, modifying it in-place.
   // The parameter is delta.
   void LLL(Fraction delta);
-
 };
 
 } // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index c2fed286d37bad..e07bcc6de8ab5c 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -578,35 +578,34 @@ FracMatrix FracMatrix::gramSchmidt() const {
   return orth;
 }
 
-void FracMatrix::LLL(Fraction delta)
-{
-    MPInt nearest;
-    Fraction mu;
-
-    FracMatrix bStar = gramSchmidt();
-
-    unsigned k = 1;
-    while (k < getNumRows())
-    {
-        for (unsigned j = k-1; j < k; j--)
-        {
-            mu = dotProduct(getRow(k), bStar.getRow(j)) / dotProduct(bStar.getRow(j), bStar.getRow(j));
-            if (abs(mu) > Fraction(1, 2))
-            {
-                nearest = floor(mu + Fraction(1, 2));
-                addToRow(k, getRow(j), -Fraction(nearest, 1));
-                bStar = gramSchmidt();
-            }
-        }
-        mu = dotProduct(getRow(k), bStar.getRow(k-1)) / dotProduct(bStar.getRow(k-1), bStar.getRow(k-1));
-        if (dotProduct(bStar.getRow(k), bStar.getRow(k)) > (delta - mu*mu) * dotProduct(bStar.getRow(k-1), bStar.getRow(k-1)))
-            k += 1;
-        else
-        {
-            swapRows(k, k-1);
-            bStar = gramSchmidt();
-            k = k > 1 ? k-1 : 1;
-        }
+void FracMatrix::LLL(Fraction delta) {
+  MPInt nearest;
+  Fraction mu;
+
+  FracMatrix bStar = gramSchmidt();
+
+  unsigned k = 1;
+  while (k < getNumRows()) {
+    for (unsigned j = k - 1; j < k; j--) {
+      mu = dotProduct(getRow(k), bStar.getRow(j)) /
+           dotProduct(bStar.getRow(j), bStar.getRow(j));
+      if (abs(mu) > Fraction(1, 2)) {
+        nearest = floor(mu + Fraction(1, 2));
+        addToRow(k, getRow(j), -Fraction(nearest, 1));
+        bStar = gramSchmidt();
+      }
     }
-    return;
+    mu = dotProduct(getRow(k), bStar.getRow(k - 1)) /
+         dotProduct(bStar.getRow(k - 1), bStar.getRow(k - 1));
+    if (dotProduct(bStar.getRow(k), bStar.getRow(k)) >
+        (delta - mu * mu) *
+            dotProduct(bStar.getRow(k - 1), bStar.getRow(k - 1)))
+      k += 1;
+    else {
+      swapRows(k, k - 1);
+      bStar = gramSchmidt();
+      k = k > 1 ? k - 1 : 1;
+    }
+  }
+  return;
 }
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 9c5cfb643119e2..4d7d0531e5ee84 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -380,39 +380,48 @@ TEST(MatrixTest, gramSchmidt) {
 }
 
 TEST(MatrixTest, LLL) {
-    FracMatrix mat = makeFracMatrix(3, 3, {{Fraction(1, 1), Fraction(1, 1), Fraction(1, 1)},
-                                                 {Fraction(-1, 1), Fraction(0, 1), Fraction(2, 1)},
-                                                 {Fraction(3, 1), Fraction(5, 1), Fraction(6, 1)}});
-    mat.LLL(Fraction(3, 4));
-    
-    FracMatrix LLL = makeFracMatrix(3, 3, {{Fraction(0, 1), Fraction(1, 1), Fraction(0, 1)},
-                                                 {Fraction(1, 1), Fraction(0, 1), Fraction(1, 1)},
-                                                 {Fraction(-1, 1), Fraction(0, 1), Fraction(2, 1)}});
-
-    for (unsigned row = 0; row < 3; row++)
-      for (unsigned col = 0; col < 3; col++)
-        EXPECT_EQ(mat(row, col), LLL(row, col));
-
-
-    mat = makeFracMatrix(2, 2, {{Fraction(12, 1), Fraction(2, 1)}, {Fraction(13, 1), Fraction(4, 1)}});
-    LLL = makeFracMatrix(2, 2, {{Fraction(1, 1),  Fraction(2, 1)}, {Fraction(9, 1),  Fraction(-4, 1)}});
-
-    mat.LLL(Fraction(3, 4));
+  FracMatrix mat =
+      makeFracMatrix(3, 3,
+                     {{Fraction(1, 1), Fraction(1, 1), Fraction(1, 1)},
+                      {Fraction(-1, 1), Fraction(0, 1), Fraction(2, 1)},
+                      {Fraction(3, 1), Fraction(5, 1), Fraction(6, 1)}});
+  mat.LLL(Fraction(3, 4));
+
+  FracMatrix LLL =
+      makeFracMatrix(3, 3,
+                     {{Fraction(0, 1), Fraction(1, 1), Fraction(0, 1)},
+                      {Fraction(1, 1), Fraction(0, 1), Fraction(1, 1)},
+                      {Fraction(-1, 1), Fraction(0, 1), Fraction(2, 1)}});
+
+  for (unsigned row = 0; row < 3; row++)
+    for (unsigned col = 0; col < 3; col++)
+      EXPECT_EQ(mat(row, col), LLL(row, col));
 
-    for (unsigned row = 0; row < 2; row++)
-      for (unsigned col = 0; col < 2; col++)
-        EXPECT_EQ(mat(row, col), LLL(row, col));
+  mat = makeFracMatrix(
+      2, 2,
+      {{Fraction(12, 1), Fraction(2, 1)}, {Fraction(13, 1), Fraction(4, 1)}});
+  LLL = makeFracMatrix(
+      2, 2,
+      {{Fraction(1, 1), Fraction(2, 1)}, {Fraction(9, 1), Fraction(-4, 1)}});
 
-    mat = makeFracMatrix(3, 3, {{Fraction(1, 1), Fraction(0, 1), Fraction(2, 1)},
-                                {Fraction(0, 1), Fraction(1, 3), -Fraction(5, 3)},
-                                {Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)}});
-    LLL = makeFracMatrix(3, 3, {{Fraction(0, 1), Fraction(1, 3), Fraction(1, 3)},
-                                {Fraction(0, 1), Fraction(1, 3), -Fraction(2, 3)},
-                                {Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)}});
+  mat.LLL(Fraction(3, 4));
 
-    mat.LLL(Fraction(3, 4));
+  for (unsigned row = 0; row < 2; row++)
+    for (unsigned col = 0; col < 2; col++)
+      EXPECT_EQ(mat(row, col), LLL(row, col));
 
-    for (unsigned row = 0; row < 3; row++)
-      for (unsigned col = 0; col < 3; col++)
-        EXPECT_EQ(mat(row, col), LLL(row, col));
+  mat = makeFracMatrix(3, 3,
+                       {{Fraction(1, 1), Fraction(0, 1), Fraction(2, 1)},
+                        {Fraction(0, 1), Fraction(1, 3), -Fraction(5, 3)},
+                        {Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)}});
+  LLL = makeFracMatrix(3, 3,
+                       {{Fraction(0, 1), Fraction(1, 3), Fraction(1, 3)},
+                        {Fraction(0, 1), Fraction(1, 3), -Fraction(2, 3)},
+                        {Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)}});
+
+  mat.LLL(Fraction(3, 4));
+
+  for (unsigned row = 0; row < 3; row++)
+    for (unsigned col = 0; col < 3; col++)
+      EXPECT_EQ(mat(row, col), LLL(row, col));
 }

>From 5556d5ea06b05650305ce4a2b272d7c736296384 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Fri, 15 Dec 2023 12:35:33 +0530
Subject: [PATCH 4/4] Comment

---
 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 e07bcc6de8ab5c..ae04c9f9149a5a 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -582,6 +582,9 @@ void FracMatrix::LLL(Fraction delta) {
   MPInt nearest;
   Fraction mu;
 
+  // `bStar` holds the Gram-Schmidt orthogonalisation
+  // of the matrix at all times. It is recomputed every
+  // time the matrix is modified during the algorithm.
   FracMatrix bStar = gramSchmidt();
 
   unsigned k = 1;



More information about the Mlir-commits mailing list