How to identify aliased arguments for array sections

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to identify aliased arguments for array sections

Ethan Beyak
Hi gfortran team,

I have a question on aliased arrays passed into subroutines. With this
short example, it shows that for array sections, there is no warning thrown
on array sections (either contiguous or non-contiguous) as actual
arguments, but there is a warning thrown for whole array actual arguments.

----------------------
$ cat aliasing.f90
! Date: 2019-09-28
module m
implicit none

private
public bar

interface bar
    procedure bar_r1, bar_r2
end interface bar

contains

    subroutine bar_r1(x1, y1, x2, y2)
    real, dimension(:), intent(in) :: x1, y1
    real, dimension(:), intent(out) :: x2, y2

                        ! without aliasing, we expect:
    x2 = x1 + y1        ! x2 = 2 + 5 = 7
    y2 = -x1 + y1       ! y2 = -2 + 5 = 3
                        ! with aliasing, we find:
                        ! y2 = -7 + 5 = -2

    end subroutine bar_r1

    subroutine bar_r2(x1, y1, x2, y2)
    real, dimension(:,:), intent(in) :: x1, y1
    real, dimension(:,:), intent(out) :: x2, y2

    x2 = x1 + y1
    y2 = -x1 + y1

    end subroutine bar_r2

end module m

program p
use m
implicit none

real, dimension(2,2) :: a,b

a = 2.0
b = 5.0
print *, "Non-contiguous array section"
call bar(a(2,:), b(2,:), a(2,:), b(2,:))
print *, a(2,:)
print *, b(2,:)

a = 2.0
b = 5.0
print *, "Contiguous array section"
call bar(a(:,2), b(:,2), a(:,2), b(:,2))
print *, a(:,2)
print *, b(:,2)

a = 2.0
b = 5.0
print *, "Whole array"
call bar(a, b, a, b)
print *, a
print *, b

end program p
----------------------

Upon compiling with 7.4.0:

$  gfortran -Waliasing -std=f2008 -fcheck=all aliasing.f90 -o a.out
aliasing.f90:63:12:

 call bar(a, b, a, b)
            1
Warning: Same actual argument associated with INTENT(IN) argument ‘y1’ and
INTENT(OUT) argument ‘y2’ at (1)
aliasing.f90:63:9:

 call bar(a, b, a, b)
         1
Warning: Same actual argument associated with INTENT(IN) argument ‘x1’ and
INTENT(OUT) argument ‘x2’ at (1)

And upon executing a.out,

 Non-contiguous array section
   7.00000000       7.00000000
  -2.00000000      -2.00000000
 Contiguous array section
   7.00000000       7.00000000
  -2.00000000      -2.00000000
 Whole array
   7.00000000       7.00000000       7.00000000       7.00000000
  -2.00000000      -2.00000000      -2.00000000      -2.00000000

What are your thoughts? Is there a flag I'm missing on compilation to catch
this warning? Or is this a deeper issue?

My -v and -c output upon compilation are:

$ gfortran -Waliasing -std=f2008 -fcheck=all aliasing.f90 -v -c

COLLECT_GCC=gfortran
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
7.4.0-1ubuntu1~18.04.1'
--with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-7
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin
--enable-default-pie --with-system-zlib --with-target-system-zlib
--enable-objc-gc=auto --enable-multiarch --disable-werror
--with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
COLLECT_GCC_OPTIONS='-Waliasing' '-std=f2008' '-fcheck=all' '-v' '-c'
'-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/7/f951 aliasing.f90 -quiet -dumpbase
aliasing.f90 -mtune=generic -march=x86-64 -auxbase aliasing -Waliasing
-std=f2008 -version -fcheck=all -fintrinsic-modules-path
/usr/lib/gcc/x86_64-linux-gnu/7/finclude -o /tmp/ccsMO5bX.s
GNU Fortran (Ubuntu 7.4.0-1ubuntu1~18.04.1) version 7.4.0 (x86_64-linux-gnu)
        compiled by GNU C version 7.4.0, GMP version 6.1.2, MPFR version
4.0.1, MPC version 1.1.0, isl version isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU Fortran2008 (Ubuntu 7.4.0-1ubuntu1~18.04.1) version 7.4.0
(x86_64-linux-gnu)
        compiled by GNU C version 7.4.0, GMP version 6.1.2, MPFR version
4.0.1, MPC version 1.1.0, isl version isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
aliasing.f90:60:9:

 call bar(a, b, a, b)
         1
Warning: Same actual argument associated with INTENT(IN) argument ‘x1’ and
INTENT(OUT) argument ‘x2’ at (1)
aliasing.f90:60:12:

 call bar(a, b, a, b)
            1
Warning: Same actual argument associated with INTENT(IN) argument ‘y1’ and
INTENT(OUT) argument ‘y2’ at (1)
COLLECT_GCC_OPTIONS='-Waliasing' '-std=f2008' '-fcheck=all' '-v' '-c'
'-mtune=generic' '-march=x86-64'
 as -v --64 -o aliasing.o /tmp/ccsMO5bX.s
GNU assembler version 2.30 (x86_64-linux-gnu) using BFD version (GNU
Binutils for Ubuntu) 2.30
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Waliasing' '-std=f2008' '-fcheck=all' '-v' '-c'
'-mtune=generic' '-march=x86-64'

Thanks for reading,
Ethan
Reply | Threaded
Open this post in threaded view
|

Re: How to identify aliased arguments for array sections

Janne Blomqvist-3
On Sat, Sep 28, 2019 at 11:06 PM Ethan Beyak <[hidden email]> wrote:
>
> Hi gfortran team,
>
> I have a question on aliased arrays passed into subroutines. With this
> short example, it shows that for array sections, there is no warning thrown
> on array sections (either contiguous or non-contiguous) as actual
> arguments, but there is a warning thrown for whole array actual arguments.

Hi,

for a general answer, in Fortran actual argument aliasing is not
allowed, but it's up to the programmer to make sure that this is
followed. That is, the compiler is free to optimize the code under the
assumption that arguments don't alias.

(There are some restrictions, IIRC arguments with the POINTER
attribute are allowed to alias)



--
Janne Blomqvist
Reply | Threaded
Open this post in threaded view
|

Re: How to identify aliased arguments for array sections

Ethan Beyak
Hi,

Thank you for the response. I see now in the 2008 standard the following
restriction (12.5.2.13 (3)):
"""
While an entity is associated with a dummy argument, the following
restrictions hold: [...] (3) Action that affects the value of the entity or
any subobject of it shall be taken only through the dummy argument unless
[POINTER/TARGET attributes ...]
"""
I'll be sure to avoid since, as you say, it is not permitted in the
standard and the responsibility is on the programmer. Thanks for bringing
this to my attention.

Ethan

On Sat, Sep 28, 2019 at 3:17 PM Janne Blomqvist <[hidden email]>
wrote:

> On Sat, Sep 28, 2019 at 11:06 PM Ethan Beyak <[hidden email]> wrote:
> >
> > Hi gfortran team,
> >
> > I have a question on aliased arrays passed into subroutines. With this
> > short example, it shows that for array sections, there is no warning
> thrown
> > on array sections (either contiguous or non-contiguous) as actual
> > arguments, but there is a warning thrown for whole array actual
> arguments.
>
> Hi,
>
> for a general answer, in Fortran actual argument aliasing is not
> allowed, but it's up to the programmer to make sure that this is
> followed. That is, the compiler is free to optimize the code under the
> assumption that arguments don't alias.
>
> (There are some restrictions, IIRC arguments with the POINTER
> attribute are allowed to alias)
>
>
>
> --
> Janne Blomqvist
>