Skip to content

[mlir][spirv] Add atan and atan2 pattern to MathToSPIRV Conversion pass #102633

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

Merged
merged 2 commits into from
Aug 12, 2024

Conversation

meehatpa
Copy link
Contributor

@meehatpa meehatpa commented Aug 9, 2024

Add missing math.atan to spirv.CL.atan and math.atan2 to spirv.CL.atan2 in MathToSPIRV.
Add math.atan to spirv.GL.atan too.

Copy link

github-actions bot commented Aug 9, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2024

@llvm/pr-subscribers-mlir-spirv

@llvm/pr-subscribers-mlir

Author: None (meehatpa)

Changes

Add missing math.atan to spirv.CL.atan and math.atan2 to spirv.CL.atan2 in MathToSPIRV.


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

2 Files Affected:

  • (modified) mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp (+2)
  • (modified) mlir/test/Conversion/MathToSPIRV/math-to-opencl-spirv.mlir (+51-39)
diff --git a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
index 0b29c93e2d8909..1260b27a0a751f 100644
--- a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
+++ b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRV.cpp
@@ -431,6 +431,8 @@ void populateMathToSPIRVPatterns(SPIRVTypeConverter &typeConverter,
   patterns.add<Log1pOpPattern<spirv::CLLogOp>, ExpM1OpPattern<spirv::CLExpOp>,
                CheckedElementwiseOpPattern<math::AbsFOp, spirv::CLFAbsOp>,
                CheckedElementwiseOpPattern<math::AbsIOp, spirv::CLSAbsOp>,
+               CheckedElementwiseOpPattern<math::AtanOp, spirv::CLAtanOp>,
+               CheckedElementwiseOpPattern<math::Atan2Op, spirv::CLAtan2Op>,
                CheckedElementwiseOpPattern<math::CeilOp, spirv::CLCeilOp>,
                CheckedElementwiseOpPattern<math::CosOp, spirv::CLCosOp>,
                CheckedElementwiseOpPattern<math::ErfOp, spirv::CLErfOp>,
diff --git a/mlir/test/Conversion/MathToSPIRV/math-to-opencl-spirv.mlir b/mlir/test/Conversion/MathToSPIRV/math-to-opencl-spirv.mlir
index 03a5c2f2bc9b15..e9ca838354c0de 100644
--- a/mlir/test/Conversion/MathToSPIRV/math-to-opencl-spirv.mlir
+++ b/mlir/test/Conversion/MathToSPIRV/math-to-opencl-spirv.mlir
@@ -4,83 +4,91 @@ module attributes { spirv.target_env = #spirv.target_env<#spirv.vce<v1.0, [Kerne
 
 // CHECK-LABEL: @float32_unary_scalar
 func.func @float32_unary_scalar(%arg0: f32) {
+  // CHECK: spirv.CL.atan %{{.*}}: f32
+  %0 = math.atan %arg0 : f32
   // CHECK: spirv.CL.cos %{{.*}}: f32
-  %0 = math.cos %arg0 : f32
+  %1 = math.cos %arg0 : f32
   // CHECK: spirv.CL.exp %{{.*}}: f32
-  %1 = math.exp %arg0 : f32
+  %2 = math.exp %arg0 : f32
   // CHECK: %[[EXP:.+]] = spirv.CL.exp %arg0
   // CHECK: %[[ONE:.+]] = spirv.Constant 1.000000e+00 : f32
   // CHECK: spirv.FSub %[[EXP]], %[[ONE]]
-  %2 = math.expm1 %arg0 : f32
+  %3 = math.expm1 %arg0 : f32
   // CHECK: spirv.CL.log %{{.*}}: f32
-  %3 = math.log %arg0 : f32
+  %4 = math.log %arg0 : f32
   // CHECK: %[[ONE:.+]] = spirv.Constant 1.000000e+00 : f32
   // CHECK: %[[ADDONE:.+]] = spirv.FAdd %[[ONE]], %{{.+}}
   // CHECK: spirv.CL.log %[[ADDONE]]
-  %4 = math.log1p %arg0 : f32
+  %5 = math.log1p %arg0 : f32
   // CHECK: spirv.CL.rint %{{.*}}: f32
-  %5 = math.roundeven %arg0 : f32
+  %6 = math.roundeven %arg0 : f32
   // CHECK: spirv.CL.rsqrt %{{.*}}: f32
-  %6 = math.rsqrt %arg0 : f32
+  %7 = math.rsqrt %arg0 : f32
   // CHECK: spirv.CL.sqrt %{{.*}}: f32
-  %7 = math.sqrt %arg0 : f32
+  %8 = math.sqrt %arg0 : f32
   // CHECK: spirv.CL.tanh %{{.*}}: f32
-  %8 = math.tanh %arg0 : f32
+  %9 = math.tanh %arg0 : f32
   // CHECK: spirv.CL.sin %{{.*}}: f32
-  %9 = math.sin %arg0 : f32
+  %10 = math.sin %arg0 : f32
   // CHECK: spirv.CL.fabs %{{.*}}: f32
-  %10 = math.absf %arg0 : f32
+  %11 = math.absf %arg0 : f32
   // CHECK: spirv.CL.ceil %{{.*}}: f32
-  %11 = math.ceil %arg0 : f32
+  %12 = math.ceil %arg0 : f32
   // CHECK: spirv.CL.floor %{{.*}}: f32
-  %12 = math.floor %arg0 : f32
+  %13 = math.floor %arg0 : f32
   // CHECK: spirv.CL.erf %{{.*}}: f32
-  %13 = math.erf %arg0 : f32
+  %14 = math.erf %arg0 : f32
   // CHECK: spirv.CL.round %{{.*}}: f32
-  %14 = math.round %arg0 : f32
+  %15 = math.round %arg0 : f32
   return
 }
 
 // CHECK-LABEL: @float32_unary_vector
 func.func @float32_unary_vector(%arg0: vector<3xf32>) {
+  // CHECK: spirv.CL.atan %{{.*}}: vector<3xf32>
+  %0 = math.atan %arg0 : vector<3xf32>
   // CHECK: spirv.CL.cos %{{.*}}: vector<3xf32>
-  %0 = math.cos %arg0 : vector<3xf32>
+  %1 = math.cos %arg0 : vector<3xf32>
   // CHECK: spirv.CL.exp %{{.*}}: vector<3xf32>
-  %1 = math.exp %arg0 : vector<3xf32>
+  %2 = math.exp %arg0 : vector<3xf32>
   // CHECK: %[[EXP:.+]] = spirv.CL.exp %arg0
   // CHECK: %[[ONE:.+]] = spirv.Constant dense<1.000000e+00> : vector<3xf32>
   // CHECK: spirv.FSub %[[EXP]], %[[ONE]]
-  %2 = math.expm1 %arg0 : vector<3xf32>
+  %3 = math.expm1 %arg0 : vector<3xf32>
   // CHECK: spirv.CL.log %{{.*}}: vector<3xf32>
-  %3 = math.log %arg0 : vector<3xf32>
+  %4 = math.log %arg0 : vector<3xf32>
   // CHECK: %[[ONE:.+]] = spirv.Constant dense<1.000000e+00> : vector<3xf32>
   // CHECK: %[[ADDONE:.+]] = spirv.FAdd %[[ONE]], %{{.+}}
   // CHECK: spirv.CL.log %[[ADDONE]]
-  %4 = math.log1p %arg0 : vector<3xf32>
+  %5 = math.log1p %arg0 : vector<3xf32>
   // CHECK: spirv.CL.rint %{{.*}}: vector<3xf32>
-  %5 = math.roundeven %arg0 : vector<3xf32>
+  %6 = math.roundeven %arg0 : vector<3xf32>
   // CHECK: spirv.CL.rsqrt %{{.*}}: vector<3xf32>
-  %6 = math.rsqrt %arg0 : vector<3xf32>
+  %7 = math.rsqrt %arg0 : vector<3xf32>
   // CHECK: spirv.CL.sqrt %{{.*}}: vector<3xf32>
-  %7 = math.sqrt %arg0 : vector<3xf32>
+  %8 = math.sqrt %arg0 : vector<3xf32>
   // CHECK: spirv.CL.tanh %{{.*}}: vector<3xf32>
-  %8 = math.tanh %arg0 : vector<3xf32>
+  %9 = math.tanh %arg0 : vector<3xf32>
   // CHECK: spirv.CL.sin %{{.*}}: vector<3xf32>
-  %9 = math.sin %arg0 : vector<3xf32>
+  %10 = math.sin %arg0 : vector<3xf32>
   return
 }
 
 // CHECK-LABEL: @float32_binary_scalar
 func.func @float32_binary_scalar(%lhs: f32, %rhs: f32) {
+  // CHECK: spirv.CL.atan2 %{{.*}}: f32
+  %0 = math.atan2 %lhs, %rhs : f32
   // CHECK: spirv.CL.pow %{{.*}}: f32
-  %0 = math.powf %lhs, %rhs : f32
+  %1 = math.powf %lhs, %rhs : f32
   return
 }
 
 // CHECK-LABEL: @float32_binary_vector
 func.func @float32_binary_vector(%lhs: vector<4xf32>, %rhs: vector<4xf32>) {
+  // CHECK: spirv.CL.atan2 %{{.*}}: vector<4xf32>
+  %0 = math.atan2 %lhs, %rhs : vector<4xf32>
   // CHECK: spirv.CL.pow %{{.*}}: vector<4xf32>
-  %0 = math.powf %lhs, %rhs : vector<4xf32>
+  %1 = math.powf %lhs, %rhs : vector<4xf32>
   return
 }
 
@@ -118,18 +126,20 @@ module attributes {
 
 // CHECK-LABEL: @vector_2d
 func.func @vector_2d(%arg0: vector<2x2xf32>) {
+  // CHECK-NEXT: math.atan {{.+}} : vector<2x2xf32>
+  %0 = math.atan %arg0 : vector<2x2xf32>
   // CHECK-NEXT: math.cos {{.+}} : vector<2x2xf32>
-  %0 = math.cos %arg0 : vector<2x2xf32>
+  %1 = math.cos %arg0 : vector<2x2xf32>
   // CHECK-NEXT: math.exp {{.+}} : vector<2x2xf32>
-  %1 = math.exp %arg0 : vector<2x2xf32>
+  %2 = math.exp %arg0 : vector<2x2xf32>
   // CHECK-NEXT: math.absf {{.+}} : vector<2x2xf32>
-  %2 = math.absf %arg0 : vector<2x2xf32>
+  %3 = math.absf %arg0 : vector<2x2xf32>
   // CHECK-NEXT: math.ceil {{.+}} : vector<2x2xf32>
-  %3 = math.ceil %arg0 : vector<2x2xf32>
+  %4 = math.ceil %arg0 : vector<2x2xf32>
   // CHECK-NEXT: math.floor {{.+}} : vector<2x2xf32>
-  %4 = math.floor %arg0 : vector<2x2xf32>
+  %5 = math.floor %arg0 : vector<2x2xf32>
   // CHECK-NEXT: math.powf {{.+}}, {{%.+}} : vector<2x2xf32>
-  %5 = math.powf %arg0, %arg0 : vector<2x2xf32>
+  %6 = math.powf %arg0, %arg0 : vector<2x2xf32>
   // CHECK-NEXT: return
   return
 }
@@ -138,18 +148,20 @@ func.func @vector_2d(%arg0: vector<2x2xf32>) {
 
 // CHECK-LABEL: @tensor_1d
 func.func @tensor_1d(%arg0: tensor<2xf32>) {
+  // CHECK-NEXT: math.atan {{.+}} : tensor<2xf32>
+  %0 = math.atan %arg0 : tensor<2xf32>
   // CHECK-NEXT: math.cos {{.+}} : tensor<2xf32>
-  %0 = math.cos %arg0 : tensor<2xf32>
+  %1 = math.cos %arg0 : tensor<2xf32>
   // CHECK-NEXT: math.exp {{.+}} : tensor<2xf32>
-  %1 = math.exp %arg0 : tensor<2xf32>
+  %2 = math.exp %arg0 : tensor<2xf32>
   // CHECK-NEXT: math.absf {{.+}} : tensor<2xf32>
-  %2 = math.absf %arg0 : tensor<2xf32>
+  %3 = math.absf %arg0 : tensor<2xf32>
   // CHECK-NEXT: math.ceil {{.+}} : tensor<2xf32>
-  %3 = math.ceil %arg0 : tensor<2xf32>
+  %4 = math.ceil %arg0 : tensor<2xf32>
   // CHECK-NEXT: math.floor {{.+}} : tensor<2xf32>
-  %4 = math.floor %arg0 : tensor<2xf32>
+  %5 = math.floor %arg0 : tensor<2xf32>
   // CHECK-NEXT: math.powf {{.+}}, {{%.+}} : tensor<2xf32>
-  %5 = math.powf %arg0, %arg0 : tensor<2xf32>
+  %6 = math.powf %arg0, %arg0 : tensor<2xf32>
   // CHECK-NEXT: return
   return
 }

kuhar
kuhar previously approved these changes Aug 9, 2024
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

@kuhar kuhar self-requested a review August 9, 2024 17:01
@kuhar kuhar dismissed their stale review August 9, 2024 17:02

See the new comment

@meehatpa
Copy link
Contributor Author

meehatpa commented Aug 9, 2024

Could we add glsl support in one sweep? https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.html#:~:text=%3Cid%3E%0Ax-,Atan%0A%0AArc%20tangent,-.%20Result%20is%20an

I updated it for glsl only for atan as atan2 op is not available in spirv.
Thanks for the quick review!

@kuhar
Copy link
Member

kuhar commented Aug 9, 2024

Could we add glsl support in one sweep? https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.html#:~:text=%3Cid%3E%0Ax-,Atan%0A%0AArc%20tangent,-.%20Result%20is%20an

I updated it for glsl only for atan as atan2 op is not available in spirv. Thanks for the quick review!

Can we use this one: https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.html#:~:text=Atan2%0A%0AArc%20tangent.%20Result%20is%20an%20angle ?

@meehatpa
Copy link
Contributor Author

meehatpa commented Aug 9, 2024

Could we add glsl support in one sweep? https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.html#:~:text=%3Cid%3E%0Ax-,Atan%0A%0AArc%20tangent,-.%20Result%20is%20an

I updated it for glsl only for atan as atan2 op is not available in spirv. Thanks for the quick review!

Can we use this one: https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.html#:~:text=Atan2%0A%0AArc%20tangent.%20Result%20is%20an%20angle ?

The issue is it is not available in spirv mlir dialect https://mlir.llvm.org/docs/Dialects/SPIR-V/. We would need to add that op in the dialect first.

Add missing math.atan to spirv.CL.atan and math.atan2 to spirv.CL.atan2
in MathToSPIRV.
@meehatpa
Copy link
Contributor Author

@kuhar would you please merge?

@kuhar kuhar merged commit 49777d7 into llvm:main Aug 12, 2024
6 of 7 checks passed
Copy link

@meehatpa Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@kuhar
Copy link
Member

kuhar commented Aug 12, 2024

@kuhar would you please merge?

Done

@meehatpa meehatpa deleted the atan_atan2_spirv branch August 12, 2024 15:10
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