Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presburger/test: increase coverage of parser #95705

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions mlir/unittests/Analysis/Presburger/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ static bool parseAndCompare(StringRef str, const IntegerPolyhedron &ex) {
}

TEST(ParseFACTest, ParseAndCompareTest) {
// constant-fold addition
EXPECT_TRUE(parseAndCompare("() : (4 + 3 >= 0)",
makeFACFromConstraints(0, 0, {}, {})));

// constant-fold addition + multiplication
EXPECT_TRUE(parseAndCompare("()[a] : (4 * 3 == 10 + 2)",
makeFACFromConstraints(0, 1, {}, {})));

// constant-fold ceildiv + floordiv
EXPECT_TRUE(parseAndCompare("(x) : (11 ceildiv 3 == 13 floordiv 3)",
makeFACFromConstraints(1, 0, {}, {})));

// simple ineq
EXPECT_TRUE(parseAndCompare("(x)[] : (x >= 0)",
makeFACFromConstraints(1, 0, {{1, 0}})));
Expand All @@ -57,6 +69,11 @@ TEST(ParseFACTest, ParseAndCompareTest) {
EXPECT_TRUE(parseAndCompare("(x)[] : (7 * x >= 0, -7 * x + 5 >= 0)",
makeFACFromConstraints(1, 0, {{7, 0}, {-7, 5}})));

// multiplication distribution
EXPECT_TRUE(
parseAndCompare("(x) : (2 * x >= 2, (-7 + x * 9) * 5 >= 0)",
makeFACFromConstraints(1, 0, {{2, -2}, {45, -35}})));

// multiple dimensions
EXPECT_TRUE(parseAndCompare("(x,y,z)[] : (x + y - z >= 0)",
makeFACFromConstraints(3, 0, {{1, 1, -1, 0}})));
Expand All @@ -70,20 +87,61 @@ TEST(ParseFACTest, ParseAndCompareTest) {
EXPECT_TRUE(parseAndCompare("()[a] : (2 * a - 4 == 0)",
makeFACFromConstraints(0, 1, {}, {{2, -4}})));

// no linear terms
EXPECT_TRUE(parseAndCompare(
"(x, y) : (26 * (x floordiv 6) == y floordiv 3)",
makeFACFromConstraints(2, 0, {}, {{0, 0, 26, -1, 0}},
{{{1, 0, 0}, 6}, {{0, 1, 0, 0}, 3}})));

// simple floordiv
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - 3 * ((x + y - 13) floordiv 3) - 42 == 0)",
makeFACFromConstraints(2, 0, {}, {{0, 1, -3, -42}}, {{{1, 1, -13}, 3}})));

// simple ceildiv
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - 3 * ((x + y - 13) ceildiv 3) - 42 == 0)",
makeFACFromConstraints(2, 0, {}, {{0, 1, -3, -42}}, {{{1, 1, -11}, 3}})));

// simple mod
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - 3 * ((x + y - 13) mod 3) - 42 == 0)",
makeFACFromConstraints(2, 0, {}, {{-3, -2, 9, -3}}, {{{1, 1, -13}, 3}})));

// multiple floordiv
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - x floordiv 3 - y floordiv 2 == 0)",
makeFACFromConstraints(2, 0, {}, {{0, 1, -1, -1, 0}},
{{{1, 0, 0}, 3}, {{0, 1, 0, 0}, 2}})));

// multiple ceildiv
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - x ceildiv 3 - y ceildiv 2 == 0)",
makeFACFromConstraints(2, 0, {}, {{0, 1, -1, -1, 0}},
{{{1, 0, 2}, 3}, {{0, 1, 0, 1}, 2}})));

// multiple mod
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - x mod 3 - y mod 2 == 0)",
makeFACFromConstraints(2, 0, {}, {{-1, 0, 3, 2, 0}},
{{{1, 0, 0}, 3}, {{0, 1, 0, 0}, 2}})));

// nested floordiv
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - (x + y floordiv 2) floordiv 3 == 0)",
makeFACFromConstraints(2, 0, {}, {{0, 1, 0, -1, 0}},
{{{0, 1, 0}, 2}, {{1, 0, 1, 0}, 3}})));

// nested mod
EXPECT_TRUE(parseAndCompare(
"(x, y) : (y - (x + y mod 2) mod 3 == 0)",
makeFACFromConstraints(2, 0, {}, {{-1, 0, 2, 3, 0}},
{{{0, 1, 0}, 2}, {{1, 1, -2, 0}, 3}})));

// nested floordiv + ceildiv + mod
EXPECT_TRUE(parseAndCompare(
"(x, y) : ((2 * x + 3 * (y floordiv 2) + x mod 7 + 1) ceildiv 3 == 42)",
makeFACFromConstraints(
2, 0, {}, {{0, 0, 0, 0, 1, -42}},
{{{0, 1, 0}, 2}, {{1, 0, 0, 0}, 7}, {{3, 0, 3, -7, 3}, 3}})));
}
Loading