Bug 32329 - Use of option "-fwhole-program" needs to exclude option "-c" in spec file
Summary: Use of option "-fwhole-program" needs to exclude option "-c" in spec file
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-06-13 23:47 UTC by Rob
Modified: 2007-06-14 08:26 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rob 2007-06-13 23:47:37 UTC
The bug report is about the use of the option "-fwhole-program".


When I compiled a program (crlibm-0.18beta1.tar.gz , described in bug report
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32180 ) I found that GCC 4.2.0 with 
option "-O2" produced code that was _slightly_ faster than GCC 4.3.0 with 
option "-O3" (on this _one_ particular program).

I set out to make GCC 4.3.0 produce better results and read the info file for gcc. I added a huge list of options intending to whittle them down a few at a time. I did eventually get a huge speedup, but this bug report is not about benchmarking.


This is about the option "-fwhole-program". I had it on the huge list and it caused problems. The docs for "-fwhole-program" say this:

`-fwhole-program'
     Assume that the current compilation unit represents whole program
     being compiled.  All public functions and variables with the
     exception of `main' and those merged by attribute
     `externally_visible' become static functions and in a affect gets
     more aggressively optimized by interprocedural optimizers.  While
     this option is equivalent to proper use of `static' keyword for
     programs consisting of single file, in combination with option
     `--combine' this flag can be used to compile most of smaller scale
     C programs since the functions and variables become local for the
     whole combined compilation unit, not for the single source file
     itself.


Problem:

Using "-fwhole-program" to compile "C" files that are then archived into a
library produces a library without any functions to call.


/usr/test/bin/gcc -O0 -std=c89  -g -fgcse-sm -fgcse-las -fgcse-after-reload -fsee -ftree-loop-linear -ftree-loop-im -fivopts -ftree-vectorize -ftracer -fvariable-expansion-in-unroller -fweb -fwhole-program -ffast-math -finline-limit=2400 -fmodulo-sched -Winline -march=athlon-xp -fgnu89-inline -finline-functions    -o crlibm_testval  test_val.o test_common.o ../libcrlibm.a -lmpfr -lmpfr -lgmp -lgmp -lm 
test_val.o: In function `main':
/root/downloads/crlibm/tests/test_val.c:114: undefined reference to `crlibm_init'
test_common.o: In function `rand_for_pow_perf':
/root/downloads/crlibm/tests/test_common.c:326: undefined reference to `log_rn'
/root/downloads/crlibm/tests/test_common.c:327: undefined reference to `log_rn'
test_common.o: In function `test_init':
/root/downloads/crlibm/tests/test_common.c:604: undefined reference to `exp_ru'
/root/downloads/crlibm/tests/test_common.c:606: undefined reference to `exp_rd'
...


Would we need to specify every single file in the library PLUS the final file we would link to the library to produce an executable using "-fwhole-program" ?

Another way of saying that is _IF_ you use "-fwhole-program" you can not use
the "-c" option since you must link everything. 



Example:

Yes:
gcc "-fwhole-program" file_1.c file_2.c file_3.c file_4.c -o result


No:
gcc "-fwhole-program" -c file_1.c file_2.c 
gcc "-fwhole-program" -c file_3.c file_4.c
ar cru library.a file_1.o file_2.o file_3.o file_4.o 
gcc "-fwhole-program" file_5.c -o result -lrary
file_1.c:000: undefined reference to `xyz'
...



If there are circumstances where it can NOT be used it should be documented.
The docs say it can be used on multiple files.

If "-fwhole-program" and "-c" can not be used together the "spec" file should make the options mutually exclusive.


The "-fwhole-program" option _could_ be used for libraries if it respected
a tag like "extern" that meant that the function would be called from outside
the current compilation unit. 

A new GNU extension is needed, something like "antistatic" or "declare" to
tell the "-fwhole-program" option to leave the ABI alone and work on everything
else. Here is a puny example:


void usage(){
  fprintf (stderr, "\nUsage: [-v] file\n");
  exit (EXIT_SUCCESS);
}

char* do_something(FILE* f, char* line) {
}

antistatic void ABI_check_program(int x) {
  if (x) 
    usage();
  else 
    do_something("test", 3);
}


If we compiled the above example with the proposed GNU extension and
used "-fwhole-program" as an option to gcc then the only visible
function would be ABI_check_program(), the rest would disappear.

That would allow "-fwhole-program" to have more uses (libraries or sections of programs not intended to interact with each other _except_ through a common file). It could be used to "protect" libraries from people looking in the .h file and going around the ABI to make direct calls (possibly inadvertantly due to similar spelling).
Comment 1 Andrew Pinski 2007-06-13 23:50:53 UTC
Read the documentation, use `externally_visible' as the docs say to.

There is nothing here really except you did not read the docs at all, even though you quoted them :).
Comment 2 Rob 2007-06-14 08:26:48 UTC
Agreed. After a 20 hour day the proximity of the word `-fwhole-program' to the attribute `externally_visible' while juggling three things at once did not work out so good.

Perhaps a compiler warning message could be created that when "-c" and "-fwhole-program" are used together without using "externally_visible" in the file the result is essentially an un-linkable object.