http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52117

--- Comment #8 from steven hirshman <sphirshman at yahoo dot com> 2012-02-07 
16:18:33 UTC ---
Thank you. I didn't catch the PAUSE problem. It now runs correctly with the
-std=f95 flag, which is what we'll use.

Thanks for your helpful responses.


________________________________
From: burnus at gcc dot gnu.org <gcc-bugzi...@gcc.gnu.org>
To: sphirsh...@yahoo.com 
Sent: Tuesday, February 7, 2012 9:36 AM
Subject: [Bug fortran/52117] allocated arrays give incorrect results when used
with RESHAPE in gcc v4.6.2

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52117

--- Comment #7 from Tobias Burnus <burnus at gcc dot gnu.org> 2012-02-07
14:36:48 UTC ---
(In reply to comment #6)
> It compiles/links with the -std:f95 flag, but gives the same wrong results
> [qsh@swim SIESTA]$ gfortran -std=f95 reshape.f90
> reshape.f90:37.11:
>      PAUSE
>            1
> Error: Deleted feature: PAUSE statement at (1)
> [qsh@swim SIESTA]$ a.out

Well, you do not use -std=f95: As the compilation fails with an "ERROR", thus
no new "a.out" file is produced. If you then start "a.out", a previously
compiled file is used.

> (did not change B to B(:,:,:) since that is not a practical solution for us,
> lots of reshapes in our code

In Fortran 2003, an allocatable (array) on the LHS is automatically allocated
(if unallocated) or reallocated (if the shape does not match).

Unfortunately, the implementation had a bug, which could appear if the RHS
consisted of only a call to an intrinsic function, located the run-time library
(libgfortran).


Unless you provide me with a time machine, there are only two choices: Avoiding
a gfortran version with that bug by using a newer/older version. Or avoiding
the (re)allocation on assignment using either compiler flags or modifying the
code.

The only solutions, I see, which do not require code changes are:

- Use any GCC version before GCC 4.6.0; for instance GCC 4.5.x
- Use GCC 4.6 older than 2010-11-28
- Use a GCC (any version) newer than 2012-02-03
- Use -fno-realloc-lhs (caveat: Flag not supported before GCC 4.6)
- Use -std=f95 (caveat: Requires that the code compiles without error with
-std=f95)


I personally would use -fno-realloc-lhs [also due to performance reasons]
and/or an compiler version without the bug. For completeness, also the
following code changes are possible; except for the first one, they are not
recommended:

- Use an array spec for allocatable LHS, e.g. "B(:,:,:) = "
- Don't use allocatables left of " = RESHAPE"
- Make the expression on the RHS more complicated: add "+ 0" or surround with 
"( )".


The first solution ("B(:,:,:) = ") has the advantage that it avoids additional
checks whether the LHS is allocated and have the correct shape. With Fortran
2003/2008 compilers, adding it improves the performance minutely - and in hot
loops it might even matter. That's the reason that gfortran has
-fno-realloc-lhs, but also that other compiles have similar flags such as
"-assume norealloc_lhs" or "-e w".

Reply via email to