Skip to content

[mlir][complex] Convert complex.tan to libm ctan call #78250

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 1 commit into from
Jan 18, 2024

Conversation

Lewuathe
Copy link
Member

We can convert complex.tan op to ctan/ctanf function in libm in the complex to libm conversion.

@llvmbot llvmbot added the mlir label Jan 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 16, 2024

@llvm/pr-subscribers-mlir

Author: Kai Sasaki (Lewuathe)

Changes

We can convert complex.tan op to ctan/ctanf function in libm in the complex to libm conversion.


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

2 Files Affected:

  • (modified) mlir/lib/Conversion/ComplexToLibm/ComplexToLibm.cpp (+4-1)
  • (modified) mlir/test/Conversion/ComplexToLibm/convert-to-libm.mlir (+14)
diff --git a/mlir/lib/Conversion/ComplexToLibm/ComplexToLibm.cpp b/mlir/lib/Conversion/ComplexToLibm/ComplexToLibm.cpp
index b3762f0293492c..f2b50d109f8f62 100644
--- a/mlir/lib/Conversion/ComplexToLibm/ComplexToLibm.cpp
+++ b/mlir/lib/Conversion/ComplexToLibm/ComplexToLibm.cpp
@@ -117,6 +117,8 @@ void mlir::populateComplexToLibmConversionPatterns(RewritePatternSet &patterns,
       patterns.getContext(), "cabsf", "cabs", benefit);
   patterns.add<ScalarOpToLibmCall<complex::AngleOp, FloatTypeResolver>>(
       patterns.getContext(), "cargf", "carg", benefit);
+  patterns.add<ScalarOpToLibmCall<complex::TanOp>>(patterns.getContext(),
+                                                   "ctanf", "ctan", benefit);
 }
 
 namespace {
@@ -136,7 +138,8 @@ void ConvertComplexToLibmPass::runOnOperation() {
   target.addLegalDialect<func::FuncDialect>();
   target.addIllegalOp<complex::PowOp, complex::SqrtOp, complex::TanhOp,
                       complex::CosOp, complex::SinOp, complex::ConjOp,
-                      complex::LogOp, complex::AbsOp, complex::AngleOp>();
+                      complex::LogOp, complex::AbsOp, complex::AngleOp,
+                      complex::TanOp>();
   if (failed(applyPartialConversion(module, target, std::move(patterns))))
     signalPassFailure();
 }
diff --git a/mlir/test/Conversion/ComplexToLibm/convert-to-libm.mlir b/mlir/test/Conversion/ComplexToLibm/convert-to-libm.mlir
index 0a19751324d8b2..ecf243c53a80cc 100644
--- a/mlir/test/Conversion/ComplexToLibm/convert-to-libm.mlir
+++ b/mlir/test/Conversion/ComplexToLibm/convert-to-libm.mlir
@@ -12,6 +12,8 @@
 // CHECK-DAG: @cabs(complex<f64>) -> f64
 // CHECK-DAG: @carg(complex<f64>) -> f64
 // CHECK-DAG: @clog(complex<f64>) -> complex<f64>
+// CHECK-DAG: @ctanf(complex<f32>) -> complex<f32>
+// CHECK-DAG: @ctan(complex<f64>) -> complex<f64>
 
 // CHECK-LABEL: func @cpow_caller
 // CHECK-SAME: %[[FLOAT:.*]]: complex<f32>
@@ -120,3 +122,15 @@ func.func @clog_caller(%float: complex<f32>, %double: complex<f64>) -> (complex<
   // CHECK: return %[[FLOAT_RESULT]], %[[DOUBLE_RESULT]]
   return %float_result, %double_result : complex<f32>, complex<f64>
 }
+
+// CHECK-LABEL: func @ctan_caller
+// CHECK-SAME: %[[FLOAT:.*]]: complex<f32>
+// CHECK-SAME: %[[DOUBLE:.*]]: complex<f64>
+func.func @ctan_caller(%float: complex<f32>, %double: complex<f64>) -> (complex<f32>, complex<f64>)  {
+  // CHECK: %[[FLOAT_RESULT:.*]] = call @ctanf(%[[FLOAT]])
+  %float_result = complex.tan %float : complex<f32>
+  // CHECK: %[[DOUBLE_RESULT:.*]] = call @ctan(%[[DOUBLE]])
+  %double_result = complex.tan %double : complex<f64>
+  // CHECK: return %[[FLOAT_RESULT]], %[[DOUBLE_RESULT]]
+  return %float_result, %double_result : complex<f32>, complex<f64>
+}

@Lewuathe Lewuathe merged commit fdcb76f into llvm:main Jan 18, 2024
@Lewuathe Lewuathe deleted the libm-ctan-support branch January 18, 2024 03:21
@Lewuathe
Copy link
Member Author

@joker-eph Thank you for the quick review!

ampandey-1995 pushed a commit to ampandey-1995/llvm-project that referenced this pull request Jan 19, 2024
We can convert `complex.tan` op to
[ctan/ctanf](https://sourceware.org/newlib/libm.html#ctan) function in
libm in the complex to libm conversion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants