Skip to content

Commit 62b6940

Browse files
authored
[mlir][spirv] Add definition for GL Pack/UnpackHalf2x16 (llvm#143889)
1 parent 4bd0a0e commit 62b6940

File tree

3 files changed

+196
-2
lines changed

3 files changed

+196
-2
lines changed

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,4 +1317,88 @@ def SPIRV_GLFractOp : SPIRV_GLUnaryArithmeticOp<"Fract", 10, SPIRV_Float> {
13171317
}];
13181318
}
13191319

1320+
// -----
1321+
1322+
def SPIRV_GLPackHalf2x16Op : SPIRV_GLOp<"PackHalf2x16", 58, [Pure]> {
1323+
let summary = "Pack two-component vector of 32-bit floats into a 32-bit integer";
1324+
1325+
let description = [{
1326+
Result is the unsigned integer obtained by converting the components of a
1327+
two-component floating-point vector to the 16-bit OpTypeFloat, and then packing
1328+
these two 16-bit integers into a 32-bit unsigned integer. The first vector
1329+
component specifies the 16 least-significant bits of the result; the second
1330+
component specifies the 16 most-significant bits.
1331+
1332+
The RelaxedPrecision Decoration only affects the conversion step of the instruction.
1333+
1334+
The v operand must be a vector of 2 components whose type is a 32-bit floating-point.
1335+
1336+
Result Type must be a 32-bit integer type.
1337+
1338+
#### Example:
1339+
1340+
```mlir
1341+
%1 = spirv.GL.PackHalf2x16 %0 : vector<2xf32> -> i32
1342+
```
1343+
}];
1344+
1345+
let arguments = (ins
1346+
VectorOfLengthAndType<[2], [SPIRV_Float32]>:$operand
1347+
);
1348+
1349+
let results = (outs
1350+
SPIRV_Int32:$result
1351+
);
1352+
1353+
let assemblyFormat = [{
1354+
attr-dict $operand `:` type($operand) `->` type($result)
1355+
}];
1356+
1357+
let hasVerifier = 0;
1358+
}
1359+
1360+
// -----
1361+
1362+
def SPIRV_GLUnpackHalf2x16Op : SPIRV_GLOp<"UnpackHalf2x16", 62, [Pure]> {
1363+
let summary = "Unpack 32-bit integer into two-component vector of 32-bit floats";
1364+
1365+
let description = [{
1366+
Result is the two-component floating-point vector with components obtained by
1367+
unpacking a 32-bit unsigned integer into a pair of 16-bit values, interpreting
1368+
those values as 16-bit floating-point numbers according to the OpenGL
1369+
Specification, and converting them to 32-bit floating-point values. Subnormal
1370+
numbers are either preserved or flushed to zero, consistently within an
1371+
implementation.
1372+
1373+
The first component of the vector is obtained from the 16 least-significant bits
1374+
of v; the second component is obtained from the 16 most-significant bits of v.
1375+
1376+
The RelaxedPrecision Decoration only affects the conversion step of the instruction.
1377+
1378+
The v operand must be a scalar with 32-bit integer type.
1379+
1380+
Result Type must be a vector of 2 components whose type is 32-bit floating point.
1381+
1382+
#### Example:
1383+
1384+
```mlir
1385+
%1 = spirv.GL.UnpackHalf2x16 %0 : i32 -> vector<2xf32>
1386+
```
1387+
}];
1388+
1389+
let arguments = (ins
1390+
SPIRV_Int32:$operand
1391+
);
1392+
1393+
let results = (outs
1394+
VectorOfLengthAndType<[2], [SPIRV_Float32]>:$result
1395+
);
1396+
1397+
let assemblyFormat = [{
1398+
attr-dict $operand `:` type($operand) `->` type($result)
1399+
}];
1400+
1401+
let hasVerifier = 0;
1402+
}
1403+
13201404
#endif // MLIR_DIALECT_SPIRV_IR_GL_OPS

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,3 +815,107 @@ func.func @exp2_invalid_type(%arg0 : i32) -> () {
815815
%0 = spirv.GL.Exp2 %arg0 : i32
816816
return
817817
}
818+
819+
// -----
820+
821+
//===----------------------------------------------------------------------===//
822+
// spirv.GL.PackHalf2x16
823+
//===----------------------------------------------------------------------===//
824+
825+
func.func @pack_half_2x16(%arg0 : vector<2xf32>) -> () {
826+
// CHECK: spirv.GL.PackHalf2x16 {{%.*}} : vector<2xf32> -> i32
827+
%0 = spirv.GL.PackHalf2x16 %arg0 : vector<2xf32> -> i32
828+
return
829+
}
830+
831+
// -----
832+
833+
func.func @pack_half_2x16_i16_output(%arg0 : vector<2xf32>) -> () {
834+
// expected-error @+1 {{op result #0 must be Int32, but got 'i16'}}
835+
%0 = spirv.GL.PackHalf2x16 %arg0 : vector<2xf32> -> i16
836+
return
837+
}
838+
839+
// -----
840+
841+
func.func @pack_half_2x16_wrong_vec_size(%arg0 : vector<3xf32>) -> () {
842+
// expected-error @+1 {{op operand #0 must be vector of Float32 values of length 2, but got 'vector<3xf32>'}}
843+
%0 = spirv.GL.PackHalf2x16 %arg0 : vector<3xf32> -> i32
844+
return
845+
}
846+
847+
// -----
848+
849+
func.func @pack_half_2x16_wrong_vec_type(%arg0 : vector<2xi32>) -> () {
850+
// expected-error @+1 {{op operand #0 must be vector of Float32 values of length 2, but got 'vector<2xi32>'}}
851+
%0 = spirv.GL.PackHalf2x16 %arg0 : vector<2xi32> -> i32
852+
return
853+
}
854+
855+
// -----
856+
857+
func.func @pack_half_2x16_scalar_in(%arg0 : f32) -> () {
858+
// expected-error @+1 {{invalid kind of type specified: expected builtin.vector, but found 'f32'}}
859+
%0 = spirv.GL.PackHalf2x16 %arg0 : f32 -> i32
860+
return
861+
}
862+
863+
// -----
864+
865+
func.func @unpack_half_2x16_vector_out(%arg0 : vector<2xf32>) -> () {
866+
// expected-error @+1 {{invalid kind of type specified: expected builtin.integer, but found 'vector<2xf32>'}}
867+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : vector<2xf32> -> vector<2xi32>
868+
return
869+
}
870+
871+
// -----
872+
873+
//===----------------------------------------------------------------------===//
874+
// spirv.GL.UnpackHalf2x16
875+
//===----------------------------------------------------------------------===//
876+
877+
func.func @unpack_half_2x16(%arg0 : i32) -> () {
878+
// CHECK: spirv.GL.UnpackHalf2x16 {{%.*}} : i32 -> vector<2xf32>
879+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i32 -> vector<2xf32>
880+
return
881+
}
882+
883+
// -----
884+
885+
func.func @unpack_half_2x16_i16_input(%arg0 : i16) -> () {
886+
// expected-error @+1 {{op operand #0 must be Int32, but got 'i16'}}
887+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i16 -> vector<2xf32>
888+
return
889+
}
890+
891+
// -----
892+
893+
func.func @unpack_half_2x16_wrong_vec_size(%arg0 : i32) -> () {
894+
// expected-error @+1 {{op result #0 must be vector of Float32 values of length 2, but got 'vector<3xf32>'}}
895+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i32 -> vector<3xf32>
896+
return
897+
}
898+
899+
// -----
900+
901+
func.func @unpack_half_2x16_wrong_vec_type(%arg0 : i32) -> () {
902+
// expected-error @+1 {{op result #0 must be vector of Float32 values of length 2, but got 'vector<2xi32>'}}
903+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i32 -> vector<2xi32>
904+
return
905+
}
906+
907+
// -----
908+
909+
func.func @unpack_half_2x16_vec_in(%arg0 : vector<2xf32>) -> () {
910+
// expected-error @+1 {{invalid kind of type specified: expected builtin.integer, but found 'vector<2xf32>'}}
911+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : vector<2xf32> -> vector<2xf32>
912+
return
913+
}
914+
915+
// -----
916+
917+
func.func @unpack_half_2x16_scalar_out(%arg0 : i32) -> () {
918+
// expected-error @+1 {{invalid kind of type specified: expected builtin.vector, but found 'f32'}}
919+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i32 -> f32
920+
return
921+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
9696
spirv.Return
9797
}
9898

