Skip to content

Commit cba1b64

Browse files
authored
[flang] Implement ACOSD and ASIND (#80448)
This PR implements two missing intrinsics from F2023: ACOSD and ASIND. The implementation breaks them down to the existing ACOS and ASIN implementation.
1 parent cca3db9 commit cba1b64

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

flang/include/flang/Optimizer/Builder/IntrinsicCall.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ struct IntrinsicLibrary {
162162
/// if the argument is an integer, into llvm intrinsics if the argument is
163163
/// real and to the `hypot` math routine if the argument is of complex type.
164164
mlir::Value genAbs(mlir::Type, llvm::ArrayRef<mlir::Value>);
165+
mlir::Value genAcosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
165166
template <void (*CallRuntime)(fir::FirOpBuilder &, mlir::Location loc,
166167
mlir::Value, mlir::Value)>
167168
fir::ExtendedValue genAdjustRtCall(mlir::Type,
@@ -173,11 +174,12 @@ struct IntrinsicLibrary {
173174
llvm::ArrayRef<fir::ExtendedValue>);
174175
mlir::Value genAnint(mlir::Type, llvm::ArrayRef<mlir::Value>);
175176
fir::ExtendedValue genAny(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
176-
mlir::Value genAtand(mlir::Type, llvm::ArrayRef<mlir::Value>);
177177
fir::ExtendedValue
178178
genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
179+
mlir::Value genAsind(mlir::Type, llvm::ArrayRef<mlir::Value>);
179180
fir::ExtendedValue genAssociated(mlir::Type,
180181
llvm::ArrayRef<fir::ExtendedValue>);
182+
mlir::Value genAtand(mlir::Type, llvm::ArrayRef<mlir::Value>);
181183
fir::ExtendedValue genBesselJn(mlir::Type,
182184
llvm::ArrayRef<fir::ExtendedValue>);
183185
fir::ExtendedValue genBesselYn(mlir::Type,

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static constexpr IntrinsicHandler handlers[]{
111111
{"abort", &I::genAbort},
112112
{"abs", &I::genAbs},
113113
{"achar", &I::genChar},
114+
{"acosd", &I::genAcosd},
114115
{"adjustl",
115116
&I::genAdjustRtCall<fir::runtime::genAdjustL>,
116117
{{{"string", asAddr}}},
@@ -134,6 +135,7 @@ static constexpr IntrinsicHandler handlers[]{
134135
&I::genAny,
135136
{{{"mask", asAddr}, {"dim", asValue}}},
136137
/*isElemental=*/false},
138+
{"asind", &I::genAsind},
137139
{"associated",
138140
&I::genAssociated,
139141
{{{"pointer", asInquired}, {"target", asInquired}}},
@@ -2003,6 +2005,21 @@ mlir::Value IntrinsicLibrary::genAbs(mlir::Type resultType,
20032005
llvm_unreachable("unexpected type in ABS argument");
20042006
}
20052007

2008+
// ACOSD
2009+
mlir::Value IntrinsicLibrary::genAcosd(mlir::Type resultType,
2010+
llvm::ArrayRef<mlir::Value> args) {
2011+
assert(args.size() == 1);
2012+
mlir::MLIRContext *context = builder.getContext();
2013+
mlir::FunctionType ftype =
2014+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
2015+
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
2016+
mlir::Value dfactor = builder.createRealConstant(
2017+
loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0));
2018+
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
2019+
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
2020+
return getRuntimeCallGenerator("acos", ftype)(builder, loc, {arg});
2021+
}
2022+
20062023
// ADJUSTL & ADJUSTR
20072024
template <void (*CallRuntime)(fir::FirOpBuilder &, mlir::Location loc,
20082025
mlir::Value, mlir::Value)>
@@ -2139,6 +2156,22 @@ IntrinsicLibrary::genAny(mlir::Type resultType,
21392156
return readAndAddCleanUp(resultMutableBox, resultType, "ANY");
21402157
}
21412158

2159+
// ASIND
2160+
mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
2161+
llvm::ArrayRef<mlir::Value> args) {
2162+
assert(args.size() == 1);
2163+
mlir::MLIRContext *context = builder.getContext();
2164+
mlir::FunctionType ftype =
2165+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
2166+
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
2167+
mlir::Value dfactor = builder.createRealConstant(
2168+
loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0));
2169+
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
2170+
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
2171+
return getRuntimeCallGenerator("asin", ftype)(builder, loc, {arg});
2172+
}
2173+
2174+
// ATAND
21422175
mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
21432176
llvm::ArrayRef<mlir::Value> args) {
21442177
assert(args.size() == 1);

flang/test/Lower/Intrinsics/acosd.f90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
2+
! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
3+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
4+
5+
function test_real4(x)
6+
real :: x, test_real4
7+
test_real4 = acosd(x)
8+
end function
9+
10+
! CHECK-LABEL: @_QPtest_real4
11+
! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
12+
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
13+
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f32
14+
! CHECK-PRECISE: %{{.*}} = fir.call @acosf(%[[arg]]) fastmath<contract> : (f32) -> f32
15+
16+
function test_real8(x)
17+
real(8) :: x, test_real8
18+
test_real8 = acosd(x)
19+
end function
20+
21+
! CHECK-LABEL: @_QPtest_real8
22+
! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64
23+
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f64
24+
! CHECK-PRECISE: %{{.*}} = fir.call @acos(%[[arg]]) fastmath<contract> : (f64) -> f64

flang/test/Lower/Intrinsics/asind.f90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
2+
! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
3+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
4+
5+
function test_real4(x)
6+
real :: x, test_real4
7+
test_real4 = asind(x)
8+
end function
9+
10+
! CHECK-LABEL: @_QPtest_real4
11+
! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
12+
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
13+
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f32
14+
! CHECK-PRECISE: %{{.*}} = fir.call @asinf(%[[arg]]) fastmath<contract> : (f32) -> f32
15+
16+
function test_real8(x)
17+
real(8) :: x, test_real8
18+
test_real8 = asind(x)
19+
end function
20+
21+
! CHECK-LABEL: @_QPtest_real8
22+
! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64
23+
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f64
24+
! CHECK-PRECISE: %{{.*}} = fir.call @asin(%[[arg]]) fastmath<contract> : (f64) -> f64

0 commit comments

Comments
 (0)