Hello All,

I have been concerned that the mainline patch below might bite us on
the hand one day because of the side effects for PDTs and OMP.
Therefore, I have attached a version for 16-branch, which eliminates
the side effects and only deals with the shared memory coarray
problem. That said, thus far there have been no reports of problems
caused by the commit to mainline and, as Andre remarked in the PR, it
does make more sense for gfc_current_ns point to the namespace being
translated.

Regtested on FC43/x86_64. OK for 16-branch?

Paul


---------- Forwarded message ---------
From: cvs-commit at gcc dot gnu.org <[email protected]>
Date: Sun, 10 May 2026 at 07:22
Subject: [Bug fortran/125051] [coarray] undefined reference to
`__caf_get_from_remote_fn_index_0.4' (shmem caf)
To: <[email protected]>


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125051

--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Paul Thomas <[email protected]>:

https://gcc.gnu.org/g:3e281ae5d6a814585b884d693ef354e75bf12be7

commit r17-427-g3e281ae5d6a814585b884d693ef354e75bf12be7
Author: Paul Thomas <[email protected]>
Date:   Fri May 8 06:34:21 2026 +0100

    Fortran: Allow access to coarray elements within modules. [PR125051]

The parts of this patch is fix the problem are chunks 2 and 3. Chunk3
prevents gfc_conv_intrinsic_caf_get from working in the module
namespace, when the array symbol is in a module. Equally, though,
gfc_current_ns is not necessarily in the referencing procedure
namespace. The second chunk makes sure that this is the case. As an
aside, it seems to us that it makes considerably more sense that
gfc_current_ns be that of the current procedure. The first chunk makes
sure that result symbol initialization does not occur outside the
function.

    Passes regtesting with FC44/x86_64.

    2026-05-10  Andre Vehreschild  <[email protected]>
                Paul Thomas  <[email protected]>

    gcc/fortran
            PR fortran/125051
            * trans-decl.cc (gfc_get_symbol_decl): gfc_defer_symbol_init
            must not be called for PDT types, classes or types with PDT
            (gfc_generate_function_code): If gfc_current_ns is not the same
            as the function namespace, stash it,change it to the function
            namespace and restore after translation of the code.
            * trans-intrinsic.cc (gfc_conv_intrinsic_caf_get): If the array
            is in a module, use the symbol namespace.
            * trans-openmp.cc (gfc_trans_omp_array_reduction_or_udr): If the
            current namespace is not that of the procedure, change to the
            procedure namspace and revert on leaving this function.

    gcc/testsuite/
            PR fortran/125051
            * gfortran.dg/coarray/pr125051.f90: New test.
From 99427b13093a97118a27c6c91ec9da0f5f07a777 Mon Sep 17 00:00:00 2001
From: Paul Thomas <[email protected]>
Date: Mon, 11 May 2026 17:33:20 +0100
Subject: [PATCH] Fortran: Allow access to coarray elements within modules.
 [PR125051]

2026-05-11  Andre Vehreschild  <[email protected]>
	    Paul Thomas  <[email protected]>

gcc/fortran
	PR fortran/125051
	* gfortran.h : Add declaration for gfc_current_function_ns.
	* symbol.cc : Ditto and initialize to NULL.
	* trans-decl.cc (gfc_generate_function_code): Set the above to
	the function namespace and reset it after translation of the
	code.
	* trans-intrinsic.cc (gfc_conv_intrinsic_caf_get): Initialize
	'ns' to null. If it has been set by the existing logic and the
	coarray is declared in a module, use gfc_current_function_ns.

gcc/testsuite/
	PR fortran/125051
	* gfortran.dg/pr125051.f90: New test.
---
 gcc/fortran/gfortran.h                        |  1 +
 gcc/fortran/symbol.cc                         |  1 +
 gcc/fortran/trans-decl.cc                     |  2 ++
 gcc/fortran/trans-intrinsic.cc                |  6 +++-
 .../gfortran.dg/coarray/pr125051.f90          | 34 +++++++++++++++++++
 5 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/pr125051.f90

diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index b0ce54e1c21..3f4c55e267f 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -2413,6 +2413,7 @@ typedef struct gfc_namespace
 gfc_namespace;
 
 extern gfc_namespace *gfc_current_ns;
+extern gfc_namespace *gfc_current_function_ns;
 extern gfc_namespace *gfc_global_ns_list;
 
 /* Global symbols are symbols of global scope. Currently we only use
diff --git a/gcc/fortran/symbol.cc b/gcc/fortran/symbol.cc
index e1b49b0ba0d..49f2af2ea81 100644
--- a/gcc/fortran/symbol.cc
+++ b/gcc/fortran/symbol.cc
@@ -102,6 +102,7 @@ const mstring dtio_procs[] =
 static int next_decl_order = 1;
 
 gfc_namespace *gfc_current_ns;
+gfc_namespace *gfc_current_function_ns = NULL;
 gfc_namespace *gfc_global_ns_list;
 
 gfc_gsymbol *gfc_gsym_root = NULL;
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 4b3f75ced71..0e287a9ceb3 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -8300,7 +8300,9 @@ gfc_generate_function_code (gfc_namespace * ns)
 
   finish_oacc_declare (ns, sym, false);
 
+  gfc_current_function_ns = ns;
   tmp = gfc_trans_code (ns->code);
+  gfc_current_function_ns = NULL;
   gfc_add_expr_to_block (&body, tmp);
 
   /* This permits the return value to be correctly initialized, even when the
diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc
index dbf645886f5..72f2885119f 100644
--- a/gcc/fortran/trans-intrinsic.cc
+++ b/gcc/fortran/trans-intrinsic.cc
@@ -1221,7 +1221,7 @@ gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
     dest_data, opt_dest_desc, get_fn_index_tree, add_data_tree, add_data_size,
     opt_src_desc, opt_src_charlen, opt_dest_charlen, team, team_no;
   symbol_attribute caf_attr_store;
-  gfc_namespace *ns;
+  gfc_namespace *ns = NULL;
   gfc_expr *get_fn_hash = expr->value.function.actual->next->expr,
 	   *get_fn_expr = expr->value.function.actual->next->next->expr;
   gfc_symbol *add_data_sym = get_fn_expr->symtree->n.sym->formal->sym;
@@ -1240,6 +1240,10 @@ gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs,
 	   && !array_expr->symtree->n.sym->attr.associate_var
 	 ? array_expr->symtree->n.sym->ns
 	 : gfc_current_ns;
+
+  if (gfc_current_function_ns && ns && array_expr->symtree->n.sym->module)
+    ns = gfc_current_function_ns;
+
   type = gfc_typenode_for_spec (&array_expr->ts);
 
   if (caf_attr == NULL)
diff --git a/gcc/testsuite/gfortran.dg/coarray/pr125051.f90 b/gcc/testsuite/gfortran.dg/coarray/pr125051.f90
new file mode 100644
index 00000000000..9097340b706
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/pr125051.f90
@@ -0,0 +1,34 @@
+!{ dg-do link }
+
+! Contributed by Paul Thomas  <[email protected]>
+! Check PR fortran/125051 is fixed.
+
+module m
+  integer, parameter :: ncells = 8, nsize = ncells+2
+  integer, parameter :: head =2, tail = ncells + 1
+  real :: second_derivative(ncells+2, ncells+2)
+  type :: density_t
+    real :: density(nsize)
+    real:: derivative(nsize)
+  end type
+  type (density_t) :: n[*]
+  real :: n_tail[*] = 0.0
+  real :: n_head[*] = 0.0
+contains
+  subroutine sub
+    integer :: image_no
+    image_no = this_image()
+    sync all
+    if (image_no > 1) n_head = n[image_no -1]%density(tail)
+    if (image_no < num_images()) n_tail = n[image_no +1]%density(head)
+    sync all
+  end
+end module
+
+program main
+  use m
+  implicit none
+  call sub
+contains
+end program
+
-- 
2.54.0

Reply via email to