Possible Front End optimization - remove entities USEd from module but not really used.

Tobias Burnus burnus@net-b.de
Mon Jan 7 19:07:00 GMT 2013


Toon Moene wrote:
> Consider the following example:
>
> $ cat loc-nomod.f
>       module noot
>       real :: x(10), y(5), z(16)
>       end module noot
>       program doit
>       use noot
>       common /aap/ a(2), b(10), c(16)
>       real :: f(6), g(3), h(10)
>       real, allocatable :: s(:), t(:), u(:)
>       locmod(r) = mod(loc(r),32)
>       allocate(s(3),t(10),u(13))
> !     print *,locmod(x(1)),locmod(y(1)),locmod(z(1))
>       print *,locmod(a(1)),locmod(b(1)),locmod(c(1))
>       print *,locmod(f(1)),locmod(g(1)),locmod(h(1))
>       print *,locmod(s(1)),locmod(t(1)),locmod(u(1))
>       end
>
> which I originally used to study default aligned for arrays of various 
> origins (module, common, local variable, allocatable.
>
> I commented out the arrays USEd from a module.  However, both with and 
> without -ffrontend-optimize (using 4.7.2),
>
> gfortran -Ofast -g -c -fdump-fortran-original loc-nomod.f
>
> showed the following in the output:
>
>   symtree: 'x'           || symbol: 'x'

Well, with "use noot", you make the symbol available to the compiler - 
hence, it is in the front-end's symtree. And 
-fdump-fortran-original/-fdump-fortran-optimized prints the symtree. 
However, if you don't use it, no declaration (Gimple tree) will be 
generated.

You can test this by placing the program in a separate file and 
compiling it. It shouldn't contain ("nm main.o") any reference to the 
module variables.

For your program (in a single file), the middle end should remove all 
module variables when compiled with "-fwhole-program" as they aren't 
used. (I haven't tried it. By the way, the ME could do more 
optimizations  than it currently does with static variables which only 
exist in a single TU.)

> <x> is in module <m>, but not referenced in subprogram <y>

We do have a similar warning with "ONLY", namely:
       use noot, only: x,y,z
           1
Warning: Unused module variable 'x' which has been explicitly imported 
at (1)


Without ONLY, there would be way to many warnings.

> or, in case only a few entities are referenced from a USEd module:
> Few entities from module <m> are used, suggest USE <m>, ONLY: <x>, 
> <y>, <z>
> with a (default) cutoff of e.g., 10 entities.

That sounds more reasonable. Though, the question is how to handle 
symbols which are from other modules, e.g. "use m1, only: foo", where 
"foo" is from module "m2" and only use-associated in "m1"? Or the same 
case, where "module m1" uses "use m2: foo => bar". Should one then 
suggest "USE m1, only: foo"? Or "USE m2, only: foo => bar" or something 
else?

Tobias



More information about the Fortran mailing list