Skip to content

Commit 40b722d

Browse files
authored
[mlir][SPIR-V] Add GL Trunc and CL trunc ops (llvm#200738)
1 parent abdab06 commit 40b722d

6 files changed

Lines changed: 87 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,27 @@ def SPIRV_CLTanhOp : SPIRV_CLUnaryArithmeticOp<"tanh", 63, SPIRV_Float> {
10371037

10381038
// -----
10391039

1040+
def SPIRV_CLTruncOp : SPIRV_CLUnaryArithmeticOp<"trunc", 66, SPIRV_Float> {
1041+
let summary = "Round to integral value using the round to zero rounding mode.";
1042+
1043+
let description = [{
1044+
Result Type and x must be floating-point or vector(2,3,4,8,16) of
1045+
floating-point values.
1046+
1047+
All of the operands, including the Result Type operand, must be of the
1048+
same type.
1049+
1050+
#### Example:
1051+
1052+
```mlir
1053+
%2 = spirv.CL.trunc %0 : f32
1054+
%3 = spirv.CL.trunc %1 : vector<3xf16>
1055+
```
1056+
}];
1057+
}
1058+
1059+
// -----
1060+
10401061
def SPIRV_CLUMaxOp : SPIRV_CLBinaryArithmeticOp<"u_max", 157, SPIRV_Integer> {
10411062
let summary = "Return maximum of two unsigned integer operands";
10421063

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,30 @@ def SPIRV_GLRoundEvenOp: SPIRV_GLUnaryArithmeticOp<"RoundEven", 2, SPIRV_Float>
418418

419419
// -----
420420

421+
def SPIRV_GLTruncOp: SPIRV_GLUnaryArithmeticOp<"Trunc", 3, SPIRV_Float> {
422+
let summary = "Rounds to the nearest whole number toward zero";
423+
424+
let description = [{
425+
Result is the value equal to the nearest whole number to x whose absolute
426+
value is not larger than the absolute value of x.
427+
428+
The operand x must be a scalar or vector whose component type is
429+
floating-point.
430+
431+
Result Type and the type of x must be the same type. Results are computed
432+
per component.
433+
434+
#### Example:
435+
436+
```mlir
437+
%2 = spirv.GL.Trunc %0 : f32
438+
%3 = spirv.GL.Trunc %1 : vector<3xf16>
439+
```
440+
}];
441+
}
442+
443+
// -----
444+
421445
def SPIRV_GLInverseSqrtOp : SPIRV_GLUnaryArithmeticOp<"InverseSqrt", 32, SPIRV_Float> {
422446
let summary = "Reciprocal of sqrt(operand)";
423447

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,24 @@ func.func @round_even_vec(%arg0 : vector<3xf16>) -> () {
371371

372372
// -----
373373

374+
//===----------------------------------------------------------------------===//
375+
// spirv.GL.Trunc
376+
//===----------------------------------------------------------------------===//
377+
378+
func.func @trunc(%arg0 : f32) -> () {
379+
// CHECK: spirv.GL.Trunc {{%.*}} : f32
380+
%2 = spirv.GL.Trunc %arg0 : f32
381+
return
382+
}
383+
384+
func.func @trunc_vec(%arg0 : vector<3xf16>) -> () {
385+
// CHECK: spirv.GL.Trunc {{%.*}} : vector<3xf16>
386+
%2 = spirv.GL.Trunc %arg0 : vector<3xf16>
387+
return
388+
}
389+
390+
// -----
391+
374392
//===----------------------------------------------------------------------===//
375393
// spirv.GL.FClamp
376394
//===----------------------------------------------------------------------===//

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,26 @@ func.func @rintvec(%arg0 : vector<3xf16>) -> () {
520520

521521
// -----
522522

523+
//===----------------------------------------------------------------------===//
524+
// spirv.CL.trunc
525+
//===----------------------------------------------------------------------===//
526+
527+
// CHECK-LABEL: func.func @trunc(
528+
func.func @trunc(%arg0 : f32) -> () {
529+
// CHECK: spirv.CL.trunc {{%.*}} : f32
530+
%0 = spirv.CL.trunc %arg0 : f32
531+
return
532+
}
533+
534+
// CHECK-LABEL: func.func @truncvec(
535+
func.func @truncvec(%arg0 : vector<3xf16>) -> () {
536+
// CHECK: spirv.CL.trunc {{%.*}} : vector<3xf16>
537+
%0 = spirv.CL.trunc %arg0 : vector<3xf16>
538+
return
539+
}
540+
541+
// -----
542+
523543
//===----------------------------------------------------------------------===//
524544
// spirv.CL.printf
525545
//===----------------------------------------------------------------------===//

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
3131
%11 = spirv.GL.Pow %arg0, %arg1 : f32
3232
// CHECK: {{%.*}} = spirv.GL.Round {{%.*}} : f32
3333
%12 = spirv.GL.Round %arg0 : f32
34+
// CHECK: {{%.*}} = spirv.GL.Trunc {{%.*}} : f32
35+
%trunc = spirv.GL.Trunc %arg0 : f32
3436
// CHECK: {{%.*}} = spirv.GL.FrexpStruct {{%.*}} : f32 -> !spirv.struct<(f32, i32)>
3537
%13 = spirv.GL.FrexpStruct %arg0 : f32 -> !spirv.struct<(f32, i32)>
3638
// CHECK: {{%.*}} = spirv.GL.Ldexp {{%.*}} : f32, {{%.*}} : i32 -> f32

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ spirv.module Physical64 OpenCL requires #spirv.vce<v1.0, [Kernel, Addresses, Vec
3737
%9 = spirv.CL.rsqrt %arg0 : f32
3838
// CHECK: {{%.*}} = spirv.CL.erf {{%.*}} : f32
3939
%10 = spirv.CL.erf %arg0 : f32
40+
// CHECK: {{%.*}} = spirv.CL.trunc {{%.*}} : f32
41+
%11 = spirv.CL.trunc %arg0 : f32
4042
spirv.Return
4143
}
4244

0 commit comments

Comments
 (0)