Skip to content

Commit 9f03639

Browse files
committed
[mlir][affine] Add unit tests for isProjectedPermutation
The only way to test `isProjectedPermutation` is through unit tests. The concept of "projected permutations" is tricky to document and these tests are a good source documentation of the expected/intended behavoiur. Hence these additional unit tests.
1 parent 3247386 commit 9f03639

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

mlir/unittests/IR/AffineMapTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,44 @@ TEST(AffineMapTest, inferMapFromAffineExprs) {
2121
map.replace(replacements);
2222
EXPECT_EQ(map, map);
2323
}
24+
25+
TEST(AffineMapTest, isProjectedPermutation) {
26+
MLIRContext ctx;
27+
OpBuilder b(&ctx);
28+
29+
// 1. Empty map - a projected permutation.
30+
AffineMap map1 = b.getEmptyAffineMap();
31+
EXPECT_TRUE(map1.isProjectedPermutation());
32+
33+
// 2. Contains a symbol - not a projected permutation.
34+
AffineMap map2 = AffineMap::get(0, 1, &ctx);
35+
EXPECT_FALSE(map2.isProjectedPermutation());
36+
37+
// 3. The result map is {0} - since zero results are _allowed_, this _is_ a
38+
// projected permutation.
39+
auto zero = b.getAffineConstantExpr(0);
40+
AffineMap map3 = AffineMap::get(1, 0, {zero}, &ctx);
41+
EXPECT_TRUE(map3.isProjectedPermutation(/*allowZeroInResults=*/true));
42+
43+
// 4. The result map is {0} - since zero results are _not allowed_, this _is
44+
// not_ a projected permutation.
45+
AffineMap map4 = AffineMap::get(1, 0, {zero}, &ctx);
46+
EXPECT_FALSE(map4.isProjectedPermutation(/*allowZeroInResults=*/false));
47+
48+
// 5. The number of results > inputs, not a projected permutation.
49+
AffineMap map5 = AffineMap::get(1, 0, {zero, zero}, &ctx);
50+
EXPECT_FALSE(map5.isProjectedPermutation(/*allowZeroInResults=*/true));
51+
52+
// 6. A constant result that's not a {0} - not a projected permutation.
53+
auto one = b.getAffineConstantExpr(1);
54+
AffineMap map6 = AffineMap::get(1, 0, {one}, &ctx);
55+
EXPECT_FALSE(map6.isProjectedPermutation(/*allowZeroInResults=*/true));
56+
57+
// 7. Not a dim expression - not a projected permutation.
58+
auto d0 = b.getAffineDimExpr(0);
59+
auto d1 = b.getAffineDimExpr(1);
60+
61+
auto sum = d0 + d1;
62+
AffineMap map7 = AffineMap::get(2, 0, {sum}, &ctx);
63+
EXPECT_FALSE(map7.isProjectedPermutation());
64+
}

0 commit comments

Comments
 (0)