[flang-commits] [flang] [Flang] Generate math.acos op for non-precise acos intrinsic calls (PR #123641)

via flang-commits flang-commits at lists.llvm.org
Mon Jan 20 08:19:22 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: Jan Leyonberg (jsjodin)

<details>
<summary>Changes</summary>

This patch changes the codgegn for non-precise acos calls to generate math.acos ops. This wasn't done before because the math dialect did not have a acos operation at the time.

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


5 Files Affected:

- (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+4-2) 
- (modified) flang/test/Lower/HLFIR/elemental-intrinsics.f90 (+1-1) 
- (modified) flang/test/Lower/Intrinsics/acos.f90 (+21-15) 
- (modified) flang/test/Lower/dummy-procedure.f90 (+1-1) 
- (modified) flang/test/Lower/trigonometric-intrinsics.f90 (+40) 


``````````diff
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 6a343645ab8786..4e5fa4f86371ad 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -1023,8 +1023,10 @@ static constexpr MathOperation mathOperations[] = {
     {"abs", "cabs", genFuncType<Ty::Real<8>, Ty::Complex<8>>,
      genComplexMathOp<mlir::complex::AbsOp>},
     {"abs", RTNAME_STRING(CAbsF128), FuncTypeReal16Complex16, genLibF128Call},
-    {"acos", "acosf", genFuncType<Ty::Real<4>, Ty::Real<4>>, genLibCall},
-    {"acos", "acos", genFuncType<Ty::Real<8>, Ty::Real<8>>, genLibCall},
+    {"acos", "acosf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
+     genMathOp<mlir::math::AcosOp>},
+    {"acos", "acos", genFuncType<Ty::Real<8>, Ty::Real<8>>,
+     genMathOp<mlir::math::AcosOp>},
     {"acos", RTNAME_STRING(AcosF128), FuncTypeReal16Real16, genLibF128Call},
     {"acos", "cacosf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>, genLibCall},
     {"acos", "cacos", genFuncType<Ty::Complex<8>, Ty::Complex<8>>, genLibCall},
diff --git a/flang/test/Lower/HLFIR/elemental-intrinsics.f90 b/flang/test/Lower/HLFIR/elemental-intrinsics.f90
index dd79688663cba4..689f0a08ca7ab5 100644
--- a/flang/test/Lower/HLFIR/elemental-intrinsics.f90
+++ b/flang/test/Lower/HLFIR/elemental-intrinsics.f90
@@ -15,7 +15,7 @@ subroutine simple_elemental(x,y)
 ! CHECK:  ^bb0(%[[VAL_9:.*]]: index):
 ! CHECK:    %[[VAL_10:.*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_9]])  : (!fir.ref<!fir.array<100xf32>>, index) -> !fir.ref<f32>
 ! CHECK:    %[[VAL_11:.*]] = fir.load %[[VAL_10]] : !fir.ref<f32>
-! CHECK:    %[[VAL_12:.*]] = fir.call @acosf(%[[VAL_11]]) fastmath<contract> : (f32) -> f32
+! CHECK:    %[[VAL_12:.*]] = math.acos %[[VAL_11]] fastmath<contract> : f32
 ! CHECK:    hlfir.yield_element %[[VAL_12]] : f32
 ! CHECK:  }
 ! CHECK: hlfir.assign
diff --git a/flang/test/Lower/Intrinsics/acos.f90 b/flang/test/Lower/Intrinsics/acos.f90
index d2ef8e1cef0a88..849d36ad1323b8 100644
--- a/flang/test/Lower/Intrinsics/acos.f90
+++ b/flang/test/Lower/Intrinsics/acos.f90
@@ -1,9 +1,9 @@
-! RUN: bbc -emit-fir %s -o - --math-runtime=fast | FileCheck --check-prefixes=ALL %s
-! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=fast %s -o - | FileCheck --check-prefixes=ALL %s
-! RUN: bbc -emit-fir %s -o - --math-runtime=relaxed | FileCheck --check-prefixes=ALL %s
-! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=relaxed %s -o - | FileCheck --check-prefixes=ALL %s
-! RUN: bbc -emit-fir %s -o - --math-runtime=precise | FileCheck --check-prefixes=ALL %s
-! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=precise %s -o - | FileCheck --check-prefixes=ALL %s
+! RUN: bbc -emit-fir %s -o - --math-runtime=fast | FileCheck --check-prefixes=ALL,FAST %s
+! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=fast %s -o - | FileCheck --check-prefixes=ALL,FAST %s
+! RUN: bbc -emit-fir %s -o - --math-runtime=relaxed | FileCheck --check-prefixes=ALL,RELAXED %s
+! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=relaxed %s -o - | FileCheck --check-prefixes=ALL,RELAXED %s
+! RUN: bbc -emit-fir %s -o - --math-runtime=precise | FileCheck --check-prefixes=ALL,PRECISE %s
+! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=precise %s -o - | FileCheck --check-prefixes=ALL,PRECISE %s
 
 function test_real4(x)
   real :: x, test_real4
@@ -11,15 +11,9 @@ function test_real4(x)
 end function
 
 ! ALL-LABEL: @_QPtest_real4
-! ALL: {{%[A-Za-z0-9._]+}} = fir.call @acosf({{%[A-Za-z0-9._]+}}) {{.*}}: (f32) -> f32
-
-function test_real8(x)
-  real(8) :: x, test_real8
-  test_real8 = acos(x)
-end function
-
-! ALL-LABEL: @_QPtest_real8
-! ALL: {{%[A-Za-z0-9._]+}} = fir.call @acos({{%[A-Za-z0-9._]+}}) {{.*}}: (f64) -> f64
+! FAST: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f32
+! RELAXED: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f32
+! PRECISE: {{%[A-Za-z0-9._]+}} = fir.call @acosf({{%[A-Za-z0-9._]+}}) {{.*}}: (f32) -> f32
 
 function test_complex4(x)
   complex :: x, test_complex4
@@ -37,3 +31,15 @@ function test_complex8(x)
 ! ALL-LABEL: @_QPtest_complex8
 ! ALL: {{%[A-Za-z0-9._]+}} = fir.call @cacos({{%[A-Za-z0-9._]+}}) {{.*}}: (complex<f64>) -> complex<f64>
 
+function test_real8(x)
+  real(8) :: x, test_real8
+  test_real8 = acos(x)
+end function
+
+! ALL-LABEL: @_QPtest_real8
+! FAST: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f64
+! RELAXED: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f64
+! PRECISE: {{%[A-Za-z0-9._]+}} = fir.call @acos({{%[A-Za-z0-9._]+}}) {{.*}}: (f64) -> f64
+
+! PRECISE-DAG: func.func private @acosf(f32) -> f32 attributes {fir.bindc_name = "acosf", fir.runtime}
+! PRECISE-DAG: func.func private @acos(f64) -> f64 attributes {fir.bindc_name = "acos", fir.runtime}
diff --git a/flang/test/Lower/dummy-procedure.f90 b/flang/test/Lower/dummy-procedure.f90
index 6874e8eca90b94..a84c351b1166b2 100644
--- a/flang/test/Lower/dummy-procedure.f90
+++ b/flang/test/Lower/dummy-procedure.f90
@@ -154,7 +154,7 @@ subroutine todo3(dummy_proc)
 
 ! CHECK-LABEL: func private @fir.acos.f32.ref_f32(%arg0: !fir.ref<f32>) -> f32
   !CHECK: %[[load:.*]] = fir.load %arg0
-  !CHECK: %[[res:.*]] = fir.call @acosf(%[[load]]) fastmath<contract> : (f32) -> f32
+  !CHECK: %[[res:.*]] = math.acos %[[load]] fastmath<contract> : f32
   !CHECK: return %[[res]] : f32
 
 ! CHECK-LABEL: func private @fir.atan2.f32.ref_f32.ref_f32(
diff --git a/flang/test/Lower/trigonometric-intrinsics.f90 b/flang/test/Lower/trigonometric-intrinsics.f90
index 731ec6bbf68557..d1edd4ef48dc30 100644
--- a/flang/test/Lower/trigonometric-intrinsics.f90
+++ b/flang/test/Lower/trigonometric-intrinsics.f90
@@ -87,6 +87,34 @@ subroutine cos_testcd(z)
   z = cos(z)
 end subroutine
 
+! CHECK-LABEL: acos_testr
+subroutine acos_testr(a, b)
+  real :: a, b
+! CHECK: fir.call @fir.acos.contract.f32.f32
+  b = acos(a)
+end subroutine
+
+! CHECK-LABEL: acos_testd
+subroutine acos_testd(a, b)
+  real(kind=8) :: a, b
+! CHECK: fir.call @fir.acos.contract.f64.f64
+  b = acos(a)
+end subroutine
+
+! CHECK-LABEL: acos_testc
+subroutine acos_testc(z)
+  complex :: z
+! CHECK: fir.call @fir.acos.contract.z32.z32
+  z = acos(z)
+end subroutine
+
+! CHECK-LABEL: acos_testcd
+subroutine acos_testcd(z)
+  complex(kind=8) :: z
+! CHECK: fir.call @fir.acos.contract.z64.z64
+  z = acos(z)
+end subroutine
+
 ! CHECK-LABEL: cosh_testr
 subroutine cosh_testr(a, b)
   real :: a, b
@@ -211,6 +239,18 @@ subroutine sinh_testcd(z)
 ! CMPLX-FAST: complex.cos %{{.*}} : complex<f64>
 ! CMPLX-PRECISE: fir.call @ccos
 
+! CHECK-LABEL: @fir.acos.contract.f32.f32
+! CHECK: math.acos {{.*}} : f32
+
+! CHECK-LABEL: @fir.acos.contract.f64.f64
+! CHECK: math.acos {{.*}} : f64
+
+! CHECK-LABEL: @fir.acos.contract.z32.z32
+! CHECK: fir.call @cacosf
+
+! CHECK-LABEL: @fir.acos.contract.z64.z64
+! CHECK: fir.call @cacos
+
 ! CHECK-LABEL: @fir.cosh.contract.f32.f32
 ! CHECK: math.cosh {{.*}} : f32
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/123641


More information about the flang-commits mailing list