[flang-commits] [flang] 12e0d52 - [flang] Optimize `sinpi` precision (#153211)

via flang-commits flang-commits at lists.llvm.org
Tue Aug 12 19:06:32 PDT 2025


Author: Connector Switch
Date: 2025-08-13T10:06:29+08:00
New Revision: 12e0d524bc0c75ce834127a2fe3d3bc0eae541c0

URL: https://github.com/llvm/llvm-project/commit/12e0d524bc0c75ce834127a2fe3d3bc0eae541c0
DIFF: https://github.com/llvm/llvm-project/commit/12e0d524bc0c75ce834127a2fe3d3bc0eae541c0.diff

LOG: [flang] Optimize `sinpi` precision (#153211)

Part of #150452.

Added: 
    

Modified: 
    flang/lib/Optimizer/Builder/IntrinsicCall.cpp
    flang/test/Lower/Intrinsics/sinpi.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 86ba02e6c57fa..82fccd3f36553 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -8136,10 +8136,10 @@ mlir::Value IntrinsicLibrary::genSinpi(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);
-  mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
+  llvm::APFloat pi =
+      llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
+                    llvm::numbers::pis);
+  mlir::Value factor = builder.createRealConstant(loc, resultType, pi);
   mlir::Value arg = mlir::arith::MulFOp::create(builder, loc, args[0], factor);
   return getRuntimeCallGenerator("sin", ftype)(builder, loc, {arg});
 }

diff  --git a/flang/test/Lower/Intrinsics/sinpi.f90 b/flang/test/Lower/Intrinsics/sinpi.f90
index 38c2277892ec7..06699b7f8ea71 100644
--- a/flang/test/Lower/Intrinsics/sinpi.f90
+++ b/flang/test/Lower/Intrinsics/sinpi.f90
@@ -1,3 +1,4 @@
+! REQUIRES: flang-supports-f128-math
 ! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
 
 function test_real4(x)
@@ -6,8 +7,7 @@ function test_real4(x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4
-! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
-! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %[[factor:.*]] = arith.constant 3.14159274 : f32
 ! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[factor]] fastmath<contract> : f32
 ! CHECK: %[[sin:.*]] = math.sin %[[mul]] fastmath<contract> : f32
 
@@ -20,3 +20,13 @@ function test_real8(x)
 ! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
 ! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[dfactor]] fastmath<contract> : f64
 ! CHECK: %[[sin:.*]] = math.sin %[[mul]] fastmath<contract> : f64
+
+function test_real16(x)
+  real(16) :: x, test_real16
+  test_real16 = sinpi(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real16
+! CHECK: %[[factor:.*]] = arith.constant 3.141592653589793238462643383279{{.*}} : f128
+! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[factor]] fastmath<contract> : f128
+! CHECK: %[[sin:.*]] = fir.call @_FortranASinF128(%[[mul]]) fastmath<contract> : (f128) -> f128


        


More information about the flang-commits mailing list