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

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.
This commit is contained in:
Jan Leyonberg
2025-01-23 10:23:50 -05:00
committed by GitHub
parent c3b40c7ea2
commit f61d93ffc4
5 changed files with 67 additions and 19 deletions

View File

@@ -1027,8 +1027,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},

View File

@@ -15,7 +15,7 @@ end subroutine
! 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

View File

@@ -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 @@ end function
! 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}

View File

@@ -154,7 +154,7 @@ end subroutine
! 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(

View File

@@ -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 @@ end subroutine
! 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