6 ms·
Why don't compilers do amalgamation themselves as a pre-compilation step?
by lanna 9y ago
Why don't compilers do amalgamation themselves as a pre-compilation step?
- monocasa 9y agoYou could argue that's the point of link time optimization.
- kornish 9y agoMy guess is that it's to prevent compilation of code which is unused. If code which isn't actually called anywhere is amalgamated and compiled, compilation time could actually increase instead of decrease. Seems easily avoidable though through basic dependency analysis, though.
- klodolph 9y agoThat's not actually true. You have to pass special linker flags to tell the linker to avoid including code which isn't used. With GNU Binutils and GCC, those flags are -Wl,--as-needed and -Wl,--gc-sections. Normally, unused code is only excluded if it is part of a static library. Even then it is only excluded or included an entire file at a time, unless you split files into multiple sections with the compiler (which has drawbacks—the compiler can do certain optimizations if it knows that two pieces of code or some code and data end up in the same section).
- kornish 9y agoAh, good to know – thanks for correcting.
- klodolph 9y agoThe compiler runs too early. If you're compiling C or C++, each individual .c/.cc/.cpp file gets compiled into a .o/.obj file by the compiler. Once these are all built, the linker combines the .o/.obj files and produces a library or executable. Compilation is finished before you get a chance to combine anything—the linker is what combines the code, and it can't do much optimization. However, with LTO (link-time optimization), the compiler doesn't finish compiling and writes out .o/.obj files with partially processed outputs. The linker is modified to re-invoke the compiler to finish compiling all the files at once. In GCC this is available with -flto. Actual amalgamation, where you combine many C files into one, is often not possible without modifying the files. It works for SQLite because they've made sure that their code works with amalgamation.
- flamedoge 9y agoIn practice, LTO compiled .o/.obj files just contains compiler IR. Combining C/C++ files into one is harder due to things like name resolution, etc.
- achamayou 9y agoBecause it means rebuilding everything every time, rather than just the relevant .o and re-linking. On a large enough codebase, being able to rebuild only what you touch and relink is the only way to get acceptable build times in dev.