[Patch, Fortran] PR 57160: short-circuit IF only with -ffrontend-optimize

Adam Hirst adam@aphirst.karoo.co.uk
Mon Jul 23 07:40:00 GMT 2018


On 20/07/18 22:37, Janus Weil wrote:
> Hi all,
>
> here is a follow-up patch to my recent commit for PR 85599, also
> dealing with the short-circuiting of logical operators. In the course
> of the extensive discussion of that PR it became clear that the
> Fortran standard allows the short-circuiting of .AND. and .OR.
> operators, but does not mandate it.
>
> gfortran currently does short-circuiting, and after my patch for PR
> 85599 warns about cases where this might remove an impure function
> call (which potentially can change results).
>
> Now, this PR (57160) is about code which relies on the
> short-circuiting behavior. Since short-circuiting is not guaranteed by
> the standard, such code is invalid. Generating a warning or an error
> at compile-time is a bit harder here, though, since there are multiple
> variations of such a situation, e.g.:
> * ASSOCIATED(p) .AND. p%T
> * ALLOCATED(a) .AND. a%T
> * i<ubound(x) .AND. x(i)
> * ...
>
> The suggestion in the PR was to do short-circuiting only with
> optimization flags, but inhibit it with -O0, so that the faulty code
> will run into a segfault (or runtime error) at least when
> optimizations are disabled, and the problem can be identified.
>
> I find this suggestion very reasonable. It makes it possible to detect
> invalid code at -O0, while keeping good performance for valid code at
> -O{1,2,3}. Also it is technically very simple to implement, and it
> immediately identified a faulty test case that has lived in the
> testsuite for eleven years without being detected.
One thing I'm not seeing in the original discussion was whether or not
this should also count for -Og, which certainly in my experience is
(paired with -g) a common debug setting.

I would err towards -Og here being paired with -O0, but I could see it
being argued both ways - either way, I thought it might at least be
worth making explicit?

> The attached patch regtests cleanly on x86_64-linux-gnu. Ok for trunk?
>
> Cheers,
> Janus
>
>
> 2018-07-20  Janus Weil  <janus@gcc.gnu.org>
>
>     PR fortran/57160
>     * trans-expr.c (gfc_conv_expr_op): Use short-circuiting operators only
>     with -ffrontend-optimize. Without that flag, make sure that both
>     operands are evaluated.
>
>
> 2018-07-20  Janus Weil  <janus@gcc.gnu.org>
>
>     PR fortran/57160
>     * gfortran.dg/actual_pointer_function_1.f90: Fix invalid test case.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20180723/454875db/attachment.sig>


More information about the Fortran mailing list