[Mlir-commits] [mlir] [MLIR][Presburger][WIP] Implement vertex enumeration and chamber decomposition for polytope generating function computation. (PR #78987)

Arjun P llvmlistbot at llvm.org
Mon Jan 22 08:11:27 PST 2024


================
@@ -147,6 +148,319 @@ GeneratingFunction mlir::presburger::detail::unimodularConeGeneratingFunction(
                             std::vector({denominator}));
 }
 
+/// We use Gaussian elimination to find the solution to a set of d equations
+/// of the form
+/// a_1 x_1 + ... + a_d x_d + b_1 m_1 + ... + b_p m_p + c = 0
+/// where x_i are variables,
+/// m_i are parameters and
+/// a_i, b_i, c are rational coefficients.
+/// The solution expresses each x_i as an affine function of the m_i, and is
+/// therefore represented as a matrix of size d x (p+1).
+/// If there is no solution, we return null.
+std::optional<ParamPoint>
+mlir::presburger::detail::findVertex(IntMatrix equations) {
+  // equations is a d x (d + p + 1) matrix.
+  // Each row represents an equation.
+
+  unsigned numEqs = equations.getNumRows();
+  unsigned numCols = equations.getNumColumns();
+
+  // First, we check that the system has a solution, and return
+  // null if not.
+  IntMatrix coeffs(numEqs, numEqs);
+  for (unsigned i = 0; i < numEqs; i++)
+    for (unsigned j = 0; j < numEqs; j++)
+      coeffs(i, j) = equations(i, j);
+
+  if (coeffs.determinant() == 0)
+    return std::nullopt;
+
+  // We work with rational numbers.
+  FracMatrix equationsF(equations);
+
+  for (unsigned i = 0; i < numEqs; ++i) {
+    // First ensure that the diagonal element is nonzero, by swapping
+    // it with a nonzero row.
+    if (equationsF(i, i) == 0) {
+      for (unsigned j = i + 1; j < numEqs; ++j) {
+        if (equationsF(j, i) != 0) {
+          equationsF.swapRows(j, i);
+          break;
+        }
+      }
+    }
+
+    Fraction b = equationsF(i, i);
+
+    // Set all elements except the diagonal to zero.
+    for (unsigned j = 0; j < numEqs; ++j) {
+      if (equationsF(j, i) == 0 || j == i)
+        continue;
+      // Set element (j, i) to zero
+      // by subtracting the ith row,
+      // appropriately scaled.
----------------
Superty wrote:

please generally use the available horizontal space

https://github.com/llvm/llvm-project/pull/78987


More information about the Mlir-commits mailing list