[Mlir-commits] [mlir] [mlir][SPIR-V] Add GL Trunc and CL trunc ops (PR #200738)

Arseniy Obolenskiy llvmlistbot at llvm.org
Mon Jun 1 00:08:34 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/200738

None

>From 2a4e71a5606637126df881b5f57aa458d32d4ea1 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Mon, 1 Jun 2026 09:07:16 +0200
Subject: [PATCH] [mlir][SPIR-V] Add GL Trunc and CL trunc ops

---
 .../mlir/Dialect/SPIRV/IR/SPIRVCLOps.td       | 21 ++++++++++++++++
 .../mlir/Dialect/SPIRV/IR/SPIRVGLOps.td       | 24 +++++++++++++++++++
 mlir/test/Dialect/SPIRV/IR/gl-ops.mlir        | 18 ++++++++++++++
 mlir/test/Dialect/SPIRV/IR/ocl-ops.mlir       | 20 ++++++++++++++++
 mlir/test/Target/SPIRV/gl-ops.mlir            |  2 ++
 mlir/test/Target/SPIRV/ocl-ops.mlir           |  2 ++
 6 files changed, 87 insertions(+)

diff --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVCLOps.td b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVCLOps.td
index 37989e6e7e54a..6ecf85adaaf6d 100644
--- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVCLOps.td
+++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVCLOps.td
@@ -1037,6 +1037,27 @@ def SPIRV_CLTanhOp : SPIRV_CLUnaryArithmeticOp<"tanh", 63, SPIRV_Float> {
 
 // -----
 
+def SPIRV_CLTruncOp : SPIRV_CLUnaryArithmeticOp<"trunc", 66, SPIRV_Float> {
+  let summary = "Round to integral value using the round to zero rounding mode.";
+
+  let description = [{
+    Result Type and x must be floating-point or vector(2,3,4,8,16) of
+    floating-point values.
+
+    All of the operands, including the Result Type operand, must be of the
+    same type.
+
+    #### Example:
+
+    ```mlir
+    %2 = spirv.CL.trunc %0 : f32
+    %3 = spirv.CL.trunc %1 : vector<3xf16>
+    ```
+  }];
+}
+
+// -----
+
 def SPIRV_CLUMaxOp : SPIRV_CLBinaryArithmeticOp<"u_max", 157, SPIRV_Integer> {
   let summary = "Return maximum of two unsigned integer operands";
 
diff --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td
index 01fe12a4660af..f08d2c01f2317 100644
--- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td
+++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td
@@ -418,6 +418,30 @@ def SPIRV_GLRoundEvenOp: SPIRV_GLUnaryArithmeticOp<"RoundEven", 2, SPIRV_Float>
 
 // -----
 
+def SPIRV_GLTruncOp: SPIRV_GLUnaryArithmeticOp<"Trunc", 3, SPIRV_Float> {
+  let summary = "Rounds to the nearest whole number toward zero";
+
+  let description = [{
+    Result is the value equal to the nearest whole number to x whose absolute
+    value is not larger than the absolute value of x.
+
+    The operand x must be a scalar or vector whose component type is
+    floating-point.
+
+    Result Type and the type of x must be the same type. Results are computed
+    per component.
+
+    #### Example:
+
+    ```mlir
+    %2 = spirv.GL.Trunc %0 : f32
+    %3 = spirv.GL.Trunc %1 : vector<3xf16>
+    ```
+  }];
+}
+
+// -----
+
 def SPIRV_GLInverseSqrtOp : SPIRV_GLUnaryArithmeticOp<"InverseSqrt", 32, SPIRV_Float> {
   let summary = "Reciprocal of sqrt(operand)";
 
diff --git a/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir b/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir
index eea80ca3798a6..61942e1369dab 100644
--- a/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir
+++ b/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir
@@ -371,6 +371,24 @@ func.func @round_even_vec(%arg0 : vector<3xf16>) -> () {
 
 // -----
 
+//===----------------------------------------------------------------------===//
+// spirv.GL.Trunc
+//===----------------------------------------------------------------------===//
+
+func.func @trunc(%arg0 : f32) -> () {
+  // CHECK: spirv.GL.Trunc {{%.*}} : f32
+  %2 = spirv.GL.Trunc %arg0 : f32
+  return
+}
+
+func.func @trunc_vec(%arg0 : vector<3xf16>) -> () {
+  // CHECK: spirv.GL.Trunc {{%.*}} : vector<3xf16>
+  %2 = spirv.GL.Trunc %arg0 : vector<3xf16>
+  return
+}
+
+// -----
+
 //===----------------------------------------------------------------------===//
 // spirv.GL.FClamp
 //===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/SPIRV/IR/ocl-ops.mlir b/mlir/test/Dialect/SPIRV/IR/ocl-ops.mlir
index 68751211c5fa6..f4612d9c3bf4f 100644
--- a/mlir/test/Dialect/SPIRV/IR/ocl-ops.mlir
+++ b/mlir/test/Dialect/SPIRV/IR/ocl-ops.mlir
@@ -520,6 +520,26 @@ func.func @rintvec(%arg0 : vector<3xf16>) -> () {
 
 // -----
 
+//===----------------------------------------------------------------------===//
+// spirv.CL.trunc
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: func.func @trunc(
+func.func @trunc(%arg0 : f32) -> () {
+  // CHECK: spirv.CL.trunc {{%.*}} : f32
+  %0 = spirv.CL.trunc %arg0 : f32
+  return
+}
+
+// CHECK-LABEL: func.func @truncvec(
+func.func @truncvec(%arg0 : vector<3xf16>) -> () {
+  // CHECK: spirv.CL.trunc {{%.*}} : vector<3xf16>
+  %0 = spirv.CL.trunc %arg0 : vector<3xf16>
+  return
+}
+
+// -----
+
 //===----------------------------------------------------------------------===//
 // spirv.CL.printf
 //===----------------------------------------------------------------------===//
diff --git a/mlir/test/Target/SPIRV/gl-ops.mlir b/mlir/test/Target/SPIRV/gl-ops.mlir
index 3255044770159..041e40ad1249e 100644
--- a/mlir/test/Target/SPIRV/gl-ops.mlir
+++ b/mlir/test/Target/SPIRV/gl-ops.mlir
@@ -31,6 +31,8 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
     %11 = spirv.GL.Pow %arg0, %arg1 : f32
     // CHECK: {{%.*}} = spirv.GL.Round {{%.*}} : f32
     %12 = spirv.GL.Round %arg0 : f32
+    // CHECK: {{%.*}} = spirv.GL.Trunc {{%.*}} : f32
+    %trunc = spirv.GL.Trunc %arg0 : f32
     // CHECK: {{%.*}} = spirv.GL.FrexpStruct {{%.*}} : f32 -> !spirv.struct<(f32, i32)>
     %13 = spirv.GL.FrexpStruct %arg0 : f32 -> !spirv.struct<(f32, i32)>
     // CHECK: {{%.*}} = spirv.GL.Ldexp {{%.*}} : f32, {{%.*}} : i32 -> f32
diff --git a/mlir/test/Target/SPIRV/ocl-ops.mlir b/mlir/test/Target/SPIRV/ocl-ops.mlir
index e43223e65db5c..227ec5093deaf 100644
--- a/mlir/test/Target/SPIRV/ocl-ops.mlir
+++ b/mlir/test/Target/SPIRV/ocl-ops.mlir
@@ -37,6 +37,8 @@ spirv.module Physical64 OpenCL requires #spirv.vce<v1.0, [Kernel, Addresses, Vec
     %9 = spirv.CL.rsqrt %arg0 : f32
     // CHECK: {{%.*}} = spirv.CL.erf {{%.*}} : f32
     %10 = spirv.CL.erf %arg0 : f32
+    // CHECK: {{%.*}} = spirv.CL.trunc {{%.*}} : f32
+    %11 = spirv.CL.trunc %arg0 : f32
     spirv.Return
   }
 



More information about the Mlir-commits mailing list