This patch just removes a
case OMP_REDUCTION_USER:
- /* Non-operators can't be used outside of the module. */
- if (udr->name[0] != '.')
- return;
I have to admit that I have not the slightest idea why those
shouldn't be permitted; it seems as if AMD's LLVM-based compiler
supports it and I also do not see any indication in OpenMP 4.0
(that added UDR) nor in OpenMP 6.1 why this should be invalid.
Just removing this makes the testcase run and I also don't think
there are any naming/scoping issues with this change, either.
Hence, I intent to commit it. Comments before I do so?
Tobias
OpenMP/Fortran: Permit non-operator user-defined reductions in modules
There should be no reason why user-defined reductions other than user-defined
operators (".my_op.") should be not accessible outside of module files. However,
the commit that added that feature did not permit it: r5-1132-g5f23671d3fffb5;
this commit added the the module support of UDR at the first place.
The OpenMP 4.0 specification already had the following wording, which implies
that use association is permitted (esp. last two words):
"If the reduction-identifier is defined in a declare reduction directive, the
declare reduction directive must be in the same subprogram, or accessible by
host or use association."
gcc/fortran/ChangeLog:
* module.cc (write_omp_udr): Also permit named UDR other than
operators.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/udr16.f90: New test.
gcc/fortran/module.cc | 38 +++++++++++++----------------
libgomp/testsuite/libgomp.fortran/udr16.f90 | 37 ++++++++++++++++++++++++++++
2 files changed, 54 insertions(+), 21 deletions(-)
diff --git a/gcc/fortran/module.cc b/gcc/fortran/module.cc
index 634af39659c..4c1c28b6a76 100644
--- a/gcc/fortran/module.cc
+++ b/gcc/fortran/module.cc
@@ -6688,27 +6688,23 @@ write_omp_udr (gfc_omp_udr *udr)
switch (udr->rop)
{
case OMP_REDUCTION_USER:
- /* Non-operators can't be used outside of the module. */
- if (udr->name[0] != '.')
- return;
- else
- {
- gfc_symtree *st;
- size_t len = strlen (udr->name + 1);
- char *name = XALLOCAVEC (char, len);
- memcpy (name, udr->name, len - 1);
- name[len - 1] = '\0';
- st = gfc_find_symtree (gfc_current_ns->uop_root, name);
- /* If corresponding user operator is private, don't write
- the UDR. */
- if (st != NULL)
- {
- gfc_user_op *uop = st->n.uop;
- if (!check_access (uop->access, uop->ns->default_access))
- return;
- }
- }
- break;
+ {
+ gfc_symtree *st;
+ size_t len = strlen (udr->name + 1);
+ char *name = XALLOCAVEC (char, len);
+ memcpy (name, udr->name, len - 1);
+ name[len - 1] = '\0';
+ st = gfc_find_symtree (gfc_current_ns->uop_root, name);
+ /* If corresponding user operator is private, don't write
+ the UDR. */
+ if (st != NULL)
+ {
+ gfc_user_op *uop = st->n.uop;
+ if (!check_access (uop->access, uop->ns->default_access))
+ return;
+ }
+ break;
+ }
case OMP_REDUCTION_PLUS:
case OMP_REDUCTION_MINUS:
case OMP_REDUCTION_TIMES:
diff --git a/libgomp/testsuite/libgomp.fortran/udr16.f90 b/libgomp/testsuite/libgomp.fortran/udr16.f90
new file mode 100644
index 00000000000..c72cc361289
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/udr16.f90
@@ -0,0 +1,37 @@
+! Test that named user-defined reductions actually work
+
+! Test case based on https://github.com/sbryngelson/offtarget
+!
+! MIT License
+! Copyright (c) 2026 Spencer Bryngelson
+
+! { dg-additional-options "-foffload-options=nvptx-none=-latomic" { target offload_target_nvptx } }
+
+module pair_m
+ implicit none
+ type :: pair_t
+ integer :: a, b
+ end type pair_t
+ !$omp declare reduction(padd: pair_t: omp_out = &
+ !$omp& pair_t(omp_out%a + omp_in%a, omp_out%b + omp_in%b)) &
+ !$omp& initializer(omp_priv = pair_t(0, 0))
+end module pair_m
+
+program udr_use_association
+ use omp_lib, only: omp_is_initial_device
+ use pair_m
+ implicit none
+ type(pair_t) :: s
+ integer :: i, host_flag
+ s = pair_t(0, 0)
+ host_flag = 1
+ !$omp target teams distribute parallel do reduction(padd: s) map(tofrom: s, host_flag)
+ do i = 1, 100
+ if (i == 1) host_flag = merge(1, 0, omp_is_initial_device())
+ s%a = s%a + i
+ s%b = s%b + 1
+ end do
+ ! print '(a,i0,a,i0)', 's%a= ', s%a, ' s%b= ', s%b
+ ! print '(a,i0,a,a)', 'RESULT ran_on_host=', host_flag, ' check=', merge('pass', 'fail', s%a == 5050 .and. s%b == 100)
+ if (s%a /= 5050 .or. s%b /= 100) stop 1
+end program udr_use_association