Skip to content

Commit 9f37c21

Browse files
committed
[mlir][vector] Fold shape_cast of broadcast with same element count
For such cases we can generate a shape cast to simplify the IR. Reviewed By: hanchung Differential Revision: https://reviews.llvm.org/D157929
1 parent da4d191 commit 9f37c21

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

mlir/lib/Dialect/Vector/IR/VectorOps.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,9 +4757,10 @@ class ShapeCastConstantFolder final : public OpRewritePattern<ShapeCastOp> {
47574757
};
47584758

47594759
/// Pattern to rewrite a ShapeCast(Broadcast) -> Broadcast.
4760-
/// This only applies when the shape of the broadcast source is a suffix of the
4761-
/// shape of the result (i.e. when broadcast without reshape is expressive
4762-
/// enough to capture the result in a single op).
4760+
/// This only applies when the shape of the broadcast source
4761+
/// 1. is a suffix of the shape of the result (i.e. when broadcast without
4762+
/// reshape is expressive enough to capture the result in a single op), or
4763+
/// 2. has the same element count as the shape cast result.
47634764
class ShapeCastBroadcastFolder final : public OpRewritePattern<ShapeCastOp> {
47644765
public:
47654766
using OpRewritePattern::OpRewritePattern;
@@ -4771,23 +4772,35 @@ class ShapeCastBroadcastFolder final : public OpRewritePattern<ShapeCastOp> {
47714772
if (!broadcastOp)
47724773
return failure();
47734774

4774-
auto broadcastSourceVectorType =
4775-
llvm::dyn_cast<VectorType>(broadcastOp.getSourceType());
4776-
auto broadcastSourceShape = broadcastSourceVectorType
4777-
? broadcastSourceVectorType.getShape()
4778-
: ArrayRef<int64_t>{};
4779-
auto shapeCastTargetShape = shapeCastOp.getResultVectorType().getShape();
4780-
4781-
// Bail if `broadcastSourceShape` is not a suffix of the result.
4782-
bool isSuffix = (broadcastSourceShape == shapeCastTargetShape.take_back(
4783-
broadcastSourceShape.size()));
4784-
if (!isSuffix)
4785-
return failure();
4775+
ArrayRef<int64_t> broadcastSourceShape;
4776+
if (auto srcType = dyn_cast<VectorType>(broadcastOp.getSourceType()))
4777+
broadcastSourceShape = srcType.getShape();
4778+
ArrayRef<int64_t> shapeCastTargetShape =
4779+
shapeCastOp.getResultVectorType().getShape();
47864780

4787-
rewriter.replaceOpWithNewOp<vector::BroadcastOp>(
4788-
shapeCastOp, shapeCastOp.getResultVectorType(),
4789-
broadcastOp.getSource());
4790-
return success();
4781+
// If `broadcastSourceShape` is a suffix of the result, we can just replace
4782+
// with a broadcast to the final shape.
4783+
if (broadcastSourceShape ==
4784+
shapeCastTargetShape.take_back(broadcastSourceShape.size())) {
4785+
rewriter.replaceOpWithNewOp<vector::BroadcastOp>(
4786+
shapeCastOp, shapeCastOp.getResultVectorType(),
4787+
broadcastOp.getSource());
4788+
return success();
4789+
}
4790+
4791+
// Otherwise, if the final result has the same element count, we can replace
4792+
// with a shape cast.
4793+
if (auto srcType = dyn_cast<VectorType>(broadcastOp.getSourceType())) {
4794+
if (srcType.getNumElements() ==
4795+
shapeCastOp.getResultVectorType().getNumElements()) {
4796+
rewriter.replaceOpWithNewOp<vector::ShapeCastOp>(
4797+
shapeCastOp, shapeCastOp.getResultVectorType(),
4798+
broadcastOp.getSource());
4799+
return success();
4800+
}
4801+
}
4802+
4803+
return failure();
47914804
}
47924805
};
47934806

mlir/test/Dialect/Vector/canonicalize.mlir

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,17 +714,28 @@ func.func @dont_fold_broadcast_shapecast_diff_shape(%arg0: vector<4xf32>) -> vec
714714

715715
// -----
716716

717-
// CHECK-LABEL: func @canonicalize_broadcast_shapecast
717+
// CHECK-LABEL: func @canonicalize_broadcast_shapecast_to_broadcast
718718
// CHECK: vector.broadcast
719719
// CHECK-NOT: vector.shape_cast
720-
func.func @canonicalize_broadcast_shapecast(%arg0: vector<3xf32>) -> vector<8x3xf32> {
720+
func.func @canonicalize_broadcast_shapecast_to_broadcast(%arg0: vector<3xf32>) -> vector<8x3xf32> {
721721
%0 = vector.broadcast %arg0 : vector<3xf32> to vector<2x4x3xf32>
722722
%1 = vector.shape_cast %0 : vector<2x4x3xf32> to vector<8x3xf32>
723723
return %1 : vector<8x3xf32>
724724
}
725725

726726
// -----
727727

728+
// CHECK-LABEL: func @canonicalize_broadcast_shapecast_to_shapecast
729+
// CHECK-NOT: vector.broadcast
730+
// CHECK: vector.shape_cast {{.+}} : vector<3x4xf32> to vector<1x12xf32>
731+
func.func @canonicalize_broadcast_shapecast_to_shapecast(%arg0: vector<3x4xf32>) -> vector<1x12xf32> {
732+
%0 = vector.broadcast %arg0 : vector<3x4xf32> to vector<1x1x3x4xf32>
733+
%1 = vector.shape_cast %0 : vector<1x1x3x4xf32> to vector<1x12xf32>
734+
return %1 : vector<1x12xf32>
735+
}
736+
737+
// -----
738+
728739
// CHECK-LABEL: fold_vector_transfers
729740
func.func @fold_vector_transfers(%A: memref<?x8xf32>) -> (vector<4x8xf32>, vector<4x9xf32>) {
730741
%c0 = arith.constant 0 : index

0 commit comments

Comments
 (0)