[Mlir-commits] [mlir] [MLIR][Presburger] Definitions for basic functions related to cones (PR #76650)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jan 4 08:08:58 PST 2024


https://github.com/Abhinav271828 updated https://github.com/llvm/llvm-project/pull/76650

>From e500c63cccbe4a332e2667350781bcbb229e6b55 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 22:45:31 +0530
Subject: [PATCH 01/15] initial commit

---
 .../mlir/Analysis/Presburger/Barvinok.h       | 54 +++++++++++++++
 .../Analysis/Presburger/GeneratingFunction.h  |  4 +-
 mlir/lib/Analysis/Presburger/Barvinok.cpp     | 65 +++++++++++++++++++
 mlir/lib/Analysis/Presburger/CMakeLists.txt   |  1 +
 4 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 mlir/include/mlir/Analysis/Presburger/Barvinok.h
 rename mlir/{lib => include/mlir}/Analysis/Presburger/GeneratingFunction.h (97%)
 create mode 100644 mlir/lib/Analysis/Presburger/Barvinok.cpp

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
new file mode 100644
index 00000000000000..c63ee1e230c8bf
--- /dev/null
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -0,0 +1,54 @@
+//===- Barvinok.h - Barvinok's Algorithm ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Functions relating to Barvinok's algorithm.
+// These include functions to manipulate cones (define a cone object, get its
+// dual, and find its index).
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
+#define MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
+
+#include "mlir/Analysis/Presburger/Matrix.h"
+#include "mlir/Analysis/Presburger/IntegerRelation.h"
+#include <optional>
+
+namespace mlir {
+namespace presburger {
+
+using PolyhedronH = IntegerRelation;
+using PolyhedronV = IntMatrix;
+using ConeH = PolyhedronH;
+using ConeV = PolyhedronV;
+
+inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0)
+{
+    // We don't distinguish between domain and range variables, so
+    // we set the number of domain variables as 0 and the number of
+    // range variables as the number of actual variables.
+    // There are no symbols (non-parametric for now) and no local
+    // (existentially quantified) variables.
+    ConeH cone(PresburgerSpace::getRelationSpace(0, num_vars, num_params, 0));
+    return cone;
+}
+
+// Get the index of a cone.
+// If it has more rays than the dimension, return 0.
+MPInt getIndex(ConeV);
+
+// Get the dual of a cone in H-representation, returning the V-representation of it.
+ConeV getDual(ConeH);
+
+// Get the dual of a cone in V-representation, returning the H-representation of it.
+ConeH getDual(ConeV);
+
+} // namespace presburger
+} // namespace mlir
+
+#endif // MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/GeneratingFunction.h b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
similarity index 97%
rename from mlir/lib/Analysis/Presburger/GeneratingFunction.h
rename to mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
index f7deba921ea51e..770d17fe17c307 100644
--- a/mlir/lib/Analysis/Presburger/GeneratingFunction.h
+++ b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
@@ -19,6 +19,7 @@
 
 namespace mlir {
 namespace presburger {
+namespace internal {
 
 // A parametric point is a vector, each of whose elements
 // is an affine function of n parameters. Each row
@@ -128,7 +129,8 @@ class GeneratingFunction {
   std::vector<std::vector<Point>> denominators;
 };
 
+} // namespace internal
 } // namespace presburger
 } // namespace mlir
 
