https://gcc.gnu.org/g:c4c2ef43206f33a3abf08f5b59a8795f5d5256a1
commit r17-6626-gc4c2ef43206f33a3abf08f5b59a8795f5d5256a1 Author: Tobias Burnus <[email protected]> Date: Tue Nov 24 03:43:28 2026 +0100 OpenMP: Fix Fortran mapping of allocatable comps of global variables [PR128969] The scan_sharing_clauses function also takes care of ensuring the struct that handles the 'addrs' argument for mapping also has space for Fortran's deep mapping of allocatable components. However, setting the flex_array_ptr boolean, which required for this, failed for global variables as those returned early. The solution is to reorder the if blocks. PR fortran/128969 gcc/ChangeLog: * omp-low.cc (scan_sharing_clauses): Reorder 'if' blocks to handle allocatable components of global variables. libgomp/ChangeLog: * testsuite/libgomp.fortran/map-static-alloc-comp-1.f90: New test. Diff: --- gcc/omp-low.cc | 25 +++++++------ .../libgomp.fortran/map-static-alloc-comp-1.f90 | 43 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc index 2046e01127a2..485fcc8565ee 100644 --- a/gcc/omp-low.cc +++ b/gcc/omp-low.cc @@ -1627,6 +1627,19 @@ scan_sharing_clauses (tree clauses, omp_context *ctx) /* Done. */ OMP_CLAUSE_MAP_DECL_MAKE_ADDRESSABLE (c) = 0; } + if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP + && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER) + { + /* Ignore GOMP_MAP_POINTER kind for arrays in regions that are + not offloaded; there is nothing to map for those. */ + if (!is_gimple_omp_offloaded (ctx->stmt) + && !POINTER_TYPE_P (TREE_TYPE (decl)) + && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)) + break; + } + if (!flex_array_ptr) + flex_array_ptr = lang_hooks.decls.omp_deep_mapping_p (ctx->stmt, c); + /* Global variables with "omp declare target" attribute don't need to be copied, the receiver side will use them directly. However, global variables with "omp declare target link" @@ -1651,18 +1664,6 @@ scan_sharing_clauses (tree clauses, omp_context *ctx) && !lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (decl))) break; - if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP - && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER) - { - /* Ignore GOMP_MAP_POINTER kind for arrays in regions that are - not offloaded; there is nothing to map for those. */ - if (!is_gimple_omp_offloaded (ctx->stmt) - && !POINTER_TYPE_P (TREE_TYPE (decl)) - && !OMP_CLAUSE_MAP_ZERO_BIAS_ARRAY_SECTION (c)) - break; - } - if (!flex_array_ptr) - flex_array_ptr = lang_hooks.decls.omp_deep_mapping_p (ctx->stmt, c); if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP && DECL_P (decl) && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH diff --git a/libgomp/testsuite/libgomp.fortran/map-static-alloc-comp-1.f90 b/libgomp/testsuite/libgomp.fortran/map-static-alloc-comp-1.f90 new file mode 100644 index 000000000000..cd0140f14e78 --- /dev/null +++ b/libgomp/testsuite/libgomp.fortran/map-static-alloc-comp-1.f90 @@ -0,0 +1,43 @@ +! Check that global variables with allocatable components +! are properly handled. +! +! PR fortran/128969 +! +! +! Test case from https://github.com/sbryngelson/offtarget +! +! MIT License +! Copyright (c) 2026 Spencer Bryngelson +! + +module dt_m + implicit none + type :: holder + integer, allocatable :: v1(:) + end type holder + type(holder) :: gq + !$omp declare target(gq) +end module dt_m + +program declared_alloc_component + use omp_lib, only: omp_is_initial_device + use dt_m + implicit none + integer :: out(64), i, host_flag + allocate(gq%v1(64)) + gq%v1 = [(3*i, i = 1, 64)] + + out = 0 + host_flag = 1 + !$omp target teams distribute parallel do map(tofrom: out, host_flag) map(always, tofrom: gq) + do i = 1, 64 + if (i == 1) host_flag = merge(1, 0, omp_is_initial_device()) + out(i) = gq%v1(i) + 1 + end do + + ! print '(a,*(i0,","))', 'out= ', out + ! print '(a,i0)', 'sum= ', sum(out) + print '(a,i0,a,a)', 'RESULT ran_on_host=', host_flag, ' check=', merge('pass', 'fail', sum(out) == 3*64*65/2 + 64) + + if (sum(out) /= 3*64*65/2 + 64) stop 1 +end program declared_alloc_component
