Re: [PATCH] fortran: Allow vector math functions only with fast-math [PR 118955]

Wilco Dijkstra <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <PAWPR08MB89824E6BA86338DC701B683D838EA@PAWPR08MB8982.eurprd08.prod.outlook.com>
Adding Fortran list too

ping

________________________________________
From: Wilco Dijkstra
Sent: 17 September 2025 18:19
To: GCC Patches <[email protected]>
Subject: [PATCH] fortran: Allow vector math functions only with fast-math [PR 118955]
 

Vector math functions are currently always enabled in Fortran.  This is
incorrect since vector math functions are designed to be Ofast only.
Add a new 'fastmath' option which only accepts vector functions if fast-math
is enabled:

!GCC$ builtin (sin) attributes simd (notinbranch) if('fastmath')

Passes regress.

gcc:
        PR fortran/118955
        * fortran/decl.cc (gfc_match_gcc_builtin): Add 'fastmath' option which
        checks for fast-math before accepting a vector function.
        * fortran/gfortran.texi (!GCC$ builtin): Update documentation.

gcc/testsuite:
        PR fortran/118955
        * gfortran.dg/simd-builtins-9.f90: Add new test.       
        * gfortran.dg/simd-builtins-9.h: Likewise.

---

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 9fe697cd5498467df2600fe878956e8050a28adb..04ed9075491d3ec2c9d7fb75bced34f9ffb9f577 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -29,6 +29,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "parse.h"
 #include "constructor.h"
 #include "target.h"
+#include "flags.h"
 
 /* Macros to access allocate memory for gfc_data_variable,
    gfc_data_value and gfc_data.  */
@@ -12562,9 +12563,17 @@ gfc_match_gcc_builtin (void)
 
   if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES)
     {
-      const char *abi = targetm.get_multilib_abi_name ();
-      if (abi == NULL || strcmp (abi, target) != 0)
-       return MATCH_YES;
+      if (strcmp (target, "fastmath") == 0)
+       {
+         if (!fast_math_flags_set_p (&global_options))
+           return MATCH_YES;
+       }
+      else
+       {
+         const char *abi = targetm.get_multilib_abi_name ();
+         if (abi == NULL || strcmp (abi, target) != 0)
+           return MATCH_YES;
+       }
     }
 
   if (gfc_vectorized_builtins == NULL)
diff --git a/gcc/fortran/gfortran.texi b/gcc/fortran/gfortran.texi
index 841f6135066043935ca31496e910c69908c0c53e..e76c37f8123d0b5885730f01928d821606d42752 100644
--- a/gcc/fortran/gfortran.texi
+++ b/gcc/fortran/gfortran.texi
@@ -3470,6 +3470,13 @@ for the built-in that should be vectorized.  Example usage:
 !GCC$ builtin (sinf) attributes simd (notinbranch) if('x86_64')
 @end smallexample
 
+The special target @code{'fastmath'} is used to specify the vector
+implementation is only valid if fast-math is enabled:
+
+@smallexample
+!GCC$ builtin (exp) attributes simd (notinbranch) if('fastmath')
+@end smallexample
+
 The purpose of the directive is to provide an API among the GCC compiler and
 the GNU C Library which would define vector implementations of math routines.
 
diff --git a/gcc/testsuite/gfortran.dg/simd-builtins-9.f90 b/gcc/testsuite/gfortran.dg/simd-builtins-9.f90
new file mode 100644
index 0000000000000000000000000000000000000000..02944e578533afccc90768516cafb473f098f19d
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/simd-builtins-9.f90
@@ -0,0 +1,16 @@
+! { dg-do compile { target { aarch64*-*-linux* } } }
+! { dg-additional-options "-nostdinc -O3 -fpre-include=simd-builtins-9.h -fdump-tree-optimized" }
+
+program test_overloaded_intrinsic
+  real(8) :: x8(3200), y8(3200)
+
+  y8 = sin(x8)
+  print *, y8
+
+  x8 = cos(y8)
+  print *, x8
+end
+
+! { dg-final { scan-tree-dump-not "sin.simdclone" "optimized" } } */
+
+! { dg-final { scan-tree-dump "cos.simdclone" "optimized" } } */
diff --git a/gcc/testsuite/gfortran.dg/simd-builtins-9.h b/gcc/testsuite/gfortran.dg/simd-builtins-9.h
new file mode 100644
index 0000000000000000000000000000000000000000..3ca3ea8062c9d1c99ccabc9dd5466befd6e6d4f6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/simd-builtins-9.h
@@ -0,0 +1,2 @@
+!GCC$ builtin (sin) attributes simd (notinbranch) if('fastmath')
+!GCC$ builtin (cos) attributes simd (notinbranch)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.