Re: [patch, fortran, doc] Explicitly mention undefined overflow

2023-03-20 Thread Thomas Koenig via Fortran

I wrote:




Yes, that's fine for trunk. I wonder if it is worth being explicit that
linear congruential pseudo-random number generators can and do fail at 
-O3?


I don't think we should put this into the docs, because that can change
at any time.  Maybe into porting_to.html, though (where I have only
mentioned this as a general issue with linear congruential generators,
without mentioning specific options. Current text can be seen at
https://gcc.gnu.org/gcc-13/porting_to.html ).

Hm


Breaking things actually goes back further than I thought.
Taking the random number generator from rnseed and running
it 10 times with my system gfortran 9.4.0 gets me, at
different levels of optimization:

$ for a in -O0 -O1 -O2; do echo $a; gfortran $a genuni.f90 && ./a.out; done
-O0
  0.269411892
  0.891386986
  0.444042951
  0.779210865
  0.500058949
  0.666437685
  0.666963458
  0.462416053
  0.376364112
   2.90278494E-02
-O1
  0.269411892
  0.891386986
  0.444042951
  0.779210865
  0.500058949
  0.666437685
  0.666963458
  0.462416053
  0.376364112
   2.90278494E-02
-O2
 -0.730588138
  0.891386986
 -0.555957019
 -0.220789105
 -0.499941051
  0.666437685
 -0.333036542
  0.462416053
  0.376364112
   2.90278494E-02

and for current trunk it is

$ for a in -O0 -O1 -O2; do echo $a; gfortran $a genuni.f90 && ./a.out; done
-O0
  0.269411892
  0.891386986
  0.444042951
  0.779210865
  0.500058949
  0.666437685
  0.666963458
  0.462416053
  0.376364112
   2.90278494E-02
-O1
  0.269411892
  0.891386986
  0.444042951
  0.779210865
  0.500058949
  0.666437685
  0.666963458
  0.462416053
  0.376364112
   2.90278494E-02
-O2
  0.211324871
  0.211324871
  0.211324871
  0.211324871
  0.211324871
  0.211324871
  0.211324871
  0.211324871
  0.211324871
  0.211324871

so it the general problem is not restricted to -O3 and not
to current trunk, it depends on the details.

I doubt that the result from 9.4.0 was expected, but rather
nobody noticed.  Or, bringing out the pseudo-RNG into a
different setting changed things.

So... any suggestions on how to improve the current wording?

Best regards

Thomas



Re: [PATCHv3, gfortran] Escalate failure when Hollerith constant to real conversion fails [PR103628]

2023-03-20 Thread Tobias Burnus

Hi,

sorry for the belated reply.

On 07.03.23 09:55, HAO CHEN GUI wrote:

2023-03-07  Haochen Gui 

gcc/
  PR target/103628
  * fortran/target-memory.cc (gfc_interpret_float): Return FAIL when
  native_interpret_expr gets a NULL tree.
  * fortran/arith.cc (gfc_hollerith2real): Return NULL when
  gfc_interpret_float fails.
  * fortran/error.cc (gfc_buffered_p): Define.
  * fortran/gfortran.h (gfc_buffered_p): Declare.
  * fortran/intrinsic.cc: Add diagnostic.h to include list.
  (do_simplify): Save errorcount and check it at finish.  Report a
  "Cannot simplify expression" error on a bad result if error count
  doesn't change and no other errors buffered.

gcc/testsuite/
  PR target/103628
  * gfortran.dg/pr103628.f90: New.

Co-Authored-By: Tobias Burnus 

...

--- a/gcc/fortran/intrinsic.cc
+++ b/gcc/fortran/intrinsic.cc

...

@@ -4708,7 +4710,12 @@ do_simplify (gfc_intrinsic_sym *specific, gfc_expr *e)

  finish:
if (result == &gfc_bad_expr)
-return false;
+{
+  if (errorcount == old_errorcount
+   && (gfc_buffered_p () && !gfc_error_flag_test ()))
+   gfc_error ("Cannot simplify expression at %L", &e->where);
+  return false;
+}


The condition looks wrong. Shouldn't it be something like

+ && (!gfc_buffered_p () || !gfc_error_flag_test ()))

Namely:
* If there is no buffering, we know that no error has been printed → call error.
* With buffering, we additionally need to check for buffered errors.
  No buffered error → call error.

Otherwise LGTM.

Thus, if my comment makes sense to you + it regtests, modify it, and
go ahead and commit it.

Sorry for the delay - and thanks again for the patch!

Tobias

-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955


[PATCH] Fortran: fix documentation of -fno-underscoring [PR109216]

2023-03-20 Thread Harald Anlauf via Fortran
Dear all,

as reported, the implicit documentation of -funderscoring, which
is found under -fno-underscoring, has gone sideways long time ago.
The attached patch should fix it.

OK for mainline, or did I miss something?

Thanks,
Harald

