Skip to content

[mlir][affine] Add unit tests for isProjectedPermutation #114775

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

Merged
merged 2 commits into from
Nov 5, 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
55 changes: 55 additions & 0 deletions mlir/unittests/IR/AffineMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,58 @@ TEST(AffineMapTest, inferMapFromAffineExprs) {
map.replace(replacements);
EXPECT_EQ(map, map);
}

TEST(AffineMapTest, isProjectedPermutation) {
MLIRContext ctx;
OpBuilder b(&ctx);

// 1. Empty map
AffineMap map1 = b.getEmptyAffineMap();
EXPECT_TRUE(map1.isProjectedPermutation());

// 2. Map with a symbol
AffineMap map2 = AffineMap::get(0, 1, &ctx);
EXPECT_FALSE(map2.isProjectedPermutation());

// 3. The result map is {0} and zero results are _allowed_.
auto zero = b.getAffineConstantExpr(0);
AffineMap map3 = AffineMap::get(1, 0, {zero}, &ctx);
EXPECT_TRUE(map3.isProjectedPermutation(/*allowZeroInResults=*/true));

// 4. The result map is {0} and zero results are _not allowed_
AffineMap map4 = AffineMap::get(1, 0, {zero}, &ctx);
EXPECT_FALSE(map4.isProjectedPermutation(/*allowZeroInResults=*/false));

// 5. The number of results > inputs
AffineMap map5 = AffineMap::get(1, 0, {zero, zero}, &ctx);
EXPECT_FALSE(map5.isProjectedPermutation(/*allowZeroInResults=*/true));

// 6. A constant result that's not a {0}
auto one = b.getAffineConstantExpr(1);
AffineMap map6 = AffineMap::get(1, 0, {one}, &ctx);
EXPECT_FALSE(map6.isProjectedPermutation(/*allowZeroInResults=*/true));

// 7. Not a dim expression
auto d0 = b.getAffineDimExpr(0);
auto d1 = b.getAffineDimExpr(1);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add some tougher tests
(d0,d1, d2, d3,d4,d5) ->(d5,d3,d0,d1,d2,d4)
(d0,d1, d2, d3,d4,d5) ->(d5,d3,d0+d1,d2,d4)
(d0,d1, d2, d3,d4,d5) ->(d5,d3,d2,d4)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, thanks, about to add these.

auto sum = d0 + d1;
AffineMap map7 = AffineMap::get(2, 0, {sum}, &ctx);
EXPECT_FALSE(map7.isProjectedPermutation());

// 8. (d0, d1, d2, d3, d4, d5) ->(d5, d3, d0, d1, d2, d4)
auto d2 = b.getAffineDimExpr(2);
auto d3 = b.getAffineDimExpr(3);
auto d4 = b.getAffineDimExpr(4);
auto d5 = b.getAffineDimExpr(5);
AffineMap map8 = AffineMap::get(6, 0, {d5, d3, d0, d1, d2, d4}, &ctx);
EXPECT_TRUE(map8.isProjectedPermutation());

// 9. (d0, d1, d2, d3, d4, d5) ->(d5, d3, d0 + d1, d2, d4)
AffineMap map9 = AffineMap::get(6, 0, {d5, d3, sum, d2, d4}, &ctx);
EXPECT_FALSE(map9.isProjectedPermutation());

// 10. (d0, d1, d2, d3, d4, d5) ->(d5, d3, d2, d4)
AffineMap map10 = AffineMap::get(6, 0, {d5, d3, d2, d4}, &ctx);
EXPECT_TRUE(map10.isProjectedPermutation());
}
Loading