See attached patch. Tobias confirmed the problem he reported in the PR is fixed by this.

Regression tested on x86_64. The changelog summarizes quite well.

As noted in my email about 126964, this patch is needed to avoid the runtime checking in my patch previously submitted for it. I had this sitting in my worktree and got sidetracked chasing 126964.

OK for mainline?

Best regards,

Jerry

---

[PATCH] fortran: [PR126950] Load the span of a span addressed dummy
 on entry

A TARGET assumed-shape dummy is addressed through the span of its
descriptor since r17-3342.  Its bounds and data pointer are loaded into
local variables on entry, but the span was reloaded from the descriptor
at each element reference.  Inside an outlined region, such as an OpenMP
target region, only those local variables are available; the descriptor
is not mapped to the device, so the reload dereferenced a host pointer
there and the region used a garbage span.

Load the span on entry as well, alongside the bounds, and use that
variable for element addressing.  The variable is created with the
dummy's declaration because the procedure body is translated before
gfc_trans_dummy_array_bias emits the load.  The load is placed in the
init block, which is already guarded by the argument being present, so
an absent optional dummy is not dereferenced.

Assisted-by: Claude Opus 5

        PR fortran/126950

gcc/fortran/ChangeLog:

        * trans.h (struct lang_decl): Add span.
        (GFC_DECL_SPAN, GFC_DECL_GET_SPAN): New macros.
        * trans-decl.cc (gfc_build_dummy_array_decl): Create the variable
        holding the span of a span addressed dummy.
        * trans-array.cc (gfc_trans_dummy_array_bias): Load it from the
        descriptor on entry.
        (gfc_get_array_span): Use it.
        * trans.cc (get_array_span): Likewise.

gcc/testsuite/ChangeLog:

        * gfortran.dg/c_loc_test_22.f90: Update dump patterns for the
        span being loaded on entry.
        * gfortran.dg/gomp/target-span-1.f90: New test.
---
From a80cd262ae4c93c278501dd63588300581cbe356 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Wed, 19 Aug 2026 10:33:56 -0700
Subject: [PATCH] fortran: [PR126950] Load the span of a span addressed dummy
 on entry

A TARGET assumed-shape dummy is addressed through the span of its
descriptor since r17-3342.  Its bounds and data pointer are loaded into
local variables on entry, but the span was reloaded from the descriptor
at each element reference.  Inside an outlined region, such as an OpenMP
target region, only those local variables are available; the descriptor
is not mapped to the device, so the reload dereferenced a host pointer
there and the region used a garbage span.

Load the span on entry as well, alongside the bounds, and use that
variable for element addressing.  The variable is created with the
dummy's declaration because the procedure body is translated before
gfc_trans_dummy_array_bias emits the load.  The load is placed in the
init block, which is already guarded by the argument being present, so
an absent optional dummy is not dereferenced.

Assisted-by: Claude Opus 5

	PR fortran/126950

gcc/fortran/ChangeLog:

	* trans.h (struct lang_decl): Add span.
	(GFC_DECL_SPAN, GFC_DECL_GET_SPAN): New macros.
	* trans-decl.cc (gfc_build_dummy_array_decl): Create the variable
	holding the span of a span addressed dummy.
	* trans-array.cc (gfc_trans_dummy_array_bias): Load it from the
	descriptor on entry.
	(gfc_get_array_span): Use it.
	* trans.cc (get_array_span): Likewise.

gcc/testsuite/ChangeLog:

	* gfortran.dg/c_loc_test_22.f90: Update dump patterns for the
	span being loaded on entry.
	* gfortran.dg/gomp/target-span-1.f90: New test.
---
 gcc/fortran/trans-array.cc                    | 20 +++++++++----
 gcc/fortran/trans-decl.cc                     |  9 ++++--
 gcc/fortran/trans.cc                          |  3 ++
 gcc/fortran/trans.h                           |  7 +++++
 gcc/testsuite/gfortran.dg/c_loc_test_22.f90   |  8 +++--
 .../gfortran.dg/gomp/target-span-1.f90        | 30 +++++++++++++++++++
 6 files changed, 67 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/gomp/target-span-1.f90

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 4c9f0212101..7ac63ce6258 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -578,11 +578,14 @@ gfc_get_array_span (tree desc, gfc_expr *expr)
   gfc_symbol *sym = (expr && expr->expr_type == EXPR_VARIABLE) ?
 		    expr->symtree->n.sym : NULL;
 
