Skip to content

Commit 47b0afd

Browse files
committed
[mlir][spirv] Add definition for VectorTimesMatrixOp
Adding op as defined in section 3.52.13. (Arithmetic Instructions) of the SPIR-V specification.
1 parent 8388040 commit 47b0afd

File tree

5 files changed

+125
-6
lines changed

5 files changed

+125
-6
lines changed

mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4387,7 +4387,8 @@ def SPIRV_OC_OpFRem : I32EnumAttrCase<"OpFRem", 140>;
43874387
def SPIRV_OC_OpFMod : I32EnumAttrCase<"OpFMod", 141>;
43884388
def SPIRV_OC_OpVectorTimesScalar : I32EnumAttrCase<"OpVectorTimesScalar", 142>;
43894389
def SPIRV_OC_OpMatrixTimesScalar : I32EnumAttrCase<"OpMatrixTimesScalar", 143>;
4390-
def SPIRV_OC_OpMatrixTimesVector : I32EnumAttrCase<"OpMatrixTimesVector", 145>;
4390+
def SPIRV_OC_OpVectorTimesMatrix : I32EnumAttrCase<"OpVectorTimesMatrix", 144>;
4391+
def SPIRV_OC_OpMatrixTimesVector : I32EnumAttrCase<"OpMatrixTimesVector", 145>;
43914392
def SPIRV_OC_OpMatrixTimesMatrix : I32EnumAttrCase<"OpMatrixTimesMatrix", 146>;
43924393
def SPIRV_OC_OpDot : I32EnumAttrCase<"OpDot", 148>;
43934394
def SPIRV_OC_OpIAddCarry : I32EnumAttrCase<"OpIAddCarry", 149>;
@@ -4559,7 +4560,8 @@ def SPIRV_OpcodeAttr :
45594560
SPIRV_OC_OpFSub, SPIRV_OC_OpIMul, SPIRV_OC_OpFMul, SPIRV_OC_OpUDiv,
45604561
SPIRV_OC_OpSDiv, SPIRV_OC_OpFDiv, SPIRV_OC_OpUMod, SPIRV_OC_OpSRem,
45614562
SPIRV_OC_OpSMod, SPIRV_OC_OpFRem, SPIRV_OC_OpFMod,
4562-
SPIRV_OC_OpVectorTimesScalar, SPIRV_OC_OpMatrixTimesScalar, SPIRV_OC_OpMatrixTimesVector,
4563+
SPIRV_OC_OpVectorTimesScalar, SPIRV_OC_OpMatrixTimesScalar,
4564+
SPIRV_OC_OpVectorTimesMatrix, SPIRV_OC_OpMatrixTimesVector,
45634565
SPIRV_OC_OpMatrixTimesMatrix, SPIRV_OC_OpDot, SPIRV_OC_OpIAddCarry,
45644566
SPIRV_OC_OpISubBorrow, SPIRV_OC_OpUMulExtended, SPIRV_OC_OpSMulExtended,
45654567
SPIRV_OC_OpIsNan, SPIRV_OC_OpIsInf, SPIRV_OC_OpOrdered, SPIRV_OC_OpUnordered,

