Skip to content

[Flang] Generate math.acos op for non-precise acos intrinsic calls #123641

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 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/HLFIR/elemental-intrinsics.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 21 additions & 15 deletions flang/test/Lower/Intrinsics/acos.f90
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
! 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
test_real4 = acos(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
Expand All @@ -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}
2 changes: 1 addition & 1 deletion flang/test/Lower/dummy-procedure.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 40 additions & 0 deletions flang/test/Lower/trigonometric-intrinsics.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Loading