]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
driver: Forward '-lgfortran', '-lm' to offloading compilation
authorThomas Schwinge <thomas@codesourcery.com>
Mon, 5 Jun 2023 09:26:37 +0000 (11:26 +0200)
committerThomas Schwinge <thomas@codesourcery.com>
Wed, 14 Jun 2023 09:41:45 +0000 (11:41 +0200)
..., so that users don't manually need to specify
'-foffload-options=-lgfortran', '-foffload-options=-lm' in addition to
'-lgfortran', '-lm' (specified manually, or implicitly by the driver).

gcc/
* gcc.cc (driver_handle_option): Forward host '-lgfortran', '-lm'
to offloading compilation.
* config/gcn/mkoffload.cc (main): Adjust.
* config/nvptx/mkoffload.cc (main): Likewise.
* doc/invoke.texi (foffload-options): Update example.
libgomp/
* testsuite/libgomp.fortran/fortran.exp (lang_link_flags): Don't
set.
* testsuite/libgomp.oacc-fortran/fortran.exp (lang_link_flags):
Likewise.
* testsuite/libgomp.c/simd-math-1.c: Remove
'-foffload-options=-lm'.
* testsuite/libgomp.fortran/fortran-torture_execute_math.f90:
Likewise.
* testsuite/libgomp.oacc-fortran/fortran-torture_execute_math.f90:
Likewise.

gcc/config/gcn/mkoffload.cc
gcc/config/nvptx/mkoffload.cc
gcc/doc/invoke.texi
gcc/gcc.cc
libgomp/testsuite/libgomp.c/simd-math-1.c
libgomp/testsuite/libgomp.fortran/fortran-torture_execute_math.f90
libgomp/testsuite/libgomp.fortran/fortran.exp
libgomp/testsuite/libgomp.oacc-fortran/fortran-torture_execute_math.f90
libgomp/testsuite/libgomp.oacc-fortran/fortran.exp

index 988c12318fd915c8710421ea158ddadc7f901fa2..8b608bf024efd07e39b8bd4dc1e3b90895be45af 100644 (file)
@@ -946,6 +946,18 @@ main (int argc, char **argv)
       else if (startswith (argv[i], STR))
        gcn_stack_size = atoi (argv[i] + strlen (STR));
 #undef STR
+      /* Translate host into offloading libraries.  */
+      else if (strcmp (argv[i], "-l_GCC_gfortran") == 0
+              || strcmp (argv[i], "-l_GCC_m") == 0)
+       {
+         /* Elide '_GCC_'.  */
+         size_t i_dst = strlen ("-l");
+         size_t i_src = strlen ("-l_GCC_");
+         char c;
+         do
+           c = argv[i][i_dst++] = argv[i][i_src++];
+         while (c != '\0');
+       }
     }
 
   if (!(fopenacc ^ fopenmp))
index 6cdea45cffed3791dbe872ba9e805a70cb799fef..aaea9fb320df2878bb9fd515e44b3f1947d32301 100644 (file)
@@ -649,6 +649,18 @@ main (int argc, char **argv)
       else if (strcmp (argv[i], "-dumpbase") == 0
               && i + 1 < argc)
        dumppfx = argv[++i];
