RFA: Revamp fortran array types

Steve Kargl sgk@troutmask.apl.washington.edu
Thu Aug 20 04:20:00 GMT 2009


On Wed, Aug 19, 2009 at 07:59:45PM +0200, Toon Moene wrote:
> Steve Kargl wrote:
> 
> >On Wed, Aug 19, 2009 at 06:43:13PM +0200, Toon Moene wrote:
> 
> 
> >>The following code is perfectly OK:
> >>
> >>      DIMENSION A(10)
> >>      A = 10.0
> >>      CALL SUB(A,A)
> >>      END
> >>      SUBROUTINE SUB(A,B)
> >>      REAL, INTENT(IN) :: A(:), B(:)
> >
> >What happens if you replace the last line with
> >
> >       REAL, INTENT(IN)  :: A(:)
> >       REAL, INTENT(OUT) :: B(:)
> >
> 
> Fascinating.  I *think* that - as long as you do not actually write to B 
>  - you're in the clear.
> 
> Of course, INTENT(OUT) :: B(:) also means you cannot read from B unless 
> you set it before, so this basically amounts to an "unused" dummy 
> argument.  I wonder what -fwhole-file does with such an example ...
> 

Change to INTENT(INOUT) if you want.  The point is that it is
the user's responsibility to write code that does not alias.
The compiler can assume that there is no aliasing.


program a
  integer, target :: b
  integer, pointer :: p1, p2
  integer j
  integer, external :: clobber  ! An explicit interface isn't necessary.
  b = 1
  p1 => b
  p2 => b
  j = clobber(p1, p2)
  print *, j, p1
end program a
!
! Can a compiler re-order line 1 an line 2 below?
!
integer function clobber(x,y)
   integer, intent(in) :: x
   integer, intent(inout) :: y
   print *, x
   y = 2         ! line 1
   clobber = x   ! line 2
   print *, x
end function clobber


>From the F95 standard, 

   If the dummy argument is not a pointer and the corresponding actual
   argument is a pointer, the actual argument shall be currently
   associated with a target and the dummy argument becomes argument
   associated with that target.

This means that the dummy argument x is argument associated with
b, and the dummy argument y is argument associated with b.

Now, look at

   NOTE 12.23
   Since a dummy argument declared with an intent of IN shall not
   be used to change the associated actual argument, the associated
   actual argument remains constant throughout the execution of the
   procedure.

With regards to Michael's original testcase, I now see that he
has the pointer attribute on his dummy arguments.  I'll need to
review the standard to understand possible aliasing issues there,
but at the moment I am ENOTIME.

--
Steve



More information about the Fortran mailing list