[Mlir-commits] [mlir] [MLIR][Presburger] Preserve identifiers in IntegerRelation::convertVa… (PR #67909)
Bharathi Ramana Joshi
llvmlistbot at llvm.org
Mon Oct 30 01:45:41 PDT 2023
https://github.com/iambrj updated https://github.com/llvm/llvm-project/pull/67909
>From 28eb165691e123728a485d06a37beffd82166825 Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Fri, 6 Oct 2023 02:09:58 +0400
Subject: [PATCH 1/8] [MLIR][Presburger] Implement Matrix::moveColumns
---
.../include/mlir/Analysis/Presburger/Matrix.h | 17 ++++++
mlir/lib/Analysis/Presburger/Matrix.cpp | 26 +++++++-
.../Analysis/Presburger/MatrixTest.cpp | 59 ++++++++++++++++++-
3 files changed, 98 insertions(+), 4 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index bed3a5f75e396c5..0325d5ebd017bbe 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -189,6 +189,23 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
/// invariants satisfied.
bool hasConsistentState() const;
+ /// Shift the columns in the range [srcPos, srcPos + num] by the specified
+ /// offset, i.e. to [srcPos + offset, srcPos + num + offset], while moving
+ /// the columns adjacent to the range to the left/right of the shifted
+ /// columns.
+ ///
+ /// For a positive offset (i.e. shifting the columns right), columns that
+ /// were at positions [0, srcPos) will stay where they are; columns that were
+ /// at positions [srcPos + num, srcPos + num + offset) will be moved to
+ /// [srcPos, srcPos + offset); and columns that were at positions
+ /// (src + num + offset, nCols) will remain where they were. For example,
+ /// if m = |0 1 2 3 4 5| then m.moveColumns(1, 2, 3) will result in
+ /// m = |0 3 4 5 1 2|.
+ ///
+ /// Similarly, a negative offset results in a left shift of the columns in
+ /// the range [srcPos, srcPos + num].
+ void moveColumns(unsigned srcPos, unsigned num, int offset);
+
private:
/// The current number of rows, columns, and reserved columns. The underlying
/// data vector is viewed as an nRows x nReservedColumns matrix, of which the
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index f0bcb09fb28f7b1..4c8d63055fc5eb3 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -192,6 +192,30 @@ template <typename T> void Matrix<T>::fillRow(unsigned row, const T &value) {
at(row, col) = value;
}
+template <typename T> void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, int offset) {
+ assert(num != 0 && "num must be non-zero");
+ assert(offset != 0 && "offset must be non-zero");
+ assert(0 <= srcPos + offset && srcPos + num + offset <= getNumColumns() &&
+ "invalid move offset");
+
+ unsigned insertCount = offset > 0 ? offset : -offset,
+ insertPos = offset > 0 ? srcPos : srcPos + num,
+ deletePos = offset > 0 ? srcPos + num : srcPos + offset;
+ // Insert new zero columns in the positions where the adjacent columns are to
+ // be moved.
+ insertColumns(insertPos, insertCount);
+ // Update deletePos if insertion of new columns invalidates it.
+ if(insertPos < deletePos)
+ deletePos += insertCount;
+
+ // Swap the adjacent columns with inserted zero columns.
+ for(unsigned i = 0; i < insertCount; i++)
+ swapColumns(insertPos + i, deletePos + i);
+
+ // Delete the now redundant zero columns.
+ removeColumns(deletePos, insertCount);
+}
+
template <typename T> void Matrix<T>::addToRow(unsigned sourceRow, unsigned targetRow,
const T &scale) {
addToRow(targetRow, getRow(sourceRow), scale);
@@ -390,4 +414,4 @@ MPInt IntMatrix::normalizeRow(unsigned row, unsigned cols) {
MPInt IntMatrix::normalizeRow(unsigned row) {
return normalizeRow(row, getNumColumns());
-}
\ No newline at end of file
+}
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 6b23cedabf624ec..57e0fe627bec5af 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -194,13 +194,21 @@ TEST(MatrixTest, resize) {
EXPECT_EQ(mat(row, col), row >= 3 || col >= 3 ? 0 : int(10 * row + col));
}
+template <typename T>
+static void checkMatEqual(const Matrix<T> m1, const Matrix<T> m2) {
+ EXPECT_EQ(m1.getNumRows(), m2.getNumRows());
+ EXPECT_EQ(m1.getNumColumns(), m2.getNumColumns());
+
+ for (unsigned row = 0; row < m1.getNumRows(); row++)
+ for (unsigned col = 0; col < m2.getNumColumns(); col++)
+ EXPECT_EQ(m1(row, col), m2(row, col));
+}
+
static void checkHermiteNormalForm(const IntMatrix &mat,
const IntMatrix &hermiteForm) {
auto [h, u] = mat.computeHermiteNormalForm();
- for (unsigned row = 0; row < mat.getNumRows(); row++)
- for (unsigned col = 0; col < mat.getNumColumns(); col++)
- EXPECT_EQ(h(row, col), hermiteForm(row, col));
+ checkMatEqual(h, hermiteForm);
}
TEST(MatrixTest, computeHermiteNormalForm) {
@@ -248,3 +256,48 @@ TEST(MatrixTest, computeHermiteNormalForm) {
checkHermiteNormalForm(mat, hermiteForm);
}
}
+
+TEST(MatrixTest, moveColumns) {
+ IntMatrix mat = makeIntMatrix(3, 4,
+ {{0, 1, 2, 3},
+ {4, 5, 6, 7},
+ {8, 9, 4, 2}});
+
+ {
+ IntMatrix movedMat = makeIntMatrix(3, 4,
+ {{0, 3, 1, 2},
+ {4, 7, 5, 6},
+ {8, 2, 9, 4}});
+
+ movedMat.moveColumns(2, 2, -1);
+ checkMatEqual(mat, movedMat);
+ }
+
+ {
+ IntMatrix movedMat = makeIntMatrix(3, 4,
+ {{0, 3, 1, 2},
+ {4, 7, 5, 6},
+ {8, 2, 9, 4}});
+
+ movedMat.moveColumns(1, 1, 2);
+ checkMatEqual(mat, movedMat);
+ }
+
+ {
+ IntMatrix movedMat = makeIntMatrix(3, 4,
+ {{1, 2, 0, 3},
+ {5, 6, 4, 7},
+ {9, 4, 8, 2}});
+
+ movedMat.moveColumns(0, 2, 1);
+ checkMatEqual(mat, movedMat);
+ }
+
+ {
+ IntMatrix movedMat =
+ makeIntMatrix(3, 4, {{1, 0, 2, 3}, {5, 4, 6, 7}, {9, 8, 4, 2}});
+
+ movedMat.moveColumns(0, 1, 1);
+ checkMatEqual(mat, movedMat);
+ }
+}
>From 6f153fc67e8d445f119ccf3a9f771cbd38076973 Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Tue, 10 Oct 2023 21:05:07 +0400
Subject: [PATCH 2/8] [MLIR][Presburger] Rewrite Matrix::moveColumns to use
dstPos
---
.../include/mlir/Analysis/Presburger/Matrix.h | 25 ++++++++++---------
mlir/lib/Analysis/Presburger/Matrix.cpp | 13 +++++++---
.../Analysis/Presburger/MatrixTest.cpp | 10 +++++---
3 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 0325d5ebd017bbe..9b69ad1eefce5b6 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -189,22 +189,23 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
/// invariants satisfied.
bool hasConsistentState() const;
- /// Shift the columns in the range [srcPos, srcPos + num] by the specified
- /// offset, i.e. to [srcPos + offset, srcPos + num + offset], while moving
- /// the columns adjacent to the range to the left/right of the shifted
+ /// Shift the columns in the source range [srcPos, srcPos + num) to the
+ /// specified destination, i.e. to [dstPos, dstPos + num), while moving the
+ /// columns adjacent to the source range to the left/right of the shifted
/// columns.
///
- /// For a positive offset (i.e. shifting the columns right), columns that
- /// were at positions [0, srcPos) will stay where they are; columns that were
- /// at positions [srcPos + num, srcPos + num + offset) will be moved to
- /// [srcPos, srcPos + offset); and columns that were at positions
- /// (src + num + offset, nCols) will remain where they were. For example,
- /// if m = |0 1 2 3 4 5| then m.moveColumns(1, 2, 3) will result in
- /// m = |0 3 4 5 1 2|.
+ /// When shifting the source columns right (i.e. dstPos > srcPos), columns
+ /// that were at positions [0, srcPos) will stay where they are; columns that
+ /// were at positions [srcPos, srcPos + num) will be moved to
+ /// [dstPos, dstPos + num); columns that were at positions
+ /// [srcPos + num, dstPos + num) will be moved to [srcPos, srcPos + num);
+ /// and columns that were at positions [dstPos + num, nCols) will remain
+ /// where they were. For example, if m = |0 1 2 3 4 5| then
+ /// m.moveColumns(1, 2, 3) will result in m = |0 3 4 5 1 2|.
///
/// Similarly, a negative offset results in a left shift of the columns in
- /// the range [srcPos, srcPos + num].
- void moveColumns(unsigned srcPos, unsigned num, int offset);
+ /// the range [srcPos, srcPos + num).
+ void moveColumns(unsigned srcPos, unsigned num, unsigned dstPos);
private:
/// The current number of rows, columns, and reserved columns. The underlying
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 4c8d63055fc5eb3..d3184c769cc6e8b 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -192,11 +192,16 @@ template <typename T> void Matrix<T>::fillRow(unsigned row, const T &value) {
at(row, col) = value;
}
-template <typename T> void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, int offset) {
- assert(num != 0 && "num must be non-zero");
- assert(offset != 0 && "offset must be non-zero");
+template <typename T> void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
+ if(num == 0)
+ return;
+
+ int offset = dstPos - srcPos;
+ if(offset == 0)
+ return;
+
assert(0 <= srcPos + offset && srcPos + num + offset <= getNumColumns() &&
- "invalid move offset");
+ "invalid move num");
unsigned insertCount = offset > 0 ? offset : -offset,
insertPos = offset > 0 ? srcPos : srcPos + num,
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 57e0fe627bec5af..4045750fa99721d 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -269,7 +269,7 @@ TEST(MatrixTest, moveColumns) {
{4, 7, 5, 6},
{8, 2, 9, 4}});
- movedMat.moveColumns(2, 2, -1);
+ movedMat.moveColumns(2, 2, 1);
checkMatEqual(mat, movedMat);
}
@@ -279,7 +279,7 @@ TEST(MatrixTest, moveColumns) {
{4, 7, 5, 6},
{8, 2, 9, 4}});
- movedMat.moveColumns(1, 1, 2);
+ movedMat.moveColumns(1, 1, 3);
checkMatEqual(mat, movedMat);
}
@@ -294,8 +294,10 @@ TEST(MatrixTest, moveColumns) {
}
{
- IntMatrix movedMat =
- makeIntMatrix(3, 4, {{1, 0, 2, 3}, {5, 4, 6, 7}, {9, 8, 4, 2}});
+ IntMatrix movedMat = makeIntMatrix(3, 4,
+ {{1, 0, 2, 3},
+ {5, 4, 6, 7},
+ {9, 8, 4, 2}});
movedMat.moveColumns(0, 1, 1);
checkMatEqual(mat, movedMat);
>From d3fe8609bee8a039990b69e5cd2cab4bdbfca01f Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Tue, 10 Oct 2023 22:40:03 +0400
Subject: [PATCH 3/8] [MLIR][Presburger] Add TODO
---
mlir/lib/Analysis/Presburger/Matrix.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index d3184c769cc6e8b..03a919c491c87ec 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -206,6 +206,7 @@ template <typename T> void Matrix<T>::moveColumns(unsigned srcPos, unsigned num,
unsigned insertCount = offset > 0 ? offset : -offset,
insertPos = offset > 0 ? srcPos : srcPos + num,
deletePos = offset > 0 ? srcPos + num : srcPos + offset;
+ // TODO: This can be done using std::rotate.
// Insert new zero columns in the positions where the adjacent columns are to
// be moved.
insertColumns(insertPos, insertCount);
>From ee127c9489a3b325eff537a2fe8ed2067cfc6928 Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Wed, 11 Oct 2023 14:52:37 +0400
Subject: [PATCH 4/8] [MLIR][Presburger] Remove implementation detail from doc
comment
---
mlir/include/mlir/Analysis/Presburger/Matrix.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 9b69ad1eefce5b6..dcf8d36dc1bd736 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -203,8 +203,7 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
/// where they were. For example, if m = |0 1 2 3 4 5| then
/// m.moveColumns(1, 2, 3) will result in m = |0 3 4 5 1 2|.
///
- /// Similarly, a negative offset results in a left shift of the columns in
- /// the range [srcPos, srcPos + num).
+ /// The left shift operation (i.e. dstPos < srcPos) works in a similar way.
void moveColumns(unsigned srcPos, unsigned num, unsigned dstPos);
private:
>From 1f7d8dc0e5e7d7eb527edff42555c2f781a11f30 Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Thu, 12 Oct 2023 23:23:30 +0400
Subject: [PATCH 5/8] [MLIR][Presburger] Address minor nits
---
mlir/lib/Analysis/Presburger/Matrix.cpp | 8 ++++----
mlir/unittests/Analysis/Presburger/MatrixTest.cpp | 4 ++--
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 03a919c491c87ec..5b8b0b14b0aa15e 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -203,9 +203,9 @@ template <typename T> void Matrix<T>::moveColumns(unsigned srcPos, unsigned num,
assert(0 <= srcPos + offset && srcPos + num + offset <= getNumColumns() &&
"invalid move num");
- unsigned insertCount = offset > 0 ? offset : -offset,
- insertPos = offset > 0 ? srcPos : srcPos + num,
- deletePos = offset > 0 ? srcPos + num : srcPos + offset;
+ unsigned insertCount = offset > 0 ? offset : -offset;
+ unsigned insertPos = offset > 0 ? srcPos : srcPos + num;
+ unsigned deletePos = offset > 0 ? srcPos + num : srcPos + offset;
// TODO: This can be done using std::rotate.
// Insert new zero columns in the positions where the adjacent columns are to
// be moved.
@@ -215,7 +215,7 @@ template <typename T> void Matrix<T>::moveColumns(unsigned srcPos, unsigned num,
deletePos += insertCount;
// Swap the adjacent columns with inserted zero columns.
- for(unsigned i = 0; i < insertCount; i++)
+ for(unsigned i = 0; i < insertCount; ++i)
swapColumns(insertPos + i, deletePos + i);
// Delete the now redundant zero columns.
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 4045750fa99721d..1df0fa45b5ac335 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -199,8 +199,8 @@ static void checkMatEqual(const Matrix<T> m1, const Matrix<T> m2) {
EXPECT_EQ(m1.getNumRows(), m2.getNumRows());
EXPECT_EQ(m1.getNumColumns(), m2.getNumColumns());
- for (unsigned row = 0; row < m1.getNumRows(); row++)
- for (unsigned col = 0; col < m2.getNumColumns(); col++)
+ for (unsigned row = 0, rows = m1.getNumRows(); row < rows; ++row)
+ for (unsigned col = 0, cols = m1.getNumColumns(); col < cols; ++col)
EXPECT_EQ(m1(row, col), m2(row, col));
}
>From 2930ec869b95872f910cd62e648895b0b1626e16 Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Thu, 12 Oct 2023 23:34:24 +0400
Subject: [PATCH 6/8] [MLIR][Presburger] Fix code formatting
---
.../include/mlir/Analysis/Presburger/Matrix.h | 29 ++++++-------
mlir/lib/Analysis/Presburger/Matrix.cpp | 2 +-
.../Analysis/Presburger/MatrixTest.cpp | 43 ++++++++-----------
3 files changed, 31 insertions(+), 43 deletions(-)
diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index dcf8d36dc1bd736..ed69ece127291fd 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -15,8 +15,8 @@
#ifndef MLIR_ANALYSIS_PRESBURGER_MATRIX_H
#define MLIR_ANALYSIS_PRESBURGER_MATRIX_H
-#include "mlir/Support/LLVM.h"
#include "mlir/Analysis/Presburger/Fraction.h"
+#include "mlir/Support/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/raw_ostream.h"
@@ -36,9 +36,10 @@ namespace presburger {
/// This class only works for the types MPInt and Fraction, since the method
/// implementations are in the Matrix.cpp file. Only these two types have
/// been explicitly instantiated there.
-template<typename T>
-class Matrix {
-static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be MPInt or Fraction.");
+template <typename T> class Matrix {
+ static_assert(std::is_same_v<T, MPInt> || std::is_same_v<T, Fraction>,
+ "T must be MPInt or Fraction.");
+
public:
Matrix() = delete;
@@ -69,9 +70,7 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
T &operator()(unsigned row, unsigned column) { return at(row, column); }
- T operator()(unsigned row, unsigned column) const {
- return at(row, column);
- }
+ T operator()(unsigned row, unsigned column) const { return at(row, column); }
/// Swap the given columns.
void swapColumns(unsigned column, unsigned otherColumn);
@@ -221,21 +220,20 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
// 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 IntMatrix : public Matrix<MPInt>
-{
+class IntMatrix : public Matrix<MPInt> {
public:
IntMatrix(unsigned rows, unsigned columns, unsigned reservedRows = 0,
- unsigned reservedColumns = 0) :
- Matrix<MPInt>(rows, columns, reservedRows, reservedColumns) {};
+ unsigned reservedColumns = 0)
+ : Matrix<MPInt>(rows, columns, reservedRows, reservedColumns){};
- IntMatrix(Matrix<MPInt> m) :
- Matrix<MPInt>(m.getNumRows(), m.getNumColumns(), m.getNumReservedRows(), m.getNumReservedColumns())
- {
+ IntMatrix(Matrix<MPInt> m)
+ : Matrix<MPInt>(m.getNumRows(), m.getNumColumns(), m.getNumReservedRows(),
+ m.getNumReservedColumns()) {
for (unsigned i = 0; i < m.getNumRows(); i++)
for (unsigned j = 0; j < m.getNumColumns(); j++)
at(i, j) = m(i, j);
};
-
+
/// Return the identity matrix of the specified dimension.
static IntMatrix identity(unsigned dimension);
@@ -257,7 +255,6 @@ class IntMatrix : public Matrix<MPInt>
/// Divide the columns of the specified row by their GCD.
/// Returns the GCD of the columns of the specified row.
MPInt normalizeRow(unsigned row);
-
};
} // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 5b8b0b14b0aa15e..3aafe5adb5ac1af 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -215,7 +215,7 @@ template <typename T> void Matrix<T>::moveColumns(unsigned srcPos, unsigned num,
deletePos += insertCount;
// Swap the adjacent columns with inserted zero columns.
- for(unsigned i = 0; i < insertCount; ++i)
+ for (unsigned i = 0; i < insertCount; ++i)
swapColumns(insertPos + i, deletePos + i);
// Delete the now redundant zero columns.
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 1df0fa45b5ac335..bb67ebdde125f91 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -7,8 +7,8 @@
//===----------------------------------------------------------------------===//
#include "mlir/Analysis/Presburger/Matrix.h"
-#include "mlir/Analysis/Presburger/Fraction.h"
#include "./Utils.h"
+#include "mlir/Analysis/Presburger/Fraction.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -218,7 +218,8 @@ TEST(MatrixTest, computeHermiteNormalForm) {
{
// Hermite form of a unimodular matrix is the identity matrix.
IntMatrix mat = makeIntMatrix(3, 3, {{2, 3, 6}, {3, 2, 3}, {17, 11, 16}});
- IntMatrix hermiteForm = makeIntMatrix(3, 3, {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}});
+ IntMatrix hermiteForm =
+ makeIntMatrix(3, 3, {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}});
checkHermiteNormalForm(mat, hermiteForm);
}
@@ -249,55 +250,45 @@ TEST(MatrixTest, computeHermiteNormalForm) {
}
{
- IntMatrix mat =
- makeIntMatrix(3, 5, {{0, 2, 0, 7, 1}, {-1, 0, 0, -3, 0}, {0, 4, 1, 0, 8}});
- IntMatrix hermiteForm =
- makeIntMatrix(3, 5, {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}});
+ IntMatrix mat = makeIntMatrix(
+ 3, 5, {{0, 2, 0, 7, 1}, {-1, 0, 0, -3, 0}, {0, 4, 1, 0, 8}});
+ IntMatrix hermiteForm = makeIntMatrix(
+ 3, 5, {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}});
checkHermiteNormalForm(mat, hermiteForm);
}
}
TEST(MatrixTest, moveColumns) {
- IntMatrix mat = makeIntMatrix(3, 4,
- {{0, 1, 2, 3},
- {4, 5, 6, 7},
- {8, 9, 4, 2}});
+ IntMatrix mat =
+ makeIntMatrix(3, 4, {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 4, 2}});
{
- IntMatrix movedMat = makeIntMatrix(3, 4,
- {{0, 3, 1, 2},
- {4, 7, 5, 6},
- {8, 2, 9, 4}});
+ IntMatrix movedMat =
+ makeIntMatrix(3, 4, {{0, 3, 1, 2}, {4, 7, 5, 6}, {8, 2, 9, 4}});
movedMat.moveColumns(2, 2, 1);
checkMatEqual(mat, movedMat);
}
{
- IntMatrix movedMat = makeIntMatrix(3, 4,
- {{0, 3, 1, 2},
- {4, 7, 5, 6},
- {8, 2, 9, 4}});
+ IntMatrix movedMat =
+ makeIntMatrix(3, 4, {{0, 3, 1, 2}, {4, 7, 5, 6}, {8, 2, 9, 4}});
movedMat.moveColumns(1, 1, 3);
checkMatEqual(mat, movedMat);
}
{
- IntMatrix movedMat = makeIntMatrix(3, 4,
- {{1, 2, 0, 3},
- {5, 6, 4, 7},
- {9, 4, 8, 2}});
+ IntMatrix movedMat =
+ makeIntMatrix(3, 4, {{1, 2, 0, 3}, {5, 6, 4, 7}, {9, 4, 8, 2}});
movedMat.moveColumns(0, 2, 1);
checkMatEqual(mat, movedMat);
}
{
- IntMatrix movedMat = makeIntMatrix(3, 4,
- {{1, 0, 2, 3},
- {5, 4, 6, 7},
- {9, 8, 4, 2}});
+ IntMatrix movedMat =
+ makeIntMatrix(3, 4, {{1, 0, 2, 3}, {5, 4, 6, 7}, {9, 8, 4, 2}});
movedMat.moveColumns(0, 1, 1);
checkMatEqual(mat, movedMat);
>From 916786b01f6afc373dedfe50ca0aa30817ac7336 Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Sun, 1 Oct 2023 11:25:59 +0300
Subject: [PATCH 7/8] [MLIR][Presburger][WIP] Preserve identifiers in
IntegerRelation::convertVarKind
---
.../Analysis/Presburger/IntegerRelation.cpp | 24 ++++------
.../Presburger/IntegerRelationTest.cpp | 46 +++++++++++++++++++
2 files changed, 55 insertions(+), 15 deletions(-)
diff --git a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
index be764bd7c9176b9..6d8e4231d8f5128 100644
--- a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
+++ b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
@@ -1325,23 +1325,17 @@ void IntegerRelation::convertVarKind(VarKind srcKind, unsigned varStart,
if (varStart >= varLimit)
return;
- // Append new local variables corresponding to the dimensions to be converted.
+ unsigned srcOffset = getVarKindOffset(srcKind);
+ unsigned dstOffset = getVarKindOffset(dstKind);
unsigned convertCount = varLimit - varStart;
- unsigned newVarsBegin = insertVar(dstKind, pos, convertCount);
+ int forwardMoveOffset = dstOffset > srcOffset ? -convertCount : 0;
- // Swap the new local variables with dimensions.
- //
- // Essentially, this moves the information corresponding to the specified ids
- // of kind `srcKind` to the `convertCount` newly created ids of kind
- // `dstKind`. In particular, this moves the columns in the constraint
- // matrices, and zeros out the initially occupied columns (because the newly
- // created ids we're swapping with were zero-initialized).
- unsigned offset = getVarKindOffset(srcKind);
- for (unsigned i = 0; i < convertCount; ++i)
- swapVar(offset + varStart + i, newVarsBegin + i);
-
- // Complete the move by deleting the initially occupied columns.
- removeVarRange(srcKind, varStart, varLimit);
+ equalities.moveColumns(srcOffset + varStart, convertCount,
+ dstOffset + pos + forwardMoveOffset);
+ inequalities.moveColumns(srcOffset + varStart, convertCount,
+ dstOffset + pos + forwardMoveOffset);
+
+ space.convertVarKind(srcKind, varStart, varLimit - varStart, dstKind, pos);
}
void IntegerRelation::addBound(BoundType type, unsigned pos,
diff --git a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
index 287f7c7c56549ff..3e94fd02936ded3 100644
--- a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
@@ -167,3 +167,49 @@ TEST(IntegerRelationTest, symbolicLexmax) {
EXPECT_TRUE(lexmax3.unboundedDomain.isIntegerEmpty());
EXPECT_TRUE(lexmax3.lexopt.isEqual(expectedLexmax3));
}
+
+TEST(IntegerRelationTest, convertVarKind) {
+ PresburgerSpace space = PresburgerSpace::getSetSpace(3, 3, 0);
+ space.resetIds();
+
+ // Attach identifiers.
+ int identifiers[6] = {0, 1, 2, 3, 4, 5};
+ space.getId(VarKind::SetDim, 0) = Identifier(&identifiers[0]);
+ space.getId(VarKind::SetDim, 1) = Identifier(&identifiers[1]);
+ space.getId(VarKind::SetDim, 2) = Identifier(&identifiers[2]);
+ space.getId(VarKind::Symbol, 0) = Identifier(&identifiers[3]);
+ space.getId(VarKind::Symbol, 1) = Identifier(&identifiers[4]);
+ space.getId(VarKind::Symbol, 2) = Identifier(&identifiers[5]);
+
+ // Cannot call parseIntegerRelation to test convertVarKind as
+ // parseIntegerRelation uses convertVarKind.
+ IntegerRelation rel = parseIntegerPolyhedron(
+ // 0 1 2 3 4 5
+ "(x, y, a)[U, V, W] : (x - U == 0, y + a - W == 0, U - V >= 0,"
+ "y - a >= 0)");
+ rel.setSpace(space);
+
+ // Make a few kind conversions.
+ rel.convertVarKind(VarKind::Symbol, 1, 2, VarKind::Domain, 0);
+ rel.convertVarKind(VarKind::Range, 2, 3, VarKind::Domain, 0);
+ rel.convertVarKind(VarKind::Range, 0, 2, VarKind::Symbol, 1);
+ rel.convertVarKind(VarKind::Domain, 1, 2, VarKind::Range, 0);
+ rel.convertVarKind(VarKind::Domain, 0, 1, VarKind::Range, 1);
+
+ space = rel.getSpace();
+
+ // Expected rel.
+ IntegerRelation expectedRel = parseIntegerPolyhedron(
+ "(V, a)[U, x, y, W] : (x - U == 0, y + a - W == 0, U - V >= 0,"
+ "y - a >= 0)");
+ expectedRel.setSpace(space);
+
+ EXPECT_TRUE(rel.isEqual(expectedRel));
+
+ EXPECT_EQ(space.getId(VarKind::SetDim, 0), Identifier(&identifiers[4]));
+ EXPECT_EQ(space.getId(VarKind::SetDim, 1), Identifier(&identifiers[2]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[3]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[0]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 2), Identifier(&identifiers[1]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 3), Identifier(&identifiers[5]));
+}
>From 3b667f5f54dfb83dfd506ab0caf93ac7f064f783 Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Mon, 30 Oct 2023 14:11:37 +0530
Subject: [PATCH 8/8] [MLIR][Presburger][WIP] Add convertVarKindToLocal unit
test
---
.../Presburger/IntegerRelationTest.cpp | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
index 3e94fd02936ded3..176eb52f44225da 100644
--- a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
@@ -213,3 +213,29 @@ TEST(IntegerRelationTest, convertVarKind) {
EXPECT_EQ(space.getId(VarKind::Symbol, 2), Identifier(&identifiers[1]));
EXPECT_EQ(space.getId(VarKind::Symbol, 3), Identifier(&identifiers[5]));
}
+
+TEST(IntegerRelationTest, convertVarKindToLocal) {
+ // Convert all range variables to local variables
+ IntegerRelation rel = parseRelationFromSet(
+ "(x, y, z) : (x >= 0, y >= 0, -z >= 0)", 1);
+ rel.convertToLocal(VarKind::Range, 0, rel.getNumRangeVars());
+ EXPECT_FALSE(rel.isEmptyByGCDTest());
+
+ // Convert all domain variables to local variables
+ IntegerRelation rel2 = parseRelationFromSet(
+ "(x, y, z) : (x >= 0, y >= 0, -z >= 0)", 2);
+ rel2.convertToLocal(VarKind::Domain, 0, rel.getNumDomainVars());
+ EXPECT_FALSE(rel2.isEmptyByGCDTest());
+
+ // Convert a prefix of range variables to local variables
+ IntegerRelation rel3 = parseRelationFromSet(
+ "(x, y, u, v) : (x >= 0, y >= 0, -u >= 0, -v >= 0)", 2);
+ rel3.convertToLocal(VarKind::Range, rel.getNumDomainVars(), 1);
+ EXPECT_FALSE(rel3.isEmptyByGCDTest());
+
+ // Convert a suffix of domain variables to local variables
+ IntegerRelation rel4 = parseRelationFromSet(
+ "(x, y, u, v) : (x >= 0, y >= 0, -u >= 0, -v >= 0)", 2);
+ rel4.convertToLocal(VarKind::Domain, rel.getNumDomainVars() - 1, rel.getNumDomainVars());
+ EXPECT_FALSE(rel4.isEmptyByGCDTest());
+}
More information about the Mlir-commits
mailing list