[Mlir-commits] [mlir] [MLIR][Presburger] Preserve identifiers in IntegerRelation::convertVa… (PR #67909)
Bharathi Ramana Joshi
llvmlistbot at llvm.org
Sat Jan 20 12:14:10 PST 2024
https://github.com/iambrj updated https://github.com/llvm/llvm-project/pull/67909
>From 5a61c363c0b619df2bbd06080fa9e51f3ee838da Mon Sep 17 00:00:00 2001
From: iambrj <joshibharathiramana at gmail.com>
Date: Sun, 21 Jan 2024 01:39:04 +0530
Subject: [PATCH] [MLIR][Presburger] Implement preserve identifiers in
IntegerRelation::convertVarKind
---
.../Analysis/Presburger/IntegerRelation.cpp | 26 ++--
.../Presburger/IntegerRelationTest.cpp | 136 ++++++++++++++++++
2 files changed, 146 insertions(+), 16 deletions(-)
diff --git a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
index 7d2a63d17676f5..a07a1fb07d4ea3 100644
--- a/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
+++ b/mlir/lib/Analysis/Presburger/IntegerRelation.cpp
@@ -1452,28 +1452,22 @@ void IntegerRelation::removeRedundantLocalVars() {
void IntegerRelation::convertVarKind(VarKind srcKind, unsigned varStart,
unsigned varLimit, VarKind dstKind,
unsigned pos) {
- assert(varLimit <= getNumVarKind(srcKind) && "Invalid id range");
+ assert(varLimit <= getNumVarKind(srcKind) && "invalid id range");
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 00d2204c9c8ef1..945b3d502f6973 100644
--- a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
@@ -485,3 +485,139 @@ TEST(IntegerRelationTest, setId) {
EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[4]));
EXPECT_EQ(space.getId(VarKind::Symbol, 3), Identifier(&identifiers[6]));
}
+
+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]));
+}
+
+TEST(IntegerRelationTest, convertVarKindToLocal) {
+ // Convert all range variables to local variables.
+ IntegerRelation rel = parseRelationFromSet(
+ "(x, y, z)[N, M] : (x - y >= 0, y - N >= 0, 3 - z >= 0, 2 * M - 5 >= 0)",
+ 1);
+ PresburgerSpace space = rel.getSpace();
+ space.resetIds();
+ // Attach identifiers.
+ char identifiers[5] = {'x', 'y', 'z', 'N', 'M'};
+ space.getId(VarKind::Domain, 0) = Identifier(&identifiers[0]);
+ space.getId(VarKind::Range, 1) = Identifier(&identifiers[1]);
+ space.getId(VarKind::Range, 2) = Identifier(&identifiers[2]);
+ space.getId(VarKind::Symbol, 0) = Identifier(&identifiers[3]);
+ space.getId(VarKind::Symbol, 1) = Identifier(&identifiers[4]);
+ rel.setSpace(space);
+ rel.convertToLocal(VarKind::Range, 0, rel.getNumRangeVars());
+ IntegerRelation expectedRel =
+ parseRelationFromSet("(x)[N, M] : (x - N >= 0, 2 * M - 5 >= 0)", 1);
+ EXPECT_TRUE(rel.isEqual(expectedRel));
+ space = rel.getSpace();
+ EXPECT_EQ(space.getId(VarKind::Domain, 0), Identifier(&identifiers[0]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[3]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[4]));
+
+ // Convert all domain variables to local variables.
+ IntegerRelation rel2 = parseRelationFromSet(
+ "(x, y, z)[N, M] : (x - y >= 0, y - N >= 0, 3 - z >= 0, 2 * M - 5 >= 0)",
+ 2);
+ space = rel2.getSpace();
+ space.resetIds();
+ space.getId(VarKind::Domain, 0) = Identifier(&identifiers[0]);
+ space.getId(VarKind::Domain, 1) = Identifier(&identifiers[1]);
+ space.getId(VarKind::Range, 0) = Identifier(&identifiers[2]);
+ space.getId(VarKind::Symbol, 0) = Identifier(&identifiers[3]);
+ space.getId(VarKind::Symbol, 1) = Identifier(&identifiers[4]);
+ rel2.setSpace(space);
+ rel2.convertToLocal(VarKind::Domain, 0, rel2.getNumDomainVars());
+ expectedRel =
+ parseIntegerPolyhedron("(z)[N, M] : (3 - z >= 0, 2 * M - 5 >= 0)");
+ EXPECT_TRUE(rel2.isEqual(expectedRel));
+ space = rel2.getSpace();
+ EXPECT_EQ(space.getId(VarKind::Range, 0), Identifier(&identifiers[2]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[3]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[4]));
+
+ // Convert a prefix of range variables to local variables.
+ IntegerRelation rel3 = parseRelationFromSet(
+ "(x, y, z)[N, M] : (x - y >= 0, y - N >= 0, 3 - z >= 0, 2 * M - 5 >= 0)",
+ 1);
+ space = rel3.getSpace();
+ space.resetIds();
+ space.getId(VarKind::Domain, 0) = Identifier(&identifiers[0]);
+ space.getId(VarKind::Range, 0) = Identifier(&identifiers[1]);
+ space.getId(VarKind::Range, 1) = Identifier(&identifiers[2]);
+ space.getId(VarKind::Symbol, 0) = Identifier(&identifiers[3]);
+ space.getId(VarKind::Symbol, 1) = Identifier(&identifiers[4]);
+ rel3.setSpace(space);
+ rel3.convertToLocal(VarKind::Range, 0, 1);
+ expectedRel = parseRelationFromSet(
+ "(x, z)[N, M] : (x - N >= 0, 3 - z >= 0, 2 * M - 5 >= 0)", 1);
+ EXPECT_TRUE(rel3.isEqual(expectedRel));
+ space = rel3.getSpace();
+ EXPECT_EQ(space.getId(VarKind::Domain, 0), Identifier(&identifiers[0]));
+ EXPECT_EQ(space.getId(VarKind::Range, 0), Identifier(&identifiers[2]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[3]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[4]));
+
+ // Convert a suffix of domain variables to local variables.
+ IntegerRelation rel4 = parseRelationFromSet(
+ "(x, y, z)[N, M] : (x - y >= 0, y - N >= 0, 3 - z >= 0, 2 * M - 5 >= 0)",
+ 2);
+ space = rel4.getSpace();
+ space.resetIds();
+ space.getId(VarKind::Domain, 0) = Identifier(&identifiers[0]);
+ space.getId(VarKind::Domain, 1) = Identifier(&identifiers[1]);
+ space.getId(VarKind::Range, 0) = Identifier(&identifiers[2]);
+ space.getId(VarKind::Symbol, 0) = Identifier(&identifiers[3]);
+ space.getId(VarKind::Symbol, 1) = Identifier(&identifiers[4]);
+ rel4.setSpace(space);
+ rel4.convertToLocal(VarKind::Domain, rel4.getNumDomainVars() - 1,
+ rel4.getNumDomainVars());
+ // expectedRel same as before.
+ EXPECT_TRUE(rel4.isEqual(expectedRel));
+ space = rel4.getSpace();
+ EXPECT_EQ(space.getId(VarKind::Domain, 0), Identifier(&identifiers[0]));
+ EXPECT_EQ(space.getId(VarKind::Range, 0), Identifier(&identifiers[2]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[3]));
+ EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[4]));
+}
More information about the Mlir-commits
mailing list