public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gmx.de>
To: Chung-Lin Tang <cltang@codesourcery.com>,
	Fortran List <fortran@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Cc: Tobias Burnus <tobias@codesourcery.com>
Subject: Re: [PATCH, Fortran] Fix setting of array lower bound for named arrays
Date: Mon, 29 Nov 2021 21:21:00 +0100	[thread overview]
Message-ID: <a7ae88fa-5683-af6f-a8d0-59f997a822e6@gmx.de> (raw)
In-Reply-To: <c8d8cac8-d066-cb1f-d1cc-c932f3edb89f@codesourcery.com>

Hi Chung-Lin,

Am 29.11.21 um 15:25 schrieb Chung-Lin Tang:
> This patch by Tobias, fixes a case of setting array low-bounds, found
> for particular uses of SOURCE=/MOLD=.
>
> For example:
> program A_M
>    implicit none
>    real, dimension (:), allocatable :: A, B
>    allocate (A(0:5))
>    call Init (A)
> contains
>    subroutine Init ( A )
>      real, dimension ( 0 : ), intent ( in ) :: A
>      integer, dimension ( 1 ) :: lb_B
>
>      allocate (B, mold = A)
>      ...
>      lb_B = lbound (B, dim=1)   ! Error: lb_B assigned 1, instead of 0
> like lower-bound of A.
>
> Referencing the Fortran standard:
>
> "16.9.109 LBOUND (ARRAY [, DIM, KIND])"
> states:
> "If DIM is present, ARRAY is a whole array, and either ARRAY is
>   an assumed-size array of rank DIM or dimension DIM of ARRAY has
>   nonzero extent, the result has a value equal to the lower bound
>   for subscript DIM of ARRAY. Otherwise, if DIM is present, the
>   result value is 1."
>
> And on what is a "whole array":
>
> "9.5.2 Whole arrays"
> "A whole array is a named array or a structure component ..."
>
> The attached patch adjusts the relevant part in gfc_trans_allocate() to
> only set
> e3_has_nodescriptor only for non-named arrays.
>
> Tobias has tested this once, and I've tested this patch as well on our
> complete set of
> testsuites (which usually serves for OpenMP related stuff). Everything
> appears well with no regressions.
>
> Is this okay for trunk?

I think you need to check the following:

  allocate(c, source=h(3))
  write(*,*) lbound(c,1), ubound(c,1) ! prints 1 3

...

  pure function h(i) result(r)
   integer, value, intent(in) :: i
   integer, allocatable :: r(:)
   allocate(r(3:5))
   r = [1,2,3]
  end function h

This used to print 3 5, which is also what e.g. NAG, Nvidia, flang do.
Intel prints 1 3, so it agrees with you.

The Fortran standard has:

9.7.1.2  Execution of an ALLOCATE statement

(6) When an ALLOCATE statement is executed for an array with no
     allocate-shape-spec-list, the bounds of source-expr determine the
     bounds of the array. Subsequent changes to the bounds of source-expr
     do not affect the array bounds.

Please re-check with Tobias.

Thanks,
Harald

> Thanks,
> Chung-Lin
>
> 2021-11-29  Tobias Burnus  <tobias@codesourcery.com>
>
> gcc/fortran/ChangeLog:
>
>      * trans-stmt.c (gfc_trans_allocate): Set e3_has_nodescriptor to true
>      only for non-named arrays.
>
> gcc/testsuite/ChangeLog:
>
>      * gfortran.dg/allocate_with_source_26.f90: Adjust testcase.
>      * gfortran.dg/allocate_with_mold_4.f90: New testcase.


WARNING: multiple messages have this Message-ID
From: Harald Anlauf <anlauf@gmx.de>
To: gcc-patches@gcc.gnu.org
Cc: fortran@gcc.gnu.org
Subject: Re: [PATCH, Fortran] Fix setting of array lower bound for named arrays
Date: Mon, 29 Nov 2021 21:21:00 +0100	[thread overview]
Message-ID: <a7ae88fa-5683-af6f-a8d0-59f997a822e6@gmx.de> (raw)
Message-ID: <20211129202100._mHnnvTUFOZ41MJ_4cdyEeHannPp1dTY_CDepRZo3Q8@z> (raw)
In-Reply-To: <c8d8cac8-d066-cb1f-d1cc-c932f3edb89f@codesourcery.com>

