See attached patch. A scalar ASSOCIATE name of a variable is a pointer
to its selector, and gfc_omp_predetermined_sharing makes that pointer
firstprivate. In a target construct the device then gets the host
address of the selector and faults when it dereferences it.
The fix treats the associate name like a by-reference dummy: privatized
by reference and predetermined shared, which OpenMP 5.2 (5.1.1)
requires anyway for an associate name whose association occurs outside
the construct. gfc_omp_scalar_p then looks through the pointer, so the
implicit data-mapping rules make the value firstprivate on a target
construct, as for any scalar.
Read the three trans-openmp.cc hunks in order:
gfc_omp_privatize_by_reference stops excluding associate names,
gfc_omp_predetermined_sharing returns shared for all of them, and
gfc_omp_scalar_p looks through the pointer. The rest is the new tests.
The OpenACC test, libgomp.oacc-fortran/associate3.f90, uses
firstprivate of an explicit-shape dummy and so needs the PR129003
patch, sent just before this one.
Regression tested on x86_64, and libgomp also with amdgcn offloading.
OK for mainline?
Regards,
Jerry
---
fortran: [PR129002] Treat a scalar associate name as its selector
A scalar ASSOCIATE name of a variable is a pointer to its selector, and
gfc_omp_predetermined_sharing made that pointer firstprivate. A target
construct therefore copied the host address of the selector to the
device, which faulted when dereferencing it. Treat the associate name
as a reference to its selector instead: privatized by reference,
predetermined shared, as OpenMP 5.2 (5.1.1) makes an associate name
whose association occurs outside the construct, and scalar for the
implicit data-mapping rules, so that a target construct makes its value
firstprivate.
Assisted-by: Claude Opus 5.5
PR fortran/129002
gcc/fortran/ChangeLog:
* trans-openmp.cc (gfc_omp_privatize_by_reference): Privatize
associate names by reference.
(gfc_omp_predetermined_sharing): Make associate names shared.
(gfc_omp_scalar_p): Look through the pointer of an associate name.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/associate3.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/associate5.f90: New test.
* testsuite/libgomp.oacc-fortran/associate3.f90: New test.
>From 47511d243e2e95238eff4019c83b1794f1d08aea Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 24 Nov 2026 17:41:07 -0800
Subject: [PATCH] fortran: [PR129002] Treat a scalar associate name as its
selector
A scalar ASSOCIATE name of a variable is a pointer to its selector, and
gfc_omp_predetermined_sharing made that pointer firstprivate. A target
construct therefore copied the host address of the selector to the
device, which faulted when dereferencing it. Treat the associate name
as a reference to its selector instead: privatized by reference,
predetermined shared, as OpenMP 5.2 (5.1.1) makes an associate name
whose association occurs outside the construct, and scalar for the
implicit data-mapping rules, so that a target construct makes its value
firstprivate.
Assisted-by: Claude Opus 5.5
PR fortran/129002
gcc/fortran/ChangeLog:
* trans-openmp.cc (gfc_omp_privatize_by_reference): Privatize
associate names by reference.
(gfc_omp_predetermined_sharing): Make associate names shared.
(gfc_omp_scalar_p): Look through the pointer of an associate name.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/associate3.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/associate5.f90: New test.
* testsuite/libgomp.oacc-fortran/associate3.f90: New test.
---
gcc/fortran/trans-openmp.cc | 20 +--
gcc/testsuite/gfortran.dg/gomp/associate3.f90 | 25 ++++
.../testsuite/libgomp.fortran/associate5.f90 | 123 ++++++++++++++++++
.../libgomp.oacc-fortran/associate3.f90 | 33 +++++
4 files changed, 188 insertions(+), 13 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/associate3.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/associate5.f90
create mode 100644 libgomp/testsuite/libgomp.oacc-fortran/associate3.f90
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index a01d888ec40..ec08fd3645b 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -262,11 +262,11 @@ gfc_omp_privatize_by_reference (const_tree decl)
/* Array POINTER/ALLOCATABLE have aggregate types, all user variables
that have POINTER_TYPE type and aren't scalar pointers, scalar
allocatables, Cray pointees or C pointers are supposed to be
- privatized by reference. */
+ privatized by reference. So are associate names that point to
+ their selector. */
if (GFC_DECL_GET_SCALAR_POINTER (decl)
|| GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
|| GFC_DECL_CRAY_POINTEE (decl)
- || GFC_DECL_ASSOCIATE_VAR_P (decl)
|| VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
return false;
@@ -290,18 +290,10 @@ gfc_omp_privatize_by_reference (const_tree decl)
enum omp_clause_default_kind
gfc_omp_predetermined_sharing (tree decl)
{
- /* Associate names preserve the association established during ASSOCIATE.
- As they are implemented either as pointers to the selector or array
- descriptor and shouldn't really change in the ASSOCIATE region,
- this decl can be either shared or firstprivate. If it is a pointer,
- use firstprivate, as it is cheaper that way, otherwise make it shared. */
+ /* An associate name whose association occurs outside the construct is
+ shared (OpenMP 5.2, 5.1.1). */
if (GFC_DECL_ASSOCIATE_VAR_P (decl))
- {
- if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
- return OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
- else
- return OMP_CLAUSE_DEFAULT_SHARED;
- }
+ return OMP_CLAUSE_DEFAULT_SHARED;
if (DECL_ARTIFICIAL (decl)
&& ! GFC_DECL_RESULT (decl)
@@ -2807,6 +2799,8 @@ gfc_omp_scalar_p (tree decl, bool ptr_alloc_ok)
return false;
type = TREE_TYPE (type);
}
+ else if (GFC_DECL_ASSOCIATE_VAR_P (decl))
+ type = TREE_TYPE (type);
if (GFC_ARRAY_TYPE_P (type)
|| GFC_CLASS_TYPE_P (type))
return false;
diff --git a/gcc/testsuite/gfortran.dg/gomp/associate3.f90 b/gcc/testsuite/gfortran.dg/gomp/associate3.f90
new file mode 100644
index 00000000000..9171d59ba79
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/associate3.f90
@@ -0,0 +1,25 @@
+! { dg-do compile }
+! { dg-additional-options "-fdump-tree-gimple" }
+!
+! PR 129002
+
+subroutine sub (w, n, lr)
+ integer :: n
+ real :: w(n)
+ real :: lr
+ integer :: i
+ associate (eta => lr)
+ !$omp target teams distribute parallel do
+ do i = 1, n
+ w(i) = w(i) - eta
+ end do
+ !$omp parallel do
+ do i = 1, n
+ w(i) = w(i) + eta
+ end do
+ end associate
+end subroutine
+
+! { dg-final { scan-tree-dump-times "#pragma omp target\[^\n\r\]* firstprivate\\(eta\\)" 1 "gimple" } }
+! { dg-final { scan-tree-dump-not "map\\(\[a-z\]*:eta " "gimple" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel\[^\n\r\]* shared\\(eta\\)" 2 "gimple" } }
diff --git a/libgomp/testsuite/libgomp.fortran/associate5.f90 b/libgomp/testsuite/libgomp.fortran/associate5.f90
new file mode 100644
index 00000000000..5597a7a560c
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/associate5.f90
@@ -0,0 +1,123 @@
+! { dg-do run }
+!
+! PR 129002
+
+module m
+ implicit none
+ type t
+ integer :: k
+ real(8) :: r
+ end type
+contains
+ subroutine upd (w, n, lr)
+ integer, intent(in) :: n
+ real, intent(inout) :: w(n)
+ real, intent(in), optional :: lr
+ integer :: l
+ associate (eta => lr, m => n)
+ !$omp target teams distribute parallel do
+ do l = 1, m
+ w(l) = w(l) - eta
+ end do
+ end associate
+ end subroutine
+
+ subroutine upd2 (w, lr)
+ real :: w(8)
+ real, intent(in) :: lr
+ integer :: l
+ associate (eta => lr)
+ !$omp target
+ do l = 1, 8
+ w(l) = w(l) * eta
+ end do
+ !$omp end target
+ end associate
+ end subroutine
+end module
+
+program p
+ use m
+ implicit none
+ real :: w(8), x, c(5), v(20), y(10)
+ integer :: i, k
+ type(t) :: d
+ character(len=5) :: str
+ integer :: a(5)
+ complex(8) :: z, zw(8)
+ logical :: flag
+
+ w = 1.0
+ call upd (w, 8, 0.5)
+ if (any (w /= 0.5)) stop 1
+ call upd2 (w, 4.0)
+ if (any (w /= 2.0)) stop 2
+
+ x = 3.0
+ associate (s => x)
+ associate (s2 => s)
+ !$omp target teams distribute parallel do
+ do i = 1, 8
+ w(i) = s + s2
+ end do
+ end associate
+ end associate
+ if (any (w /= 6.0)) stop 3
+
+ c = [1., 2., 3., 4., 5.]
+ d = t(7, 2.5d0)
+ associate (s => c(3), r => d%r, kk => d%k)
+ !$omp target parallel do
+ do i = 1, 8
+ w(i) = s * i + real (r) + kk
+ end do
+ end associate
+ do i = 1, 8
+ if (w(i) /= 3.0 * i + 9.5) stop 4
+ end do
+
+ str = 'abcde'
+ z = (1d0, -2d0)
+ flag = .true.
+ associate (ch => str, zz => z, f => flag)
+ !$omp target teams distribute parallel do
+ do i = 1, 5
+ a(i) = ichar (ch(i:i))
+ if (f) zw(i) = zz * i
+ end do
+ end associate
+ do i = 1, 5
+ if (a(i) /= ichar (str(i:i))) stop 5
+ if (zw(i) /= z * i) stop 6
+ end do
+
+ ! A scalar associate name is firstprivate in a target region.
+ k = 1
+ associate (s => k)
+ !$omp target
+ s = 2
+ !$omp end target
+ end associate
+ if (k /= 1) stop 7
+
+ ! A non-scalar one is mapped tofrom.
+ y = [(real (i), i = 1, 10)]
+ associate (vv => y(2:5))
+ !$omp target teams distribute parallel do
+ do i = 1, 4
+ vv(i) = -vv(i)
+ end do
+ end associate
+ if (any (y(2:5) /= [-2., -3., -4., -5.])) stop 8
+
+ ! Shared on the host when associated outside the construct.
+ v = 0
+ associate (s => v(2))
+ !$omp parallel num_threads(4)
+ !$omp single
+ s = 5
+ !$omp end single
+ !$omp end parallel
+ end associate
+ if (v(2) /= 5) stop 9
+end program
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/associate3.f90 b/libgomp/testsuite/libgomp.oacc-fortran/associate3.f90
new file mode 100644
index 00000000000..eeeef03844c
--- /dev/null
+++ b/libgomp/testsuite/libgomp.oacc-fortran/associate3.f90
@@ -0,0 +1,33 @@
+! { dg-do run }
+!
+! PR 129002
+
+subroutine sub (a, r, lr)
+ real :: a(16), r(16)
+ real, intent(in) :: lr
+ integer :: i
+ associate (s => lr)
+ !$acc parallel loop firstprivate(a) copyout(r)
+ do i = 1, 16
+ r(i) = a(i) + a(17 - i) - s
+ end do
+ end associate
+end subroutine
+
+program p
+ real :: a(16), r(16), w(64), x
+ integer :: i
+ a = [(real (i), i = 1, 16)]
+ call sub (a, r, 1.0)
+ if (any (r /= 16.0)) stop 1
+
+ x = 0.25
+ w = 1
+ associate (s => x)
+ !$acc parallel loop copy(w)
+ do i = 1, 64
+ w(i) = w(i) - s
+ end do
+ end associate
+ if (any (w /= 0.75)) stop 2
+end program
--
2.56.1