[gcc r14-12485] fortran: Fix OpenMP iterator depend lowering for component arrays [PR102459]

Paul Thomas pault@gcc.gnu.org
Sun Mar 29 21:03:38 GMT 2026


https://gcc.gnu.org/g:e90b598a52e96d9b9812d984ada141ff18369c1c

commit r14-12485-ge90b598a52e96d9b9812d984ada141ff18369c1c
Author: Christopher Albert <albert@tugraz.at>
Date:   Tue Mar 10 18:46:54 2026 +0100

    fortran: Fix OpenMP iterator depend lowering for component arrays [PR102459]
    
    When lowering an OpenMP depend clause with an iterator expression such as
    x(j)%a, the front end currently looks only at the first REF_ARRAY to
    decide between scalar-reference lowering and array-descriptor lowering.
    For x(j)%a that first ref is the scalar base element x(j), but the full
    expression is still the rank-1 component array a.
    
    As a result, the code calls gfc_conv_expr_reference on an array-valued
    expression, which later reaches gfc_conv_scalarized_array_ref without a
    scalarizer state and ICEs.
    
    Fix this by choosing the lowering path from the rank of the full
    expression.  Rank-zero expressions still use gfc_conv_expr_reference,
    while array-valued expressions are lowered through gfc_conv_expr_descriptor.
    Apply the same adjustment to the analogous depobj helper and add a
    regression test for task depend clauses covering both x(j)%a and the
    scalar control case x(j)%a(1).
    
    gcc/fortran/ChangeLog:
    
            PR fortran/102459
            * trans-openmp.cc (gfc_trans_omp_clauses): Choose the scalar
            reference path from the full expression rank rather than the first
            array reference.
            (gfc_trans_omp_depobj): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/102459
            * gfortran.dg/pr102459.f90: New test.
    
    Signed-off-by: Christopher Albert <albert@tugraz.at>
    (cherry picked from commit d2ab04fbba7b97d17e4f9e0885d71a4e1faafc96)

Diff:
---
 gcc/fortran/trans-openmp.cc            |  8 ++++++--
 gcc/testsuite/gfortran.dg/pr102459.f90 | 15 +++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index f867e2240bf8..122981f8c361 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -3074,7 +3074,11 @@ gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
 		{
 		  tree ptr;
 		  gfc_init_se (&se, NULL);
-		  if (n->expr->ref->u.ar.type == AR_ELEMENT)
+		  /* The first ref can be an element selection on the base
+		     object while the full expression still denotes an array,
+		     e.g. x(j)%a.  Pick the lowering path from the overall
+		     expression rank, not from the first REF_ARRAY.  */
+		  if (n->expr->rank == 0)
 		    {
 		      gfc_conv_expr_reference (&se, n->expr);
 		      ptr = se.expr;
@@ -6243,7 +6247,7 @@ gfc_trans_omp_depobj (gfc_code *code)
       else if (n->expr && n->expr->ref->u.ar.type != AR_FULL)
 	{
 	  gfc_init_se (&se, NULL);
-	  if (n->expr->ref->u.ar.type == AR_ELEMENT)
+	  if (n->expr->rank == 0)
 	    {
 	      gfc_conv_expr_reference (&se, n->expr);
 	      var = se.expr;
diff --git a/gcc/testsuite/gfortran.dg/pr102459.f90 b/gcc/testsuite/gfortran.dg/pr102459.f90
new file mode 100644
index 000000000000..c6b6c5eed634
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr102459.f90
@@ -0,0 +1,15 @@
+! { dg-do compile }
+! { dg-additional-options "-fopenmp" }
+
+program p
+  type t
+    integer :: a(2)
+  end type
+  type(t) :: x(8)
+
+  !$omp task depend (iterator(j=1:8), out:x(j)%a)
+  !$omp end task
+
+  !$omp task depend (iterator(j=1:8), out:x(j)%a(1))
+  !$omp end task
+end


More information about the Gcc-cvs mailing list