[Mlir-commits] [mlir] [mlir][test] Add unittests for `getInversePermutation` (PR #116945)
Andrzej WarzyĆski
llvmlistbot at llvm.org
Fri Nov 22 00:46:57 PST 2024
https://github.com/banach-space updated https://github.com/llvm/llvm-project/pull/116945
>From 9d41db6ba8f40ea572e729553e0932cd13da4199 Mon Sep 17 00:00:00 2001
From: Andrzej Warzynski <andrzej.warzynski at arm.com>
Date: Wed, 20 Nov 2024 09:35:43 +0000
Subject: [PATCH 1/2] [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`.
---
mlir/unittests/IR/AffineMapTest.cpp | 36 +++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/mlir/unittests/IR/AffineMapTest.cpp b/mlir/unittests/IR/AffineMapTest.cpp
index eaeb18d128ca5e..c5a949431ac0a9 100644
--- a/mlir/unittests/IR/AffineMapTest.cpp
+++ b/mlir/unittests/IR/AffineMapTest.cpp
@@ -76,3 +76,39 @@ TEST(AffineMapTest, isProjectedPermutation) {
AffineMap map10 = AffineMap::get(6, 0, {d5, d3, d2, d4}, &ctx);
EXPECT_TRUE(map10.isProjectedPermutation());
}
+
+TEST(AffineMapTest, getInversePermutation) {
+ MLIRContext ctx;
+ OpBuilder b(&ctx);
+
+ // 0. Empty map
+ AffineMap map0 = AffineMap::get(0, 0, {}, &ctx);
+ AffineMap inverseMap0 = inversePermutation(map0);
+ EXPECT_TRUE(inverseMap0.isEmpty());
+
+ auto d0 = b.getAffineDimExpr(0);
+ auto d1 = b.getAffineDimExpr(1);
+ auto d2 = b.getAffineDimExpr(2);
+
+ // 1. (d0, d1, d2) -> (d1, d1, d0, d2, d1, d2, d1, d0)
+ AffineMap map1 = AffineMap::get(3, 0, {d1, d1, d0, d2, d1, d2, d1, d0}, &ctx);
+ // (d0, d1, d2, d3, d4, d5, d6, d7) -> (d2, d0, d3)
+ AffineMap inverseMap1 = inversePermutation(map1);
+ auto resultsInv1 = inverseMap1.getResults();
+ EXPECT_EQ(resultsInv1.size(), 3UL);
+ EXPECT_TRUE(resultsInv1[0].isFunctionOfDim(2));
+ EXPECT_TRUE(resultsInv1[1].isFunctionOfDim(0));
+ EXPECT_TRUE(resultsInv1[2].isFunctionOfDim(3));
+
+ // 2. (d0, d1, d2) -> (d1, d0 + d1, d0, d2, d1, d2, d1, d0)
+ auto sum = d0 + d1;
+ AffineMap map2 =
+ AffineMap::get(3, 0, {d1, sum, d0, d2, d1, d2, d1, d0}, &ctx);
+ // (d0, d1, d2, d3, d4, d5, d6, d7) -> (d2, d0, d3)
+ AffineMap inverseMap2 = inversePermutation(map2);
+ auto resultsInv2 = inverseMap2.getResults();
+ EXPECT_EQ(resultsInv2.size(), 3UL);
+ EXPECT_TRUE(resultsInv1[0].isFunctionOfDim(2));
+ EXPECT_TRUE(resultsInv1[1].isFunctionOfDim(0));
+ EXPECT_TRUE(resultsInv1[2].isFunctionOfDim(3));
+}
>From ed5aab1fc4d4ace83e57932c0196fc51b5611a35 Mon Sep 17 00:00:00 2001
From: Andrzej Warzynski <andrzej.warzynski at arm.com>
Date: Fri, 22 Nov 2024 08:46:38 +0000
Subject: [PATCH 2/2] fixup! [mlir][test] Add unittests for
`getInversePermutation`
Make the checks more restrictive
---
mlir/unittests/IR/AffineMapTest.cpp | 30 +++++++++++++++++++++++------
1 file changed, 24 insertions(+), 6 deletions(-)
diff --git a/mlir/unittests/IR/AffineMapTest.cpp b/mlir/unittests/IR/AffineMapTest.cpp
index c5a949431ac0a9..166692f731d1cf 100644
--- a/mlir/unittests/IR/AffineMapTest.cpp
+++ b/mlir/unittests/IR/AffineMapTest.cpp
@@ -96,9 +96,18 @@ TEST(AffineMapTest, getInversePermutation) {
AffineMap inverseMap1 = inversePermutation(map1);
auto resultsInv1 = inverseMap1.getResults();
EXPECT_EQ(resultsInv1.size(), 3UL);
- EXPECT_TRUE(resultsInv1[0].isFunctionOfDim(2));
- EXPECT_TRUE(resultsInv1[1].isFunctionOfDim(0));
- EXPECT_TRUE(resultsInv1[2].isFunctionOfDim(3));
+
+ // 1.1 Expect d2
+ AffineDimExpr expr = llvm::dyn_cast<AffineDimExpr>(resultsInv1[0]);
+ EXPECT_TRUE(expr && expr.getPosition() == 2);
+
+ // 1.2 Expect d0
+ expr = llvm::dyn_cast<AffineDimExpr>(resultsInv1[1]);
+ EXPECT_TRUE(expr && expr.getPosition() == 0);
+
+ // 1.3 Expect d3
+ expr = llvm::dyn_cast<AffineDimExpr>(resultsInv1[2]);
+ EXPECT_TRUE(expr && expr.getPosition() == 3);
// 2. (d0, d1, d2) -> (d1, d0 + d1, d0, d2, d1, d2, d1, d0)
auto sum = d0 + d1;
@@ -108,7 +117,16 @@ TEST(AffineMapTest, getInversePermutation) {
AffineMap inverseMap2 = inversePermutation(map2);
auto resultsInv2 = inverseMap2.getResults();
EXPECT_EQ(resultsInv2.size(), 3UL);
- EXPECT_TRUE(resultsInv1[0].isFunctionOfDim(2));
- EXPECT_TRUE(resultsInv1[1].isFunctionOfDim(0));
- EXPECT_TRUE(resultsInv1[2].isFunctionOfDim(3));
+
+ // 2.1 Expect d2
+ expr = llvm::dyn_cast<AffineDimExpr>(resultsInv2[0]);
+ EXPECT_TRUE(expr && expr.getPosition() == 2);
+
+ // 2.2 Expect d0
+ expr = llvm::dyn_cast<AffineDimExpr>(resultsInv2[1]);
+ EXPECT_TRUE(expr && expr.getPosition() == 0);
+
+ // 2.3 Expect d3
+ expr = llvm::dyn_cast<AffineDimExpr>(resultsInv2[2]);
+ EXPECT_TRUE(expr && expr.getPosition() == 3);
}
More information about the Mlir-commits
mailing list