Skip to content

Commit 3dd9931

Browse files
iambrjGroverkss
authored andcommitted
[MLIR][Presburger] Implement domain and range restriction for PresburgerRelation
This patch implements domain and range restriction for PresburgerRelation Reviewed By: Groverkss Differential Revision: https://reviews.llvm.org/D154798
1 parent 6236bf5 commit 3dd9931

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class PresburgerRelation {
6464
/// exceeds that of some disjunct, an assert failure will occur.
6565
void setSpace(const PresburgerSpace &oSpace);
6666

67+
void insertVarInPlace(VarKind kind, unsigned pos, unsigned num = 1);
68+
6769
/// Return a reference to the list of disjuncts.
6870
ArrayRef<IntegerRelation> getAllDisjuncts() const;
6971

@@ -83,6 +85,18 @@ class PresburgerRelation {
8385
/// Return the intersection of this set and the given set.
8486
PresburgerRelation intersect(const PresburgerRelation &set) const;
8587

88+
/// Intersect the given `set` with the range in-place.
89+
///
90+
/// Formally, let the relation `this` be R: A -> B and `set` is C, then this
91+
/// operation modifies R to be A -> (B intersection C).
92+
PresburgerRelation intersectRange(PresburgerSet &set);
93+
94+
/// Intersect the given `set` with the domain in-place.
95+
///
96+
/// Formally, let the relation `this` be R: A -> B and `set` is C, then this
97+
/// operation modifies R to be (A intersection C) -> B.
98+
PresburgerRelation intersectDomain(const PresburgerSet &set);
99+
86100
/// Invert the relation, i.e. swap its domain and range.
87101
///
88102
/// Formally, if `this`: A -> B then `inverse` updates `this` in-place to

mlir/lib/Analysis/Presburger/PresburgerRelation.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ void PresburgerRelation::setSpace(const PresburgerSpace &oSpace) {
3030
disjunct.setSpaceExceptLocals(space);
3131
}
3232

33+
void PresburgerRelation::insertVarInPlace(VarKind kind, unsigned pos,
34+
unsigned num) {
35+
for (IntegerRelation &cs : disjuncts)
36+
cs.insertVar(kind, pos, num);
37+
space.insertVar(kind, pos, num);
38+
}
39+
3340
unsigned PresburgerRelation::getNumDisjuncts() const {
3441
return disjuncts.size();
3542
}
@@ -117,6 +124,26 @@ PresburgerRelation::intersect(const PresburgerRelation &set) const {
117124
return result;
118125
}
119126

127+
PresburgerRelation PresburgerRelation::intersectRange(PresburgerSet &set) {
128+
assert(space.getRangeSpace().isCompatible(set.getSpace()) &&
129+
"Range of `this` must be compatible with range of `set`");
130+
131+
PresburgerRelation other = set;
132+
other.insertVarInPlace(VarKind::Domain, 0, getNumDomainVars());
133+
return intersect(other);
134+
}
135+
136+
PresburgerRelation
137+
PresburgerRelation::intersectDomain(const PresburgerSet &set) {
138+
assert(space.getDomainSpace().isCompatible(set.getSpace()) &&
139+
"Domain of `this` must be compatible with range of `set`");
140+
141+
PresburgerRelation other = set;
142+
other.insertVarInPlace(VarKind::Domain, 0, getNumDomainVars());
143+
other.inverse();
144+
return intersect(other);
145+
}
146+
120147
void PresburgerRelation::inverse() {
121148
for (IntegerRelation &cs : disjuncts)
122149
cs.inverse();

mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,73 @@ parsePresburgerRelationFromPresburgerSet(ArrayRef<StringRef> strs,
3131
return result;
3232
}
3333

34+
TEST(PresburgerRelationTest, intersectDomainAndRange) {
35+
PresburgerRelation rel = parsePresburgerRelationFromPresburgerSet(
36+
{// (x, y) -> (x + N, y - N)
37+
"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0)",
38+
// (x, y) -> (x + y, x - y)
39+
"(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0)",
40+
// (x, y) -> (x - y, y - x)}
41+
"(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0)"},
42+
2);
43+
44+
{
45+
PresburgerSet set =
46+
parsePresburgerSet({// (2x, x)
47+
"(a, b)[N] : (a - 2 * b == 0)",
48+
// (x, -x)
49+
"(a, b)[N] : (a + b == 0)",
50+
// (N, N)
51+
"(a, b)[N] : (a - N == 0, b - N == 0)"});
52+
53+
PresburgerRelation expectedRel = parsePresburgerRelationFromPresburgerSet(
54+
{"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, x - 2 * y == 0)",
55+
"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, x + y == 0)",
56+
"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, x - N == 0, y - N "
57+
"== 0)",
58+
"(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, x - 2 * y == 0)",
59+
"(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, x + y == 0)",
60+
"(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, x - N == 0, y - N "
61+
"== 0)",
62+
"(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, x - 2 * y == 0)",
63+
"(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, x + y == 0)",
64+
"(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, x - N == 0, y - N "
65+
"== 0)"},
66+
2);
67+
68+
PresburgerRelation computedRel = rel.intersectDomain(set);
69+
EXPECT_TRUE(computedRel.isEqual(expectedRel));
70+
}
71+
72+
{
73+
PresburgerSet set =
74+
parsePresburgerSet({// (2x, x)
75+
"(a, b)[N] : (a - 2 * b == 0)",
76+
// (x, -x)
77+
"(a, b)[N] : (a + b == 0)",
78+
// (N, N)
79+
"(a, b)[N] : (a - N == 0, b - N == 0)"});
80+
81+
PresburgerRelation expectedRel = parsePresburgerRelationFromPresburgerSet(
82+
{"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, a - 2 * b == 0)",
83+
"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, a + b == 0)",
84+
"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, a - N == 0, b - N "
85+
"== 0)",
86+
"(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, a - 2 * b == 0)",
87+
"(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, a + b == 0)",
88+
"(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, a - N == 0, b - N "
89+
"== 0)",
90+
"(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, a - 2 * b == 0)",
91+
"(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, a + b == 0)",
92+
"(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, a - N == 0, b - N "
93+
"== 0)"},
94+
2);
95+
96+
PresburgerRelation computedRel = rel.intersectRange(set);
97+
EXPECT_TRUE(computedRel.isEqual(expectedRel));
98+
}
99+
}
100+
34101
TEST(PresburgerRelationTest, applyDomainAndRange) {
35102
{
36103
PresburgerRelation map1 = parsePresburgerRelationFromPresburgerSet(

0 commit comments

Comments
 (0)