Skip to content

Conversation

@kuhar
Copy link
Member

@kuhar kuhar commented Oct 20, 2025

This rewrite does not preserve numerics: for example, we'd expect the maximum fp value to yield Inf instead of identity.

GL.Length does not allow for fast math flags, so we need to remove this. Special cases (constants) can be handled via a folder if someone wants to implement one.

This rewrite does not preserve numerics: for example, we'd expect the
maximum fp value to yield Inf instead of identity.

`GL.Length` does not allow for fast math flags, so we need to remove
this. Special cases (constants) can be handled via a folder if someone wants to
implement one.
@kuhar kuhar requested review from IgWod-IMG and bjacob October 20, 2025 18:47
@kuhar kuhar marked this pull request as ready for review October 20, 2025 18:47
@kuhar kuhar requested a review from antiagainst as a code owner October 20, 2025 18:47
@llvmbot
Copy link
Member

llvmbot commented Oct 20, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-spirv

Author: Jakub Kuderski (kuhar)

Changes

This rewrite does not preserve numerics: for example, we'd expect the maximum fp value to yield Inf instead of identity.

GL.Length does not allow for fast math flags, so we need to remove this. Special cases (constants) can be handled via a folder if someone wants to implement one.


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

3 Files Affected:

  • (modified) mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.td (-8)
  • (modified) mlir/lib/Dialect/SPIRV/IR/SPIRVGLCanonicalization.cpp (+2-2)
  • (modified) mlir/test/Dialect/SPIRV/Transforms/gl-canonicalize.mlir (-22)
diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.td b/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.td
index 39fbab8f37a2e..e8d2274d29aa0 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.td
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVCanonicalization.td
@@ -75,11 +75,3 @@ def ConvertComparisonIntoClamp2_#CmpClampPair[0] : Pat<
         )),
     (CmpClampPair[1] $input, $min, $max)>;
 }
-
-//===----------------------------------------------------------------------===//
-// spirv.GL.Length -> spirv.GL.FAbs
-//===----------------------------------------------------------------------===//
-
-def ConvertGLLengthToGLFAbs : Pat<
-    (SPIRV_GLLengthOp SPIRV_Float:$operand),
-    (SPIRV_GLFAbsOp $operand)>;
diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVGLCanonicalization.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVGLCanonicalization.cpp
index 46acb8c156fc6..3ad8057a58dc9 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVGLCanonicalization.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVGLCanonicalization.cpp
@@ -34,8 +34,8 @@ void populateSPIRVGLCanonicalizationPatterns(RewritePatternSet &results) {
               ConvertComparisonIntoClamp2_SPIRV_SLessThanOp,
               ConvertComparisonIntoClamp2_SPIRV_SLessThanEqualOp,
               ConvertComparisonIntoClamp2_SPIRV_ULessThanOp,
-              ConvertComparisonIntoClamp2_SPIRV_ULessThanEqualOp,
-              ConvertGLLengthToGLFAbs>(results.getContext());
+              ConvertComparisonIntoClamp2_SPIRV_ULessThanEqualOp>(
+      results.getContext());
 }
 } // namespace spirv
 } // namespace mlir
diff --git a/mlir/test/Dialect/SPIRV/Transforms/gl-canonicalize.mlir b/mlir/test/Dialect/SPIRV/Transforms/gl-canonicalize.mlir
index 33b877667512e..c1447b38f0a48 100644
--- a/mlir/test/Dialect/SPIRV/Transforms/gl-canonicalize.mlir
+++ b/mlir/test/Dialect/SPIRV/Transforms/gl-canonicalize.mlir
@@ -177,25 +177,3 @@ func.func @clamp_ulessthanequal(%input: i32, %min: i32, %max: i32) -> i32 {
   // CHECK-NEXT: spirv.ReturnValue [[RES]]
   spirv.ReturnValue %2 : i32
 }
-
-// -----
-
-//===----------------------------------------------------------------------===//
-// spirv.GL.Length
-//===----------------------------------------------------------------------===//
-
-// CHECK-LABEL: @convert_length_into_fabs_scalar
-func.func @convert_length_into_fabs_scalar(%arg0 : f32) -> f32 {
-  //CHECK: spirv.GL.FAbs {{%.*}} : f32
-  //CHECK-NOT: spirv.GL.Length
-  %0 = spirv.GL.Length %arg0 : f32 -> f32
-  spirv.ReturnValue %0 : f32
-}
-
-// CHECK-LABEL: @dont_convert_length_into_fabs_vec
-func.func @dont_convert_length_into_fabs_vec(%arg0 : vector<3xf32>) -> f32 {
-  //CHECK: spirv.GL.Length {{%.*}} : vector<3xf32> -> f32
-  //CHECK-NOT: spirv.GL.FAbs
-  %0 = spirv.GL.Length %arg0 : vector<3xf32> -> f32
-  spirv.ReturnValue %0 : f32
-}

Copy link
Contributor

@bjacob bjacob left a comment

Choose a reason for hiding this comment

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

It's a shame though to lose a fast approximation that is perfectly good for many applications, particularly in graphics. Should this motivate a follow-up to support fast-math here (to the extent that LLVM IR is able to meaningfully represent fast-math) ?

@bjacob
Copy link
Contributor

bjacob commented Oct 20, 2025

Just realized that this was a canonicalization pattern.

That part was clearly wrong. Fast approximations are great, but they can't be canonicalizations unless they're proven to be exact.

@kuhar
Copy link
Member Author

kuhar commented Oct 20, 2025

The spirv spec doesn't support fast math for this op, so it'd be better to do this rewrite at a higher level before we lower to spirv

@kuhar kuhar merged commit ef87da0 into llvm:main Oct 20, 2025
15 of 16 checks passed
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
This rewrite does not preserve numerics: for example, we'd expect the
maximum fp value to yield Inf instead of identity.

`GL.Length` does not allow for fast math flags, so we need to remove
this. Special cases (constants) can be handled via a folder if someone
wants to implement one.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
This rewrite does not preserve numerics: for example, we'd expect the
maximum fp value to yield Inf instead of identity.

`GL.Length` does not allow for fast math flags, so we need to remove
this. Special cases (constants) can be handled via a folder if someone
wants to implement one.
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