[Mlir-commits] [mlir] [mlir][vector] Additional transpose folding (PR #138347)
James Newling
llvmlistbot at llvm.org
Fri May 2 14:16:27 PDT 2025
https://github.com/newling created https://github.com/llvm/llvm-project/pull/138347
Fold transpose with unit-dimensions. Seen in the wild:
```
%0 = vector.transpose %arg, [0, 2, 1, 3] : vector<6x1x1x4xi8> to vector<6x1x1x4xi8>
```
This transpose can be folded because (1) it preserves the shape and (2) the shuffled dims are unit extent.
Also addresses comment about static vs anonymous namespace: https://github.com/llvm/llvm-project/pull/135841#discussion_r2071869067
>From 457afd9a319a5b19585e52acab33eae879a07e10 Mon Sep 17 00:00:00 2001
From: James Newling <james.newling at gmail.com>
Date: Fri, 2 May 2025 14:11:29 -0700
Subject: [PATCH] catch additional foldable case
---
mlir/lib/Dialect/Vector/IR/VectorOps.cpp | 32 +++++++++---------
mlir/test/Dialect/Vector/canonicalize.mlir | 22 -------------
.../Vector/canonicalize/vector-transpose.mlir | 33 +++++++++++++++++++
3 files changed, 49 insertions(+), 38 deletions(-)
diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 0f96442bc3756..dc30741daccd4 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -5575,13 +5575,11 @@ LogicalResult ShapeCastOp::verify() {
return success();
}
-namespace {
-
/// Return true if `transpose` does not permute a pair of non-unit dims.
/// By `order preserving` we mean that the flattened versions of the input and
/// output vectors are (numerically) identical. In other words `transpose` is
/// effectively a shape cast.
-bool isOrderPreserving(TransposeOp transpose) {
+static bool isOrderPreserving(TransposeOp transpose) {
ArrayRef<int64_t> permutation = transpose.getPermutation();
VectorType sourceType = transpose.getSourceVectorType();
ArrayRef<int64_t> inShape = sourceType.getShape();
@@ -5601,8 +5599,6 @@ bool isOrderPreserving(TransposeOp transpose) {
return true;
}
-} // namespace
-
OpFoldResult ShapeCastOp::fold(FoldAdaptor adaptor) {
VectorType resultType = getType();
@@ -5999,18 +5995,22 @@ OpFoldResult vector::TransposeOp::fold(FoldAdaptor adaptor) {
if (llvm::dyn_cast_if_present<ub::PoisonAttr>(adaptor.getVector()))
return ub::PoisonAttr::get(getContext());
- // Eliminate identity transpose ops. This happens when the dimensions of the
- // input vector remain in their original order after the transpose operation.
- ArrayRef<int64_t> perm = getPermutation();
-
- // Check if the permutation of the dimensions contains sequential values:
- // {0, 1, 2, ...}.
- for (int64_t i = 0, e = perm.size(); i < e; i++) {
- if (perm[i] != i)
- return {};
- }
+ // Eliminate identity transposes, and more generally any transposes that
+ // preserves the shape without permuting elements.
+ //
+ // Examples of what to fold:
+ // %0 = vector.transpose %arg, [0, 1] : vector<1x1xi8> to vector<1x1xi8>
+ // %0 = vector.transpose %arg, [0, 1] : vector<2x2xi8> to vector<2x2xi8>
+ // %0 = vector.transpose %arg, [1, 0] : vector<1x1xi8> to vector<1x1xi8>
+ //
+ // Example of what NOT to fold:
+ // %0 = vector.transpose %arg, [1, 0] : vector<2x2xi8> to vector<2x2xi8>
+ //
+ if (getSourceVectorType() == getResultVectorType() &&
+ isOrderPreserving(*this))
+ return getVector();
- return getVector();
+ return {};
}
LogicalResult vector::TransposeOp::verify() {
diff --git a/mlir/test/Dialect/Vector/canonicalize.mlir b/mlir/test/Dialect/Vector/canonicalize.mlir
index 99f0850000a16..974f4506a2ef0 100644
--- a/mlir/test/Dialect/Vector/canonicalize.mlir
+++ b/mlir/test/Dialect/Vector/canonicalize.mlir
@@ -450,28 +450,6 @@ func.func @extract_strided_fold_insert(%a: vector<2x8xf32>, %b: vector<1x4xf32>,
// -----
-// CHECK-LABEL: transpose_1D_identity
-// CHECK-SAME: ([[ARG:%.*]]: vector<4xf32>)
-func.func @transpose_1D_identity(%arg : vector<4xf32>) -> vector<4xf32> {
- // CHECK-NOT: transpose
- %0 = vector.transpose %arg, [0] : vector<4xf32> to vector<4xf32>
- // CHECK-NEXT: return [[ARG]]
- return %0 : vector<4xf32>
-}
-
-// -----
-
-// CHECK-LABEL: transpose_2D_identity
-// CHECK-SAME: ([[ARG:%.*]]: vector<4x3xf32>)
-func.func @transpose_2D_identity(%arg : vector<4x3xf32>) -> vector<4x3xf32> {
- // CHECK-NOT: transpose
- %0 = vector.transpose %arg, [0, 1] : vector<4x3xf32> to vector<4x3xf32>
- // CHECK-NEXT: return [[ARG]]
- return %0 : vector<4x3xf32>
-}
-
-// -----
-
// CHECK-LABEL: transpose_3D_identity
// CHECK-SAME: ([[ARG:%.*]]: vector<4x3x2xf32>)
func.func @transpose_3D_identity(%arg : vector<4x3x2xf32>) -> vector<4x3x2xf32> {
diff --git a/mlir/test/Dialect/Vector/canonicalize/vector-transpose.mlir b/mlir/test/Dialect/Vector/canonicalize/vector-transpose.mlir
index 91ee0d335ecca..aa227c4628d42 100644
--- a/mlir/test/Dialect/Vector/canonicalize/vector-transpose.mlir
+++ b/mlir/test/Dialect/Vector/canonicalize/vector-transpose.mlir
@@ -247,3 +247,36 @@ func.func @negative_shape_cast_transpose(%arg : vector<6xi8>) -> vector<2x3xi8>
%1 = vector.transpose %0, [1, 0] : vector<3x2xi8> to vector<2x3xi8>
return %1 : vector<2x3xi8>
}
+
+// -----
+
+// Test of transpose folding
+// CHECK-LABEL: transpose_1D_identity
+// CHECK-SAME: [[ARG:%.*]]: vector<4xf32>
+// CHECK-NEXT: return [[ARG]]
+func.func @transpose_1D_identity(%arg : vector<4xf32>) -> vector<4xf32> {
+ %0 = vector.transpose %arg, [0] : vector<4xf32> to vector<4xf32>
+ return %0 : vector<4xf32>
+}
+
+// -----
+
+// Test of transpose folding
+// CHECK-LABEL: transpose_2D_identity
+// CHECK-SAME: [[ARG:%.*]]: vector<4x3xf32>
+// CHECK-NEXT: return [[ARG]]
+func.func @transpose_2D_identity(%arg : vector<4x3xf32>) -> vector<4x3xf32> {
+ %0 = vector.transpose %arg, [0, 1] : vector<4x3xf32> to vector<4x3xf32>
+ return %0 : vector<4x3xf32>
+}
+
+// -----
+
+// Test of transpose folding
+// CHECK-LABEL: transpose_shape_and_order_preserving
+// CHECK-SAME: [[ARG:%.*]]: vector<6x1x1x4xi8>
+// CHECK-NEXT: return [[ARG]]
+func.func @transpose_shape_and_order_preserving(%arg : vector<6x1x1x4xi8>) -> vector<6x1x1x4xi8> {
+ %0 = vector.transpose %arg, [0, 2, 1, 3] : vector<6x1x1x4xi8> to vector<6x1x1x4xi8>
+ return %0 : vector<6x1x1x4xi8>
+}
More information about the Mlir-commits
mailing list