Skip to content

Commit

Permalink
[Flang] Lower sin, cos intrinsics
Browse files Browse the repository at this point in the history
The intrinsic computes the sin, cosine values. By default they are lowered
to runtime calls to the pgmath library, for llvm lowering they are
lowered to llvm intrinsics. The generic and llvm lowering does not
lower floating point types with kind greater than 8, the llvm lowering
does not support the complex types.

This is part of the upstreaming effort from the fir-dev branch in [1].
[1] https://github.com/flang-compiler/f18-llvm-project

Reviewed By: clementval

Differential Revision: https://reviews.llvm.org/D122320

Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
  • Loading branch information
kiranchandramohan and schweitzpgi committed Mar 23, 2022
1 parent 94fd00f commit 7a9891c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
4 changes: 4 additions & 0 deletions flang/lib/Lower/IntrinsicCall.cpp
Expand Up @@ -1019,6 +1019,8 @@ static constexpr RuntimeFunction llvmIntrinsics[] = {
// ceil is used for CEILING but is different, it returns a real.
{"ceil", "llvm.ceil.f32", genF32F32FuncType},
{"ceil", "llvm.ceil.f64", genF64F64FuncType},
{"cos", "llvm.cos.f32", genF32F32FuncType},
{"cos", "llvm.cos.f64", genF64F64FuncType},
{"cosh", "coshf", genF32F32FuncType},
{"cosh", "cosh", genF64F64FuncType},
{"exp", "llvm.exp.f32", genF32F32FuncType},
Expand All @@ -1040,6 +1042,8 @@ static constexpr RuntimeFunction llvmIntrinsics[] = {
{"sign", "llvm.copysign.f64", genF64F64F64FuncType},
{"sign", "llvm.copysign.f80", genF80F80F80FuncType},
{"sign", "llvm.copysign.f128", genF128F128F128FuncType},
{"sin", "llvm.sin.f32", genF32F32FuncType},
{"sin", "llvm.sin.f64", genF64F64FuncType},
{"sinh", "sinhf", genF32F32FuncType},
{"sinh", "sinh", genF64F64FuncType},
{"sqrt", "llvm.sqrt.f32", genF32F32FuncType},
Expand Down
40 changes: 40 additions & 0 deletions flang/test/Lower/llvm-math.f90
Expand Up @@ -175,4 +175,44 @@ SUBROUTINE SQRTD_WRAPPER(IN, OUT)
! CHECK-LABEL: func private @fir.sqrt.f64.f64(%arg0: f64)
! CHECK-NEXT: %0 = fir.call @llvm.sqrt.f64(%arg0) : (f64) -> f64
! CHECK-NEXT: return %0 : f64
! CHECK-NEXT: }

SUBROUTINE COS_WRAPPER(IN, OUT)
REAL :: IN, OUT
OUT = COS(IN)
END SUBROUTINE

! CHECK-LABEL: func private @fir.cos.f32.f32(%arg0: f32)
! CHECK-NEXT: %0 = fir.call @llvm.cos.f32(%arg0) : (f32) -> f32
! CHECK-NEXT: return %0 : f32
! CHECK-NEXT: }

SUBROUTINE COSD_WRAPPER(IN, OUT)
REAL(KIND=8) :: IN, OUT
OUT = COS(IN)
END SUBROUTINE

! CHECK-LABEL: func private @fir.cos.f64.f64(%arg0: f64)
! CHECK-NEXT: %0 = fir.call @llvm.cos.f64(%arg0) : (f64) -> f64
! CHECK-NEXT: return %0 : f64
! CHECK-NEXT: }

SUBROUTINE SIN_WRAPPER(IN, OUT)
REAL :: IN, OUT
OUT = SIN(IN)
END SUBROUTINE

! CHECK-LABEL: func private @fir.sin.f32.f32(%arg0: f32)
! CHECK-NEXT: %0 = fir.call @llvm.sin.f32(%arg0) : (f32) -> f32
! CHECK-NEXT: return %0 : f32
! CHECK-NEXT: }

