[committed] PR fortran/102366] [10/11/12 Regression] large arrays no longer become static

Message ID trinity-17b743b8-b1cc-4425-b280-4dd7a0476416-1631908943055@3c-app-gmx-bs54
State Committed
Delegated to: Jonathan Wakely
Headers
Series [committed] PR fortran/102366] [10/11/12 Regression] large arrays no longer become static |

Commit Message

Harald Anlauf Sept. 17, 2021, 8:02 p.m. UTC
  The attempt to fix a misleading warning lead to a regression that prevented
putting large variables in the main into static storage.  So instead of
preventing the move, we now disable the useless warning for variables in
the main.

Regtested on x86_64-pc-linux-gnu.  The patch was ok'ed in the PR by Jakub.
Pushed to mainline; will backport to affected branches.

Note, however, that the Fortran 2018 standard has:

F2018  8.5.16  SAVE attribute

(4) A variable, common block, or procedure pointer declared in the scoping
unit of a main program, [...] implicitly has the SAVE attribute

We already have code that sets IMPLICIT_SAVE for variables e.g. in
(sub)modules, but for code such as

  real(kind=4) :: a(10)
  a=1.0
end

(with and without PROGRAM statement) the array turns out to be too small to
currently get moved to static storage.  I get in decl.c::match_attr_spec:
gfc_state_stack->state == COMP_NONE, which defeated my attempts to an
ultimate solution.

I have opened

  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102390

to track this.

Thanks,
Harald
  

Patch

commit 51166eb2c534692c3c7779def24f83c8c3811b98
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Fri Sep 17 21:45:33 2021 +0200

    Fortran - (large) arrays in the main shall be static

    gcc/fortran/ChangeLog:

            PR fortran/102366
            * trans-decl.c (gfc_finish_var_decl): Disable the warning message
            for variables moved from stack to static storange if they are
            declared in the main, but allow the move to happen.

    gcc/testsuite/ChangeLog:

            PR fortran/102366
            * gfortran.dg/pr102366.f90: New test.

diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index bed61e2325d..3bd8a0fe935 100644
--- a/gcc/fortran/trans-decl.c
+++ b/gcc/fortran/trans-decl.c
@@ -743,7 +743,6 @@  gfc_finish_var_decl (tree decl, gfc_symbol * sym)

   /* Keep variables larger than max-stack-var-size off stack.  */
   if (!(sym->ns->proc_name && sym->ns->proc_name->attr.recursive)
-      && !(sym->ns->proc_name && sym->ns->proc_name->attr.is_main_program)
       && !sym->attr.automatic
       && sym->attr.save != SAVE_EXPLICIT
       && sym->attr.save != SAVE_IMPLICIT
@@ -757,7 +756,9 @@  gfc_finish_var_decl (tree decl, gfc_symbol * sym)
 	  || sym->attr.allocatable)
       && !DECL_ARTIFICIAL (decl))
     {
-      if (flag_max_stack_var_size > 0)
+      if (flag_max_stack_var_size > 0
+	  && !(sym->ns->proc_name
+	       && sym->ns->proc_name->attr.is_main_program))
 	gfc_warning (OPT_Wsurprising,
 		     "Array %qs at %L is larger than limit set by "
 		     "%<-fmax-stack-var-size=%>, moved from stack to static "
diff --git a/gcc/testsuite/gfortran.dg/pr102366.f90 b/gcc/testsuite/gfortran.dg/pr102366.f90
new file mode 100644
index 00000000000..d002f64a8ae
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr102366.f90
@@ -0,0 +1,9 @@ 
+! { dg-do compile }
+! { dg-options "-fdump-tree-original -Wall" }
+! { dg-final { scan-tree-dump-times "static real" 1 "original" } }
+! PR fortran/102366 - large arrays no longer become static
+
+program p
+  real(kind=4) :: a(16776325)
+  a=1.0
+end