See attached patch. gfortran maps an assumed-shape dummy from its first
element for stride times extent of its last dimension. That is the
array's size only when it is contiguous: for a transposed actual
argument the map is too short, for others it runs past the last
element, and with a negative stride it misses everything below the
first element.

The fix maps the dummy from its lowest to its highest addressed
element. It relies on the stride and bounds variables that the
descriptor gives a non-contiguous dummy; constant strides mean the
packed array, which keeps the old map.

Start at dummy_array_span in trans-openmp.cc, which computes the offset
of the lowest element and the length. Then its three users: implicit
maps in gfc_omp_finish_clause, and map clauses and to/from of target
update in gfc_trans_omp_clauses. dummy_bias there makes all clauses on
one dummy share a single offset, because separate offset temporaries
made libgomp see a cycle of base pointers. OpenACC is left alone.

The three gomp scan-tree-dump updates follow from the new start and
length; the rest is the new libgomp test. The bug showed up with the
DO CONCURRENT to OpenMP series, but the patch does not depend on it and
applies to master on its own.

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

OK for mainline?

Regards,

Jerry

---

fortran: [PR128977] Wrong OpenMP map of a strided assumed-shape dummy

An assumed-shape dummy was mapped from its first element for stride
times extent of its last dimension, which is its size only when it is
contiguous.  For a transposed or otherwise strided actual argument the
map was too short or ran past the last element, and with a negative
stride it missed the elements below the first.  The patch maps the
dummy from its lowest to its highest addressed element instead, for
implicit maps, map clauses and target update.

Assisted-by: Claude Opus 5.5

        PR fortran/128977

gcc/fortran/ChangeLog:

        * trans-openmp.cc (dummy_array_span): New function.
        (gfc_omp_finish_clause): Use it to map an assumed-shape dummy
        from its lowest to its highest addressed element.
        (gfc_trans_omp_clauses): Likewise for map, to and from clauses.

gcc/testsuite/ChangeLog:

        * gfortran.dg/gomp/map-multi-1.f90: Expect a computed start and
        length for the assumed-shape dummy.
        * gfortran.dg/gomp/map-multi-2.f90: Expect a computed bias.
        * gfortran.dg/gomp/pr78260-2.f90: Expect an explicit length for
        the array result.

libgomp/ChangeLog:

        * testsuite/libgomp.fortran/map-assumed-shape-1.f90: New test.
>From 31f9b93485f820594efa5d3c661b544a5ceb480c Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Mon, 23 Nov 2026 15:25:42 -0800
Subject: [PATCH] fortran: [PR128977] Wrong OpenMP map of a strided
 assumed-shape dummy

An assumed-shape dummy was mapped from its first element for stride
times extent of its last dimension, which is its size only when it is
contiguous.  For a transposed or otherwise strided actual argument the
map was too short or ran past the last element, and with a negative
stride it missed the elements below the first.  The patch maps the
dummy from its lowest to its highest addressed element instead, for
implicit maps, map clauses and target update.

Assisted-by: Claude Opus 5.5

	PR fortran/128977

gcc/fortran/ChangeLog:

	* trans-openmp.cc (dummy_array_span): New function.
	(gfc_omp_finish_clause): Use it to map an assumed-shape dummy
	from its lowest to its highest addressed element.
	(gfc_trans_omp_clauses): Likewise for map, to and from clauses.

gcc/testsuite/ChangeLog:

	* gfortran.dg/gomp/map-multi-1.f90: Expect a computed start and
	length for the assumed-shape dummy.
	* gfortran.dg/gomp/map-multi-2.f90: Expect a computed bias.
	* gfortran.dg/gomp/pr78260-2.f90: Expect an explicit length for
	the array result.

libgomp/ChangeLog:

	* testsuite/libgomp.fortran/map-assumed-shape-1.f90: New test.
---
 gcc/fortran/trans-openmp.cc                   | 139 +++++++++++++++++-
 .../gfortran.dg/gomp/map-multi-1.f90          |   2 +-
 .../gfortran.dg/gomp/map-multi-2.f90          |   4 +-
 gcc/testsuite/gfortran.dg/gomp/pr78260-2.f90  |   4 +-
 .../libgomp.fortran/map-assumed-shape-1.f90   |  96 ++++++++++++
 5 files changed, 238 insertions(+), 7 deletions(-)
 create mode 100644 libgomp/testsuite/libgomp.fortran/map-assumed-shape-1.f90

diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 13b17e573db..ea5695bbc2a 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -1665,6 +1665,74 @@ gfc_build_cond_assign_expr (stmtblock_t *block, tree cond_val,
   return val;
 }
 
+/* If PTR is the data pointer of an assumed-shape dummy, whose elements need
+   be neither contiguous nor above its first element, return the number of
+   bytes from its lowest addressed element to the end of its highest, and
+   set *LO to the offset in bytes of the lowest from PTR.  Both are zero if
+   the array has no elements or PRESENT is false.  Otherwise, or if the
+   array is known to be contiguous, return NULL_TREE.  */
+
+static tree
+dummy_array_span (tree ptr, tree present, tree *lo)
+{
+  if (!DECL_P (ptr)
+      || !DECL_LANG_SPECIFIC (ptr)
+      || !GFC_DECL_SAVED_DESCRIPTOR (ptr)
+      || !POINTER_TYPE_P (TREE_TYPE (ptr))
+      || !GFC_ARRAY_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
+    return NULL_TREE;
+  tree type = TREE_TYPE (TREE_TYPE (ptr));
+  tree zero = gfc_index_zero_node;
+  tree lo_idx = zero, hi_idx = zero;
+  tree empty = logical_false_node;
+  bool contiguous = true;
+  for (int n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
+    {
+      tree lb = GFC_TYPE_ARRAY_LBOUND (type, n);
+      tree ub = GFC_TYPE_ARRAY_UBOUND (type, n);
+      tree stride = GFC_TYPE_ARRAY_STRIDE (type, n);
+      if (!lb || !ub || !stride)
+	return NULL_TREE;
+      /* Constant strides are those of the packed array.  */
+      if (!INTEGER_CST_P (stride))
+	contiguous = false;
+      tree d = fold_build2 (MINUS_EXPR, gfc_array_index_type,
+			    fold_convert (gfc_array_index_type, ub),
+			    fold_convert (gfc_array_index_type, lb));
+      empty = fold_build2 (TRUTH_OR_EXPR, logical_type_node, empty,
+			   fold_build2 (LT_EXPR, logical_type_node, d, zero));
+      d = fold_build2 (MULT_EXPR, gfc_array_index_type, d,
+		       fold_convert (gfc_array_index_type, stride));
+      lo_idx = fold_build2 (PLUS_EXPR, gfc_array_index_type, lo_idx,
+			    fold_build2 (MIN_EXPR, gfc_array_index_type,
+					 unshare_expr (d), zero));
+      hi_idx = fold_build2 (PLUS_EXPR, gfc_array_index_type, hi_idx,
+			    fold_build2 (MAX_EXPR, gfc_array_index_type,
+					 d, zero));
+    }
+  if (contiguous)
+    return NULL_TREE;
+  tree elemsz = fold_convert (gfc_array_index_type,
+			      TYPE_SIZE_UNIT (TREE_TYPE (type)));
+  tree len = fold_build2 (MINUS_EXPR, gfc_array_index_type, hi_idx, lo_idx);
+  len = fold_build2 (PLUS_EXPR, gfc_array_index_type, len,
+		     gfc_index_one_node);
+  len = fold_build2 (MULT_EXPR, gfc_array_index_type, len, elemsz);
+  len = fold_build3 (COND_EXPR, gfc_array_index_type, empty, zero, len);
+  *lo = fold_build3 (COND_EXPR, gfc_array_index_type, unshare_expr (empty),
+		     zero, fold_build2 (MULT_EXPR, gfc_array_index_type,
+					unshare_expr (lo_idx),
+					unshare_expr (elemsz)));
+  if (present)
+    {
+      len = fold_build3 (COND_EXPR, gfc_array_index_type, present, len, zero);
+      *lo = fold_build3 (COND_EXPR, gfc_array_index_type,
+			 unshare_expr (present), *lo, zero);
+    }
+  *lo = fold_convert (sizetype, *lo);
+  return fold_convert (sizetype, len);
+}
+
 void
 gfc_omp_finish_clause (tree c, gimple_seq *pre_p, bool openacc)
 {
@@ -1883,7 +1951,23 @@ gfc_omp_finish_clause (tree c, gimple_seq *pre_p, bool openacc)
     }
   else
     {
-      if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
+      tree span, lo;
+      if (!openacc
+	  && OMP_CLAUSE_SIZE (c) == NULL_TREE
+	  && c4
+	  && (span = dummy_array_span (orig_decl, present, &lo)) != NULL_TREE)
+	{
+	  tree size = create_tmp_var (sizetype);
+	  tree bias = create_tmp_var (sizetype);
+	  gimplify_assign (size, span, pre_p);
+	  gimplify_assign (bias, lo, pre_p);
+	  OMP_CLAUSE_DECL (c)
+	    = build_fold_indirect_ref (fold_build_pointer_plus (orig_decl,
+								bias));
+	  OMP_CLAUSE_SIZE (c) = size;
+	  OMP_CLAUSE_SIZE (c4) = bias;
+	}
+      else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
 	{
 	  if (DECL_P (decl))
 	    OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
@@ -3958,6 +4042,9 @@ gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
   gfc_omp_namelist *prev = NULL;
   gfc_se se;
   vec<gfc_symbol *> descriptors = vNULL;
+  /* Map offset of each assumed-shape dummy, shared by its clauses so that
+     they map the same storage.  */
+  hash_map<tree, tree> dummy_bias;
 
   if (clauses == NULL)
     return NULL_TREE;
@@ -4951,7 +5038,32 @@ gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
 		      OMP_CLAUSE_DECL (node) = build_fold_indirect_ref (tmp);
 		    }
 		  else
-		    OMP_CLAUSE_DECL (node) = decl;
+		    {
+		      OMP_CLAUSE_DECL (node) = decl;
+		      tree ptr = (INDIRECT_REF_P (decl)
+				  ? TREE_OPERAND (decl, 0) : NULL_TREE);
+		      tree span, lo;
+		      if (!openacc
+			  && node4
+			  && ptr
+			  && OMP_CLAUSE_DECL (node4) == ptr
+			  && (span = dummy_array_span (ptr, present, &lo)))
+			{
+			  tree *cached = iterator ? NULL : dummy_bias.get (ptr);
+			  tree bias = (cached ? *cached
+				       : gfc_evaluate_now (lo, &iter_block));
+			  if (!iterator)
+			    dummy_bias.put (ptr, bias);
+			  if (!integer_zerop (bias))
+			    OMP_CLAUSE_DECL (node)
+			      = build_fold_indirect_ref
+				  (fold_build_pointer_plus (ptr, bias));
+			  OMP_CLAUSE_SIZE (node)
+			    = gfc_evaluate_now (span, &iter_block);
+			  if (OMP_CLAUSE_MAP_KIND (node4) == GOMP_MAP_POINTER)
+			    OMP_CLAUSE_SIZE (node4) = bias;
+			}
+		    }
 
 		  if (!n->sym->attr.dimension
 		      && n->sym->ts.type == BT_CHARACTER
@@ -5460,10 +5572,33 @@ gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
 		    }
 		  else
 		    {
+		      tree ptr = (INDIRECT_REF_P (decl)
+				  ? TREE_OPERAND (decl, 0) : NULL_TREE);
+		      tree span, lo;
 		      OMP_CLAUSE_DECL (node) = decl;
 		      if (gfc_omp_is_allocatable_or_ptr (decl))
 			OMP_CLAUSE_SIZE (node)
 				= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
+		      else if (!openacc
+			       && ptr
+			       && DECL_P (ptr)
+			       && (span = dummy_array_span
+				    (ptr, gfc_omp_check_optional_argument (ptr,
+									  true),
+				     &lo)))
+			{
+			  tree *cached = iterator ? NULL : dummy_bias.get (ptr);
+			  tree bias = (cached ? *cached
+				       : gfc_evaluate_now (lo, &iter_block));
+			  if (!iterator)
+			    dummy_bias.put (ptr, bias);
+			  if (!integer_zerop (bias))
+			    OMP_CLAUSE_DECL (node)
+			      = build_fold_indirect_ref
+				  (fold_build_pointer_plus (ptr, bias));
+			  OMP_CLAUSE_SIZE (node)
+			    = gfc_evaluate_now (span, &iter_block);
+			}
 		    }
 		}
 	      else
diff --git a/gcc/testsuite/gfortran.dg/gomp/map-multi-1.f90 b/gcc/testsuite/gfortran.dg/gomp/map-multi-1.f90
index c12b6d431cf..be19e6a2914 100644
--- a/gcc/testsuite/gfortran.dg/gomp/map-multi-1.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/map-multi-1.f90
@@ -47,4 +47,4 @@ subroutine g (a)
 
 end subroutine
 
-! { dg-final { scan-tree-dump-times {map\(tofrom:\*a\.0\) map\(alloc:a\.0 \[pointer assign, bias: 0\]\)} 2 "original" } }
+! { dg-final { scan-tree-dump-times {map\(tofrom:\*\(a\.0 \+ D\.[0-9]+\) \[len: D\.[0-9]+\]\) map\(alloc:a\.0 \[pointer assign, bias: D\.[0-9]+\]\)} 2 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/map-multi-2.f90 b/gcc/testsuite/gfortran.dg/gomp/map-multi-2.f90
index fbd4dcbb256..647b7c80ca1 100644
--- a/gcc/testsuite/gfortran.dg/gomp/map-multi-2.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/map-multi-2.f90
@@ -54,7 +54,7 @@ subroutine g (a)
 
 end subroutine
 
-! { dg-final { scan-tree-dump-times {map\(alloc:a\.0 \[pointer assign, bias: 0\]\)} 4 "original" } }
-! { dg-final { scan-tree-dump-times {map\(alloc:a\.0 \[pointer assign, bias: 0\]\)} 2 "gimple" } }
+! { dg-final { scan-tree-dump-times {map\(alloc:a\.0 \[pointer assign, bias: D\.[0-9]+\]\)} 4 "original" } }
+! { dg-final { scan-tree-dump-times {map\(alloc:a\.0 \[pointer assign, bias: D\.[0-9]+\]\)} 2 "gimple" } }
 
 
diff --git a/gcc/testsuite/gfortran.dg/gomp/pr78260-2.f90 b/gcc/testsuite/gfortran.dg/gomp/pr78260-2.f90
index cd771b33a25..c727d7697af 100644
--- a/gcc/testsuite/gfortran.dg/gomp/pr78260-2.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/pr78260-2.f90
@@ -54,8 +54,8 @@ end module m
 ! { dg-final { scan-tree-dump-times "#pragma omp target update to\\(\\*\\(integer\\(kind=4\\)\\\[0:\\\] \\* restrict\\) arr.data \\\[len: D.\[0-9\]+ \\* 4\\\]\\)" 1 "original" } }
 ! { dg-final { scan-tree-dump-times "#pragma omp target data map\\(tofrom:\\*\\(integer\\(kind=4\\)\\\[0:\\\] \\* restrict\\) __result->data \\\[len: D.\[0-9\]+\\\]\\) map\\(to:\\*__result \\\[pointer set, len: ..\\\]\\) map\\(alloc:\\(integer\\(kind=4\\)\\\[0:\\\] \\* restrict\\) __result->data \\\[pointer assign, bias: 0\\\]\\) map\\(alloc:__result \\\[pointer assign, bias: 0\\\]\\)" 1 "original" } }
 ! { dg-final { scan-tree-dump-times "#pragma omp target update to\\(\\*\\(integer\\(kind=4\\)\\\[0:\\\] \\* restrict\\) __result->data \\\[len: D.\[0-9\]+ \\* 4\\\]\\)" 1 "original" } }
-! { dg-final { scan-tree-dump-times "#pragma omp target data map\\(tofrom:\\*__result.0\\) map\\(alloc:__result.0 \\\[pointer assign, bias: 0\\\]\\)" 2 "original" } }
-! { dg-final { scan-tree-dump-times "#pragma omp target update to\\(\\*__result.0\\)" 2 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp target data map\\(tofrom:\\*__result.0 \\\[len: 4\\\]\\) map\\(alloc:__result.0 \\\[pointer assign, bias: 0\\\]\\)" 2 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp target update to\\(\\*__result.0 \\\[len: 4\\\]\\)" 2 "original" } }
 ! { dg-final { scan-tree-dump-times "#pragma omp target data map\\(tofrom:__result_f1\\)" 1 "original" } }
 ! { dg-final { scan-tree-dump-times "#pragma omp target update to\\(__result_f1\\)" 1 "original" } }
 
diff --git a/libgomp/testsuite/libgomp.fortran/map-assumed-shape-1.f90 b/libgomp/testsuite/libgomp.fortran/map-assumed-shape-1.f90
new file mode 100644
index 00000000000..7877c1ec39d
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/map-assumed-shape-1.f90
@@ -0,0 +1,96 @@
+! { dg-do run }
+!
+! PR 128977
+
+module m
+  implicit none
+contains
+  subroutine scale (k, a, d, ad)
+    integer, intent(in) :: k
+    double precision, intent(in) :: a(:,:), d(:)
+    double precision, intent(out) :: ad(:,:)
+    integer :: j
+    select case (k)
+    case (1)
+      !$omp target teams distribute parallel do
+      do j = 1, size (a, 2)
+        ad(:,j) = a(:,j) * d(j)
+      end do
+    case (2)
+      !$omp target teams distribute parallel do map(to: a, d) map(from: ad)
+      do j = 1, size (a, 2)
+        ad(:,j) = a(:,j) * d(j)
+      end do
+    case (3)
+      !$omp target enter data map(to: a, d) map(alloc: ad)
+      !$omp target teams distribute parallel do
+      do j = 1, size (a, 2)
+        ad(:,j) = a(:,j) * d(j)
+      end do
+      !$omp target exit data map(from: ad) map(release: a, d)
+    case (4)
+      ad = 0d0
+      !$omp target data map(to: d) map(alloc: a) map(tofrom: ad)
+        !$omp target update to(a)
+        !$omp target teams distribute parallel do
+        do j = 1, size (a, 2)
+          ad(:,j) = a(:,j) * d(j)
+        end do
+        !$omp target update from(ad)
+      !$omp end target data
+    end select
+  end subroutine scale
+
+  subroutine scale_opt (a, d, ad)
+    double precision, intent(in) :: a(:,:), d(:)
+    double precision, intent(out), optional :: ad(:,:)
+    integer :: j
+    !$omp target teams distribute parallel do
+    do j = 1, size (a, 2)
+      if (present (ad)) ad(:,j) = a(:,j) * d(j)
+    end do
+  end subroutine scale_opt
+end module m
+
+program p
+  use m
+  implicit none
+  double precision :: g(3,4), t(4,3), x(4,5), r(4,3), w(8,3), d(3)
+  integer :: i, j, k
+
+  g = reshape ([(dble (i), i = 1, 12)], shape (g))
+  x = reshape ([(dble (i), i = 1, 20)], shape (x))
+  t = transpose (g)
+  d = [1d0, 10d0, 100d0]
+
+  do k = 1, 4
+    call scale (k, transpose (g), d, r)
+    call check (t, 10 * k + 1)
+    call scale (k, x(:,1:5:2), d, r)
+    call check (x(:,1:5:2), 10 * k + 2)
+    call scale (k, x(4:1:-1,2:4), d, r)
+    call check (x(4:1:-1,2:4), 10 * k + 3)
+    call scale (k, x(:,5:1:-2), d, r)
+    call check (x(:,5:1:-2), 10 * k + 4)
+    w = -1d0
+    call scale (k, t, d, w(8:1:-2,:))
+    r = w(8:1:-2,:)
+    call check (t, 10 * k + 5)
+    if ((k == 1 .or. k == 4) .and. any (w(7:1:-2,:) /= -1d0)) stop 10 * k + 6
+  end do
+
+  r = -1d0
+  call scale_opt (transpose (g), d, r)
+  call check (t, 7)
+  call scale_opt (transpose (g), d)
+
+contains
+
+  subroutine check (a, n)
+    double precision, intent(in) :: a(:,:)
+    integer, intent(in) :: n
+    do j = 1, 3
+      if (any (r(:,j) /= a(:,j) * d(j))) stop n
+    end do
+  end subroutine check
+end program p
-- 
2.56.1

Reply via email to