Skip to content

[mlir][spirv] Fix coop matrix store #65709

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
merged 3 commits into from
Sep 8, 2023
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
27 changes: 19 additions & 8 deletions mlir/include/mlir/Dialect/SPIRV/IR/SPIRVCooperativeMatrixOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,32 @@ def SPIRV_KHRCooperativeMatrixStoreOp : SPIRV_KhrVendorOp<"CooperativeMatrixStor
inactive.

``` {.ebnf}
coop-matrix-store-op ::= `spirv.KHR.CooperativeMatrixStore `
ssa-use `, ` ssa-use `, `
ssa-use `, ` cooperative-matrix-layout `, `
(`[` memory-operand `]`)? `:`
pointer-type `,` coop-matrix-type
coop-matrix-store-op ::= `spirv.KHR.CooperativeMatrixStore`
ssa-use `,` ssa-use `,`
ssa-use `,` `<` cooperative-matrix-layout `>`
(`,` `<` memory-operand `>`)? `:`
pointer-type `,` coop-matrix-type `,` stride-type
```

TODO: In the SPIR-V spec, `stride` is an optional argument. We should also
support this optionality in the SPIR-V dialect.

#### Example:

```
spirv.KHR.CooperativeMatrixStore %ptr, %obj, %stride :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<16x8xi32, Workgroup, MatrixA>
spirv.KHR.CooperativeMatrixStore %ptr, %obj, %stride, <RowMajor> :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<16x8xi32, Workgroup, MatrixA>, i32

spirv.KHR.CooperativeMatrixStore %ptr, %obj, %stride, <ColumnMajor>, <Volatile> :
!spirv.ptr<f32, StorageBuffer>, !spirv.coopmatrix<8x8xf32, Subgroup, MatrixAcc>, i64
```
}];

let assemblyFormat = [{
$pointer `,` $object `,` $stride `,` $matrix_layout ( `,` $memory_operand^ )? attr-dict `:`
type(operands)
}];

