In the following Fortran program, the variable "foo" is never used, it is only set. Result: Using "gfortran -O3", "foo" is not optimized away. Expected: "foo" is optimized away as it happens with "ifort -O2". program a implicit none integer*8 it,two parameter(it=1073741824,two=2) complex foo(it*two-1) foo(10)=1. write(*,*) '' end program a
Is there a reason the Fortran frontend gives function local variables static storage duration? a () { struct __st_parameter_dt dt_parm.1; static integer(kind=4) options.0[8] = {68, 255, 0, 0, 0, 1, 0, 1}; static complex(kind=4) foo[2147483647]; <bb 2>: _gfortran_set_options (8, &options.0); foo[9] = __complex__ (1.0e+0, 0.0);
It works with it == 1024 in which case foo is not static.
> Is there a reason the Fortran frontend gives function local variables static > storage duration? For huge arrays, there is a problem if the memory is allocated on the stack, as one quickly hits stack-size limits. Thus gfortran puts large local arrays in static memory, except when a procedure can be called recursively/simultaneously (RECURSIVE attribute, -frecursive, -fopenmp). The size for which this happens is controlled by -fmax-stack-var-size=<n>. Actually, I don't quite see why the "static" matters: As local variable it cannot be accessed from elsewhere and if it is not accessed in the procedure ... Additionally, C's main() and Fortran's PRORGRAM (= "MAIN__") are special because they are only called once.
I did some testing with sunf95, icc and ifort. sunf95 also puts the variable in .bss as gfortran does, while ifort puts it on the stack (unless explicitly declared as static ["SAVE"]). If the variable is static, neither of the compilers optimizes it away. a) Why are static variables not optimized away? (Not even in main()/MAIN_?) b) Is there something what one could do to get the advantage of not having huge variables on the stack but still allowing to optimize the variable away?
Optimizing dead local static variables requires special handling which probably is not thought to be worth the trouble. If the variable is unused the programmer can as well remove it.
Subject: Re: Optimize away only set but not used variable Sent from my iPhone On Feb 25, 2009, at 1:43 AM, "rguenth at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org > wrote: > > > ------- Comment #1 from rguenth at gcc dot gnu dot org 2009-02-25 > 09:43 ------- > Is there a reason the Fortran frontend gives function local > variables static > storage duration? > Yes, it is larger than the threshhold. Remember fortran has no recursive functions except for the ones which marked as such. > a () > { > struct __st_parameter_dt dt_parm.1; > static integer(kind=4) options.0[8] = {68, 255, 0, 0, 0, 1, 0, 1}; > static complex(kind=4) foo[2147483647]; > > <bb 2>: > _gfortran_set_options (8, &options.0); > foo[9] = __complex__ (1.0e+0, 0.0); > > > -- > > rguenth at gcc dot gnu dot org changed: > > What |Removed |Added > --- > --- > ---------------------------------------------------------------------- > Status|UNCONFIRMED |NEW > Ever Confirmed|0 |1 > Last reconfirmed|0000-00-00 00:00:00 |2009-02-25 09:43:40 > date| | > > > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39298 >
The fortran front-end needs to be able to tell the middle-end that the function cannot be recursive and then the middle-end needs to use that info.
Fixed in GCC 5 by r5-657.