+      /* Translate host into offloading libraries.  */
+      else if (strcmp (argv[i], "-l_GCC_gfortran") == 0
+              || strcmp (argv[i], "-l_GCC_m") == 0)
+       {
+         /* Elide '_GCC_'.  */
+         size_t i_dst = strlen ("-l");
+         size_t i_src = strlen ("-l_GCC_");
+         char c;
+         do
+           c = argv[i][i_dst++] = argv[i][i_src++];
+         while (c != '\0');
+       }
     }
   if (!(fopenacc ^ fopenmp))
     fatal_error (input_location, "either %<-fopenacc%> or %<-fopenmp%> "
index 8fa3f9fae01607eb6cff26e886a6756165aa2d85..9ecbd32a228c1051df5a9bce4d4d7e6019762508 100644 (file)
@@ -2716,9 +2716,8 @@ the @option{-foffload-options=@var{target-list}=@var{options}} form.  The
 Typical command lines are
 
 @smallexample
--foffload-options=-lgfortran -foffload-options=-lm
--foffload-options="-lgfortran -lm" -foffload-options=nvptx-none=-latomic
--foffload-options=amdgcn-amdhsa=-march=gfx906 -foffload-options=-lm
+-foffload-options='-fno-math-errno -ffinite-math-only' -foffload-options=nvptx-none=-latomic
+-foffload-options=amdgcn-amdhsa=-march=gfx906 -foffload-options=-O3
 @end smallexample
 
 @opindex fopenacc
index 38155f8fa4bd1691bc34a7384bf836b70548a02a..08bdf28b0adafb346923482f571ed4d668f19b8f 100644 (file)
@@ -47,6 +47,9 @@ compilation is specified by a string called a "spec".  */
 #include "opts-jobserver.h"
 #include "common/common-target.h"
 
+#ifndef MATH_LIBRARY
+#define MATH_LIBRARY "m"
+#endif
 \f
 
 /* Manage the manipulation of env vars.
@@ -4117,6 +4120,48 @@ next_item:
     }
 }
 
+/* Forward certain options to offloading compilation.  */
+
+static void
+forward_offload_option (size_t opt_index, const char *arg, bool validated)
+{
+  switch (opt_index)
+    {
+    case OPT_l:
+      /* Use a '_GCC_' prefix and standard name ('-l_GCC_m' irrespective of the
+        host's 'MATH_LIBRARY', for example), so that the 'mkoffload's can tell
+        this has been synthesized here, and translate/drop as necessary.  */
+      /* Note that certain libraries ('-lc', '-lgcc', '-lgomp', for example)
+        are injected by default in offloading compilation, and therefore not
+        forwarded here.  */
+      /* GCC libraries.  */
+      if (/* '-lgfortran' */ strcmp (arg, "gfortran") == 0 )
+       save_switch (concat ("-foffload-options=-l_GCC_", arg, NULL),
+                    0, NULL, validated, true);
+      /* Other libraries.  */
+      else
+       {
+         /* The case will need special consideration where on the host
+            '!need_math', but for offloading compilation still need
+            '-foffload-options=-l_GCC_m'.  The problem is that we don't get
+            here anything like '-lm', because it's not synthesized in
+            'gcc/fortran/gfortranspec.cc:lang_specific_driver', for example.
+            Generally synthesizing '-foffload-options=-l_GCC_m' etc. in the
+            language specific drivers is non-trivial, needs very careful
+            review of their options handling.  However, this issue is not
+            actually relevant for the current set of supported host/offloading
+            configurations.  */
+         int need_math = (MATH_LIBRARY[0] != '\0');
+         if (/* '-lm' */ (need_math && strcmp (arg, MATH_LIBRARY) == 0))
+           save_switch ("-foffload-options=-l_GCC_m",
+                        0, NULL, validated, true);
+       }
+      break;
+    default:
+      gcc_unreachable ();
+    }
+}
+
 /* Handle a driver option; arguments and return value as for
    handle_option.  */
 
@@ -4375,6 +4420,17 @@ driver_handle_option (struct gcc_options *opts,
       /* POSIX allows separation of -l and the lib arg; canonicalize
         by concatenating -l with its arg */
       add_infile (concat ("-l", arg, NULL), "*");
+
+      /* Forward to offloading compilation '-l[...]' flags for standard,
+        well-known libraries.  */
+      /* Doing this processing here means that we don't get to see libraries
+        injected via specs, such as '-lquadmath' injected via
+        '[build]/[target]/libgfortran/libgfortran.spec'.  However, this issue
+        is not actually relevant for the current set of host/offloading
+        configurations.  */
+      if (ENABLE_OFFLOADING)
+       forward_offload_option (opt_index, arg, validated);
+
       do_save = false;
       break;
 
index 947bf606e3602cbcd5153b01e30defe33897b5f2..dd2077cc5973b307787c366a3958ef293a9c56c9 100644 (file)
@@ -4,7 +4,6 @@
 /* { dg-do run } */
 /* { dg-options "-O2 -ftree-vectorize -fno-math-errno" } */
 /* { dg-additional-options -foffload-options=amdgcn-amdhsa=-mstack-size=3000000 { target offload_target_amdgcn } } */
-/* { dg-additional-options -foffload-options=-lm } */
 
 #undef PRINT_RESULT
 #define VERBOSE 0
index 3348a0bb3adc64af0ce27f462037e7b02c62b839..2d0caa6b84ad11e06b16e90e2233de915b0ea466 100644 (file)
@@ -1,4 +1,3 @@
 ! { dg-do run }
-! { dg-additional-options -foffload-options=-lm }
 
 include '../../../gcc/testsuite/gfortran.fortran-torture/execute/math.f90'
index e69656bce23ba99e71d7615fb6bb1bbae61b6bc4..32e4bb2af4e0f1028171d54e661b682979c97848 100644 (file)
@@ -28,7 +28,6 @@ if { $blddir != "" } {
     set lang_source_re {^.*\.[fF](|90|95|03|08)$}
     set lang_include_flags "-fintrinsic-modules-path=${blddir}"
 }
-set lang_link_flags "-foffload=-lgfortran"
 lappend ALWAYS_CFLAGS "compiler=$GFORTRAN_UNDER_TEST"
 
 # Initialize dg.
@@ -63,7 +62,6 @@ if { $blddir != "" } {
     unset libquadmath_library_path
     unset lang_library_paths
 }
-unset lang_link_flags
 
 # All done.
 dg-finish
index 1b2ac4407623986cd6c0439ff6ecc83064621ce2..edc940c4706a9f0108e847973fd67bda46b175c2 100644 (file)
@@ -1,5 +1,4 @@
 ! { dg-do run }
 !TODO { dg-prune-output {using 'vector_length \(32\)', ignoring 1} }
-! { dg-additional-options -foffload-options=-lm }
 
 include '../../../gcc/testsuite/gfortran.fortran-torture/execute/math.f90'
index 2e8aa2285fbaa07d17b6deb77f04406c6af68343..e5844ad00fa2f24d668d95f949f6e47422f194d2 100644 (file)
@@ -30,7 +30,6 @@ if { $blddir != "" } {
     set lang_source_re {^.*\.[fF](|90|95|03|08)$}
     set lang_include_flags "-fintrinsic-modules-path=${blddir}"
 }
-set lang_link_flags "-foffload=-lgfortran"
 lappend ALWAYS_CFLAGS "compiler=$GFORTRAN_UNDER_TEST"
 
 # Initialize dg.
@@ -113,7 +112,6 @@ if { $blddir != "" } {
     unset libquadmath_library_path
     unset lang_library_paths
 }
-unset lang_link_flags
 
 # All done.
 dg-finish