diff options
author | Harald Anlauf <anlauf@gmx.de> | 2025-01-19 21:06:56 +0100 |
---|---|---|
committer | Harald Anlauf <anlauf@gmx.de> | 2025-01-20 18:43:18 +0100 |
commit | 0d1e62b83561baa185bf080515750a89dd3ac410 (patch) | |
tree | a4dd9f658eba51b9d148b180a5b9522869dbffc3 /gcc/fortran/trans-expr.cc | |
parent | 0b58219fe112c01ff335edf699c4fc69e718c75b (diff) | |
download | gcc-0d1e62b83561baa185bf080515750a89dd3ac410.zip gcc-0d1e62b83561baa185bf080515750a89dd3ac410.tar.gz gcc-0d1e62b83561baa185bf080515750a89dd3ac410.tar.bz2 |
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.
Diffstat (limited to 'gcc/fortran/trans-expr.cc')
-rw-r--r-- | gcc/fortran/trans-expr.cc | 11 |
1 files changed, 9 insertions, 2 deletions
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); |