From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from troutmask.apl.washington.edu (troutmask.apl.washington.edu [128.95.76.21]) by sourceware.org (Postfix) with ESMTPS id 8DD15385741C for ; Thu, 31 Mar 2022 17:07:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8DD15385741C Received: from troutmask.apl.washington.edu (localhost [127.0.0.1]) by troutmask.apl.washington.edu (8.16.1/8.16.1) with ESMTPS id 22VH7l41057295 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO) for ; Thu, 31 Mar 2022 10:07:47 -0700 (PDT) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.16.1/8.16.1/Submit) id 22VH7lj9057294 for fortran@gcc.gnu.org; Thu, 31 Mar 2022 10:07:47 -0700 (PDT) (envelope-from sgk) Date: Thu, 31 Mar 2022 10:07:47 -0700 From: Steve Kargl To: fortran@gcc.gnu.org Subject: allocatable arrays and -fmax-stack-var-size Message-ID: <20220331170747.GA57166@troutmask.apl.washington.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: fortran@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Fortran mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Mar 2022 17:07:50 -0000 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