mlir/include/mlir/Dialect/SPIRV/IR/SPIRVMatrixOps.td

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def SPIRV_MatrixTimesMatrixOp : SPIRV_Op<"MatrixTimesMatrix", [Pure]> {
6363

6464
// -----
6565

66-
def SPIRV_MatrixTimesScalarOp : SPIRV_Op<
67-
"MatrixTimesScalar", [Pure, AllTypesMatch<["matrix", "result"]>]> {
66+
def SPIRV_MatrixTimesScalarOp : SPIRV_Op<"MatrixTimesScalar", [Pure, AllTypesMatch<["matrix", "result"]>]> {
6867
let summary = "Scale a floating-point matrix.";
6968

7069
let description = [{
@@ -115,7 +114,7 @@ def SPIRV_MatrixTimesScalarOp : SPIRV_Op<
115114
// -----
116115

117116
def SPIRV_MatrixTimesVectorOp : SPIRV_Op<"MatrixTimesVector", [Pure]> {
118-
let summary = "Linear-algebraic multiply of matrix X vector.";
117+
let summary = "Linear-algebraic Matrix X Vector.";
119118

120119
let description = [{
121120
Result Type must be a vector of floating-point type.
@@ -198,4 +197,53 @@ def SPIRV_TransposeOp : SPIRV_Op<"Transpose", [Pure]> {
198197

199198
// -----
200199

200+
def SPIRV_VectorTimesMatrixOp : SPIRV_Op<"VectorTimesMatrix", [
201+
Pure,
202+
AllElementTypesMatch<["vector", "result"]>
203+
]> {
204+
let summary = "Linear-algebraic Vector X Matrix.";
205+
206+
let description = [{
207+
Result Type must be a vector of floating-point type.
208+
209+
Vector must be a vector with the same Component Type as the Component
210+
Type in Result Type. Its number of components must equal the number of
211+
components in each column in Matrix.
212+
213+
Matrix must be a matrix with the same Component Type as the Component
214+
Type in Result Type. Its number of columns must equal the number of
215+
components in Result Type.
216+
217+
<!-- End of AutoGen section -->
218+
219+
#### Example:
220+
221+
```mlir
222+
%result = spirv.VectorTimesMatrix %vector, %matrix : vector<4xf32>, !spirv.matrix<4 x vector<4xf32>> -> vector<4xf32>
223+
```
224+
}];
225+
226+
let availability = [
227+
MinVersion<SPIRV_V_1_0>,
228+
MaxVersion<SPIRV_V_1_6>,
229+
Extension<[]>,
230+
Capability<[SPIRV_C_Matrix]>
231+
];
232+
233+
let arguments = (ins
234+
SPIRV_VectorOf<SPIRV_Float>:$vector,
235+
SPIRV_MatrixOrCoopMatrixOf<SPIRV_Float>:$matrix
236+
);
237+
238+
let results = (outs
239+
SPIRV_VectorOf<SPIRV_Float>:$result
240+
);
241+
242+
let assemblyFormat = [{
243+
operands attr-dict `:` type($vector) `,` type($matrix) `->` type($result)
244+
}];
245+
}
246+
247+
// -----
248+
201249
#endif // MLIR_DIALECT_SPIRV_IR_MATRIX_OPS

mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,30 @@ LogicalResult spirv::MatrixTimesVectorOp::verify() {
17251725
return success();
17261726
}
17271727

1728+
//===----------------------------------------------------------------------===//
1729+
// spirv.VectorTimesMatrix
1730+
//===----------------------------------------------------------------------===//
1731+
1732+
LogicalResult spirv::VectorTimesMatrixOp::verify() {
1733+
auto vectorType = llvm::cast<VectorType>(getVector().getType());
1734+
auto matrixType = llvm::cast<spirv::MatrixType>(getMatrix().getType());
1735+
auto resultType = llvm::cast<VectorType>(getType());
1736+
1737+
if (matrixType.getNumRows() != vectorType.getNumElements())
1738+
return emitOpError("number of components in vector must equal the number "
1739+
"of components in each column in matrix");
1740+
1741+
if (resultType.getNumElements() != matrixType.getNumColumns())
1742+
return emitOpError("number of columns in matrix must equal the number of "
1743+
"components in result");
1744+
1745+
if (matrixType.getElementType() != resultType.getElementType())
1746+
return emitOpError("matrix must be a matrix with the same component type "
1747+
"as the component type in result");
1748+
1749+
return success();
1750+
}
1751+
17281752
//===----------------------------------------------------------------------===//
17291753
// spirv.MatrixTimesMatrix
17301754
//===----------------------------------------------------------------------===//

mlir/test/Dialect/SPIRV/IR/matrix-ops.mlir

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
3636
spirv.ReturnValue %result : vector<4xf32>
3737
}
3838

39+
// CHECK-LABEL: @vector_times_matrix_1
40+
spirv.func @vector_times_matrix_1(%arg0: vector<3xf32>, %arg1: !spirv.matrix<4 x vector<3xf32>>) -> vector<4xf32> "None" {
41+
// CHECK: {{%.*}} = spirv.VectorTimesMatrix {{%.*}}, {{%.*}} : vector<3xf32>, !spirv.matrix<4 x vector<3xf32>> -> vector<4xf32>
42+
%result = spirv.VectorTimesMatrix %arg0, %arg1 : vector<3xf32>, !spirv.matrix<4 x vector<3xf32>> -> vector<4xf32>
43+
spirv.ReturnValue %result : vector<4xf32>
44+
}
45+
3946
// CHECK-LABEL: @matrix_times_matrix_1
4047
spirv.func @matrix_times_matrix_1(%arg0: !spirv.matrix<3 x vector<3xf32>>, %arg1: !spirv.matrix<3 x vector<3xf32>>) -> !spirv.matrix<3 x vector<3xf32>> "None"{
4148
// CHECK: {{%.*}} = spirv.MatrixTimesMatrix {{%.*}}, {{%.*}} : !spirv.matrix<3 x vector<3xf32>>, !spirv.matrix<3 x vector<3xf32>> -> !spirv.matrix<3 x vector<3xf32>>
@@ -123,7 +130,6 @@ func.func @matrix_times_matrix_component_type_mismatch_1(%arg0 : !spirv.matrix<3
123130
return
124131
}
125132

126-
127133
// -----
128134

129135
func.func @matrix_times_matrix_component_type_mismatch_2(%arg0 : !spirv.matrix<3 x vector<3xf64>>, %arg1 : !spirv.matrix<3x vector<3xf32>>){
@@ -155,3 +161,35 @@ func.func @matrix_times_vector_column_mismatch(%arg0: !spirv.matrix<4 x vector<3
155161
%result = spirv.MatrixTimesVector %arg0, %arg1 : !spirv.matrix<4 x vector<3xf32>>, vector<3xf32> -> vector<3xf32>
156162
return
157163
}
164+
165+
// -----
166+
167+
func.func @vector_times_matrix_vector_matrix_mismatch(%arg0: vector<4xf32>, %arg1: !spirv.matrix<4 x vector<3xf32>>) {
168+
// expected-error @+1 {{number of components in vector must equal the number of components in each column in matrix}}
169+
%result = spirv.VectorTimesMatrix %arg0, %arg1 : vector<4xf32>, !spirv.matrix<4 x vector<3xf32>> -> vector<3xf32>
170+
return
171+
}
172+
173+
// -----
174+
175+
func.func @vector_times_matrix_result_matrix_mismatch(%arg0: vector<3xf32>, %arg1: !spirv.matrix<4 x vector<3xf32>>) {
176+
// expected-error @+1 {{number of columns in matrix must equal the number of components in result}}
177+
%result = spirv.VectorTimesMatrix %arg0, %arg1 : vector<3xf32>, !spirv.matrix<4 x vector<3xf32>> -> vector<3xf32>
178+
return
179+
}
180+
181+
// -----
182+
183+
func.func @vector_times_matrix_vector_type_mismatch(%arg0: vector<3xf16>, %arg1: !spirv.matrix<4 x vector<3xf32>>) {
184+
// expected-error @+1 {{op failed to verify that all of {vector, result} have same element type}}
185+
%result = spirv.VectorTimesMatrix %arg0, %arg1 : vector<3xf16>, !spirv.matrix<4 x vector<3xf32>> -> vector<4xf32>
186+
return
187+
}
188+
189+
// -----
190+
191+
func.func @vector_times_matrix_matrix_type_mismatch(%arg0: vector<3xf32>, %arg1: !spirv.matrix<4 x vector<3xf16>>) {
192+
// expected-error @+1 {{matrix must be a matrix with the same component type as the component type in result}}
193+
%result = spirv.VectorTimesMatrix %arg0, %arg1 : vector<3xf32>, !spirv.matrix<4 x vector<3xf16>> -> vector<4xf32>
194+
return
195+
}

mlir/test/Target/SPIRV/matrix.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
4242
%result = spirv.MatrixTimesVector %arg0, %arg1 : !spirv.matrix<3 x vector<4xf32>>, vector<3xf32> -> vector<4xf32>
4343
spirv.ReturnValue %result : vector<4xf32>
4444
}
45+
46+
// CHECK-LABEL: @vector_times_matrix_1
47+
spirv.func @vector_times_matrix_1(%arg0: vector<3xf32>, %arg1: !spirv.matrix<4 x vector<3xf32>>) -> vector<4xf32> "None" {
48+
// CHECK: {{%.*}} = spirv.VectorTimesMatrix {{%.*}}, {{%.*}} : vector<3xf32>, !spirv.matrix<4 x vector<3xf32>> -> vector<4xf32>
49+
%result = spirv.VectorTimesMatrix %arg0, %arg1 : vector<3xf32>, !spirv.matrix<4 x vector<3xf32>> -> vector<4xf32>
50+
spirv.ReturnValue %result : vector<4xf32>
51+
}
4552

4653
// CHECK-LABEL: @matrix_times_matrix_1
4754
spirv.func @matrix_times_matrix_1(%arg0: !spirv.matrix<3 x vector<3xf32>>, %arg1: !spirv.matrix<3 x vector<3xf32>>) -> !spirv.matrix<3 x vector<3xf32>> "None"{

0 commit comments

Comments
 (0)