Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any benefit to passing all source files at once to a compiler?

I have read about "Whole Program Optimization" (wpo) and "Link Time Code Generation" (ltcg).

I wonder is there more inter-module-analysis going on if I pass all sources at once to the compiler from the cli (like "g++ a.cpp b.cpp")? Or is that just going to enable one of those flags?

Is there a difference between compilers for this? For instance can the intel compiler benefit from such practice while other compilers don't?

like image 693
onqtam Avatar asked Mar 06 '14 09:03

onqtam


3 Answers

I wonder is there more inter-module-analysis going on if I pass all sources at once to the compiler from the cli (like "g++ a.cpp b.cpp")?

For GCC, no, doing that does not enable any WPO, each translation unit is processed separately, in isolation. I'm 99% sure the same is true for Clang, and 90% sure it's true for most other compilers.

With GCC, to enable inter-module optimisation you need to request it explicitly via the -flto switch, which still processes each translation unit in isolation, but additional information is written to the object file and then when they are linked together further optimisation passes are done to produce the final output.

like image 73
Jonathan Wakely Avatar answered Oct 07 '22 20:10

Jonathan Wakely


At least for GCC, yes (if you supply the correct compiler flags).

The compiler performs optimization based on the knowledge it has of the program. Using the -funit-at-a-time flag will allow the compiler to consider information gained from later functions in the file when compiling a function. Compiling multiple files at once to a single output file (and using -funit-at-a-time) will allow the compiler to use information gained from all of the files when compiling each of them.

Source: GCC optimization options

Though compiling all filed individually and enabling LTO should conceputally give very similar (or identical) results.

like image 27
Damon Avatar answered Oct 07 '22 20:10

Damon


Intel C++ Compiler(ICC) supports this as a part Interprocedural Optimization. As Jonathan mentioned, each source file is compiled with IPO and stores them in a mock object file. Intel linking tool must be used to link the mock object files. During the linking phase, the compiler is invoked for one final time (when build with IPO) to perform interprocedural optimization across all mock object files. More information is available on this is documented at https://software.intel.com/en-us/node/459446.

like image 27
Anoop - Intel Avatar answered Oct 07 '22 20:10

Anoop - Intel