[Mlir-commits] [mlir] [mlir][vector] Group re-order patterns together (PR #102856)
Andrzej WarzyĆski
llvmlistbot at llvm.org
Wed Aug 14 09:30:39 PDT 2024
https://github.com/banach-space updated https://github.com/llvm/llvm-project/pull/102856
>From ad558f01ddc899db2246b29d9c260a70f71722f4 Mon Sep 17 00:00:00 2001
From: Andrzej Warzynski <andrzej.warzynski at arm.com>
Date: Sun, 11 Aug 2024 17:38:33 +0100
Subject: [PATCH 1/4] [mlir][vector] Group tests for re-order patterns
Moves all tests for patterns that re-order vector.transpose and
vector.broadcast Ops (*) under one test-flag:
* `test-vector-reorder-patterns`.
To facilitate this,
* `-test-sink-vector-broadcast` is renamed as
`test-vector-reorder-patterns`,
* "sink-vector-broadcast.mlir" is renamed as "vector-reorder.mlir",
* tests for `ReorderCastOpsOnBroadcast` and
`ReorderElementwiseOpsOnTranspose` patterns are moved from
"vector-reduce-to-contract.mlir" to "vector-reorder.mlir",
* `ReorderElementwiseOpsOnTranspose` patterns are removed from
`populateVectorReductionToContractPatterns` and added to (newly
created) `populateReoderVectorTransposePatterns`.
* `ReorderCastOpsOnBroadcast` patterns are removed from
`populateVectorReductionToContractPatterns` - these are already
present in `populateSinkVectorBroadcastPatter`.
This should allow us better layering and more straightforward testing.
For the latter, the goal is to be able to easily identify which pattern
a particular test is exercising (especially when it's a specific
pattern).
Note for downstream users: in order to preserve the current
functionality, please make sure to add
* `populateReoderVectorTransposePatterns` and
`populateSinkVectorBroadcastPatter`,
wherever you are using `populateVectorReductionToContractPatterns`
(*) I didn't notice any other re-order patterns.
---
.../Vector/Transforms/VectorRewritePatterns.h | 4 +
.../TransformOps/LinalgTransformOps.cpp | 1 +
.../Vector/Transforms/VectorTransforms.cpp | 9 +-
.../Vector/vector-reduce-to-contract.mlir | 122 ------------------
...tor-broadcast.mlir => vector-reorder.mlir} | 114 +++++++++++++++-
.../Dialect/Vector/TestVectorTransforms.cpp | 5 +-
6 files changed, 128 insertions(+), 127 deletions(-)
rename mlir/test/Dialect/Vector/{sink-vector-broadcast.mlir => vector-reorder.mlir} (65%)
diff --git a/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h b/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h
index 10970fd03e6eb2..2c17ffd49d1d41 100644
--- a/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h
+++ b/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h
@@ -148,6 +148,10 @@ void populateVectorTransferCollapseInnerMostContiguousDimsPatterns(
void populateSinkVectorBroadcastPatterns(RewritePatternSet &patterns,
PatternBenefit benefit = 1);
+/// Patterns that re-order transpose Ops.
+void populateReoderVectorTransposePatterns(RewritePatternSet &patterns,
+ PatternBenefit benefit = 1);
+
/// Patterns that fold chained vector reductions. These patterns assume that
/// elementwise operations (e.g., `arith.addf` with vector operands) are
/// cheaper than vector reduction.
diff --git a/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp b/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
index 48b3abbeee7010..e2f9ca1fc75027 100644
--- a/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
+++ b/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
@@ -3452,6 +3452,7 @@ transform::VectorizeChildrenAndApplyPatternsOp::applyToOne(
if (!getDisableMultiReductionToContractPatterns())
vector::populateVectorReductionToContractPatterns(patterns);
+ vector::populateReoderVectorTransposePatterns(patterns);
vector::populateSinkVectorBroadcastPatterns(patterns);
patterns.add<linalg::LinalgCopyVTRForwardingPattern,
diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
index ccbaa3e9759975..922e3c61a77310 100644
--- a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
+++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
@@ -2030,8 +2030,7 @@ void mlir::vector::populateVectorContractCanonicalizeMatmulToMMT(
void mlir::vector::populateVectorReductionToContractPatterns(
RewritePatternSet &patterns, PatternBenefit benefit) {
patterns.add<MultiReduceToContract, CombineContractBroadcast,
- CombineContractABTranspose, CombineContractResultTranspose,
- ReorderCastOpsOnBroadcast, ReorderElementwiseOpsOnTranspose>(
+ CombineContractABTranspose, CombineContractResultTranspose>(
patterns.getContext(), benefit);
}
@@ -2049,6 +2048,12 @@ void mlir::vector::populateSinkVectorBroadcastPatterns(
patterns.getContext(), benefit);
}
+void mlir::vector::populateReoderVectorTransposePatterns(
+ RewritePatternSet &patterns, PatternBenefit benefit) {
+ patterns.add<ReorderElementwiseOpsOnTranspose>(patterns.getContext(),
+ benefit);
+}
+
void mlir::vector::populateChainedVectorReductionFoldingPatterns(
RewritePatternSet &patterns, PatternBenefit benefit) {
patterns.add<ChainedReduction>(patterns.getContext(), benefit);
diff --git a/mlir/test/Dialect/Vector/vector-reduce-to-contract.mlir b/mlir/test/Dialect/Vector/vector-reduce-to-contract.mlir
index c0dbea81df892a..24070dbf017a58 100644
--- a/mlir/test/Dialect/Vector/vector-reduce-to-contract.mlir
+++ b/mlir/test/Dialect/Vector/vector-reduce-to-contract.mlir
@@ -245,128 +245,6 @@ func.func @contract_broadcast_would_have_no_reduction_dim_pair(%arg0 : vector<1x
}
-//===----------------------------------------------------------------------===//
-// [Pattern: ReorderCastOpsOnBroadcast]
-//
-// Reorder casting ops and vector ops. The casting ops have almost identical
-// pattern, so only arith.extsi op is tested.
-//
-// TODO: Potential duplication with sink-vector-broadcast.mlir
-//===----------------------------------------------------------------------===//
-
-// -----
-
-func.func @broadcast_vector_extsi(%a : vector<4xi8>) -> vector<2x4xi32> {
- // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : vector<4xi8> to vector<4xi32>
- // CHECK: vector.broadcast %[[EXT:.+]] : vector<4xi32> to vector<2x4xi32>
- %b = vector.broadcast %a : vector<4xi8> to vector<2x4xi8>
- %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
- return %r : vector<2x4xi32>
-}
-
-// -----
-
-func.func @broadcast_scalar_extsi(%a : i8) -> vector<2x4xi32> {
- // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : i8 to i32
- // CHECK: vector.broadcast %[[EXT]] : i32 to vector<2x4xi32>
- %b = vector.broadcast %a : i8 to vector<2x4xi8>
- %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
- return %r : vector<2x4xi32>
-}
-
-// -----
-
-//===----------------------------------------------------------------------===//
-// [Pattern: ReorderElementwiseOpsOnTranspose]
-//
-// TODO: Potential duplication with sink-vector-broadcast.mlir
-//===----------------------------------------------------------------------===//
-func.func @transpose_extsi(%a : vector<4x2xi8>) -> vector<2x4xi32> {
- // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : vector<4x2xi8> to vector<4x2xi32>
- // CHECK: vector.transpose %[[EXT]], [1, 0] : vector<4x2xi32> to vector<2x4xi32>
- %b = vector.transpose %a, [1, 0]: vector<4x2xi8> to vector<2x4xi8>
- %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
- return %r : vector<2x4xi32>
-}
-
-//===----------------------------------------------------------------------===//
-// Reorder elementwise ops and vector ops.
-// TODO: Potential duplication with sink-vector-broadcast.mlir
-//===----------------------------------------------------------------------===//
-
-// -----
-
-// CHECK-LABEL: func @transpose_elementwise_same_type
-// CHECK-SAME: (%[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
-// CHECK: %[[ADD:.+]] = arith.addf %[[A]], %[[B]] : vector<4x2xf32>
-// CHECK: %[[T:.+]] = vector.transpose %[[ADD]], [1, 0]
-// CHECK: return %[[T]]
-
-func.func @transpose_elementwise_same_type(%a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xf32> {
- %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
- %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
- %r = arith.addf %at, %bt : vector<2x4xf32>
- return %r : vector<2x4xf32>
-}
-
-// -----
-
-// CHECK-LABEL: func @transpose_elementwise_diff_operand_types
-// CHECK-SAME: (%[[COND:.+]]: vector<4x2xi1>, %[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
-// CHECK: %[[S:.+]] = arith.select %[[COND]], %[[A]], %[[B]] : vector<4x2xi1>, vector<4x2xf32>
-// CHECK: %[[T:.+]] = vector.transpose %[[S]], [1, 0] : vector<4x2xf32> to vector<2x4xf32>
-// CHECK: return %[[T]]
-func.func @transpose_elementwise_diff_operand_types(%cond: vector<4x2xi1>, %a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xf32> {
- %condt = vector.transpose %cond, [1, 0]: vector<4x2xi1> to vector<2x4xi1>
- %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
- %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
- %r = arith.select %condt, %at, %bt : vector<2x4xi1>, vector<2x4xf32>
- return %r : vector<2x4xf32>
-}
-
-// -----
-
-// CHECK-LABEL: func @transpose_elementwise_diff_operand_result_type
-// CHECK-SAME: (%[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
-// CHECK: %[[CMP:.+]] = arith.cmpf olt, %[[A]], %[[B]] : vector<4x2xf32>
-// CHECK: %[[T:.+]] = vector.transpose %[[CMP]], [1, 0] : vector<4x2xi1> to vector<2x4xi1>
-// CHECK: return %[[T]]
-func.func @transpose_elementwise_diff_operand_result_type(%a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xi1> {
- %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
- %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
- %r = arith.cmpf olt, %at, %bt : vector<2x4xf32>
- return %r : vector<2x4xi1>
-}
-
-// -----
-
-// CHECK-LABEL: func @transpose_elementwise_splat_constant
-// CHECK-SAME: (%[[A:.+]]: vector<4x6x3x2xf32>)
-// CHECK: %[[B:.+]] = arith.constant dense<5.000000e+00> : vector<4x6x3x2xf32>
-// CHECK: %[[ADD:.+]] = arith.addf %[[A]], %[[B]] : vector<4x6x3x2xf32>
-// CHECK: %[[T:.+]] = vector.transpose %[[ADD]], [1, 0, 3, 2] : vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
-// CHECK: return %[[T:.+]] : vector<6x4x2x3xf32>
-
-func.func @transpose_elementwise_splat_constant(%a : vector<4x6x3x2xf32>) -> vector<6x4x2x3xf32> {
- %b = arith.constant dense<5.0> : vector<6x4x2x3xf32>
- %at = vector.transpose %a, [1, 0, 3, 2]: vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
- %r = arith.addf %at, %b : vector<6x4x2x3xf32>
- return %r : vector<6x4x2x3xf32>
-}
-
-// -----
-
-// CHECK-LABEL: func @transpose_elementwise_diff_map
-// CHECK: vector.transpose
-// CHECK: vector.transpose
-// CHECK: arith.addf
-func.func @transpose_elementwise_diff_map(%a : vector<4x6x3x2xf32>, %b: vector<6x2x4x3xf32>) -> vector<6x4x2x3xf32> {
- %at = vector.transpose %a, [1, 0, 3, 2]: vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
- %bt = vector.transpose %b, [0, 2, 1, 3]: vector<6x2x4x3xf32> to vector<6x4x2x3xf32>
- %r = arith.addf %at, %bt : vector<6x4x2x3xf32>
- return %r : vector<6x4x2x3xf32>
-}
-
// -----
// CHECK-DAG: #[[$LHS_MAP:.+]] = affine_map<(d0, d1, d2, d3) -> (d0, d3, d1)>
diff --git a/mlir/test/Dialect/Vector/sink-vector-broadcast.mlir b/mlir/test/Dialect/Vector/vector-reorder.mlir
similarity index 65%
rename from mlir/test/Dialect/Vector/sink-vector-broadcast.mlir
rename to mlir/test/Dialect/Vector/vector-reorder.mlir
index dd2e98831a708a..c314909b012b61 100644
--- a/mlir/test/Dialect/Vector/sink-vector-broadcast.mlir
+++ b/mlir/test/Dialect/Vector/vector-reorder.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -test-sink-vector-broadcast -split-input-file | FileCheck %s
+// RUN: mlir-opt %s -test-vector-reorder-patterns -split-input-file | FileCheck %s
//-----------------------------------------------------------------------------
// [Pattern: ReorderElementwiseOpsOnBroadcast]
@@ -208,3 +208,115 @@ func.func @negative_op_only_supports_vectors(%arg0 : f32) -> vector<1xf32> {
%1 = vector.fma %0, %0, %0 : vector<1xf32>
return %1 : vector<1xf32>
}
+
+//===----------------------------------------------------------------------===//
+// [Pattern: ReorderCastOpsOnBroadcast]
+//
+// Reorder casting ops and vector ops. The casting ops have almost identical
+// pattern, so only arith.extsi op is tested.
+//===----------------------------------------------------------------------===//
+
+// -----
+
+func.func @broadcast_vector_extsi(%a : vector<4xi8>) -> vector<2x4xi32> {
+ // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : vector<4xi8> to vector<4xi32>
+ // CHECK: vector.broadcast %[[EXT:.+]] : vector<4xi32> to vector<2x4xi32>
+ %b = vector.broadcast %a : vector<4xi8> to vector<2x4xi8>
+ %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
+ return %r : vector<2x4xi32>
+}
+
+// -----
+
+func.func @broadcast_scalar_extsi(%a : i8) -> vector<2x4xi32> {
+ // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : i8 to i32
+ // CHECK: vector.broadcast %[[EXT]] : i32 to vector<2x4xi32>
+ %b = vector.broadcast %a : i8 to vector<2x4xi8>
+ %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
+ return %r : vector<2x4xi32>
+}
+
+//===----------------------------------------------------------------------===//
+// [Pattern: ReorderElementwiseOpsOnTranspose]
+//===----------------------------------------------------------------------===//
+
+func.func @transpose_extsi(%a : vector<4x2xi8>) -> vector<2x4xi32> {
+ // CHECK: %[[EXT:.+]] = arith.extsi %{{.+}} : vector<4x2xi8> to vector<4x2xi32>
+ // CHECK: vector.transpose %[[EXT]], [1, 0] : vector<4x2xi32> to vector<2x4xi32>
+ %b = vector.transpose %a, [1, 0]: vector<4x2xi8> to vector<2x4xi8>
+ %r = arith.extsi %b : vector<2x4xi8> to vector<2x4xi32>
+ return %r : vector<2x4xi32>
+}
+
+// -----
+
+// CHECK-LABEL: func @transpose_elementwise_same_type
+// CHECK-SAME: (%[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
+// CHECK: %[[ADD:.+]] = arith.addf %[[A]], %[[B]] : vector<4x2xf32>
+// CHECK: %[[T:.+]] = vector.transpose %[[ADD]], [1, 0]
+// CHECK: return %[[T]]
+
+func.func @transpose_elementwise_same_type(%a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xf32> {
+ %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+ %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+ %r = arith.addf %at, %bt : vector<2x4xf32>
+ return %r : vector<2x4xf32>
+}
+
+// -----
+
+// CHECK-LABEL: func @transpose_elementwise_diff_operand_types
+// CHECK-SAME: (%[[COND:.+]]: vector<4x2xi1>, %[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
+// CHECK: %[[S:.+]] = arith.select %[[COND]], %[[A]], %[[B]] : vector<4x2xi1>, vector<4x2xf32>
+// CHECK: %[[T:.+]] = vector.transpose %[[S]], [1, 0] : vector<4x2xf32> to vector<2x4xf32>
+// CHECK: return %[[T]]
+func.func @transpose_elementwise_diff_operand_types(%cond: vector<4x2xi1>, %a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xf32> {
+ %condt = vector.transpose %cond, [1, 0]: vector<4x2xi1> to vector<2x4xi1>
+ %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+ %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+ %r = arith.select %condt, %at, %bt : vector<2x4xi1>, vector<2x4xf32>
+ return %r : vector<2x4xf32>
+}
+
+// -----
+
+// CHECK-LABEL: func @transpose_elementwise_diff_operand_result_type
+// CHECK-SAME: (%[[A:.+]]: vector<4x2xf32>, %[[B:.+]]: vector<4x2xf32>)
+// CHECK: %[[CMP:.+]] = arith.cmpf olt, %[[A]], %[[B]] : vector<4x2xf32>
+// CHECK: %[[T:.+]] = vector.transpose %[[CMP]], [1, 0] : vector<4x2xi1> to vector<2x4xi1>
+// CHECK: return %[[T]]
+func.func @transpose_elementwise_diff_operand_result_type(%a : vector<4x2xf32>, %b : vector<4x2xf32>) -> vector<2x4xi1> {
+ %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+ %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+ %r = arith.cmpf olt, %at, %bt : vector<2x4xf32>
+ return %r : vector<2x4xi1>
+}
+
+// -----
+
+// CHECK-LABEL: func @transpose_elementwise_splat_constant
+// CHECK-SAME: (%[[A:.+]]: vector<4x6x3x2xf32>)
+// CHECK: %[[B:.+]] = arith.constant dense<5.000000e+00> : vector<4x6x3x2xf32>
+// CHECK: %[[ADD:.+]] = arith.addf %[[A]], %[[B]] : vector<4x6x3x2xf32>
+// CHECK: %[[T:.+]] = vector.transpose %[[ADD]], [1, 0, 3, 2] : vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
+// CHECK: return %[[T:.+]] : vector<6x4x2x3xf32>
+
+func.func @transpose_elementwise_splat_constant(%a : vector<4x6x3x2xf32>) -> vector<6x4x2x3xf32> {
+ %b = arith.constant dense<5.0> : vector<6x4x2x3xf32>
+ %at = vector.transpose %a, [1, 0, 3, 2]: vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
+ %r = arith.addf %at, %b : vector<6x4x2x3xf32>
+ return %r : vector<6x4x2x3xf32>
+}
+
+// -----
+
+// CHECK-LABEL: func @transpose_elementwise_diff_map
+// CHECK: vector.transpose
+// CHECK: vector.transpose
+// CHECK: arith.addf
+func.func @transpose_elementwise_diff_map(%a : vector<4x6x3x2xf32>, %b: vector<6x2x4x3xf32>) -> vector<6x4x2x3xf32> {
+ %at = vector.transpose %a, [1, 0, 3, 2]: vector<4x6x3x2xf32> to vector<6x4x2x3xf32>
+ %bt = vector.transpose %b, [0, 2, 1, 3]: vector<6x2x4x3xf32> to vector<6x4x2x3xf32>
+ %r = arith.addf %at, %bt : vector<6x4x2x3xf32>
+ return %r : vector<6x4x2x3xf32>
+}
diff --git a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
index 29c763b622e877..c4479bf77d6c5e 100644
--- a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
+++ b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
@@ -385,16 +385,17 @@ struct TestSinkVectorBroadcast
registry.insert<memref::MemRefDialect, affine::AffineDialect>();
}
- StringRef getArgument() const final { return "test-sink-vector-broadcast"; }
+ StringRef getArgument() const final { return "test-vector-reorder-patterns"; }
StringRef getDescription() const final {
return "Test lowering patterns that eliminate redundant brodacast "
- "operations.";
+ "and transpose operations.";
}
void runOnOperation() override {
RewritePatternSet patterns(&getContext());
populateSinkVectorBroadcastPatterns(patterns);
+ populateReoderVectorTransposePatterns(patterns);
(void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
}
};
>From 90797f19e16f8da9b11c7a65d2a5b707c00ccde7 Mon Sep 17 00:00:00 2001
From: Andrzej Warzynski <andrzej.warzynski at arm.com>
Date: Mon, 12 Aug 2024 10:45:56 +0100
Subject: [PATCH 2/4] fixup! [mlir][vector] Group tests for re-order patterns
Rename TestSinkVectorBroadcast as TestVectorReorderPatterns
---
.../lib/Dialect/Vector/TestVectorTransforms.cpp | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
index c4479bf77d6c5e..f468873aff859d 100644
--- a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
+++ b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
@@ -374,12 +374,13 @@ struct TestVectorTransferCollapseInnerMostContiguousDims
}
};
-struct TestSinkVectorBroadcast
- : public PassWrapper<TestSinkVectorBroadcast, OperationPass<func::FuncOp>> {
- MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestSinkVectorBroadcast)
+struct TestVectorReorderPatterns
+ : public PassWrapper<TestVectorReorderPatterns,
+ OperationPass<func::FuncOp>> {
+ MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestVectorReorderPatterns)
- TestSinkVectorBroadcast() = default;
- TestSinkVectorBroadcast(const TestSinkVectorBroadcast &pass) = default;
+ TestVectorReorderPatterns() = default;
+ TestVectorReorderPatterns(const TestVectorReorderPatterns &pass) = default;
void getDependentDialects(DialectRegistry ®istry) const override {
registry.insert<memref::MemRefDialect, affine::AffineDialect>();
@@ -920,7 +921,7 @@ void registerTestVectorLowerings() {
PassRegistration<TestVectorTransferCollapseInnerMostContiguousDims>();
- PassRegistration<TestSinkVectorBroadcast>();
+ PassRegistration<TestVectorReorderPatterns>();
PassRegistration<TestVectorReduceToContractPatternsPatterns>();
>From 7eef40d5da0f1b62e3621395d7c297ade3bd39e4 Mon Sep 17 00:00:00 2001
From: Andrzej Warzynski <andrzej.warzynski at arm.com>
Date: Tue, 13 Aug 2024 17:38:50 +0100
Subject: [PATCH 3/4] fixup! fixup! [mlir][vector] Group tests for re-order
patterns
Group all re-order patterns under `populateSinkVectorOpsPatterns`.
Rename test flag and test pass accordingly.
---
.../Vector/Transforms/VectorRewritePatterns.h | 23 +++++++++++++------
.../TransformOps/LinalgTransformOps.cpp | 3 +--
.../Vector/Transforms/VectorTransforms.cpp | 13 ++++-------
mlir/test/Dialect/Vector/vector-reorder.mlir | 2 +-
.../Dialect/Vector/TestVectorTransforms.cpp | 18 +++++++--------
5 files changed, 30 insertions(+), 29 deletions(-)
diff --git a/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h b/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h
index 2c17ffd49d1d41..456d021bc79358 100644
--- a/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h
+++ b/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h
@@ -144,13 +144,22 @@ void populateVectorTransferFullPartialPatterns(
void populateVectorTransferCollapseInnerMostContiguousDimsPatterns(
RewritePatternSet &patterns, PatternBenefit benefit = 1);
-/// Patterns that remove redundant vector broadcasts.
-void populateSinkVectorBroadcastPatterns(RewritePatternSet &patterns,
- PatternBenefit benefit = 1);
-
-/// Patterns that re-order transpose Ops.
-void populateReoderVectorTransposePatterns(RewritePatternSet &patterns,
- PatternBenefit benefit = 1);
+/// Patterns that remove redundant Vector Ops by re-ordering them with
+/// e.g. elementwise Ops:
+/// ```
+/// %at = vector.transpose %a, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+/// %bt = vector.transpose %b, [1, 0]: vector<4x2xf32> to vector<2x4xf32>
+/// %r = arith.addf %at, %bt : vector<2x4xf32>
+/// ```
+/// gets converted to:
+/// ```
+/// %0 = arith.addf %a, %b : vector<4x2xf32>
+/// %r = vector.transpose %0, [1, 0] : vector<2x4xf32>
+/// ```
+/// At the moment, these patterns are limited to vector.broadcast and
+/// vector.transpose.
+void populateSinkVectorOpsPatterns(RewritePatternSet &patterns,
+ PatternBenefit benefit = 1);
/// Patterns that fold chained vector reductions. These patterns assume that
/// elementwise operations (e.g., `arith.addf` with vector operands) are
diff --git a/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp b/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
index e2f9ca1fc75027..248d255d8076d6 100644
--- a/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
+++ b/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
@@ -3452,8 +3452,7 @@ transform::VectorizeChildrenAndApplyPatternsOp::applyToOne(
if (!getDisableMultiReductionToContractPatterns())
vector::populateVectorReductionToContractPatterns(patterns);
- vector::populateReoderVectorTransposePatterns(patterns);
- vector::populateSinkVectorBroadcastPatterns(patterns);
+ vector::populateSinkVectorOpsPatterns(patterns);
patterns.add<linalg::LinalgCopyVTRForwardingPattern,
linalg::LinalgCopyVTWForwardingPattern>(ctx,
diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
index 922e3c61a77310..ad4e42b31962e1 100644
--- a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
+++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
@@ -2042,15 +2042,10 @@ void mlir::vector::
benefit);
}
-void mlir::vector::populateSinkVectorBroadcastPatterns(
- RewritePatternSet &patterns, PatternBenefit benefit) {
- patterns.add<ReorderCastOpsOnBroadcast, ReorderElementwiseOpsOnBroadcast>(
- patterns.getContext(), benefit);
-}
-
-void mlir::vector::populateReoderVectorTransposePatterns(
- RewritePatternSet &patterns, PatternBenefit benefit) {
- patterns.add<ReorderElementwiseOpsOnTranspose>(patterns.getContext(),
+void mlir::vector::populateSinkVectorOpsPatterns(RewritePatternSet &patterns,
+ PatternBenefit benefit) {
+ patterns.add<ReorderElementwiseOpsOnTranspose, ReorderCastOpsOnBroadcast,
+ ReorderElementwiseOpsOnBroadcast>(patterns.getContext(),
benefit);
}
diff --git a/mlir/test/Dialect/Vector/vector-reorder.mlir b/mlir/test/Dialect/Vector/vector-reorder.mlir
index c314909b012b61..5a3699333265c1 100644
--- a/mlir/test/Dialect/Vector/vector-reorder.mlir
+++ b/mlir/test/Dialect/Vector/vector-reorder.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -test-vector-reorder-patterns -split-input-file | FileCheck %s
+// RUN: mlir-opt %s -test-vector-sink-patterns -split-input-file | FileCheck %s
//-----------------------------------------------------------------------------
// [Pattern: ReorderElementwiseOpsOnBroadcast]
diff --git a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
index f468873aff859d..72aaa7dc4f8973 100644
--- a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
+++ b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
@@ -374,19 +374,18 @@ struct TestVectorTransferCollapseInnerMostContiguousDims
}
};
-struct TestVectorReorderPatterns
- : public PassWrapper<TestVectorReorderPatterns,
- OperationPass<func::FuncOp>> {
- MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestVectorReorderPatterns)
+struct TestVectorSinkPatterns
+ : public PassWrapper<TestVectorSinkPatterns, OperationPass<func::FuncOp>> {
+ MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestVectorSinkPatterns)
- TestVectorReorderPatterns() = default;
- TestVectorReorderPatterns(const TestVectorReorderPatterns &pass) = default;
+ TestVectorSinkPatterns() = default;
+ TestVectorSinkPatterns(const TestVectorSinkPatterns &pass) = default;
void getDependentDialects(DialectRegistry ®istry) const override {
registry.insert<memref::MemRefDialect, affine::AffineDialect>();
}
- StringRef getArgument() const final { return "test-vector-reorder-patterns"; }
+ StringRef getArgument() const final { return "test-vector-sink-patterns"; }
StringRef getDescription() const final {
return "Test lowering patterns that eliminate redundant brodacast "
@@ -395,8 +394,7 @@ struct TestVectorReorderPatterns
void runOnOperation() override {
RewritePatternSet patterns(&getContext());
- populateSinkVectorBroadcastPatterns(patterns);
- populateReoderVectorTransposePatterns(patterns);
+ populateSinkVectorOpsPatterns(patterns);
(void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
}
};
@@ -921,7 +919,7 @@ void registerTestVectorLowerings() {
PassRegistration<TestVectorTransferCollapseInnerMostContiguousDims>();
- PassRegistration<TestVectorReorderPatterns>();
+ PassRegistration<TestVectorSinkPatterns>();
PassRegistration<TestVectorReduceToContractPatternsPatterns>();
>From b2a70dd22d627561b20ea8f532a0ea12732b6234 Mon Sep 17 00:00:00 2001
From: Andrzej Warzynski <andrzej.warzynski at arm.com>
Date: Wed, 14 Aug 2024 16:24:53 +0100
Subject: [PATCH 4/4] fixup! fixup! fixup! [mlir][vector] Group tests for
re-order patterns
Rename test
---
.../test/Dialect/Vector/{vector-reorder.mlir => vector-sink.mlir} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename mlir/test/Dialect/Vector/{vector-reorder.mlir => vector-sink.mlir} (100%)
diff --git a/mlir/test/Dialect/Vector/vector-reorder.mlir b/mlir/test/Dialect/Vector/vector-sink.mlir
similarity index 100%
rename from mlir/test/Dialect/Vector/vector-reorder.mlir
rename to mlir/test/Dialect/Vector/vector-sink.mlir
More information about the Mlir-commits
mailing list