The attached patch from Andre fixes this issue. It goes on top of the other caf_shmem patches.

New test case which skips it for -fcoarray=single and otherwise verified that caf.exp is indeed setting the GFORTRAN_NUM_IMAGES to 8.

Author: Andre Vehreschild <[email protected]>
Date:   Sat Feb 14 11:37:07 2026 -0800

    Fortran: Fix coarray shared mem [PR121429]

            PR fortran/121429

    libgfortran/ChangeLog:

            * caf/shmem.c (_gfortran_caf_deregister): Add a call to
            sync_team.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/coarray/deallocate_sync.f90: New test.

OK for mainline after we get the whole patch set approved. I am working through comments and fixes as I go so we have a trail.

Regards,

Jerry
commit 0acfae43f7b093c8d52bfbcea3a758bc24519416
Author: Andre Vehreschild <[email protected]>
Date:   Sat Feb 14 11:37:07 2026 -0800

    Fortran: Fix coarray shared mem [PR121429]
    
            PR fortran/121429
    
    libgfortran/ChangeLog:
    
            * caf/shmem.c (_gfortran_caf_deregister): Add a call to
            sync_team.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/coarray/deallocate_sync.f90: New test.

diff --git a/gcc/testsuite/gfortran.dg/coarray/deallocate_sync.f90 b/gcc/testsuite/gfortran.dg/coarray/deallocate_sync.f90
new file mode 100644
index 00000000000..8c6f2b79fb1
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/deallocate_sync.f90
@@ -0,0 +1,26 @@
+! { dg-do run }
+! Test case from pr121429 which failed because the deallocate was not
+! doing an implicit sync all.
+program main
+  implicit none
+  integer, allocatable, codimension[:] :: a(:), b(:)
+  integer :: n, i, left, right
+  integer :: k
+  n = num_images()
+  i = this_image()
+  !   We skip this test for -fcoarray=single
+  if (n .ne. 1) then
+    ! Verify in the testsuite that num_images = 8 as set in caf.exp
+    if (n .ne. 8) error stop "Need at least three images"
+    left = modulo(i+n-2,n)+1
+    right = modulo(i,n)+1
+    do k=1,1000
+       allocate(a(k+5)[*])
+       allocate(b(k)[*])
+       b(:)[right] = right + 1000 * k
+       deallocate (a)  ! This synchronizes automatically
+       if (any(b(:)[left] /= left + 1000 * k)) error stop "Test failed"
+       deallocate (b)
+    end do
+  end if
+end program main
diff --git a/libgfortran/caf/shmem.c b/libgfortran/caf/shmem.c
index 9913db6d709..de6a2b23017 100644
--- a/libgfortran/caf/shmem.c
+++ b/libgfortran/caf/shmem.c
@@ -465,6 +465,7 @@ _gfortran_caf_deregister (caf_token_t *token, caf_deregister_t type, int *stat,
       free (ca);
       free (TOKEN (*token));
       *token = NULL;
+      sync_team (caf_current_team);
     }
   else
     {

Reply via email to