Skip to content

Conversation

@banach-space
Copy link
Contributor

@banach-space banach-space commented Nov 20, 2024

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.

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 llvm#114775 for in which I added tests for
`isProjectedPermutation`.
@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2024

@llvm/pr-subscribers-mlir

Author: Andrzej Warzyński (banach-space)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/116945.diff

1 Files Affected:

  • (modified) mlir/unittests/IR/AffineMapTest.cpp (+36)
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));
+}

AffineMap inverseMap1 = inversePermutation(map1);
auto resultsInv1 = inverseMap1.getResults();
EXPECT_EQ(resultsInv1.size(), 3UL);
EXPECT_TRUE(resultsInv1[0].isFunctionOfDim(2));
Copy link
Contributor

@javedabsar1 javedabsar1 Nov 21, 2024

Choose a reason for hiding this comment

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

Please double check this instead of fully trusting me. But I think in case of isFunctionOfDim, d2 has to appear somewhere in the expression e.g. d2+d1 would work, and so is a less restrictive ask then if we are really check d2 and only `d2.

  auto expr = llvm::dyn_cast<AffineDimExpr>(resultsInv1[0]);
   if (!expr)  EXPECT_EQ(0,1);
  EXPECT_EQ(expr.getPosition(), 2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a great point and I really appreciate the suggestion. In fact, I wasn't sure how to do it :) That's now been incorporated.

Copy link
Contributor

@javedabsar1 javedabsar1 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for adding these tests. H

@banach-space banach-space merged commit c87336f into llvm:main Nov 22, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants