See attached patch. A function returns an array through a hidden
argument, __result, which create_function_arglist makes DECL_ARTIFICIAL
but did not mark GFC_DECL_RESULT, as the decls for other function
results are. gfc_omp_predetermined_mapping maps an artificial decl 'to'
unless it is a result, so what a target construct writes to the result
stays on the device, and the caller sees the values from before the
region.
The fix sets GFC_DECL_RESULT on that argument, so the mapping falls
through to the usual implicit rules and the result data is mapped
tofrom. GFC_DECL_RESULT is otherwise read only by the OpenMP hooks
gfc_omp_predetermined_sharing, which keeps the argument shared, and
gfc_omp_disregard_value_expr, which only looks at results with a value
expression.
The fix is the one line in create_function_arglist in trans-decl.cc;
the rest is the new gomp scan test and libgomp run test.
Regression tested on x86_64, and libgomp also with amdgcn offloading.
OK for mainline?
Regards,
Jerry
---
fortran: [PR129004] Map an array-valued function result tofrom
The hidden argument through which a function returns an array is
DECL_ARTIFICIAL but was not marked GFC_DECL_RESULT, so
gfc_omp_predetermined_mapping mapped it 'to' only, and what a target
region wrote to the result was lost. Mark it GFC_DECL_RESULT.
Assisted-by: Claude Opus 5.5
PR fortran/129004
gcc/fortran/ChangeLog:
* trans-decl.cc (create_function_arglist): Set GFC_DECL_RESULT on
the hidden result argument.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/target-result-1.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/target-result-1.f90: New test.
>From 545ffc2d4e402f38719f98d89d69f41012c07e5d Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 24 Nov 2026 17:41:07 -0800
Subject: [PATCH] fortran: [PR129004] Map an array-valued function result
tofrom
The hidden argument through which a function returns an array is
DECL_ARTIFICIAL but was not marked GFC_DECL_RESULT, so
gfc_omp_predetermined_mapping mapped it 'to' only, and what a target
region wrote to the result was lost. Mark it GFC_DECL_RESULT.
Assisted-by: Claude Opus 5.5
PR fortran/129004
gcc/fortran/ChangeLog:
* trans-decl.cc (create_function_arglist): Set GFC_DECL_RESULT on
the hidden result argument.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/target-result-1.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/target-result-1.f90: New test.
---
gcc/fortran/trans-decl.cc | 2 ++
.../gfortran.dg/gomp/target-result-1.f90 | 20 +++++++++++
.../libgomp.fortran/target-result-1.f90 | 36 +++++++++++++++++++
3 files changed, 58 insertions(+)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/target-result-1.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-result-1.f90
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 028ec6e7bef..a5317f5f475 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -2880,6 +2880,8 @@ create_function_arglist (gfc_symbol * sym)
DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
TREE_READONLY (parm) = 1;
DECL_ARTIFICIAL (parm) = 1;
+ /* OpenMP maps an artificial decl 'to' unless it is the result. */
+ GFC_DECL_RESULT (parm) = 1;
gfc_finish_decl (parm);
arglist = chainon (arglist, parm);
diff --git a/gcc/testsuite/gfortran.dg/gomp/target-result-1.f90 b/gcc/testsuite/gfortran.dg/gomp/target-result-1.f90
new file mode 100644
index 00000000000..07218be8535
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/target-result-1.f90
@@ -0,0 +1,20 @@
+! { dg-do compile }
+! { dg-additional-options "-fdump-tree-gimple" }
+!
+! PR 129004
+
+module m
+contains
+ function f (a) result (r)
+ real(8), intent(in) :: a(:)
+ real(8), allocatable :: r(:)
+ integer :: i
+ allocate (r, mold=a)
+ !$omp target teams distribute parallel do
+ do i = 1, size (a)
+ r(i) = -a(i)
+ end do
+ end function
+end module
+
+! { dg-final { scan-tree-dump "map\\(tofrom:\\*\\(real\\(kind=8\\)\\\[0:\\\] \\* restrict\\) __result->data" "gimple" } }
diff --git a/libgomp/testsuite/libgomp.fortran/target-result-1.f90 b/libgomp/testsuite/libgomp.fortran/target-result-1.f90
new file mode 100644
index 00000000000..f60cb92909c
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/target-result-1.f90
@@ -0,0 +1,36 @@
+! { dg-do run }
+!
+! PR 129004
+
+module m
+contains
+ function f (a) result (r)
+ real(8), intent(in) :: a(:)
+ real(8), allocatable :: r(:)
+ integer :: i
+ allocate (r, mold=a)
+ !$omp target teams distribute parallel do
+ do i = 1, size (a)
+ r(i) = -a(i)
+ end do
+ end function
+
+ function g (a)
+ real(8), intent(in) :: a(:)
+ real(8), allocatable :: g(:)
+ integer :: i
+ allocate (g(size (a)))
+ !$omp target
+ do i = 1, size (a)
+ g(i) = 2 * a(i)
+ end do
+ !$omp end target
+ end function
+end module
+
+program p
+ use m
+ real(8) :: a(5) = [1, 2, 3, 4, 5]
+ if (any (f (a) /= -a)) stop 1
+ if (any (g (a) /= 2 * a)) stop 2
+end program
--
2.56.1