GCC Bugzilla – Bug 26682
gfortran fails with -fwhole-program optimization
Last modified: 2007-10-03 21:30:34 UTC
The following simple.f90 program:
PRINT *,"Hello, World!"
END PROGRAM hello_world
does not compile with the following arguments:
gfortran -fwhole-program -O2 -o simple simple.f90
~/gcc_install/lib/gcc/i686-pc-linux-gnu/4.1.0/../../../libgfortranbegin.a(fmain.o)(.text+0x23): In function `main':
../../../gcc-4.1.0/libgfortran/fmain.c:18: undefined reference to `MAIN__'
collect2: ld returned 1 exit status
The program compiles fine with just -O2 or -fwhole-program and prints out the expected Hello, World!
I think the problem roughly is that -fwhole-program combined with -O2 is optimizing out the main function since it is not getting called internally.
I was unable to find in the documentation any method of forcing external visiblity in a fortran 90 program. There was also no mention of gfortran not having -fwhole-program working in http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Optimize-Options.html
I have a fix already in mind for this simple issue. Though -fwhole-program is not going to be fixed for Fortran code until the front-end stops having more than one decl per function.
There are three different issues:
1. -fwhole-program should be documented as not working for the Fortran programs.
2. The Fortran main program (MAIN__) needs to be kept by the -fwhole-program optimization; I think one way of achieving this is to mark it as "externally_visible", but I might be wrong (I've already been wrong on that issue,!).
3. We need to generate a correct call graph for Fortran code, either by emitting one single decl per function, or by making the cgraph calculation aware of this possible situation.
I'll try to tackle 1 and 2, but 3 needs to wait until 4.3 is branched.
Subject: Bug 26682
Date: Wed Oct 3 09:46:46 2007
New Revision: 128977
* options.c (gfc_post_options): Issue an error when
-fwhole-program is used.
* doc/invoke.texi (-fwhole-program): Document that Fortran
doesn't support this option.
Subject: Bug 26682
Date: Wed Oct 3 21:27:39 2007
New Revision: 128993
* trans-decl.c (build_function_decl): Set "externally_visible"
attribute on the MAIN program decl.
I've fixed 1 and 2 (and added an error message when -fwhole-program is used with Fortran), and there is a separate PR to track 3, so I'll close this one.