1 //===- IntegerPolyhedron.cpp - Tests for IntegerPolyhedron 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 "./Utils.h" 10 #include "mlir/Analysis/Presburger/IntegerRelation.h" 11 #include "mlir/Analysis/Presburger/PWMAFunction.h" 12 #include "mlir/Analysis/Presburger/Simplex.h" 13 14 #include <gmock/gmock.h> 15 #include <gtest/gtest.h> 16 17 #include <numeric> 18 19 using namespace mlir; 20 using namespace presburger; 21 22 using testing::ElementsAre; 23 24 enum class TestFunction { Sample, Empty }; 25 26 /// Construct a IntegerPolyhedron from a set of inequality and 27 /// equality constraints. 28 static IntegerPolyhedron 29 makeSetFromConstraints(unsigned ids, ArrayRef<SmallVector<int64_t, 4>> ineqs, 30 ArrayRef<SmallVector<int64_t, 4>> eqs, 31 unsigned syms = 0) { 32 IntegerPolyhedron set( 33 ineqs.size(), eqs.size(), ids + 1, 34 PresburgerSpace::getSetSpace(ids - syms, syms, /*numLocals=*/0)); 35 for (const auto &eq : eqs) 36 set.addEquality(eq); 37 for (const auto &ineq : ineqs) 38 set.addInequality(ineq); 39 return set; 40 } 41 42 static void dump(ArrayRef<int64_t> vec) { 43 for (int64_t x : vec) 44 llvm::errs() << x << ' '; 45 llvm::errs() << '\n'; 46 } 47 48 /// If fn is TestFunction::Sample (default): 49 /// 50 /// If hasSample is true, check that findIntegerSample returns a valid sample 51 /// for the IntegerPolyhedron poly. Also check that getIntegerLexmin finds a 52 /// non-empty lexmin. 53 /// 54 /// If hasSample is false, check that findIntegerSample returns None and 55 /// findIntegerLexMin returns Empty. 56 /// 57 /// If fn is TestFunction::Empty, check that isIntegerEmpty returns the 58 /// opposite of hasSample. 59 static void checkSample(bool hasSample, const IntegerPolyhedron &poly, 60 TestFunction fn = TestFunction::Sample) { 61 Optional<SmallVector<int64_t, 8>> maybeSample; 62 MaybeOptimum<SmallVector<int64_t, 8>> maybeLexMin; 63 switch (fn) { 64 case TestFunction::Sample: 65 maybeSample = poly.findIntegerSample(); 66 maybeLexMin = poly.findIntegerLexMin(); 67 68 if (!hasSample) { 69 EXPECT_FALSE(maybeSample.has_value()); 70 if (maybeSample.has_value()) { 71 llvm::errs() << "findIntegerSample gave sample: "; 72 dump(*maybeSample); 73 } 74 75 EXPECT_TRUE(maybeLexMin.isEmpty()); 76 if (maybeLexMin.isBounded()) { 77 llvm::errs() << "findIntegerLexMin gave sample: "; 78 dump(*maybeLexMin); 79 } 80 } else { 81 ASSERT_TRUE(maybeSample.has_value()); 82 EXPECT_TRUE(poly.containsPoint(*maybeSample)); 83 84 ASSERT_FALSE(maybeLexMin.isEmpty()); 85 if (maybeLexMin.isUnbounded()) { 86 EXPECT_TRUE(Simplex(poly).isUnbounded()); 87 } 88 if (maybeLexMin.isBounded()) { 89 EXPECT_TRUE(poly.containsPoint(*maybeLexMin)); 90 } 91 } 92 break; 93 case TestFunction::Empty: 94 EXPECT_EQ(!hasSample, poly.isIntegerEmpty()); 95 break; 96 } 97 } 98 99 /// Check sampling for all the permutations of the dimensions for the given 100 /// constraint set. Since the GBR algorithm progresses dimension-wise, different 101 /// orderings may cause the algorithm to proceed differently. At least some of 102 ///.these permutations should make it past the heuristics and test the 103 /// implementation of the GBR algorithm itself. 104 /// Use TestFunction fn to test. 105 static void checkPermutationsSample(bool hasSample, unsigned nDim, 106 ArrayRef<SmallVector<int64_t, 4>> ineqs, 107 ArrayRef<SmallVector<int64_t, 4>> eqs, 108 TestFunction fn = TestFunction::Sample) { 109 SmallVector<unsigned, 4> perm(nDim); 110 std::iota(perm.begin(), perm.end(), 0); 111 auto permute = [&perm](ArrayRef<int64_t> coeffs) { 112 SmallVector<int64_t, 4> permuted; 113 for (unsigned id : perm) 114 permuted.push_back(coeffs[id]); 115 permuted.push_back(coeffs.back()); 116 return permuted; 117 }; 118 do { 119 SmallVector<SmallVector<int64_t, 4>, 4> permutedIneqs, permutedEqs; 120 for (const auto &ineq : ineqs) 121 permutedIneqs.push_back(permute(ineq)); 122 for (const auto &eq : eqs) 123 permutedEqs.push_back(permute(eq)); 124 125 checkSample(hasSample, 126 makeSetFromConstraints(nDim, permutedIneqs, permutedEqs), fn); 127 } while (std::next_permutation(perm.begin(), perm.end())); 128 } 129 130 TEST(IntegerPolyhedronTest, removeInequality) { 131 IntegerPolyhedron set = 132 makeSetFromConstraints(1, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}}, {}); 133 134 set.removeInequalityRange(0, 0); 135 EXPECT_EQ(set.getNumInequalities(), 5u); 136 137 set.removeInequalityRange(1, 3); 138 EXPECT_EQ(set.getNumInequalities(), 3u); 139 EXPECT_THAT(set.getInequality(0), ElementsAre(0, 0)); 140 EXPECT_THAT(set.getInequality(1), ElementsAre(3, 3)); 141 EXPECT_THAT(set.getInequality(2), ElementsAre(4, 4)); 142 143 set.removeInequality(1); 144 EXPECT_EQ(set.getNumInequalities(), 2u); 145 EXPECT_THAT(set.getInequality(0), ElementsAre(0, 0)); 146 EXPECT_THAT(set.getInequality(1), ElementsAre(4, 4)); 147 } 148 149 TEST(IntegerPolyhedronTest, removeEquality) { 150 IntegerPolyhedron set = 151 makeSetFromConstraints(1, {}, {{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}}); 152 153 set.removeEqualityRange(0, 0); 154 EXPECT_EQ(set.getNumEqualities(), 5u); 155 156 set.removeEqualityRange(1, 3); 157 EXPECT_EQ(set.getNumEqualities(), 3u); 158 EXPECT_THAT(set.getEquality(0), ElementsAre(0, 0)); 159 EXPECT_THAT(set.getEquality(1), ElementsAre(3, 3)); 160 EXPECT_THAT(set.getEquality(2), ElementsAre(4, 4)); 161 162 set.removeEquality(1); 163 EXPECT_EQ(set.getNumEqualities(), 2u); 164 EXPECT_THAT(set.getEquality(0), ElementsAre(0, 0)); 165 EXPECT_THAT(set.getEquality(1), ElementsAre(4, 4)); 166 } 167 168 TEST(IntegerPolyhedronTest, clearConstraints) { 169 IntegerPolyhedron set = makeSetFromConstraints(1, {}, {}); 170 171 set.addInequality({1, 0}); 172 EXPECT_EQ(set.atIneq(0, 0), 1); 173 EXPECT_EQ(set.atIneq(0, 1), 0); 174 175 set.clearConstraints(); 176 177 set.addInequality({1, 0}); 178 EXPECT_EQ(set.atIneq(0, 0), 1); 179 EXPECT_EQ(set.atIneq(0, 1), 0); 180 } 181 182 TEST(IntegerPolyhedronTest, removeIdRange) { 183 IntegerPolyhedron set(PresburgerSpace::getSetSpace(3, 2, 1)); 184 185 set.addInequality({10, 11, 12, 20, 21, 30, 40}); 186 set.removeVar(VarKind::Symbol, 1); 187 EXPECT_THAT(set.getInequality(0), 188 testing::ElementsAre(10, 11, 12, 20, 30, 40)); 189 190 set.removeVarRange(VarKind::SetDim, 0, 2); 191 EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 30, 40)); 192 193 set.removeVarRange(VarKind::Local, 1, 1); 194 EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 30, 40)); 195 196 set.removeVarRange(VarKind::Local, 0, 1); 197 EXPECT_THAT(set.getInequality(0), testing::ElementsAre(12, 20, 40)); 198 } 199 200 TEST(IntegerPolyhedronTest, FindSampleTest) { 201 // Bounded sets with only inequalities. 202 // 0 <= 7x <= 5 203 checkSample(true, parsePoly("(x) : (7 * x >= 0, -7 * x + 5 >= 0)")); 204 205 // 1 <= 5x and 5x <= 4 (no solution). 206 checkSample(false, parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)")); 207 208 // 1 <= 5x and 5x <= 9 (solution: x = 1). 209 checkSample(true, parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)")); 210 211 // Bounded sets with equalities. 212 // x >= 8 and 40 >= y and x = y. 213 checkSample(true, 214 parsePoly("(x,y) : (x - 8 >= 0, -y + 40 >= 0, x - y == 0)")); 215 216 // x <= 10 and y <= 10 and 10 <= z and x + 2y = 3z. 217 // solution: x = y = z = 10. 218 checkSample(true, parsePoly("(x,y,z) : (-x + 10 >= 0, -y + 10 >= 0, " 219 "z - 10 >= 0, x + 2 * y - 3 * z == 0)")); 220 221 // x <= 10 and y <= 10 and 11 <= z and x + 2y = 3z. 222 // This implies x + 2y >= 33 and x + 2y <= 30, which has no solution. 223 checkSample(false, parsePoly("(x,y,z) : (-x + 10 >= 0, -y + 10 >= 0, " 224 "z - 11 >= 0, x + 2 * y - 3 * z == 0)")); 225 226 // 0 <= r and r <= 3 and 4q + r = 7. 227 // Solution: q = 1, r = 3. 228 checkSample(true, 229 parsePoly("(q,r) : (r >= 0, -r + 3 >= 0, 4 * q + r - 7 == 0)")); 230 231 // 4q + r = 7 and r = 0. 232 // Solution: q = 1, r = 3. 233 checkSample(false, parsePoly("(q,r) : (4 * q + r - 7 == 0, r == 0)")); 234 235 // The next two sets are large sets that should take a long time to sample 236 // with a naive branch and bound algorithm but can be sampled efficiently with 237 // the GBR algorithm. 238 // 239 // This is a triangle with vertices at (1/3, 0), (2/3, 0) and (10000, 10000). 240 checkSample(true, parsePoly("(x,y) : (y >= 0, " 241 "300000 * x - 299999 * y - 100000 >= 0, " 242 "-300000 * x + 299998 * y + 200000 >= 0)")); 243 244 // This is a tetrahedron with vertices at 245 // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 10000), and (10000, 10000, 10000). 246 // The first three points form a triangular base on the xz plane with the 247 // apex at the fourth point, which is the only integer point. 248 checkPermutationsSample( 249 true, 3, 250 { 251 {0, 1, 0, 0}, // y >= 0 252 {0, -1, 1, 0}, // z >= y 253 {300000, -299998, -1, 254 -100000}, // -300000x + 299998y + 100000 + z <= 0. 255 {-150000, 149999, 0, 100000}, // -150000x + 149999y + 100000 >= 0. 256 }, 257 {}); 258 259 // Same thing with some spurious extra dimensions equated to constants. 260 checkSample( 261 true, 262 parsePoly("(a,b,c,d,e) : (b + d - e >= 0, -b + c - d + e >= 0, " 263 "300000 * a - 299998 * b - c - 9 * d + 21 * e - 112000 >= 0, " 264 "-150000 * a + 149999 * b - 15 * d + 47 * e + 68000 >= 0, " 265 "d - e == 0, d + e - 2000 == 0)")); 266 267 // This is a tetrahedron with vertices at 268 // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 100), (100, 100 - 1/3, 100). 269 checkPermutationsSample(false, 3, 270 { 271 {0, 1, 0, 0}, 272 {0, -300, 299, 0}, 273 {300 * 299, -89400, -299, -100 * 299}, 274 {-897, 894, 0, 598}, 275 }, 276 {}); 277 278 // Two tests involving equalities that are integer empty but not rational 279 // empty. 280 281 // This is a line segment from (0, 1/3) to (100, 100 + 1/3). 282 checkSample( 283 false, 284 parsePoly("(x,y) : (x >= 0, -x + 100 >= 0, 3 * x - 3 * y + 1 == 0)")); 285 286 // A thin parallelogram. 0 <= x <= 100 and x + 1/3 <= y <= x + 2/3. 287 checkSample(false, 288 parsePoly("(x,y) : (x >= 0, -x + 100 >= 0, " 289 "3 * x - 3 * y + 2 >= 0, -3 * x + 3 * y - 1 >= 0)")); 290 291 checkSample(true, parsePoly("(x,y) : (2 * x >= 0, -2 * x + 99 >= 0, " 292 "2 * y >= 0, -2 * y + 99 >= 0)")); 293 294 // 2D cone with apex at (10000, 10000) and 295 // edges passing through (1/3, 0) and (2/3, 0). 296 checkSample(true, parsePoly("(x,y) : (300000 * x - 299999 * y - 100000 >= 0, " 297 "-300000 * x + 299998 * y + 200000 >= 0)")); 298 299 // Cartesian product of a tetrahedron and a 2D cone. 300 // The tetrahedron has vertices at 301 // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 10000), and (10000, 10000, 10000). 302 // The first three points form a triangular base on the xz plane with the 303 // apex at the fourth point, which is the only integer point. 304 // The cone has apex at (10000, 10000) and 305 // edges passing through (1/3, 0) and (2/3, 0). 306 checkPermutationsSample( 307 true /* not empty */, 5, 308 { 309 // Tetrahedron contraints: 310 {0, 1, 0, 0, 0, 0}, // y >= 0 311 {0, -1, 1, 0, 0, 0}, // z >= y 312 // -300000x + 299998y + 100000 + z <= 0. 313 {300000, -299998, -1, 0, 0, -100000}, 314 // -150000x + 149999y + 100000 >= 0. 315 {-150000, 149999, 0, 0, 0, 100000}, 316 317 // Triangle constraints: 318 // 300000p - 299999q >= 100000 319 {0, 0, 0, 300000, -299999, -100000}, 320 // -300000p + 299998q + 200000 >= 0 321 {0, 0, 0, -300000, 299998, 200000}, 322 }, 323 {}); 324 325 // Cartesian product of same tetrahedron as above and {(p, q) : 1/3 <= p <= 326 // 2/3}. Since the second set is empty, the whole set is too. 327 checkPermutationsSample( 328 false /* empty */, 5, 329 { 330 // Tetrahedron contraints: 331 {0, 1, 0, 0, 0, 0}, // y >= 0 332 {0, -1, 1, 0, 0, 0}, // z >= y 333 // -300000x + 299998y + 100000 + z <= 0. 334 {300000, -299998, -1, 0, 0, -100000}, 335 // -150000x + 149999y + 100000 >= 0. 336 {-150000, 149999, 0, 0, 0, 100000}, 337 338 // Second set constraints: 339 // 3p >= 1 340 {0, 0, 0, 3, 0, -1}, 341 // 3p <= 2 342 {0, 0, 0, -3, 0, 2}, 343 }, 344 {}); 345 346 // Cartesian product of same tetrahedron as above and 347 // {(p, q, r) : 1 <= p <= 2 and p = 3q + 3r}. 348 // Since the second set is empty, the whole set is too. 349 checkPermutationsSample( 350 false /* empty */, 5, 351 { 352 // Tetrahedron contraints: 353 {0, 1, 0, 0, 0, 0, 0}, // y >= 0 354 {0, -1, 1, 0, 0, 0, 0}, // z >= y 355 // -300000x + 299998y + 100000 + z <= 0. 356 {300000, -299998, -1, 0, 0, 0, -100000}, 357 // -150000x + 149999y + 100000 >= 0. 358 {-150000, 149999, 0, 0, 0, 0, 100000}, 359 360 // Second set constraints: 361 // p >= 1 362 {0, 0, 0, 1, 0, 0, -1}, 363 // p <= 2 364 {0, 0, 0, -1, 0, 0, 2}, 365 }, 366 { 367 {0, 0, 0, 1, -3, -3, 0}, // p = 3q + 3r 368 }); 369 370 // Cartesian product of a tetrahedron and a 2D cone. 371 // The tetrahedron is empty and has vertices at 372 // (1/3, 0, 0), (2/3, 0, 0), (2/3, 0, 100), and (100, 100 - 1/3, 100). 373 // The cone has apex at (10000, 10000) and 374 // edges passing through (1/3, 0) and (2/3, 0). 375 // Since the tetrahedron is empty, the Cartesian product is too. 376 checkPermutationsSample(false /* empty */, 5, 377 { 378 // Tetrahedron contraints: 379 {0, 1, 0, 0, 0, 0}, 380 {0, -300, 299, 0, 0, 0}, 381 {300 * 299, -89400, -299, 0, 0, -100 * 299}, 382 {-897, 894, 0, 0, 0, 598}, 383 384 // Triangle constraints: 385 // 300000p - 299999q >= 100000 386 {0, 0, 0, 300000, -299999, -100000}, 387 // -300000p + 299998q + 200000 >= 0 388 {0, 0, 0, -300000, 299998, 200000}, 389 }, 390 {}); 391 392 // Cartesian product of same tetrahedron as above and 393 // {(p, q) : 1/3 <= p <= 2/3}. 394 checkPermutationsSample(false /* empty */, 5, 395 { 396 // Tetrahedron contraints: 397 {0, 1, 0, 0, 0, 0}, 398 {0, -300, 299, 0, 0, 0}, 399 {300 * 299, -89400, -299, 0, 0, -100 * 299}, 400 {-897, 894, 0, 0, 0, 598}, 401 402 // Second set constraints: 403 // 3p >= 1 404 {0, 0, 0, 3, 0, -1}, 405 // 3p <= 2 406 {0, 0, 0, -3, 0, 2}, 407 }, 408 {}); 409 410 checkSample(true, parsePoly("(x, y, z) : (2 * x - 1 >= 0, x - y - 1 == 0, " 411 "y - z == 0)")); 412 413 // Regression tests for the computation of dual coefficients. 414 checkSample(false, parsePoly("(x, y, z) : (" 415 "6*x - 4*y + 9*z + 2 >= 0," 416 "x + 5*y + z + 5 >= 0," 417 "-4*x + y + 2*z - 1 >= 0," 418 "-3*x - 2*y - 7*z - 1 >= 0," 419 "-7*x - 5*y - 9*z - 1 >= 0)")); 420 checkSample(true, parsePoly("(x, y, z) : (" 421 "3*x + 3*y + 3 >= 0," 422 "-4*x - 8*y - z + 4 >= 0," 423 "-7*x - 4*y + z + 1 >= 0," 424 "2*x - 7*y - 8*z - 7 >= 0," 425 "9*x + 8*y - 9*z - 7 >= 0)")); 426 } 427 428 TEST(IntegerPolyhedronTest, IsIntegerEmptyTest) { 429 // 1 <= 5x and 5x <= 4 (no solution). 430 EXPECT_TRUE( 431 parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)").isIntegerEmpty()); 432 // 1 <= 5x and 5x <= 9 (solution: x = 1). 433 EXPECT_FALSE( 434 parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)").isIntegerEmpty()); 435 436 // Unbounded sets. 437 EXPECT_TRUE(parsePoly("(x,y,z) : (2 * y - 1 >= 0, -2 * y + 1 >= 0, " 438 "2 * z - 1 >= 0, 2 * x - 1 == 0)") 439 .isIntegerEmpty()); 440 441 EXPECT_FALSE(parsePoly("(x,y,z) : (2 * x - 1 >= 0, -3 * x + 3 >= 0, " 442 "5 * z - 6 >= 0, -7 * z + 17 >= 0, 3 * y - 2 >= 0)") 443 .isIntegerEmpty()); 444 445 EXPECT_FALSE( 446 parsePoly("(x,y,z) : (2 * x - 1 >= 0, x - y - 1 == 0, y - z == 0)") 447 .isIntegerEmpty()); 448 449 // IntegerPolyhedron::isEmpty() does not detect the following sets to be 450 // empty. 451 452 // 3x + 7y = 1 and 0 <= x, y <= 10. 453 // Since x and y are non-negative, 3x + 7y can never be 1. 454 EXPECT_TRUE(parsePoly("(x,y) : (x >= 0, -x + 10 >= 0, y >= 0, -y + 10 >= 0, " 455 "3 * x + 7 * y - 1 == 0)") 456 .isIntegerEmpty()); 457 458 // 2x = 3y and y = x - 1 and x + y = 6z + 2 and 0 <= x, y <= 100. 459 // Substituting y = x - 1 in 3y = 2x, we obtain x = 3 and hence y = 2. 460 // Since x + y = 5 cannot be equal to 6z + 2 for any z, the set is empty. 461 EXPECT_TRUE( 462 parsePoly("(x,y,z) : (x >= 0, -x + 100 >= 0, y >= 0, -y + 100 >= 0, " 463 "2 * x - 3 * y == 0, x - y - 1 == 0, x + y - 6 * z - 2 == 0)") 464 .isIntegerEmpty()); 465 466 // 2x = 3y and y = x - 1 + 6z and x + y = 6q + 2 and 0 <= x, y <= 100. 467 // 2x = 3y implies x is a multiple of 3 and y is even. 468 // Now y = x - 1 + 6z implies y = 2 mod 3. In fact, since y is even, we have 469 // y = 2 mod 6. Then since x = y + 1 + 6z, we have x = 3 mod 6, implying 470 // x + y = 5 mod 6, which contradicts x + y = 6q + 2, so the set is empty. 471 EXPECT_TRUE( 472 parsePoly( 473 "(x,y,z,q) : (x >= 0, -x + 100 >= 0, y >= 0, -y + 100 >= 0, " 474 "2 * x - 3 * y == 0, x - y + 6 * z - 1 == 0, x + y - 6 * q - 2 == 0)") 475 .isIntegerEmpty()); 476 477 // Set with symbols. 478 EXPECT_FALSE(parsePoly("(x)[s] : (x + s >= 0, x - s == 0)").isIntegerEmpty()); 479 } 480 481 TEST(IntegerPolyhedronTest, removeRedundantConstraintsTest) { 482 IntegerPolyhedron poly = 483 parsePoly("(x) : (x - 2 >= 0, -x + 2 >= 0, x - 2 == 0)"); 484 poly.removeRedundantConstraints(); 485 486 // Both inequalities are redundant given the equality. Both have been removed. 487 EXPECT_EQ(poly.getNumInequalities(), 0u); 488 EXPECT_EQ(poly.getNumEqualities(), 1u); 489 490 IntegerPolyhedron poly2 = 491 parsePoly("(x,y) : (x - 3 >= 0, y - 2 >= 0, x - y == 0)"); 492 poly2.removeRedundantConstraints(); 493 494 // The second inequality is redundant and should have been removed. The 495 // remaining inequality should be the first one. 496 EXPECT_EQ(poly2.getNumInequalities(), 1u); 497 EXPECT_THAT(poly2.getInequality(0), ElementsAre(1, 0, -3)); 498 EXPECT_EQ(poly2.getNumEqualities(), 1u); 499 500 IntegerPolyhedron poly3 = 501 parsePoly("(x,y,z) : (x - y == 0, x - z == 0, y - z == 0)"); 502 poly3.removeRedundantConstraints(); 503 504 // One of the three equalities can be removed. 505 EXPECT_EQ(poly3.getNumInequalities(), 0u); 506 EXPECT_EQ(poly3.getNumEqualities(), 2u); 507 508 IntegerPolyhedron poly4 = 509 parsePoly("(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) : (" 510 "b - 1 >= 0," 511 "-b + 500 >= 0," 512 "-16 * d + f >= 0," 513 "f - 1 >= 0," 514 "-f + 998 >= 0," 515 "16 * d - f + 15 >= 0," 516 "-16 * e + g >= 0," 517 "g - 1 >= 0," 518 "-g + 998 >= 0," 519 "16 * e - g + 15 >= 0," 520 "h >= 0," 521 "-h + 1 >= 0," 522 "j - 1 >= 0," 523 "-j + 500 >= 0," 524 "-f + 16 * l + 15 >= 0," 525 "f - 16 * l >= 0," 526 "-16 * m + o >= 0," 527 "o - 1 >= 0," 528 "-o + 998 >= 0," 529 "16 * m - o + 15 >= 0," 530 "p >= 0," 531 "-p + 1 >= 0," 532 "-g - h + 8 * q + 8 >= 0," 533 "-o - p + 8 * q + 8 >= 0," 534 "o + p - 8 * q - 1 >= 0," 535 "g + h - 8 * q - 1 >= 0," 536 "-f + n >= 0," 537 "f - n >= 0," 538 "k - 10 >= 0," 539 "-k + 10 >= 0," 540 "i - 13 >= 0," 541 "-i + 13 >= 0," 542 "c - 10 >= 0," 543 "-c + 10 >= 0," 544 "a - 13 >= 0," 545 "-a + 13 >= 0" 546 ")"); 547 548 // The above is a large set of constraints without any redundant constraints, 549 // as verified by the Fourier-Motzkin based removeRedundantInequalities. 550 unsigned nIneq = poly4.getNumInequalities(); 551 unsigned nEq = poly4.getNumEqualities(); 552 poly4.removeRedundantInequalities(); 553 ASSERT_EQ(poly4.getNumInequalities(), nIneq); 554 ASSERT_EQ(poly4.getNumEqualities(), nEq); 555 // Now we test that removeRedundantConstraints does not find any constraints 556 // to be redundant either. 557 poly4.removeRedundantConstraints(); 558 EXPECT_EQ(poly4.getNumInequalities(), nIneq); 559 EXPECT_EQ(poly4.getNumEqualities(), nEq); 560 561 IntegerPolyhedron poly5 = parsePoly( 562 "(x,y) : (128 * x + 127 >= 0, -x + 7 >= 0, -128 * x + y >= 0, y >= 0)"); 563 // 128x + 127 >= 0 implies that 128x >= 0, since x has to be an integer. 564 // (This should be caught by GCDTightenInqualities().) 565 // So -128x + y >= 0 and 128x + 127 >= 0 imply y >= 0 since we have 566 // y >= 128x >= 0. 567 poly5.removeRedundantConstraints(); 568 EXPECT_EQ(poly5.getNumInequalities(), 3u); 569 SmallVector<int64_t, 8> redundantConstraint = {0, 1, 0}; 570 for (unsigned i = 0; i < 3; ++i) { 571 // Ensure that the removed constraint was the redundant constraint [3]. 572 EXPECT_NE(poly5.getInequality(i), ArrayRef<int64_t>(redundantConstraint)); 573 } 574 } 575 576 TEST(IntegerPolyhedronTest, addConstantUpperBound) { 577 IntegerPolyhedron poly(PresburgerSpace::getSetSpace(2)); 578 poly.addBound(IntegerPolyhedron::UB, 0, 1); 579 EXPECT_EQ(poly.atIneq(0, 0), -1); 580 EXPECT_EQ(poly.atIneq(0, 1), 0); 581 EXPECT_EQ(poly.atIneq(0, 2), 1); 582 583 poly.addBound(IntegerPolyhedron::UB, {1, 2, 3}, 1); 584 EXPECT_EQ(poly.atIneq(1, 0), -1); 585 EXPECT_EQ(poly.atIneq(1, 1), -2); 586 EXPECT_EQ(poly.atIneq(1, 2), -2); 587 } 588 589 TEST(IntegerPolyhedronTest, addConstantLowerBound) { 590 IntegerPolyhedron poly(PresburgerSpace::getSetSpace(2)); 591 poly.addBound(IntegerPolyhedron::LB, 0, 1); 592 EXPECT_EQ(poly.atIneq(0, 0), 1); 593 EXPECT_EQ(poly.atIneq(0, 1), 0); 594 EXPECT_EQ(poly.atIneq(0, 2), -1); 595 596 poly.addBound(IntegerPolyhedron::LB, {1, 2, 3}, 1); 597 EXPECT_EQ(poly.atIneq(1, 0), 1); 598 EXPECT_EQ(poly.atIneq(1, 1), 2); 599 EXPECT_EQ(poly.atIneq(1, 2), 2); 600 } 601 602 /// Check if the expected division representation of local variables matches the 603 /// computed representation. The expected division representation is given as 604 /// a vector of expressions set in `expectedDividends` and the corressponding 605 /// denominator in `expectedDenominators`. The `denominators` and `dividends` 606 /// obtained through `getLocalRepr` function is verified against the 607 /// `expectedDenominators` and `expectedDividends` respectively. 608 static void checkDivisionRepresentation( 609 IntegerPolyhedron &poly, 610 const std::vector<SmallVector<int64_t, 8>> &expectedDividends, 611 ArrayRef<unsigned> expectedDenominators) { 612 DivisionRepr divs = poly.getLocalReprs(); 613 614 // Check that the `denominators` and `expectedDenominators` match. 615 EXPECT_TRUE(expectedDenominators == divs.getDenoms()); 616 617 // Check that the `dividends` and `expectedDividends` match. If the 618 // denominator for a division is zero, we ignore its dividend. 619 EXPECT_TRUE(divs.getNumDivs() == expectedDividends.size()); 620 for (unsigned i = 0, e = divs.getNumDivs(); i < e; ++i) 621 if (divs.hasRepr(i)) 622 for (unsigned j = 0, f = divs.getNumVars() + 1; j < f; ++j) 623 EXPECT_TRUE(expectedDividends[i][j] == divs.getDividend(i)[j]); 624 } 625 626 TEST(IntegerPolyhedronTest, computeLocalReprSimple) { 627 IntegerPolyhedron poly(PresburgerSpace::getSetSpace(1)); 628 629 poly.addLocalFloorDiv({1, 4}, 10); 630 poly.addLocalFloorDiv({1, 0, 100}, 10); 631 632 std::vector<SmallVector<int64_t, 8>> divisions = {{1, 0, 0, 4}, 633 {1, 0, 0, 100}}; 634 SmallVector<unsigned, 8> denoms = {10, 10}; 635 636 // Check if floordivs can be computed when no other inequalities exist 637 // and floor divs do not depend on each other. 638 checkDivisionRepresentation(poly, divisions, denoms); 639 } 640 641 TEST(IntegerPolyhedronTest, computeLocalReprConstantFloorDiv) { 642 IntegerPolyhedron poly(PresburgerSpace::getSetSpace(4)); 643 644 poly.addInequality({1, 0, 3, 1, 2}); 645 poly.addInequality({1, 2, -8, 1, 10}); 646 poly.addEquality({1, 2, -4, 1, 10}); 647 648 poly.addLocalFloorDiv({0, 0, 0, 0, 100}, 30); 649 poly.addLocalFloorDiv({0, 0, 0, 0, 0, 206}, 101); 650 651 std::vector<SmallVector<int64_t, 8>> divisions = {{0, 0, 0, 0, 0, 0, 3}, 652 {0, 0, 0, 0, 0, 0, 2}}; 653 SmallVector<unsigned, 8> denoms = {1, 1}; 654 655 // Check if floordivs with constant numerator can be computed. 656 checkDivisionRepresentation(poly, divisions, denoms); 657 } 658 659 TEST(IntegerPolyhedronTest, computeLocalReprRecursive) { 660 IntegerPolyhedron poly(PresburgerSpace::getSetSpace(4)); 661 poly.addInequality({1, 0, 3, 1, 2}); 662 poly.addInequality({1, 2, -8, 1, 10}); 663 poly.addEquality({1, 2, -4, 1, 10}); 664 665 poly.addLocalFloorDiv({0, -2, 7, 2, 10}, 3); 666 poly.addLocalFloorDiv({3, 0, 9, 2, 2, 10}, 5); 667 poly.addLocalFloorDiv({0, 1, -123, 2, 0, -4, 10}, 3); 668 669 poly.addInequality({1, 2, -2, 1, -5, 0, 6, 100}); 670 poly.addInequality({1, 2, -8, 1, 3, 7, 0, -9}); 671 672 std::vector<SmallVector<int64_t, 8>> divisions = { 673 {0, -2, 7, 2, 0, 0, 0, 10}, 674 {3, 0, 9, 2, 2, 0, 0, 10}, 675 {0, 1, -123, 2, 0, -4, 0, 10}}; 676 677 SmallVector<unsigned, 8> denoms = {3, 5, 3}; 678 679 // Check if floordivs which may depend on other floordivs can be computed. 680 checkDivisionRepresentation(poly, divisions, denoms); 681 } 682 683 TEST(IntegerPolyhedronTest, computeLocalReprTightUpperBound) { 684 { 685 IntegerPolyhedron poly = parsePoly("(i) : (i mod 3 - 1 >= 0)"); 686 687 // The set formed by the poly is: 688 // 3q - i + 2 >= 0 <-- Division lower bound 689 // -3q + i - 1 >= 0 690 // -3q + i >= 0 <-- Division upper bound 691 // We remove redundant constraints to get the set: 692 // 3q - i + 2 >= 0 <-- Division lower bound 693 // -3q + i - 1 >= 0 <-- Tighter division upper bound 694 // thus, making the upper bound tighter. 695 poly.removeRedundantConstraints(); 696 697 std::vector<SmallVector<int64_t, 8>> divisions = {{1, 0, 0}}; 698 SmallVector<unsigned, 8> denoms = {3}; 699 700 // Check if the divisions can be computed even with a tighter upper bound. 701 checkDivisionRepresentation(poly, divisions, denoms); 702 } 703 704 { 705 IntegerPolyhedron poly = 706 parsePoly("(i, j, q) : (4*q - i - j + 2 >= 0, -4*q + i + j >= 0)"); 707 // Convert `q` to a local variable. 708 poly.convertToLocal(VarKind::SetDim, 2, 3); 709 710 std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 1}}; 711 SmallVector<unsigned, 8> denoms = {4}; 712 713 // Check if the divisions can be computed even with a tighter upper bound. 714 checkDivisionRepresentation(poly, divisions, denoms); 715 } 716 } 717 718 TEST(IntegerPolyhedronTest, computeLocalReprFromEquality) { 719 { 720 IntegerPolyhedron poly = parsePoly("(i, j, q) : (-4*q + i + j == 0)"); 721 // Convert `q` to a local variable. 722 poly.convertToLocal(VarKind::SetDim, 2, 3); 723 724 std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 0}}; 725 SmallVector<unsigned, 8> denoms = {4}; 726 727 checkDivisionRepresentation(poly, divisions, denoms); 728 } 729 { 730 IntegerPolyhedron poly = parsePoly("(i, j, q) : (4*q - i - j == 0)"); 731 // Convert `q` to a local variable. 732 poly.convertToLocal(VarKind::SetDim, 2, 3); 733 734 std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 0}}; 735 SmallVector<unsigned, 8> denoms = {4}; 736 737 checkDivisionRepresentation(poly, divisions, denoms); 738 } 739 { 740 IntegerPolyhedron poly = parsePoly("(i, j, q) : (3*q + i + j - 2 == 0)"); 741 // Convert `q` to a local variable. 742 poly.convertToLocal(VarKind::SetDim, 2, 3); 743 744 std::vector<SmallVector<int64_t, 8>> divisions = {{-1, -1, 0, 2}}; 745 SmallVector<unsigned, 8> denoms = {3}; 746 747 checkDivisionRepresentation(poly, divisions, denoms); 748 } 749 } 750 751 TEST(IntegerPolyhedronTest, computeLocalReprFromEqualityAndInequality) { 752 { 753 IntegerPolyhedron poly = 754 parsePoly("(i, j, q, k) : (-3*k + i + j == 0, 4*q - " 755 "i - j + 2 >= 0, -4*q + i + j >= 0)"); 756 // Convert `q` and `k` to local variables. 757 poly.convertToLocal(VarKind::SetDim, 2, 4); 758 759 std::vector<SmallVector<int64_t, 8>> divisions = {{1, 1, 0, 0, 1}, 760 {1, 1, 0, 0, 0}}; 761 SmallVector<unsigned, 8> denoms = {4, 3}; 762 763 checkDivisionRepresentation(poly, divisions, denoms); 764 } 765 } 766 767 TEST(IntegerPolyhedronTest, computeLocalReprNoRepr) { 768 IntegerPolyhedron poly = 769 parsePoly("(x, q) : (x - 3 * q >= 0, -x + 3 * q + 3 >= 0)"); 770 // Convert q to a local variable. 771 poly.convertToLocal(VarKind::SetDim, 1, 2); 772 773 std::vector<SmallVector<int64_t, 8>> divisions = {{0, 0, 0}}; 774 SmallVector<unsigned, 8> denoms = {0}; 775 776 // Check that no division is computed. 777 checkDivisionRepresentation(poly, divisions, denoms); 778 } 779 780 TEST(IntegerPolyhedronTest, computeLocalReprNegConstNormalize) { 781 IntegerPolyhedron poly = 782 parsePoly("(x, q) : (-1 - 3*x - 6 * q >= 0, 6 + 3*x + 6*q >= 0)"); 783 // Convert q to a local variable. 784 poly.convertToLocal(VarKind::SetDim, 1, 2); 785 786 // q = floor((-1/3 - x)/2) 787 // = floor((1/3) + (-1 - x)/2) 788 // = floor((-1 - x)/2). 789 std::vector<SmallVector<int64_t, 8>> divisions = {{-1, 0, -1}}; 790 SmallVector<unsigned, 8> denoms = {2}; 791 checkDivisionRepresentation(poly, divisions, denoms); 792 } 793 794 TEST(IntegerPolyhedronTest, simplifyLocalsTest) { 795 // (x) : (exists y: 2x + y = 1 and y = 2). 796 IntegerPolyhedron poly(PresburgerSpace::getSetSpace(1, 0, 1)); 797 poly.addEquality({2, 1, -1}); 798 poly.addEquality({0, 1, -2}); 799 800 EXPECT_TRUE(poly.isEmpty()); 801 802 // (x) : (exists y, z, w: 3x + y = 1 and 2y = z and 3y = w and z = w). 803 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1, 0, 3)); 804 poly2.addEquality({3, 1, 0, 0, -1}); 805 poly2.addEquality({0, 2, -1, 0, 0}); 806 poly2.addEquality({0, 3, 0, -1, 0}); 807 poly2.addEquality({0, 0, 1, -1, 0}); 808 809 EXPECT_TRUE(poly2.isEmpty()); 810 811 // (x) : (exists y: x >= y + 1 and 2x + y = 0 and y >= -1). 812 IntegerPolyhedron poly3(PresburgerSpace::getSetSpace(1, 0, 1)); 813 poly3.addInequality({1, -1, -1}); 814 poly3.addInequality({0, 1, 1}); 815 poly3.addEquality({2, 1, 0}); 816 817 EXPECT_TRUE(poly3.isEmpty()); 818 } 819 820 TEST(IntegerPolyhedronTest, mergeDivisionsSimple) { 821 { 822 // (x) : (exists z, y = [x / 2] : x = 3y and x + z + 1 >= 0). 823 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1, 0, 1)); 824 poly1.addLocalFloorDiv({1, 0, 0}, 2); // y = [x / 2]. 825 poly1.addEquality({1, 0, -3, 0}); // x = 3y. 826 poly1.addInequality({1, 1, 0, 1}); // x + z + 1 >= 0. 827 828 // (x) : (exists y = [x / 2], z : x = 5y). 829 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 830 poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 831 poly2.addEquality({1, -5, 0}); // x = 5y. 832 poly2.appendVar(VarKind::Local); // Add local id z. 833 834 poly1.mergeLocalVars(poly2); 835 836 // Local space should be same. 837 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 838 839 // 1 division should be matched + 2 unmatched local ids. 840 EXPECT_EQ(poly1.getNumLocalVars(), 3u); 841 EXPECT_EQ(poly2.getNumLocalVars(), 3u); 842 } 843 844 { 845 // (x) : (exists z = [x / 5], y = [x / 2] : x = 3y). 846 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 847 poly1.addLocalFloorDiv({1, 0}, 5); // z = [x / 5]. 848 poly1.addLocalFloorDiv({1, 0, 0}, 2); // y = [x / 2]. 849 poly1.addEquality({1, 0, -3, 0}); // x = 3y. 850 851 // (x) : (exists y = [x / 2], z = [x / 5]: x = 5z). 852 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 853 poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 854 poly2.addLocalFloorDiv({1, 0, 0}, 5); // z = [x / 5]. 855 poly2.addEquality({1, 0, -5, 0}); // x = 5z. 856 857 poly1.mergeLocalVars(poly2); 858 859 // Local space should be same. 860 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 861 862 // 2 divisions should be matched. 863 EXPECT_EQ(poly1.getNumLocalVars(), 2u); 864 EXPECT_EQ(poly2.getNumLocalVars(), 2u); 865 } 866 867 { 868 // Division Normalization test. 869 // (x) : (exists z, y = [x / 2] : x = 3y and x + z + 1 >= 0). 870 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1, 0, 1)); 871 // This division would be normalized. 872 poly1.addLocalFloorDiv({3, 0, 0}, 6); // y = [3x / 6] -> [x/2]. 873 poly1.addEquality({1, 0, -3, 0}); // x = 3z. 874 poly1.addInequality({1, 1, 0, 1}); // x + y + 1 >= 0. 875 876 // (x) : (exists y = [x / 2], z : x = 5y). 877 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 878 poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 879 poly2.addEquality({1, -5, 0}); // x = 5y. 880 poly2.appendVar(VarKind::Local); // Add local id z. 881 882 poly1.mergeLocalVars(poly2); 883 884 // Local space should be same. 885 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 886 887 // One division should be matched + 2 unmatched local ids. 888 EXPECT_EQ(poly1.getNumLocalVars(), 3u); 889 EXPECT_EQ(poly2.getNumLocalVars(), 3u); 890 } 891 } 892 893 TEST(IntegerPolyhedronTest, mergeDivisionsNestedDivsions) { 894 { 895 // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z >= x). 896 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 897 poly1.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 898 poly1.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3]. 899 poly1.addInequality({-1, 1, 1, 0}); // y + z >= x. 900 901 // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z <= x). 902 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 903 poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 904 poly2.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3]. 905 poly2.addInequality({1, -1, -1, 0}); // y + z <= x. 906 907 poly1.mergeLocalVars(poly2); 908 909 // Local space should be same. 910 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 911 912 // 2 divisions should be matched. 913 EXPECT_EQ(poly1.getNumLocalVars(), 2u); 914 EXPECT_EQ(poly2.getNumLocalVars(), 2u); 915 } 916 917 { 918 // (x) : (exists y = [x / 2], z = [x + y / 3], w = [z + 1 / 5]: y + z >= x). 919 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 920 poly1.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 921 poly1.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3]. 922 poly1.addLocalFloorDiv({0, 0, 1, 1}, 5); // w = [z + 1 / 5]. 923 poly1.addInequality({-1, 1, 1, 0, 0}); // y + z >= x. 924 925 // (x) : (exists y = [x / 2], z = [x + y / 3], w = [z + 1 / 5]: y + z <= x). 926 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 927 poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 928 poly2.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3]. 929 poly2.addLocalFloorDiv({0, 0, 1, 1}, 5); // w = [z + 1 / 5]. 930 poly2.addInequality({1, -1, -1, 0, 0}); // y + z <= x. 931 932 poly1.mergeLocalVars(poly2); 933 934 // Local space should be same. 935 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 936 937 // 3 divisions should be matched. 938 EXPECT_EQ(poly1.getNumLocalVars(), 3u); 939 EXPECT_EQ(poly2.getNumLocalVars(), 3u); 940 } 941 { 942 // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z >= x). 943 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 944 poly1.addLocalFloorDiv({2, 0}, 4); // y = [2x / 4] -> [x / 2]. 945 poly1.addLocalFloorDiv({1, 1, 0}, 3); // z = [x + y / 3]. 946 poly1.addInequality({-1, 1, 1, 0}); // y + z >= x. 947 948 // (x) : (exists y = [x / 2], z = [x + y / 3]: y + z <= x). 949 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 950 poly2.addLocalFloorDiv({1, 0}, 2); // y = [x / 2]. 951 // This division would be normalized. 952 poly2.addLocalFloorDiv({3, 3, 0}, 9); // z = [3x + 3y / 9] -> [x + y / 3]. 953 poly2.addInequality({1, -1, -1, 0}); // y + z <= x. 954 955 poly1.mergeLocalVars(poly2); 956 957 // Local space should be same. 958 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 959 960 // 2 divisions should be matched. 961 EXPECT_EQ(poly1.getNumLocalVars(), 2u); 962 EXPECT_EQ(poly2.getNumLocalVars(), 2u); 963 } 964 } 965 966 TEST(IntegerPolyhedronTest, mergeDivisionsConstants) { 967 { 968 // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z >= x). 969 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 970 poly1.addLocalFloorDiv({1, 1}, 2); // y = [x + 1 / 2]. 971 poly1.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3]. 972 poly1.addInequality({-1, 1, 1, 0}); // y + z >= x. 973 974 // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x). 975 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 976 poly2.addLocalFloorDiv({1, 1}, 2); // y = [x + 1 / 2]. 977 poly2.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3]. 978 poly2.addInequality({1, -1, -1, 0}); // y + z <= x. 979 980 poly1.mergeLocalVars(poly2); 981 982 // Local space should be same. 983 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 984 985 // 2 divisions should be matched. 986 EXPECT_EQ(poly1.getNumLocalVars(), 2u); 987 EXPECT_EQ(poly2.getNumLocalVars(), 2u); 988 } 989 { 990 // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z >= x). 991 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 992 poly1.addLocalFloorDiv({1, 1}, 2); // y = [x + 1 / 2]. 993 // Normalization test. 994 poly1.addLocalFloorDiv({3, 0, 6}, 9); // z = [3x + 6 / 9] -> [x + 2 / 3]. 995 poly1.addInequality({-1, 1, 1, 0}); // y + z >= x. 996 997 // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x). 998 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 999 // Normalization test. 1000 poly2.addLocalFloorDiv({2, 2}, 4); // y = [2x + 2 / 4] -> [x + 1 / 2]. 1001 poly2.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3]. 1002 poly2.addInequality({1, -1, -1, 0}); // y + z <= x. 1003 1004 poly1.mergeLocalVars(poly2); 1005 1006 // Local space should be same. 1007 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 1008 1009 // 2 divisions should be matched. 1010 EXPECT_EQ(poly1.getNumLocalVars(), 2u); 1011 EXPECT_EQ(poly2.getNumLocalVars(), 2u); 1012 } 1013 } 1014 1015 TEST(IntegerPolyhedronTest, mergeDivisionsDuplicateInSameSet) { 1016 // (x) : (exists y = [x + 1 / 3], z = [x + 1 / 3]: y + z >= x). 1017 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 1018 poly1.addLocalFloorDiv({1, 1}, 3); // y = [x + 1 / 2]. 1019 poly1.addLocalFloorDiv({1, 0, 1}, 3); // z = [x + 1 / 3]. 1020 poly1.addInequality({-1, 1, 1, 0}); // y + z >= x. 1021 1022 // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x). 1023 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 1024 poly2.addLocalFloorDiv({1, 1}, 3); // y = [x + 1 / 3]. 1025 poly2.addLocalFloorDiv({1, 0, 2}, 3); // z = [x + 2 / 3]. 1026 poly2.addInequality({1, -1, -1, 0}); // y + z <= x. 1027 1028 poly1.mergeLocalVars(poly2); 1029 1030 // Local space should be same. 1031 EXPECT_EQ(poly1.getNumLocalVars(), poly2.getNumLocalVars()); 1032 1033 // 1 divisions should be matched. 1034 EXPECT_EQ(poly1.getNumLocalVars(), 3u); 1035 EXPECT_EQ(poly2.getNumLocalVars(), 3u); 1036 } 1037 1038 TEST(IntegerPolyhedronTest, negativeDividends) { 1039 // (x) : (exists y = [-x + 1 / 2], z = [-x - 2 / 3]: y + z >= x). 1040 IntegerPolyhedron poly1(PresburgerSpace::getSetSpace(1)); 1041 poly1.addLocalFloorDiv({-1, 1}, 2); // y = [x + 1 / 2]. 1042 // Normalization test with negative dividends 1043 poly1.addLocalFloorDiv({-3, 0, -6}, 9); // z = [3x + 6 / 9] -> [x + 2 / 3]. 1044 poly1.addInequality({-1, 1, 1, 0}); // y + z >= x. 1045 1046 // (x) : (exists y = [x + 1 / 3], z = [x + 2 / 3]: y + z <= x). 1047 IntegerPolyhedron poly2(PresburgerSpace::getSetSpace(1)); 1048 // Normalization test. 1049 poly2.addLocalFloorDiv({-2, 2}, 4); // y = [-2x + 2 / 4] -> [-x + 1 / 2]. 1050 poly2.addLocalFloorDiv({-1, 0, -2}, 3); // z = [-x - 2 / 3]. 1051 poly2.addInequality({1, -1, -1, 0}); // y + z <= x. 1052 1053 poly1.mergeLocalVars(poly2); 1054 1055 // Merging triggers normalization. 1056 std::vector<SmallVector<int64_t, 8>> divisions = {{-1, 0, 0, 1}, 1057 {-1, 0, 0, -2}}; 1058 SmallVector<unsigned, 8> denoms = {2, 3}; 1059 checkDivisionRepresentation(poly1, divisions, denoms); 1060 } 1061 1062 void expectRationalLexMin(const IntegerPolyhedron &poly, 1063 ArrayRef<Fraction> min) { 1064 auto lexMin = poly.findRationalLexMin(); 1065 ASSERT_TRUE(lexMin.isBounded()); 1066 EXPECT_EQ(ArrayRef<Fraction>(*lexMin), min); 1067 } 1068 1069 void expectNoRationalLexMin(OptimumKind kind, const IntegerPolyhedron &poly) { 1070 ASSERT_NE(kind, OptimumKind::Bounded) 1071 << "Use expectRationalLexMin for bounded min"; 1072 EXPECT_EQ(poly.findRationalLexMin().getKind(), kind); 1073 } 1074 1075 TEST(IntegerPolyhedronTest, findRationalLexMin) { 1076 expectRationalLexMin( 1077 parsePoly("(x, y, z) : (x + 10 >= 0, y + 40 >= 0, z + 30 >= 0)"), 1078 {{-10, 1}, {-40, 1}, {-30, 1}}); 1079 expectRationalLexMin( 1080 parsePoly( 1081 "(x, y, z) : (2*x + 7 >= 0, 3*y - 5 >= 0, 8*z + 10 >= 0, 9*z >= 0)"), 1082 {{-7, 2}, {5, 3}, {0, 1}}); 1083 expectRationalLexMin(parsePoly("(x, y) : (3*x + 2*y + 10 >= 0, -3*y + 10 >= " 1084 "0, 4*x - 7*y - 10 >= 0)"), 1085 {{-50, 29}, {-70, 29}}); 1086 1087 // Test with some locals. This is basically x >= 11, 0 <= x - 2e <= 1. 1088 // It'll just choose x = 11, e = 5.5 since it's rational lexmin. 1089 expectRationalLexMin( 1090 parsePoly( 1091 "(x, y) : (x - 2*(x floordiv 2) == 0, y - 2*x >= 0, x - 11 >= 0)"), 1092 {{11, 1}, {22, 1}}); 1093 1094 expectRationalLexMin(parsePoly("(x, y) : (3*x + 2*y + 10 >= 0," 1095 "-4*x + 7*y + 10 >= 0, -3*y + 10 >= 0)"), 1096 {{-50, 9}, {10, 3}}); 1097 1098 // Cartesian product of above with itself. 1099 expectRationalLexMin( 1100 parsePoly("(x, y, z, w) : (3*x + 2*y + 10 >= 0, -4*x + 7*y + 10 >= 0," 1101 "-3*y + 10 >= 0, 3*z + 2*w + 10 >= 0, -4*z + 7*w + 10 >= 0," 1102 "-3*w + 10 >= 0)"), 1103 {{-50, 9}, {10, 3}, {-50, 9}, {10, 3}}); 1104 1105 // Same as above but for the constraints on z and w, we express "10" in terms 1106 // of x and y. We know that x and y still have to take the values 1107 // -50/9 and 10/3 since their constraints are the same and their values are 1108 // minimized first. Accordingly, the values -9x - 12y, -9x - 0y - 10, 1109 // and -9x - 15y + 10 are all equal to 10. 1110 expectRationalLexMin( 1111 parsePoly( 1112 "(x, y, z, w) : (3*x + 2*y + 10 >= 0, -4*x + 7*y + 10 >= 0, " 1113 "-3*y + 10 >= 0, 3*z + 2*w - 9*x - 12*y >= 0," 1114 "-4*z + 7*w + - 9*x - 9*y - 10 >= 0, -3*w - 9*x - 15*y + 10 >= 0)"), 1115 {{-50, 9}, {10, 3}, {-50, 9}, {10, 3}}); 1116 1117 // Same as above with one constraint removed, making the lexmin unbounded. 1118 expectNoRationalLexMin( 1119 OptimumKind::Unbounded, 1120 parsePoly("(x, y, z, w) : (3*x + 2*y + 10 >= 0, -4*x + 7*y + 10 >= 0," 1121 "-3*y + 10 >= 0, 3*z + 2*w - 9*x - 12*y >= 0," 1122 "-4*z + 7*w + - 9*x - 9*y - 10>= 0)")); 1123 1124 // Again, the lexmin is unbounded. 1125 expectNoRationalLexMin( 1126 OptimumKind::Unbounded, 1127 parsePoly("(x, y, z) : (2*x + 5*y + 8*z - 10 >= 0," 1128 "2*x + 10*y + 8*z - 10 >= 0, 2*x + 5*y + 10*z - 10 >= 0)")); 1129 1130 // The set is empty. 1131 expectNoRationalLexMin(OptimumKind::Empty, 1132 parsePoly("(x) : (2*x >= 0, -x - 1 >= 0)")); 1133 } 1134 1135 void expectIntegerLexMin(const IntegerPolyhedron &poly, ArrayRef<int64_t> min) { 1136 auto lexMin = poly.findIntegerLexMin(); 1137 ASSERT_TRUE(lexMin.isBounded()); 1138 EXPECT_EQ(ArrayRef<int64_t>(*lexMin), min); 1139 } 1140 1141 void expectNoIntegerLexMin(OptimumKind kind, const IntegerPolyhedron &poly) { 1142 ASSERT_NE(kind, OptimumKind::Bounded) 1143 << "Use expectRationalLexMin for bounded min"; 1144 EXPECT_EQ(poly.findRationalLexMin().getKind(), kind); 1145 } 1146 1147 TEST(IntegerPolyhedronTest, findIntegerLexMin) { 1148 expectIntegerLexMin(parsePoly("(x, y, z) : (2*x + 13 >= 0, 4*y - 3*x - 2 >= " 1149 "0, 11*z + 5*y - 3*x + 7 >= 0)"), 1150 {-6, -4, 0}); 1151 // Similar to above but no lower bound on z. 1152 expectNoIntegerLexMin(OptimumKind::Unbounded, 1153 parsePoly("(x, y, z) : (2*x + 13 >= 0, 4*y - 3*x - 2 " 1154 ">= 0, -11*z + 5*y - 3*x + 7 >= 0)")); 1155 } 1156 1157 void expectSymbolicIntegerLexMin( 1158 StringRef polyStr, 1159 ArrayRef<std::pair<StringRef, SmallVector<SmallVector<int64_t, 8>, 8>>> 1160 expectedLexminRepr, 1161 ArrayRef<StringRef> expectedUnboundedDomainRepr) { 1162 IntegerPolyhedron poly = parsePoly(polyStr); 1163 1164 ASSERT_NE(poly.getNumDimVars(), 0u); 1165 ASSERT_NE(poly.getNumSymbolVars(), 0u); 1166 1167 PWMAFunction expectedLexmin = 1168 parsePWMAF(/*numInputs=*/poly.getNumSymbolVars(), 1169 /*numOutputs=*/poly.getNumDimVars(), expectedLexminRepr, 1170 /*numSymbols=*/poly.getNumSymbolVars()); 1171 1172 PresburgerSet expectedUnboundedDomain = parsePresburgerSetFromPolyStrings( 1173 /*numDims=*/0, expectedUnboundedDomainRepr, poly.getNumSymbolVars()); 1174 1175 SymbolicLexMin result = poly.findSymbolicIntegerLexMin(); 1176 1177 EXPECT_TRUE(result.lexmin.isEqual(expectedLexmin)); 1178 if (!result.lexmin.isEqual(expectedLexmin)) { 1179 llvm::errs() << "got:\n"; 1180 result.lexmin.dump(); 1181 llvm::errs() << "expected:\n"; 1182 expectedLexmin.dump(); 1183 } 1184 1185 EXPECT_TRUE(result.unboundedDomain.isEqual(expectedUnboundedDomain)); 1186 if (!result.unboundedDomain.isEqual(expectedUnboundedDomain)) 1187 result.unboundedDomain.dump(); 1188 } 1189 1190 void expectSymbolicIntegerLexMin( 1191 StringRef polyStr, 1192 ArrayRef<std::pair<StringRef, SmallVector<SmallVector<int64_t, 8>, 8>>> 1193 result) { 1194 expectSymbolicIntegerLexMin(polyStr, result, {}); 1195 } 1196 1197 TEST(IntegerPolyhedronTest, findSymbolicIntegerLexMin) { 1198 expectSymbolicIntegerLexMin("(x)[a] : (x - a >= 0)", 1199 { 1200 {"()[a] : ()", {{1, 0}}}, // a 1201 }); 1202 1203 expectSymbolicIntegerLexMin( 1204 "(x)[a, b] : (x - a >= 0, x - b >= 0)", 1205 { 1206 {"()[a, b] : (a - b >= 0)", {{1, 0, 0}}}, // a 1207 {"()[a, b] : (b - a - 1 >= 0)", {{0, 1, 0}}}, // b 1208 }); 1209 1210 expectSymbolicIntegerLexMin( 1211 "(x)[a, b, c] : (x -a >= 0, x - b >= 0, x - c >= 0)", 1212 { 1213 {"()[a, b, c] : (a - b >= 0, a - c >= 0)", {{1, 0, 0, 0}}}, // a 1214 {"()[a, b, c] : (b - a - 1 >= 0, b - c >= 0)", {{0, 1, 0, 0}}}, // b 1215 {"()[a, b, c] : (c - a - 1 >= 0, c - b - 1 >= 0)", 1216 {{0, 0, 1, 0}}}, // c 1217 }); 1218 1219 expectSymbolicIntegerLexMin("(x, y)[a] : (x - a >= 0, x + y >= 0)", 1220 { 1221 {"()[a] : ()", {{1, 0}, {-1, 0}}}, // (a, -a) 1222 }); 1223 1224 expectSymbolicIntegerLexMin( 1225 "(x, y)[a] : (x - a >= 0, x + y >= 0, y >= 0)", 1226 { 1227 {"()[a] : (a >= 0)", {{1, 0}, {0, 0}}}, // (a, 0) 1228 {"()[a] : (-a - 1 >= 0)", {{1, 0}, {-1, 0}}}, // (a, -a) 1229 }); 1230 1231 expectSymbolicIntegerLexMin( 1232 "(x, y)[a, b, c] : (x - a >= 0, y - b >= 0, c - x - y >= 0)", 1233 { 1234 {"()[a, b, c] : (c - a - b >= 0)", 1235 {{1, 0, 0, 0}, {0, 1, 0, 0}}}, // (a, b) 1236 }); 1237 1238 expectSymbolicIntegerLexMin( 1239 "(x, y, z)[a, b, c] : (c - z >= 0, b - y >= 0, x + y + z - a == 0)", 1240 { 1241 {"()[a, b, c] : ()", 1242 {{1, -1, -1, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}}}, // (a - b - c, b, c) 1243 }); 1244 1245 expectSymbolicIntegerLexMin( 1246 "(x)[a, b] : (a >= 0, b >= 0, x >= 0, a + b + x - 1 >= 0)", 1247 { 1248 {"()[a, b] : (a >= 0, b >= 0, a + b - 1 >= 0)", {{0, 0, 0}}}, // 0 1249 {"()[a, b] : (a == 0, b == 0)", {{0, 0, 1}}}, // 1 1250 }); 1251 1252 expectSymbolicIntegerLexMin( 1253 "(x)[a, b] : (1 - a >= 0, a >= 0, 1 - b >= 0, b >= 0, 1 - x >= 0, x >= " 1254 "0, a + b + x - 1 >= 0)", 1255 { 1256 {"()[a, b] : (1 - a >= 0, a >= 0, 1 - b >= 0, b >= 0, a + b - 1 >= " 1257 "0)", 1258 {{0, 0, 0}}}, // 0 1259 {"()[a, b] : (a == 0, b == 0)", {{0, 0, 1}}}, // 1 1260 }); 1261 1262 expectSymbolicIntegerLexMin( 1263 "(x, y, z)[a, b] : (x - a == 0, y - b == 0, x >= 0, y >= 0, z >= 0, x + " 1264 "y + z - 1 >= 0)", 1265 { 1266 {"()[a, b] : (a >= 0, b >= 0, 1 - a - b >= 0)", 1267 {{1, 0, 0}, {0, 1, 0}, {-1, -1, 1}}}, // (a, b, 1 - a - b) 1268 {"()[a, b] : (a >= 0, b >= 0, a + b - 2 >= 0)", 1269 {{1, 0, 0}, {0, 1, 0}, {0, 0, 0}}}, // (a, b, 0) 1270 }); 1271 1272 expectSymbolicIntegerLexMin("(x)[a, b] : (x - a == 0, x - b >= 0)", 1273 { 1274 {"()[a, b] : (a - b >= 0)", {{1, 0, 0}}}, // a 1275 }); 1276 1277 expectSymbolicIntegerLexMin( 1278 "(q)[a] : (a - 1 - 3*q == 0, q >= 0)", 1279 { 1280 {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)", 1281 {{0, 1, 0}}}, // a floordiv 3 1282 }); 1283 1284 expectSymbolicIntegerLexMin( 1285 "(r, q)[a] : (a - r - 3*q == 0, q >= 0, 1 - r >= 0, r >= 0)", 1286 { 1287 {"()[a] : (a - 0 - 3*(a floordiv 3) == 0, a >= 0)", 1288 {{0, 0, 0}, {0, 1, 0}}}, // (0, a floordiv 3) 1289 {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)", 1290 {{0, 0, 1}, {0, 1, 0}}}, // (1 a floordiv 3) 1291 }); 1292 1293 expectSymbolicIntegerLexMin( 1294 "(r, q)[a] : (a - r - 3*q == 0, q >= 0, 2 - r >= 0, r - 1 >= 0)", 1295 { 1296 {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)", 1297 {{0, 0, 1}, {0, 1, 0}}}, // (1, a floordiv 3) 1298 {"()[a] : (a - 2 - 3*(a floordiv 3) == 0, a >= 0)", 1299 {{0, 0, 2}, {0, 1, 0}}}, // (2, a floordiv 3) 1300 }); 1301 1302 expectSymbolicIntegerLexMin( 1303 "(r, q)[a] : (a - r - 3*q == 0, q >= 0, r >= 0)", 1304 { 1305 {"()[a] : (a - 3*(a floordiv 3) == 0, a >= 0)", 1306 {{0, 0, 0}, {0, 1, 0}}}, // (0, a floordiv 3) 1307 {"()[a] : (a - 1 - 3*(a floordiv 3) == 0, a >= 0)", 1308 {{0, 0, 1}, {0, 1, 0}}}, // (1, a floordiv 3) 1309 {"()[a] : (a - 2 - 3*(a floordiv 3) == 0, a >= 0)", 1310 {{0, 0, 2}, {0, 1, 0}}}, // (2, a floordiv 3) 1311 }); 1312 1313 expectSymbolicIntegerLexMin( 1314 "(x, y, z, w)[g] : (" 1315 // x, y, z, w are boolean variables. 1316 "1 - x >= 0, x >= 0, 1 - y >= 0, y >= 0," 1317 "1 - z >= 0, z >= 0, 1 - w >= 0, w >= 0," 1318 // We have some constraints on them: 1319 "x + y + z - 1 >= 0," // x or y or z 1320 "x + y + w - 1 >= 0," // x or y or w 1321 "1 - x + 1 - y + 1 - w - 1 >= 0," // ~x or ~y or ~w 1322 // What's the lexmin solution using exactly g true vars? 1323 "g - x - y - z - w == 0)", 1324 { 1325 {"()[g] : (g - 1 == 0)", 1326 {{0, 0}, {0, 1}, {0, 0}, {0, 0}}}, // (0, 1, 0, 0) 1327 {"()[g] : (g - 2 == 0)", 1328 {{0, 0}, {0, 0}, {0, 1}, {0, 1}}}, // (0, 0, 1, 1) 1329 {"()[g] : (g - 3 == 0)", 1330 {{0, 0}, {0, 1}, {0, 1}, {0, 1}}}, // (0, 1, 1, 1) 1331 }); 1332 1333 // Bezout's lemma: if a, b are constants, 1334 // the set of values that ax + by can take is all multiples of gcd(a, b). 1335 expectSymbolicIntegerLexMin( 1336 // If (x, y) is a solution for a given [a, r], then so is (x - 5, y + 2). 1337 // So the lexmin is unbounded if it exists. 1338 "(x, y)[a, r] : (a >= 0, r - a + 14*x + 35*y == 0)", {}, 1339 // According to Bezout's lemma, 14x + 35y can take on all multiples 1340 // of 7 and no other values. So the solution exists iff r - a is a 1341 // multiple of 7. 1342 {"()[a, r] : (a >= 0, r - a - 7*((r - a) floordiv 7) == 0)"}); 1343 1344 // The lexmins are unbounded. 1345 expectSymbolicIntegerLexMin("(x, y)[a] : (9*x - 4*y - 2*a >= 0)", {}, 1346 {"()[a] : ()"}); 1347 1348 // Test cases adapted from isl. 1349 expectSymbolicIntegerLexMin( 1350 // a = 2b - 2(c - b), c - b >= 0. 1351 // So b is minimized when c = b. 1352 "(b, c)[a] : (a - 4*b + 2*c == 0, c - b >= 0)", 1353 { 1354 {"()[a] : (a - 2*(a floordiv 2) == 0)", 1355 {{0, 1, 0}, {0, 1, 0}}}, // (a floordiv 2, a floordiv 2) 1356 }); 1357 1358 expectSymbolicIntegerLexMin( 1359 // 0 <= b <= 255, 1 <= a - 512b <= 509, 1360 // b + 8 >= 1 + 16*(b + 8 floordiv 16) // i.e. b % 16 != 8 1361 "(b)[a] : (255 - b >= 0, b >= 0, a - 512*b - 1 >= 0, 512*b -a + 509 >= " 1362 "0, b + 7 - 16*((8 + b) floordiv 16) >= 0)", 1363 { 1364 {"()[a] : (255 - (a floordiv 512) >= 0, a >= 0, a - 512*(a floordiv " 1365 "512) - 1 >= 0, 512*(a floordiv 512) - a + 509 >= 0, (a floordiv " 1366 "512) + 7 - 16*((8 + (a floordiv 512)) floordiv 16) >= 0)", 1367 {{0, 1, 0, 0}}}, // (a floordiv 2, a floordiv 2) 1368 }); 1369 1370 expectSymbolicIntegerLexMin( 1371 "(a, b)[K, N, x, y] : (N - K - 2 >= 0, K + 4 - N >= 0, x - 4 >= 0, x + 6 " 1372 "- 2*N >= 0, K+N - x - 1 >= 0, a - N + 1 >= 0, K+N-1-a >= 0,a + 6 - b - " 1373 "N >= 0, 2*N - 4 - a >= 0," 1374 "2*N - 3*K + a - b >= 0, 4*N - K + 1 - 3*b >= 0, b - N >= 0, a - x - 1 " 1375 ">= 0)", 1376 {{ 1377 "()[K, N, x, y] : (x + 6 - 2*N >= 0, 2*N - 5 - x >= 0, x + 1 -3*K + " 1378 "N " 1379 ">= 0, N + K - 2 - x >= 0, x - 4 >= 0)", 1380 {{0, 0, 1, 0, 1}, {0, 1, 0, 0, 0}} // (1 + x, N) 1381 }}); 1382 } 1383 1384 static void 1385 expectComputedVolumeIsValidOverapprox(const IntegerPolyhedron &poly, 1386 Optional<uint64_t> trueVolume, 1387 Optional<uint64_t> resultBound) { 1388 expectComputedVolumeIsValidOverapprox(poly.computeVolume(), trueVolume, 1389 resultBound); 1390 } 1391 1392 TEST(IntegerPolyhedronTest, computeVolume) { 1393 // 0 <= x <= 3 + 1/3, -5.5 <= y <= 2 + 3/5, 3 <= z <= 1.75. 1394 // i.e. 0 <= x <= 3, -5 <= y <= 2, 3 <= z <= 3 + 1/4. 1395 // So volume is 4 * 8 * 1 = 32. 1396 expectComputedVolumeIsValidOverapprox( 1397 parsePoly("(x, y, z) : (x >= 0, -3*x + 10 >= 0, 2*y + 11 >= 0," 1398 "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)"), 1399 /*trueVolume=*/32ull, /*resultBound=*/32ull); 1400 1401 // Same as above but y has bounds 2 + 1/5 <= y <= 2 + 3/5. So the volume is 1402 // zero. 1403 expectComputedVolumeIsValidOverapprox( 1404 parsePoly("(x, y, z) : (x >= 0, -3*x + 10 >= 0, 5*y - 11 >= 0," 1405 "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)"), 1406 /*trueVolume=*/0ull, /*resultBound=*/0ull); 1407 1408 // Now x is unbounded below but y still has no integer values. 1409 expectComputedVolumeIsValidOverapprox( 1410 parsePoly("(x, y, z) : (-3*x + 10 >= 0, 5*y - 11 >= 0," 1411 "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)"), 1412 /*trueVolume=*/0ull, /*resultBound=*/0ull); 1413 1414 // A diamond shape, 0 <= x + y <= 10, 0 <= x - y <= 10, 1415 // with vertices at (0, 0), (5, 5), (5, 5), (10, 0). 1416 // x and y can take 11 possible values so result computed is 11*11 = 121. 1417 expectComputedVolumeIsValidOverapprox( 1418 parsePoly("(x, y) : (x + y >= 0, -x - y + 10 >= 0, x - y >= 0," 1419 "-x + y + 10 >= 0)"), 1420 /*trueVolume=*/61ull, /*resultBound=*/121ull); 1421 1422 // Effectively the same diamond as above; constrain the variables to be even 1423 // and double the constant terms of the constraints. The algorithm can't 1424 // eliminate locals exactly, so the result is an overapproximation by 1425 // computing that x and y can take 21 possible values so result is 21*21 = 1426 // 441. 1427 expectComputedVolumeIsValidOverapprox( 1428 parsePoly("(x, y) : (x + y >= 0, -x - y + 20 >= 0, x - y >= 0," 1429 " -x + y + 20 >= 0, x - 2*(x floordiv 2) == 0," 1430 "y - 2*(y floordiv 2) == 0)"), 1431 /*trueVolume=*/61ull, /*resultBound=*/441ull); 1432 1433 // Unbounded polytope. 1434 expectComputedVolumeIsValidOverapprox( 1435 parsePoly("(x, y) : (2*x - y >= 0, y - 3*x >= 0)"), 1436 /*trueVolume=*/{}, /*resultBound=*/{}); 1437 } 1438 1439 TEST(IntegerPolyhedronTest, containsPointNoLocal) { 1440 IntegerPolyhedron poly1 = parsePoly("(x) : ((x floordiv 2) - x == 0)"); 1441 EXPECT_TRUE(poly1.containsPointNoLocal({0})); 1442 EXPECT_FALSE(poly1.containsPointNoLocal({1})); 1443 1444 IntegerPolyhedron poly2 = parsePoly( 1445 "(x) : (x - 2*(x floordiv 2) == 0, x - 4*(x floordiv 4) - 2 == 0)"); 1446 EXPECT_TRUE(poly2.containsPointNoLocal({6})); 1447 EXPECT_FALSE(poly2.containsPointNoLocal({4})); 1448 1449 IntegerPolyhedron poly3 = parsePoly("(x, y) : (2*x - y >= 0, y - 3*x >= 0)"); 1450 EXPECT_TRUE(poly3.containsPointNoLocal({0, 0})); 1451 EXPECT_FALSE(poly3.containsPointNoLocal({1, 0})); 1452 } 1453 1454 TEST(IntegerPolyhedronTest, truncateEqualityRegressionTest) { 1455 // IntegerRelation::truncate was truncating inequalities to the number of 1456 // equalities. 1457 IntegerRelation set(PresburgerSpace::getSetSpace(1)); 1458 IntegerRelation::CountsSnapshot snapshot = set.getCounts(); 1459 set.addEquality({1, 0}); 1460 set.truncate(snapshot); 1461 EXPECT_EQ(set.getNumEqualities(), 0u); 1462 } 1463