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