Skip to content

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Aug 27, 2025

Closes #150452.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Aug 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 27, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Connector Switch (c8ef)

Changes

Closes #150452.


Full diff: https://github.com/llvm/llvm-project/pull/155629.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+5-4)
  • (modified) flang/test/Lower/Intrinsics/tand.f90 (+12-2)
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index f6d71a9d1d933..70a3734798379 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -8290,10 +8290,11 @@ mlir::Value IntrinsicLibrary::genTand(mlir::Type resultType,
   mlir::MLIRContext *context = builder.getContext();
   mlir::FunctionType ftype =
       mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
-  llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
-  mlir::Value dfactor = builder.createRealConstant(
-      loc, mlir::Float64Type::get(context), pi / llvm::APFloat(180.0));
-  mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
+  const llvm::fltSemantics &fltSem =
+      llvm::cast<mlir::FloatType>(resultType).getFloatSemantics();
+  llvm::APFloat pi = llvm::APFloat(fltSem, llvm::numbers::pis);
+  mlir::Value factor = builder.createRealConstant(
+      loc, resultType, pi / llvm::APFloat(fltSem, "180.0"));
   mlir::Value arg = mlir::arith::MulFOp::create(builder, loc, args[0], factor);
   return getRuntimeCallGenerator("tan", ftype)(builder, loc, {arg});
 }
diff --git a/flang/test/Lower/Intrinsics/tand.f90 b/flang/test/Lower/Intrinsics/tand.f90
index b0f0c52400f78..8c8927e6f5a99 100644
--- a/flang/test/Lower/Intrinsics/tand.f90
+++ b/flang/test/Lower/Intrinsics/tand.f90
@@ -1,3 +1,4 @@
+! REQUIRES: flang-supports-f128-math
 ! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
 ! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
 ! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
@@ -8,8 +9,7 @@ function test_real4(x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4
-! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
-! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %[[factor:.*]] = arith.constant 0.0174532924 : f32
 ! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f32
 ! CHECK-PRECISE: %{{.*}} = fir.call @tanf(%[[arg]]) fastmath<contract> : (f32) -> f32
 ! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath<contract> : f32
@@ -24,3 +24,13 @@ function test_real8(x)
 ! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f64
 ! CHECK-PRECISE: %{{.*}} = fir.call @tan(%[[arg]]) fastmath<contract> : (f64) -> f64
 ! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath<contract> : f64
+
+function test_real16(x)
+  real(16) :: x, test_real16
+  test_real16 = tand(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real16
+! CHECK: %[[factor:.*]] = arith.constant 0.0174532925199432957692369076848861{{.*}} : f128
+! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f128
+! CHECK: %[[result:.*]] = fir.call @_FortranATanF128({{.*}}) fastmath<contract> : (f128) -> f128

@c8ef
Copy link
Contributor Author

c8ef commented Aug 27, 2025

BTW, I have a question about the f128 provider in Flang.

In the current main branch, FLANG_RUNTIME_F128_MATH_LIB can only be set to libquadmath, which is shipped with gcc for glibc versions below 2.26. With newer glibc versions, we can use the f128 variant of math functions directly. However, I came across some code that indicates it is not supported. In issue #83874, it is mentioned that FLANG_RUNTIME_F128_MATH_LIB can be set to libm. I am unsure why it is currently not supported.

#error "Float128Math build with glibc>=2.26 is unsupported yet"

@c8ef c8ef requested review from tblah and vzakhari August 27, 2025 14:36
@tblah
Copy link
Contributor

tblah commented Aug 27, 2025

We support f128 if either libquadmath is available or if libm defines macros indicating that it supports f128: LDBL_MANT_DIG == 113. See #106957

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@c8ef
Copy link
Contributor Author

c8ef commented Aug 27, 2025

We support f128 if either libquadmath is available or if libm defines macros indicating that it supports f128: LDBL_MANT_DIG == 113. See #106957

Thanks for the pointer, I'll look into it later. On my Ubuntu 22.04 development box, the glibc version is 2.35. My thought is to have f128 usable by default on both x86_64 and aarch64, but on x86, it's currently unsupported by default.

@c8ef c8ef merged commit 626e4ba into llvm:main Aug 27, 2025
12 checks passed
@c8ef c8ef deleted the tand branch August 27, 2025 14:49
@vzakhari
Copy link
Contributor

@c8ef, the support for newer versions of glibc needs to be implemented and tested. There is no fundamental reasons not to support it, its just has not been done yet. If you would like to work on that, I will be glad to review it.

@c8ef
Copy link
Contributor Author

c8ef commented Aug 27, 2025

@c8ef, the support for newer versions of glibc needs to be implemented and tested. There is no fundamental reasons not to support it, its just has not been done yet. If you would like to work on that, I will be glad to review it.

Will do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] Create the real constant directly with the argument type.
4 participants