Hi Chung-Lin,

Am 29.11.21 um 15:25 schrieb Chung-Lin Tang:
> This patch by Tobias, fixes a case of setting array low-bounds, found
> for particular uses of SOURCE=/MOLD=.
> 
> For example:
> program A_M
>    implicit none
>    real, dimension (:), allocatable :: A, B
>    allocate (A(0:5))
>    call Init (A)
> contains
>    subroutine Init ( A )
>      real, dimension ( 0 : ), intent ( in ) :: A
>      integer, dimension ( 1 ) :: lb_B
> 
>      allocate (B, mold = A)
>      ...
>      lb_B = lbound (B, dim=1)   ! Error: lb_B assigned 1, instead of 0 
> like lower-bound of A.
> 
> Referencing the Fortran standard:
> 
> "16.9.109 LBOUND (ARRAY [, DIM, KIND])"
> states:
> "If DIM is present, ARRAY is a whole array, and either ARRAY is
>   an assumed-size array of rank DIM or dimension DIM of ARRAY has
>   nonzero extent, the result has a value equal to the lower bound
>   for subscript DIM of ARRAY. Otherwise, if DIM is present, the
>   result value is 1."
> 
> And on what is a "whole array":
> 
> "9.5.2 Whole arrays"
> "A whole array is a named array or a structure component ..."
> 
> The attached patch adjusts the relevant part in gfc_trans_allocate() to 
> only set
> e3_has_nodescriptor only for non-named arrays.
> 
> Tobias has tested this once, and I've tested this patch as well on our 
> complete set of
> testsuites (which usually serves for OpenMP related stuff). Everything 
> appears well with no regressions.
> 
> Is this okay for trunk?

I think you need to check the following:

  allocate(c, source=h(3))
  write(*,*) lbound(c,1), ubound(c,1) ! prints 1 3

...

  pure function h(i) result(r)
   integer, value, intent(in) :: i
   integer, allocatable :: r(:)
   allocate(r(3:5))
   r = [1,2,3]
  end function h

This used to print 3 5, which is also what e.g. NAG, Nvidia, flang do.
Intel prints 1 3, so it agrees with you.

The Fortran standard has:

9.7.1.2  Execution of an ALLOCATE statement

(6) When an ALLOCATE statement is executed for an array with no
     allocate-shape-spec-list, the bounds of source-expr determine the
     bounds of the array. Subsequent changes to the bounds of source-expr
     do not affect the array bounds.

Please re-check with Tobias.

Thanks,
Harald

> Thanks,
> Chung-Lin
> 
> 2021-11-29  Tobias Burnus  <tobias@codesourcery.com>
> 
> gcc/fortran/ChangeLog:
> 
>      * trans-stmt.c (gfc_trans_allocate): Set e3_has_nodescriptor to true
>      only for non-named arrays.
> 
> gcc/testsuite/ChangeLog:
> 
>      * gfortran.dg/allocate_with_source_26.f90: Adjust testcase.
>      * gfortran.dg/allocate_with_mold_4.f90: New testcase.



  reply	other threads:[~2021-11-29 20:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-29 14:25 Chung-Lin Tang
2021-11-29 20:21 ` Harald Anlauf [this message]
2021-11-29 20:21   ` Harald Anlauf
2021-11-29 20:56   ` Tobias Burnus
2021-11-29 21:11     ` Harald Anlauf
2021-11-30 17:24       ` Tobias Burnus
2021-11-30 19:54         ` Harald Anlauf
2021-11-30 20:01           ` Toon Moene

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a7ae88fa-5683-af6f-a8d0-59f997a822e6@gmx.de \
    --to=anlauf@gmx.de \
    --cc=cltang@codesourcery.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=tobias@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).