| 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 "Parser.h" |
| 11 |
#include "mlir/Analysis/Presburger/Simplex.h" |
| 12 |
|
| 13 |
#include <gmock/gmock.h> |
| 14 |
#include <gtest/gtest.h> |
| 15 |
|
| 16 |
using namespace mlir; |
| 17 |
using namespace presburger; |
| 18 |
|
| 19 |
static IntegerRelation parseRelationFromSet(StringRef set, unsigned numDomain) { |
| 20 |
IntegerRelation rel = parseIntegerPolyhedron(set); |
| 21 |
|
| 22 |
rel.convertVarKind(VarKind::SetDim, 0, numDomain, VarKind::Domain); |
| 23 |
|
| 24 |
return rel; |
| 25 |
} |
| 26 |
|
| 27 |
TEST(IntegerRelationTest, getDomainAndRangeSet) { |
| 28 |
IntegerRelation rel = parseRelationFromSet( |
| 29 |
"(x, xr)[N] : (xr - x - 10 == 0, xr >= 0, N - xr >= 0)", 1); |
| 30 |
|
| 31 |
IntegerPolyhedron domainSet = rel.getDomainSet(); |
| 32 |
|
| 33 |
IntegerPolyhedron expectedDomainSet = |
| 34 |
parseIntegerPolyhedron("(x)[N] : (x + 10 >= 0, N - x - 10 >= 0)"); |
| 35 |
|
| 36 |
EXPECT_TRUE(domainSet.isEqual(expectedDomainSet)); |
| 37 |
|
| 38 |
IntegerPolyhedron rangeSet = rel.getRangeSet(); |
| 39 |
|
| 40 |
IntegerPolyhedron expectedRangeSet = |
| 41 |
parseIntegerPolyhedron("(x)[N] : (x >= 0, N - x >= 0)"); |
| 42 |
|
| 43 |
EXPECT_TRUE(rangeSet.isEqual(expectedRangeSet)); |
| 44 |
} |
| 45 |
|
| 46 |
TEST(IntegerRelationTest, inverse) { |
| 47 |
IntegerRelation rel = |
| 48 |
parseRelationFromSet("(x, y, z)[N, M] : (z - x - y == 0, x >= 0, N - x " |
| 49 |
">= 0, y >= 0, M - y >= 0)", |
| 50 |
2); |
| 51 |
|
| 52 |
IntegerRelation inverseRel = |
| 53 |
parseRelationFromSet("(z, x, y)[N, M] : (x >= 0, N - x >= 0, y >= 0, M " |
| 54 |
"- y >= 0, x + y - z == 0)", |
| 55 |
1); |
| 56 |
|
| 57 |
rel.inverse(); |
| 58 |
|
| 59 |
EXPECT_TRUE(rel.isEqual(inverseRel)); |
| 60 |
} |
| 61 |
|
| 62 |
TEST(IntegerRelationTest, intersectDomainAndRange) { |
| 63 |
IntegerRelation rel = parseRelationFromSet( |
| 64 |
"(x, y, z)[N, M]: (y floordiv 2 - N >= 0, z floordiv 5 - M" |
| 65 |
">= 0, x + y + z floordiv 7 == 0)", |
| 66 |
1); |
| 67 |
|
| 68 |
{ |
| 69 |
IntegerPolyhedron poly = |
| 70 |
parseIntegerPolyhedron("(x)[N, M] : (x >= 0, M - x - 1 >= 0)"); |
| 71 |
|
| 72 |
IntegerRelation expectedRel = parseRelationFromSet( |
| 73 |
"(x, y, z)[N, M]: (y floordiv 2 - N >= 0, z floordiv 5 - M" |
| 74 |
">= 0, x + y + z floordiv 7 == 0, x >= 0, M - x - 1 >= 0)", |
| 75 |
1); |
| 76 |
|
| 77 |
IntegerRelation copyRel = rel; |
| 78 |
copyRel.intersectDomain(poly); |
| 79 |
EXPECT_TRUE(copyRel.isEqual(expectedRel)); |
| 80 |
} |
| 81 |
|
| 82 |
{ |
| 83 |
IntegerPolyhedron poly = parseIntegerPolyhedron( |
| 84 |
"(y, z)[N, M] : (y >= 0, M - y - 1 >= 0, y + z == 0)"); |
| 85 |
|
| 86 |
IntegerRelation expectedRel = parseRelationFromSet( |
| 87 |
"(x, y, z)[N, M]: (y floordiv 2 - N >= 0, z floordiv 5 - M" |
| 88 |
">= 0, x + y + z floordiv 7 == 0, y >= 0, M - y - 1 >= 0, y + z == 0)", |
| 89 |
1); |
| 90 |
|
| 91 |
IntegerRelation copyRel = rel; |
| 92 |
copyRel.intersectRange(poly); |
| 93 |
EXPECT_TRUE(copyRel.isEqual(expectedRel)); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
TEST(IntegerRelationTest, applyDomainAndRange) { |
| 98 |
|
| 99 |
{ |
| 100 |
IntegerRelation map1 = parseRelationFromSet( |
| 101 |
"(x, y, a, b)[N] : (a - x - N == 0, b - y + N == 0)", 2); |
| 102 |
IntegerRelation map2 = |
| 103 |
parseRelationFromSet("(x, y, a)[N] : (a - x - y == 0)", 2); |
| 104 |
|
| 105 |
map1.applyRange(map2); |
| 106 |
|
| 107 |
IntegerRelation map3 = |
| 108 |
parseRelationFromSet("(x, y, a)[N] : (a - x - y == 0)", 2); |
| 109 |
|
| 110 |
EXPECT_TRUE(map1.isEqual(map3)); |
| 111 |
} |
| 112 |
|
| 113 |
{ |
| 114 |
IntegerRelation map1 = parseRelationFromSet( |
| 115 |
"(x, y, a, b)[N] : (a - x + N == 0, b - y - N == 0)", 2); |
| 116 |
IntegerRelation map2 = |
| 117 |
parseRelationFromSet("(x, y, a, b)[N] : (a - N == 0, b - N == 0)", 2); |
| 118 |
|
| 119 |
IntegerRelation map3 = |
| 120 |
parseRelationFromSet("(x, y, a, b)[N] : (x - N == 0, y - N == 0)", 2); |
| 121 |
|
| 122 |
map1.applyDomain(map2); |
| 123 |
|
| 124 |
EXPECT_TRUE(map1.isEqual(map3)); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
TEST(IntegerRelationTest, symbolicLexmin) { |
| 129 |
SymbolicLexMin lexmin = |
| 130 |
parseRelationFromSet("(a, x)[b] : (x - a >= 0, x - b >= 0)", 1) |
| 131 |
.findSymbolicIntegerLexMin(); |
| 132 |
|
| 133 |
PWMAFunction expectedLexmin = parsePWMAF({ |
| 134 |
{"(a)[b] : (a - b >= 0)", "(a)[b] -> (a)"}, // a |
| 135 |
{"(a)[b] : (b - a - 1 >= 0)", "(a)[b] -> (b)"}, // b |
| 136 |
}); |
| 137 |
EXPECT_TRUE(lexmin.unboundedDomain.isIntegerEmpty()); |
| 138 |
EXPECT_TRUE(lexmin.lexmin.isEqual(expectedLexmin)); |
| 139 |
} |