diff --git a/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h b/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h --- a/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h +++ b/mlir/include/mlir/Analysis/Presburger/PresburgerRelation.h @@ -83,6 +83,22 @@ /// Return the intersection of this set and the given set. PresburgerRelation intersect(const PresburgerRelation &set) const; + /// Intersect the given PresbrugerSet with the range of `this` + /// PresburgerRelation in-place. + /// + /// Formally, if `this`: A -> B and given `set`: B, then this function updates + /// `this` to `result`: A -> B where a point (a, b) belongs to `result` iff + /// b belongs to `set`. + void intersectRange(const PresburgerSet &set); + + /// Intersect the given PresbrugerSet with the domain of `this` + /// PresburgerRelation in-place. + /// + /// Formally, if `this`: A -> B and given `set`: A, then this function updates + /// `this` to `result`: A -> B where a point (a, b) belongs to `result` iff + /// a belongs to `set`. + void intersectDomain(const PresburgerSet &set); + /// Invert the relation, i.e. swap its domain and range. /// /// Formally, if `this`: A -> B then `inverse` updates `this` in-place to diff --git a/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp b/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp --- a/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp +++ b/mlir/lib/Analysis/Presburger/PresburgerRelation.cpp @@ -109,6 +109,38 @@ return result; } +void PresburgerRelation::intersectRange(const PresburgerSet &set) { + assert(space.getRangeSpace().isCompatible(set.getSpace()) && + "Range of `this` must be compatible with domain of `set`"); + + PresburgerRelation result(getSpace()); + for (const IntegerRelation &csA : disjuncts) { + for (const IntegerRelation &csB : set.disjuncts) { + IntegerRelation rangeIntersection = csA; + rangeIntersection.intersectRange(IntegerPolyhedron(csB)); + if (!rangeIntersection.isEmpty()) + result.unionInPlace(rangeIntersection); + } + } + *this = result; +} + +void PresburgerRelation::intersectDomain(const PresburgerSet &set) { + assert(space.getDomainSpace().isCompatible(set.getSpace()) && + "Domain of `this` must be compatible with domain of `set`"); + + PresburgerRelation result(getSpace()); + for (const IntegerRelation &csA : disjuncts) { + for (const IntegerRelation &csB : set.disjuncts) { + IntegerRelation domainIntersection = csA; + domainIntersection.intersectDomain(IntegerPolyhedron(csB)); + if (!domainIntersection.isEmpty()) + result.unionInPlace(domainIntersection); + } + } + *this = result; +} + void PresburgerRelation::inverse() { for (IntegerRelation &cs : disjuncts) cs.inverse(); diff --git a/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp b/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp --- a/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp +++ b/mlir/unittests/Analysis/Presburger/PresburgerRelationTest.cpp @@ -31,6 +31,75 @@ return result; } +TEST(PresburgerRelationTest, intersectDomainAndRange) { + PresburgerRelation rel = parsePresburgerRelationFromPresburgerSet( + {// (x, y) -> (x + N, y - N) + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0)", + // (x, y) -> (x + y, x - y) + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0)", + // (x, y) -> (x - y, y - x)} + "(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0)"}, + 2); + + { + PresburgerSet set = + parsePresburgerSet({// (2x, x) + "(a, b)[N] : (a - 2 * b == 0)", + // (x, -x) + "(a, b)[N] : (a + b == 0)", + // (N, N) + "(a, b)[N] : (a - N == 0, b - N == 0)"}); + + PresburgerRelation expectedRel = parsePresburgerRelationFromPresburgerSet( + {"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, x - 2 * y == 0)", + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, x + y == 0)", + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, x - N == 0, y - N " + "== 0)", + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, x - 2 * y == 0)", + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, x + y == 0)", + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, x - N == 0, y - N " + "== 0)", + "(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, x - 2 * y == 0)", + "(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, x + y == 0)", + "(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, x - N == 0, y - N " + "== 0)"}, + 2); + + PresburgerRelation copyRel = rel; + copyRel.intersectDomain(set); + EXPECT_TRUE(copyRel.isEqual(expectedRel)); + } + + { + PresburgerSet set = + parsePresburgerSet({// (2x, x) + "(a, b)[N] : (a - 2 * b == 0)", + // (x, -x) + "(a, b)[N] : (a + b == 0)", + // (N, N) + "(a, b)[N] : (a - N == 0, b - N == 0)"}); + + PresburgerRelation expectedRel = parsePresburgerRelationFromPresburgerSet( + {"(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, a - 2 * b == 0)", + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, a + b == 0)", + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0, a - N == 0, b - N " + "== 0)", + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, a - 2 * b == 0)", + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, a + b == 0)", + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0, a - N == 0, b - N " + "== 0)", + "(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, a - 2 * b == 0)", + "(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, a + b == 0)", + "(x, y, a, b)[N] : (a - x + y == 0, b - y + x == 0, a - N == 0, b - N " + "== 0)"}, + 2); + + PresburgerRelation copyRel = rel; + copyRel.intersectRange(set); + EXPECT_TRUE(copyRel.isEqual(expectedRel)); + } +} + TEST(PresburgerRelationTest, applyDomainAndRange) { { PresburgerRelation map1 = parsePresburgerRelationFromPresburgerSet(