Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions mlir/lib/Dialect/Vector/Transforms/VectorLinearize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,50 @@ struct LinearizeVectorToElements final
}
};

/// Convert broadcasts from scalars or 1-element vectors, such as
///
/// ```mlir
/// vector.broadcast %value : f32 to vector<4x4xf32>
/// ```
///
/// to broadcasts to rank-1 vectors, with shape_casts before/after as needed.
/// The above becomes,
///
/// ```mlir
/// %out_1d = vector.broadcast %value : f32 to vector<16xf32>
/// %out_nd = vector.shape_cast %out_1d : vector<16xf32> to vector<4x4xf32>
/// ```
struct LinearizeVectorBroadcast final
: public OpConversionPattern<vector::BroadcastOp> {
using Base::Base;

LinearizeVectorBroadcast(const TypeConverter &typeConverter,
MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(typeConverter, context, benefit) {}

LogicalResult
matchAndRewrite(vector::BroadcastOp broadcastOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {

int numElements = 1;
Type sourceType = broadcastOp.getSourceType();
if (auto vecType = dyn_cast<VectorType>(sourceType)) {
numElements = vecType.getNumElements();
}

if (numElements != 1) {
return rewriter.notifyMatchFailure(
broadcastOp, "only broadcasts of single elements can be linearized.");
}

auto dstTy = getTypeConverter()->convertType(broadcastOp.getType());
rewriter.replaceOpWithNewOp<vector::BroadcastOp>(broadcastOp, dstTy,
adaptor.getSource());

return success();
}
};

} // namespace

/// This method defines the set of operations that are linearizable, and hence
Expand Down Expand Up @@ -909,8 +953,8 @@ void mlir::vector::populateVectorLinearizeBasePatterns(
patterns
.add<LinearizeConstantLike, LinearizeVectorizable, LinearizeVectorBitCast,
LinearizeVectorCreateMask, LinearizeVectorLoad, LinearizeVectorStore,
LinearizeVectorFromElements, LinearizeVectorToElements>(
typeConverter, patterns.getContext());
LinearizeVectorBroadcast, LinearizeVectorFromElements,
LinearizeVectorToElements>(typeConverter, patterns.getContext());
}

void mlir::vector::populateVectorLinearizeShuffleLikeOpsPatterns(
Expand Down
41 changes: 41 additions & 0 deletions mlir/test/Dialect/Vector/linearize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,47 @@ func.func @test_linearize_across_for(%arg0 : vector<4xi8>) -> vector<4xi8> {

// -----

// CHECK-LABEL: linearize_vector_broadcast_scalar_source
// CHECK-SAME: (%[[ARG:.*]]: i32) -> vector<4x2xi32>
func.func @linearize_vector_broadcast_scalar_source(%arg0: i32) -> vector<4x2xi32> {

// CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[ARG]] : i32 to vector<8xi32>
// CHECK: %[[CAST:.*]] = vector.shape_cast %[[BROADCAST]] : vector<8xi32> to vector<4x2xi32>
// CHECK: return %[[CAST]] : vector<4x2xi32>
%0 = vector.broadcast %arg0 : i32 to vector<4x2xi32>
return %0 : vector<4x2xi32>
}

// -----

// CHECK-LABEL: linearize_vector_broadcast_rank_two_source
// CHECK-SAME: (%[[ARG:.*]]: vector<1x1xi32>) -> vector<4x2xi32>
func.func @linearize_vector_broadcast_rank_two_source(%arg0: vector<1x1xi32>) -> vector<4x2xi32> {

// CHECK: %[[CAST0:.*]] = vector.shape_cast %[[ARG]] : vector<1x1xi32> to vector<1xi32>
// CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[CAST0]] : vector<1xi32> to vector<8xi32>
// CHECK: %[[CAST1:.*]] = vector.shape_cast %[[BROADCAST]] : vector<8xi32> to vector<4x2xi32>
// CHECK: return %[[CAST1]] : vector<4x2xi32>
%0 = vector.broadcast %arg0 : vector<1x1xi32> to vector<4x2xi32>
return %0 : vector<4x2xi32>
}

// -----

// CHECK-LABEL: linearize_scalable_vector_broadcast
// CHECK-SAME: (%[[ARG:.*]]: i32) -> vector<4x[2]xi32>
func.func @linearize_scalable_vector_broadcast(%arg0: i32) -> vector<4x[2]xi32> {
Comment on lines +433 to +460
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] I think that you can safely skip "linearize" and "vector" from these names (that info is already "encoded" in the path). Naming is hard 🤷🏻

Suggested change
func.func @linearize_vector_broadcast_scalar_source(%arg0: i32) -> vector<4x2xi32> {
// CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[ARG]] : i32 to vector<8xi32>
// CHECK: %[[CAST:.*]] = vector.shape_cast %[[BROADCAST]] : vector<8xi32> to vector<4x2xi32>
// CHECK: return %[[CAST]] : vector<4x2xi32>
%0 = vector.broadcast %arg0 : i32 to vector<4x2xi32>
return %0 : vector<4x2xi32>
}
// -----
// CHECK-LABEL: linearize_vector_broadcast_rank_two_source
// CHECK-SAME: (%[[ARG:.*]]: vector<1x1xi32>) -> vector<4x2xi32>
func.func @linearize_vector_broadcast_rank_two_source(%arg0: vector<1x1xi32>) -> vector<4x2xi32> {
// CHECK: %[[CAST0:.*]] = vector.shape_cast %[[ARG]] : vector<1x1xi32> to vector<1xi32>
// CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[CAST0]] : vector<1xi32> to vector<8xi32>
// CHECK: %[[CAST1:.*]] = vector.shape_cast %[[BROADCAST]] : vector<8xi32> to vector<4x2xi32>
// CHECK: return %[[CAST1]] : vector<4x2xi32>
%0 = vector.broadcast %arg0 : vector<1x1xi32> to vector<4x2xi32>
return %0 : vector<4x2xi32>
}
// -----
// CHECK-LABEL: linearize_scalable_vector_broadcast
// CHECK-SAME: (%[[ARG:.*]]: i32) -> vector<4x[2]xi32>
func.func @linearize_scalable_vector_broadcast(%arg0: i32) -> vector<4x[2]xi32> {
func.func @broadcast_scalar_source(%arg0: i32) -> vector<4x2xi32> {
// CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[ARG]] : i32 to vector<8xi32>
// CHECK: %[[CAST:.*]] = vector.shape_cast %[[BROADCAST]] : vector<8xi32> to vector<4x2xi32>
// CHECK: return %[[CAST]] : vector<4x2xi32>
%0 = vector.broadcast %arg0 : i32 to vector<4x2xi32>
return %0 : vector<4x2xi32>
}
// -----
// CHECK-LABEL: linearize_vector_broadcast_rank_two_source
// CHECK-SAME: (%[[ARG:.*]]: vector<1x1xi32>) -> vector<4x2xi32>
func.func @broadcast_rank_two_source(%arg0: vector<1x1xi32>) -> vector<4x2xi32> {
// CHECK: %[[CAST0:.*]] = vector.shape_cast %[[ARG]] : vector<1x1xi32> to vector<1xi32>
// CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[CAST0]] : vector<1xi32> to vector<8xi32>
// CHECK: %[[CAST1:.*]] = vector.shape_cast %[[BROADCAST]] : vector<8xi32> to vector<4x2xi32>
// CHECK: return %[[CAST1]] : vector<4x2xi32>
%0 = vector.broadcast %arg0 : vector<1x1xi32> to vector<4x2xi32>
return %0 : vector<4x2xi32>
}
// -----
// CHECK-LABEL: linearize_scalable_vector_broadcast
// CHECK-SAME: (%[[ARG:.*]]: i32) -> vector<4x[2]xi32>
func.func @broadcast_rank_two_source_scalable(%arg0: i32) -> vector<4x[2]xi32> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can post a follow-up with updates to the function names, here I was just being consistent with what is currently in this file!


// CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[ARG]] : i32 to vector<[8]xi32>
// CHECK: %[[CAST:.*]] = vector.shape_cast %[[BROADCAST]] : vector<[8]xi32> to vector<4x[2]xi32>
// CHECK: return %[[CAST]] : vector<4x[2]xi32>
%0 = vector.broadcast %arg0 : i32 to vector<4x[2]xi32>
return %0 : vector<4x[2]xi32>

}

// -----

// CHECK-LABEL: linearize_create_mask
// CHECK-SAME: (%[[ARG0:.*]]: index, %[[ARG1:.*]]: index) -> vector<1x16xi1>
func.func @linearize_create_mask(%arg0 : index, %arg1 : index) -> vector<1x16xi1> {
Expand Down