Skip to content

Commit a01c548

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 a01c548

File tree

5 files changed

+126
-6
lines changed

5 files changed

+126
-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: 48 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,50 @@ def SPIRV_TransposeOp : SPIRV_Op<"Transpose", [Pure]> {
198197

199198
// -----
200199

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

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,34 @@ 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 (resultType.getElementType() != vectorType.getElementType())
1746+
return emitOpError("vector must be a vector with the same component type "
1747+
"as the component type in result");
1748+
1749+
if (matrixType.getElementType() != resultType.getElementType())
1750+
return emitOpError("matrix must be a matrix with the same component type "
1751+
"as the component type in result");
1752+
1753+
return success();
1754+
}
1755+
17281756
//===----------------------------------------------------------------------===//
17291757
// spirv.MatrixTimesMatrix
17301758
//===----------------------------------------------------------------------===//

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<3xi32>, %arg1: !spirv.matrix<4 x vector<3xf32>>) {
184+
// expected-error @+1 {{vector must be a vector with the same component type as the component type in result}}
185+
%result = spirv.VectorTimesMatrix %arg0, %arg1 : vector<3xi32>, !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)