Skip to content

[mlir][spirv] Linearize ND vectors. #80451

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 5 additions & 4 deletions mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ struct ConstantCompositeOpPattern final
if (!srcType || srcType.getNumElements() == 1)
return failure();

// arith.constant should only have vector or tenor types.
assert((isa<VectorType, RankedTensorType>(srcType)));
assert((isa<VectorType, RankedTensorType>(srcType) &&
"arith.constant should only have vector or tensor types"));

Type dstType = getTypeConverter()->convertType(srcType);
if (!dstType)
Expand All @@ -250,8 +250,9 @@ struct ConstantCompositeOpPattern final
srcType.getElementType());
dstElementsAttr = dstElementsAttr.reshape(dstAttrType);
} else {
// TODO: add support for large vectors.
return failure();
dstAttrType =
VectorType::get(srcType.getNumElements(), srcType.getElementType());
dstElementsAttr = dstElementsAttr.reshape(dstAttrType);
}
}

Expand Down
4 changes: 4 additions & 0 deletions mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ convertVectorType(const spirv::TargetEnv &targetEnv,
if (type.getRank() <= 1 && type.getNumElements() == 1)
return convertScalarType(targetEnv, options, scalarType, storageClass);

// Linearize ND vectors
if (type.getRank() > 1)
type = VectorType::get(type.getNumElements(), scalarType);

if (!spirv::CompositeType::isValid(type)) {
LLVM_DEBUG(llvm::dbgs()
<< type << " illegal: not a valid composite type\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ module attributes {
#spirv.vce<v1.0, [Int8, Int16, Int64, Float16, Float64, Shader], []>, #spirv.resource_limits<>>
} {

func.func @unsupported_2x2elem_vector(%arg0: vector<2x2xi32>) {
func.func @unsupported_2x2elem_vector(%arg0: vector<3x5xi32>) {
// expected-error@+1 {{failed to legalize operation 'arith.muli'}}
%2 = arith.muli %arg0, %arg0: vector<2x2xi32>
%2 = arith.muli %arg0, %arg0: vector<3x5xi32>
return
}

Expand Down
2 changes: 2 additions & 0 deletions mlir/test/Conversion/ArithToSPIRV/arith-to-spirv.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ func.func @constant() {
%9 = arith.constant dense<[[1, 2], [3, 4], [5, 6]]> : tensor<3x2xi32>
// CHECK: spirv.Constant dense<{{\[}}1, 2, 3, 4, 5, 6]> : tensor<6xi32> : !spirv.array<6 x i32>
%10 = arith.constant dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi32>
// CHECK: spirv.Constant dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : vector<4xf32>
%11 = arith.constant dense<[[1.0, 2.0], [3.0, 4.0]]> : vector<2x2xf32>
return
}

Expand Down