Skip to content

Commit f61d93f

Browse files
authored
[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.
1 parent c3b40c7 commit f61d93f

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,10 @@ static constexpr MathOperation mathOperations[] = {
10271027
{"abs", "cabs", genFuncType<Ty::Real<8>, Ty::Complex<8>>,
10281028
genComplexMathOp<mlir::complex::AbsOp>},
10291029
{"abs", RTNAME_STRING(CAbsF128), FuncTypeReal16Complex16, genLibF128Call},
1030-
{"acos", "acosf", genFuncType<Ty::Real<4>, Ty::Real<4>>, genLibCall},
1031-
{"acos", "acos", genFuncType<Ty::Real<8>, Ty::Real<8>>, genLibCall},
1030+
{"acos", "acosf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
1031+
genMathOp<mlir::math::AcosOp>},
1032+
{"acos", "acos", genFuncType<Ty::Real<8>, Ty::Real<8>>,
1033+
genMathOp<mlir::math::AcosOp>},
10321034
{"acos", RTNAME_STRING(AcosF128), FuncTypeReal16Real16, genLibF128Call},
10331035
{"acos", "cacosf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>, genLibCall},
10341036
{"acos", "cacos", genFuncType<Ty::Complex<8>, Ty::Complex<8>>, genLibCall},

flang/test/Lower/HLFIR/elemental-intrinsics.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ subroutine simple_elemental(x,y)
1515
! CHECK: ^bb0(%[[VAL_9:.*]]: index):
1616
! CHECK: %[[VAL_10:.*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_9]]) : (!fir.ref<!fir.array<100xf32>>, index) -> !fir.ref<f32>
1717
! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_10]] : !fir.ref<f32>
18-
! CHECK: %[[VAL_12:.*]] = fir.call @acosf(%[[VAL_11]]) fastmath<contract> : (f32) -> f32
18+
! CHECK: %[[VAL_12:.*]] = math.acos %[[VAL_11]] fastmath<contract> : f32
1919
! CHECK: hlfir.yield_element %[[VAL_12]] : f32
2020
! CHECK: }
2121
! CHECK: hlfir.assign

flang/test/Lower/Intrinsics/acos.f90

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
! RUN: bbc -emit-fir %s -o - --math-runtime=fast | FileCheck --check-prefixes=ALL %s
2-
! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=fast %s -o - | FileCheck --check-prefixes=ALL %s
3-
! RUN: bbc -emit-fir %s -o - --math-runtime=relaxed | FileCheck --check-prefixes=ALL %s
4-
! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=relaxed %s -o - | FileCheck --check-prefixes=ALL %s
5-
! RUN: bbc -emit-fir %s -o - --math-runtime=precise | FileCheck --check-prefixes=ALL %s
6-
! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=precise %s -o - | FileCheck --check-prefixes=ALL %s
1+
! RUN: bbc -emit-fir %s -o - --math-runtime=fast | FileCheck --check-prefixes=ALL,FAST %s
2+
! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=fast %s -o - | FileCheck --check-prefixes=ALL,FAST %s
3+
! RUN: bbc -emit-fir %s -o - --math-runtime=relaxed | FileCheck --check-prefixes=ALL,RELAXED %s
4+
! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=relaxed %s -o - | FileCheck --check-prefixes=ALL,RELAXED %s
5+
! RUN: bbc -emit-fir %s -o - --math-runtime=precise | FileCheck --check-prefixes=ALL,PRECISE %s
6+
! RUN: %flang_fc1 -emit-fir -mllvm -math-runtime=precise %s -o - | FileCheck --check-prefixes=ALL,PRECISE %s
77

88
function test_real4(x)
99
real :: x, test_real4
1010
test_real4 = acos(x)
1111
end function
1212

1313
! ALL-LABEL: @_QPtest_real4
14-
! ALL: {{%[A-Za-z0-9._]+}} = fir.call @acosf({{%[A-Za-z0-9._]+}}) {{.*}}: (f32) -> f32
15-
16-
function test_real8(x)
17-
real(8) :: x, test_real8
18-
test_real8 = acos(x)
19-
end function
20-
21-
! ALL-LABEL: @_QPtest_real8
22-
! ALL: {{%[A-Za-z0-9._]+}} = fir.call @acos({{%[A-Za-z0-9._]+}}) {{.*}}: (f64) -> f64
14+
! FAST: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f32
15+
! RELAXED: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f32
16+
! PRECISE: {{%[A-Za-z0-9._]+}} = fir.call @acosf({{%[A-Za-z0-9._]+}}) {{.*}}: (f32) -> f32
2317

