public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7709] Fortran/OpenMP: Fix privatization of associated names
@ 2022-03-18 16:40 Tobias Burnus
  0 siblings, 0 replies; only message in thread
From: Tobias Burnus @ 2022-03-18 16:40 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:c133bdfa9e7d9225510d00dbb7270cc052e4e4ee

commit r12-7709-gc133bdfa9e7d9225510d00dbb7270cc052e4e4ee
Author: Tobias Burnus <tobias@codesourcery.com>
Date:   Fri Mar 18 17:40:22 2022 +0100

    Fortran/OpenMP: Fix privatization of associated names
    
    gfc_omp_predetermined_sharing cases the associate-name pointer variable
    to be OMP_CLAUSE_DEFAULT_FIRSTPRIVATE, which is fine. However, the associated
    selector is shared. Thus, the target of associate-name pointer should not get
    copied. (It was before but because of gfc_omp_privatize_by_reference returning
    false, the selector was not only wrongly copied but this was also not done
    properly.)
    
    gcc/fortran/ChangeLog:
    
            PR fortran/103039
            * trans-openmp.cc (gfc_omp_clause_copy_ctor, gfc_omp_clause_dtor):
            Only privatize pointer for associate names.
    
    libgomp/ChangeLog:
    
            PR fortran/103039
            * testsuite/libgomp.fortran/associate4.f90: New test.

Diff:
---
 gcc/fortran/trans-openmp.cc                      | 10 +++
 libgomp/testsuite/libgomp.fortran/associate4.f90 | 92 ++++++++++++++++++++++++
 2 files changed, 102 insertions(+)

diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index fad76a4791f..101924f5e76 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -808,6 +808,11 @@ gfc_omp_clause_copy_ctor (tree clause, tree dest, tree src)
   gcc_assert (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_FIRSTPRIVATE
 	      || OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_LINEAR);
 
+  /* Privatize pointer, only; cf. gfc_omp_predetermined_sharing. */
+  if (DECL_P (OMP_CLAUSE_DECL (clause))
+      && GFC_DECL_ASSOCIATE_VAR_P (OMP_CLAUSE_DECL (clause)))
+    return build2 (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
+
   if (DECL_ARTIFICIAL (OMP_CLAUSE_DECL (clause))
       && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (clause))
       && GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_DECL (clause)))
@@ -1321,6 +1326,11 @@ gfc_omp_clause_dtor (tree clause, tree decl)
   tree type = TREE_TYPE (decl), tem;
   tree decl_type = TREE_TYPE (OMP_CLAUSE_DECL (clause));
 
+  /* Only pointer was privatized; cf. gfc_omp_clause_copy_ctor. */
+  if (DECL_P (OMP_CLAUSE_DECL (clause))
+      && GFC_DECL_ASSOCIATE_VAR_P (OMP_CLAUSE_DECL (clause)))
+    return NULL_TREE;
+
   if (DECL_ARTIFICIAL (OMP_CLAUSE_DECL (clause))
       && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (clause))
       && GFC_DECL_SAVED_DESCRIPTOR (OMP_CLAUSE_DECL (clause)))
diff --git a/libgomp/testsuite/libgomp.fortran/associate4.f90 b/libgomp/testsuite/libgomp.fortran/associate4.f90
new file mode 100644
index 00000000000..f0949b5530d
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/associate4.f90
@@ -0,0 +1,92 @@
+! PR fortran/51722
+
+module m
+implicit none
+
+contains
+
+subroutine seltype
+
+type :: a
+   integer :: p = 2
+end type a
+
+type, extends(a) :: b
+   integer :: cnt = 0
+end type b
+
+integer :: k, s
+class(a), pointer :: x
+
+allocate(a :: x)
+s = 0
+select type (y => x)
+class is (a)
+!$omp parallel do default(shared) private(k) reduction(+:s)
+   do k = 1,10
+      s = s + k*y%p
+   end do
+!$omp end parallel do
+end select
+
+if (s /= 110) error stop
+deallocate(x)
+allocate(b :: x)
+
+s = 0
+select type (y => x)
+class is (b)
+!$omp parallel do default(shared) private(k) reduction(+:s)
+   do k = 1,10
+      s = s + k*y%p
+!$omp atomic update
+      y%cnt = y%cnt + 2
+   end do
+!$omp end parallel do
+if (s /= 110) error stop
+if (y%p /= 2) error stop
+if (y%cnt /= 10*2) error stop
+end select
+
+deallocate(x)
+
+end subroutine seltype
+
+subroutine assoc
+
+type :: b
+   integer :: r = 3
+end type b
+
+type :: a
+   integer :: p = 2
+   class(b), pointer :: u => null()
+end type a
+
+integer :: k, s
+class(a), pointer :: x
+
+s = 0
+allocate(a :: x)
+allocate(b :: x%u)
+
+associate(f => x%u)
+!$omp parallel do default(shared) private(k) reduction(+:s)
+   do k = 1,10
+      s = s + k*f%r
+   end do
+!$omp end parallel do
+end associate
+
+deallocate(x%u)
+deallocate(x)
+
+if (s /= 165) error stop
+end subroutine assoc
+end module m
+
+use m
+implicit none (type, external)
+call seltype
+call assoc
+end


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-18 16:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-18 16:40 [gcc r12-7709] Fortran/OpenMP: Fix privatization of associated names Tobias Burnus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).