ThinLTO from Clang 3.9 onwards on Fedora 24
On September 2nd 2016, llvm released version 3.9 . With that came the long-awaited ThinLTO functionality, see this blog post from llvm about it.
However, there are some caveats for getting ThinLTO to run under Fedora.
The biggest one is the actual «Clang for Fedora23 x86_64 Linux» package off the official download site as this doesn’t include the required LLVMgold.so plugin for ld.gold .
The other issue is that Fedora still defaults the old crusty BDF ld as ld instead of the less awful ld.gold one.
So, follow Clang’s get started guide in order to acquire the needed sources,
and also grab cmake’s gui frontend as it’ll make the configuration much easier (
cmake-gui package on Fedora).
Speaking of clang, you may also want to grab Clang Analyzer,
which does a wonderful job of finding (even obscure) issues in your code if you let it run over it.
Let’s get started, shall we:
sudo alternatives --config ldand select ld.gold from the available options.
binutils-devel(this has the required include headers for the LLVMgold.so plugin to be actually built)
- configure clang without any lto on itself (i.e., set
LLVM_ENABLE_LTO=Off). and set
- compile clang (NOTE: this will take quite some time) and unless you change the build type (
CMAKE_BUILD_TYPE) to something like «Release», then the installed binaries will be massive – think double-digit GByte in size.
- make check/ninja-build check (yes, run it), and if this passes,
- install (likely via sudo).
how to finally use ThinLTO
Set clang as CC and clang++ as CXX, then
-flto=thin to the C(XX)FLAGS of the project that you wish to build.
Make sure to check the LLVMgold documentation or you’ll be running into issues with ar and ranlib.
If you plan on rebuilding clang from source from time to time, make sure that you enable ccache – it’ll save you quite some time, especially when using make instead of ninja-build (the latter’s already doing a great job of minimizing the transitive closure for rebuilds).
Always make a copy of your cmake configure settings before you do a rebuild – getting them right every time when the build doesn’t go through is quite the chore.