]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fortran: Add -fcoarray=shared option to auto-link -lcaf_shmem
authorJerry DeLisle <jvdelisle@gcc.gnu.org>
Sat, 9 May 2026 18:49:21 +0000 (11:49 -0700)
committerJerry DeLisle <jvdelisle@gcc.gnu.org>
Mon, 11 May 2026 16:23:46 +0000 (09:23 -0700)
The new -fcoarray=shared option provides a convenient shorthand for
the common invocation -fcoarray=lib -lcaf_shmem.  The driver transforms
-fcoarray=shared into -fcoarray=lib for the frontend and automatically
appends -lcaf_shmem to the link command.  Existing uses of -fcoarray=lib
are unaffected.

Assisted by: Claude Sonnet 4.6

gcc/:

* flag-types.h (gfc_fcoarray): Add GFC_FCOARRAY_SHARED.

gcc/fortran/:

* lang.opt (fcoarray=): Add shared enum value; update help text.
* gfortranspec.cc (CAF_SHMEM_LIBRARY): New macro.
(lang_specific_driver): Detect -fcoarray=shared in first pass and
set need_caf_shmem flag.  In second pass, transform -fcoarray=shared
to -fcoarray=lib for cc1.  Append -lcaf_shmem when need_caf_shmem
is set and linking is active.

gcc/testsuite/:

* gfortran.dg/coarray_51.f90: New test.

gcc/flag-types.h
gcc/fortran/gfortranspec.cc
gcc/fortran/lang.opt
gcc/testsuite/gfortran.dg/coarray_51.f90 [new file with mode: 0644]

index 77dc1f658fbd141db2f217f2c454d3b2408b57b1..3d182785c82449de9eab7019bebc22946023545e 100644 (file)
@@ -456,7 +456,8 @@ enum gfc_fcoarray
 {
   GFC_FCOARRAY_NONE = 0,
   GFC_FCOARRAY_SINGLE,
-  GFC_FCOARRAY_LIB
+  GFC_FCOARRAY_LIB,
+  GFC_FCOARRAY_SHARED
 };
 
 
index d0df0ee61717f63088635c098729cc614d1140bd..13b240013e8a828c92603cd963944a67124696a6 100644 (file)
@@ -62,6 +62,10 @@ along with GCC; see the file COPYING3.  If not see
 #define FORTRAN_LIBRARY "gfortran"
 #endif
 
+#ifndef CAF_SHMEM_LIBRARY
+#define CAF_SHMEM_LIBRARY "caf_shmem"
+#endif
+
 /* Name of the spec file.  */
 #define SPEC_FILE "libgfortran.spec"
 
@@ -205,6 +209,9 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
   /* Whether we need to link statically.  */
   int static_linking = 0;
 
+  /* Whether -fcoarray=shared was given; triggers auto-link of -lcaf_shmem.  */
+  int need_caf_shmem = 0;
+
   /* The number of input and output files in the incoming arg list.  */
   int n_infiles = 0;
   int n_outfiles = 0;
@@ -272,6 +279,11 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
          ++n_outfiles;
          break;
 
+       case OPT_fcoarray_:
+         if (decoded_options[i].value == GFC_FCOARRAY_SHARED)
+           need_caf_shmem = 1;
+         break;
+
        case OPT_v:
          verbose = 1;
          break;
@@ -349,6 +361,16 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
              saw_speclang = (strcmp (lang, "none") != 0);
            }
 
+         /* -fcoarray=shared is a driver-level alias for -fcoarray=lib that
+            also arranges for -lcaf_shmem to be linked automatically.  Pass
+            -fcoarray=lib to cc1 so the frontend uses the library code path.  */
+         if (decoded_options[i].opt_index == OPT_fcoarray_
+             && decoded_options[i].value == GFC_FCOARRAY_SHARED)
+           {
+             append_option (OPT_fcoarray_, "lib", GFC_FCOARRAY_LIB);
+             continue;
+           }
+
          append_arg (&decoded_options[i]);
 
          continue;
@@ -404,6 +426,9 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
        default:
          break;
        }
+
+      if (need_caf_shmem)
+       append_option (OPT_l, CAF_SHMEM_LIBRARY, 1);
     }
 
 #ifdef ENABLE_SHARED_LIBGCC
index 754e5c9115b3bd8d7ba630784e027f411b7d1fde..cbd13aa20190ef1e343a9a4a2956d537c72bc6eb 100644 (file)
@@ -833,7 +833,7 @@ Experimental unsigned numbers.
 
 fcoarray=
 Fortran RejectNegative Joined Enum(gfc_fcoarray) Var(flag_coarray) Init(GFC_FCOARRAY_NONE)
--fcoarray=<none|single|lib>    Specify which coarray parallelization should be used.
+-fcoarray=<none|single|lib|shared>     Specify which coarray parallelization should be used.
 
 Enum
 Name(gfc_fcoarray) Type(enum gfc_fcoarray) UnknownError(Unrecognized option: %qs)
@@ -847,6 +847,9 @@ Enum(gfc_fcoarray) String(single) Value(GFC_FCOARRAY_SINGLE)
 EnumValue
 Enum(gfc_fcoarray) String(lib) Value(GFC_FCOARRAY_LIB)
 
+EnumValue
+Enum(gfc_fcoarray) String(shared) Value(GFC_FCOARRAY_SHARED)
+
 fcheck=
 Fortran RejectNegative JoinedOrMissing
 -fcheck=[...]  Specify which runtime checks are to be performed.
diff --git a/gcc/testsuite/gfortran.dg/coarray_51.f90 b/gcc/testsuite/gfortran.dg/coarray_51.f90
new file mode 100644 (file)
index 0000000..da8a270
--- /dev/null
@@ -0,0 +1,15 @@
+! { dg-do link }
+! { dg-options "-fcoarray=shared -fdump-tree-original" }
+!
+! Test that -fcoarray=shared is accepted and generates the same
+! library-based CAF calls as -fcoarray=lib.
+
+program test
+  implicit none
+  integer :: x[*]
+  x = this_image ()
+  sync all
+end program test
+
+! { dg-final { scan-tree-dump-times "_gfortran_caf_init \\(&argc, &argv\\);" 1 "original" } }
+! { dg-final { scan-tree-dump-times "_gfortran_caf_sync_all \\(" 1 "original" } }