2418
function test_complex4(x)
2519
complex :: x, test_complex4
@@ -37,3 +31,15 @@ function test_complex8(x)
3731
! ALL-LABEL: @_QPtest_complex8
3832
! ALL: {{%[A-Za-z0-9._]+}} = fir.call @cacos({{%[A-Za-z0-9._]+}}) {{.*}}: (complex<f64>) -> complex<f64>
3933

34+
function test_real8(x)
35+
real(8) :: x, test_real8
36+
test_real8 = acos(x)
37+
end function
38+
39+
! ALL-LABEL: @_QPtest_real8
40+
! FAST: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f64
41+
! RELAXED: {{%[A-Za-z0-9._]+}} = math.acos {{%[A-Za-z0-9._]+}} {{.*}}: f64
42+
! PRECISE: {{%[A-Za-z0-9._]+}} = fir.call @acos({{%[A-Za-z0-9._]+}}) {{.*}}: (f64) -> f64
43+
44+
! PRECISE-DAG: func.func private @acosf(f32) -> f32 attributes {fir.bindc_name = "acosf", fir.runtime}
45+
! PRECISE-DAG: func.func private @acos(f64) -> f64 attributes {fir.bindc_name = "acos", fir.runtime}

flang/test/Lower/dummy-procedure.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ subroutine todo3(dummy_proc)
154154

155155
! CHECK-LABEL: func private @fir.acos.f32.ref_f32(%arg0: !fir.ref<f32>) -> f32
156156
!CHECK: %[[load:.*]] = fir.load %arg0
157-
!CHECK: %[[res:.*]] = fir.call @acosf(%[[load]]) fastmath<contract> : (f32) -> f32
157+
!CHECK: %[[res:.*]] = math.acos %[[load]] fastmath<contract> : f32
158158
!CHECK: return %[[res]] : f32
159159

160160
! CHECK-LABEL: func private @fir.atan2.f32.ref_f32.ref_f32(

flang/test/Lower/trigonometric-intrinsics.f90

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,34 @@ subroutine cos_testcd(z)
8787
z = cos(z)
8888
end subroutine
8989

90+
! CHECK-LABEL: acos_testr
91+
subroutine acos_testr(a, b)
92+
real :: a, b
93+
! CHECK: fir.call @fir.acos.contract.f32.f32
94+
b = acos(a)
95+
end subroutine
96+
97+
! CHECK-LABEL: acos_testd
98+
subroutine acos_testd(a, b)
99+
real(kind=8) :: a, b
100+
! CHECK: fir.call @fir.acos.contract.f64.f64
101+
b = acos(a)
102+
end subroutine
103+
104+
! CHECK-LABEL: acos_testc
105+
subroutine acos_testc(z)
106+
complex :: z
107+
! CHECK: fir.call @fir.acos.contract.z32.z32
108+
z = acos(z)
109+
end subroutine
110+
111+
! CHECK-LABEL: acos_testcd
112+
subroutine acos_testcd(z)
113+
complex(kind=8) :: z
114+
! CHECK: fir.call @fir.acos.contract.z64.z64
115+
z = acos(z)
116+
end subroutine
117+
90118
! CHECK-LABEL: cosh_testr
91119
subroutine cosh_testr(a, b)
92120
real :: a, b
@@ -211,6 +239,18 @@ subroutine sinh_testcd(z)
211239
! CMPLX-FAST: complex.cos %{{.*}} : complex<f64>
212240
! CMPLX-PRECISE: fir.call @ccos
213241

242+
! CHECK-LABEL: @fir.acos.contract.f32.f32
243+
! CHECK: math.acos {{.*}} : f32
244+
245+
! CHECK-LABEL: @fir.acos.contract.f64.f64
246+
! CHECK: math.acos {{.*}} : f64
247+
248+
! CHECK-LABEL: @fir.acos.contract.z32.z32
249+
! CHECK: fir.call @cacosf
250+
251+
! CHECK-LABEL: @fir.acos.contract.z64.z64
252+
! CHECK: fir.call @cacos
253+
214254
! CHECK-LABEL: @fir.cosh.contract.f32.f32
215255
! CHECK: math.cosh {{.*}} : f32
216256

0 commit comments

Comments
 (0)