Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ Optimization Flags: -fuse-linker-plugin vs -fwhole-program

I am reading:
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

It first suggests:
In combination with -flto using this option (-fwhole-program) should not be used. Instead relying on a linker plugin should provide safer and more precise information.

And then, it suggests:
If the program does not require any symbols to be exported, it is possible to combine -flto and -fwhole-program to allow the interprocedural optimizers to use more aggressive assumptions which may lead to improved optimization opportunities. Use of -fwhole-program is not needed when linker plugin is active (see -fuse-linker-plugin).

Does it mean that in theory, using -fuse-linker-plugin with -flto always gets a better optimized executable than using -fwhole-program with -flto?

I tried to use ld to link with -fuse-linker-plugin and -fwhole-program separately, and the executables' sizes at least are different.

P.S. I am using gcc 4.6.2, and ld 2.21.53.0.1 on CentOS 6.

like image 267
Hei Avatar asked Mar 25 '13 03:03

Hei


People also ask

Does order of gcc flags matter?

Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

What are optimization flags?

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program. The compiler performs optimization based on the knowledge it has of the program.

What does the werror flag do?

The "-Werror" compiler flag treats all warnings as build errors. By promoting all warnings to errors, it enforces the developers to ensure such build warnings that may otherwise go unnoticed or only loosely concerned about by developers to now treat it with priority given that it will interrupt the build process.


1 Answers

UPDATE: See @PeterCordes comment below. Essentially, -fuse-linker-plugin is no longer necessary.

These differences are subtle. First, understand what -flto actually does. It essentially creates an output that can be optimized later (at "link-time").

What -fwhole-program does is assumes "that the current compilation unit represents the whole program being compiled" whether or not that is actually the case. Therefore, GCC will assume that it knows all of the places that call a particular function. As it says, it might use more aggressive inter-procedural optimizers. I'll explain that in a bit.

Lastly, what -fuse-linker-plugin does is actually perform the optimizations at link time that would normally be done as each compilation unit is performed. So, this one is designed to pair with -flto because -flto means save enough information to do optimizations later and -fuse-linker-plugin means actually do those optimizations.

So, where do they differ? Well, as GCC doc suggests, there is no advantage in principle of using -fwhole-program because that option assumes something that you then have to ensure is true. To break it, simply define a function in one .cpp file and use it in another. You will get a linker error.

Is there any advantage to -fwhole-program? Well, if you only have one compilation unit then you can use it, but honestly, it won't be any better. I was able to get different sized executables by using equivalent programs, but when checking the actual generated machine code, they were identical. In fact, the only differences that I saw were that line numbers with debugging information were different.

like image 129
Nick Apperson Avatar answered Sep 18 '22 16:09

Nick Apperson