Usually code is compiled one file at a time, which is slow. The solution? A faster method called LTO. Hereโs everything you need to know about it:
Link-time optimization (LTO) is a method used during the compilation and linking of software code to improve its performance.
Traditionally, code is compiled and optimized file by file, resulting in separate units of machine code.
With LTO, instead of generating machine code directly, the compiler creates an intermediate representation (IR) for each file.
Later, during the linking phase, these IRs are analyzed together, allowing for more extensive optimizations across the entire program rather than individual files.
This whole-program analysis enables the removal of unused code, inlining of functions across different parts of the program, and other optimizations that were not possible when optimizing each file separately.
There are different types of LTO:
๐๐ฎ๐๐ถ๐ฐ ๐๐ง๐ข: Compiles each source file into an IR object file, then links them into a single module for whole-program optimization.
๐ฃ๐ฎ๐ฟ๐ฎ๐น๐น๐ฒ๐น ๐๐ง๐ข (๐ง๐ต๐ถ๐ป ๐๐ง๐ข): Distributes optimization work across source files in parallel, significantly reducing compile time while retaining most performance gains.
๐๐ฒ๐ณ๐ฒ๐ฟ๐ฟ๐ฒ๐ฑ ๐ ๐ผ๐ฑ๐ฒ ๐ฆ๐ฒ๐น๐ฒ๐ฐ๐๐ถ๐ผ๐ป: Object files store information to choose between basic and parallel LTO modes during linking.
๐๐ฒ๐ณ๐ฒ๐ฟ๐ฟ๐ฒ๐ฑ ๐๐ง๐ข ๐๐ป๐ฎ๐ฏ๐น๐ฒ๐บ๐ฒ๐ป๐: Allows the decision to use LTO or not at the linking stage for flexibility.