See the attached patch.
This is a first time use of Claude Code to assist with creating a patch.
It was interesting to observe the prompts and feedback where it identified the
files needing to be changed and suggested the changes. Showing me each diff per
file before doing anything.
It asked if I wanted to edit the files? Yes, it proceeded.
I open a separate terminal window to look with git status and git diff.
I have not added to the commit log the "Assisted by: part yet." I am going to
see if I can find a preferred wording there.
The patch does not change any of the existing invocations for -fcoarray, simply
a shorter version of it.
Regression tested on x86-64 and manually with some example coarray codes I have
here.
The added test case is based on one of our existing tests.
OK for trunk? We could go to 16 as well this is fairly simple patch.
Regards,
Jerry
From a1c4a92fb4dd65226bb3a37c658dbd74dbc4d5c5 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 9 May 2026 11:49:21 -0700
Subject: [PATCH] fortran: Add -fcoarray=shared option to auto-link -lcaf_shmem
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.
gcc/ChangeLog:
* flag-types.h (gfc_fcoarray): Add GFC_FCOARRAY_SHARED.
gcc/fortran/ChangeLog:
* 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/flag-types.h | 3 ++-
gcc/fortran/gfortranspec.cc | 25 ++++++++++++++++++++++++
gcc/fortran/lang.opt | 5 ++++-
gcc/testsuite/gfortran.dg/coarray_51.f90 | 15 ++++++++++++++
4 files changed, 46 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/coarray_51.f90
diff --git a/gcc/flag-types.h b/gcc/flag-types.h
index 77dc1f658fb..3d182785c82 100644
--- a/gcc/flag-types.h
+++ b/gcc/flag-types.h
@@ -456,7 +456,8 @@ enum gfc_fcoarray
{
GFC_FCOARRAY_NONE = 0,
GFC_FCOARRAY_SINGLE,
- GFC_FCOARRAY_LIB
+ GFC_FCOARRAY_LIB,
+ GFC_FCOARRAY_SHARED
};
diff --git a/gcc/fortran/gfortranspec.cc b/gcc/fortran/gfortranspec.cc
index d0df0ee6171..13b240013e8 100644
--- a/gcc/fortran/gfortranspec.cc
+++ b/gcc/fortran/gfortranspec.cc
@@ -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
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
index 754e5c9115b..cbd13aa2019 100644
--- a/gcc/fortran/lang.opt
+++ b/gcc/fortran/lang.opt
@@ -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
index 00000000000..d07351c7654
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray_51.f90
@@ -0,0 +1,15 @@
+! { dg-do compile }
+! { 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" } }
--
2.54.0