1 //===- IntegerRelationTest.cpp - Tests for IntegerRelation class ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Analysis/Presburger/IntegerRelation.h"
10 #include "./Utils.h"
11 
12 #include <gmock/gmock.h>
13 #include <gtest/gtest.h>
14 
15 using namespace mlir;
16 using namespace presburger;
17 
18 static IntegerRelation parseRelationFromSet(StringRef set, unsigned numDomain) {
19   IntegerRelation rel = parsePoly(set);
20 
21   rel.convertVarKind(VarKind::SetDim, 0, numDomain, VarKind::Domain);
22 
23   return rel;
24 }
25 
26 TEST(IntegerRelationTest, getDomainAndRangeSet) {
27   IntegerRelation rel = parseRelationFromSet(
28       "(x, xr)[N] : (xr - x - 10 == 0, xr >= 0, N - xr >= 0)", 1);
29 
30   IntegerPolyhedron domainSet = rel.getDomainSet();
31 
32   IntegerPolyhedron expectedDomainSet =
33       parsePoly("(x)[N] : (x + 10 >= 0, N - x - 10 >= 0)");
34 
35   EXPECT_TRUE(domainSet.isEqual(expectedDomainSet));
36 
37   IntegerPolyhedron rangeSet = rel.getRangeSet();
38 
39   IntegerPolyhedron expectedRangeSet =
40       parsePoly("(x)[N] : (x >= 0, N - x >= 0)");
41 
42   EXPECT_TRUE(rangeSet.isEqual(expectedRangeSet));
43 }
44 
45 TEST(IntegerRelationTest, inverse) {
46   IntegerRelation rel =
47       parseRelationFromSet("(x, y, z)[N, M] : (z - x - y == 0, x >= 0, N - x "
48                            ">= 0, y >= 0, M - y >= 0)",
49                            2);
50 
51   IntegerRelation inverseRel =
52       parseRelationFromSet("(z, x, y)[N, M]  : (x >= 0, N - x >= 0, y >= 0, M "
53                            "- y >= 0, x + y - z == 0)",
54                            1);
55 
56   rel.inverse();
57 
58   EXPECT_TRUE(rel.isEqual(inverseRel));
59 }
60 
61 TEST(IntegerRelationTest, intersectDomainAndRange) {
62   IntegerRelation rel = parseRelationFromSet(
63       "(x, y, z)[N, M]: (y floordiv 2 - N >= 0, z floordiv 5 - M"
64       ">= 0, x + y + z floordiv 7 == 0)",
65       1);
66 
67   {
68     IntegerPolyhedron poly = parsePoly("(x)[N, M] : (x >= 0, M - x - 1 >= 0)");
69 
70     IntegerRelation expectedRel = parseRelationFromSet(
71         "(x, y, z)[N, M]: (y floordiv 2 - N >= 0, z floordiv 5 - M"
72         ">= 0, x + y + z floordiv 7 == 0, x >= 0, M - x - 1 >= 0)",
73         1);
74 
75     IntegerRelation copyRel = rel;
76     copyRel.intersectDomain(poly);
77     EXPECT_TRUE(copyRel.isEqual(expectedRel));
78   }
79 
80   {
81     IntegerPolyhedron poly =
82         parsePoly("(y, z)[N, M] : (y >= 0, M - y - 1 >= 0, y + z == 0)");
83 
84     IntegerRelation expectedRel = parseRelationFromSet(
85         "(x, y, z)[N, M]: (y floordiv 2 - N >= 0, z floordiv 5 - M"
86         ">= 0, x + y + z floordiv 7 == 0, y >= 0, M - y - 1 >= 0, y + z == 0)",
87         1);
88 
89     IntegerRelation copyRel = rel;
90     copyRel.intersectRange(poly);
91     EXPECT_TRUE(copyRel.isEqual(expectedRel));
92   }
93 }
94 
95 TEST(IntegerRelationTest, applyDomainAndRange) {
96 
97   {
98     IntegerRelation map1 = parseRelationFromSet(
99         "(x, y, a, b)[N] : (a - x - N == 0, b - y + N == 0)", 2);
100     IntegerRelation map2 =
101         parseRelationFromSet("(x, y, a)[N] : (a - x - y == 0)", 2);
102 
103     map1.applyRange(map2);
104 
105     IntegerRelation map3 =
106         parseRelationFromSet("(x, y, a)[N] : (a - x - y == 0)", 2);
107 
108     EXPECT_TRUE(map1.isEqual(map3));
109   }
110 
111   {
112     IntegerRelation map1 = parseRelationFromSet(
113         "(x, y, a, b)[N] : (a - x + N == 0, b - y - N == 0)", 2);
114     IntegerRelation map2 =
115         parseRelationFromSet("(x, y, a, b)[N] : (a - N == 0, b - N == 0)", 2);
116 
117     IntegerRelation map3 =
118         parseRelationFromSet("(x, y, a, b)[N] : (x - N == 0, y - N == 0)", 2);
119 
120     map1.applyDomain(map2);
121 
122     EXPECT_TRUE(map1.isEqual(map3));
123   }
124 }
125