-#endif // MLIR_ANALYSIS_PRESBURGER_GENERATINGFUNCTION_H
+#endif // MLIR_ANALYSIS_PRESBURGER_GENERATINGFUNCTION_H
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
new file mode 100644
index 00000000000000..ddb24d1a79993f
--- /dev/null
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -0,0 +1,65 @@
+//===- QuasiPolynomial.cpp - Barvinok's Algorithm ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Analysis/Presburger/Barvinok.h"
+
+using namespace mlir;
+using namespace presburger;
+
+// Assuming that the input cone is pointed at the origin,
+// converts it to its dual in V-representation.
+// Essentially we just remove the all-zeroes constant column.
+ConeV mlir::presburger::getDual(ConeH cone)
+{
+    ConeV dual(cone.getNumInequalities(), cone.getNumCols()-1, 0, 0);
+    // Assuming that an inequality of the form
+    // a1*x1 + ... + an*xn + b ≥ 0
+    // is represented as a row [a1, ..., an, b]
+    // and that b = 0.
+
+    for (unsigned i = 0; i < cone.getNumInequalities(); i++)
+    {
+        assert(dual.at(i, cone.getNumCols()-1) == 0 && "H-representation of cone is not centred at the origin!");
+        for (unsigned j = 0; j < cone.getNumCols()-1; j++)
+        {
+            dual.at(i, j) = cone.atIneq(i, j);
+        }
+    }
+
+    // Now dual is of the form [ [a1, ..., an] , ... ]
+    // which is the V-representation of the dual.
+    return dual;
+}
+
+// Converts a cone in V-representation to the H-representation
+// of its dual, pointed at the origin (not at the original vertex).
+// Essentially adds a column consisting only of zeroes to the end.
+ConeH mlir::presburger::getDual(ConeV cone)
+{
+    ConeH dual = defineHRep(cone.getNumRows(), cone.getNumColumns());
+    cone.insertColumn(cone.getNumColumns());
+
+    for (unsigned i = 0; i < cone.getNumRows(); i++)
+        dual.addInequality(cone.getRow(i));
+
+    // Now dual is of the form [ [a1, ..., an, 0] , ... ]
+    // which is the H-representation of the dual.
+    return dual;
+}
+
+// Find the index of a cone in V-representation.
+// If there are more rays than variables, return 0.
+MPInt mlir::presburger::getIndex(ConeV cone)
+{
+    unsigned rows = cone.getNumRows();
+    unsigned cols = cone.getNumColumns();
+    if (rows > cols)
+        return MPInt(0);
+
+    return cone.determinant();
+}
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/CMakeLists.txt b/mlir/lib/Analysis/Presburger/CMakeLists.txt
index e77e1623dae175..83d0514c9e7d17 100644
--- a/mlir/lib/Analysis/Presburger/CMakeLists.txt
+++ b/mlir/lib/Analysis/Presburger/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_mlir_library(MLIRPresburger
+  Barvinok.cpp
   IntegerRelation.cpp
   LinearTransform.cpp
   Matrix.cpp

>From b2566609ac319a9effad78405e7c8f948b3490ce Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 22:53:15 +0530
Subject: [PATCH 02/15] Formatting

---
 .../mlir/Analysis/Presburger/Barvinok.h       | 33 +++++----
 mlir/lib/Analysis/Presburger/Barvinok.cpp     | 70 +++++++++----------
 2 files changed, 55 insertions(+), 48 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index c63ee1e230c8bf..6ffdf96502fd20 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -15,37 +15,46 @@
 #ifndef MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
 #define MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
 
-#include "mlir/Analysis/Presburger/Matrix.h"
 #include "mlir/Analysis/Presburger/IntegerRelation.h"
+#include "mlir/Analysis/Presburger/Matrix.h"
 #include <optional>
 
 namespace mlir {
 namespace presburger {
 
+// A polyhedron in H-representation is a set of relations
+// in d variables with integer coefficients.
 using PolyhedronH = IntegerRelation;
+
+// A polyhedron in V-representation is a set of rays, i.e.,
+// vectors, stored as rows of a matrix.
 using PolyhedronV = IntMatrix;
+
+// A cone in either representation is a special case of
+// a polyhedron in that representation.
 using ConeH = PolyhedronH;
 using ConeV = PolyhedronV;
 
-inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0)
-{
-    // We don't distinguish between domain and range variables, so
-    // we set the number of domain variables as 0 and the number of
-    // range variables as the number of actual variables.
-    // There are no symbols (non-parametric for now) and no local
-    // (existentially quantified) variables.
-    ConeH cone(PresburgerSpace::getRelationSpace(0, num_vars, num_params, 0));
-    return cone;
+inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0) {
+  // We don't distinguish between domain and range variables, so
+  // we set the number of domain variables as 0 and the number of
+  // range variables as the number of actual variables.
+  // There are no symbols (non-parametric for now) and no local
+  // (existentially quantified) variables.
+  return ConeH(PresburgerSpace::getRelationSpace(/*numDomain=*/0,
+                                                 /*numRange=*/num_vars,
+                                                 /*numSymbols=*/num_params,
+                                                 /*numLocals=*/0));
 }
 
 // Get the index of a cone.
 // If it has more rays than the dimension, return 0.
 MPInt getIndex(ConeV);
 
-// Get the dual of a cone in H-representation, returning the V-representation of it.
+// Get the dual of a cone in H-representation, returning its V-representation.
 ConeV getDual(ConeH);
 
-// Get the dual of a cone in V-representation, returning the H-representation of it.
+// Get the dual of a cone in V-representation, returning its H-representation.
 ConeH getDual(ConeV);
 
 } // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
index ddb24d1a79993f..698dde6f15d6f4 100644
--- a/mlir/lib/Analysis/Presburger/Barvinok.cpp
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -14,52 +14,50 @@ using namespace presburger;
 // Assuming that the input cone is pointed at the origin,
 // converts it to its dual in V-representation.
 // Essentially we just remove the all-zeroes constant column.
-ConeV mlir::presburger::getDual(ConeH cone)
-{
-    ConeV dual(cone.getNumInequalities(), cone.getNumCols()-1, 0, 0);
-    // Assuming that an inequality of the form
-    // a1*x1 + ... + an*xn + b ≥ 0
-    // is represented as a row [a1, ..., an, b]
-    // and that b = 0.
-
-    for (unsigned i = 0; i < cone.getNumInequalities(); i++)
-    {
-        assert(dual.at(i, cone.getNumCols()-1) == 0 && "H-representation of cone is not centred at the origin!");
-        for (unsigned j = 0; j < cone.getNumCols()-1; j++)
-        {
-            dual.at(i, j) = cone.atIneq(i, j);
-        }
+ConeV mlir::presburger::getDual(ConeH cone) {
+  unsigned inequalities = cone.getNumInequalities();
+  unsigned variables = cone.getNumCols() - 1;
+  ConeV dual(inequalities, variables, 0, 0);
+  // Assuming that an inequality of the form
+  // a1*x1 + ... + an*xn + b ≥ 0
+  // is represented as a row [a1, ..., an, b]
+  // and that b = 0.
+
+  for (unsigned i = 0; i < inequalities; i++) {
+    assert(dual.at(i, variables) == 0 &&
+           "H-representation of cone is not centred at the origin!");
+    for (unsigned j = 0; j < variables; j++) {
+      dual.at(i, j) = cone.atIneq(i, j);
     }
+  }
 
-    // Now dual is of the form [ [a1, ..., an] , ... ]
-    // which is the V-representation of the dual.
-    return dual;
+  // Now dual is of the form [ [a1, ..., an] , ... ]
+  // which is the V-representation of the dual.
+  return dual;
 }
 
 // Converts a cone in V-representation to the H-representation
 // of its dual, pointed at the origin (not at the original vertex).
 // Essentially adds a column consisting only of zeroes to the end.
-ConeH mlir::presburger::getDual(ConeV cone)
-{
-    ConeH dual = defineHRep(cone.getNumRows(), cone.getNumColumns());
-    cone.insertColumn(cone.getNumColumns());
-
-    for (unsigned i = 0; i < cone.getNumRows(); i++)
-        dual.addInequality(cone.getRow(i));
-
-    // Now dual is of the form [ [a1, ..., an, 0] , ... ]
-    // which is the H-representation of the dual.
-    return dual;
+ConeH mlir::presburger::getDual(ConeV cone) {
+  unsigned rows = cone.getNumRows();
+  unsigned columns = cone.getNumColumns();
+  ConeH dual = defineHRep(rows, columns);
+  cone.insertColumn(columns);
+
+  for (unsigned i = 0; i < rows; i++)
+    dual.addInequality(cone.getRow(i));
+
+  // Now dual is of the form [ [a1, ..., an, 0] , ... ]
+  // which is the H-representation of the dual.
+  return dual;
 }
 
 // Find the index of a cone in V-representation.
 // If there are more rays than variables, return 0.
-MPInt mlir::presburger::getIndex(ConeV cone)
-{
-    unsigned rows = cone.getNumRows();
-    unsigned cols = cone.getNumColumns();
-    if (rows > cols)
-        return MPInt(0);
+MPInt mlir::presburger::getIndex(ConeV cone) {
+  if (cone.getNumRows() > cone.getNumColumns())
+    return MPInt(0);
 
-    return cone.determinant();
+  return cone.determinant();
 }
\ No newline at end of file

>From b76e6f6da58d2bbe1bc22be248ee88452ea71d17 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 23:32:49 +0530
Subject: [PATCH 03/15] Add tests for cone fns

---
 .../mlir/Analysis/Presburger/Barvinok.h       |  7 +--
 mlir/lib/Analysis/Presburger/Barvinok.cpp     |  6 ++-
 .../Analysis/Presburger/BarvinokTest.cpp      | 47 +++++++++++++++++++
 .../Analysis/Presburger/CMakeLists.txt        |  1 +
 4 files changed, 56 insertions(+), 5 deletions(-)
 create mode 100644 mlir/unittests/Analysis/Presburger/BarvinokTest.cpp

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index 6ffdf96502fd20..665227a20fc258 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -35,15 +35,16 @@ using PolyhedronV = IntMatrix;
 using ConeH = PolyhedronH;
 using ConeV = PolyhedronV;
 
-inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0) {
+inline ConeH defineHRep(int num_vars) {
   // We don't distinguish between domain and range variables, so
   // we set the number of domain variables as 0 and the number of
   // range variables as the number of actual variables.
-  // There are no symbols (non-parametric for now) and no local
+  // There are no symbols (we don't work with parametric cones) and no local
   // (existentially quantified) variables.
+  // Once the cone is defined, we use `addInequality()` to set inequalities.
   return ConeH(PresburgerSpace::getRelationSpace(/*numDomain=*/0,
                                                  /*numRange=*/num_vars,
-                                                 /*numSymbols=*/num_params,
+                                                 /*numSymbols=*/0,
                                                  /*numLocals=*/0));
 }
 
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
index 698dde6f15d6f4..8c6fc9a6e3f53d 100644
--- a/mlir/lib/Analysis/Presburger/Barvinok.cpp
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -24,7 +24,7 @@ ConeV mlir::presburger::getDual(ConeH cone) {
   // and that b = 0.
 
   for (unsigned i = 0; i < inequalities; i++) {
-    assert(dual.at(i, variables) == 0 &&
+    assert(cone.atIneq(i, variables) == 0 &&
            "H-representation of cone is not centred at the origin!");
     for (unsigned j = 0; j < variables; j++) {
       dual.at(i, j) = cone.atIneq(i, j);
@@ -42,7 +42,9 @@ ConeV mlir::presburger::getDual(ConeH cone) {
 ConeH mlir::presburger::getDual(ConeV cone) {
   unsigned rows = cone.getNumRows();
   unsigned columns = cone.getNumColumns();
-  ConeH dual = defineHRep(rows, columns);
+  ConeH dual = defineHRep(columns);
+  // Add a new column (for constants) at the end.
+  // This will be initialized to zero.
   cone.insertColumn(columns);
 
   for (unsigned i = 0; i < rows; i++)
diff --git a/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp b/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
new file mode 100644
index 00000000000000..2b7f0491d588eb
--- /dev/null
+++ b/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
@@ -0,0 +1,47 @@
+#include "mlir/Analysis/Presburger/Barvinok.h"
+#include "./Utils.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace mlir;
+using namespace presburger;
+
+// We randomly generate 3 vectors with 4 entries each
+// and define a cone's H-representation using these
+// numbers. We check that the dual contains the same
+// numbers.
+// We do the same in the reverse case.
+TEST(BarvinokTest, getDual) {
+  ConeH cone1 = defineHRep(4);
+  cone1.addInequality({1, 2, 3, 4, 0});
+  cone1.addInequality({3, 4, 2, 5, 0});
+  cone1.addInequality({6, 2, 6, 1, 0});
+
+  ConeV dual1 = getDual(cone1);
+
+  EXPECT_EQ_INT_MATRIX(
+      dual1, makeIntMatrix(3, 4, {{1, 2, 3, 4}, {3, 4, 2, 5}, {6, 2, 6, 1}}));
+
+  ConeV cone2 = makeIntMatrix(3, 4, {{3, 6, 1, 5}, {3, 1, 7, 2}, {9, 3, 2, 7}});
+
+  ConeH dual2 = getDual(cone2);
+
+  ConeH expected = defineHRep(4);
+  expected.addInequality({3, 6, 1, 5, 0});
+  expected.addInequality({3, 1, 7, 2, 0});
+  expected.addInequality({9, 3, 2, 7, 0});
+
+  EXPECT_TRUE(dual2.isEqual(expected));
+}
+
+// We randomly generate a nxn matrix to use as a cone
+// with n inequalities in n variables and check for
+// the determinant being equal to the index.
+TEST(BarvinokTest, getIndex) {
+  ConeV cone = makeIntMatrix(3, 3, {{4, 2, 1}, {5, 2, 7}, {4, 1, 6}});
+  EXPECT_EQ(getIndex(cone), cone.determinant());
+
+  cone = makeIntMatrix(
+      4, 4, {{4, 2, 5, 1}, {4, 1, 3, 6}, {8, 2, 5, 6}, {5, 2, 5, 7}});
+  EXPECT_EQ(getIndex(cone), cone.determinant());
+}
diff --git a/mlir/unittests/Analysis/Presburger/CMakeLists.txt b/mlir/unittests/Analysis/Presburger/CMakeLists.txt
index e37133354e53ca..25f9e5939e4a25 100644
--- a/mlir/unittests/Analysis/Presburger/CMakeLists.txt
+++ b/mlir/unittests/Analysis/Presburger/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_mlir_unittest(MLIRPresburgerTests
+  BarvinokTest.cpp
   FractionTest.cpp
   IntegerPolyhedronTest.cpp
   IntegerRelationTest.cpp

>From 855bcb46c775c970fd56768759a2681bdc52a9b2 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 00:18:43 +0530
Subject: [PATCH 04/15] Use public_namespace_name::detail

---
 mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
index 770d17fe17c307..e29478e97be02c 100644
--- a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
+++ b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
@@ -19,7 +19,7 @@
 
 namespace mlir {
 namespace presburger {
-namespace internal {
+namespace public_namespace_name::detail {
 
 // A parametric point is a vector, each of whose elements
 // is an affine function of n parameters. Each row

>From df7549fdc05937216611b3af1830e90ad22d9bb7 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 10:14:19 +0530
Subject: [PATCH 05/15] Resolve conflict

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

diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 25300f84cfc047..a17565efbce95b 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -452,6 +452,8 @@ 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 5a9c4bd13f384a17bc7399f696cf62c80dc3e82d Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 22:45:31 +0530
Subject: [PATCH 06/15] initial commit

---
 .../mlir/Analysis/Presburger/Barvinok.h       | 54 +++++++++++++++
 .../Analysis/Presburger/GeneratingFunction.h  |  4 +-
 mlir/lib/Analysis/Presburger/Barvinok.cpp     | 65 +++++++++++++++++++
 mlir/lib/Analysis/Presburger/CMakeLists.txt   |  1 +
 4 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 mlir/include/mlir/Analysis/Presburger/Barvinok.h
 rename mlir/{lib => include/mlir}/Analysis/Presburger/GeneratingFunction.h (97%)
 create mode 100644 mlir/lib/Analysis/Presburger/Barvinok.cpp

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
new file mode 100644
index 00000000000000..c63ee1e230c8bf
--- /dev/null
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -0,0 +1,54 @@
+//===- Barvinok.h - Barvinok's Algorithm ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Functions relating to Barvinok's algorithm.
+// These include functions to manipulate cones (define a cone object, get its
+// dual, and find its index).
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
+#define MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
+
+#include "mlir/Analysis/Presburger/Matrix.h"
+#include "mlir/Analysis/Presburger/IntegerRelation.h"
+#include <optional>
+
+namespace mlir {
+namespace presburger {
+
+using PolyhedronH = IntegerRelation;
+using PolyhedronV = IntMatrix;
+using ConeH = PolyhedronH;
+using ConeV = PolyhedronV;
+
+inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0)
+{
+    // We don't distinguish between domain and range variables, so
+    // we set the number of domain variables as 0 and the number of
+    // range variables as the number of actual variables.
+    // There are no symbols (non-parametric for now) and no local
+    // (existentially quantified) variables.
+    ConeH cone(PresburgerSpace::getRelationSpace(0, num_vars, num_params, 0));
+    return cone;
+}
+
+// Get the index of a cone.
+// If it has more rays than the dimension, return 0.
+MPInt getIndex(ConeV);
+
+// Get the dual of a cone in H-representation, returning the V-representation of it.
+ConeV getDual(ConeH);
+
+// Get the dual of a cone in V-representation, returning the H-representation of it.
+ConeH getDual(ConeV);
+
+} // namespace presburger
+} // namespace mlir
+
+#endif // MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/GeneratingFunction.h b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
similarity index 97%
rename from mlir/lib/Analysis/Presburger/GeneratingFunction.h
rename to mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
index f7deba921ea51e..770d17fe17c307 100644
--- a/mlir/lib/Analysis/Presburger/GeneratingFunction.h
+++ b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
@@ -19,6 +19,7 @@
 
 namespace mlir {
 namespace presburger {
+namespace internal {
 
 // A parametric point is a vector, each of whose elements
 // is an affine function of n parameters. Each row
@@ -128,7 +129,8 @@ class GeneratingFunction {
   std::vector<std::vector<Point>> denominators;
 };
 
+} // namespace internal
 } // namespace presburger
 } // namespace mlir
 
-#endif // MLIR_ANALYSIS_PRESBURGER_GENERATINGFUNCTION_H
+#endif // MLIR_ANALYSIS_PRESBURGER_GENERATINGFUNCTION_H
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
new file mode 100644
index 00000000000000..ddb24d1a79993f
--- /dev/null
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -0,0 +1,65 @@
+//===- QuasiPolynomial.cpp - Barvinok's Algorithm ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Analysis/Presburger/Barvinok.h"
+
+using namespace mlir;
+using namespace presburger;
+
+// Assuming that the input cone is pointed at the origin,
+// converts it to its dual in V-representation.
+// Essentially we just remove the all-zeroes constant column.
+ConeV mlir::presburger::getDual(ConeH cone)
+{
+    ConeV dual(cone.getNumInequalities(), cone.getNumCols()-1, 0, 0);
+    // Assuming that an inequality of the form
+    // a1*x1 + ... + an*xn + b ≥ 0
+    // is represented as a row [a1, ..., an, b]
+    // and that b = 0.
+
+    for (unsigned i = 0; i < cone.getNumInequalities(); i++)
+    {
+        assert(dual.at(i, cone.getNumCols()-1) == 0 && "H-representation of cone is not centred at the origin!");
+        for (unsigned j = 0; j < cone.getNumCols()-1; j++)
+        {
+            dual.at(i, j) = cone.atIneq(i, j);
+        }
+    }
+
+    // Now dual is of the form [ [a1, ..., an] , ... ]
+    // which is the V-representation of the dual.
+    return dual;
+}
+
+// Converts a cone in V-representation to the H-representation
+// of its dual, pointed at the origin (not at the original vertex).
+// Essentially adds a column consisting only of zeroes to the end.
+ConeH mlir::presburger::getDual(ConeV cone)
+{
+    ConeH dual = defineHRep(cone.getNumRows(), cone.getNumColumns());
+    cone.insertColumn(cone.getNumColumns());
+
+    for (unsigned i = 0; i < cone.getNumRows(); i++)
+        dual.addInequality(cone.getRow(i));
+
+    // Now dual is of the form [ [a1, ..., an, 0] , ... ]
+    // which is the H-representation of the dual.
+    return dual;
+}
+
+// Find the index of a cone in V-representation.
+// If there are more rays than variables, return 0.
+MPInt mlir::presburger::getIndex(ConeV cone)
+{
+    unsigned rows = cone.getNumRows();
+    unsigned cols = cone.getNumColumns();
+    if (rows > cols)
+        return MPInt(0);
+
+    return cone.determinant();
+}
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/CMakeLists.txt b/mlir/lib/Analysis/Presburger/CMakeLists.txt
index e77e1623dae175..83d0514c9e7d17 100644
--- a/mlir/lib/Analysis/Presburger/CMakeLists.txt
+++ b/mlir/lib/Analysis/Presburger/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_mlir_library(MLIRPresburger
+  Barvinok.cpp
   IntegerRelation.cpp
   LinearTransform.cpp
   Matrix.cpp

>From d7e4ee018d8f8f8e19376dcf01406f5cef64e2c5 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 22:53:15 +0530
Subject: [PATCH 07/15] Formatting

---
 .../mlir/Analysis/Presburger/Barvinok.h       | 33 +++++----
 mlir/lib/Analysis/Presburger/Barvinok.cpp     | 70 +++++++++----------
 2 files changed, 55 insertions(+), 48 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index c63ee1e230c8bf..6ffdf96502fd20 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -15,37 +15,46 @@
 #ifndef MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
 #define MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
 
-#include "mlir/Analysis/Presburger/Matrix.h"
 #include "mlir/Analysis/Presburger/IntegerRelation.h"
+#include "mlir/Analysis/Presburger/Matrix.h"
 #include <optional>
 
 namespace mlir {
 namespace presburger {
 
+// A polyhedron in H-representation is a set of relations
+// in d variables with integer coefficients.
 using PolyhedronH = IntegerRelation;
+
+// A polyhedron in V-representation is a set of rays, i.e.,
+// vectors, stored as rows of a matrix.
 using PolyhedronV = IntMatrix;
+
+// A cone in either representation is a special case of
+// a polyhedron in that representation.
 using ConeH = PolyhedronH;
 using ConeV = PolyhedronV;
 
-inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0)
-{
-    // We don't distinguish between domain and range variables, so
-    // we set the number of domain variables as 0 and the number of
-    // range variables as the number of actual variables.
-    // There are no symbols (non-parametric for now) and no local
-    // (existentially quantified) variables.
-    ConeH cone(PresburgerSpace::getRelationSpace(0, num_vars, num_params, 0));
-    return cone;
+inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0) {
+  // We don't distinguish between domain and range variables, so
+  // we set the number of domain variables as 0 and the number of
+  // range variables as the number of actual variables.
+  // There are no symbols (non-parametric for now) and no local
+  // (existentially quantified) variables.
+  return ConeH(PresburgerSpace::getRelationSpace(/*numDomain=*/0,
+                                                 /*numRange=*/num_vars,
+                                                 /*numSymbols=*/num_params,
+                                                 /*numLocals=*/0));
 }
 
 // Get the index of a cone.
 // If it has more rays than the dimension, return 0.
 MPInt getIndex(ConeV);
 
-// Get the dual of a cone in H-representation, returning the V-representation of it.
+// Get the dual of a cone in H-representation, returning its V-representation.
 ConeV getDual(ConeH);
 
-// Get the dual of a cone in V-representation, returning the H-representation of it.
+// Get the dual of a cone in V-representation, returning its H-representation.
 ConeH getDual(ConeV);
 
 } // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
index ddb24d1a79993f..698dde6f15d6f4 100644
--- a/mlir/lib/Analysis/Presburger/Barvinok.cpp
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -14,52 +14,50 @@ using namespace presburger;
 // Assuming that the input cone is pointed at the origin,
 // converts it to its dual in V-representation.
 // Essentially we just remove the all-zeroes constant column.
-ConeV mlir::presburger::getDual(ConeH cone)
-{
-    ConeV dual(cone.getNumInequalities(), cone.getNumCols()-1, 0, 0);
-    // Assuming that an inequality of the form
-    // a1*x1 + ... + an*xn + b ≥ 0
-    // is represented as a row [a1, ..., an, b]
-    // and that b = 0.
-
-    for (unsigned i = 0; i < cone.getNumInequalities(); i++)
-    {
-        assert(dual.at(i, cone.getNumCols()-1) == 0 && "H-representation of cone is not centred at the origin!");
-        for (unsigned j = 0; j < cone.getNumCols()-1; j++)
-        {
-            dual.at(i, j) = cone.atIneq(i, j);
-        }
+ConeV mlir::presburger::getDual(ConeH cone) {
+  unsigned inequalities = cone.getNumInequalities();
+  unsigned variables = cone.getNumCols() - 1;
+  ConeV dual(inequalities, variables, 0, 0);
+  // Assuming that an inequality of the form
+  // a1*x1 + ... + an*xn + b ≥ 0
+  // is represented as a row [a1, ..., an, b]
+  // and that b = 0.
+
+  for (unsigned i = 0; i < inequalities; i++) {
+    assert(dual.at(i, variables) == 0 &&
+           "H-representation of cone is not centred at the origin!");
+    for (unsigned j = 0; j < variables; j++) {
+      dual.at(i, j) = cone.atIneq(i, j);
     }
+  }
 
-    // Now dual is of the form [ [a1, ..., an] , ... ]
-    // which is the V-representation of the dual.
-    return dual;
+  // Now dual is of the form [ [a1, ..., an] , ... ]
+  // which is the V-representation of the dual.
+  return dual;
 }
 
 // Converts a cone in V-representation to the H-representation
 // of its dual, pointed at the origin (not at the original vertex).
 // Essentially adds a column consisting only of zeroes to the end.
-ConeH mlir::presburger::getDual(ConeV cone)
-{
-    ConeH dual = defineHRep(cone.getNumRows(), cone.getNumColumns());
-    cone.insertColumn(cone.getNumColumns());
-
-    for (unsigned i = 0; i < cone.getNumRows(); i++)
-        dual.addInequality(cone.getRow(i));
-
-    // Now dual is of the form [ [a1, ..., an, 0] , ... ]
-    // which is the H-representation of the dual.
-    return dual;
+ConeH mlir::presburger::getDual(ConeV cone) {
+  unsigned rows = cone.getNumRows();
+  unsigned columns = cone.getNumColumns();
+  ConeH dual = defineHRep(rows, columns);
+  cone.insertColumn(columns);
+
+  for (unsigned i = 0; i < rows; i++)
+    dual.addInequality(cone.getRow(i));
+
+  // Now dual is of the form [ [a1, ..., an, 0] , ... ]
+  // which is the H-representation of the dual.
+  return dual;
 }
 
 // Find the index of a cone in V-representation.
 // If there are more rays than variables, return 0.
-MPInt mlir::presburger::getIndex(ConeV cone)
-{
-    unsigned rows = cone.getNumRows();
-    unsigned cols = cone.getNumColumns();
-    if (rows > cols)
-        return MPInt(0);
+MPInt mlir::presburger::getIndex(ConeV cone) {
+  if (cone.getNumRows() > cone.getNumColumns())
+    return MPInt(0);
 
-    return cone.determinant();
+  return cone.determinant();
 }
\ No newline at end of file

>From 3c6f4554019ac0c964e62351772cfc43db93b63f Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sat, 30 Dec 2023 23:32:49 +0530
Subject: [PATCH 08/15] Add tests for cone fns

---
 .../mlir/Analysis/Presburger/Barvinok.h       |  7 +--
 mlir/lib/Analysis/Presburger/Barvinok.cpp     |  6 ++-
 .../Analysis/Presburger/BarvinokTest.cpp      | 47 +++++++++++++++++++
 .../Analysis/Presburger/CMakeLists.txt        |  1 +
 4 files changed, 56 insertions(+), 5 deletions(-)
 create mode 100644 mlir/unittests/Analysis/Presburger/BarvinokTest.cpp

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index 6ffdf96502fd20..665227a20fc258 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -35,15 +35,16 @@ using PolyhedronV = IntMatrix;
 using ConeH = PolyhedronH;
 using ConeV = PolyhedronV;
 
-inline ConeH defineHRep(int num_ineqs, int num_vars, int num_params = 0) {
+inline ConeH defineHRep(int num_vars) {
   // We don't distinguish between domain and range variables, so
   // we set the number of domain variables as 0 and the number of
   // range variables as the number of actual variables.
-  // There are no symbols (non-parametric for now) and no local
+  // There are no symbols (we don't work with parametric cones) and no local
   // (existentially quantified) variables.
+  // Once the cone is defined, we use `addInequality()` to set inequalities.
   return ConeH(PresburgerSpace::getRelationSpace(/*numDomain=*/0,
                                                  /*numRange=*/num_vars,
-                                                 /*numSymbols=*/num_params,
+                                                 /*numSymbols=*/0,
                                                  /*numLocals=*/0));
 }
 
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
index 698dde6f15d6f4..8c6fc9a6e3f53d 100644
--- a/mlir/lib/Analysis/Presburger/Barvinok.cpp
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -24,7 +24,7 @@ ConeV mlir::presburger::getDual(ConeH cone) {
   // and that b = 0.
 
   for (unsigned i = 0; i < inequalities; i++) {
-    assert(dual.at(i, variables) == 0 &&
+    assert(cone.atIneq(i, variables) == 0 &&
            "H-representation of cone is not centred at the origin!");
     for (unsigned j = 0; j < variables; j++) {
       dual.at(i, j) = cone.atIneq(i, j);
@@ -42,7 +42,9 @@ ConeV mlir::presburger::getDual(ConeH cone) {
 ConeH mlir::presburger::getDual(ConeV cone) {
   unsigned rows = cone.getNumRows();
   unsigned columns = cone.getNumColumns();
-  ConeH dual = defineHRep(rows, columns);
+  ConeH dual = defineHRep(columns);
+  // Add a new column (for constants) at the end.
+  // This will be initialized to zero.
   cone.insertColumn(columns);
 
   for (unsigned i = 0; i < rows; i++)
diff --git a/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp b/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
new file mode 100644
index 00000000000000..2b7f0491d588eb
--- /dev/null
+++ b/mlir/unittests/Analysis/Presburger/BarvinokTest.cpp
@@ -0,0 +1,47 @@
+#include "mlir/Analysis/Presburger/Barvinok.h"
+#include "./Utils.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace mlir;
+using namespace presburger;
+
+// We randomly generate 3 vectors with 4 entries each
+// and define a cone's H-representation using these
+// numbers. We check that the dual contains the same
+// numbers.
+// We do the same in the reverse case.
+TEST(BarvinokTest, getDual) {
+  ConeH cone1 = defineHRep(4);
+  cone1.addInequality({1, 2, 3, 4, 0});
+  cone1.addInequality({3, 4, 2, 5, 0});
+  cone1.addInequality({6, 2, 6, 1, 0});
+
+  ConeV dual1 = getDual(cone1);
+
+  EXPECT_EQ_INT_MATRIX(
+      dual1, makeIntMatrix(3, 4, {{1, 2, 3, 4}, {3, 4, 2, 5}, {6, 2, 6, 1}}));
+
+  ConeV cone2 = makeIntMatrix(3, 4, {{3, 6, 1, 5}, {3, 1, 7, 2}, {9, 3, 2, 7}});
+
+  ConeH dual2 = getDual(cone2);
+
+  ConeH expected = defineHRep(4);
+  expected.addInequality({3, 6, 1, 5, 0});
+  expected.addInequality({3, 1, 7, 2, 0});
+  expected.addInequality({9, 3, 2, 7, 0});
+
+  EXPECT_TRUE(dual2.isEqual(expected));
+}
+
+// We randomly generate a nxn matrix to use as a cone
+// with n inequalities in n variables and check for
+// the determinant being equal to the index.
+TEST(BarvinokTest, getIndex) {
+  ConeV cone = makeIntMatrix(3, 3, {{4, 2, 1}, {5, 2, 7}, {4, 1, 6}});
+  EXPECT_EQ(getIndex(cone), cone.determinant());
+
+  cone = makeIntMatrix(
+      4, 4, {{4, 2, 5, 1}, {4, 1, 3, 6}, {8, 2, 5, 6}, {5, 2, 5, 7}});
+  EXPECT_EQ(getIndex(cone), cone.determinant());
+}
diff --git a/mlir/unittests/Analysis/Presburger/CMakeLists.txt b/mlir/unittests/Analysis/Presburger/CMakeLists.txt
index e37133354e53ca..25f9e5939e4a25 100644
--- a/mlir/unittests/Analysis/Presburger/CMakeLists.txt
+++ b/mlir/unittests/Analysis/Presburger/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_mlir_unittest(MLIRPresburgerTests
+  BarvinokTest.cpp
   FractionTest.cpp
   IntegerPolyhedronTest.cpp
   IntegerRelationTest.cpp

>From 08d6cd624edf9ddb92550fc6d65bc41f5d011fcf Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 00:18:43 +0530
Subject: [PATCH 09/15] Use public_namespace_name::detail

---
 mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
index 770d17fe17c307..e29478e97be02c 100644
--- a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
+++ b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
@@ -19,7 +19,7 @@
 
 namespace mlir {
 namespace presburger {
-namespace internal {
+namespace public_namespace_name::detail {
 
 // A parametric point is a vector, each of whose elements
 // is an affine function of n parameters. Each row

>From 6592268fdb1d8e31bb795b37b3d63707d312c08d Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 10:25:36 +0530
Subject: [PATCH 10/15] Formatting

---
 mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
index e29478e97be02c..abf9cce8c9d4d4 100644
--- a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
+++ b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
@@ -129,7 +129,7 @@ class GeneratingFunction {
   std::vector<std::vector<Point>> denominators;
 };
 
-} // namespace internal
+} // namespace public_namespace_name::detail
 } // namespace presburger
 } // namespace mlir
 

>From 7007cf83615ed479a0d7daab81a2b1eeb7514bbc Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 21:17:54 +0530
Subject: [PATCH 11/15] Documentation

---
 mlir/include/mlir/Analysis/Presburger/Barvinok.h | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index 665227a20fc258..d39d0e704bf4e4 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -22,11 +22,11 @@
 namespace mlir {
 namespace presburger {
 
-// A polyhedron in H-representation is a set of relations
+// A polyhedron in H-representation is a set of inequalities
 // in d variables with integer coefficients.
 using PolyhedronH = IntegerRelation;
 
-// A polyhedron in V-representation is a set of rays, i.e.,
+// A polyhedron in V-representation is a set of rays and points, i.e.,
 // vectors, stored as rows of a matrix.
 using PolyhedronV = IntMatrix;
 
@@ -48,14 +48,21 @@ inline ConeH defineHRep(int num_vars) {
                                                  /*numLocals=*/0));
 }
 
-// Get the index of a cone.
+// Get the index of a cone, i.e., the volume of the parallelepiped
+// spanned by its generators, which is equal to the number of integer
+// points in its fundamental parallelepiped.
+// If the index is 1, the cone is unimodular.
+// Barvinok, A., and J. E. Pommersheim. "An algorithmic theory of lattice points in polyhedra."
+// p. 107
 // If it has more rays than the dimension, return 0.
 MPInt getIndex(ConeV);
 
 // Get the dual of a cone in H-representation, returning its V-representation.
+// This assumes that the input is pointed at the origin; it fails otherwise.
 ConeV getDual(ConeH);
 
 // Get the dual of a cone in V-representation, returning its H-representation.
+// The returned cone is pointed at the origin.
 ConeH getDual(ConeV);
 
 } // namespace presburger

>From 9419d5b426261476d6bfda938b9457a030fe6f39 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 21:20:33 +0530
Subject: [PATCH 12/15] Use getSetSpace

---
 mlir/include/mlir/Analysis/Presburger/Barvinok.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index d39d0e704bf4e4..2acf9f68cb3c9e 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -42,10 +42,9 @@ inline ConeH defineHRep(int num_vars) {
   // There are no symbols (we don't work with parametric cones) and no local
   // (existentially quantified) variables.
   // Once the cone is defined, we use `addInequality()` to set inequalities.
-  return ConeH(PresburgerSpace::getRelationSpace(/*numDomain=*/0,
-                                                 /*numRange=*/num_vars,
-                                                 /*numSymbols=*/0,
-                                                 /*numLocals=*/0));
+  return ConeH(PresburgerSpace::getSetSpace(/*numDims=*/num_vars,
+                                            /*numSymbols=*/0,
+                                            /*numLocals=*/0));
 }
 
 // Get the index of a cone, i.e., the volume of the parallelepiped

>From edf909e8945f629abf0c04865d669d130dd390da Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 21:21:28 +0530
Subject: [PATCH 13/15] Clang-format

---
 mlir/include/mlir/Analysis/Presburger/Barvinok.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index 2acf9f68cb3c9e..c8d61641a47b5a 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -51,9 +51,8 @@ inline ConeH defineHRep(int num_vars) {
 // spanned by its generators, which is equal to the number of integer
 // points in its fundamental parallelepiped.
 // If the index is 1, the cone is unimodular.
-// Barvinok, A., and J. E. Pommersheim. "An algorithmic theory of lattice points in polyhedra."
-// p. 107
-// If it has more rays than the dimension, return 0.
+// Barvinok, A., and J. E. Pommersheim. "An algorithmic theory of lattice points
+// in polyhedra." p. 107 If it has more rays than the dimension, return 0.
 MPInt getIndex(ConeV);
 
 // Get the dual of a cone in H-representation, returning its V-representation.

>From e5bcf072b98e5bb13ceae52ce44a7e80e87a9141 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Sun, 31 Dec 2023 21:47:21 +0530
Subject: [PATCH 14/15] Add newlines

---
 mlir/include/mlir/Analysis/Presburger/Barvinok.h           | 2 +-
 mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h | 2 +-
 mlir/lib/Analysis/Presburger/Barvinok.cpp                  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index c8d61641a47b5a..c168ddf12be1e8 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -66,4 +66,4 @@ ConeH getDual(ConeV);
 } // namespace presburger
 } // namespace mlir
 
-#endif // MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
\ No newline at end of file
+#endif // MLIR_ANALYSIS_PRESBURGER_BARVINOK_H
diff --git a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
index abf9cce8c9d4d4..fcc5c8c1dea15c 100644
--- a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
+++ b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
@@ -133,4 +133,4 @@ class GeneratingFunction {
 } // namespace presburger
 } // namespace mlir
 
-#endif // MLIR_ANALYSIS_PRESBURGER_GENERATINGFUNCTION_H
\ No newline at end of file
+#endif // MLIR_ANALYSIS_PRESBURGER_GENERATINGFUNCTION_H
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
index 8c6fc9a6e3f53d..348797a8a3f137 100644
--- a/mlir/lib/Analysis/Presburger/Barvinok.cpp
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -62,4 +62,4 @@ MPInt mlir::presburger::getIndex(ConeV cone) {
     return MPInt(0);
 
   return cone.determinant();
-}
\ No newline at end of file
+}

>From 3cf91b99ce361eddfd2ed470f7f9d7e1ead3c959 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 4 Jan 2024 21:38:37 +0530
Subject: [PATCH 15/15] Documentation and var namesg

---
 mlir/include/mlir/Analysis/Presburger/Barvinok.h     |  6 ++++--
 .../mlir/Analysis/Presburger/GeneratingFunction.h    |  2 --
 mlir/lib/Analysis/Presburger/Barvinok.cpp            | 12 ++++++------
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Barvinok.h b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
index c168ddf12be1e8..cc98f1272bdd45 100644
--- a/mlir/include/mlir/Analysis/Presburger/Barvinok.h
+++ b/mlir/include/mlir/Analysis/Presburger/Barvinok.h
@@ -52,11 +52,13 @@ inline ConeH defineHRep(int num_vars) {
 // points in its fundamental parallelepiped.
 // If the index is 1, the cone is unimodular.
 // Barvinok, A., and J. E. Pommersheim. "An algorithmic theory of lattice points
-// in polyhedra." p. 107 If it has more rays than the dimension, return 0.
+// in polyhedra." p. 107
+// If it has more rays than the dimension, return 0.
 MPInt getIndex(ConeV);
 
 // Get the dual of a cone in H-representation, returning its V-representation.
-// This assumes that the input is pointed at the origin; it fails otherwise.
+// This assumes that the input is pointed at the origin; it assert-fails
+// otherwise.
 ConeV getDual(ConeH);
 
 // Get the dual of a cone in V-representation, returning its H-representation.
diff --git a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
index fcc5c8c1dea15c..f7deba921ea51e 100644
--- a/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
+++ b/mlir/include/mlir/Analysis/Presburger/GeneratingFunction.h
@@ -19,7 +19,6 @@
 
 namespace mlir {
 namespace presburger {
-namespace public_namespace_name::detail {
 
 // A parametric point is a vector, each of whose elements
 // is an affine function of n parameters. Each row
@@ -129,7 +128,6 @@ class GeneratingFunction {
   std::vector<std::vector<Point>> denominators;
 };
 
-} // namespace public_namespace_name::detail
 } // namespace presburger
 } // namespace mlir
 
diff --git a/mlir/lib/Analysis/Presburger/Barvinok.cpp b/mlir/lib/Analysis/Presburger/Barvinok.cpp
index 348797a8a3f137..f3c70aa0b5a1f9 100644
--- a/mlir/lib/Analysis/Presburger/Barvinok.cpp
+++ b/mlir/lib/Analysis/Presburger/Barvinok.cpp
@@ -15,18 +15,18 @@ using namespace presburger;
 // converts it to its dual in V-representation.
 // Essentially we just remove the all-zeroes constant column.
 ConeV mlir::presburger::getDual(ConeH cone) {
-  unsigned inequalities = cone.getNumInequalities();
-  unsigned variables = cone.getNumCols() - 1;
-  ConeV dual(inequalities, variables, 0, 0);
+  unsigned numIneq = cone.getNumInequalities();
+  unsigned numVar = cone.getNumCols() - 1;
+  ConeV dual(numIneq, numVar, 0, 0);
   // Assuming that an inequality of the form
   // a1*x1 + ... + an*xn + b ≥ 0
   // is represented as a row [a1, ..., an, b]
   // and that b = 0.
 
-  for (unsigned i = 0; i < inequalities; i++) {
-    assert(cone.atIneq(i, variables) == 0 &&
+  for (unsigned i = 0; i < numIneq; i++) {
+    assert(cone.atIneq(i, numVar) == 0 &&
            "H-representation of cone is not centred at the origin!");
-    for (unsigned j = 0; j < variables; j++) {
+    for (unsigned j = 0; j < numVar; j++) {
       dual.at(i, j) = cone.atIneq(i, j);
     }
   }



More information about the Mlir-commits mailing list