From 0d1e62b83561baa185bf080515750a89dd3ac410 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Sun, 19 Jan 2025 21:06:56 +0100 Subject: Fortran: do not copy back for parameter actual arguments [PR81978] When an array is packed for passing as an actual argument, and the array has the PARAMETER attribute (i.e., it is a named constant that can reside in read-only memory), do not copy back (unpack) from the temporary. PR fortran/81978 gcc/fortran/ChangeLog: * trans-array.cc (gfc_conv_array_parameter): Do not copy back data if actual array parameter has the PARAMETER attribute. * trans-expr.cc (gfc_conv_subref_array_arg): Likewise. gcc/testsuite/ChangeLog: * gfortran.dg/pr81978.f90: New test. --- gcc/fortran/trans-expr.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'gcc/fortran/trans-expr.cc') diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index bef49d3..dcf42d5 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -5200,6 +5200,7 @@ gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77, gfc_se work_se; gfc_se *parmse; bool pass_optional; + bool readonly; pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional; @@ -5416,8 +5417,14 @@ gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77, /* Wrap the whole thing up by adding the second loop to the post-block and following it by the post-block of the first loop. In this way, - if the temporary needs freeing, it is done after use! */ - if (intent != INTENT_IN) + if the temporary needs freeing, it is done after use! + If input expr is read-only, e.g. a PARAMETER array, copying back + modified values is undefined behavior. */ + readonly = (expr->expr_type == EXPR_VARIABLE + && expr->symtree + && expr->symtree->n.sym->attr.flavor == FL_PARAMETER); + + if ((intent != INTENT_IN) && !readonly) { gfc_add_block_to_block (&parmse->post, &loop2.pre); gfc_add_block_to_block (&parmse->post, &loop2.post); -- cgit v1.1