-  if (span_addressed_array (desc)
-      || (get_CFI_desc (NULL, expr, &desc, NULL)
-	  && (POINTER_TYPE_P (TREE_TYPE (desc))
-	      ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
-	      : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
+  if (tree span = GFC_DECL_GET_SPAN (desc))
+    /* A span addressed dummy loaded its span on entry.  */
+    tmp = span;
+  else if (span_addressed_array (desc)
+	   || (get_CFI_desc (NULL, expr, &desc, NULL)
+	       && (POINTER_TYPE_P (TREE_TYPE (desc))
+		   ? GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc)))
+		   : GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))))
     /* This will have the span field set.  */
     tmp = gfc_conv_descriptor_span_get (gfc_get_span_descriptor (desc));
   else if (expr->ts.type == BT_ASSUMED)
@@ -7520,6 +7523,13 @@ gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc,
   if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
     gfc_add_modify (&init, GFC_TYPE_ARRAY_OFFSET (type), offset);
 
+  /* Load the span once here, like the bounds above, so that element
+     addressing does not reload it from the descriptor.  The descriptor
+     itself is not available in an outlined region, such as an OpenMP
+     target region, whereas this local variable is.  */
+  if (tree span = GFC_DECL_GET_SPAN (tmpdesc))
+    gfc_add_modify (&init, span, gfc_conv_descriptor_span_get (dumdesc));
+
   gfc_trans_vla_type_sizes (sym, &init);
 
   stmtInit = gfc_finish_block (&init);
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index dae1edda7d6..feddb1da854 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -1407,9 +1407,14 @@ gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
   GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
 
   /* The elements of the actual argument can be spaced by more than the
-     element size, so the span of the descriptor is used to address them.  */
+     element size, so the span of the descriptor is used to address them.
+     Create the variable that holds it here, since the body is translated
+     before gfc_trans_dummy_array_bias loads it from the descriptor.  */
   if (gfc_is_span_addressed_dummy (sym) && packed == PACKED_NO)
-    GFC_DECL_PTR_ARRAY_P (decl) = 1;
+    {
+      GFC_DECL_PTR_ARRAY_P (decl) = 1;
+      GFC_DECL_SPAN (decl) = gfc_create_var (gfc_array_index_type, "span");
+    }
 
   if (sym->ns->proc_name->backend_decl == current_function_decl
       || sym->attr.contained)
diff --git a/gcc/fortran/trans.cc b/gcc/fortran/trans.cc
index c2ad65c9a74..b98fe0c680f 100644
--- a/gcc/fortran/trans.cc
+++ b/gcc/fortran/trans.cc
@@ -471,6 +471,9 @@ get_array_span (tree type, tree decl)
 	     to be multiplied with the size.  */
 	  span = gfc_resize_class_size_with_len (NULL, decl, span);
 	}
