]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fortran: Allow vector math functions only with fast-math [PR 118955]
authorWilco Dijkstra <wilco.dijkstra@arm.com>
Wed, 17 Sep 2025 15:50:04 +0000 (15:50 +0000)
committerWilco Dijkstra <wilco.dijkstra@arm.com>
Tue, 27 Jan 2026 11:45:16 +0000 (11:45 +0000)
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.

gcc/fortran/decl.cc
gcc/fortran/gfortran.texi
gcc/testsuite/gfortran.dg/simd-builtins-9.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/simd-builtins-9.h [new file with mode: 0644]

index 903d4d2b617821fc0012af98e4c31d20b5ba5e89..844e27f007ffdea2d95d9c503f2fc1b947b57bf7 100644 (file)
@@ -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)
index c4f39acf6bc31398d010a4165e31b871b39aab20..a930cc1dc9c04348d9ef284789720a79055ca702 100644 (file)
@@ -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 (file)
index 0000000..02944e5
--- /dev/null
@@ -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 (file)
index 0000000..3ca3ea8
--- /dev/null
@@ -0,0 +1,2 @@
+!GCC$ builtin (sin) attributes simd (notinbranch) if('fastmath')
+!GCC$ builtin (cos) attributes simd (notinbranch)