Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][spirv] Add definition for GL Fract #132921

Merged
merged 1 commit into from
Mar 25, 2025

Conversation

IgWod-IMG
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2025

@llvm/pr-subscribers-mlir-spirv

@llvm/pr-subscribers-mlir

Author: Igor Wodiany (IgWod-IMG)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/132921.diff

3 Files Affected:

  • (modified) mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td (+25)
  • (modified) mlir/test/Dialect/SPIRV/IR/gl-ops.mlir (+26)
  • (modified) mlir/test/Target/SPIRV/gl-ops.mlir (+2)
diff --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td
index 1cdfa02f81787..d6dc0e0d7d000 100644
--- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td
+++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVGLOps.td
@@ -1161,4 +1161,29 @@ def SPIRV_GLFindUMsbOp : SPIRV_GLUnaryArithmeticOp<"FindUMsb", 75, SPIRV_Int32>
   }];
 }
 
+// ----
+
+def SPIRV_GLFractOp : SPIRV_GLUnaryArithmeticOp<"Fract", 10, SPIRV_Float> {
+  let summary = "Returns the x - floor(x) of the operand";
+
+  let description = [{
+    Result is x - floor x.
+
+    fract(±0) = +0.
+
+    fract(±Inf) = NaN.
+
+    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
+    %result = spirv.GL.Sqrt %x : f32
+    %result = spirv.GL.Sqrt %x : vector<3xf16>
+    ```
+  }];
+}
+
 #endif // MLIR_DIALECT_SPIRV_IR_GL_OPS
diff --git a/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir b/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir
index beda3872bc8d2..0be047932c1f3 100644
--- a/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir
+++ b/mlir/test/Dialect/SPIRV/IR/gl-ops.mlir
@@ -663,3 +663,29 @@ func.func @reflect_invalid_type(%arg0 : i32, %arg1 : i32) {
   %0 = spirv.GL.Reflect %arg0, %arg1 : i32
   return
 }
+
+// -----
+
+//===----------------------------------------------------------------------===//
+// spirv.GL.Fract
+//===----------------------------------------------------------------------===//
+
+func.func @fract(%arg0 : f32) -> () {
+  // CHECK: spirv.GL.Fract {{%.*}} : f32
+  %0 = spirv.GL.Fract %arg0 : f32
+  return
+}
+
+func.func @fractvec(%arg0 : vector<3xf16>) -> () {
+  // CHECK: spirv.GL.Fract {{%.*}} : vector<3xf16>
+  %0 = spirv.GL.Fract %arg0 : vector<3xf16>
+  return
+}
+
+// -----
+
+func.func @fract_invalid_type(%arg0 : i32) {
+  // expected-error @+1 {{'spirv.GL.Fract' op operand #0 must be 16/32/64-bit float or vector of 16/32/64-bit float values}}
+  %0 = spirv.GL.Fract %arg0 : i32
+  return
+}
diff --git a/mlir/test/Target/SPIRV/gl-ops.mlir b/mlir/test/Target/SPIRV/gl-ops.mlir
index 119304cea7d4a..7f9771220b75d 100644
--- a/mlir/test/Target/SPIRV/gl-ops.mlir
+++ b/mlir/test/Target/SPIRV/gl-ops.mlir
@@ -32,6 +32,8 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
     %14 = spirv.GL.Ldexp %arg0 : f32, %arg2 : i32 -> f32
     // CHECK: {{%.*}} = spirv.GL.FMix {{%.*}} : f32, {{%.*}} : f32, {{%.*}} : f32 -> f32
     %15 = spirv.GL.FMix %arg0 : f32, %arg1 : f32, %arg0 : f32 -> f32
+    // CHECK: {{%.*}} = spirv.GL.Fract {{%.*}} : f32
+    %16 = spirv.GL.Fract %arg0 : f32
     spirv.Return
   }
 

Comment on lines 1170 to 1174
Result is x - floor x.

fract(±0) = +0.

fract(±Inf) = NaN.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could wrap this in a code block maybe to make the formatting nicer to read on the website -- it doesn't have to be 1:1 import from the spirv spec

@IgWod-IMG
Copy link
Contributor Author

I have updated the description and pushed an update. Let me know if you had something else in mind.

@IgWod-IMG IgWod-IMG merged commit 7b3885d into llvm:main Mar 25, 2025
11 checks passed
@IgWod-IMG IgWod-IMG deleted the img_fract branch March 25, 2025 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants