16f9afad6SGroverkss //===- IntegerPolyhedron.cpp - Tests for IntegerPolyhedron class ----------===//
26f9afad6SGroverkss //
36f9afad6SGroverkss // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46f9afad6SGroverkss // See https://llvm.org/LICENSE.txt for license information.
56f9afad6SGroverkss // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66f9afad6SGroverkss //
76f9afad6SGroverkss //===----------------------------------------------------------------------===//
86f9afad6SGroverkss
98c867f78SGroverkss #include "Parser.h"
108c867f78SGroverkss #include "Utils.h"
11bb901355SGroverkss #include "mlir/Analysis/Presburger/IntegerRelation.h"
1279ad5fb2SArjun P #include "mlir/Analysis/Presburger/PWMAFunction.h"
139f8cb685SArjun P #include "mlir/Analysis/Presburger/Simplex.h"
146f9afad6SGroverkss
156f9afad6SGroverkss #include <gmock/gmock.h>
166f9afad6SGroverkss #include <gtest/gtest.h>
176f9afad6SGroverkss
1849b754b5SGroverkss #include <numeric>
19a1fe1f5fSKazu Hirata #include <optional>
2049b754b5SGroverkss
210c1f6865SGroverkss using namespace mlir;
220c1f6865SGroverkss using namespace presburger;
230c1f6865SGroverkss
246f9afad6SGroverkss using testing::ElementsAre;
256f9afad6SGroverkss
2649b754b5SGroverkss enum class TestFunction { Sample, Empty };
2749b754b5SGroverkss
286f9afad6SGroverkss /// Construct a IntegerPolyhedron from a set of inequality and
296f9afad6SGroverkss /// equality constraints.
306f9afad6SGroverkss static IntegerPolyhedron
makeSetFromConstraints(unsigned ids,ArrayRef<SmallVector<int64_t,4>> ineqs,ArrayRef<SmallVector<int64_t,4>> eqs,unsigned syms=0)316f9afad6SGroverkss makeSetFromConstraints(unsigned ids, ArrayRef<SmallVector<int64_t, 4>> ineqs,
326f9afad6SGroverkss ArrayRef<SmallVector<int64_t, 4>> eqs,
336f9afad6SGroverkss unsigned syms = 0) {
34a5a598beSGroverkss IntegerPolyhedron set(
35a5a598beSGroverkss ineqs.size(), eqs.size(), ids + 1,
36a5a598beSGroverkss PresburgerSpace::getSetSpace(ids - syms, syms, /*numLocals=*/0));
376f9afad6SGroverkss for (const auto &eq : eqs)
386f9afad6SGroverkss set.addEquality(eq);
396f9afad6SGroverkss for (const auto &ineq : ineqs)
406f9afad6SGroverkss set.addInequality(ineq);
416f9afad6SGroverkss return set;
426f9afad6SGroverkss }
436f9afad6SGroverkss
dump(ArrayRef<DynamicAPInt> vec)44*1a0e67d7SRamkumar Ramachandra static void dump(ArrayRef<DynamicAPInt> vec) {
45*1a0e67d7SRamkumar Ramachandra for (const DynamicAPInt &x : vec)
469f8cb685SArjun P llvm::errs() << x << ' ';
479f8cb685SArjun P llvm::errs() << '\n';
489f8cb685SArjun P }
499f8cb685SArjun P
5049b754b5SGroverkss /// If fn is TestFunction::Sample (default):
519f8cb685SArjun P ///
5249b754b5SGroverkss /// If hasSample is true, check that findIntegerSample returns a valid sample
539f8cb685SArjun P /// for the IntegerPolyhedron poly. Also check that getIntegerLexmin finds a
549f8cb685SArjun P /// non-empty lexmin.
559f8cb685SArjun P ///
5670c73d1bSKazu Hirata /// If hasSample is false, check that findIntegerSample returns std::nullopt
5770c73d1bSKazu Hirata /// and findIntegerLexMin returns Empty.
5849b754b5SGroverkss ///
5949b754b5SGroverkss /// If fn is TestFunction::Empty, check that isIntegerEmpty returns the
6049b754b5SGroverkss /// opposite of hasSample.
checkSample(bool hasSample,const IntegerPolyhedron & poly,TestFunction fn=TestFunction::Sample)6149b754b5SGroverkss static void checkSample(bool hasSample, const IntegerPolyhedron &poly,
6249b754b5SGroverkss TestFunction fn = TestFunction::Sample) {
63*1a0e67d7SRamkumar Ramachandra std::optional<SmallVector<DynamicAPInt, 8>> maybeSample;
64*1a0e67d7SRamkumar Ramachandra MaybeOptimum<SmallVector<DynamicAPInt, 8>> maybeLexMin;
6549b754b5SGroverkss switch (fn) {
6649b754b5SGroverkss case TestFunction::Sample:
6749b754b5SGroverkss maybeSample = poly.findIntegerSample();
68cfd6ba89SArjun P maybeLexMin = poly.findIntegerLexMin();
699f8cb685SArjun P
7049b754b5SGroverkss if (!hasSample) {
71491d2701SKazu Hirata EXPECT_FALSE(maybeSample.has_value());
72491d2701SKazu Hirata if (maybeSample.has_value()) {
739f8cb685SArjun P llvm::errs() << "findIntegerSample gave sample: ";
749f8cb685SArjun P dump(*maybeSample);
759f8cb685SArjun P }
769f8cb685SArjun P
779f8cb685SArjun P EXPECT_TRUE(maybeLexMin.isEmpty());
789f8cb685SArjun P if (maybeLexMin.isBounded()) {
796761dd7dSArjun P llvm::errs() << "findIntegerLexMin gave sample: ";
809f8cb685SArjun P dump(*maybeLexMin);
8149b754b5SGroverkss }
8249b754b5SGroverkss } else {
83491d2701SKazu Hirata ASSERT_TRUE(maybeSample.has_value());
8449b754b5SGroverkss EXPECT_TRUE(poly.containsPoint(*maybeSample));
859f8cb685SArjun P
869f8cb685SArjun P ASSERT_FALSE(maybeLexMin.isEmpty());
87f3e1dcc5SLorenzo Chelini if (maybeLexMin.isUnbounded()) {
889f8cb685SArjun P EXPECT_TRUE(Simplex(poly).isUnbounded());
89f3e1dcc5SLorenzo Chelini }
90f3e1dcc5SLorenzo Chelini if (maybeLexMin.isBounded()) {
91dc07d2c9SArjun P EXPECT_TRUE(poly.containsPointNoLocal(*maybeLexMin));
9249b754b5SGroverkss }
93f3e1dcc5SLorenzo Chelini }
9449b754b5SGroverkss break;
9549b754b5SGroverkss case TestFunction::Empty:
9649b754b5SGroverkss EXPECT_EQ(!hasSample, poly.isIntegerEmpty());
9749b754b5SGroverkss break;
9849b754b5SGroverkss }
9949b754b5SGroverkss }
10049b754b5SGroverkss
10149b754b5SGroverkss /// Check sampling for all the permutations of the dimensions for the given
10249b754b5SGroverkss /// constraint set. Since the GBR algorithm progresses dimension-wise, different
10349b754b5SGroverkss /// orderings may cause the algorithm to proceed differently. At least some of
10449b754b5SGroverkss ///.these permutations should make it past the heuristics and test the
10549b754b5SGroverkss /// implementation of the GBR algorithm itself.
10649b754b5SGroverkss /// Use TestFunction fn to test.
checkPermutationsSample(bool hasSample,unsigned nDim,ArrayRef<SmallVector<int64_t,4>> ineqs,ArrayRef<SmallVector<int64_t,4>> eqs,TestFunction fn=TestFunction::Sample)10749b754b5SGroverkss static void checkPermutationsSample(bool hasSample, unsigned nDim,
10849b754b5SGroverkss ArrayRef<SmallVector<int64_t, 4>> ineqs,
10949b754b5SGroverkss ArrayRef<SmallVector<int64_t, 4>> eqs,
11049b754b5SGroverkss TestFunction fn = TestFunction::Sample) {
11149b754b5SGroverkss SmallVector<unsigned, 4> perm(nDim);
11249b754b5SGroverkss std::iota(perm.begin(), perm.end(), 0);
11349b754b5SGroverkss auto permute = [&perm](ArrayRef<int64_t> coeffs) {
11449b754b5SGroverkss SmallVector<int64_t, 4> permuted;
11549b754b5SGroverkss for (unsigned id : perm)
11649b754b5SGroverkss permuted.push_back(coeffs[id]);
11749b754b5SGroverkss permuted.push_back(coeffs.back());
11849b754b5SGroverkss return permuted;
11949b754b5SGroverkss };
12049b754b5SGroverkss do {
12149b754b5SGroverkss SmallVector<SmallVector<int64_t, 4>, 4> permutedIneqs, permutedEqs;
12249b754b5SGroverkss for (const auto &ineq : ineqs)
12349b754b5SGroverkss permutedIneqs.push_back(permute(ineq));
12449b754b5SGroverkss for (const auto &eq : eqs)
12549b754b5SGroverkss permutedEqs.push_back(permute(eq));
12649b754b5SGroverkss
12749b754b5SGroverkss checkSample(hasSample,
12849b754b5SGroverkss makeSetFromConstraints(nDim, permutedIneqs, permutedEqs), fn);
12949b754b5SGroverkss } while (std::next_permutation(perm.begin(), perm.end()));
13049b754b5SGroverkss }
13149b754b5SGroverkss
TEST(IntegerPolyhedronTest,removeInequality)1326f9afad6SGroverkss TEST(IntegerPolyhedronTest, removeInequality) {
1336f9afad6SGroverkss IntegerPolyhedron set =
1346f9afad6SGroverkss makeSetFromConstraints(1, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}}, {});
1356f9afad6SGroverkss
1366f9afad6SGroverkss set.removeInequalityRange(0, 0);
1376f9afad6SGroverkss EXPECT_EQ(set.getNumInequalities(), 5u);
1386f9afad6SGroverkss
1396f9afad6SGroverkss set.removeInequalityRange(1, 3);
1406f9afad6SGroverkss EXPECT_EQ(set.getNumInequalities(), 3u);
1416f9afad6SGroverkss EXPECT_THAT(set.getInequality(0), ElementsAre(0, 0));
1426f9afad6SGroverkss EXPECT_THAT(set.getInequality(1), ElementsAre(3, 3));
1436f9afad6SGroverkss EXPECT_THAT(set.getInequality(2), ElementsAre(4, 4));
1446f9afad6SGroverkss
1456f9afad6SGroverkss set.removeInequality(1);
1466f9afad6SGroverkss EXPECT_EQ(set.getNumInequalities(), 2u);
1476f9afad6SGroverkss EXPECT_THAT(set.getInequality(0), ElementsAre(0, 0));
1486f9afad6SGroverkss EXPECT_THAT(set.getInequality(1), ElementsAre(4, 4));
1496f9afad6SGroverkss }
1506f9afad6SGroverkss
TEST(IntegerPolyhedronTest,removeEquality)1516f9afad6SGroverkss TEST(IntegerPolyhedronTest, removeEquality) {
1526f9afad6SGroverkss IntegerPolyhedron set =
1536f9afad6SGroverkss makeSetFromConstraints(1, {}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}});
1546f9afad6SGroverkss
1556f9afad6SGroverkss set.removeEqualityRange(0, 0);
1566f9afad6SGroverkss EXPECT_EQ(set.getNumEqualities(), 5u);
1576f9afad6SGroverkss
1586f9afad6SGroverkss set.removeEqualityRange(1, 3);
1596f9afad6SGroverkss EXPECT_EQ(set.getNumEqualities(), 3u);
1606f9afad6SGroverkss EXPECT_THAT(set.getEquality(0), ElementsAre(0, 0));
1616f9afad6SGroverkss EXPECT_THAT(set.getEquality(1), ElementsAre(3, 3));
1626f9afad6SGroverkss EXPECT_THAT(set.getEquality(2), ElementsAre(4, 4));
1636f9afad6SGroverkss
1646f9afad6SGroverkss set.removeEquality(1);
1656f9afad6SGroverkss EXPECT_EQ(set.getNumEqualities(), 2u);
1666f9afad6SGroverkss EXPECT_THAT(set.getEquality(0), ElementsAre(0, 0));
1676f9afad6SGroverkss EXPECT_THAT(set.getEquality(1), ElementsAre(4, 4));
1686f9afad6SGroverkss }
1696f9afad6SGroverkss
TEST(IntegerPolyhedronTest,clearConstraints)1706f9afad6SGroverkss TEST(IntegerPolyhedronTest, clearConstraints) {
1716f9afad6SGroverkss IntegerPolyhedron set = makeSetFromConstraints(1, {}, {});
1726f9afad6SGroverkss
1736f9afad6SGroverkss set.addInequality({1, 0});
1746f9afad6SGroverkss EXPECT_EQ(set.atIneq(0, 0), 1);
1756f9afad6SGroverkss EXPECT_EQ(set.atIneq(0, 1), 0);
1766f9afad6SGroverkss
1776f9afad6SGroverkss set.clearConstraints();
1786f9afad6SGroverkss
1796f9afad6SGroverkss set.addInequality({1, 0});
1806f9afad6SGroverkss EXPECT_EQ(set.atIneq(0, 0), 1);
1816f9afad6SGroverkss EXPECT_EQ(set.atIneq(0, 1), 0);
1826f9afad6SGroverkss }
1836f9afad6SGroverkss
TEST(IntegerPolyhedronTest,removeIdRange)1846f9afad6SGroverkss TEST(IntegerPolyhedronTest, removeIdRange) {
185a5a598beSGroverkss IntegerPolyhedron set(PresburgerSpace::getSetSpace(3, 2, 1));
1866f9afad6SGroverkss
1876f9afad6SGroverkss set.addInequality({10, 11, 12, 20, 21, 30, 40});
188d95140a5SGroverkss set.removeVar(VarKind::Symbol, 1);
1896f9afad6SGroverkss EXPECT_THAT(set.getInequality(0),
1906f9afad6SGroverkss testing::ElementsAre(10, 11, 12, 20, 30, 40));
1916f9afad6SGroverkss
192d95140a5SGroverkss set.removeVarRange(VarKind::SetDim, 0, 2);
1936f9afad6SGroverkss EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 30, 40));
1946f9afad6SGroverkss
195d95140a5SGroverkss set.removeVarRange(VarKind::Local, 1, 1);
1966f9afad6SGroverkss EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 30, 40));
1976f9afad6SGroverkss
198d95140a5SGroverkss set.removeVarRange(VarKind::Local, 0, 1);
1996f9afad6SGroverkss EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 40));
2006f9afad6SGroverkss }
2016f9afad6SGroverkss
TEST(IntegerPolyhedronTest,FindSampleTest)20249b754b5SGroverkss TEST(IntegerPolyhedronTest, FindSampleTest) {
20349b754b5SGroverkss // Bounded sets with only inequalities.
20449b754b5SGroverkss // 0 <= 7x <= 5
2058c867f78SGroverkss checkSample(true,
2068c867f78SGroverkss parseIntegerPolyhedron("(x) : (7 * x >= 0, -7 * x + 5 >= 0)"));
20749b754b5SGroverkss
20849b754b5SGroverkss // 1 <= 5x and 5x <= 4 (no solution).
2098c867f78SGroverkss checkSample(
2108c867f78SGroverkss false, parseIntegerPolyhedron("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)"));
21149b754b5SGroverkss
21249b754b5SGroverkss // 1 <= 5x and 5x <= 9 (solution: x = 1).
2138c867f78SGroverkss checkSample(
2148c867f78SGroverkss true, parseIntegerPolyhedron("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)"));
21549b754b5SGroverkss
21649b754b5SGroverkss // Bounded sets with equalities.
21749b754b5SGroverkss // x >= 8 and 40 >= y and x = y.
2188c867f78SGroverkss checkSample(true, parseIntegerPolyhedron(
2198c867f78SGroverkss "(x,y) : (x - 8 >= 0, -y + 40 >= 0, x - y == 0)"));
22049b754b5SGroverkss
22149b754b5SGroverkss // x <= 10 and y <= 10 and 10 <= z and x + 2y = 3z.
22249b754b5SGroverkss // solution: x = y = z = 10.
2238c867f78SGroverkss checkSample(true,
2248c867f78SGroverkss parseIntegerPolyhedron("(x,y,z) : (-x + 10 >= 0, -y + 10 >= 0, "
2254b86d559SArjun P "z - 10 >= 0, x + 2 * y - 3 * z == 0)"));
22649b754b5SGroverkss
22749b754b5SGroverkss // x <= 10 and y <= 10 and 11 <= z and x + 2y = 3z.
22849b754b5SGroverkss // This implies x + 2y >= 33 and x + 2y <= 30, which has no solution.
2298c867f78SGroverkss checkSample(false,
2308c867f78SGroverkss parseIntegerPolyhedron("(x,y,z) : (-x + 10 >= 0, -y + 10 >= 0, "
2314b86d559SArjun P "z - 11 >= 0, x + 2 * y - 3 * z == 0)"));
23249b754b5SGroverkss
23349b754b5SGroverkss // 0 <= r and r <= 3 and 4q + r = 7.
23449b754b5SGroverkss // Solution: q = 1, r = 3.
2358c867f78SGroverkss checkSample(true, parseIntegerPolyhedron(
2368c867f78SGroverkss "(q,r) : (r >= 0, -r + 3 >= 0, 4 * q + r - 7 == 0)"));
23749b754b5SGroverkss
23849b754b5SGroverkss // 4q + r = 7 and r = 0.
23949b754b5SGroverkss // Solution: q = 1, r = 3.
2408c867f78SGroverkss checkSample(false,
2418c867f78SGroverkss parseIntegerPolyhedron("(q,r) : (4 * q + r - 7 == 0, r == 0)"));
24249b754b5SGroverkss
24349b754b5SGroverkss // The next two sets are large sets that should take a long time to sample
24449b754b5SGroverkss // with a naive branch and bound algorithm but can be sampled efficiently with
24549b754b5SGroverkss // the GBR algorithm.
24649b754b5SGroverkss //
24749b754b5SGroverkss // This is a triangle with vertices at (1/3, 0), (2/3, 0) and (10000, 10000).
2488c867f78SGroverkss checkSample(
2498c867f78SGroverkss true, parseIntegerPolyhedron("(x,y) : (y >= 0, "
25049b754b5SGroverkss "300000 * x - 299999 * y - 100000 >= 0, "
2514b86d559SArjun P "-300000 * x + 299998 * y + 200000 >= 0)"));
25249b754b5SGroverkss
25349b754b5SGroverkss // This is a tetrahedron with vertices at
25449b754b5SGroverkss // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 10000), and (10000, 10000, 10000).
25549b754b5SGroverkss // The first three points form a triangular base on the xz plane with the
25649b754b5SGroverkss // apex at the fourth point, which is the only integer point.
25749b754b5SGroverkss checkPermutationsSample(
25849b754b5SGroverkss true, 3,
25949b754b5SGroverkss {
26049b754b5SGroverkss {0, 1, 0, 0}, // y >= 0
26149b754b5SGroverkss {0, -1, 1, 0}, // z >= y
26249b754b5SGroverkss {300000, -299998, -1,
26349b754b5SGroverkss -100000}, // -300000x + 299998y + 100000 + z <= 0.
26449b754b5SGroverkss {-150000, 149999, 0, 100000}, // -150000x + 149999y + 100000 >= 0.
26549b754b5SGroverkss },
26649b754b5SGroverkss {});
26749b754b5SGroverkss
26849b754b5SGroverkss // Same thing with some spurious extra dimensions equated to constants.
2698c867f78SGroverkss checkSample(true,
2708c867f78SGroverkss parseIntegerPolyhedron(
2718c867f78SGroverkss "(a,b,c,d,e) : (b + d - e >= 0, -b + c - d + e >= 0, "
27249b754b5SGroverkss "300000 * a - 299998 * b - c - 9 * d + 21 * e - 112000 >= 0, "
27349b754b5SGroverkss "-150000 * a + 149999 * b - 15 * d + 47 * e + 68000 >= 0, "
2744b86d559SArjun P "d - e == 0, d + e - 2000 == 0)"));
27549b754b5SGroverkss
27649b754b5SGroverkss // This is a tetrahedron with vertices at
27749b754b5SGroverkss // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 100), (100, 100 - 1/3, 100).
27849b754b5SGroverkss checkPermutationsSample(false, 3,
27949b754b5SGroverkss {
28049b754b5SGroverkss {0, 1, 0, 0},
28149b754b5SGroverkss {0, -300, 299, 0},
28249b754b5SGroverkss {300 * 299, -89400, -299, -100 * 299},
28349b754b5SGroverkss {-897, 894, 0, 598},
28449b754b5SGroverkss },
28549b754b5SGroverkss {});
28649b754b5SGroverkss
28749b754b5SGroverkss // Two tests involving equalities that are integer empty but not rational
28849b754b5SGroverkss // empty.
28949b754b5SGroverkss
29049b754b5SGroverkss // This is a line segment from (0, 1/3) to (100, 100 + 1/3).
2918c867f78SGroverkss checkSample(false,
2928c867f78SGroverkss parseIntegerPolyhedron(
2938c867f78SGroverkss "(x,y) : (x >= 0, -x + 100 >= 0, 3 * x - 3 * y + 1 == 0)"));
29449b754b5SGroverkss
29549b754b5SGroverkss // A thin parallelogram. 0 <= x <= 100 and x + 1/3 <= y <= x + 2/3.
2968c867f78SGroverkss checkSample(false, parseIntegerPolyhedron(
2978c867f78SGroverkss "(x,y) : (x >= 0, -x + 100 >= 0, "
2984b86d559SArjun P "3 * x - 3 * y + 2 >= 0, -3 * x + 3 * y - 1 >= 0)"));
29949b754b5SGroverkss
3008c867f78SGroverkss checkSample(true,
3018c867f78SGroverkss parseIntegerPolyhedron("(x,y) : (2 * x >= 0, -2 * x + 99 >= 0, "
3024b86d559SArjun P "2 * y >= 0, -2 * y + 99 >= 0)"));
30349b754b5SGroverkss
30449b754b5SGroverkss // 2D cone with apex at (10000, 10000) and
30549b754b5SGroverkss // edges passing through (1/3, 0) and (2/3, 0).
3068c867f78SGroverkss checkSample(true, parseIntegerPolyhedron(
3078c867f78SGroverkss "(x,y) : (300000 * x - 299999 * y - 100000 >= 0, "
3084b86d559SArjun P "-300000 * x + 299998 * y + 200000 >= 0)"));
30949b754b5SGroverkss
31049b754b5SGroverkss // Cartesian product of a tetrahedron and a 2D cone.
31149b754b5SGroverkss // The tetrahedron has vertices at
31249b754b5SGroverkss // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 10000), and (10000, 10000, 10000).
31349b754b5SGroverkss // The first three points form a triangular base on the xz plane with the
31449b754b5SGroverkss // apex at the fourth point, which is the only integer point.
31549b754b5SGroverkss // The cone has apex at (10000, 10000) and
31649b754b5SGroverkss // edges passing through (1/3, 0) and (2/3, 0).
31749b754b5SGroverkss checkPermutationsSample(
31849b754b5SGroverkss true /* not empty */, 5,
31949b754b5SGroverkss {
32049b754b5SGroverkss // Tetrahedron contraints:
32149b754b5SGroverkss {0, 1, 0, 0, 0, 0}, // y >= 0
32249b754b5SGroverkss {0, -1, 1, 0, 0, 0}, // z >= y
32349b754b5SGroverkss // -300000x + 299998y + 100000 + z <= 0.
32449b754b5SGroverkss {300000, -299998, -1, 0, 0, -100000},
32549b754b5SGroverkss // -150000x + 149999y + 100000 >= 0.
32649b754b5SGroverkss {-150000, 149999, 0, 0, 0, 100000},
32749b754b5SGroverkss
32849b754b5SGroverkss // Triangle constraints:
32949b754b5SGroverkss // 300000p - 299999q >= 100000
33049b754b5SGroverkss {0, 0, 0, 300000, -299999, -100000},
33149b754b5SGroverkss // -300000p + 299998q + 200000 >= 0
33249b754b5SGroverkss {0, 0, 0, -300000, 299998, 200000},
33349b754b5SGroverkss },
33449b754b5SGroverkss {});
33549b754b5SGroverkss
33649b754b5SGroverkss // Cartesian product of same tetrahedron as above and {(p, q) : 1/3 <= p <=
33749b754b5SGroverkss // 2/3}. Since the second set is empty, the whole set is too.
33849b754b5SGroverkss checkPermutationsSample(
33949b754b5SGroverkss false /* empty */, 5,
34049b754b5SGroverkss {
34149b754b5SGroverkss // Tetrahedron contraints:
34249b754b5SGroverkss {0, 1, 0, 0, 0, 0}, // y >= 0
34349b754b5SGroverkss {0, -1, 1, 0, 0, 0}, // z >= y
34449b754b5SGroverkss // -300000x + 299998y + 100000 + z <= 0.
34549b754b5SGroverkss {300000, -299998, -1, 0, 0, -100000},
34649b754b5SGroverkss // -150000x + 149999y + 100000 >= 0.
34749b754b5SGroverkss {-150000, 149999, 0, 0, 0, 100000},
34849b754b5SGroverkss
34949b754b5SGroverkss // Second set constraints:
35049b754b5SGroverkss // 3p >= 1
35149b754b5SGroverkss {0, 0, 0, 3, 0, -1},
35249b754b5SGroverkss // 3p <= 2
35349b754b5SGroverkss {0, 0, 0, -3, 0, 2},
35449b754b5SGroverkss },
35549b754b5SGroverkss {});
35649b754b5SGroverkss
35749b754b5SGroverkss // Cartesian product of same tetrahedron as above and
35849b754b5SGroverkss // {(p, q, r) : 1 <= p <= 2 and p = 3q + 3r}.
35949b754b5SGroverkss // Since the second set is empty, the whole set is too.
36049b754b5SGroverkss checkPermutationsSample(
36149b754b5SGroverkss false /* empty */, 5,
36249b754b5SGroverkss {
36349b754b5SGroverkss // Tetrahedron contraints:
36449b754b5SGroverkss {0, 1, 0, 0, 0, 0, 0}, // y >= 0
36549b754b5SGroverkss {0, -1, 1, 0, 0, 0, 0}, // z >= y
36649b754b5SGroverkss // -300000x + 299998y + 100000 + z <= 0.
36749b754b5SGroverkss {300000, -299998, -1, 0, 0, 0, -100000},
36849b754b5SGroverkss // -150000x + 149999y + 100000 >= 0.
36949b754b5SGroverkss {-150000, 149999, 0, 0, 0, 0, 100000},
37049b754b5SGroverkss
37149b754b5SGroverkss // Second set constraints:
37249b754b5SGroverkss // p >= 1
37349b754b5SGroverkss {0, 0, 0, 1, 0, 0, -1},
37449b754b5SGroverkss // p <= 2
37549b754b5SGroverkss {0, 0, 0, -1, 0, 0, 2},
37649b754b5SGroverkss },
37749b754b5SGroverkss {
37849b754b5SGroverkss {0, 0, 0, 1, -3, -3, 0}, // p = 3q + 3r
37949b754b5SGroverkss });
38049b754b5SGroverkss
38149b754b5SGroverkss // Cartesian product of a tetrahedron and a 2D cone.
38249b754b5SGroverkss // The tetrahedron is empty and has vertices at
38349b754b5SGroverkss // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 100), and (100, 100 - 1/3, 100).
38449b754b5SGroverkss // The cone has apex at (10000, 10000) and
38549b754b5SGroverkss // edges passing through (1/3, 0) and (2/3, 0).
38649b754b5SGroverkss // Since the tetrahedron is empty, the Cartesian product is too.
38749b754b5SGroverkss checkPermutationsSample(false /* empty */, 5,
38849b754b5SGroverkss {
38949b754b5SGroverkss // Tetrahedron contraints:
39049b754b5SGroverkss {0, 1, 0, 0, 0, 0},
39149b754b5SGroverkss {0, -300, 299, 0, 0, 0},
39249b754b5SGroverkss {300 * 299, -89400, -299, 0, 0, -100 * 299},
39349b754b5SGroverkss {-897, 894, 0, 0, 0, 598},
39449b754b5SGroverkss
39549b754b5SGroverkss // Triangle constraints:
39649b754b5SGroverkss // 300000p - 299999q >= 100000
39749b754b5SGroverkss {0, 0, 0, 300000, -299999, -100000},
39849b754b5SGroverkss // -300000p + 299998q + 200000 >= 0
39949b754b5SGroverkss {0, 0, 0, -300000, 299998, 200000},
40049b754b5SGroverkss },
40149b754b5SGroverkss {});
40249b754b5SGroverkss
40349b754b5SGroverkss // Cartesian product of same tetrahedron as above and
40449b754b5SGroverkss // {(p, q) : 1/3 <= p <= 2/3}.
40549b754b5SGroverkss checkPermutationsSample(false /* empty */, 5,
40649b754b5SGroverkss {
40749b754b5SGroverkss // Tetrahedron contraints:
40849b754b5SGroverkss {0, 1, 0, 0, 0, 0},
40949b754b5SGroverkss {0, -300, 299, 0, 0, 0},
41049b754b5SGroverkss {300 * 299, -89400, -299, 0, 0, -100 * 299},
41149b754b5SGroverkss {-897, 894, 0, 0, 0, 598},
41249b754b5SGroverkss
41349b754b5SGroverkss // Second set constraints:
41449b754b5SGroverkss // 3p >= 1
41549b754b5SGroverkss {0, 0, 0, 3, 0, -1},
41649b754b5SGroverkss // 3p <= 2
41749b754b5SGroverkss {0, 0, 0, -3, 0, 2},
41849b754b5SGroverkss },
41949b754b5SGroverkss {});
42049b754b5SGroverkss
4218c867f78SGroverkss checkSample(true, parseIntegerPolyhedron(
4228c867f78SGroverkss "(x, y, z) : (2 * x - 1 >= 0, x - y - 1 == 0, "
4234b86d559SArjun P "y - z == 0)"));
42449b754b5SGroverkss
425dc07d2c9SArjun P // Test with a local id.
4268c867f78SGroverkss checkSample(true, parseIntegerPolyhedron("(x) : (x == 5*(x floordiv 2))"));
427dc07d2c9SArjun P
42849b754b5SGroverkss // Regression tests for the computation of dual coefficients.
4298c867f78SGroverkss checkSample(false, parseIntegerPolyhedron("(x, y, z) : ("
43049b754b5SGroverkss "6*x - 4*y + 9*z + 2 >= 0,"
43149b754b5SGroverkss "x + 5*y + z + 5 >= 0,"
43249b754b5SGroverkss "-4*x + y + 2*z - 1 >= 0,"
43349b754b5SGroverkss "-3*x - 2*y - 7*z - 1 >= 0,"
4344b86d559SArjun P "-7*x - 5*y - 9*z - 1 >= 0)"));
4358c867f78SGroverkss checkSample(true, parseIntegerPolyhedron("(x, y, z) : ("
43649b754b5SGroverkss "3*x + 3*y + 3 >= 0,"
43749b754b5SGroverkss "-4*x - 8*y - z + 4 >= 0,"
43849b754b5SGroverkss "-7*x - 4*y + z + 1 >= 0,"
43949b754b5SGroverkss "2*x - 7*y - 8*z - 7 >= 0,"
4404b86d559SArjun P "9*x + 8*y - 9*z - 7 >= 0)"));
44149b754b5SGroverkss }
44249b754b5SGroverkss
TEST(IntegerPolyhedronTest,IsIntegerEmptyTest)44349b754b5SGroverkss TEST(IntegerPolyhedronTest, IsIntegerEmptyTest) {
44449b754b5SGroverkss // 1 <= 5x and 5x <= 4 (no solution).
4458c867f78SGroverkss EXPECT_TRUE(parseIntegerPolyhedron("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)")
4468c867f78SGroverkss .isIntegerEmpty());
44749b754b5SGroverkss // 1 <= 5x and 5x <= 9 (solution: x = 1).
4488c867f78SGroverkss EXPECT_FALSE(parseIntegerPolyhedron("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)")
4498c867f78SGroverkss .isIntegerEmpty());
45049b754b5SGroverkss
45149b754b5SGroverkss // Unbounded sets.
4528c867f78SGroverkss EXPECT_TRUE(
4538c867f78SGroverkss parseIntegerPolyhedron("(x,y,z) : (2 * y - 1 >= 0, -2 * y + 1 >= 0, "
4544b86d559SArjun P "2 * z - 1 >= 0, 2 * x - 1 == 0)")
45549b754b5SGroverkss .isIntegerEmpty());
45649b754b5SGroverkss
4578c867f78SGroverkss EXPECT_FALSE(parseIntegerPolyhedron(
4588c867f78SGroverkss "(x,y,z) : (2 * x - 1 >= 0, -3 * x + 3 >= 0, "
4594b86d559SArjun P "5 * z - 6 >= 0, -7 * z + 17 >= 0, 3 * y - 2 >= 0)")
46049b754b5SGroverkss .isIntegerEmpty());
46149b754b5SGroverkss
4628c867f78SGroverkss EXPECT_FALSE(parseIntegerPolyhedron(
4638c867f78SGroverkss "(x,y,z) : (2 * x - 1 >= 0, x - y - 1 == 0, y - z == 0)")
46449b754b5SGroverkss .isIntegerEmpty());
46549b754b5SGroverkss
46649b754b5SGroverkss // IntegerPolyhedron::isEmpty() does not detect the following sets to be
46749b754b5SGroverkss // empty.
46849b754b5SGroverkss
46949b754b5SGroverkss // 3x + 7y = 1 and 0 <= x, y <= 10.
47049b754b5SGroverkss // Since x and y are non-negative, 3x + 7y can never be 1.
4718c867f78SGroverkss EXPECT_TRUE(parseIntegerPolyhedron(
4728c867f78SGroverkss "(x,y) : (x >= 0, -x + 10 >= 0, y >= 0, -y + 10 >= 0, "
4734b86d559SArjun P "3 * x + 7 * y - 1 == 0)")
47449b754b5SGroverkss .isIntegerEmpty());
47549b754b5SGroverkss
47649b754b5SGroverkss // 2x = 3y and y = x - 1 and x + y = 6z + 2 and 0 <= x, y <= 100.
47749b754b5SGroverkss // Substituting y = x - 1 in 3y = 2x, we obtain x = 3 and hence y = 2.
47849b754b5SGroverkss // Since x + y = 5 cannot be equal to 6z + 2 for any z, the set is empty.
4798c867f78SGroverkss EXPECT_TRUE(parseIntegerPolyhedron(
4808c867f78SGroverkss "(x,y,z) : (x >= 0, -x + 100 >= 0, y >= 0, -y + 100 >= 0, "
4814b86d559SArjun P "2 * x - 3 * y == 0, x - y - 1 == 0, x + y - 6 * z - 2 == 0)")
48249b754b5SGroverkss .isIntegerEmpty());
48349b754b5SGroverkss
48449b754b5SGroverkss // 2x = 3y and y = x - 1 + 6z and x + y = 6q + 2 and 0 <= x, y <= 100.
48549b754b5SGroverkss // 2x = 3y implies x is a multiple of 3 and y is even.
48649b754b5SGroverkss // Now y = x - 1 + 6z implies y = 2 mod 3. In fact, since y is even, we have
48749b754b5SGroverkss // y = 2 mod 6. Then since x = y + 1 + 6z, we have x = 3 mod 6, implying
48849b754b5SGroverkss // x + y = 5 mod 6, which contradicts x + y = 6q + 2, so the set is empty.
48949b754b5SGroverkss EXPECT_TRUE(
4908c867f78SGroverkss parseIntegerPolyhedron(
49149b754b5SGroverkss "(x,y,z,q) : (x >= 0, -x + 100 >= 0, y >= 0, -y + 100 >= 0, "
4924b86d559SArjun P "2 * x - 3 * y == 0, x - y + 6 * z - 1 == 0, x + y - 6 * q - 2 == 0)")
49349b754b5SGroverkss .isIntegerEmpty());
49449b754b5SGroverkss
49549b754b5SGroverkss // Set with symbols.
4968c867f78SGroverkss EXPECT_FALSE(parseIntegerPolyhedron("(x)[s] : (x + s >= 0, x - s == 0)")
4978c867f78SGroverkss .isIntegerEmpty());
49849b754b5SGroverkss }
49949b754b5SGroverkss
TEST(IntegerPolyhedronTest,removeRedundantConstraintsTest)50049b754b5SGroverkss TEST(IntegerPolyhedronTest, removeRedundantConstraintsTest) {
50149b754b5SGroverkss IntegerPolyhedron poly =
5028c867f78SGroverkss parseIntegerPolyhedron("(x) : (x - 2 >= 0, -x + 2 >= 0, x - 2 == 0)");
50349b754b5SGroverkss poly.removeRedundantConstraints();
50449b754b5SGroverkss
50549b754b5SGroverkss // Both inequalities are redundant given the equality. Both have been removed.
50649b754b5SGroverkss EXPECT_EQ(poly.getNumInequalities(), 0u);
50749b754b5SGroverkss EXPECT_EQ(poly.getNumEqualities(), 1u);
50849b754b5SGroverkss
50949b754b5SGroverkss IntegerPolyhedron poly2 =
5108c867f78SGroverkss parseIntegerPolyhedron("(x,y) : (x - 3 >= 0, y - 2 >= 0, x - y == 0)");
51149b754b5SGroverkss poly2.removeRedundantConstraints();
51249b754b5SGroverkss
51349b754b5SGroverkss // The second inequality is redundant and should have been removed. The
51449b754b5SGroverkss // remaining inequality should be the first one.
51549b754b5SGroverkss EXPECT_EQ(poly2.getNumInequalities(), 1u);
51649b754b5SGroverkss EXPECT_THAT(poly2.getInequality(0), ElementsAre(1, 0, -3));
51749b754b5SGroverkss EXPECT_EQ(poly2.getNumEqualities(), 1u);
51849b754b5SGroverkss
51949b754b5SGroverkss IntegerPolyhedron poly3 =
5208c867f78SGroverkss parseIntegerPolyhedron("(x,y,z) : (x - y == 0, x - z == 0, y - z == 0)");
52149b754b5SGroverkss poly3.removeRedundantConstraints();
52249b754b5SGroverkss
52349b754b5SGroverkss // One of the three equalities can be removed.
52449b754b5SGroverkss EXPECT_EQ(poly3.getNumInequalities(), 0u);
52549b754b5SGroverkss EXPECT_EQ(poly3.getNumEqualities(), 2u);
52649b754b5SGroverkss
5278c867f78SGroverkss IntegerPolyhedron poly4 = parseIntegerPolyhedron(
5288c867f78SGroverkss "(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) : ("
52949b754b5SGroverkss "b - 1 >= 0,"
53049b754b5SGroverkss "-b + 500 >= 0,"
53149b754b5SGroverkss "-16 * d + f >= 0,"
53249b754b5SGroverkss "f - 1 >= 0,"
53349b754b5SGroverkss "-f + 998 >= 0,"
53449b754b5SGroverkss "16 * d - f + 15 >= 0,"
53549b754b5SGroverkss "-16 * e + g >= 0,"
53649b754b5SGroverkss "g - 1 >= 0,"
53749b754b5SGroverkss "-g + 998 >= 0,"
53849b754b5SGroverkss "16 * e - g + 15 >= 0,"
53949b754b5SGroverkss "h >= 0,"
54049b754b5SGroverkss "-h + 1 >= 0,"
54149b754b5SGroverkss "j - 1 >= 0,"
54249b754b5SGroverkss "-j + 500 >= 0,"
54349b754b5SGroverkss "-f + 16 * l + 15 >= 0,"
54449b754b5SGroverkss "f - 16 * l >= 0,"
54549b754b5SGroverkss "-16 * m + o >= 0,"
54649b754b5SGroverkss "o - 1 >= 0,"
54749b754b5SGroverkss "-o + 998 >= 0,"
54849b754b5SGroverkss "16 * m - o + 15 >= 0,"
54949b754b5SGroverkss "p >= 0,"
55049b754b5SGroverkss "-p + 1 >= 0,"
55149b754b5SGroverkss "-g - h + 8 * q + 8 >= 0,"
55249b754b5SGroverkss "-o - p + 8 * q + 8 >= 0,"
55349b754b5SGroverkss "o + p - 8 * q - 1 >= 0,"
55449b754b5SGroverkss "g + h - 8 * q - 1 >= 0,"
55549b754b5SGroverkss "-f + n >= 0,"
55649b754b5SGroverkss "f - n >= 0,"
55749b754b5SGroverkss "k - 10 >= 0,"
55849b754b5SGroverkss "-k + 10 >= 0,"
55949b754b5SGroverkss "i - 13 >= 0,"
56049b754b5SGroverkss "-i + 13 >= 0,"
56149b754b5SGroverkss "c - 10 >= 0,"
56249b754b5SGroverkss "-c + 10 >= 0,"
56349b754b5SGroverkss "a - 13 >= 0,"
56449b754b5SGroverkss "-a + 13 >= 0"
5654b86d559SArjun P ")");
56649b754b5SGroverkss
56749b754b5SGroverkss // The above is a large set of constraints without any redundant constraints,
56849b754b5SGroverkss // as verified by the Fourier-Motzkin based removeRedundantInequalities.
56949b754b5SGroverkss unsigned nIneq = poly4.getNumInequalities();
57049b754b5SGroverkss unsigned nEq = poly4.getNumEqualities();
57149b754b5SGroverkss poly4.removeRedundantInequalities();
57249b754b5SGroverkss ASSERT_EQ(poly4.getNumInequalities(), nIneq);
57349b754b5SGroverkss ASSERT_EQ(poly4.getNumEqualities(), nEq);
57449b754b5SGroverkss // Now we test that removeRedundantConstraints does not find any constraints
57549b754b5SGroverkss // to be redundant either.
57649b754b5SGroverkss poly4.removeRedundantConstraints();
57749b754b5SGroverkss EXPECT_EQ(poly4.getNumInequalities(), nIneq);
57849b754b5SGroverkss EXPECT_EQ(poly4.getNumEqualities(), nEq);
57949b754b5SGroverkss
5808c867f78SGroverkss IntegerPolyhedron poly5 = parseIntegerPolyhedron(
5814b86d559SArjun P "(x,y) : (128 * x + 127 >= 0, -x + 7 >= 0, -128 * x + y >= 0, y >= 0)");
58249b754b5SGroverkss // 128x + 127 >= 0 implies that 128x >= 0, since x has to be an integer.
58349b754b5SGroverkss // (This should be caught by GCDTightenInqualities().)
58449b754b5SGroverkss // So -128x + y >= 0 and 128x + 127 >= 0 imply y >= 0 since we have
58549b754b5SGroverkss // y >= 128x >= 0.
58649b754b5SGroverkss poly5.removeRedundantConstraints();
58749b754b5SGroverkss EXPECT_EQ(poly5.getNumInequalities(), 3u);
588*1a0e67d7SRamkumar Ramachandra SmallVector<DynamicAPInt, 8> redundantConstraint =
589*1a0e67d7SRamkumar Ramachandra getDynamicAPIntVec({0, 1, 0});
59049b754b5SGroverkss for (unsigned i = 0; i < 3; ++i) {
59149b754b5SGroverkss // Ensure that the removed constraint was the redundant constraint [3].
592*1a0e67d7SRamkumar Ramachandra EXPECT_NE(poly5.getInequality(i),
593*1a0e67d7SRamkumar Ramachandra ArrayRef<DynamicAPInt>(redundantConstraint));
59449b754b5SGroverkss }
59549b754b5SGroverkss }
59649b754b5SGroverkss
TEST(IntegerPolyhedronTest,addConstantUpperBound)59749b754b5SGroverkss TEST(IntegerPolyhedronTest, addConstantUpperBound) {
598a5a598beSGroverkss IntegerPolyhedron poly(PresburgerSpace::getSetSpace(2));
59947bff1ccSMatthias Springer poly.addBound(BoundType::UB, 0, 1);
60049b754b5SGroverkss EXPECT_EQ(poly.atIneq(0, 0), -1);
60149b754b5SGroverkss EXPECT_EQ(poly.atIneq(0, 1), 0);
60249b754b5SGroverkss EXPECT_EQ(poly.atIneq(0, 2), 1);
60349b754b5SGroverkss
60447bff1ccSMatthias Springer poly.addBound(BoundType::UB, {1, 2, 3}, 1);
60549b754b5SGroverkss EXPECT_EQ(poly.atIneq(1, 0), -1);
60649b754b5SGroverkss EXPECT_EQ(poly.atIneq(1, 1), -2);
60749b754b5SGroverkss EXPECT_EQ(poly.atIneq(1, 2), -2);
60849b754b5SGroverkss }
60949b754b5SGroverkss
TEST(IntegerPolyhedronTest,addConstantLowerBound)61049b754b5SGroverkss TEST(IntegerPolyhedronTest, addConstantLowerBound) {
611a5a598beSGroverkss IntegerPolyhedron poly(PresburgerSpace::getSetSpace(2));
61247bff1ccSMatthias Springer poly.addBound(BoundType::LB, 0, 1);
61349b754b5SGroverkss EXPECT_EQ(poly.atIneq(0, 0), 1);
61449b754b5SGroverkss EXPECT_EQ(poly.atIneq(0, 1), 0);
61549b754b5SGroverkss EXPECT_EQ(poly.atIneq(0, 2), -1);
61649b754b5SGroverkss
61747bff1ccSMatthias Springer poly.addBound(BoundType::LB, {1, 2, 3}, 1);
61849b754b5SGroverkss EXPECT_EQ(poly.atIneq(1, 0), 1);
61949b754b5SGroverkss EXPECT_EQ(poly.atIneq(1, 1), 2);
62049b754b5SGroverkss EXPECT_EQ(poly.atIneq(1, 2), 2);
62149b754b5SGroverkss }
62249b754b5SGroverkss
62349b754b5SGroverkss /// Check if the expected division representation of local variables matches the
62449b754b5SGroverkss /// computed representation. The expected division representation is given as
62549b754b5SGroverkss /// a vector of expressions set in `expectedDividends` and the corressponding
62649b754b5SGroverkss /// denominator in `expectedDenominators`. The `denominators` and `dividends`
62749b754b5SGroverkss /// obtained through `getLocalRepr` function is verified against the
62849b754b5SGroverkss /// `expectedDenominators` and `expectedDividends` respectively.
checkDivisionRepresentation(IntegerPolyhedron & poly,const std::vector<SmallVector<int64_t,8>> & expectedDividends,ArrayRef<int64_t> expectedDenominators)62949b754b5SGroverkss static void checkDivisionRepresentation(
63049b754b5SGroverkss IntegerPolyhedron &poly,
63149b754b5SGroverkss const std::vector<SmallVector<int64_t, 8>> &expectedDividends,
6326d6f6c4dSArjun P ArrayRef<int64_t> expectedDenominators) {
633479c4f64SGroverkss DivisionRepr divs = poly.getLocalReprs();
63449b754b5SGroverkss
63549b754b5SGroverkss // Check that the `denominators` and `expectedDenominators` match.
636*1a0e67d7SRamkumar Ramachandra EXPECT_EQ(ArrayRef<DynamicAPInt>(getDynamicAPIntVec(expectedDenominators)),
6376d6f6c4dSArjun P divs.getDenoms());
63849b754b5SGroverkss
63949b754b5SGroverkss // Check that the `dividends` and `expectedDividends` match. If the
64049b754b5SGroverkss // denominator for a division is zero, we ignore its dividend.
641479c4f64SGroverkss EXPECT_TRUE(divs.getNumDivs() == expectedDividends.size());
642477c2c6fSArjun P for (unsigned i = 0, e = divs.getNumDivs(); i < e; ++i) {
643477c2c6fSArjun P if (divs.hasRepr(i)) {
644477c2c6fSArjun P for (unsigned j = 0, f = divs.getNumVars() + 1; j < f; ++j) {
645479c4f64SGroverkss EXPECT_TRUE(expectedDividends[i][j] == divs.getDividend(i)[j]);
6460e98fadcSArjun P }
647477c2c6fSArjun P }
648477c2c6fSArjun P }
649477c2c6fSArjun P }
65049b754b5SGroverkss
TEST(IntegerPolyhedronTest,computeLocalReprSimple)65149b754b5SGroverkss TEST(IntegerPolyhedronTest, computeLocalReprSimple) {
652a5a598beSGroverkss IntegerPolyhedron poly(PresburgerSpace::getSetSpace(1));
65349b754b5SGroverkss
65449b754b5SGroverkss poly.addLocalFloorDiv({1, 4}, 10);
65549b754b5SGroverkss poly.addLocalFloorDiv({1, 0, 100}, 10);
65649b754b5SGroverkss
65749b754b5SGroverkss std::vector<SmallVector<int64_t, 8>> divisions = {{1, 0, 0, 4},
65849b754b5SGroverkss {1, 0, 0, 100}};
6596d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {10, 10};
66049b754b5SGroverkss
66149b754b5SGroverkss // Check if floordivs can be computed when no other inequalities exist
66249b754b5SGroverkss // and floor divs do not depend on each other.
66349b754b5SGroverkss checkDivisionRepresentation(poly, divisions, denoms);
66449b754b5SGroverkss }
66549b754b5SGroverkss
TEST(IntegerPolyhedronTest,computeLocalReprConstantFloorDiv)66649b754b5SGroverkss TEST(IntegerPolyhedronTest, computeLocalReprConstantFloorDiv) {
667a5a598beSGroverkss IntegerPolyhedron poly(PresburgerSpace::getSetSpace(4));
66849b754b5SGroverkss
66949b754b5SGroverkss poly.addInequality({1, 0, 3, 1, 2});
67049b754b5SGroverkss poly.addInequality({1, 2, -8, 1, 10});
67149b754b5SGroverkss poly.addEquality({1, 2, -4, 1, 10});
67249b754b5SGroverkss
67349b754b5SGroverkss poly.addLocalFloorDiv({0, 0, 0, 0, 100}, 30);
67449b754b5SGroverkss poly.addLocalFloorDiv({0, 0, 0, 0, 0, 206}, 101);
67549b754b5SGroverkss
67649b754b5SGroverkss std::vector<SmallVector<int64_t, 8>> divisions = {{0, 0, 0, 0, 0, 0, 3},
67749b754b5SGroverkss {0, 0, 0, 0, 0, 0, 2}};
6786d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {1, 1};
67949b754b5SGroverkss
68049b754b5SGroverkss // Check if floordivs with constant numerator can be computed.
68149b754b5SGroverkss checkDivisionRepresentation(poly, divisions, denoms);
68249b754b5SGroverkss }
68349b754b5SGroverkss
TEST(IntegerPolyhedronTest,computeLocalReprRecursive)68449b754b5SGroverkss TEST(IntegerPolyhedronTest, computeLocalReprRecursive) {
685a5a598beSGroverkss IntegerPolyhedron poly(PresburgerSpace::getSetSpace(4));
68649b754b5SGroverkss poly.addInequality({1, 0, 3, 1, 2});
68749b754b5SGroverkss poly.addInequality({1, 2, -8, 1, 10});
68849b754b5SGroverkss poly.addEquality({1, 2, -4, 1, 10});
68949b754b5SGroverkss
69049b754b5SGroverkss poly.addLocalFloorDiv({0, -2, 7, 2, 10}, 3);
69149b754b5SGroverkss poly.addLocalFloorDiv({3, 0, 9, 2, 2, 10}, 5);
69249b754b5SGroverkss poly.addLocalFloorDiv({0, 1, -123, 2, 0, -4, 10}, 3);
69349b754b5SGroverkss
69449b754b5SGroverkss poly.addInequality({1, 2, -2, 1, -5, 0, 6, 100});
69549b754b5SGroverkss poly.addInequality({1, 2, -8, 1, 3, 7, 0, -9});
69649b754b5SGroverkss
69749b754b5SGroverkss std::vector<SmallVector<int64_t, 8>> divisions = {
69849b754b5SGroverkss {0, -2, 7, 2, 0, 0, 0, 10},
69949b754b5SGroverkss {3, 0, 9, 2, 2, 0, 0, 10},
70049b754b5SGroverkss {0, 1, -123, 2, 0, -4, 0, 10}};
70149b754b5SGroverkss
7026d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {3, 5, 3};
70349b754b5SGroverkss
70449b754b5SGroverkss // Check if floordivs which may depend on other floordivs can be computed.
70549b754b5SGroverkss checkDivisionRepresentation(poly, divisions, denoms);
70649b754b5SGroverkss }
70749b754b5SGroverkss
TEST(IntegerPolyhedronTest,computeLocalReprTightUpperBound)70849b754b5SGroverkss TEST(IntegerPolyhedronTest, computeLocalReprTightUpperBound) {
70949b754b5SGroverkss {
7108c867f78SGroverkss IntegerPolyhedron poly = parseIntegerPolyhedron("(i) : (i mod 3 - 1 >= 0)");
71149b754b5SGroverkss
71249b754b5SGroverkss // The set formed by the poly is:
71349b754b5SGroverkss // 3q - i + 2 >= 0 <-- Division lower bound
71449b754b5SGroverkss // -3q + i - 1 >= 0
71549b754b5SGroverkss // -3q + i >= 0 <-- Division upper bound
71649b754b5SGroverkss // We remove redundant constraints to get the set:
71749b754b5SGroverkss // 3q - i + 2 >= 0 <-- Division lower bound
71849b754b5SGroverkss // -3q + i - 1 >= 0 <-- Tighter division upper bound
71949b754b5SGroverkss // thus, making the upper bound tighter.
72049b754b5SGroverkss poly.removeRedundantConstraints();
72149b754b5SGroverkss
72249b754b5SGroverkss std::vector<SmallVector<int64_t, 8>> divisions = {{1, 0, 0}};
7236d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {3};
72449b754b5SGroverkss
72549b754b5SGroverkss // Check if the divisions can be computed even with a tighter upper bound.
72649b754b5SGroverkss checkDivisionRepresentation(poly, divisions, denoms);
72749b754b5SGroverkss }
72849b754b5SGroverkss
72949b754b5SGroverkss {
7308c867f78SGroverkss IntegerPolyhedron poly = parseIntegerPolyhedron(
7318c867f78SGroverkss "(i, j, q) : (4*q - i - j + 2 >= 0, -4*q + i + j >= 0)");
73249b754b5SGroverkss // Convert `q` to a local variable.
733d95140a5SGroverkss poly.convertToLocal(VarKind::SetDim, 2, 3);
73449b754b5SGroverkss
73549b754b5SGroverkss std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 1}};
7366d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {4};
73749b754b5SGroverkss
73849b754b5SGroverkss // Check if the divisions can be computed even with a tighter upper bound.
73949b754b5SGroverkss checkDivisionRepresentation(poly, divisions, denoms);
74049b754b5SGroverkss }
74149b754b5SGroverkss }
74249b754b5SGroverkss
TEST(IntegerPolyhedronTest,computeLocalReprFromEquality)7431e7c464dSPrashant Kumar TEST(IntegerPolyhedronTest, computeLocalReprFromEquality) {
7441e7c464dSPrashant Kumar {
7458c867f78SGroverkss IntegerPolyhedron poly =
7468c867f78SGroverkss parseIntegerPolyhedron("(i, j, q) : (-4*q + i + j == 0)");
7471e7c464dSPrashant Kumar // Convert `q` to a local variable.
748d95140a5SGroverkss poly.convertToLocal(VarKind::SetDim, 2, 3);
7491e7c464dSPrashant Kumar
750e9fa1863SArjun P std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 0}};
7516d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {4};
7521e7c464dSPrashant Kumar
7531e7c464dSPrashant Kumar checkDivisionRepresentation(poly, divisions, denoms);
7541e7c464dSPrashant Kumar }
7551e7c464dSPrashant Kumar {
7568c867f78SGroverkss IntegerPolyhedron poly =
7578c867f78SGroverkss parseIntegerPolyhedron("(i, j, q) : (4*q - i - j == 0)");
7581e7c464dSPrashant Kumar // Convert `q` to a local variable.
759d95140a5SGroverkss poly.convertToLocal(VarKind::SetDim, 2, 3);
7601e7c464dSPrashant Kumar
761e9fa1863SArjun P std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 0}};
7626d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {4};
7631e7c464dSPrashant Kumar
7641e7c464dSPrashant Kumar checkDivisionRepresentation(poly, divisions, denoms);
7651e7c464dSPrashant Kumar }
7661e7c464dSPrashant Kumar {
7678c867f78SGroverkss IntegerPolyhedron poly =
7688c867f78SGroverkss parseIntegerPolyhedron("(i, j, q) : (3*q + i + j - 2 == 0)");
7691e7c464dSPrashant Kumar // Convert `q` to a local variable.
770d95140a5SGroverkss poly.convertToLocal(VarKind::SetDim, 2, 3);
7711e7c464dSPrashant Kumar
772e9fa1863SArjun P std::vector<SmallVector<int64_t, 8>> divisions = {{-1, -1, 0, 2}};
7736d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {3};
7741e7c464dSPrashant Kumar
7751e7c464dSPrashant Kumar checkDivisionRepresentation(poly, divisions, denoms);
7761e7c464dSPrashant Kumar }
7771e7c464dSPrashant Kumar }
7781e7c464dSPrashant Kumar
TEST(IntegerPolyhedronTest,computeLocalReprFromEqualityAndInequality)7791e7c464dSPrashant Kumar TEST(IntegerPolyhedronTest, computeLocalReprFromEqualityAndInequality) {
7801e7c464dSPrashant Kumar {
7811e7c464dSPrashant Kumar IntegerPolyhedron poly =
7828c867f78SGroverkss parseIntegerPolyhedron("(i, j, q, k) : (-3*k + i + j == 0, 4*q - "
7834b86d559SArjun P "i - j + 2 >= 0, -4*q + i + j >= 0)");
7841e7c464dSPrashant Kumar // Convert `q` and `k` to local variables.
785d95140a5SGroverkss poly.convertToLocal(VarKind::SetDim, 2, 4);
7861e7c464dSPrashant Kumar
7871e7c464dSPrashant Kumar std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 0, 1},
788e9fa1863SArjun P {1, 1, 0, 0, 0}};
7896d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {4, 3};
7901e7c464dSPrashant Kumar
7911e7c464dSPrashant Kumar checkDivisionRepresentation(poly, divisions, denoms);
7921e7c464dSPrashant Kumar }
7931e7c464dSPrashant Kumar }
7941e7c464dSPrashant Kumar
TEST(IntegerPolyhedronTest,computeLocalReprNoRepr)79549b754b5SGroverkss TEST(IntegerPolyhedronTest, computeLocalReprNoRepr) {
79649b754b5SGroverkss IntegerPolyhedron poly =
7978c867f78SGroverkss parseIntegerPolyhedron("(x, q) : (x - 3 * q >= 0, -x + 3 * q + 3 >= 0)");
79849b754b5SGroverkss // Convert q to a local variable.
799d95140a5SGroverkss poly.convertToLocal(VarKind::SetDim, 1, 2);
80049b754b5SGroverkss
80149b754b5SGroverkss std::vector<SmallVector<int64_t, 8>> divisions = {{0, 0, 0}};
8026d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {0};
80349b754b5SGroverkss
80449b754b5SGroverkss // Check that no division is computed.
80549b754b5SGroverkss checkDivisionRepresentation(poly, divisions, denoms);
80649b754b5SGroverkss }
80749b754b5SGroverkss
TEST(IntegerPolyhedronTest,computeLocalReprNegConstNormalize)808855cd847SArjun P TEST(IntegerPolyhedronTest, computeLocalReprNegConstNormalize) {
8098c867f78SGroverkss IntegerPolyhedron poly = parseIntegerPolyhedron(
8108c867f78SGroverkss "(x, q) : (-1 - 3*x - 6 * q >= 0, 6 + 3*x + 6*q >= 0)");
811855cd847SArjun P // Convert q to a local variable.
812d95140a5SGroverkss poly.convertToLocal(VarKind::SetDim, 1, 2);
813855cd847SArjun P
814855cd847SArjun P // q = floor((-1/3 - x)/2)
815855cd847SArjun P // = floor((1/3) + (-1 - x)/2)
816855cd847SArjun P // = floor((-1 - x)/2).
817855cd847SArjun P std::vector<SmallVector<int64_t, 8>> divisions = {{-1, 0, -1}};
8186d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {2};
819855cd847SArjun P checkDivisionRepresentation(poly, divisions, denoms);
820855cd847SArjun P }
821855cd847SArjun P
TEST(IntegerPolyhedronTest,simplifyLocalsTest)82249b754b5SGroverkss TEST(IntegerPolyhedronTest, simplifyLocalsTest) {
82349b754b5SGroverkss // (x) : (exists y: 2x + y = 1 and y = 2).
824a5a598beSGroverkss IntegerPolyhedron poly(PresburgerSpace::getSetSpace(1, 0, 1));
82549b754b5SGroverkss poly.addEquality({2, 1, -1});
82649b754b5SGroverkss poly.addEquality({0, 1, -2});
82749b754b5SGroverkss
82849b754b5SGroverkss EXPECT_TRUE(poly.isEmpty());
82949b754b5SGroverkss
83049b754b5SGroverkss // (x) : (exists y, z, w: 3x + y = 1 and 2y = z and 3y = w and z = w).
831a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1, 0, 3));
83249b754b5SGroverkss poly2.addEquality({3, 1, 0, 0, -1});
83349b754b5SGroverkss poly2.addEquality({0, 2, -1, 0, 0});
83449b754b5SGroverkss poly2.addEquality({0, 3, 0, -1, 0});
83549b754b5SGroverkss poly2.addEquality({0, 0, 1, -1, 0});
83649b754b5SGroverkss
83749b754b5SGroverkss EXPECT_TRUE(poly2.isEmpty());
83849b754b5SGroverkss
83949b754b5SGroverkss // (x) : (exists y: x >= y + 1 and 2x + y = 0 and y >= -1).
840a5a598beSGroverkss IntegerPolyhedron poly3(PresburgerSpace::getSetSpace(1, 0, 1));
84149b754b5SGroverkss poly3.addInequality({1, -1, -1});
84249b754b5SGroverkss poly3.addInequality({0, 1, 1});
84349b754b5SGroverkss poly3.addEquality({2, 1, 0});
84449b754b5SGroverkss
84549b754b5SGroverkss EXPECT_TRUE(poly3.isEmpty());
84649b754b5SGroverkss }
84749b754b5SGroverkss
TEST(IntegerPolyhedronTest,mergeDivisionsSimple)84849b754b5SGroverkss TEST(IntegerPolyhedronTest, mergeDivisionsSimple) {
84949b754b5SGroverkss {
85049b754b5SGroverkss // (x) : (exists z, y = [x / 2] : x = 3y and x + z + 1 >= 0).
851a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1, 0, 1));
85249b754b5SGroverkss poly1.addLocalFloorDiv({1, 0, 0}, 2); // y = [x / 2].
85349b754b5SGroverkss poly1.addEquality({1, 0, -3, 0}); // x = 3y.
85449b754b5SGroverkss poly1.addInequality({1, 1, 0, 1}); // x + z + 1 >= 0.
85549b754b5SGroverkss
85649b754b5SGroverkss // (x) : (exists y = [x / 2], z : x = 5y).
857a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
85849b754b5SGroverkss poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
85949b754b5SGroverkss poly2.addEquality({1, -5, 0}); // x = 5y.
860d95140a5SGroverkss poly2.appendVar(VarKind::Local); // Add local id z.
86149b754b5SGroverkss
862d95140a5SGroverkss poly1.mergeLocalVars(poly2);
86349b754b5SGroverkss
86449b754b5SGroverkss // Local space should be same.
865d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
86649b754b5SGroverkss
86749b754b5SGroverkss // 1 division should be matched + 2 unmatched local ids.
868d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 3u);
869d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 3u);
87049b754b5SGroverkss }
87149b754b5SGroverkss
87249b754b5SGroverkss {
87349b754b5SGroverkss // (x) : (exists z = [x / 5], y = [x / 2] : x = 3y).
874a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
87549b754b5SGroverkss poly1.addLocalFloorDiv({1, 0}, 5); // z = [x / 5].
87649b754b5SGroverkss poly1.addLocalFloorDiv({1, 0, 0}, 2); // y = [x / 2].
87749b754b5SGroverkss poly1.addEquality({1, 0, -3, 0}); // x = 3y.
87849b754b5SGroverkss
87949b754b5SGroverkss // (x) : (exists y = [x / 2], z = [x / 5]: x = 5z).
880a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
88149b754b5SGroverkss poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
88249b754b5SGroverkss poly2.addLocalFloorDiv({1, 0, 0}, 5); // z = [x / 5].
88349b754b5SGroverkss poly2.addEquality({1, 0, -5, 0}); // x = 5z.
88449b754b5SGroverkss
885d95140a5SGroverkss poly1.mergeLocalVars(poly2);
88649b754b5SGroverkss
88749b754b5SGroverkss // Local space should be same.
888d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
88949b754b5SGroverkss
89049b754b5SGroverkss // 2 divisions should be matched.
891d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 2u);
892d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 2u);
89349b754b5SGroverkss }
89449b754b5SGroverkss
89549b754b5SGroverkss {
89649b754b5SGroverkss // Division Normalization test.
89749b754b5SGroverkss // (x) : (exists z, y = [x / 2] : x = 3y and x + z + 1 >= 0).
898a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1, 0, 1));
89949b754b5SGroverkss // This division would be normalized.
90049b754b5SGroverkss poly1.addLocalFloorDiv({3, 0, 0}, 6); // y = [3x / 6] -> [x/2].
90149b754b5SGroverkss poly1.addEquality({1, 0, -3, 0}); // x = 3z.
90249b754b5SGroverkss poly1.addInequality({1, 1, 0, 1}); // x + y + 1 >= 0.
90349b754b5SGroverkss
90449b754b5SGroverkss // (x) : (exists y = [x / 2], z : x = 5y).
905a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
90649b754b5SGroverkss poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
90749b754b5SGroverkss poly2.addEquality({1, -5, 0}); // x = 5y.
908d95140a5SGroverkss poly2.appendVar(VarKind::Local); // Add local id z.
90949b754b5SGroverkss
910d95140a5SGroverkss poly1.mergeLocalVars(poly2);
91149b754b5SGroverkss
91249b754b5SGroverkss // Local space should be same.
913d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
91449b754b5SGroverkss
91549b754b5SGroverkss // One division should be matched + 2 unmatched local ids.
916d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 3u);
917d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 3u);
91849b754b5SGroverkss }
91949b754b5SGroverkss }
92049b754b5SGroverkss
TEST(IntegerPolyhedronTest,mergeDivisionsNestedDivsions)92149b754b5SGroverkss TEST(IntegerPolyhedronTest, mergeDivisionsNestedDivsions) {
92249b754b5SGroverkss {
92349b754b5SGroverkss // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z >= x).
924a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
92549b754b5SGroverkss poly1.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
92649b754b5SGroverkss poly1.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3].
92749b754b5SGroverkss poly1.addInequality({-1, 1, 1, 0}); // y + z >= x.
92849b754b5SGroverkss
92949b754b5SGroverkss // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z <= x).
930a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
93149b754b5SGroverkss poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
93249b754b5SGroverkss poly2.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3].
93349b754b5SGroverkss poly2.addInequality({1, -1, -1, 0}); // y + z <= x.
93449b754b5SGroverkss
935d95140a5SGroverkss poly1.mergeLocalVars(poly2);
93649b754b5SGroverkss
93749b754b5SGroverkss // Local space should be same.
938d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
93949b754b5SGroverkss
94049b754b5SGroverkss // 2 divisions should be matched.
941d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 2u);
942d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 2u);
94349b754b5SGroverkss }
94449b754b5SGroverkss
94549b754b5SGroverkss {
94649b754b5SGroverkss // (x) : (exists y = [x / 2], z = [x + y / 3], w = [z + 1 / 5]: y + z >= x).
947a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
94849b754b5SGroverkss poly1.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
94949b754b5SGroverkss poly1.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3].
95049b754b5SGroverkss poly1.addLocalFloorDiv({0, 0, 1, 1}, 5); // w = [z + 1 / 5].
95149b754b5SGroverkss poly1.addInequality({-1, 1, 1, 0, 0}); // y + z >= x.
95249b754b5SGroverkss
95349b754b5SGroverkss // (x) : (exists y = [x / 2], z = [x + y / 3], w = [z + 1 / 5]: y + z <= x).
954a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
95549b754b5SGroverkss poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
95649b754b5SGroverkss poly2.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3].
95749b754b5SGroverkss poly2.addLocalFloorDiv({0, 0, 1, 1}, 5); // w = [z + 1 / 5].
95849b754b5SGroverkss poly2.addInequality({1, -1, -1, 0, 0}); // y + z <= x.
95949b754b5SGroverkss
960d95140a5SGroverkss poly1.mergeLocalVars(poly2);
96149b754b5SGroverkss
96249b754b5SGroverkss // Local space should be same.
963d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
96449b754b5SGroverkss
96549b754b5SGroverkss // 3 divisions should be matched.
966d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 3u);
967d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 3u);
96849b754b5SGroverkss }
96949b754b5SGroverkss {
97049b754b5SGroverkss // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z >= x).
971a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
97249b754b5SGroverkss poly1.addLocalFloorDiv({2, 0}, 4); // y = [2x / 4] -> [x / 2].
97349b754b5SGroverkss poly1.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3].
97449b754b5SGroverkss poly1.addInequality({-1, 1, 1, 0}); // y + z >= x.
97549b754b5SGroverkss
97649b754b5SGroverkss // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z <= x).
977a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
97849b754b5SGroverkss poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2].
97949b754b5SGroverkss // This division would be normalized.
98049b754b5SGroverkss poly2.addLocalFloorDiv({3, 3, 0}, 9); // z = [3x + 3y / 9] -> [x + y / 3].
98149b754b5SGroverkss poly2.addInequality({1, -1, -1, 0}); // y + z <= x.
98249b754b5SGroverkss
983d95140a5SGroverkss poly1.mergeLocalVars(poly2);
98449b754b5SGroverkss
98549b754b5SGroverkss // Local space should be same.
986d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
98749b754b5SGroverkss
98849b754b5SGroverkss // 2 divisions should be matched.
989d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 2u);
990d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 2u);
99149b754b5SGroverkss }
99249b754b5SGroverkss }
99349b754b5SGroverkss
TEST(IntegerPolyhedronTest,mergeDivisionsConstants)99449b754b5SGroverkss TEST(IntegerPolyhedronTest, mergeDivisionsConstants) {
99549b754b5SGroverkss {
99649b754b5SGroverkss // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z >= x).
997a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
99849b754b5SGroverkss poly1.addLocalFloorDiv({1, 1}, 2); // y = [x + 1 / 2].
99949b754b5SGroverkss poly1.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3].
100049b754b5SGroverkss poly1.addInequality({-1, 1, 1, 0}); // y + z >= x.
100149b754b5SGroverkss
100249b754b5SGroverkss // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x).
1003a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
100449b754b5SGroverkss poly2.addLocalFloorDiv({1, 1}, 2); // y = [x + 1 / 2].
100549b754b5SGroverkss poly2.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3].
100649b754b5SGroverkss poly2.addInequality({1, -1, -1, 0}); // y + z <= x.
100749b754b5SGroverkss
1008d95140a5SGroverkss poly1.mergeLocalVars(poly2);
100949b754b5SGroverkss
101049b754b5SGroverkss // Local space should be same.
1011d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
101249b754b5SGroverkss
101349b754b5SGroverkss // 2 divisions should be matched.
1014d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 2u);
1015d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 2u);
101649b754b5SGroverkss }
101749b754b5SGroverkss {
101849b754b5SGroverkss // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z >= x).
1019a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
102049b754b5SGroverkss poly1.addLocalFloorDiv({1, 1}, 2); // y = [x + 1 / 2].
102149b754b5SGroverkss // Normalization test.
102249b754b5SGroverkss poly1.addLocalFloorDiv({3, 0, 6}, 9); // z = [3x + 6 / 9] -> [x + 2 / 3].
102349b754b5SGroverkss poly1.addInequality({-1, 1, 1, 0}); // y + z >= x.
102449b754b5SGroverkss
102549b754b5SGroverkss // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x).
1026a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
102749b754b5SGroverkss // Normalization test.
102849b754b5SGroverkss poly2.addLocalFloorDiv({2, 2}, 4); // y = [2x + 2 / 4] -> [x + 1 / 2].
102949b754b5SGroverkss poly2.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3].
103049b754b5SGroverkss poly2.addInequality({1, -1, -1, 0}); // y + z <= x.
103149b754b5SGroverkss
1032d95140a5SGroverkss poly1.mergeLocalVars(poly2);
103349b754b5SGroverkss
103449b754b5SGroverkss // Local space should be same.
1035d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
103649b754b5SGroverkss
103749b754b5SGroverkss // 2 divisions should be matched.
1038d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 2u);
1039d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 2u);
104049b754b5SGroverkss }
104149b754b5SGroverkss }
104249b754b5SGroverkss
TEST(IntegerPolyhedronTest,mergeDivisionsDuplicateInSameSet)10434ffd0b6fSGroverkss TEST(IntegerPolyhedronTest, mergeDivisionsDuplicateInSameSet) {
10444ffd0b6fSGroverkss // (x) : (exists y = [x + 1 / 3], z = [x + 1 / 3]: y + z >= x).
10454ffd0b6fSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
10464ffd0b6fSGroverkss poly1.addLocalFloorDiv({1, 1}, 3); // y = [x + 1 / 2].
10474ffd0b6fSGroverkss poly1.addLocalFloorDiv({1, 0, 1}, 3); // z = [x + 1 / 3].
10484ffd0b6fSGroverkss poly1.addInequality({-1, 1, 1, 0}); // y + z >= x.
10494ffd0b6fSGroverkss
10504ffd0b6fSGroverkss // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x).
10514ffd0b6fSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
10524ffd0b6fSGroverkss poly2.addLocalFloorDiv({1, 1}, 3); // y = [x + 1 / 3].
10534ffd0b6fSGroverkss poly2.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3].
10544ffd0b6fSGroverkss poly2.addInequality({1, -1, -1, 0}); // y + z <= x.
10554ffd0b6fSGroverkss
1056d95140a5SGroverkss poly1.mergeLocalVars(poly2);
10574ffd0b6fSGroverkss
10584ffd0b6fSGroverkss // Local space should be same.
1059d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars());
10604ffd0b6fSGroverkss
10614ffd0b6fSGroverkss // 1 divisions should be matched.
1062d95140a5SGroverkss EXPECT_EQ(poly1.getNumLocalVars(), 3u);
1063d95140a5SGroverkss EXPECT_EQ(poly2.getNumLocalVars(), 3u);
10644ffd0b6fSGroverkss }
10654ffd0b6fSGroverkss
TEST(IntegerPolyhedronTest,negativeDividends)1066b6098c07SPrashant Kumar TEST(IntegerPolyhedronTest, negativeDividends) {
1067b6098c07SPrashant Kumar // (x) : (exists y = [-x + 1 / 2], z = [-x - 2 / 3]: y + z >= x).
1068a5a598beSGroverkss IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1));
1069b6098c07SPrashant Kumar poly1.addLocalFloorDiv({-1, 1}, 2); // y = [x + 1 / 2].
1070b6098c07SPrashant Kumar // Normalization test with negative dividends
1071b6098c07SPrashant Kumar poly1.addLocalFloorDiv({-3, 0, -6}, 9); // z = [3x + 6 / 9] -> [x + 2 / 3].
1072b6098c07SPrashant Kumar poly1.addInequality({-1, 1, 1, 0}); // y + z >= x.
1073b6098c07SPrashant Kumar
1074b6098c07SPrashant Kumar // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x).
1075a5a598beSGroverkss IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1));
1076b6098c07SPrashant Kumar // Normalization test.
1077b6098c07SPrashant Kumar poly2.addLocalFloorDiv({-2, 2}, 4); // y = [-2x + 2 / 4] -> [-x + 1 / 2].
1078b6098c07SPrashant Kumar poly2.addLocalFloorDiv({-1, 0, -2}, 3); // z = [-x - 2 / 3].
1079b6098c07SPrashant Kumar poly2.addInequality({1, -1, -1, 0}); // y + z <= x.
1080b6098c07SPrashant Kumar
1081d95140a5SGroverkss poly1.mergeLocalVars(poly2);
1082b6098c07SPrashant Kumar
1083b6098c07SPrashant Kumar // Merging triggers normalization.
1084b6098c07SPrashant Kumar std::vector<SmallVector<int64_t, 8>> divisions = {{-1, 0, 0, 1},
1085b6098c07SPrashant Kumar {-1, 0, 0, -2}};
10866d6f6c4dSArjun P SmallVector<int64_t, 8> denoms = {2, 3};
1087b6098c07SPrashant Kumar checkDivisionRepresentation(poly1, divisions, denoms);
1088b6098c07SPrashant Kumar }
1089b6098c07SPrashant Kumar
expectRationalLexMin(const IntegerPolyhedron & poly,ArrayRef<Fraction> min)10906db01958SArjun P void expectRationalLexMin(const IntegerPolyhedron &poly,
10916db01958SArjun P ArrayRef<Fraction> min) {
1092cfd6ba89SArjun P auto lexMin = poly.findRationalLexMin();
10938e799588SArjun P ASSERT_TRUE(lexMin.isBounded());
10946db01958SArjun P EXPECT_EQ(ArrayRef<Fraction>(*lexMin), min);
10956db01958SArjun P }
10966db01958SArjun P
expectNoRationalLexMin(OptimumKind kind,const IntegerPolyhedron & poly)10978e799588SArjun P void expectNoRationalLexMin(OptimumKind kind, const IntegerPolyhedron &poly) {
10988e799588SArjun P ASSERT_NE(kind, OptimumKind::Bounded)
10998e799588SArjun P << "Use expectRationalLexMin for bounded min";
1100cfd6ba89SArjun P EXPECT_EQ(poly.findRationalLexMin().getKind(), kind);
11016db01958SArjun P }
11026db01958SArjun P
TEST(IntegerPolyhedronTest,findRationalLexMin)11036761dd7dSArjun P TEST(IntegerPolyhedronTest, findRationalLexMin) {
11046db01958SArjun P expectRationalLexMin(
11058c867f78SGroverkss parseIntegerPolyhedron(
11068c867f78SGroverkss "(x, y, z) : (x + 10 >= 0, y + 40 >= 0, z + 30 >= 0)"),
11076db01958SArjun P {{-10, 1}, {-40, 1}, {-30, 1}});
11086db01958SArjun P expectRationalLexMin(
11098c867f78SGroverkss parseIntegerPolyhedron(
11104b86d559SArjun P "(x, y, z) : (2*x + 7 >= 0, 3*y - 5 >= 0, 8*z + 10 >= 0, 9*z >= 0)"),
11116db01958SArjun P {{-7, 2}, {5, 3}, {0, 1}});
11128c867f78SGroverkss expectRationalLexMin(
11138c867f78SGroverkss parseIntegerPolyhedron("(x, y) : (3*x + 2*y + 10 >= 0, -3*y + 10 >= "
11144b86d559SArjun P "0, 4*x - 7*y - 10 >= 0)"),
11156db01958SArjun P {{-50, 29}, {-70, 29}});
11166db01958SArjun P
11176db01958SArjun P // Test with some locals. This is basically x >= 11, 0 <= x - 2e <= 1.
11186db01958SArjun P // It'll just choose x = 11, e = 5.5 since it's rational lexmin.
11196db01958SArjun P expectRationalLexMin(
11208c867f78SGroverkss parseIntegerPolyhedron(
11214b86d559SArjun P "(x, y) : (x - 2*(x floordiv 2) == 0, y - 2*x >= 0, x - 11 >= 0)"),
11226db01958SArjun P {{11, 1}, {22, 1}});
11236db01958SArjun P
11248c867f78SGroverkss expectRationalLexMin(
11258c867f78SGroverkss parseIntegerPolyhedron("(x, y) : (3*x + 2*y + 10 >= 0,"
11264b86d559SArjun P "-4*x + 7*y + 10 >= 0, -3*y + 10 >= 0)"),
11276db01958SArjun P {{-50, 9}, {10, 3}});
11286db01958SArjun P
11296db01958SArjun P // Cartesian product of above with itself.
11306db01958SArjun P expectRationalLexMin(
11318c867f78SGroverkss parseIntegerPolyhedron(
11328c867f78SGroverkss "(x, y, z, w) : (3*x + 2*y + 10 >= 0, -4*x + 7*y + 10 >= 0,"
11336db01958SArjun P "-3*y + 10 >= 0, 3*z + 2*w + 10 >= 0, -4*z + 7*w + 10 >= 0,"
11344b86d559SArjun P "-3*w + 10 >= 0)"),
11356db01958SArjun P {{-50, 9}, {10, 3}, {-50, 9}, {10, 3}});
11366db01958SArjun P
11376db01958SArjun P // Same as above but for the constraints on z and w, we express "10" in terms
11386db01958SArjun P // of x and y. We know that x and y still have to take the values
11396db01958SArjun P // -50/9 and 10/3 since their constraints are the same and their values are
11406db01958SArjun P // minimized first. Accordingly, the values -9x - 12y, -9x - 0y - 10,
11416db01958SArjun P // and -9x - 15y + 10 are all equal to 10.
11426db01958SArjun P expectRationalLexMin(
11438c867f78SGroverkss parseIntegerPolyhedron(
11446db01958SArjun P "(x, y, z, w) : (3*x + 2*y + 10 >= 0, -4*x + 7*y + 10 >= 0, "
11456db01958SArjun P "-3*y + 10 >= 0, 3*z + 2*w - 9*x - 12*y >= 0,"
11464b86d559SArjun P "-4*z + 7*w + - 9*x - 9*y - 10 >= 0, -3*w - 9*x - 15*y + 10 >= 0)"),
11476db01958SArjun P {{-50, 9}, {10, 3}, {-50, 9}, {10, 3}});
11486db01958SArjun P
11496db01958SArjun P // Same as above with one constraint removed, making the lexmin unbounded.
11506db01958SArjun P expectNoRationalLexMin(
11518e799588SArjun P OptimumKind::Unbounded,
11528c867f78SGroverkss parseIntegerPolyhedron(
11538c867f78SGroverkss "(x, y, z, w) : (3*x + 2*y + 10 >= 0, -4*x + 7*y + 10 >= 0,"
11546db01958SArjun P "-3*y + 10 >= 0, 3*z + 2*w - 9*x - 12*y >= 0,"
11554b86d559SArjun P "-4*z + 7*w + - 9*x - 9*y - 10>= 0)"));
11566db01958SArjun P
11576db01958SArjun P // Again, the lexmin is unbounded.
11586db01958SArjun P expectNoRationalLexMin(
11598e799588SArjun P OptimumKind::Unbounded,
11608c867f78SGroverkss parseIntegerPolyhedron(
11618c867f78SGroverkss "(x, y, z) : (2*x + 5*y + 8*z - 10 >= 0,"
11624b86d559SArjun P "2*x + 10*y + 8*z - 10 >= 0, 2*x + 5*y + 10*z - 10 >= 0)"));
11636db01958SArjun P
11646db01958SArjun P // The set is empty.
11658c867f78SGroverkss expectNoRationalLexMin(
11668c867f78SGroverkss OptimumKind::Empty,
11678c867f78SGroverkss parseIntegerPolyhedron("(x) : (2*x >= 0, -x - 1 >= 0)"));
11686db01958SArjun P }
11696db01958SArjun P
expectIntegerLexMin(const IntegerPolyhedron & poly,ArrayRef<int64_t> min)11709f8cb685SArjun P void expectIntegerLexMin(const IntegerPolyhedron &poly, ArrayRef<int64_t> min) {
1171*1a0e67d7SRamkumar Ramachandra MaybeOptimum<SmallVector<DynamicAPInt, 8>> lexMin = poly.findIntegerLexMin();
11729f8cb685SArjun P ASSERT_TRUE(lexMin.isBounded());
1173*1a0e67d7SRamkumar Ramachandra EXPECT_EQ(*lexMin, getDynamicAPIntVec(min));
11749f8cb685SArjun P }
11759f8cb685SArjun P
expectNoIntegerLexMin(OptimumKind kind,const IntegerPolyhedron & poly)11769f8cb685SArjun P void expectNoIntegerLexMin(OptimumKind kind, const IntegerPolyhedron &poly) {
11779f8cb685SArjun P ASSERT_NE(kind, OptimumKind::Bounded)
11789f8cb685SArjun P << "Use expectRationalLexMin for bounded min";
1179cfd6ba89SArjun P EXPECT_EQ(poly.findRationalLexMin().getKind(), kind);
11809f8cb685SArjun P }
11819f8cb685SArjun P
TEST(IntegerPolyhedronTest,findIntegerLexMin)11826761dd7dSArjun P TEST(IntegerPolyhedronTest, findIntegerLexMin) {
11838c867f78SGroverkss expectIntegerLexMin(
11848c867f78SGroverkss parseIntegerPolyhedron("(x, y, z) : (2*x + 13 >= 0, 4*y - 3*x - 2 >= "
11854b86d559SArjun P "0, 11*z + 5*y - 3*x + 7 >= 0)"),
11869f8cb685SArjun P {-6, -4, 0});
11879f8cb685SArjun P // Similar to above but no lower bound on z.
11888c867f78SGroverkss expectNoIntegerLexMin(
11898c867f78SGroverkss OptimumKind::Unbounded,
11908c867f78SGroverkss parseIntegerPolyhedron("(x, y, z) : (2*x + 13 >= 0, 4*y - 3*x - 2 "
11914b86d559SArjun P ">= 0, -11*z + 5*y - 3*x + 7 >= 0)"));
11929f8cb685SArjun P }
11939f8cb685SArjun P
expectSymbolicIntegerLexMin(StringRef polyStr,ArrayRef<std::pair<StringRef,StringRef>> expectedLexminRepr,ArrayRef<StringRef> expectedUnboundedDomainRepr)119479ad5fb2SArjun P void expectSymbolicIntegerLexMin(
119579ad5fb2SArjun P StringRef polyStr,
11968c867f78SGroverkss ArrayRef<std::pair<StringRef, StringRef>> expectedLexminRepr,
119779ad5fb2SArjun P ArrayRef<StringRef> expectedUnboundedDomainRepr) {
11988c867f78SGroverkss IntegerPolyhedron poly = parseIntegerPolyhedron(polyStr);
119979ad5fb2SArjun P
1200d95140a5SGroverkss ASSERT_NE(poly.getNumDimVars(), 0u);
1201d95140a5SGroverkss ASSERT_NE(poly.getNumSymbolVars(), 0u);
120279ad5fb2SArjun P
120356863adfSiambrj SymbolicLexOpt result = poly.findSymbolicIntegerLexMin();
120479ad5fb2SArjun P
12058c867f78SGroverkss if (expectedLexminRepr.empty()) {
120656863adfSiambrj EXPECT_TRUE(result.lexopt.getDomain().isIntegerEmpty());
12078c867f78SGroverkss } else {
12088c867f78SGroverkss PWMAFunction expectedLexmin = parsePWMAF(expectedLexminRepr);
120956863adfSiambrj EXPECT_TRUE(result.lexopt.isEqual(expectedLexmin));
121079ad5fb2SArjun P }
121179ad5fb2SArjun P
12128c867f78SGroverkss if (expectedUnboundedDomainRepr.empty()) {
12138c867f78SGroverkss EXPECT_TRUE(result.unboundedDomain.isIntegerEmpty());
12148c867f78SGroverkss } else {
12158c867f78SGroverkss PresburgerSet expectedUnboundedDomain =
12168c867f78SGroverkss parsePresburgerSet(expectedUnboundedDomainRepr);
121779ad5fb2SArjun P EXPECT_TRUE(result.unboundedDomain.isEqual(expectedUnboundedDomain));
12188c867f78SGroverkss }
121979ad5fb2SArjun P }
122079ad5fb2SArjun P
expectSymbolicIntegerLexMin(StringRef polyStr,ArrayRef<std::pair<StringRef,StringRef>> result)122179ad5fb2SArjun P void expectSymbolicIntegerLexMin(
12228c867f78SGroverkss StringRef polyStr, ArrayRef<std::pair<StringRef, StringRef>> result) {
122379ad5fb2SArjun P expectSymbolicIntegerLexMin(polyStr, result, {});
122479ad5fb2SArjun P }
122579ad5fb2SArjun P
TEST(IntegerPolyhedronTest,findSymbolicIntegerLexMin)122679ad5fb2SArjun P TEST(IntegerPolyhedronTest, findSymbolicIntegerLexMin) {
122779ad5fb2SArjun P expectSymbolicIntegerLexMin("(x)[a] : (x - a >= 0)",
122879ad5fb2SArjun P {
12298c867f78SGroverkss {"()[a] : ()", "()[a] -> (a)"},
123079ad5fb2SArjun P });
123179ad5fb2SArjun P
123279ad5fb2SArjun P expectSymbolicIntegerLexMin(
123379ad5fb2SArjun P "(x)[a, b] : (x - a >= 0, x - b >= 0)",
123479ad5fb2SArjun P {
12358c867f78SGroverkss {"()[a, b] : (a - b >= 0)", "()[a, b] -> (a)"},
12368c867f78SGroverkss {"()[a, b] : (b - a - 1 >= 0)", "()[a, b] -> (b)"},
123779ad5fb2SArjun P });
123879ad5fb2SArjun P
123979ad5fb2SArjun P expectSymbolicIntegerLexMin(
124079ad5fb2SArjun P "(x)[a, b, c] : (x -a >= 0, x - b >= 0, x - c >= 0)",
124179ad5fb2SArjun P {
12428c867f78SGroverkss {"()[a, b, c] : (a - b >= 0, a - c >= 0)", "()[a, b, c] -> (a)"},
12438c867f78SGroverkss {"()[a, b, c] : (b - a - 1 >= 0, b - c >= 0)", "()[a, b, c] -> (b)"},
1244c4abef28SArjun P {"()[a, b, c] : (c - a - 1 >= 0, c - b - 1 >= 0)",
12458c867f78SGroverkss "()[a, b, c] -> (c)"},
124679ad5fb2SArjun P });
124779ad5fb2SArjun P
124879ad5fb2SArjun P expectSymbolicIntegerLexMin("(x, y)[a] : (x - a >= 0, x + y >= 0)",
124979ad5fb2SArjun P {
12508c867f78SGroverkss {"()[a] : ()", "()[a] -> (a, -a)"},
125179ad5fb2SArjun P });
125279ad5fb2SArjun P
12538c867f78SGroverkss expectSymbolicIntegerLexMin("(x, y)[a] : (x - a >= 0, x + y >= 0, y >= 0)",
125479ad5fb2SArjun P {
12558c867f78SGroverkss {"()[a] : (a >= 0)", "()[a] -> (a, 0)"},
12568c867f78SGroverkss {"()[a] : (-a - 1 >= 0)", "()[a] -> (a, -a)"},
125779ad5fb2SArjun P });
125879ad5fb2SArjun P
125979ad5fb2SArjun P expectSymbolicIntegerLexMin(
126079ad5fb2SArjun P "(x, y)[a, b, c] : (x - a >= 0, y - b >= 0, c - x - y >= 0)",
126179ad5fb2SArjun P {
12628c867f78SGroverkss {"()[a, b, c] : (c - a - b >= 0)", "()[a, b, c] -> (a, b)"},
126379ad5fb2SArjun P });
126479ad5fb2SArjun P
126579ad5fb2SArjun P expectSymbolicIntegerLexMin(
126679ad5fb2SArjun P "(x, y, z)[a, b, c] : (c - z >= 0, b - y >= 0, x + y + z - a == 0)",
126779ad5fb2SArjun P {
12688c867f78SGroverkss {"()[a, b, c] : ()", "()[a, b, c] -> (a - b - c, b, c)"},
126979ad5fb2SArjun P });
127079ad5fb2SArjun P
127179ad5fb2SArjun P expectSymbolicIntegerLexMin(
127279ad5fb2SArjun P "(x)[a, b] : (a >= 0, b >= 0, x >= 0, a + b + x - 1 >= 0)",
127379ad5fb2SArjun P {
12748c867f78SGroverkss {"()[a, b] : (a >= 0, b >= 0, a + b - 1 >= 0)", "()[a, b] -> (0)"},
12758c867f78SGroverkss {"()[a, b] : (a == 0, b == 0)", "()[a, b] -> (1)"},
127679ad5fb2SArjun P });
127779ad5fb2SArjun P
127879ad5fb2SArjun P expectSymbolicIntegerLexMin(
127979ad5fb2SArjun P "(x)[a, b] : (1 - a >= 0, a >= 0, 1 - b >= 0, b >= 0, 1 - x >= 0, x >= "
128079ad5fb2SArjun P "0, a + b + x - 1 >= 0)",
128179ad5fb2SArjun P {
1282c4abef28SArjun P {"()[a, b] : (1 - a >= 0, a >= 0, 1 - b >= 0, b >= 0, a + b - 1 >= "
1283c4abef28SArjun P "0)",
12848c867f78SGroverkss "()[a, b] -> (0)"},
12858c867f78SGroverkss {"()[a, b] : (a == 0, b == 0)", "()[a, b] -> (1)"},
128679ad5fb2SArjun P });
128779ad5fb2SArjun P
128879ad5fb2SArjun P expectSymbolicIntegerLexMin(
128979ad5fb2SArjun P "(x, y, z)[a, b] : (x - a == 0, y - b == 0, x >= 0, y >= 0, z >= 0, x + "
129079ad5fb2SArjun P "y + z - 1 >= 0)",
129179ad5fb2SArjun P {
1292c4abef28SArjun P {"()[a, b] : (a >= 0, b >= 0, 1 - a - b >= 0)",
12938c867f78SGroverkss "()[a, b] -> (a, b, 1 - a - b)"},
1294c4abef28SArjun P {"()[a, b] : (a >= 0, b >= 0, a + b - 2 >= 0)",
12958c867f78SGroverkss "()[a, b] -> (a, b, 0)"},
129679ad5fb2SArjun P });
129779ad5fb2SArjun P
12988c867f78SGroverkss expectSymbolicIntegerLexMin(
12998c867f78SGroverkss "(x)[a, b] : (x - a == 0, x - b >= 0)",
130079ad5fb2SArjun P {
13018c867f78SGroverkss {"()[a, b] : (a - b >= 0)", "()[a, b] -> (a)"},
130279ad5fb2SArjun P });
130379ad5fb2SArjun P
130479ad5fb2SArjun P expectSymbolicIntegerLexMin(
130579ad5fb2SArjun P "(q)[a] : (a - 1 - 3*q == 0, q >= 0)",
130679ad5fb2SArjun P {
1307c4abef28SArjun P {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)",
13088c867f78SGroverkss "()[a] -> (a floordiv 3)"},
130979ad5fb2SArjun P });
131079ad5fb2SArjun P
131179ad5fb2SArjun P expectSymbolicIntegerLexMin(
131279ad5fb2SArjun P "(r, q)[a] : (a - r - 3*q == 0, q >= 0, 1 - r >= 0, r >= 0)",
131379ad5fb2SArjun P {
1314c4abef28SArjun P {"()[a] : (a - 0 - 3*(a floordiv 3) == 0, a >= 0)",
13158c867f78SGroverkss "()[a] -> (0, a floordiv 3)"},
1316c4abef28SArjun P {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)",
13178c867f78SGroverkss "()[a] -> (1, a floordiv 3)"}, // (1 a floordiv 3)
131879ad5fb2SArjun P });
131979ad5fb2SArjun P
132079ad5fb2SArjun P expectSymbolicIntegerLexMin(
132179ad5fb2SArjun P "(r, q)[a] : (a - r - 3*q == 0, q >= 0, 2 - r >= 0, r - 1 >= 0)",
132279ad5fb2SArjun P {
1323c4abef28SArjun P {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)",
13248c867f78SGroverkss "()[a] -> (1, a floordiv 3)"},
1325c4abef28SArjun P {"()[a] : (a - 2 - 3*(a floordiv 3) == 0, a >= 0)",
13268c867f78SGroverkss "()[a] -> (2, a floordiv 3)"},
132779ad5fb2SArjun P });
132879ad5fb2SArjun P
132979ad5fb2SArjun P expectSymbolicIntegerLexMin(
133079ad5fb2SArjun P "(r, q)[a] : (a - r - 3*q == 0, q >= 0, r >= 0)",
133179ad5fb2SArjun P {
1332c4abef28SArjun P {"()[a] : (a - 3*(a floordiv 3) == 0, a >= 0)",
13338c867f78SGroverkss "()[a] -> (0, a floordiv 3)"},
1334c4abef28SArjun P {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)",
13358c867f78SGroverkss "()[a] -> (1, a floordiv 3)"},
1336c4abef28SArjun P {"()[a] : (a - 2 - 3*(a floordiv 3) == 0, a >= 0)",
13378c867f78SGroverkss "()[a] -> (2, a floordiv 3)"},
133879ad5fb2SArjun P });
133979ad5fb2SArjun P
134079ad5fb2SArjun P expectSymbolicIntegerLexMin(
134179ad5fb2SArjun P "(x, y, z, w)[g] : ("
134279ad5fb2SArjun P // x, y, z, w are boolean variables.
134379ad5fb2SArjun P "1 - x >= 0, x >= 0, 1 - y >= 0, y >= 0,"
134479ad5fb2SArjun P "1 - z >= 0, z >= 0, 1 - w >= 0, w >= 0,"
134579ad5fb2SArjun P // We have some constraints on them:
134679ad5fb2SArjun P "x + y + z - 1 >= 0," // x or y or z
134779ad5fb2SArjun P "x + y + w - 1 >= 0," // x or y or w
134879ad5fb2SArjun P "1 - x + 1 - y + 1 - w - 1 >= 0," // ~x or ~y or ~w
134979ad5fb2SArjun P // What's the lexmin solution using exactly g true vars?
135079ad5fb2SArjun P "g - x - y - z - w == 0)",
135179ad5fb2SArjun P {
13528c867f78SGroverkss {"()[g] : (g - 1 == 0)", "()[g] -> (0, 1, 0, 0)"},
13538c867f78SGroverkss {"()[g] : (g - 2 == 0)", "()[g] -> (0, 0, 1, 1)"},
13548c867f78SGroverkss {"()[g] : (g - 3 == 0)", "()[g] -> (0, 1, 1, 1)"},
135579ad5fb2SArjun P });
135679ad5fb2SArjun P
135779ad5fb2SArjun P // Bezout's lemma: if a, b are constants,
135879ad5fb2SArjun P // the set of values that ax + by can take is all multiples of gcd(a, b).
135979ad5fb2SArjun P expectSymbolicIntegerLexMin(
136079ad5fb2SArjun P // If (x, y) is a solution for a given [a, r], then so is (x - 5, y + 2).
136179ad5fb2SArjun P // So the lexmin is unbounded if it exists.
136279ad5fb2SArjun P "(x, y)[a, r] : (a >= 0, r - a + 14*x + 35*y == 0)", {},
136379ad5fb2SArjun P // According to Bezout's lemma, 14x + 35y can take on all multiples
136479ad5fb2SArjun P // of 7 and no other values. So the solution exists iff r - a is a
136579ad5fb2SArjun P // multiple of 7.
1366c4abef28SArjun P {"()[a, r] : (a >= 0, r - a - 7*((r - a) floordiv 7) == 0)"});
136779ad5fb2SArjun P
136879ad5fb2SArjun P // The lexmins are unbounded.
136979ad5fb2SArjun P expectSymbolicIntegerLexMin("(x, y)[a] : (9*x - 4*y - 2*a >= 0)", {},
1370c4abef28SArjun P {"()[a] : ()"});
137179ad5fb2SArjun P
137279ad5fb2SArjun P // Test cases adapted from isl.
137379ad5fb2SArjun P expectSymbolicIntegerLexMin(
137479ad5fb2SArjun P // a = 2b - 2(c - b), c - b >= 0.
137579ad5fb2SArjun P // So b is minimized when c = b.
137679ad5fb2SArjun P "(b, c)[a] : (a - 4*b + 2*c == 0, c - b >= 0)",
137779ad5fb2SArjun P {
1378c4abef28SArjun P {"()[a] : (a - 2*(a floordiv 2) == 0)",
13798c867f78SGroverkss "()[a] -> (a floordiv 2, a floordiv 2)"},
138079ad5fb2SArjun P });
138179ad5fb2SArjun P
138279ad5fb2SArjun P expectSymbolicIntegerLexMin(
138379ad5fb2SArjun P // 0 <= b <= 255, 1 <= a - 512b <= 509,
138479ad5fb2SArjun P // b + 8 >= 1 + 16*(b + 8 floordiv 16) // i.e. b % 16 != 8
138579ad5fb2SArjun P "(b)[a] : (255 - b >= 0, b >= 0, a - 512*b - 1 >= 0, 512*b -a + 509 >= "
138679ad5fb2SArjun P "0, b + 7 - 16*((8 + b) floordiv 16) >= 0)",
138779ad5fb2SArjun P {
1388c4abef28SArjun P {"()[a] : (255 - (a floordiv 512) >= 0, a >= 0, a - 512*(a floordiv "
138979ad5fb2SArjun P "512) - 1 >= 0, 512*(a floordiv 512) - a + 509 >= 0, (a floordiv "
139079ad5fb2SArjun P "512) + 7 - 16*((8 + (a floordiv 512)) floordiv 16) >= 0)",
13918c867f78SGroverkss "()[a] -> (a floordiv 512)"},
139279ad5fb2SArjun P });
139379ad5fb2SArjun P
139479ad5fb2SArjun P expectSymbolicIntegerLexMin(
139579ad5fb2SArjun P "(a, b)[K, N, x, y] : (N - K - 2 >= 0, K + 4 - N >= 0, x - 4 >= 0, x + 6 "
139679ad5fb2SArjun P "- 2*N >= 0, K+N - x - 1 >= 0, a - N + 1 >= 0, K+N-1-a >= 0,a + 6 - b - "
139779ad5fb2SArjun P "N >= 0, 2*N - 4 - a >= 0,"
139879ad5fb2SArjun P "2*N - 3*K + a - b >= 0, 4*N - K + 1 - 3*b >= 0, b - N >= 0, a - x - 1 "
139979ad5fb2SArjun P ">= 0)",
14008c867f78SGroverkss {
14018c867f78SGroverkss {"()[K, N, x, y] : (x + 6 - 2*N >= 0, 2*N - 5 - x >= 0, x + 1 -3*K + "
14028c867f78SGroverkss "N >= 0, N + K - 2 - x >= 0, x - 4 >= 0)",
14038c867f78SGroverkss "()[K, N, x, y] -> (1 + x, N)"},
14048c867f78SGroverkss });
140579ad5fb2SArjun P }
140679ad5fb2SArjun P
14071096fcffSArjun P static void
expectComputedVolumeIsValidOverapprox(const IntegerPolyhedron & poly,std::optional<int64_t> trueVolume,std::optional<int64_t> resultBound)14081096fcffSArjun P expectComputedVolumeIsValidOverapprox(const IntegerPolyhedron &poly,
14090a81ace0SKazu Hirata std::optional<int64_t> trueVolume,
14100a81ace0SKazu Hirata std::optional<int64_t> resultBound) {
14111096fcffSArjun P expectComputedVolumeIsValidOverapprox(poly.computeVolume(), trueVolume,
14121096fcffSArjun P resultBound);
14131096fcffSArjun P }
14141096fcffSArjun P
TEST(IntegerPolyhedronTest,computeVolume)14151096fcffSArjun P TEST(IntegerPolyhedronTest, computeVolume) {
14161096fcffSArjun P // 0 <= x <= 3 + 1/3, -5.5 <= y <= 2 + 3/5, 3 <= z <= 1.75.
14171096fcffSArjun P // i.e. 0 <= x <= 3, -5 <= y <= 2, 3 <= z <= 3 + 1/4.
14181096fcffSArjun P // So volume is 4 * 8 * 1 = 32.
14191096fcffSArjun P expectComputedVolumeIsValidOverapprox(
14208c867f78SGroverkss parseIntegerPolyhedron(
14218c867f78SGroverkss "(x, y, z) : (x >= 0, -3*x + 10 >= 0, 2*y + 11 >= 0,"
14224b86d559SArjun P "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)"),
14231096fcffSArjun P /*trueVolume=*/32ull, /*resultBound=*/32ull);
14241096fcffSArjun P
14251096fcffSArjun P // Same as above but y has bounds 2 + 1/5 <= y <= 2 + 3/5. So the volume is
14261096fcffSArjun P // zero.
14271096fcffSArjun P expectComputedVolumeIsValidOverapprox(
14288c867f78SGroverkss parseIntegerPolyhedron(
14298c867f78SGroverkss "(x, y, z) : (x >= 0, -3*x + 10 >= 0, 5*y - 11 >= 0,"
14304b86d559SArjun P "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)"),
14311096fcffSArjun P /*trueVolume=*/0ull, /*resultBound=*/0ull);
14321096fcffSArjun P
14331096fcffSArjun P // Now x is unbounded below but y still has no integer values.
14341096fcffSArjun P expectComputedVolumeIsValidOverapprox(
14358c867f78SGroverkss parseIntegerPolyhedron("(x, y, z) : (-3*x + 10 >= 0, 5*y - 11 >= 0,"
14364b86d559SArjun P "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)"),
14371096fcffSArjun P /*trueVolume=*/0ull, /*resultBound=*/0ull);
14381096fcffSArjun P
14391096fcffSArjun P // A diamond shape, 0 <= x + y <= 10, 0 <= x - y <= 10,
14401096fcffSArjun P // with vertices at (0, 0), (5, 5), (5, 5), (10, 0).
14411096fcffSArjun P // x and y can take 11 possible values so result computed is 11*11 = 121.
14421096fcffSArjun P expectComputedVolumeIsValidOverapprox(
14438c867f78SGroverkss parseIntegerPolyhedron(
14448c867f78SGroverkss "(x, y) : (x + y >= 0, -x - y + 10 >= 0, x - y >= 0,"
14454b86d559SArjun P "-x + y + 10 >= 0)"),
14461096fcffSArjun P /*trueVolume=*/61ull, /*resultBound=*/121ull);
14471096fcffSArjun P
14481096fcffSArjun P // Effectively the same diamond as above; constrain the variables to be even
14491096fcffSArjun P // and double the constant terms of the constraints. The algorithm can't
14501096fcffSArjun P // eliminate locals exactly, so the result is an overapproximation by
14511096fcffSArjun P // computing that x and y can take 21 possible values so result is 21*21 =
14521096fcffSArjun P // 441.
14531096fcffSArjun P expectComputedVolumeIsValidOverapprox(
14548c867f78SGroverkss parseIntegerPolyhedron(
14558c867f78SGroverkss "(x, y) : (x + y >= 0, -x - y + 20 >= 0, x - y >= 0,"
14561096fcffSArjun P " -x + y + 20 >= 0, x - 2*(x floordiv 2) == 0,"
14574b86d559SArjun P "y - 2*(y floordiv 2) == 0)"),
14581096fcffSArjun P /*trueVolume=*/61ull, /*resultBound=*/441ull);
14591096fcffSArjun P
14601096fcffSArjun P // Unbounded polytope.
14611096fcffSArjun P expectComputedVolumeIsValidOverapprox(
14628c867f78SGroverkss parseIntegerPolyhedron("(x, y) : (2*x - y >= 0, y - 3*x >= 0)"),
14631096fcffSArjun P /*trueVolume=*/{}, /*resultBound=*/{});
14641096fcffSArjun P }
14654418669fSArjun P
containsPointNoLocal(const IntegerPolyhedron & poly,ArrayRef<int64_t> point)14666d6f6c4dSArjun P bool containsPointNoLocal(const IntegerPolyhedron &poly,
14676d6f6c4dSArjun P ArrayRef<int64_t> point) {
1468*1a0e67d7SRamkumar Ramachandra return poly.containsPointNoLocal(getDynamicAPIntVec(point)).has_value();
14696d6f6c4dSArjun P }
14706d6f6c4dSArjun P
TEST(IntegerPolyhedronTest,containsPointNoLocal)14714418669fSArjun P TEST(IntegerPolyhedronTest, containsPointNoLocal) {
14728c867f78SGroverkss IntegerPolyhedron poly1 =
14738c867f78SGroverkss parseIntegerPolyhedron("(x) : ((x floordiv 2) - x == 0)");
14748c867f78SGroverkss EXPECT_TRUE(poly1.containsPointNoLocal({0}));
14758c867f78SGroverkss EXPECT_FALSE(poly1.containsPointNoLocal({1}));
14764418669fSArjun P
14778c867f78SGroverkss IntegerPolyhedron poly2 = parseIntegerPolyhedron(
14784418669fSArjun P "(x) : (x - 2*(x floordiv 2) == 0, x - 4*(x floordiv 4) - 2 == 0)");
14796d6f6c4dSArjun P EXPECT_TRUE(containsPointNoLocal(poly2, {6}));
14806d6f6c4dSArjun P EXPECT_FALSE(containsPointNoLocal(poly2, {4}));
14814418669fSArjun P
14828c867f78SGroverkss IntegerPolyhedron poly3 =
14838c867f78SGroverkss parseIntegerPolyhedron("(x, y) : (2*x - y >= 0, y - 3*x >= 0)");
148484d07d02SGroverkss
1485941f71adSAlexandre Ganea EXPECT_TRUE(poly3.containsPointNoLocal(ArrayRef<int64_t>({0, 0})));
148684d07d02SGroverkss EXPECT_FALSE(poly3.containsPointNoLocal({1, 0}));
14874418669fSArjun P }
14889615d717SArjun P
TEST(IntegerPolyhedronTest,truncateEqualityRegressionTest)14899615d717SArjun P TEST(IntegerPolyhedronTest, truncateEqualityRegressionTest) {
14909615d717SArjun P // IntegerRelation::truncate was truncating inequalities to the number of
14919615d717SArjun P // equalities.
1492a5a598beSGroverkss IntegerRelation set(PresburgerSpace::getSetSpace(1));
14939615d717SArjun P IntegerRelation::CountsSnapshot snapshot = set.getCounts();
14949615d717SArjun P set.addEquality({1, 0});
14959615d717SArjun P set.truncate(snapshot);
14969615d717SArjun P EXPECT_EQ(set.getNumEqualities(), 0u);
14979615d717SArjun P }
1498