SUBROUTINE SIND_WRAPPER(IN, OUT)
REAL(KIND=8) :: IN, OUT
OUT = SIN(IN)
END SUBROUTINE

! CHECK-LABEL: func private @fir.sin.f64.f64(%arg0: f64)
! CHECK-NEXT: %0 = fir.call @llvm.sin.f64(%arg0) : (f64) -> f64
! CHECK-NEXT: return %0 : f64
! CHECK-NEXT: }
80 changes: 80 additions & 0 deletions flang/test/Lower/trigonometric-intrinsics.f90
Expand Up @@ -29,6 +29,34 @@ subroutine atan_testcd(z)
z = atan(z)
end subroutine

! CHECK-LABEL: cos_testr
subroutine cos_testr(a, b)
real :: a, b
! CHECK: fir.call @fir.cos.f32.f32
b = cos(a)
end subroutine

! CHECK-LABEL: cos_testd
subroutine cos_testd(a, b)
real(kind=8) :: a, b
! CHECK: fir.call @fir.cos.f64.f64
b = cos(a)
end subroutine

! CHECK-LABEL: cos_testc
subroutine cos_testc(z)
complex :: z
! CHECK: fir.call @fir.cos.z4.z4
z = cos(z)
end subroutine

! CHECK-LABEL: cos_testcd
subroutine cos_testcd(z)
complex(kind=8) :: z
! CHECK: fir.call @fir.cos.z8.z8
z = cos(z)
end subroutine

! CHECK-LABEL: cosh_testr
subroutine cosh_testr(a, b)
real :: a, b
Expand Down Expand Up @@ -57,6 +85,34 @@ subroutine cosh_testcd(z)
z = cosh(z)
end subroutine

! CHECK-LABEL: sin_testr
subroutine sin_testr(a, b)
real :: a, b
! CHECK: fir.call @fir.sin.f32.f32
b = sin(a)
end subroutine

! CHECK-LABEL: sin_testd
subroutine sin_testd(a, b)
real(kind=8) :: a, b
! CHECK: fir.call @fir.sin.f64.f64
b = sin(a)
end subroutine

! CHECK-LABEL: sin_testc
subroutine sin_testc(z)
complex :: z
! CHECK: fir.call @fir.sin.z4.z4
z = sin(z)
end subroutine

! CHECK-LABEL: sin_testcd
subroutine sin_testcd(z)
complex(kind=8) :: z
! CHECK: fir.call @fir.sin.z8.z8
z = sin(z)
end subroutine

! CHECK-LABEL: sinh_testr
subroutine sinh_testr(a, b)
real :: a, b
Expand Down Expand Up @@ -97,6 +153,18 @@ subroutine sinh_testcd(z)
! CHECK-LABEL: @fir.atan.z8.z8
! CHECK: fir.call {{.*}}atan

! CHECK-LABEL: @fir.cos.f32.f32
! CHECK: fir.call {{.*}}cos

! CHECK-LABEL: @fir.cos.f64.f64
! CHECK: fir.call {{.*}}cos

! CHECK-LABEL: @fir.cos.z4.z4
! CHECK: fir.call {{.*}}cos

! CHECK-LABEL: @fir.cos.z8.z8
! CHECK: fir.call {{.*}}cos

! CHECK-LABEL: @fir.cosh.f32.f32
! CHECK: fir.call {{.*}}cosh

Expand All @@ -109,6 +177,18 @@ subroutine sinh_testcd(z)
! CHECK-LABEL: @fir.cosh.z8.z8
! CHECK: fir.call {{.*}}cosh

! CHECK-LABEL: @fir.sin.f32.f32
! CHECK: fir.call {{.*}}sin

! CHECK-LABEL: @fir.sin.f64.f64
! CHECK: fir.call {{.*}}sin

! CHECK-LABEL: @fir.sin.z4.z4
! CHECK: fir.call {{.*}}sin

! CHECK-LABEL: @fir.sin.z8.z8
! CHECK: fir.call {{.*}}sin

! CHECK-LABEL: @fir.sinh.f32.f32
! CHECK: fir.call {{.*}}sinh

Expand Down

0 comments on commit 7a9891c

Please sign in to comment.