99-
spirv.func @vector(%arg0 : f32, %arg1 : vector<3xf32>, %arg2 : vector<3xf32>) "None" {
99+
spirv.func @vector(%arg0 : f32, %arg1 : vector<3xf32>, %arg2 : vector<3xf32>) "None" {
100100
// CHECK: {{%.*}} = spirv.GL.Cross {{%.*}}, {{%.*}} : vector<3xf32>
101101
%0 = spirv.GL.Cross %arg1, %arg2 : vector<3xf32>
102102
// CHECK: {{%.*}} = spirv.GL.Normalize {{%.*}} : f32
@@ -114,5 +114,11 @@ spirv.func @vector(%arg0 : f32, %arg1 : vector<3xf32>, %arg2 : vector<3xf32>) "N
114114
spirv.Return
115115
}
116116

117-
117+
spirv.func @pack_half_2x16(%arg0 : i32) "None" {
118+
// CHECK: {{%.*}} = spirv.GL.UnpackHalf2x16 {{%.*}} : i32 -> vector<2xf32>
119+
%0 = spirv.GL.UnpackHalf2x16 %arg0 : i32 -> vector<2xf32>
120+
// CHECK: {{%.*}} = spirv.GL.PackHalf2x16 {{%.*}} : vector<2xf32> -> i32
121+
%1 = spirv.GL.PackHalf2x16 %0 : vector<2xf32> -> i32
122+
spirv.Return
123+
}
118124
}

0 commit comments

Comments
 (0)