public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, Fortran] PR 57160: short-circuit IF only with -ffrontend-optimize
@ 2018-07-20 21:38 Janus Weil
  2018-07-23  7:40 ` Adam Hirst
  0 siblings, 1 reply; 35+ messages in thread
From: Janus Weil @ 2018-07-20 21:38 UTC (permalink / raw)
  To: gfortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1919 bytes --]

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.

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.

[-- Attachment #2: pr57160.diff --]
[-- Type: text/x-patch, Size: 1277 bytes --]

Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(revision 262908)
+++ gcc/fortran/trans-expr.c	(working copy)
@@ -3348,12 +3348,12 @@ gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
       return;
 
     case INTRINSIC_AND:
-      code = TRUTH_ANDIF_EXPR;
+      code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
       lop = 1;
       break;
 
     case INTRINSIC_OR:
-      code = TRUTH_ORIF_EXPR;
+      code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
       lop = 1;
       break;
 
Index: gcc/testsuite/gfortran.dg/actual_pointer_function_1.f90
===================================================================
--- gcc/testsuite/gfortran.dg/actual_pointer_function_1.f90	(revision 262908)
+++ gcc/testsuite/gfortran.dg/actual_pointer_function_1.f90	(working copy)
@@ -17,7 +17,11 @@ CONTAINS
 
   logical function cp_logger_log(logger)
     TYPE(cp_logger_type), POINTER ::logger
-    cp_logger_log = associated (logger) .and. (logger%a .eq. 42)
+    if (associated (logger)) then
+      cp_logger_log = (logger%a .eq. 42)
+    else
+      cp_logger_log = .false.
+    end if
   END function
 
   FUNCTION cp_get_default_logger(v) RESULT(res)

^ permalink raw reply	[flat|nested] 35+ messages in thread
* Re: [Patch, Fortran] PR 57160: short-circuit IF only with -ffrontend-optimize
@ 2018-07-24  9:12 Dominique d'Humières
  2018-07-24 13:46 ` Janus Weil
  0 siblings, 1 reply; 35+ messages in thread
From: Dominique d'Humières @ 2018-07-24  9:12 UTC (permalink / raw)
  To: Janus Weil; +Cc: adam, gfortran, gcc-patches

Hi Janus,

> 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)
> * …
>

Aren’t you confusing portability with validity?
The above codes are indeed invalid without short-circuit evaluation,
 but I did not find anything in the standard saying such codes are
invalid with short-circuit evaluation.

> 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.

This PR has nothing to do with optimization and I think
 it is a very bad idea to (ab)use any optimization option.

Please leave the default behavior (and test) as they are now.
If you want non short-circuit evaluation, introduce an option for it.

Note that the warning introduce for pr85599 should be disabled
for non short-circuit evaluations.

TIA

Dominique

^ permalink raw reply	[flat|nested] 35+ messages in thread
* Re: [Patch, Fortran] PR 57160: short-circuit IF only with -ffrontend-optimize
@ 2018-07-27 16:42 Dominique d'Humières
  2018-07-28  8:03 ` Janus Weil
  0 siblings, 1 reply; 35+ messages in thread
From: Dominique d'Humières @ 2018-07-27 16:42 UTC (permalink / raw)
  To: Janus Weil; +Cc: Thomas Koenig, gfortran

> If there is further constructive criticism regarding v3 of the patch,
> as posted at https://gcc.gnu.org/ml/fortran/2018-07/msg00103.html,
> I'll be happy to deal with that. If not, I'd like to commit it by the
> weekend.

This patch breaks backward compatibility and make the situation more confusing that it is presently.

Hence I am definitively against this patch.

Now I have wasted more than enough of my time on this nonsense and I won’t post anything else on this thread.

Dominique

^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2018-08-10 14:14 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-20 21:38 [Patch, Fortran] PR 57160: short-circuit IF only with -ffrontend-optimize Janus Weil
2018-07-23  7:40 ` Adam Hirst
2018-07-23 17:11   ` Janus Weil
2018-07-23 21:06     ` Fritz Reese
2018-07-24 18:46       ` Janus Weil
2018-07-24 19:49         ` Janus Weil
2018-07-24 19:50         ` Thomas Koenig
2018-07-24 20:14           ` Janus Weil
2018-07-25 20:05             ` Janus Weil
2018-07-25 20:59               ` Nicolas Koenig
2018-07-25 21:01                 ` Thomas Koenig
2018-07-25 21:31                 ` Janus Weil
2018-07-24  9:12 Dominique d'Humières
2018-07-24 13:46 ` Janus Weil
2018-07-24 15:42   ` Janne Blomqvist
2018-07-24 16:18     ` Janus Weil
2018-07-27 16:42 Dominique d'Humières
2018-07-28  8:03 ` Janus Weil
2018-08-01 20:46   ` Janus Weil
2018-08-06 20:59     ` Janus Weil
2018-08-07 10:11       ` Dominique d'Humières
2018-08-07 17:14         ` Janus Weil
2018-08-07 22:21           ` Thomas König
2018-08-08  0:17             ` William Clodius
2018-08-08  5:41             ` Janus Weil
2018-08-08  8:35           ` Manfred Schwarb
2018-08-08 11:23             ` Janus Weil
2018-08-08 11:29               ` Arjen Markus
2018-08-08 18:21                 ` Janus Weil
2018-08-08 20:48                   ` Janus Weil
2018-08-09  1:05                     ` Jerry DeLisle
2018-08-09 21:14                       ` Janus Weil
2018-08-10  0:38                         ` Jerry DeLisle
2018-08-10 14:14                           ` Janus Weil
2018-08-08 19:50             ` Janus Weil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).