Bug 26682 - gfortran fails with -fwhole-program optimization
Summary: gfortran fails with -fwhole-program optimization
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.1.0
: P3 normal
Target Milestone: ---
Assignee: Francois-Xavier Coudert
URL:
Keywords: documentation, link-failure
Depends on:
Blocks:
 
Reported: 2006-03-14 15:16 UTC by Joshua Cogliati
Modified: 2007-10-03 21:30 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-10-01 10:58:25


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joshua Cogliati 2006-03-14 15:16:59 UTC
The following simple.f90 program:
PROGRAM hello_world
  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
Comment 1 Andrew Pinski 2006-03-14 15:19:46 UTC
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.
Comment 2 Francois-Xavier Coudert 2007-10-01 10:58:24 UTC
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.
Comment 3 Francois-Xavier Coudert 2007-10-03 09:46:59 UTC
Subject: Bug 26682

Author: fxcoudert
Date: Wed Oct  3 09:46:46 2007
New Revision: 128977

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=128977
Log:
	PR fortran/26682

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

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/doc/invoke.texi
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/options.c

Comment 4 Francois-Xavier Coudert 2007-10-03 21:27:49 UTC
Subject: Bug 26682

Author: fxcoudert
Date: Wed Oct  3 21:27:39 2007
New Revision: 128993

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=128993
Log:
	PR fortran/26682
	* trans-decl.c (build_function_decl): Set "externally_visible"
	attribute on the MAIN program decl.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c

Comment 5 Francois-Xavier Coudert 2007-10-03 21:30:34 UTC
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.