|
| 1 | +//===- PresburgerRelationTest.cpp - Tests for PresburgerRelation 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 | +#include "mlir/Analysis/Presburger/PresburgerRelation.h" |
| 9 | +#include "Parser.h" |
| 10 | + |
| 11 | +#include <gmock/gmock.h> |
| 12 | +#include <gtest/gtest.h> |
| 13 | +#include <iostream> |
| 14 | + |
| 15 | +using namespace mlir; |
| 16 | +using namespace presburger; |
| 17 | + |
| 18 | +static PresburgerRelation |
| 19 | +parsePresburgerRelationFromPresburgerSet(ArrayRef<StringRef> strs, |
| 20 | + unsigned numDomain) { |
| 21 | + assert(!strs.empty() && "strs should not be empty"); |
| 22 | + |
| 23 | + IntegerRelation rel = parseIntegerPolyhedron(strs[0]); |
| 24 | + rel.convertVarKind(VarKind::SetDim, 0, numDomain, VarKind::Domain); |
| 25 | + PresburgerRelation result(rel); |
| 26 | + for (unsigned i = 1, e = strs.size(); i < e; ++i) { |
| 27 | + rel = parseIntegerPolyhedron(strs[i]); |
| 28 | + rel.convertVarKind(VarKind::SetDim, 0, numDomain, VarKind::Domain); |
| 29 | + result.unionInPlace(rel); |
| 30 | + } |
| 31 | + return result; |
| 32 | +} |
| 33 | + |
| 34 | +TEST(PresburgerRelationTest, applyDomainAndRange) { |
| 35 | + { |
| 36 | + PresburgerRelation map1 = parsePresburgerRelationFromPresburgerSet( |
| 37 | + {// (x, y) -> (x + N, y - N) |
| 38 | + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0)", |
| 39 | + // (x, y) -> (y, x) |
| 40 | + "(x, y, a, b)[N] : (a - y == 0, b - x == 0)", |
| 41 | + // (x, y) -> (x + y, x - y) |
| 42 | + "(x, y, a, b)[N] : (a - x - y == 0, b - x + y == 0)"}, |
| 43 | + 2); |
| 44 | + PresburgerRelation map2 = parsePresburgerRelationFromPresburgerSet( |
| 45 | + {// (x, y) -> (x + y) |
| 46 | + "(x, y, r)[N] : (r - x - y == 0)", |
| 47 | + // (x, y) -> (N) |
| 48 | + "(x, y, r)[N] : (r - N == 0)", |
| 49 | + // (x, y) -> (y - x) |
| 50 | + "(x, y, r)[N] : (r + x - y == 0)"}, |
| 51 | + 2); |
| 52 | + |
| 53 | + map1.applyRange(map2); |
| 54 | + |
| 55 | + PresburgerRelation map3 = parsePresburgerRelationFromPresburgerSet( |
| 56 | + { |
| 57 | + // (x, y) -> (x + y) |
| 58 | + "(x, y, r)[N] : (r - x - y == 0)", |
| 59 | + // (x, y) -> (N) |
| 60 | + "(x, y, r)[N] : (r - N == 0)", |
| 61 | + // (x, y) -> (y - x - 2N) |
| 62 | + "(x, y, r)[N] : (r - y + x + 2 * N == 0)", |
| 63 | + // (x, y) -> (x - y) |
| 64 | + "(x, y, r)[N] : (r - x + y == 0)", |
| 65 | + // (x, y) -> (2x) |
| 66 | + "(x, y, r)[N] : (r - 2 * x == 0)", |
| 67 | + // (x, y) -> (-2y) |
| 68 | + "(x, y, r)[N] : (r + 2 * y == 0)", |
| 69 | + }, |
| 70 | + 2); |
| 71 | + |
| 72 | + EXPECT_TRUE(map1.isEqual(map3)); |
| 73 | + } |
| 74 | + |
| 75 | + { |
| 76 | + PresburgerRelation map1 = parsePresburgerRelationFromPresburgerSet( |
| 77 | + {// (x, y) -> (y, x) |
| 78 | + "(x, y, a, b)[N] : (y - a == 0, x - b == 0)", |
| 79 | + // (x, y) -> (x + N, y - N) |
| 80 | + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0)"}, |
| 81 | + 2); |
| 82 | + PresburgerRelation map2 = parsePresburgerRelationFromPresburgerSet( |
| 83 | + {// (x, y) -> (x - y) |
| 84 | + "(x, y, r)[N] : (x - y - r == 0)", |
| 85 | + // (x, y) -> N |
| 86 | + "(x, y, r)[N] : (N - r == 0)"}, |
| 87 | + 2); |
| 88 | + |
| 89 | + map1.applyDomain(map2); |
| 90 | + |
| 91 | + PresburgerRelation map3 = parsePresburgerRelationFromPresburgerSet( |
| 92 | + {// (y - x) -> (x, y) |
| 93 | + "(r, x, y)[N] : (y - x - r == 0)", |
| 94 | + // (x - y - 2N) -> (x, y) |
| 95 | + "(r, x, y)[N] : (x - y - 2 * N - r == 0)", |
| 96 | + // (x, y) -> N |
| 97 | + "(r, x, y)[N] : (N - r == 0)"}, |
| 98 | + 1); |
| 99 | + |
| 100 | + EXPECT_TRUE(map1.isEqual(map3)); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +TEST(PresburgerRelationTest, inverse) { |
| 105 | + { |
| 106 | + PresburgerRelation rel = parsePresburgerRelationFromPresburgerSet( |
| 107 | + {// (x, y) -> (-y, -x) |
| 108 | + "(x, y, a, b)[N] : (y + a == 0, x + b == 0)", |
| 109 | + // (x, y) -> (x + N, y - N) |
| 110 | + "(x, y, a, b)[N] : (x - a + N == 0, y - b - N == 0)"}, |
| 111 | + 2); |
| 112 | + |
| 113 | + rel.inverse(); |
| 114 | + |
| 115 | + PresburgerRelation inverseRel = parsePresburgerRelationFromPresburgerSet( |
| 116 | + {// (x, y) -> (-y, -x) |
| 117 | + "(x, y, a, b)[N] : (y + a == 0, x + b == 0)", |
| 118 | + // (x, y) -> (x - N, y + N) |
| 119 | + "(x, y, a, b)[N] : (x - N - a == 0, y + N - b == 0)"}, |
| 120 | + 2); |
| 121 | + |
| 122 | + EXPECT_TRUE(rel.isEqual(inverseRel)); |
| 123 | + } |
| 124 | +} |
0 commit comments