+      else if (tree cached = GFC_DECL_GET_SPAN (decl))
+	/* A span addressed dummy loaded its span on entry.  */
+	span = cached;
       else if (GFC_DECL_PTR_ARRAY_P (decl))
 	span = gfc_conv_descriptor_span_get (gfc_get_span_descriptor (decl));
       else
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index e9e310974ee..e5162d5515c 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -1055,6 +1055,8 @@ struct GTY(())	lang_type	 {
 struct GTY(()) lang_decl {
   /* Dummy variables.  */
   tree saved_descriptor;
+  /* Element spacing of a span addressed dummy, loaded once on entry.  */
+  tree span;
   /* Assigned integer nodes.  Stringlength is the IO format string's length.
      Addr is the address of the string or the target label. Stringlength is
      initialized to -2 and assigned to -1 when addr is assigned to the
@@ -1076,6 +1078,11 @@ struct GTY(()) lang_decl {
 #define GFC_DECL_CAF_OFFSET(node) DECL_LANG_SPECIFIC(node)->caf_offset
 #define GFC_DECL_SAVED_DESCRIPTOR(node) \
   (DECL_LANG_SPECIFIC(node)->saved_descriptor)
+#define GFC_DECL_SPAN(node) (DECL_LANG_SPECIFIC(node)->span)
+/* Return the cached span of a span addressed dummy, or NULL_TREE.  */
+#define GFC_DECL_GET_SPAN(node) \
+  (DECL_P (node) && DECL_LANG_SPECIFIC (node) \
+   ? GFC_DECL_SPAN (node) : NULL_TREE)
 #define GFC_DECL_SCALAR_ALLOCATABLE(node) \
   (DECL_LANG_SPECIFIC (node)->scalar_allocatable)
 #define GFC_DECL_SCALAR_POINTER(node) \
diff --git a/gcc/testsuite/gfortran.dg/c_loc_test_22.f90 b/gcc/testsuite/gfortran.dg/c_loc_test_22.f90
index 91547e8e337..2360c33913b 100644
--- a/gcc/testsuite/gfortran.dg/c_loc_test_22.f90
+++ b/gcc/testsuite/gfortran.dg/c_loc_test_22.f90
@@ -17,9 +17,11 @@ end
 ! { dg-final { scan-tree-dump-not " _gfortran_internal_pack" "original" } }
 ! { dg-final { scan-tree-dump-times "parm.\[0-9\]+.data = \\(void .\\) &\\(.xxx.\[0-9\]+\\)\\\[0\\\];" 1 "original" } }
 ! { dg-final { scan-tree-dump-times "parm.\[0-9\]+.data = \\(void .\\) &\\(.xxx.\[0-9\]+\\)\\\[D.\[0-9\]+ \\* 4\\\];" 1 "original" } }
-! A TARGET assumed-shape dummy is addressed with the descriptor's runtime
-! span, so the element offset is span-scaled instead of a constant 16.
+! A TARGET assumed-shape dummy is addressed with the runtime span that is
+! loaded from the descriptor on entry, so the element offset is span-scaled
+! instead of a constant 16.
 ! { dg-final { scan-tree-dump-times "parm.\[0-9\]+.data = \\(void .\\) &\\(.yyy.\[0-9\]+\\)\\\[0\\\];" 1 "original" } }
-! { dg-final { scan-tree-dump-times "parm.\[0-9\]+.data = \\(void .\\) yyy.\[0-9\]+ \\+ \\(sizetype\\) \\(\\(yyy->span \\* D.\[0-9\]+\\) \\* 4\\);" 1 "original" } }
+! { dg-final { scan-tree-dump-times "span.\[0-9\]+ = yyy->span;" 1 "original" } }
+! { dg-final { scan-tree-dump-times "parm.\[0-9\]+.data = \\(void .\\) yyy.\[0-9\]+ \\+ \\(sizetype\\) \\(\\(D.\[0-9\]+ \\* span.\[0-9\]+\\) \\* 4\\);" 1 "original" } }
 
 ! { dg-final { scan-tree-dump-times "D.\[0-9\]+ = parm.\[0-9\]+.data;\[^;]+ptr\[1-4\] = D.\[0-9\]+;" 4 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/target-span-1.f90 b/gcc/testsuite/gfortran.dg/gomp/target-span-1.f90
new file mode 100644
index 00000000000..30ba7e1862e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/target-span-1.f90
@@ -0,0 +1,30 @@
+! { dg-do compile }
+! { dg-additional-options "-fdump-tree-original" }
+!
+! PR fortran/126950
+!
+! A TARGET assumed-shape dummy is addressed through the span of its
+! descriptor.  The descriptor is not mapped to the device, so the span has
+! to be loaded into a local variable on entry and that variable used inside
+! the target region, rather than the region dereferencing the descriptor.
+
+module m
+  use iso_c_binding
+contains
+  subroutine tgt (t)
+    real(c_double), target :: t(:)
+    !$omp target has_device_addr(t)
+    call inner (t(1))
+    !$omp end target
+  end subroutine tgt
+
+  subroutine inner (a)
+    real(c_double) :: a
+  end subroutine inner
+end module m
+
+! The span is loaded from the descriptor once, on entry.
+! { dg-final { scan-tree-dump-times "span\.\[0-9\]+ = t->span;" 1 "original" } }
+! The element reference uses that variable, not the descriptor.
+! { dg-final { scan-tree-dump "t\.\[0-9\]+ \\+ \\(sizetype\\) \\(\\(offset\.\[0-9\]+ \\+ \[^)\]*stride\.\[0-9\]+\[^)\]*\\) \\* span\.\[0-9\]+\\)" "original" } }
+! { dg-final { scan-tree-dump-not "\\* t->span" "original" } }
-- 
2.55.0

Reply via email to