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

kargls at comcast dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kargls at comcast dot net

--- Comment #4 from kargls at comcast dot net ---
(In reply to Slava Zakharin from comment #3)
> Thank you for taking a look!
> 
> This code seems to comply with Fortran 2008 standard:
> 7.2.2 Pointer assignment
> ... data-pointer-object (bounds-remapping-list ) => data-target
> 

The issue isn't pointer assignment; other than you purposefully set
up an aliasing issue.  Your issues arise from the ordinary assignment.

  result(i,j,:,:) = abs(temp)

This is where your aliasing pops up, and gfortran does not currently
detect nor avoid the problem for you.  See trans-array.cc where one finds

  /* Resolve array data dependencies.  Creates a temporary if required.  */
  /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
     dependency.cc.  */

  void
  gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
                                 gfc_ss * rss)

gfortran scalarizes the assignment, which is essentially

  do n = 1, 5
     do m = 1, 4
        result(i,j,m,n) = abs(temp(m,n))
     end do
  end do

If one modifies gfc_conv_resolve_dependencies() to force the generation
of a temporary, then one get what you want.  As you, the programmer,
are aware of an aliasing issue, you can also pro-actively prevent the 
problem.

  subroutine test(result, i, j, data)
    implicit none                          ! <-- Should always appear
    integer :: i, j
    complex, target :: data(21)
    real, pointer :: result(:,:,:,:)
    complex, pointer, save :: temp(:,:)
    complex, allocatable :: tmp(:,:)       ! <-- Temporary variable that
    result(i:i,j:j,1:4,1:5) => data(2:)%re
    temp(1:4,1:5) => data
    tmp = temp                             ! <-- gfortran does not generate.
    result(i,j,:,:) = abs(tmp)
 end subroutine test

Richard Maine, a long-time member of J3 and editor for both F95 and
F2003, has many posts in comp.lang.fortran cautioning users to not
use Fortran pointers unless one absolutely has no other choice and
actually knows how to use them.

Reply via email to