public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* allocatable arrays and -fmax-stack-var-size
@ 2022-03-31 17:07 Steve Kargl
  2022-03-31 18:36 ` Thomas Koenig
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Kargl @ 2022-03-31 17:07 UTC (permalink / raw)
  To: fortran

So, it seems that at some point in the past, the option
-fmax-stack-var-size was expanded to allow the placement
of an allocatable array into static memory.  This has
a possibly unintended consequence in that automatic 
deallocation of an allocatable array does not (or can
not) occur.

program foo
   implicit none
   call testAutoDealloc(20)
   call testAutoDealloc(200)
   contains
      subroutine testAutoDealloc(n)
         integer, intent(in) :: n
         real, allocatable :: temp(:)
         allocate(temp(n))
         temp = n
         if (temp(n) /= n) stop n
      end
end program foo

% gfcx -o u  a.f90 && ./u
% gfcx -o u -fmax-stack-var-size=10 -fdump-tree-original a.f90 && ./u
At line 9 of file a.f90
Fortran runtime error: Attempting to allocate already allocated variable 'temp'
% head -4 u-a.f90.005t.original
__attribute__((fn spec (". r ")))
void testautodealloc (integer(kind=4) & restrict n)
{
  static struct array01_real(kind=4) temp = {.data=0B};

Now, it seems that an explicit deallocation of temp at the end of
the subroutine testAutoDealloc suppresses the runtime error.  Looking
at a -fdump-tree-original with the modified code shows

    if ((real(kind=4)[0:] * restrict) temp.data == 0B)
      {
        _gfortran_runtime_error_at (...Attempt to DEALLOCATE unallocated...);
      }
    else
      {
        __builtin_free ((void *) temp.data);
        (real(kind=4)[0:] * restrict) temp.data = 0B;
      }

Should the automatic deallocation of allocatable arrays be restore?
I'll let someone who cares enough to pursue this route.  Until then,
here's a patch to the manual to caution the unwary.

diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 6435dc4d4de..b5002d2a31a 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1786,13 +1786,19 @@ The default value for @var{n} is 65535.
 @item -fmax-stack-var-size=@var{n}
 @opindex @code{fmax-stack-var-size}
 This option specifies the size in bytes of the largest array that will be put
-on the stack; if the size is exceeded static memory is used (except in
-procedures marked as RECURSIVE). Use the option @option{-frecursive} to
-allow for recursive procedures which do not have a RECURSIVE attribute or
-for parallel programs. Use @option{-fno-automatic} to never use the stack.
+on the stack.  If the size of an array exceeds @var{n}, then the array is
+placed in static memory (except in procedures marked as RECURSIVE).  Use
+the option @option{-frecursive} to allow for recursive procedures which
+do not have a RECURSIVE attribute or for parallel programs.
+Use @option{-fno-automatic} to never use the stack.
+
+The @option{-Wsurprising} option can be used to determine which arrays
+have been placed into static memory.
+
+@option{-fmax-stack-var-size} can inhibit the automatic deallocation of
+allocatable arrays.  Proper memory management is required if this option
+is used (i.e., explicit deallocation is encouraged).
 
-This option currently only affects local arrays declared with constant
-bounds, and may not apply to all character variables.
 Future versions of GNU Fortran may improve this behavior.
 
 The default value for @var{n} is 65536.


-- 
Steve

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

* Re: allocatable arrays and -fmax-stack-var-size
  2022-03-31 17:07 allocatable arrays and -fmax-stack-var-size Steve Kargl
@ 2022-03-31 18:36 ` Thomas Koenig
  2022-03-31 18:49   ` Steve Kargl
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Koenig @ 2022-03-31 18:36 UTC (permalink / raw)
  To: Steve Kargl, fortran

Hi Steve,

> So, it seems that at some point in the past, the option
> -fmax-stack-var-size was expanded to allow the placement
> of an allocatable array into static memory.  This has
> a possibly unintended consequence in that automatic
> deallocation of an allocatable array does not (or can
> not) occur.

Sounds like a bug to me, and if your test program worked
in a previous release, it's a regression.

Probably best to open a PR.

Best regards

	Thomas

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

* Re: allocatable arrays and -fmax-stack-var-size
  2022-03-31 18:36 ` Thomas Koenig
@ 2022-03-31 18:49   ` Steve Kargl
  0 siblings, 0 replies; 3+ messages in thread
From: Steve Kargl @ 2022-03-31 18:49 UTC (permalink / raw)
  To: Thomas Koenig; +Cc: fortran

On Thu, Mar 31, 2022 at 08:36:37PM +0200, Thomas Koenig wrote:
> Hi Steve,
> 
> > So, it seems that at some point in the past, the option
> > -fmax-stack-var-size was expanded to allow the placement
> > of an allocatable array into static memory.  This has
> > a possibly unintended consequence in that automatic
> > deallocation of an allocatable array does not (or can
> > not) occur.
> 
> Sounds like a bug to me, and if your test program worked
> in a previous release, it's a regression.
> 
> Probably best to open a PR.
> 

Thomas

Seems someone from Fortran Discourse forum beat me to it.

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

With all of the work on openmp, I wasn't sure if this
was intended or not.  Either way it is surprising to me
that an allocatable array is placed in static memory.


I went looking and found this chunk of code in trans-decl.cc
(lines 743-774 where I removed the paragraph warning).

 /* Keep variables larger than max-stack-var-size off stack.  */
  if (!(sym->ns->proc_name && sym->ns->proc_name->attr.recursive)
      && !sym->attr.automatic
      && sym->attr.save != SAVE_EXPLICIT
      && sym->attr.save != SAVE_IMPLICIT
      && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
      && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
	 /* Put variable length auto array pointers always into stack.  */
      && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
	  || sym->attr.dimension == 0
	  || sym->as->type != AS_EXPLICIT
	  || sym->attr.pointer
	  || sym->attr.allocatable)
      && !DECL_ARTIFICIAL (decl))
    {
      if (flag_max_stack_var_size > 0
	  && !(sym->ns->proc_name
	       && sym->ns->proc_name->attr.is_main_program))
	gfc_warning (OPT_Wsurprising,
		     ...
		     sym->name, &sym->declared_at);

      TREE_STATIC (decl) = 1;

If I set the last line to 0, I get what I expect as far as an
allocatable array.  I have been unable to decipher the 12 line
conditional.

-- 
Steve

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

end of thread, other threads:[~2022-03-31 18:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31 17:07 allocatable arrays and -fmax-stack-var-size Steve Kargl
2022-03-31 18:36 ` Thomas Koenig
2022-03-31 18:49   ` Steve Kargl

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