[Fortran,PR84870,v1] Fix ICE and allocated memory not assigned correctly.

Message ID 20240919160131.656b36e0@vepi2
State New
Headers
Series [Fortran,PR84870,v1] Fix ICE and allocated memory not assigned correctly. |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Andre Vehreschild Sept. 19, 2024, 2:01 p.m. UTC
  Hi all,

in PR84870 an ICE was reported, that has been fixed in the meantime by some
other patch. Nevertheless did a testcase reveal that the memory handling still
was not correct. I.e. the test case in the patch was answering 2 for both x.b.a
and y.b.a which is not correct.

For a coarray all memory is allocated using an array descriptor. For scalars
just a temporary descriptor is created and handed to the caf-register routine.
The error here was, that the memory now handed back in the temporary descriptor
was not used for the memory in the component, thus the pointer in the component
was not updated. The patch fixes this.

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

Regards,
	Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de
  

Comments

Harald Anlauf Sept. 23, 2024, 7:19 p.m. UTC | #1
Hi Andre,

Am 19.09.24 um 16:01 schrieb Andre Vehreschild:
> Hi all,
>
> in PR84870 an ICE was reported, that has been fixed in the meantime by some
> other patch. Nevertheless did a testcase reveal that the memory handling still
> was not correct. I.e. the test case in the patch was answering 2 for both x.b.a
> and y.b.a which is not correct.
>
> For a coarray all memory is allocated using an array descriptor. For scalars
> just a temporary descriptor is created and handed to the caf-register routine.
> The error here was, that the memory now handed back in the temporary descriptor
> was not used for the memory in the component, thus the pointer in the component
> was not updated. The patch fixes this.
>
> Regtests ok on x86_64-pc-linux-gnu / Fedora 39. Ok for mainline?

this looks good to me.

Thanks for the patch!

Harald

> Regards,
> 	Andre
> --
> Andre Vehreschild * Email: vehre ad gmx dot de
  
Andre Vehreschild Sept. 24, 2024, 11:16 a.m. UTC | #2
Hi Harald,

thanks for the review. Committed as gcc-15-3825-gf5035d7d015

Thanks again,
	Andre

On Mon, 23 Sep 2024 21:19:40 +0200
Harald Anlauf <anlauf@gmx.de> wrote:

> Hi Andre,
>
> Am 19.09.24 um 16:01 schrieb Andre Vehreschild:
> > Hi all,
> >
> > in PR84870 an ICE was reported, that has been fixed in the meantime by some
> > other patch. Nevertheless did a testcase reveal that the memory handling
> > still was not correct. I.e. the test case in the patch was answering 2 for
> > both x.b.a and y.b.a which is not correct.
> >
> > For a coarray all memory is allocated using an array descriptor. For scalars
> > just a temporary descriptor is created and handed to the caf-register
> > routine. The error here was, that the memory now handed back in the
> > temporary descriptor was not used for the memory in the component, thus the
> > pointer in the component was not updated. The patch fixes this.
> >
> > Regtests ok on x86_64-pc-linux-gnu / Fedora 39. Ok for mainline?
>
> this looks good to me.
>
> Thanks for the patch!
>
> Harald
>
> > Regards,
> > 	Andre
> > --
> > Andre Vehreschild * Email: vehre ad gmx dot de
>


--
Andre Vehreschild * Email: vehre ad gmx dot de
  

Patch

From c26e97a8196fc26abf36a0bad6ffd6f9da7ba5d8 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild <vehre@gcc.gnu.org>
Date: Thu, 19 Sep 2024 15:09:52 +0200
Subject: [PATCH] Fortran: Assign allocated caf-memory to scalar members
 [PR84870]

Allocating a coarray required an array-descriptor.  For scalars a
temporary descriptor was created.  Assigning the allocated memory from
the temporary descriptor back to the scalar is now added.

gcc/fortran/ChangeLog:

	PR fortran/84870

	* trans-array.cc (duplicate_allocatable_coarray): For scalar
	allocatable components the memory allocated is now assigned to
	the component's pointer.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray/alloc_comp_10.f90: New test.
---
 gcc/fortran/trans-array.cc                    |  2 ++
 .../gfortran.dg/coarray/alloc_comp_10.f90     | 24 +++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/alloc_comp_10.f90

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 838b6d3da80..3da7479fd10 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -9451,6 +9451,7 @@  duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src, tree type,
 				  gfc_build_addr_expr (NULL_TREE, dest_tok),
 				  NULL_TREE, NULL_TREE, NULL_TREE,
 				  GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY);
+      gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));
       null_data = gfc_finish_block (&block);

       gfc_init_block (&block);
@@ -9460,6 +9461,7 @@  duplicate_allocatable_coarray (tree dest, tree dest_tok, tree src, tree type,
 				  gfc_build_addr_expr (NULL_TREE, dest_tok),
 				  NULL_TREE, NULL_TREE, NULL_TREE,
 				  GFC_CAF_COARRAY_ALLOC);
+      gfc_add_modify (&block, dest, gfc_conv_descriptor_data_get (dummy_desc));

       tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
       tmp = build_call_expr_loc (input_location, tmp, 3, dest, src,
diff --git a/gcc/testsuite/gfortran.dg/coarray/alloc_comp_10.f90 b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_10.f90
new file mode 100644
index 00000000000..a31d005498c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_10.f90
@@ -0,0 +1,24 @@ 
+!{ dg-do run }
+
+! Check that copying of memory for allocated scalar is assigned
+! to coarray object.
+
+! Contributed by G. Steinmetz  <gscfq@t-online.de>
+
+program p
+  type t
+    integer, allocatable :: a
+  end type
+  type t2
+    type(t), allocatable :: b
+  end type
+  type(t2) :: x, y[*]
+
+  x%b = t(1)
+  y = x
+  y%b%a = 2
+
+  if (x%b%a /= 1) stop 1
+  if (y%b%a /= 2) stop 2
+end
+
--
2.46.0