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.

rebuilding clang

Let’s get started, shall we:

  1. run sudo alternatives --config ld and select ld.gold from the available options.
  2. install binutils-devel (this has the required include headers for the LLVMgold.so plugin to be actually built)
  3. configure clang without any lto on itself (i.e., set LLVM_ENABLE_LTO=Off). and set LLVM_BINUTILS_DIR to /usr/include.
  4. 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.
  5. make check/ninja-build check (yes, run it), and if this passes,
  6. 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.

closing remarks