Skip to content

[mlir][spirv][vector] Support converting vector.from_elements to SPIR-V #118540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,32 @@ struct VectorFmaOpConvert final : public OpConversionPattern<vector::FMAOp> {
}
};

struct VectorFromElementsOpConvert final
: public OpConversionPattern<vector::FromElementsOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(vector::FromElementsOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Type resultType = getTypeConverter()->convertType(op.getType());
if (!resultType)
return failure();
OperandRange elements = op.getElements();
if (isa<spirv::ScalarType>(resultType)) {
// In the case with a single scalar operand / single-element result,
// pass through the scalar.
rewriter.replaceOp(op, elements[0]);
return success();
}
// SPIRVTypeConverter rejects vectors with rank > 1, so multi-dimensional
// vector.from_elements cases should not need to be handled, only 1d.
assert(cast<VectorType>(resultType).getRank() == 1);
rewriter.replaceOpWithNewOp<spirv::CompositeConstructOp>(op, resultType,
elements);
return success();
}
};

struct VectorInsertOpConvert final
: public OpConversionPattern<vector::InsertOp> {
using OpConversionPattern::OpConversionPattern;
Expand Down Expand Up @@ -952,8 +978,9 @@ void mlir::populateVectorToSPIRVPatterns(
VectorBitcastConvert, VectorBroadcastConvert,
VectorExtractElementOpConvert, VectorExtractOpConvert,
VectorExtractStridedSliceOpConvert, VectorFmaOpConvert<spirv::GLFmaOp>,
VectorFmaOpConvert<spirv::CLFmaOp>, VectorInsertElementOpConvert,
VectorInsertOpConvert, VectorReductionPattern<GL_INT_MAX_MIN_OPS>,
VectorFmaOpConvert<spirv::CLFmaOp>, VectorFromElementsOpConvert,
VectorInsertElementOpConvert, VectorInsertOpConvert,
VectorReductionPattern<GL_INT_MAX_MIN_OPS>,
VectorReductionPattern<CL_INT_MAX_MIN_OPS>,
VectorReductionFloatMinMax<CL_FLOAT_MAX_MIN_OPS>,
VectorReductionFloatMinMax<GL_FLOAT_MAX_MIN_OPS>, VectorShapeCast,
Expand Down
29 changes: 29 additions & 0 deletions mlir/test/Conversion/VectorToSPIRV/vector-to-spirv.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,35 @@ func.func @extract_dynamic_cst(%arg0 : vector<4xf32>) -> f32 {

// -----

// CHECK-LABEL: @from_elements_0d
// CHECK-SAME: %[[ARG0:.+]]: f32
// CHECK: %[[RETVAL:.+]] = builtin.unrealized_conversion_cast %[[ARG0]]
// CHECK: return %[[RETVAL]]
func.func @from_elements_0d(%arg0 : f32) -> vector<f32> {
%0 = vector.from_elements %arg0 : vector<f32>
return %0: vector<f32>
}

// CHECK-LABEL: @from_elements_1x
// CHECK-SAME: %[[ARG0:.+]]: f32
// CHECK: %[[RETVAL:.+]] = builtin.unrealized_conversion_cast %[[ARG0]]
// CHECK: return %[[RETVAL]]
func.func @from_elements_1x(%arg0 : f32) -> vector<1xf32> {
%0 = vector.from_elements %arg0 : vector<1xf32>
return %0: vector<1xf32>
}

// CHECK-LABEL: @from_elements_3x
// CHECK-SAME: %[[ARG0:.+]]: f32, %[[ARG1:.+]]: f32, %[[ARG2:.+]]: f32
// CHECK: %[[RETVAL:.+]] = spirv.CompositeConstruct %[[ARG0]], %[[ARG1]], %[[ARG2]] : (f32, f32, f32) -> vector<3xf32>
// CHECK: return %[[RETVAL]]
func.func @from_elements_3x(%arg0 : f32, %arg1 : f32, %arg2 : f32) -> vector<3xf32> {
%0 = vector.from_elements %arg0, %arg1, %arg2 : vector<3xf32>
return %0: vector<3xf32>
}

// -----

// CHECK-LABEL: @insert
// CHECK-SAME: %[[V:.*]]: vector<4xf32>, %[[S:.*]]: f32
// CHECK: spirv.CompositeInsert %[[S]], %[[V]][2 : i32] : f32 into vector<4xf32>
Expand Down
Loading