From c296196044248f974b4907bb2f5bdeeea24adb5b Mon Sep 17 00:00:00 2001
From: Harald Anlauf 
Date: Mon, 20 Mar 2023 20:55:00 +0100
Subject: [PATCH] Fortran: fix documentation of -fno-underscoring [PR109216]

gcc/fortran/ChangeLog:

	PR fortran/109216
	* invoke.texi: Correct documentation of how underscores are appended
	to external names.
---
 gcc/fortran/invoke.texi | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 5679e2f2650..cbe7f377507 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1573,7 +1573,7 @@ Do not transform names of entities specified in the Fortran
 source file by appending underscores to them.

 With @option{-funderscoring} in effect, GNU Fortran appends one
-underscore to external names with no underscores.  This is done to ensure
+underscore to external names.  This is done to ensure
 compatibility with code produced by many UNIX Fortran compilers.

 @emph{Caution}: The default behavior of GNU Fortran is
@@ -1596,7 +1596,7 @@ I = J() + MAX_COUNT (MY_VAR, LVAR)
 @noindent
 is implemented as something akin to:
 @smallexample
-i = j_() + max_count__(&my_var__, &lvar);
+i = j_() + max_count_(&my_var, &lvar);
 @end smallexample

 With @option{-fno-underscoring}, the same statement is implemented as:
--
2.35.3



[PATCH] Fortran: reject MODULE PROCEDURE outside generic module interface [PR99036]

2023-03-20 Thread Harald Anlauf via Fortran
Dear all,

the attached trivial patch catches a MODULE PROCEDURE outside of a
module interface before we run into an internal error.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

This PR is marked as an 11/12/13 regression, so this is a candidate
for backporting.

Thanks,
Harald

From 9c59709fad91c99041a9cb770b98da17af01d260 Mon Sep 17 00:00:00 2001
From: Harald Anlauf 
Date: Mon, 20 Mar 2023 21:50:59 +0100
Subject: [PATCH] Fortran: reject MODULE PROCEDURE outside generic module
 interface [PR99036]

gcc/fortran/ChangeLog:

	PR fortran/99036
	* decl.cc (gfc_match_modproc): Reject MODULE PROCEDURE if not in a
	generic module interface.

gcc/testsuite/ChangeLog:

	PR fortran/99036
	* gfortran.dg/pr99036.f90: New test.
---
 gcc/fortran/decl.cc   | 1 +
 gcc/testsuite/gfortran.dg/pr99036.f90 | 9 +
 2 files changed, 10 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr99036.f90

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index c8f0bb83c2c..b29f491fe1f 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -9998,6 +9998,7 @@ gfc_match_modproc (void)
   if ((gfc_state_stack->state != COMP_INTERFACE
&& gfc_state_stack->state != COMP_CONTAINS)
   || gfc_state_stack->previous == NULL
+  || !current_interface.type
   || current_interface.type == INTERFACE_NAMELESS
   || current_interface.type == INTERFACE_ABSTRACT)
 {
diff --git a/gcc/testsuite/gfortran.dg/pr99036.f90 b/gcc/testsuite/gfortran.dg/pr99036.f90
new file mode 100644
index 000..a6e396f6f71
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr99036.f90
@@ -0,0 +1,9 @@
+! { dg-do compile }
+! PR fortran/99036 - ICE in gfc_current_interface_head
+! Contributed by G. Steinmetz
+
+module m
+contains
+  module procedure s ! { dg-error "must be in a generic module interface" }
+  end
+end
--
2.35.3



Re: [patch, fortran, doc] Explicitly mention undefined overflow

2023-03-20 Thread Harald Anlauf via Fortran

Hi Thomas,

Am 20.03.23 um 08:14 schrieb Thomas Koenig via Gcc-patches:

so it the general problem is not restricted to -O3 and not
to current trunk, it depends on the details.

I doubt that the result from 9.4.0 was expected, but rather
nobody noticed.  Or, bringing out the pseudo-RNG into a
different setting changed things.

So... any suggestions on how to improve the current wording?


how about changing:

"... relying on a specific, non-standard behavior may now generate
unexpected results."

to

"... relying on a specific, non-standard behavior may generate
unexpected results depending on optimization level and other compiler
flags."

We cannot know all the codes used in the wild ...

Cheers,
Harald


Best regards

 Thomas






Re: [PATCH] Fortran: fix documentation of -fno-underscoring [PR109216]

2023-03-20 Thread Sandra Loosemore

On 3/20/23 14:05, Harald Anlauf via Gcc-patches wrote:

Dear all,

as reported, the implicit documentation of -funderscoring, which
is found under -fno-underscoring, has gone sideways long time ago.
The attached patch should fix it.

OK for mainline, or did I miss something?


This is OK.

-Sandra


[PATCHv4, gfortran] Escalate failure when Hollerith constant to real conversion fails [PR103628]

2023-03-20 Thread HAO CHEN GUI via Fortran
Hi,
  I refined the patch according to reviewer's advice. The main change is to
check if buffer_p is set and buffered error exists. Also two regtests are
fixed by catching the new error.

  I sent out the revised one for review due to my limited knowledge on
Fortran front end.

  The patch escalates the failure when Hollerith constant to real conversion
fails in native_interpret_expr. It finally reports an "Cannot simplify
expression" error in do_simplify method.

  The patch for pr95450 added a verification for decoding/encoding checking
in native_interpret_expr. native_interpret_expr may fail on real type
conversion and returns a NULL tree then. But upper layer calls don't handle
the failure so that an ICE is reported when the verification fails.

  IBM long double is an example. It doesn't have a unique memory presentation
for some real values. So it may not pass the verification. The new test
case shows the problem.

  errorcount is used to check if an error is already reported or not when
getting a bad expr. Buffered errors need to be excluded as they don't
increase error count either.

  The patch passed regression test on Power and x86 linux platforms.

Thanks
Gui Haochen

ChangeLog
2023-03-21  Haochen Gui 

gcc/
PR target/103628
* fortran/target-memory.cc (gfc_interpret_float): Return FAIL when
native_interpret_expr gets a NULL tree.
* fortran/arith.cc (gfc_hollerith2real): Return NULL when
gfc_interpret_float fails.
* fortran/error.cc (gfc_buffered_p): Define.
* fortran/gfortran.h (gfc_buffered_p): Declare.
* fortran/intrinsic.cc: Add diagnostic.h to include list.
(do_simplify): Save errorcount and check it at finish.  Report a
"Cannot simplify expression" error on a bad result if error count
doesn't change and no other errors buffered.

gcc/testsuite/
PR target/103628
* gfortran.dg/assumed_size_refs_2.f90: Catch "Cannot simplify
expression" error.
* gfortran.dg/unpack_field_1.f90: Likewise.
* gfortran.dg/pr103628.f90: New.

Co-Authored-By: Tobias Burnus 


patch.diff
diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc
index c0d12cfad9d..d3d38c7eb6a 100644
--- a/gcc/fortran/arith.cc
+++ b/gcc/fortran/arith.cc
@@ -2752,10 +2752,12 @@ gfc_hollerith2real (gfc_expr *src, int kind)
   result = gfc_get_constant_expr (BT_REAL, kind, &src->where);

   hollerith2representation (result, src);
-  gfc_interpret_float (kind, (unsigned char *) result->representation.string,
-  result->representation.length, result->value.real);
-
-  return result;
+  if (gfc_interpret_float (kind,
+  (unsigned char *) result->representation.string,
+  result->representation.length, result->value.real))
+return result;
+  else
+return NULL;
 }

 /* Convert character to real.  The constant will be padded or truncated.  */
