Skip to content

Commit 9d41db6

Browse files
committed
[mlir][test] Add unittests for getInversePermutation
The only way to test `getInversePermutation` is through unit tests. The concept of "inverse permutations" is tricky to document and these tests are a good source documentation of the expected/intended behavoiur. Hence these additional unit tests. This is a follon-on #114775 for in which I added tests for `isProjectedPermutation`.
1 parent 0775088 commit 9d41db6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

mlir/unittests/IR/AffineMapTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,39 @@ TEST(AffineMapTest, isProjectedPermutation) {
7676
AffineMap map10 = AffineMap::get(6, 0, {d5, d3, d2, d4}, &ctx);
7777
EXPECT_TRUE(map10.isProjectedPermutation());
7878
}
79+
80+
TEST(AffineMapTest, getInversePermutation) {
81+
MLIRContext ctx;
82+
OpBuilder b(&ctx);
83+
84+
// 0. Empty map
85+
AffineMap map0 = AffineMap::get(0, 0, {}, &ctx);
86+
AffineMap inverseMap0 = inversePermutation(map0);
87+
EXPECT_TRUE(inverseMap0.isEmpty());
88+
89+
auto d0 = b.getAffineDimExpr(0);
90+
auto d1 = b.getAffineDimExpr(1);
91+
auto d2 = b.getAffineDimExpr(2);
92+
93+
// 1. (d0, d1, d2) -> (d1, d1, d0, d2, d1, d2, d1, d0)
94+
AffineMap map1 = AffineMap::get(3, 0, {d1, d1, d0, d2, d1, d2, d1, d0}, &ctx);
95+
// (d0, d1, d2, d3, d4, d5, d6, d7) -> (d2, d0, d3)
96+
AffineMap inverseMap1 = inversePermutation(map1);
97+
auto resultsInv1 = inverseMap1.getResults();
98+
EXPECT_EQ(resultsInv1.size(), 3UL);
99+
EXPECT_TRUE(resultsInv1[0].isFunctionOfDim(2));
100+
EXPECT_TRUE(resultsInv1[1].isFunctionOfDim(0));
101+
EXPECT_TRUE(resultsInv1[2].isFunctionOfDim(3));
102+
103+
// 2. (d0, d1, d2) -> (d1, d0 + d1, d0, d2, d1, d2, d1, d0)
104+
auto sum = d0 + d1;
105+
AffineMap map2 =
106+
AffineMap::get(3, 0, {d1, sum, d0, d2, d1, d2, d1, d0}, &ctx);
107+
// (d0, d1, d2, d3, d4, d5, d6, d7) -> (d2, d0, d3)
108+
AffineMap inverseMap2 = inversePermutation(map2);
109+
auto resultsInv2 = inverseMap2.getResults();
110+
EXPECT_EQ(resultsInv2.size(), 3UL);
111+
EXPECT_TRUE(resultsInv1[0].isFunctionOfDim(2));
112+
EXPECT_TRUE(resultsInv1[1].isFunctionOfDim(0));
113+
EXPECT_TRUE(resultsInv1[2].isFunctionOfDim(3));
114+
}

0 commit comments

Comments
 (0)