let availability = [
MinVersion<SPIRV_V_1_6>,
MaxVersion<SPIRV_V_1_6>,
Expand All @@ -195,8 +206,8 @@ def SPIRV_KHRCooperativeMatrixStoreOp : SPIRV_KhrVendorOp<"CooperativeMatrixStor
let arguments = (ins
SPIRV_AnyPtr:$pointer,
SPIRV_AnyCooperativeMatrix:$object,
SPIRV_Integer:$stride,
SPIRV_KHR_CooperativeMatrixLayoutAttr:$matrix_layout,
SPIRV_Integer:$stride,
OptionalAttr<SPIRV_MemoryAccessAttr>:$memory_operand
);

Expand Down
43 changes: 0 additions & 43 deletions mlir/lib/Dialect/SPIRV/IR/CooperativeMatrixOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,49 +106,6 @@ LogicalResult KHRCooperativeMatrixLoadOp::verify() {
// spirv.KHR.CooperativeMatrixStore
//===----------------------------------------------------------------------===//

ParseResult KHRCooperativeMatrixStoreOp::parse(OpAsmParser &parser,
OperationState &result) {
std::array<OpAsmParser::UnresolvedOperand, 3> operandInfo = {};
for (auto &op : operandInfo) {
if (parser.parseOperand(op) || parser.parseComma())
return failure();
}

CooperativeMatrixLayoutKHR layout;
if (parseEnumKeywordAttr<CooperativeMatrixLayoutKHRAttr>(
layout, parser, result, kKhrCooperativeMatrixLayoutAttrName)) {
return failure();
}

if (parseMemoryAccessAttributes(parser, result, kMemoryOperandAttrName))
return failure();

Type ptrType;
Type objectType;
if (parser.parseColon() || parser.parseType(ptrType) || parser.parseComma() ||
parser.parseType(objectType)) {
return failure();
}

Type strideType = parser.getBuilder().getIntegerType(32);
if (parser.resolveOperands(operandInfo, {ptrType, objectType, strideType},
parser.getNameLoc(), result.operands)) {
return failure();
}

return success();
}

void KHRCooperativeMatrixStoreOp::print(OpAsmPrinter &printer) {
printer << " " << getPointer() << ", " << getObject() << ", " << getStride()
<< ", " << getMatrixLayout();

// Print optional memory operand attribute.
if (auto memOperand = getMemoryOperand())
printer << " [\"" << *memOperand << "\"]";
printer << " : " << getPointer().getType() << ", " << getObject().getType();
}

LogicalResult KHRCooperativeMatrixStoreOp::verify() {
return verifyPointerAndCoopMatrixType(*this, getPointer().getType(),
getObject().getType());
Expand Down
35 changes: 23 additions & 12 deletions mlir/test/Dialect/SPIRV/IR/cooperative-matrix-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,32 @@ spirv.func @cooperative_matrix_load_function(%ptr : !spirv.ptr<i32, Function>, %
// CHECK-LABEL: @cooperative_matrix_store
spirv.func @cooperative_matrix_store(%ptr : !spirv.ptr<i32, StorageBuffer>, %stride : i32,
%m : !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>) "None" {
// CHECK: spirv.KHR.CooperativeMatrixStore {{%.*}}, {{%.*}}, {{%.*}}, RowMajor :
// CHECK-SAME: !spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>
spirv.KHR.CooperativeMatrixStore %ptr, %m, %stride, RowMajor :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>
// CHECK: spirv.KHR.CooperativeMatrixStore {{%.*}}, {{%.*}}, {{%.*}}, <RowMajor> :
// CHECK-SAME: !spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>, i32
spirv.KHR.CooperativeMatrixStore %ptr, %m, %stride, <RowMajor> :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>, i32
spirv.Return
}

// CHECK-LABEL: @cooperative_matrix_store_memoperand
spirv.func @cooperative_matrix_store_memoperand(%ptr : !spirv.ptr<i32, StorageBuffer>,
%m : !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>,
%stride : i32) "None" {
// CHECK: spirv.KHR.CooperativeMatrixStore {{%.*}}, {{%.*}}, {{%.*}}, ColumnMajor ["Volatile"] :
// CHECK-SAME: !spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>
spirv.KHR.CooperativeMatrixStore %ptr, %m, %stride, ColumnMajor ["Volatile"] :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>
// CHECK: spirv.KHR.CooperativeMatrixStore {{%.*}}, {{%.*}}, {{%.*}}, <ColumnMajor>, <Volatile> :
// CHECK-SAME: !spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>, i32
spirv.KHR.CooperativeMatrixStore %ptr, %m, %stride, <ColumnMajor>, <Volatile> :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>, i32
spirv.Return
}

// CHECK-LABEL: @cooperative_matrix_store_stride_i16
spirv.func @cooperative_matrix_store_stride_i16(%ptr : !spirv.ptr<i32, StorageBuffer>,
%m : !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>,
%stride : i16) "None" {
// CHECK: spirv.KHR.CooperativeMatrixStore {{%.*}}, {{%.*}}, {{%.*}}, <ColumnMajor> :
// CHECK-SAME: !spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>, i16
spirv.KHR.CooperativeMatrixStore %ptr, %m, %stride, <ColumnMajor> :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Subgroup, MatrixB>, i16
spirv.Return
}

Expand Down Expand Up @@ -128,9 +139,9 @@ spirv.func @cooperative_matrix_store_missing_attr(%ptr : !spirv.ptr<i32, Storage

spirv.func @cooperative_matrix_store_missing_attr(%ptr : !spirv.ptr<i32, StorageBuffer>, %stride : i32,
%m : !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>) "None" {
// expected-error @+1 {{expected valid keyword}}
// expected-error @+1 {{expected '<'}}
spirv.KHR.CooperativeMatrixStore %ptr, %m, %stride, :
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>
!spirv.ptr<i32, StorageBuffer>, !spirv.coopmatrix<8x16xi32, Workgroup, MatrixA>, i32
spirv.Return
}

Expand All @@ -139,8 +150,8 @@ spirv.func @cooperative_matrix_store_missing_attr(%ptr : !spirv.ptr<i32, Storage
spirv.func @cooperative_matrix_store_bad_object_type(%ptr : !spirv.ptr<i32, StorageBuffer>,
%stride : i32) "None" {
// expected-error @+1 {{op operand #1 must be any SPIR-V cooperative matrix type}}
spirv.KHR.CooperativeMatrixStore %ptr, %stride, %stride, RowMajor :
!spirv.ptr<i32, StorageBuffer>, i32
spirv.KHR.CooperativeMatrixStore %ptr, %stride, %stride, <RowMajor> :
!spirv.ptr<i32, StorageBuffer>, i32, i32
spirv.Return
}

Expand Down