Skip to content

Commit c87336f

Browse files
authored
[mlir][test] Add unittests for getInversePermutation (#116945)
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 follow-on of #114775 in which I added tests for `isProjectedPermutation`.
1 parent 5ac81a1 commit c87336f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

mlir/unittests/IR/AffineMapTest.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,57 @@ 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+
100+
// 1.1 Expect d2
101+
AffineDimExpr expr = llvm::dyn_cast<AffineDimExpr>(resultsInv1[0]);
102+
EXPECT_TRUE(expr && expr.getPosition() == 2);
103+
104+
// 1.2 Expect d0
105+
expr = llvm::dyn_cast<AffineDimExpr>(resultsInv1[1]);
106+
EXPECT_TRUE(expr && expr.getPosition() == 0);
107+
108+
// 1.3 Expect d3
109+
expr = llvm::dyn_cast<AffineDimExpr>(resultsInv1[2]);
110+
EXPECT_TRUE(expr && expr.getPosition() == 3);
111+
112+
// 2. (d0, d1, d2) -> (d1, d0 + d1, d0, d2, d1, d2, d1, d0)
113+
auto sum = d0 + d1;
114+
AffineMap map2 =
115+
AffineMap::get(3, 0, {d1, sum, d0, d2, d1, d2, d1, d0}, &ctx);
116+
// (d0, d1, d2, d3, d4, d5, d6, d7) -> (d2, d0, d3)
117+
AffineMap inverseMap2 = inversePermutation(map2);
118+
auto resultsInv2 = inverseMap2.getResults();
119+
EXPECT_EQ(resultsInv2.size(), 3UL);
120+
121+
// 2.1 Expect d2
122+
expr = llvm::dyn_cast<AffineDimExpr>(resultsInv2[0]);
123+
EXPECT_TRUE(expr && expr.getPosition() == 2);
124+
125+
// 2.2 Expect d0
126+
expr = llvm::dyn_cast<AffineDimExpr>(resultsInv2[1]);
127+
EXPECT_TRUE(expr && expr.getPosition() == 0);
128+
129+
// 2.3 Expect d3
130+
expr = llvm::dyn_cast<AffineDimExpr>(resultsInv2[2]);
131+
EXPECT_TRUE(expr && expr.getPosition() == 3);
132+
}

0 commit comments

Comments
 (0)