Skip to content

[mlir] Add math to LLVM lowering support for missing trigonometric & hyperbolic ops #125753

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

Conversation

PaulCarabas
Copy link
Contributor

@PaulCarabas PaulCarabas commented Feb 4, 2025

The patch adds support for math -> LLVM dialect lowering for TanOp, Sinh, Cosh, Tanh

Copy link

github-actions bot commented Feb 4, 2025

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.

@PaulCarabas PaulCarabas force-pushed the math_to_llvm_trig_hyperbolic_ops_lowering branch from db8b3ab to 3baf642 Compare February 5, 2025 09:13
@PaulCarabas PaulCarabas marked this pull request as ready for review February 5, 2025 09:14
@llvmbot llvmbot added the mlir label Feb 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-mlir

Author: Paul Carabas (PaulCarabas)

Changes

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

2 Files Affected:

  • (modified) mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp (+9-1)
  • (modified) mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir (+24-2)
diff --git a/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp b/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
index 668f8385ac2dcf4..98680773e00d2ac 100644
--- a/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
+++ b/mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
@@ -39,6 +39,7 @@ using CeilOpLowering = ConvertFMFMathToLLVMPattern<math::CeilOp, LLVM::FCeilOp>;
 using CopySignOpLowering =
     ConvertFMFMathToLLVMPattern<math::CopySignOp, LLVM::CopySignOp>;
 using CosOpLowering = ConvertFMFMathToLLVMPattern<math::CosOp, LLVM::CosOp>;
+using CoshOpLowering = ConvertFMFMathToLLVMPattern<math::CoshOp, LLVM::CoshOp>;
 using CtPopFOpLowering =
     VectorConvertToLLVMPattern<math::CtPopOp, LLVM::CtPopOp>;
 using Exp2OpLowering = ConvertFMFMathToLLVMPattern<math::Exp2Op, LLVM::Exp2Op>;
@@ -58,9 +59,12 @@ using RoundEvenOpLowering =
 using RoundOpLowering =
     ConvertFMFMathToLLVMPattern<math::RoundOp, LLVM::RoundOp>;
 using SinOpLowering = ConvertFMFMathToLLVMPattern<math::SinOp, LLVM::SinOp>;
+using SinhOpLowering = ConvertFMFMathToLLVMPattern<math::SinhOp, LLVM::SinhOp>;
 using SqrtOpLowering = ConvertFMFMathToLLVMPattern<math::SqrtOp, LLVM::SqrtOp>;
 using FTruncOpLowering =
     ConvertFMFMathToLLVMPattern<math::TruncOp, LLVM::FTruncOp>;
+using TanOpLowering = ConvertFMFMathToLLVMPattern<math::TanOp, LLVM::TanOp>;
+using TanhOpLowering = ConvertFMFMathToLLVMPattern<math::TanhOp, LLVM::TanhOp>;
 
 // A `CtLz/CtTz/absi(a)` is converted into `CtLz/CtTz/absi(a, false)`.
 template <typename MathOp, typename LLVMOp>
