https://gcc.gnu.org/g:66a2e7137049482ac317a5af8d64f43c315f87a6

commit r16-7066-g66a2e7137049482ac317a5af8d64f43c315f87a6
Author: Wilco Dijkstra <[email protected]>
Date:   Wed Sep 17 15:50:04 2025 +0000

    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')
    
    gcc/fortran:
            PR fortran/118955
            * decl.cc (gfc_match_gcc_builtin): Add 'fastmath' option which
            checks for fast-math before accepting a vector function.
            * 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:
---
 gcc/fortran/decl.cc                           | 15 ++++++++++++---
 gcc/fortran/gfortran.texi                     |  7 +++++++
 gcc/testsuite/gfortran.dg/simd-builtins-9.f90 | 16 ++++++++++++++++
 gcc/testsuite/gfortran.dg/simd-builtins-9.h   |  2 ++
 4 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 903d4d2b6178..844e27f007ff 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.  */
@@ -12816,9 +12817,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 c4f39acf6bc3..a930cc1dc9c0 100644
--- a/gcc/fortran/gfortran.texi
+++ b/gcc/fortran/gfortran.texi
@@ -3454,6 +3454,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 000000000000..02944e578533
--- /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 000000000000..3ca3ea8062c9
--- /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)

Reply via email to