diff --git a/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp b/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp --- a/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp +++ b/mlir/unittests/Analysis/Presburger/IntegerPolyhedronTest.cpp @@ -170,47 +170,38 @@ TEST(IntegerPolyhedronTest, FindSampleTest) { // Bounded sets with only inequalities. - - MLIRContext context; - // 0 <= 7x <= 5 - checkSample(true, parsePoly("(x) : (7 * x >= 0, -7 * x + 5 >= 0)", &context)); + checkSample(true, parsePoly("(x) : (7 * x >= 0, -7 * x + 5 >= 0)")); // 1 <= 5x and 5x <= 4 (no solution). - checkSample(false, - parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)", &context)); + checkSample(false, parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)")); // 1 <= 5x and 5x <= 9 (solution: x = 1). - checkSample(true, - parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)", &context)); + checkSample(true, parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)")); // Bounded sets with equalities. // x >= 8 and 40 >= y and x = y. - checkSample(true, parsePoly("(x,y) : (x - 8 >= 0, -y + 40 >= 0, x - y == 0)", - &context)); + checkSample(true, + parsePoly("(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, parsePoly("(x,y,z) : (-x + 10 >= 0, -y + 10 >= 0, " - "z - 10 >= 0, x + 2 * y - 3 * z == 0)", - &context)); + "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, parsePoly("(x,y,z) : (-x + 10 >= 0, -y + 10 >= 0, " - "z - 11 >= 0, x + 2 * y - 3 * z == 0)", - &context)); + "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, - parsePoly("(q,r) : (r >= 0, -r + 3 >= 0, 4 * q + r - 7 == 0)", &context)); + checkSample(true, + parsePoly("(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, - parsePoly("(q,r) : (4 * q + r - 7 == 0, r == 0)", &context)); + checkSample(false, parsePoly("(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 @@ -219,8 +210,7 @@ // This is a triangle with vertices at (1/3, 0), (2/3, 0) and (10000, 10000). checkSample(true, parsePoly("(x,y) : (y >= 0, " "300000 * x - 299999 * y - 100000 >= 0, " - "-300000 * x + 299998 * y + 200000 >= 0)", - &context)); + "-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). @@ -243,8 +233,7 @@ parsePoly("(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)", - &context)); + "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). @@ -263,24 +252,20 @@ // This is a line segment from (0, 1/3) to (100, 100 + 1/3). checkSample( false, - parsePoly("(x,y) : (x >= 0, -x + 100 >= 0, 3 * x - 3 * y + 1 == 0)", - &context)); + parsePoly("(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, parsePoly("(x,y) : (x >= 0, -x + 100 >= 0, " - "3 * x - 3 * y + 2 >= 0, -3 * x + 3 * y - 1 >= 0)", - &context)); + "3 * x - 3 * y + 2 >= 0, -3 * x + 3 * y - 1 >= 0)")); checkSample(true, parsePoly("(x,y) : (2 * x >= 0, -2 * x + 99 >= 0, " - "2 * y >= 0, -2 * y + 99 >= 0)", - &context)); + "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, parsePoly("(x,y) : (300000 * x - 299999 * y - 100000 >= 0, " - "-300000 * x + 299998 * y + 200000 >= 0)", - &context)); + "-300000 * x + 299998 * y + 200000 >= 0)")); // Cartesian product of a tetrahedron and a 2D cone. // The tetrahedron has vertices at @@ -394,8 +379,7 @@ {}); checkSample(true, parsePoly("(x, y, z) : (2 * x - 1 >= 0, x - y - 1 == 0, " - "y - z == 0)", - &context)); + "y - z == 0)")); // Regression tests for the computation of dual coefficients. checkSample(false, parsePoly("(x, y, z) : (" @@ -403,42 +387,34 @@ "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)", - &context)); + "-7*x - 5*y - 9*z - 1 >= 0)")); checkSample(true, parsePoly("(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)", - &context)); + "9*x + 8*y - 9*z - 7 >= 0)")); } TEST(IntegerPolyhedronTest, IsIntegerEmptyTest) { - - MLIRContext context; - // 1 <= 5x and 5x <= 4 (no solution). - EXPECT_TRUE(parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)", &context) - .isIntegerEmpty()); + EXPECT_TRUE( + parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 4 >= 0)").isIntegerEmpty()); // 1 <= 5x and 5x <= 9 (solution: x = 1). - EXPECT_FALSE(parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)", &context) - .isIntegerEmpty()); + EXPECT_FALSE( + parsePoly("(x) : (5 * x - 1 >= 0, -5 * x + 9 >= 0)").isIntegerEmpty()); // Unbounded sets. EXPECT_TRUE(parsePoly("(x,y,z) : (2 * y - 1 >= 0, -2 * y + 1 >= 0, " - "2 * z - 1 >= 0, 2 * x - 1 == 0)", - &context) + "2 * z - 1 >= 0, 2 * x - 1 == 0)") .isIntegerEmpty()); EXPECT_FALSE(parsePoly("(x,y,z) : (2 * x - 1 >= 0, -3 * x + 3 >= 0, " - "5 * z - 6 >= 0, -7 * z + 17 >= 0, 3 * y - 2 >= 0)", - &context) + "5 * z - 6 >= 0, -7 * z + 17 >= 0, 3 * y - 2 >= 0)") .isIntegerEmpty()); EXPECT_FALSE( - parsePoly("(x,y,z) : (2 * x - 1 >= 0, x - y - 1 == 0, y - z == 0)", - &context) + parsePoly("(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 @@ -447,8 +423,7 @@ // 3x + 7y = 1 and 0 <= x, y <= 10. // Since x and y are non-negative, 3x + 7y can never be 1. EXPECT_TRUE(parsePoly("(x,y) : (x >= 0, -x + 10 >= 0, y >= 0, -y + 10 >= 0, " - "3 * x + 7 * y - 1 == 0)", - &context) + "3 * x + 7 * y - 1 == 0)") .isIntegerEmpty()); // 2x = 3y and y = x - 1 and x + y = 6z + 2 and 0 <= x, y <= 100. @@ -456,8 +431,7 @@ // Since x + y = 5 cannot be equal to 6z + 2 for any z, the set is empty. EXPECT_TRUE( parsePoly("(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)", - &context) + "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. @@ -468,20 +442,16 @@ EXPECT_TRUE( parsePoly( "(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)", - &context) + "2 * x - 3 * y == 0, x - y + 6 * z - 1 == 0, x + y - 6 * q - 2 == 0)") .isIntegerEmpty()); // Set with symbols. - EXPECT_FALSE(parsePoly("(x)[s] : (x + s >= 0, x - s == 0)", &context) - .isIntegerEmpty()); + EXPECT_FALSE(parsePoly("(x)[s] : (x + s >= 0, x - s == 0)").isIntegerEmpty()); } TEST(IntegerPolyhedronTest, removeRedundantConstraintsTest) { - MLIRContext context; - IntegerPolyhedron poly = - parsePoly("(x) : (x - 2 >= 0, -x + 2 >= 0, x - 2 == 0)", &context); + parsePoly("(x) : (x - 2 >= 0, -x + 2 >= 0, x - 2 == 0)"); poly.removeRedundantConstraints(); // Both inequalities are redundant given the equality. Both have been removed. @@ -489,7 +459,7 @@ EXPECT_EQ(poly.getNumEqualities(), 1u); IntegerPolyhedron poly2 = - parsePoly("(x,y) : (x - 3 >= 0, y - 2 >= 0, x - y == 0)", &context); + parsePoly("(x,y) : (x - 3 >= 0, y - 2 >= 0, x - y == 0)"); poly2.removeRedundantConstraints(); // The second inequality is redundant and should have been removed. The @@ -499,7 +469,7 @@ EXPECT_EQ(poly2.getNumEqualities(), 1u); IntegerPolyhedron poly3 = - parsePoly("(x,y,z) : (x - y == 0, x - z == 0, y - z == 0)", &context); + parsePoly("(x,y,z) : (x - y == 0, x - z == 0, y - z == 0)"); poly3.removeRedundantConstraints(); // One of the three equalities can be removed. @@ -544,8 +514,7 @@ "-c + 10 >= 0," "a - 13 >= 0," "-a + 13 >= 0" - ")", - &context); + ")"); // The above is a large set of constraints without any redundant constraints, // as verified by the Fourier-Motzkin based removeRedundantInequalities. @@ -561,8 +530,7 @@ EXPECT_EQ(poly4.getNumEqualities(), nEq); IntegerPolyhedron poly5 = parsePoly( - "(x,y) : (128 * x + 127 >= 0, -x + 7 >= 0, -128 * x + y >= 0, y >= 0)", - &context); + "(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 @@ -612,7 +580,6 @@ IntegerPolyhedron &poly, const std::vector> &expectedDividends, const SmallVectorImpl &expectedDenominators) { - std::vector> dividends; SmallVector denominators; @@ -689,10 +656,8 @@ } TEST(IntegerPolyhedronTest, computeLocalReprTightUpperBound) { - MLIRContext context; - { - IntegerPolyhedron poly = parsePoly("(i) : (i mod 3 - 1 >= 0)", &context); + IntegerPolyhedron poly = parsePoly("(i) : (i mod 3 - 1 >= 0)"); // The set formed by the poly is: // 3q - i + 2 >= 0 <-- Division lower bound @@ -712,8 +677,8 @@ } { - IntegerPolyhedron poly = parsePoly( - "(i, j, q) : (4*q - i - j + 2 >= 0, -4*q + i + j >= 0)", &context); + IntegerPolyhedron poly = + parsePoly("(i, j, q) : (4*q - i - j + 2 >= 0, -4*q + i + j >= 0)"); // Convert `q` to a local variable. poly.convertDimToLocal(2, 3); @@ -726,10 +691,8 @@ } TEST(IntegerPolyhedronTest, computeLocalReprFromEquality) { - MLIRContext context; { - IntegerPolyhedron poly = - parsePoly("(i, j, q) : (-4*q + i + j == 0)", &context); + IntegerPolyhedron poly = parsePoly("(i, j, q) : (-4*q + i + j == 0)"); // Convert `q` to a local variable. poly.convertDimToLocal(2, 3); @@ -739,8 +702,7 @@ checkDivisionRepresentation(poly, divisions, denoms); } { - IntegerPolyhedron poly = - parsePoly("(i, j, q) : (4*q - i - j == 0)", &context); + IntegerPolyhedron poly = parsePoly("(i, j, q) : (4*q - i - j == 0)"); // Convert `q` to a local variable. poly.convertDimToLocal(2, 3); @@ -750,8 +712,7 @@ checkDivisionRepresentation(poly, divisions, denoms); } { - IntegerPolyhedron poly = - parsePoly("(i, j, q) : (3*q + i + j - 2 == 0)", &context); + IntegerPolyhedron poly = parsePoly("(i, j, q) : (3*q + i + j - 2 == 0)"); // Convert `q` to a local variable. poly.convertDimToLocal(2, 3); @@ -763,12 +724,10 @@ } TEST(IntegerPolyhedronTest, computeLocalReprFromEqualityAndInequality) { - MLIRContext context; { IntegerPolyhedron poly = parsePoly("(i, j, q, k) : (-3*k + i + j == 0, 4*q - " - "i - j + 2 >= 0, -4*q + i + j >= 0)", - &context); + "i - j + 2 >= 0, -4*q + i + j >= 0)"); // Convert `q` and `k` to local variables. poly.convertDimToLocal(2, 4); @@ -781,9 +740,8 @@ } TEST(IntegerPolyhedronTest, computeLocalReprNoRepr) { - MLIRContext context; IntegerPolyhedron poly = - parsePoly("(x, q) : (x - 3 * q >= 0, -x + 3 * q + 3 >= 0)", &context); + parsePoly("(x, q) : (x - 3 * q >= 0, -x + 3 * q + 3 >= 0)"); // Convert q to a local variable. poly.convertDimToLocal(1, 2); @@ -796,8 +754,8 @@ TEST(IntegerPolyhedronTest, computeLocalReprNegConstNormalize) { MLIRContext context; - IntegerPolyhedron poly = parsePoly( - "(x, q) : (-1 - 3*x - 6 * q >= 0, 6 + 3*x + 6*q >= 0)", &context); + IntegerPolyhedron poly = + parsePoly("(x, q) : (-1 - 3*x - 6 * q >= 0, 6 + 3*x + 6*q >= 0)"); // Convert q to a local variable. poly.convertDimToLocal(1, 2); @@ -1068,41 +1026,33 @@ } TEST(IntegerPolyhedronTest, getRationalLexMin) { - MLIRContext context; expectRationalLexMin( - parsePoly("(x, y, z) : (x + 10 >= 0, y + 40 >= 0, z + 30 >= 0)", - &context), + parsePoly("(x, y, z) : (x + 10 >= 0, y + 40 >= 0, z + 30 >= 0)"), {{-10, 1}, {-40, 1}, {-30, 1}}); expectRationalLexMin( parsePoly( - "(x, y, z) : (2*x + 7 >= 0, 3*y - 5 >= 0, 8*z + 10 >= 0, 9*z >= 0)", - &context), + "(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( - parsePoly( - "(x, y) : (3*x + 2*y + 10 >= 0, -3*y + 10 >= 0, 4*x - 7*y - 10 >= 0)", - &context), - {{-50, 29}, {-70, 29}}); + expectRationalLexMin(parsePoly("(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( parsePoly( - "(x, y) : (x - 2*(x floordiv 2) == 0, y - 2*x >= 0, x - 11 >= 0)", - &context), + "(x, y) : (x - 2*(x floordiv 2) == 0, y - 2*x >= 0, x - 11 >= 0)"), {{11, 1}, {22, 1}}); expectRationalLexMin(parsePoly("(x, y) : (3*x + 2*y + 10 >= 0," - "-4*x + 7*y + 10 >= 0, -3*y + 10 >= 0)", - &context), + "-4*x + 7*y + 10 >= 0, -3*y + 10 >= 0)"), {{-50, 9}, {10, 3}}); // Cartesian product of above with itself. expectRationalLexMin( parsePoly("(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)", - &context), + "-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 @@ -1114,8 +1064,7 @@ parsePoly( "(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)", - &context), + "-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. @@ -1123,19 +1072,17 @@ OptimumKind::Unbounded, parsePoly("(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)", - &context)); + "-4*z + 7*w + - 9*x - 9*y - 10>= 0)")); // Again, the lexmin is unbounded. expectNoRationalLexMin( OptimumKind::Unbounded, parsePoly("(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)", - &context)); + "2*x + 10*y + 8*z - 10 >= 0, 2*x + 5*y + 10*z - 10 >= 0)")); // The set is empty. expectNoRationalLexMin(OptimumKind::Empty, - parsePoly("(x) : (2*x >= 0, -x - 1 >= 0)", &context)); + parsePoly("(x) : (2*x >= 0, -x - 1 >= 0)")); } static void @@ -1147,30 +1094,25 @@ } TEST(IntegerPolyhedronTest, computeVolume) { - MLIRContext context; - // 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( parsePoly("(x, y, z) : (x >= 0, -3*x + 10 >= 0, 2*y + 11 >= 0," - "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)", - &context), + "-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( parsePoly("(x, y, z) : (x >= 0, -3*x + 10 >= 0, 5*y - 11 >= 0," - "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)", - &context), + "-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( parsePoly("(x, y, z) : (-3*x + 10 >= 0, 5*y - 11 >= 0," - "-5*y + 13 >= 0, z - 3 >= 0, -4*z + 13 >= 0)", - &context), + "-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, @@ -1178,8 +1120,7 @@ // x and y can take 11 possible values so result computed is 11*11 = 121. expectComputedVolumeIsValidOverapprox( parsePoly("(x, y) : (x + y >= 0, -x - y + 10 >= 0, x - y >= 0," - "-x + y + 10 >= 0)", - &context), + "-x + y + 10 >= 0)"), /*trueVolume=*/61ull, /*resultBound=*/121ull); // Effectively the same diamond as above; constrain the variables to be even @@ -1190,13 +1131,12 @@ expectComputedVolumeIsValidOverapprox( parsePoly("(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)", - &context), + "y - 2*(y floordiv 2) == 0)"), /*trueVolume=*/61ull, /*resultBound=*/441ull); // Unbounded polytope. expectComputedVolumeIsValidOverapprox( - parsePoly("(x, y) : (2*x - y >= 0, y - 3*x >= 0)", &context), + parsePoly("(x, y) : (2*x - y >= 0, y - 3*x >= 0)"), /*trueVolume=*/{}, /*resultBound=*/{}); } diff --git a/mlir/unittests/Analysis/Presburger/PWMAFunctionTest.cpp b/mlir/unittests/Analysis/Presburger/PWMAFunctionTest.cpp --- a/mlir/unittests/Analysis/Presburger/PWMAFunctionTest.cpp +++ b/mlir/unittests/Analysis/Presburger/PWMAFunctionTest.cpp @@ -43,11 +43,9 @@ ArrayRef, 8>>> data, unsigned numSymbols = 0) { - static MLIRContext context; - PWMAFunction result(numInputs - numSymbols, numSymbols, numOutputs); for (const auto &pair : data) { - IntegerPolyhedron domain = parsePoly(pair.first, &context); + IntegerPolyhedron domain = parsePoly(pair.first); result.addPiece( domain, makeMatrix(numOutputs, domain.getNumIds() + 1, pair.second)); } diff --git a/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp b/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp --- a/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp +++ b/mlir/unittests/Analysis/Presburger/PresburgerSetTest.cpp @@ -22,17 +22,15 @@ #include namespace mlir { - /// Parse a list of StringRefs to IntegerPolyhedron and combine them into a /// PresburgerSet be using the union operation. It is expected that the strings /// are all valid IntegerSet representation and that all of them have the same /// number of dimensions as is specified by the numDims argument. -static PresburgerSet parsePresburgerSetFromPolyStrings(unsigned numDims, - ArrayRef strs, - MLIRContext *context) { +static PresburgerSet +parsePresburgerSetFromPolyStrings(unsigned numDims, ArrayRef strs) { PresburgerSet set = PresburgerSet::getEmptySet(numDims); for (StringRef str : strs) - set.unionPolyInPlace(parsePoly(str, context)); + set.unionPolyInPlace(parsePoly(str)); return set; } @@ -108,12 +106,9 @@ } TEST(SetTest, containsPoint) { - MLIRContext context; - PresburgerSet setA = parsePresburgerSetFromPolyStrings( 1, - {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}, - &context); + {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}); for (unsigned x = 0; x <= 21; ++x) { if ((2 <= x && x <= 8) || (10 <= x && x <= 20)) EXPECT_TRUE(setA.containsPoint({x})); @@ -124,11 +119,9 @@ // A parallelogram with vertices {(3, 1), (10, -6), (24, 8), (17, 15)} union // a square with opposite corners (2, 2) and (10, 10). PresburgerSet setB = parsePresburgerSetFromPolyStrings( - 2, - {"(x,y) : (x + y - 4 >= 0, -x - y + 32 >= 0, " - "x - y - 2 >= 0, -x + y + 16 >= 0)", - "(x,y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, -y + 10 >= 0)"}, - &context); + 2, {"(x,y) : (x + y - 4 >= 0, -x - y + 32 >= 0, " + "x - y - 2 >= 0, -x + y + 16 >= 0)", + "(x,y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, -y + 10 >= 0)"}); for (unsigned x = 1; x <= 25; ++x) { for (unsigned y = -6; y <= 16; ++y) { @@ -143,12 +136,9 @@ } TEST(SetTest, Union) { - MLIRContext context; - PresburgerSet set = parsePresburgerSetFromPolyStrings( 1, - {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}, - &context); + {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}); // Universe union set. testUnionAtPoints(PresburgerSet::getUniverse(1), set, @@ -172,12 +162,9 @@ } TEST(SetTest, Intersect) { - MLIRContext context; - PresburgerSet set = parsePresburgerSetFromPolyStrings( 1, - {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}, - &context); + {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}); // Universe intersection set. testIntersectAtPoints(PresburgerSet::getUniverse(1), set, @@ -201,40 +188,33 @@ } TEST(SetTest, Subtract) { - MLIRContext context; // The interval [2, 8] minus the interval [10, 20]. - testSubtractAtPoints(parsePresburgerSetFromPolyStrings( - 1, {"(x) : (x - 2 >= 0, -x + 8 >= 0)"}, &context), - parsePresburgerSetFromPolyStrings( - 1, {"(x) : (x - 10 >= 0, -x + 20 >= 0)"}, &context), - {{1}, {2}, {8}, {9}, {10}, {20}, {21}}); - - // Universe minus [2, 8] U [10, 20] testSubtractAtPoints( - parsePresburgerSetFromPolyStrings(1, {"(x) : ()"}, &context), + parsePresburgerSetFromPolyStrings(1, {"(x) : (x - 2 >= 0, -x + 8 >= 0)"}), parsePresburgerSetFromPolyStrings(1, - {"(x) : (x - 2 >= 0, -x + 8 >= 0)", - "(x) : (x - 10 >= 0, -x + 20 >= 0)"}, - &context), + {"(x) : (x - 10 >= 0, -x + 20 >= 0)"}), {{1}, {2}, {8}, {9}, {10}, {20}, {21}}); + // Universe minus [2, 8] U [10, 20] + testSubtractAtPoints(parsePresburgerSetFromPolyStrings(1, {"(x) : ()"}), + parsePresburgerSetFromPolyStrings( + 1, {"(x) : (x - 2 >= 0, -x + 8 >= 0)", + "(x) : (x - 10 >= 0, -x + 20 >= 0)"}), + {{1}, {2}, {8}, {9}, {10}, {20}, {21}}); + // ((-infinity, 0] U [3, 4] U [6, 7]) - ([2, 3] U [5, 6]) testSubtractAtPoints( - parsePresburgerSetFromPolyStrings(1, - {"(x) : (-x >= 0)", - "(x) : (x - 3 >= 0, -x + 4 >= 0)", - "(x) : (x - 6 >= 0, -x + 7 >= 0)"}, - &context), - parsePresburgerSetFromPolyStrings(1, - {"(x) : (x - 2 >= 0, -x + 3 >= 0)", - "(x) : (x - 5 >= 0, -x + 6 >= 0)"}, - &context), + parsePresburgerSetFromPolyStrings(1, {"(x) : (-x >= 0)", + "(x) : (x - 3 >= 0, -x + 4 >= 0)", + "(x) : (x - 6 >= 0, -x + 7 >= 0)"}), + parsePresburgerSetFromPolyStrings(1, {"(x) : (x - 2 >= 0, -x + 3 >= 0)", + "(x) : (x - 5 >= 0, -x + 6 >= 0)"}), {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}}); // Expected result is {[x, y] : x > y}, i.e., {[x, y] : x >= y + 1}. testSubtractAtPoints( - parsePresburgerSetFromPolyStrings(2, {"(x, y) : (x - y >= 0)"}, &context), - parsePresburgerSetFromPolyStrings(2, {"(x, y) : (x + y >= 0)"}, &context), + parsePresburgerSetFromPolyStrings(2, {"(x, y) : (x - y >= 0)"}), + parsePresburgerSetFromPolyStrings(2, {"(x, y) : (x + y >= 0)"}), {{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}}); // A rectangle with corners at (2, 2) and (10, 10), minus @@ -246,14 +226,12 @@ 2, { "(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, -y + 10 >= 0)", - }, - &context), + }), parsePresburgerSetFromPolyStrings( 2, { "(x, y) : (x - 5 >= 0, y + 10 >= 0, -x + 7 >= 0, -y + 100 >= 0)", - }, - &context), + }), {{1, 2}, {2, 2}, {4, 2}, {5, 2}, {7, 2}, {8, 2}, {11, 2}, {1, 1}, {2, 1}, {4, 1}, {5, 1}, {7, 1}, {8, 1}, {11, 1}, {1, 10}, {2, 10}, {4, 10}, {5, 10}, {7, 10}, {8, 10}, {11, 10}, @@ -265,14 +243,12 @@ // resulting set can be represented as a union of four rectangles. testSubtractAtPoints( parsePresburgerSetFromPolyStrings( - 2, {"(x, y) : (x - 2 >= 0, y -2 >= 0, -x + 10 >= 0, -y + 10 >= 0)"}, - &context), + 2, {"(x, y) : (x - 2 >= 0, y -2 >= 0, -x + 10 >= 0, -y + 10 >= 0)"}), parsePresburgerSetFromPolyStrings( 2, { "(x, y) : (x - 5 >= 0, y - 4 >= 0, -x + 7 >= 0, -y + 8 >= 0)", - }, - &context), + }), {{1, 1}, {2, 2}, {10, 10}, @@ -288,25 +264,24 @@ // The second set is a superset of the first one, since on the line x + y = 0, // y <= 1 is equivalent to x >= -1. So the result is empty. - testSubtractAtPoints(parsePresburgerSetFromPolyStrings( - 2, {"(x, y) : (x >= 0, x + y == 0)"}, &context), - parsePresburgerSetFromPolyStrings( - 2, {"(x, y) : (-y + 1 >= 0, x + y == 0)"}, &context), - {{0, 0}, - {1, -1}, - {2, -2}, - {-1, 1}, - {-2, 2}, - {1, 1}, - {-1, -1}, - {-1, 1}, - {1, -1}}); + testSubtractAtPoints( + parsePresburgerSetFromPolyStrings(2, {"(x, y) : (x >= 0, x + y == 0)"}), + parsePresburgerSetFromPolyStrings(2, + {"(x, y) : (-y + 1 >= 0, x + y == 0)"}), + {{0, 0}, + {1, -1}, + {2, -2}, + {-1, 1}, + {-2, 2}, + {1, 1}, + {-1, -1}, + {-1, 1}, + {1, -1}}); // The result should be {0} U {2}. testSubtractAtPoints( - parsePresburgerSetFromPolyStrings(1, {"(x) : (x >= 0, -x + 2 >= 0)"}, - &context), - parsePresburgerSetFromPolyStrings(1, {"(x) : (x - 1 == 0)"}, &context), + parsePresburgerSetFromPolyStrings(1, {"(x) : (x >= 0, -x + 2 >= 0)"}), + parsePresburgerSetFromPolyStrings(1, {"(x) : (x - 1 == 0)"}), {{-1}, {0}, {1}, {2}, {3}}); // Sets with lots of redundant inequalities to test the redundancy heuristic. @@ -321,14 +296,11 @@ { "(x, y) : (x + y - 4 >= 0, -x - y + 32 >= 0, x - y - 2 >= 0, " "-x + y + 16 >= 0)", - }, - &context), + }), parsePresburgerSetFromPolyStrings( - 2, - {"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, " - "-y + 10 >= 0, x + y - 2 >= 0, -x - y + 30 >= 0, x - y >= 0, " - "-x + y + 10 >= 0)"}, - &context), + 2, {"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 10 >= 0, " + "-y + 10 >= 0, x + y - 2 >= 0, -x - y + 30 >= 0, x - y >= 0, " + "-x + y + 10 >= 0)"}), {{1, 2}, {2, 2}, {3, 2}, {4, 2}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {10, 2}, {11, 2}, {10, 1}, {10, 10}, {10, 11}, {10, 9}, {11, 10}, {10, -6}, {11, -6}, @@ -338,19 +310,15 @@ // 7]) testSubtractAtPoints( parsePresburgerSetFromPolyStrings( - 1, - {"(x) : (-x - 5 >= 0)", "(x) : (x - 3 == 0)", "(x) : (x - 4 == 0)", - "(x) : (x - 5 == 0)"}, - &context), + 1, {"(x) : (-x - 5 >= 0)", "(x) : (x - 3 == 0)", "(x) : (x - 4 == 0)", + "(x) : (x - 5 == 0)"}), parsePresburgerSetFromPolyStrings( - 1, - {"(x) : (-x - 2 >= 0, x - 10 >= 0, -x >= 0, -x + 10 >= 0, " - "x - 100 >= 0, x - 50 >= 0)", - "(x) : (x - 3 >= 0, -x + 4 >= 0, x + 1 >= 0, " - "x + 7 >= 0, -x + 10 >= 0)", - "(x) : (x - 6 >= 0, -x + 7 >= 0, x + 1 >= 0, x - 3 >= 0, " - "-x + 5 >= 0)"}, - &context), + 1, {"(x) : (-x - 2 >= 0, x - 10 >= 0, -x >= 0, -x + 10 >= 0, " + "x - 100 >= 0, x - 50 >= 0)", + "(x) : (x - 3 >= 0, -x + 4 >= 0, x + 1 >= 0, " + "x + 7 >= 0, -x + 10 >= 0)", + "(x) : (x - 6 >= 0, -x + 7 >= 0, x + 1 >= 0, x - 3 >= 0, " + "-x + 5 >= 0)"}), {{-6}, {-5}, {-4}, @@ -369,8 +337,6 @@ } TEST(SetTest, Complement) { - - MLIRContext context; // Complement of universe. testComplementAtPoints( PresburgerSet::getUniverse(1), @@ -382,10 +348,8 @@ {{-1}, {-2}, {-8}, {1}, {2}, {8}, {9}, {10}, {20}, {21}}); testComplementAtPoints( - parsePresburgerSetFromPolyStrings(2, - {"(x,y) : (x - 2 >= 0, y - 2 >= 0, " - "-x + 10 >= 0, -y + 10 >= 0)"}, - &context), + parsePresburgerSetFromPolyStrings(2, {"(x,y) : (x - 2 >= 0, y - 2 >= 0, " + "-x + 10 >= 0, -y + 10 >= 0)"}), {{1, 1}, {2, 1}, {1, 2}, @@ -401,15 +365,12 @@ } TEST(SetTest, isEqual) { - - MLIRContext context; // set = [2, 8] U [10, 20]. PresburgerSet universe = PresburgerSet::getUniverse(1); PresburgerSet emptySet = PresburgerSet::getEmptySet(1); PresburgerSet set = parsePresburgerSetFromPolyStrings( 1, - {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}, - &context); + {"(x) : (x - 2 >= 0, -x + 8 >= 0)", "(x) : (x - 10 >= 0, -x + 20 >= 0)"}); // universe != emptySet. EXPECT_FALSE(universe.isEqual(emptySet)); @@ -446,11 +407,9 @@ // square is one unit taller than rect. PresburgerSet square = parsePresburgerSetFromPolyStrings( - 2, {"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 9 >= 0, -y + 9 >= 0)"}, - &context); + 2, {"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 9 >= 0, -y + 9 >= 0)"}); PresburgerSet rect = parsePresburgerSetFromPolyStrings( - 2, {"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 9 >= 0, -y + 8 >= 0)"}, - &context); + 2, {"(x, y) : (x - 2 >= 0, y - 2 >= 0, -x + 9 >= 0, -y + 8 >= 0)"}); EXPECT_FALSE(square.isEqual(rect)); PresburgerSet universeRect = square.unionSet(square.complement()); PresburgerSet universeSquare = rect.unionSet(rect.complement()); @@ -467,23 +426,17 @@ void expectEmpty(const PresburgerSet &s) { EXPECT_TRUE(s.isIntegerEmpty()); } TEST(SetTest, divisions) { - MLIRContext context; - // evens = {x : exists q, x = 2q}. - PresburgerSet evens{ - parsePoly("(x) : (x - 2 * (x floordiv 2) == 0)", &context)}; + PresburgerSet evens{parsePoly("(x) : (x - 2 * (x floordiv 2) == 0)")}; // odds = {x : exists q, x = 2q + 1}. - PresburgerSet odds{ - parsePoly("(x) : (x - 2 * (x floordiv 2) - 1 == 0)", &context)}; + PresburgerSet odds{parsePoly("(x) : (x - 2 * (x floordiv 2) - 1 == 0)")}; // multiples3 = {x : exists q, x = 3q}. - PresburgerSet multiples3{ - parsePoly("(x) : (x - 3 * (x floordiv 3) == 0)", &context)}; + PresburgerSet multiples3{parsePoly("(x) : (x - 3 * (x floordiv 3) == 0)")}; // multiples6 = {x : exists q, x = 6q}. - PresburgerSet multiples6{ - parsePoly("(x) : (x - 6 * (x floordiv 6) == 0)", &context)}; + PresburgerSet multiples6{parsePoly("(x) : (x - 6 * (x floordiv 6) == 0)")}; // evens /\ odds = empty. expectEmpty(PresburgerSet(evens).intersect(PresburgerSet(odds))); @@ -494,8 +447,8 @@ // even multiples of 3 = multiples of 6. expectEqual(multiples3.intersect(evens), multiples6); - PresburgerSet setA{parsePoly("(x) : (-x >= 0)", &context)}; - PresburgerSet setB{parsePoly("(x) : (x floordiv 2 - 4 >= 0)", &context)}; + PresburgerSet setA{parsePoly("(x) : (-x >= 0)")}; + PresburgerSet setB{parsePoly("(x) : (x floordiv 2 - 4 >= 0)")}; EXPECT_TRUE(setA.subtract(setB).isEqual(setA)); } @@ -514,185 +467,143 @@ } TEST(SetTest, coalesceContainedOneDim) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : (x >= 0, -x + 4 >= 0)", "(x) : (x - 1 >= 0, -x + 2 >= 0)"}, - &context); + 1, {"(x) : (x >= 0, -x + 4 >= 0)", "(x) : (x - 1 >= 0, -x + 2 >= 0)"}); expectCoalesce(1, set); } TEST(SetTest, coalesceFirstEmpty) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : ( x >= 0, -x - 1 >= 0)", "(x) : ( x - 1 >= 0, -x + 2 >= 0)"}, - &context); + 1, {"(x) : ( x >= 0, -x - 1 >= 0)", "(x) : ( x - 1 >= 0, -x + 2 >= 0)"}); expectCoalesce(1, set); } TEST(SetTest, coalesceSecondEmpty) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : (x - 1 >= 0, -x + 2 >= 0)", "(x) : (x >= 0, -x - 1 >= 0)"}, - &context); + 1, {"(x) : (x - 1 >= 0, -x + 2 >= 0)", "(x) : (x >= 0, -x - 1 >= 0)"}); expectCoalesce(1, set); } TEST(SetTest, coalesceBothEmpty) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : (x - 3 >= 0, -x - 1 >= 0)", "(x) : (x >= 0, -x - 1 >= 0)"}, - &context); + 1, {"(x) : (x - 3 >= 0, -x - 1 >= 0)", "(x) : (x >= 0, -x - 1 >= 0)"}); expectCoalesce(0, set); } TEST(SetTest, coalesceFirstUniv) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : ()", "(x) : ( x >= 0, -x + 1 >= 0)"}, &context); + 1, {"(x) : ()", "(x) : ( x >= 0, -x + 1 >= 0)"}); expectCoalesce(1, set); } TEST(SetTest, coalesceSecondUniv) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : ( x >= 0, -x + 1 >= 0)", "(x) : ()"}, &context); + 1, {"(x) : ( x >= 0, -x + 1 >= 0)", "(x) : ()"}); expectCoalesce(1, set); } TEST(SetTest, coalesceBothUniv) { - MLIRContext context; PresburgerSet set = - parsePresburgerSetFromPolyStrings(1, {"(x) : ()", "(x) : ()"}, &context); + parsePresburgerSetFromPolyStrings(1, {"(x) : ()", "(x) : ()"}); expectCoalesce(1, set); } TEST(SetTest, coalesceFirstUnivSecondEmpty) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : ()", "(x) : ( x >= 0, -x - 1 >= 0)"}, &context); + 1, {"(x) : ()", "(x) : ( x >= 0, -x - 1 >= 0)"}); expectCoalesce(1, set); } TEST(SetTest, coalesceFirstEmptySecondUniv) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : ( x >= 0, -x - 1 >= 0)", "(x) : ()"}, &context); + 1, {"(x) : ( x >= 0, -x - 1 >= 0)", "(x) : ()"}); expectCoalesce(1, set); } TEST(SetTest, coalesceCutOneDim) { - MLIRContext context; - PresburgerSet set = - parsePresburgerSetFromPolyStrings(1, - { - "(x) : ( x >= 0, -x + 3 >= 0)", - "(x) : ( x - 2 >= 0, -x + 4 >= 0)", - }, - &context); + PresburgerSet set = parsePresburgerSetFromPolyStrings( + 1, { + "(x) : ( x >= 0, -x + 3 >= 0)", + "(x) : ( x - 2 >= 0, -x + 4 >= 0)", + }); expectCoalesce(2, set); } TEST(SetTest, coalesceSeparateOneDim) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 1, {"(x) : ( x >= 0, -x + 2 >= 0)", "(x) : ( x - 3 >= 0, -x + 4 >= 0)"}, - &context); + 1, {"(x) : ( x >= 0, -x + 2 >= 0)", "(x) : ( x - 3 >= 0, -x + 4 >= 0)"}); expectCoalesce(2, set); } TEST(SetTest, coalesceContainedTwoDim) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 3 >= 0)", - "(x,y) : (x >= 0, -x + 3 >= 0, y - 2 >= 0, -y + 3 >= 0)", - }, - &context); + 2, { + "(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 3 >= 0)", + "(x,y) : (x >= 0, -x + 3 >= 0, y - 2 >= 0, -y + 3 >= 0)", + }); expectCoalesce(1, set); } TEST(SetTest, coalesceCutTwoDim) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 2 >= 0)", - "(x,y) : (x >= 0, -x + 3 >= 0, y - 1 >= 0, -y + 3 >= 0)", - }, - &context); + 2, { + "(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 2 >= 0)", + "(x,y) : (x >= 0, -x + 3 >= 0, y - 1 >= 0, -y + 3 >= 0)", + }); expectCoalesce(2, set); } TEST(SetTest, coalesceSeparateTwoDim) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 1 >= 0)", - "(x,y) : (x >= 0, -x + 3 >= 0, y - 2 >= 0, -y + 3 >= 0)", - }, - &context); + 2, { + "(x,y) : (x >= 0, -x + 3 >= 0, y >= 0, -y + 1 >= 0)", + "(x,y) : (x >= 0, -x + 3 >= 0, y - 2 >= 0, -y + 3 >= 0)", + }); expectCoalesce(2, set); } TEST(SetTest, coalesceContainedEq) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x >= 0, -x + 3 >= 0, x - y == 0)", - "(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)", - }, - &context); + 2, { + "(x,y) : (x >= 0, -x + 3 >= 0, x - y == 0)", + "(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)", + }); expectCoalesce(1, set); } TEST(SetTest, coalesceCuttingEq) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x - 1 >= 0, -x + 3 >= 0, x - y == 0)", - "(x,y) : (x >= 0, -x + 2 >= 0, x - y == 0)", - }, - &context); + 2, { + "(x,y) : (x - 1 >= 0, -x + 3 >= 0, x - y == 0)", + "(x,y) : (x >= 0, -x + 2 >= 0, x - y == 0)", + }); expectCoalesce(2, set); } TEST(SetTest, coalesceSeparateEq) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x - 3 >= 0, -x + 4 >= 0, x - y == 0)", - "(x,y) : (x >= 0, -x + 1 >= 0, x - y == 0)", - }, - &context); + 2, { + "(x,y) : (x - 3 >= 0, -x + 4 >= 0, x - y == 0)", + "(x,y) : (x >= 0, -x + 1 >= 0, x - y == 0)", + }); expectCoalesce(2, set); } TEST(SetTest, coalesceContainedEqAsIneq) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x >= 0, -x + 3 >= 0, x - y >= 0, -x + y >= 0)", - "(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)", - }, - &context); + 2, { + "(x,y) : (x >= 0, -x + 3 >= 0, x - y >= 0, -x + y >= 0)", + "(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)", + }); expectCoalesce(1, set); } TEST(SetTest, coalesceContainedEqComplex) { - MLIRContext context; PresburgerSet set = parsePresburgerSetFromPolyStrings( - 2, - { - "(x,y) : (x - 2 == 0, x - y == 0)", - "(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)", - }, - &context); + 2, { + "(x,y) : (x - 2 == 0, x - y == 0)", + "(x,y) : (x - 1 >= 0, -x + 2 >= 0, x - y == 0)", + }); expectCoalesce(1, set); } @@ -705,12 +616,10 @@ } TEST(SetTest, computeVolume) { - MLIRContext context; // Diamond with vertices at (0, 0), (5, 5), (5, 5), (10, 0). PresburgerSet diamond( parsePoly("(x, y) : (x + y >= 0, -x - y + 10 >= 0, x - y >= 0, -x + y + " - "10 >= 0)", - &context)); + "10 >= 0)")); expectComputedVolumeIsValidOverapprox(diamond, /*trueVolume=*/61ull, /*resultBound=*/121ull); @@ -718,8 +627,7 @@ // Diamond with vertices at (-5, 0), (0, -5), (0, 5), (5, 0). PresburgerSet shiftedDiamond(parsePoly( "(x, y) : (x + y + 5 >= 0, -x - y + 5 >= 0, x - y + 5 >= 0, -x + y + " - "5 >= 0)", - &context)); + "5 >= 0)")); expectComputedVolumeIsValidOverapprox(shiftedDiamond, /*trueVolume=*/61ull, /*resultBound=*/121ull); @@ -727,8 +635,7 @@ // Diamond with vertices at (-5, 0), (5, -10), (5, 10), (15, 0). PresburgerSet biggerDiamond(parsePoly( "(x, y) : (x + y + 5 >= 0, -x - y + 15 >= 0, x - y + 5 >= 0, -x + y + " - "15 >= 0)", - &context)); + "15 >= 0)")); expectComputedVolumeIsValidOverapprox(biggerDiamond, /*trueVolume=*/221ull, /*resultBound=*/441ull); @@ -745,8 +652,7 @@ /*resultBound=*/683ull); // Unbounded polytope. - PresburgerSet unbounded( - parsePoly("(x, y) : (2*x - y >= 0, y - 3*x >= 0)", &context)); + PresburgerSet unbounded(parsePoly("(x, y) : (2*x - y >= 0, y - 3*x >= 0)")); expectComputedVolumeIsValidOverapprox(unbounded, /*trueVolume=*/{}, /*resultBound=*/{}); diff --git a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp --- a/mlir/unittests/Analysis/Presburger/SimplexTest.cpp +++ b/mlir/unittests/Analysis/Presburger/SimplexTest.cpp @@ -481,12 +481,10 @@ } TEST(SimplexTest, IsRationalSubsetOf) { - MLIRContext context; - IntegerPolyhedron univ = parsePoly("(x) : ()", &context); - IntegerPolyhedron empty = - parsePoly("(x) : (x + 0 >= 0, -x - 1 >= 0)", &context); - IntegerPolyhedron s1 = parsePoly("(x) : ( x >= 0, -x + 4 >= 0)", &context); - IntegerPolyhedron s2 = parsePoly("(x) : (x - 1 >= 0, -x + 3 >= 0)", &context); + IntegerPolyhedron univ = parsePoly("(x) : ()"); + IntegerPolyhedron empty = parsePoly("(x) : (x + 0 >= 0, -x - 1 >= 0)"); + IntegerPolyhedron s1 = parsePoly("(x) : ( x >= 0, -x + 4 >= 0)"); + IntegerPolyhedron s2 = parsePoly("(x) : (x - 1 >= 0, -x + 3 >= 0)"); Simplex simUniv(univ); Simplex simEmpty(empty); diff --git a/mlir/unittests/Analysis/Presburger/Utils.h b/mlir/unittests/Analysis/Presburger/Utils.h --- a/mlir/unittests/Analysis/Presburger/Utils.h +++ b/mlir/unittests/Analysis/Presburger/Utils.h @@ -24,8 +24,9 @@ /// Parses a IntegerPolyhedron from a StringRef. It is expected that the /// string represents a valid IntegerSet, otherwise it will violate a gtest /// assertion. -inline IntegerPolyhedron parsePoly(StringRef str, MLIRContext *context) { - FailureOr poly = parseIntegerSetToFAC(str, context); +inline IntegerPolyhedron parsePoly(StringRef str) { + MLIRContext context; + FailureOr poly = parseIntegerSetToFAC(str, &context); EXPECT_TRUE(succeeded(poly)); return *poly; }