From 6560adb5840596afbeb3f4574bf6d0c52e439d69 Mon Sep 17 00:00:00 2001 From: Connector Switch Date: Fri, 22 Aug 2025 15:55:46 +0800 Subject: [PATCH] [flang] optimize atand/atan2d precision (#154544) Part of https://github.com/llvm/llvm-project/issues/150452. --- flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 9 ++--- flang/test/Lower/Intrinsics/atan2d.f90 | 12 +++---- flang/test/Lower/Intrinsics/atand.f90 | 35 ++++++++++++++----- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp index cc3ae31ff466..7e987de94c5d 100644 --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -2917,10 +2917,11 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType, mlir::FunctionType::get(context, {resultType}, {args[0].getType()}); atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args); } - llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi); - mlir::Value dfactor = builder.createRealConstant( - loc, mlir::Float64Type::get(context), llvm::APFloat(180.0) / pi); - mlir::Value factor = builder.createConvert(loc, resultType, dfactor); + const llvm::fltSemantics &fltSem = + llvm::cast(resultType).getFloatSemantics(); + llvm::APFloat pi = llvm::APFloat(fltSem, llvm::numbers::pis); + mlir::Value factor = builder.createRealConstant( + loc, resultType, llvm::APFloat(fltSem, "180.0") / pi); return mlir::arith::MulFOp::create(builder, loc, atan, factor); } diff --git a/flang/test/Lower/Intrinsics/atan2d.f90 b/flang/test/Lower/Intrinsics/atan2d.f90 index 6ebf2976266a..ea9174243c45 100644 --- a/flang/test/Lower/Intrinsics/atan2d.f90 +++ b/flang/test/Lower/Intrinsics/atan2d.f90 @@ -1,21 +1,19 @@ ! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" - -function test_real4(y,x) +function test_real4(y, x) real(4) :: x, y, test_real4 - test_real4 = atan2d(y,x) + test_real4 = atan2d(y, x) end function ! CHECK-LABEL: @_QPtest_real4 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32 -! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64 -! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 +! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath : f32 -function test_real8(y,x) +function test_real8(y, x) real(8) :: x, y, test_real8 - test_real8 = atan2d(y,x) + test_real8 = atan2d(y, x) end function ! CHECK-LABEL: @_QPtest_real8 diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90 index 07ea56eff1e4..45303228a42b 100644 --- a/flang/test/Lower/Intrinsics/atand.f90 +++ b/flang/test/Lower/Intrinsics/atand.f90 @@ -1,3 +1,4 @@ +! REQUIRES: flang-supports-f128-math ! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" ! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE" ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" @@ -10,8 +11,7 @@ end function ! CHECK-LABEL: @_QPtest_real4 ! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath : (f32) -> f32 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32 -! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64 -! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 +! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath : f32 function test_real8(x) @@ -25,23 +25,42 @@ end function ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath : f64 -function test_real4_yx(y,x) +function test_real16(x) + real(16) :: x, test_real16 + test_real16 = atand(x) +end function + +! CHECK-LABEL: @_QPtest_real16 +! CHECK: %[[atan:.*]] = fir.call @_FortranAAtanF128({{.*}}) fastmath : (f128) -> f128 +! CHECK: %[[factor:.*]] = arith.constant 57.295779513082320876798154814105{{.*}} : f128 +! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath : f128 + +function test_real4_yx(y, x) real(4) :: x, y, test_real4 - test_real4 = atand(y,x) + test_real4 = atand(y, x) end function ! CHECK-LABEL: @_QPtest_real4_yx ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32 -! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64 -! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 +! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath : f32 -function test_real8_yx(y,x) +function test_real8_yx(y, x) real(8) :: x, y, test_real8 - test_real8 = atand(y,x) + test_real8 = atand(y, x) end function ! CHECK-LABEL: @_QPtest_real8_yx ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath : f64 + +function test_real16_yx(y, x) + real(16) :: x, y, test_real16 + test_real16 = atand(y, x) +end function + +! CHECK-LABEL: @_QPtest_real16_yx +! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f128 +! CHECK: %[[factor:.*]] = arith.constant 57.295779513082320876798154814105{{.*}} : f128 +! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath : f128