diff --git a/gcc/fortran/error.cc b/gcc/fortran/error.cc
index 214fb78ba7b..872d42e731e 100644
--- a/gcc/fortran/error.cc
+++ b/gcc/fortran/error.cc
@@ -49,6 +49,13 @@ static gfc_error_buffer error_buffer;
 static output_buffer *pp_error_buffer, *pp_warning_buffer;
 static int warningcount_buffered, werrorcount_buffered;

+/* Return buffered_p.  */
+bool
+gfc_buffered_p (void)
+{
+  return buffered_p;
+}
+
 /* Return true if there output_buffer is empty.  */

 static bool
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 219ef8c7612..edfe11796a6 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -3328,6 +3328,7 @@ void gfc_internal_error (const char *, ...) 
ATTRIBUTE_NORETURN ATTRIBUTE_GCC_GFC
 void gfc_clear_error (void);
 bool gfc_error_check (void);
 bool gfc_error_flag_test (void);
+bool gfc_buffered_p (void);

 notification gfc_notification_std (int);
 bool gfc_notify_std (int, const char *, ...) ATTRIBUTE_GCC_GFC(2,3);
diff --git a/gcc/fortran/intrinsic.cc b/gcc/fortran/intrinsic.cc
index e89131f5a71..2572b7a3448 100644
--- a/gcc/fortran/intrinsic.cc
+++ b/gcc/fortran/intrinsic.cc
@@ -25,6 +25,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "options.h"
 #include "gfortran.h"
 #include "intrinsic.h"
+#include "diagnostic.h" /* For errorcount.  */

 /* Namespace to hold the resolved symbols for intrinsic subroutines.  */
 static gfc_namespace *gfc_intrinsic_namespace;
@@ -4620,6 +4621,7 @@ do_simplify (gfc_intrinsic_sym *specific, gfc_expr *e)
 {
   gfc_expr *result, *a1, *a2, *a3, *a4, *a5, *a6;
   gfc_actual_arglist *arg;
+  int old_errorcount = errorcount;

   /* Max and min require special handling due to the variable number
  of args.  */
@@ -4708,7 +4710,12 @@ do_simplify (gfc_intrinsic_sym *specific, gfc_expr *e)

 finish:
   if (result == &gfc_bad_expr)
-return false;
+{
+  if (errorcount == old_errorcount
+ && (!gfc_buffered_p () && !gfc_error_flag_test ()))
+   gfc_error ("Cannot simplify expression at %L", &e->wh