Skip to content

Commit 20d70fc

Browse files
committed
[mlir][spirv] Add definition for GL Length
1 parent 06c7835 commit 20d70fc

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,46 @@ def SPIRV_GLFMixOp :
11601160

11611161
// -----
11621162

1163+
def SPIRV_GLLengthOp : SPIRV_GLOp<"Length", 66, [
1164+
Pure,
1165+
TypesMatchWith<"result type must match operand element type",
1166+
"operand", "result",
1167+
"::mlir::getElementTypeOrSelf($_self)">
1168+
]> {
1169+
let summary = "Return the length of a vector x";
1170+
1171+
let description = [{
1172+
Result is the length of vector x, i.e., sqrt(x[0]**2 + x[1]**2 + ...).
1173+
1174+
The operand x must be a scalar or vector whose component type is floating-point.
1175+
1176+
Result Type must be a scalar of the same type as the component type of x.
1177+
1178+
#### Example:
1179+
1180+
```mlir
1181+
%2 = spirv.GL.Length %0 : vector<3xf32> -> f32
1182+
%3 = spirv.GL.Length %1 : f32 -> f32
1183+
```
1184+
}];
1185+
1186+
let arguments = (ins
1187+
SPIRV_ScalarOrVectorOf<SPIRV_Float>:$operand
1188+
);
1189+
1190+
let results = (outs
1191+
SPIRV_Float:$result
1192+
);
1193+
1194+
let assemblyFormat = [{
1195+
$operand attr-dict `:` type($operand) `->` type($result)
1196+
}];
1197+
1198+
let hasVerifier = 0;
1199+
}
1200+
1201+
// -----
1202+
11631203
def SPIRV_GLDistanceOp : SPIRV_GLOp<"Distance", 67, [
11641204
Pure,
11651205
AllTypesMatch<["p0", "p1"]>,

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,3 +983,69 @@ func.func @unpack_half_2x16_scalar_out(%arg0 : i32) -> () {
983983
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i32 -> f32
984984
return
985985
}
986+
987+
// -----
988+
989+
//===----------------------------------------------------------------------===//
990+
// spirv.GL.Length
991+
//===----------------------------------------------------------------------===//
992+
993+
func.func @length(%arg0 : f32) -> () {
994+
// CHECK: spirv.GL.Length {{%.*}} : f32 -> f32
995+
%0 = spirv.GL.Length %arg0 : f32 -> f32
996+
return
997+
}
998+
999+
func.func @lengthvec(%arg0 : vector<3xf32>) -> () {
1000+
// CHECK: spirv.GL.Length {{%.*}} : vector<3xf32> -> f32
1001+
%0 = spirv.GL.Length %arg0 : vector<3xf32> -> f32
1002+
return
1003+
}
1004+
1005+
// -----
1006+
1007+
func.func @length_i32_in(%arg0 : i32) -> () {
1008+
// expected-error @+1 {{op operand #0 must be 16/32/64-bit float or vector of 16/32/64-bit float values of length 2/3/4/8/16, but got 'i32'}}
1009+
%0 = spirv.GL.Length %arg0 : i32 -> f32
1010+
return
1011+
}
1012+
1013+
// -----
1014+
1015+
func.func @length_f16_in(%arg0 : f16) -> () {
1016+
// expected-error @+1 {{op failed to verify that result type must match operand element type}}
1017+
%0 = spirv.GL.Length %arg0 : f16 -> f32
1018+
return
1019+
}
1020+
1021+
// -----
1022+
1023+
func.func @length_i32vec_in(%arg0 : vector<3xi32>) -> () {
1024+
// expected-error @+1 {{op operand #0 must be 16/32/64-bit float or vector of 16/32/64-bit float values of length 2/3/4/8/16, but got 'vector<3xi32>'}}
1025+
%0 = spirv.GL.Length %arg0 : vector<3xi32> -> f32
1026+
return
1027+
}
1028+
1029+
// -----
1030+
1031+
func.func @length_f16vec_in(%arg0 : vector<3xf16>) -> () {
1032+
// expected-error @+1 {{op failed to verify that result type must match operand element type}}
1033+
%0 = spirv.GL.Length %arg0 : vector<3xf16> -> f32
1034+
return
1035+
}
1036+
1037+
// -----
1038+
1039+
func.func @length_i32_out(%arg0 : vector<3xf32>) -> () {
1040+
// expected-error @+1 {{op result #0 must be 16/32/64-bit float, but got 'i32'}}
1041+
%0 = spirv.GL.Length %arg0 : vector<3xf32> -> i32
1042+
return
1043+
}
1044+
1045+
// -----
1046+
1047+
func.func @length_vec_out(%arg0 : vector<3xf32>) -> () {
1048+
// expected-error @+1 {{op result #0 must be 16/32/64-bit float, but got 'vector<3xf32>'}}
1049+
%0 = spirv.GL.Length %arg0 : vector<3xf32> -> vector<3xf32>
1050+
return
1051+
}

mlir/test/Target/SPIRV/gl-ops.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
128128
%8 = spirv.GL.FindSMsb %arg3 : vector<3xi32>
129129
// CHECK: {{%.*}} = spirv.GL.FindUMsb {{%.*}} : vector<3xi32>
130130
%9 = spirv.GL.FindUMsb %arg3 : vector<3xi32>
131+
// CHECK: {{%.*}} = spirv.GL.Length {{%.*}} : f32 -> f32
132+
%10 = spirv.GL.Length %arg0 : f32 -> f32
133+
// CHECK: {{%.*}} = spirv.GL.Length {{%.*}} : vector<3xf32> -> f32
134+
%11 = spirv.GL.Length %arg1 : vector<3xf32> -> f32
131135
spirv.Return
132136
}
133137

0 commit comments

Comments
 (0)