https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128890

            Bug ID: 128890
           Summary: Some uses of target polymorphic arrays don't use the
                    span attached to their decl
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mikael at gcc dot gnu.org
  Target Milestone: ---

This is based on the last comment from the sashiko review at [1].

Testcase:

program prog
   implicit none
   integer, parameter :: n = 5
   type :: t1
      integer :: c1, c2
   end type
   type, extends(t1) :: t2
      integer :: c3
   end type
   type, extends(t2) :: t3
      integer :: c4, c5
   end type
   type(t3), target :: a(n)
   integer :: i
   a = [ (t3(5*i+4, 2*i*i+1, i*i, 3*i+2, 3*i*i+2), i=1,n) ]
   call sub1(a%t2)
contains
   subroutine sub1(x)
      class(t1), target, intent(in) :: x(:)
      integer, allocatable :: b(:)
      class(t1), allocatable :: c(:)
      integer, parameter :: expected(*) = [3, 9, 19, 33, 51]
      b = x%c2
      call check_all(b, expected, 18)
      do i=1,n
         call check_integer(x(i)%c2, expected(i), 10+i)
      end do
      call sub2(x(2:n-1))
   end subroutine
   subroutine sub2(x)
      class(t1), target, intent(in) :: x(:)
      integer, allocatable :: b(:)
      integer, parameter :: expected(*) = [9, 19, 33]
      b = x%c2
      call check_all(b, expected, 28)
      do i=1,n-2
         call check_integer(x(i)%c2, expected(i), 20+i)
      end do
   end subroutine
   subroutine check_all(x, y, e)
      integer, target, intent(in) :: x(:), y(:), e
      call check_integer(size(x, dim=1), size(y, dim=1), e*10+7)
      do i=1,size(x, dim=1)
         call check_integer(x(i), y(i), e*10+i)
      end do
      !if (any(x /= y)) error stop e*10+9
   end subroutine
   subroutine check_integer(x, y, e)
      integer, value :: x, y, e
      if (x /= y) then
         print *, e, "FAIL", x, y
         !error stop e
      end if
   end subroutine
end program

when executed, the program outputs:
         182 FAIL           5           9
         183 FAIL           4          19
         184 FAIL          19          33
         185 FAIL          11          51
          12 FAIL           5           9
          13 FAIL           4          19
          14 FAIL          19          33
          15 FAIL          11          51
         281 FAIL           5           9
         282 FAIL           4          19
         283 FAIL          19          33
          21 FAIL           5           9
          22 FAIL           4          19
          23 FAIL          19          33

Here is the analysis from the AI review at [1]:
> Updating only gfc_get_array_span in gcc/fortran/trans-array.cc is not
> sufficient to make indexing of a polymorphic target dummy use the descriptor
> span: gfc_get_array_span is only consulted when gfc_conv_expr_descriptor
> populates the span field of a newly built descriptor (which is why
> target_dummy_span_4.f90 passes when class_wrapper forwards the whole array x
> at offset 0 to another target dummy in check_all). Direct, scalarized, and
> section accesses to the polymorphic target dummy still index using
> _vptr->_size instead of GFC_DECL_SPAN, producing wrong-code:
> 1. Scalarized array references (such as x%c1 in an array expression, or when
>    gfc_conv_subref_array_arg packs x or x%c1 for a call to a non-target
>    dummy via gfc_conv_procedure_call in gcc/fortran/trans-expr.cc):
>    gfc_conv_scalarized_array_ref (gcc/fortran/trans-array.cc) calls
>    build_class_array_ref before checking for span-addressed arrays.
>    build_class_array_ref replaces sym->backend_decl (which carries
>    GFC_DECL_SPAN) with GFC_DECL_SAVED_DESCRIPTOR (decl), computes
>    size = gfc_class_vtab_size_get (decl) (_vptr->_size), and builds
>    gfc_build_spanned_array_ref (base, index, size). Furthermore,
>    is_span_addressed_array (gcc/fortran/trans-array.cc) requires
>    VAR_P (expr) && !GFC_DECL_CLASS (expr), which is false when
>    gfc_maybe_dereference_var wraps the local VAR_DECL in an INDIRECT_REF.
> 2. Single-element indexing (such as x(i)%c1 via gfc_conv_array_ref ->
>    build_array_ref in gcc/fortran/trans-array.cc -> gfc_build_array_ref in
>    gcc/fortran/trans.cc): gfc_conv_variable calls gfc_maybe_dereference_var
>    with descriptor_only_p = false (gcc/fortran/trans-expr.cc), which
>    replaces the local array decl with GFC_DECL_SAVED_DESCRIPTOR so se->expr
>    becomes (*x)._data. Because GFC_DECL_PTR_ARRAY_P on the _data FIELD_DECL
>    is only set for class pointers (gcc/fortran/trans-types.cc),
>    is_span_addressed_array ((*x)._data) returns false. Then build_array_ref
>    extracts vptr = gfc_class_vptr_get (*x), and gfc_build_array_ref in
>    gcc/fortran/trans.cc prioritizes if (vptr) span = gfc_vptr_size_get (vptr)
>    over get_array_span (type, decl) (where get_array_span in
>    gcc/fortran/trans.cc also checks GFC_DECL_CLASS (decl) before
>    GFC_DECL_GET_SPAN (decl) without unwrapping indirect references).
> 3. Non-zero-offset array sections (such as x(2:) in gfc_conv_expr_descriptor
>    -> gfc_get_dataptr_offset -> build_array_ref in
>    gcc/fortran/trans-array.cc): gfc_get_dataptr_offset passes desc = *x.0 to
>    build_array_ref. In build_array_ref, the INDIRECT_REF_P (desc) &&
>    GFC_DECL_SAVED_DESCRIPTOR (TREE_OPERAND (desc, 0)) check extracts
>    cdesc = (*x)._data and sets vptr = gfc_class_vptr_get (*x), causing
>    gfc_build_array_ref to multiply the section base offset by _vptr->_size
>    instead of GFC_DECL_SPAN.
> All of these paths (build_class_array_ref, build_array_ref,
> gfc_build_array_ref, get_array_span in gcc/fortran/trans.cc, and
> is_span_addressed_array) need to prefer GFC_DECL_GET_SPAN on the underlying
> local dummy array decl over _vptr->_size when indexing a span-addressed
> polymorphic dummy.

gfortran-16 shows the same output.

[1]:
https://gcc.sashiko.dev/#/patchset/20261026104353.58786-1-morin-mikael%40orange.fr

Reply via email to