Skip to content

Commit 19afd27

Browse files
authored
[Flang] Fix ACOSD and ASIND (fixes issue #145593) (#145656)
Original implementation converted DEG->RAD before calling ACOS/ASIN, but the conversion needs to happen after invoking ACOS/ASIN.
1 parent 721adc1 commit 19afd27

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,16 +2648,18 @@ mlir::Value IntrinsicLibrary::genAbs(mlir::Type resultType,
26482648
// ACOSD
26492649
mlir::Value IntrinsicLibrary::genAcosd(mlir::Type resultType,
26502650
llvm::ArrayRef<mlir::Value> args) {
2651+
// maps ACOSD to ACOS * 180 / pi
26512652
assert(args.size() == 1);
26522653
mlir::MLIRContext *context = builder.getContext();
26532654
mlir::FunctionType ftype =
26542655
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
2656+
mlir::Value result =
2657+
getRuntimeCallGenerator("acos", ftype)(builder, loc, {args[0]});
26552658
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
26562659
mlir::Value dfactor = builder.createRealConstant(
2657-
loc, mlir::Float64Type::get(context), pi / llvm::APFloat(180.0));
2660+
loc, mlir::Float64Type::get(context), llvm::APFloat(180.0) / pi);
26582661
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
2659-
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
2660-
return getRuntimeCallGenerator("acos", ftype)(builder, loc, {arg});
2662+
return builder.create<mlir::arith::MulFOp>(loc, result, factor);
26612663
}
26622664

26632665
// ADJUSTL & ADJUSTR
@@ -2799,16 +2801,18 @@ IntrinsicLibrary::genAny(mlir::Type resultType,
27992801
// ASIND
28002802
mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
28012803
llvm::ArrayRef<mlir::Value> args) {
2804+
// maps ASIND to ASIN * 180 / pi
28022805
assert(args.size() == 1);
28032806
mlir::MLIRContext *context = builder.getContext();
28042807
mlir::FunctionType ftype =
28052808
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
2809+
mlir::Value result =
2810+
getRuntimeCallGenerator("asin", ftype)(builder, loc, {args[0]});
28062811
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
28072812
mlir::Value dfactor = builder.createRealConstant(
2808-
loc, mlir::Float64Type::get(context), pi / llvm::APFloat(180.0));
2813+
loc, mlir::Float64Type::get(context), llvm::APFloat(180.0) / pi);
28092814
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
2810-
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
2811-
return getRuntimeCallGenerator("asin", ftype)(builder, loc, {arg});
2815+
return builder.create<mlir::arith::MulFOp>(loc, result, factor);
28122816
}
28132817

28142818
// ATAND, ATAN2D

flang/test/Lower/Intrinsics/acosd.f90

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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"
31
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
42

53
function test_real4(x)
@@ -8,17 +6,17 @@ function test_real4(x)
86
end function
97

108
! CHECK-LABEL: @_QPtest_real4
11-
! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
9+
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
10+
! CHECK: %[[result:.*]] = math.acos %{{.*}} fastmath<contract> : f32
1211
! 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
12+
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[factor]] fastmath<contract> : f32
1513

1614
function test_real8(x)
1715
real(8) :: x, test_real8
1816
test_real8 = acosd(x)
1917
end function
2018

2119
! 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
20+
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
21+
! CHECK: %[[result:.*]] = math.acos %{{.*}} fastmath<contract> : f64
22+
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[dfactor]] fastmath<contract> : f64

flang/test/Lower/Intrinsics/asind.f90

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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"
31
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
42

53
function test_real4(x)
@@ -8,17 +6,17 @@ function test_real4(x)
86
end function
97

108
! CHECK-LABEL: @_QPtest_real4
11-
! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
9+
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
10+
! CHECK: %[[result:.*]] = math.asin %{{.*}} fastmath<contract> : f32
1211
! 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
12+
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[factor]] fastmath<contract> : f32
1513

1614
function test_real8(x)
1715
real(8) :: x, test_real8
1816
test_real8 = asind(x)
1917
end function
2018

2119
! 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
20+
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
21+
! CHECK: %[[result:.*]] = math.asin %{{.*}} fastmath<contract> : f64
22+
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[dfactor]] fastmath<contract> : f64

0 commit comments

Comments
 (0)