Hi Harald,

I totally understand your confusion. I also had a hard time figuring what is
needed there. I got to restructure the code fragment and now only allow pure
*and* elemental intrinsic function and pure *and* elemental user-defined
functions (hoping that's the opposite of intrinsics) in a caf accessor. For all
others a temporary is to be created in the helper structure. I also added a
comment to clarify the intention. I think this is better now. Opinions? 

Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainline?

Regards,
        Andre

On Fri, 4 Jul 2025 19:29:08 +0200
Harald Anlauf <anl...@gmx.de> wrote:

> Andre,
> 
> either your patch to coarray.cc is wrong, or the comment in the code
> is not concise, or I am too dense to understand the intent of the
> change:
> 
> diff --git a/gcc/fortran/coarray.cc b/gcc/fortran/coarray.cc
> index ef8fd4e42d0..01aac581a74 100644
> --- a/gcc/fortran/coarray.cc
> +++ b/gcc/fortran/coarray.cc
> @@ -700,7 +700,7 @@ check_add_new_component (gfc_symbol *type, gfc_expr 
> *e, gfc_symbol *add_data)
>             && !e->symtree->n.sym->attr.elemental
>             && !(e->value.function.isym
>                  && (e->value.function.isym->pure
> -                    || e->value.function.isym->elemental)))
> +                    && e->value.function.isym->elemental)))
>           /* Treat non-pure/non-elemental functions.  */
>           check_add_new_comp_handle_array (e, type, add_data);
>         else
> 
> Can you please elaborate?
> 
> I understood the code comment in the way that any pure or elemental
> intrinsic should be handled in the else branch.  Or do you have
> an example which is different?
> 
> The change to trans-decl.cc (fix of decl) looks fine for me.
> 
> Harald
> 
> 
> m 04.07.25 um 13:43 schrieb Andre Vehreschild:
> > Hi all,
> > 
> > attached patch narrows the use of intrinsic functions in the caf accessor
> > down to pure elemental functions. This is needed because functions that get
> > extracted into the caf accessor routine, have no access to the source
> > image's memory. E.g. team_number() is marked as pure, but takes a pointer
> > argument to an object in the source image's memory, which is not available
> > on the remote image where the accessor is executed. This patch fixes that
> > and also corrects the type in the decl of the ABI of team_number. It is of
> > the opaque type team_type aka void* now and not integer as formerly
> > declared.
> > 
> > Regtest ok on x86_64-pc-linux-gnu / F41. Ok for mainline?
> > 
> > Regards,
> >     Andre
> > 
> > On Tue, 1 Jul 2025 12:54:49 -0700
> > Jerry D <jvdelis...@gmail.com> wrote:
> >   
> >> On 7/1/25 12:51 PM, Harald Anlauf wrote:  
> >>> Dear all,
> >>>
> >>> the attached patch fixes the following minor issues found by running
> >>> f951 under valgrind for the just added new testcases coindexed_6.f90
> >>> and coindexed_7.f90:
> >>>
> >>> - minor front-end memleaks with non-freed strings and lost GMP variables
> >>>    (these are simple and obvious fixes)
> >>>
> >>> - an inconsistency between pure/elemental functions being either
> >>>    non-intrinsic or intrinsic.  Checking for the latter was likely missed
> >>>    from the beginning.
> >>>
> >>> No new testcase.
> >>>
> >>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> >>>
> >>> Thanks,
> >>> Harald
> >>>      
> >>
> >> Yes, OK, straight-forward.
> >>
> >> Thanks,
> >>
> >> Jerry  
> > 
> >   
> 


-- 
Andre Vehreschild * Email: vehre ad gmx dot de 
From 2ad3600f5756b4c50fd70efde6d965a0037eb833 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild <ve...@gcc.gnu.org>
Date: Fri, 4 Jul 2025 11:26:46 +0200
Subject: [PATCH] Fortran: Fix pure/elemental function treatment and
 team_number parameter attribution.

gcc/fortran/ChangeLog:

	* coarray.cc (check_add_new_component): Only allow pure
	elemental (intrinsic) functions in a coarray accessor.
	* trans-decl.cc (gfc_build_builtin_function_decls): The only
	argument of team_number() is of type void* in the library ABI.
---
 gcc/fortran/coarray.cc    | 26 ++++++++++++++++----------
 gcc/fortran/trans-decl.cc |  7 +++----
 2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/gcc/fortran/coarray.cc b/gcc/fortran/coarray.cc
index ef8fd4e42d0..c611b539968 100644
--- a/gcc/fortran/coarray.cc
+++ b/gcc/fortran/coarray.cc
@@ -696,17 +696,23 @@ check_add_new_component (gfc_symbol *type, gfc_expr *e, gfc_symbol *add_data)
 	    check_add_new_component (type, actual->expr, add_data);
 	  break;
 	case EXPR_FUNCTION:
-	  if (!e->symtree->n.sym->attr.pure
-	      && !e->symtree->n.sym->attr.elemental
-	      && !(e->value.function.isym
-		   && (e->value.function.isym->pure
-		       || e->value.function.isym->elemental)))
-	    /* Treat non-pure/non-elemental functions.  */
-	    check_add_new_comp_handle_array (e, type, add_data);
+	  if ((e->symtree->n.sym->attr.pure
+	       && e->symtree->n.sym->attr.elemental)
+	      || (e->value.function.isym && e->value.function.isym->pure
+		  && e->value.function.isym->elemental))
+	    {
+	      /* Only allow pure and elemental function calls in a coarray
+		 accessor, because all other may have side effects or access
+		 pointers, which may not be possible in the accessor running on
+		 another host.  */
+	      for (gfc_actual_arglist *actual = e->value.function.actual;
+		   actual; actual = actual->next)
+		check_add_new_component (type, actual->expr, add_data);
+	    }
 	  else
-	    for (gfc_actual_arglist *actual = e->value.function.actual; actual;
-		 actual = actual->next)
-	      check_add_new_component (type, actual->expr, add_data);
+	    /* Extract the expression, evaluate it and add a temporary with its
+	       value to the helper structure.  */
+	    check_add_new_comp_handle_array (e, type, add_data);
 	  break;
 	case EXPR_VARIABLE:
 	    check_add_new_comp_handle_array (e, type, add_data);
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 43bd7be54cb..ba4a842a025 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -4223,10 +4223,9 @@ gfc_build_builtin_function_decls (void)
 	get_identifier (PREFIX ("caf_sync_team")), ". r w w w ", void_type_node,
 	4, pvoid_type_node, pint_type, pchar_type_node, size_type_node);
 
-      gfor_fndecl_caf_team_number
-      	= gfc_build_library_function_decl_with_spec (
-	    get_identifier (PREFIX("caf_team_number")), ". r ",
-      	    integer_type_node, 1, integer_type_node);
+      gfor_fndecl_caf_team_number = gfc_build_library_function_decl_with_spec (
+	get_identifier (PREFIX ("caf_team_number")), ". r ", integer_type_node,
+	1, pvoid_type_node);
 
       gfor_fndecl_caf_image_status = gfc_build_library_function_decl_with_spec (
 	get_identifier (PREFIX ("caf_image_status")), ". r r ",
-- 
2.50.0

Reply via email to