@@ -310,6 +314,7 @@ void mlir::populateMathToLLVMConversionPatterns(
     CeilOpLowering,
     CopySignOpLowering,
     CosOpLowering,
+    CoshOpLowering,
     CountLeadingZerosOpLowering,
     CountTrailingZerosOpLowering,
     CtPopFOpLowering,
@@ -327,8 +332,11 @@ void mlir::populateMathToLLVMConversionPatterns(
     RoundOpLowering,
     RsqrtOpLowering,
     SinOpLowering,
+    SinhOpLowering,
     SqrtOpLowering,
-    FTruncOpLowering
+    FTruncOpLowering,
+    TanOpLowering,
+    TanhOpLowering
   >(converter);
   // clang-format on
 }
diff --git a/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir b/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
index 56129dbd2788923..24eef9341bf74f2 100644
--- a/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
+++ b/mlir/test/Conversion/MathToLLVM/math-to-llvm.mlir
@@ -161,11 +161,33 @@ func.func @rsqrt(%arg0 : f32) {
 
 // -----
 
-// CHECK-LABEL: func @sine(
+// CHECK-LABEL: func @trigonometrics(
 // CHECK-SAME: f32
-func.func @sine(%arg0 : f32) {
+func.func @trigonometrics(%arg0 : f32) {
   // CHECK: llvm.intr.sin(%arg0) : (f32) -> f32
   %0 = math.sin %arg0 : f32
+
+  // CHECK: llvm.intr.cos(%arg0) : (f32) -> f32
+  %1 = math.cos %arg0 : f32
+
+  // CHECK: llvm.intr.tan(%arg0) : (f32) -> f32
+  %2 = math.tan %arg0 : f32
+  func.return
+}
+
+// -----
+
+// CHECK-LABEL: func @hyperbolics(
+// CHECK-SAME: f32
+func.func @hyperbolics(%arg0 : f32) {
+  // CHECK: llvm.intr.sinh(%arg0) : (f32) -> f32
+  %0 = math.sinh %arg0 : f32
+
+  // CHECK: llvm.intr.cosh(%arg0) : (f32) -> f32
+  %1 = math.cosh %arg0 : f32
+
+  // CHECK: llvm.intr.tanh(%arg0) : (f32) -> f32
+  %2 = math.tanh %arg0 : f32
   func.return
 }
 

@PaulCarabas PaulCarabas changed the title Add math to LLVM lowering support for missing trigonometric & hyperbolic ops [mlir] Add math to LLVM lowering support for missing trigonometric & hyperbolic ops Feb 5, 2025
@PaulCarabas PaulCarabas force-pushed the math_to_llvm_trig_hyperbolic_ops_lowering branch from 3baf642 to 15dba10 Compare February 5, 2025 09:18
@PaulCarabas
Copy link
Contributor Author

@matthias-springer @tpopp @zero9178 could any of you please take a look?

Copy link
Member

@zero9178 zero9178 left a comment

Choose a reason for hiding this comment

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

LGTM!

@zero9178
Copy link
Member

zero9178 commented Feb 5, 2025

The build failure in flang seems to be real as they seem to have their own lowerings for math to LLVM.

Can these be removed from flang now? IIRC the LLVM lowerings lower to libc calls if the target has no instruction for the intrinsic.
CCing @clementval @jeanPerier for visibility

@PaulCarabas
Copy link
Contributor Author

The build failure in flang seems to be real as they seem to have their own lowerings for math to LLVM.

Can these be removed from flang now? IIRC the LLVM lowerings lower to libc calls if the target has no instruction for the intrinsic. CCing @clementval @jeanPerier for visibility

Thanks @zero9178 for checking out the failure.
Would modifying the flang test to reflect that the math ops now lower to intrinsics instead of the calls be sufficient?

@PaulCarabas PaulCarabas force-pushed the math_to_llvm_trig_hyperbolic_ops_lowering branch from 8f7e6d7 to c60f3c5 Compare February 5, 2025 15:46
@llvmbot llvmbot added the flang Flang issues not falling into any other category label Feb 5, 2025
…hyperbolic ops

Update flang test to reflect changes
@PaulCarabas PaulCarabas force-pushed the math_to_llvm_trig_hyperbolic_ops_lowering branch from c60f3c5 to 5ba25f2 Compare February 5, 2025 17:55
@zero9178
Copy link
Member

zero9178 commented Feb 5, 2025

The build failure in flang seems to be real as they seem to have their own lowerings for math to LLVM.
Can these be removed from flang now? IIRC the LLVM lowerings lower to libc calls if the target has no instruction for the intrinsic. CCing @clementval @jeanPerier for visibility

Thanks @zero9178 for checking out the failure. Would modifying the flang test to reflect that the math ops now lower to intrinsics instead of the calls be sufficient?

I suspect this to be the right thing, but ideally one of the flang developers would be able to tell

@clementval
Copy link
Contributor

The build failure in flang seems to be real as they seem to have their own lowerings for math to LLVM.
Can these be removed from flang now? IIRC the LLVM lowerings lower to libc calls if the target has no instruction for the intrinsic. CCing @clementval @jeanPerier for visibility

Thanks @zero9178 for checking out the failure. Would modifying the flang test to reflect that the math ops now lower to intrinsics instead of the calls be sufficient?

I suspect this to be the right thing, but ideally one of the flang developers would be able to tell

I think @vzakhari has done part of the Math work in flang.

@clementval clementval requested a review from vzakhari February 5, 2025 19:39
@vzakhari
Copy link
Contributor

vzakhari commented Feb 5, 2025

Hi @zero9178,

The build failure in flang

Where can I see the details of the failure?

@zero9178
Copy link
Member

zero9178 commented Feb 5, 2025

Hi @zero9178,

The build failure in flang

Where can I see the details of the failure?

The build failures were with a previous revision of this PR that did not yet incorporate the test changes in the current version.
Please see the current version of the PR for the test changes and whether it is okay to call intrinsics rather than libc calls (e.g., does flang rely here on errno being set?).
Otherwise, I suspect there might also be dead patterns in flang now that could be cleaned up (in either this PR or a separate commit)

@vzakhari
Copy link
Contributor

vzakhari commented Feb 5, 2025

It should be okay to convert math operations to LLVM dialect intrinsics. As long as math dialect does not model/care about errno, the LLVM dialect intrinsics are suitable.

The build failure that you refer to, is it about the actual compiler build or about flang LIT tests?

@clementval
Copy link
Contributor

The build failure that you refer to, is it about the actual compiler build or about flang LIT tests?

I think this was LIT tests failures.

@vzakhari
Copy link
Contributor

vzakhari commented Feb 5, 2025

The build failure that you refer to, is it about the actual compiler build or about flang LIT tests?

I think this was LIT tests failures.

Thanks, Valentin! I suppose Intrinsics/math-codegen.fir would fail either way (whether this conversion generates calls or llvm.intr operations), so it should just be fixed accordingly.

@PaulCarabas
Copy link
Contributor Author

PaulCarabas commented Feb 5, 2025

The build failure that you refer to, is it about the actual compiler build or about flang LIT tests?

I think this was LIT tests failures.

Thanks, Valentin! I suppose Intrinsics/math-codegen.fir would fail either way (whether this conversion generates calls or llvm.intr operations), so it should just be fixed accordingly.

Yes, it was just the LIT test which was failing (Intrinsics/math-codegen.fir). The actual failure can be seen here.

Thanks all for your input! The PR checks are fully green now - if everything's fine, can anybody help with the merge, please?

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

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

I will merge it. Thank you for the changes!

@vzakhari vzakhari merged commit df1bee0 into llvm:main Feb 6, 2025
8 checks passed
Copy link

github-actions bot commented Feb 6, 2025

@PaulCarabas 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!

Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…hyperbolic ops (llvm#125753)

The patch adds support for math -> LLVM dialect lowering for TanOp,
Sinh, Cosh, Tanh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang Flang issues not falling into any other category mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants