See attached patch. With firstprivate(a) on an OpenACC compute
construct, where A is a Fortran explicit-shape dummy array,
lower_omp_target sizes the copy from the type of A. That type is a
pointer, not a reference, so the device gets a copy of 8 bytes and
reads past it for everything after the first two elements.

The fix also looks through the pointer when omp_privatize_by_reference
says the variable is privatized by reference, which is what the
Fortran front end's hook answers for such a dummy. The changed code is
in the OpenACC-only branch of the firstprivate case, so OpenMP is not
affected.

The fix is the one-line change in lower_omp_target in omp-low.cc, which
needs a middle-end review; the rest is the new libgomp.oacc-fortran
test.

Regression tested on x86_64, and libgomp also with amdgcn offloading.

OK for mainline?

Regards,

Jerry

---

openacc: [PR129003] Size the firstprivate copy of a by-reference dummy

For firstprivate on an OpenACC compute construct, lower_omp_target took
the size of the copy from the type of the variable, looking through a
reference type only.  A Fortran explicit-shape dummy array has a pointer
type and is privatized by reference, so only the size of a pointer was
copied, and the device read past it.  Look through the pointer of a
variable that omp_privatize_by_reference says is privatized by
reference.

Assisted-by: Claude Opus 5.5

        PR fortran/129003

gcc/ChangeLog:

        * omp-low.cc (lower_omp_target): For OpenACC firstprivate, take the
        size of a variable privatized by reference from its pointed-to type.

libgomp/ChangeLog:

        * testsuite/libgomp.oacc-fortran/firstprivate-2.f90: New test.
>From 25d4cfc8b7f1ccfeb58fb64fbbe1180c59e48467 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 24 Nov 2026 17:41:07 -0800
Subject: [PATCH] openacc: [PR129003] Size the firstprivate copy of a
 by-reference dummy

For firstprivate on an OpenACC compute construct, lower_omp_target took
the size of the copy from the type of the variable, looking through a
reference type only.  A Fortran explicit-shape dummy array has a pointer
type and is privatized by reference, so only the size of a pointer was
copied, and the device read past it.  Look through the pointer of a
variable that omp_privatize_by_reference says is privatized by
reference.

Assisted-by: Claude Opus 5.5

	PR fortran/129003

gcc/ChangeLog:

	* omp-low.cc (lower_omp_target): For OpenACC firstprivate, take the
	size of a variable privatized by reference from its pointed-to type.

libgomp/ChangeLog:

	* testsuite/libgomp.oacc-fortran/firstprivate-2.f90: New test.
---
 gcc/omp-low.cc                                |  1 +
 .../libgomp.oacc-fortran/firstprivate-2.f90   | 20 +++++++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 libgomp/testsuite/libgomp.oacc-fortran/firstprivate-2.f90

diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index 485fcc8565e..93a90caaffd 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -13696,6 +13696,7 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
 		gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
 		s = TREE_TYPE (ovar);
 		if (TREE_CODE (s) == REFERENCE_TYPE
+		    || omp_privatize_by_reference (ovar)
 		    || omp_check_optional_argument (ovar, false))
 		  s = TREE_TYPE (s);
 		s = TYPE_SIZE_UNIT (s);
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/firstprivate-2.f90 b/libgomp/testsuite/libgomp.oacc-fortran/firstprivate-2.f90
new file mode 100644
index 00000000000..567a1b9ab6f
--- /dev/null
+++ b/libgomp/testsuite/libgomp.oacc-fortran/firstprivate-2.f90
@@ -0,0 +1,20 @@
+! { dg-do run }
+!
+! PR 129003
+
+subroutine sub (a, r)
+  real :: a(16), r(16)
+  integer :: i
+  !$acc parallel loop firstprivate(a) copyout(r)
+  do i = 1, 16
+    r(i) = a(i) + a(17 - i)
+  end do
+end subroutine
+
+program p
+  real :: a(16), r(16)
+  integer :: i
+  a = [(real (i), i = 1, 16)]
+  call sub (a, r)
+  if (any (r /= 17.0)) stop 1
+end program
-- 
2.56.1

Reply via email to