Optimize Linux even more by using clang + full LTO to compile the kernel
I recently found out that you can optimize linux kernel even more by using clang-fullLTO, and here's how I did it with the help of kernel.org.
The Linux kernel has always traditionally been compiled with GNU toolchains such as GCC and binutils. Ongoing work has allowed for Clang and LLVM utilities to be used as viable substitutes. Distributions such as Android, ChromeOS, and OpenMandriva use Clang built kernels. LLVM is a collection of toolchain components implemented in terms of C++ objects. Clang is a front-end to LLVM that supports C and the GNU C extensions required by the kernel, and is pronounced “klang,” not “see-lang.”
tldr
- Add
LLVM=1
arg tomake
- Enable
LTO_CLANG_FULL
inmake nconfig
- Compile
Prerequisites
- Make sure latest clang is installed
- Make sure it's in the
$PATH
aka.which clang
returns a valid path. - Sufficient RAM if you want full LTO
Steps
On x86 and arm{,64}, you set LLVM=1
, on other archs, please refer to kernel.org
I automate these steps by adding the arg and path to clang into my kernel compilation script and kernel configuration script
1. Set $PATH for clang. (Don't set it in bashrc, since make
will call default shell in /etc/passwd
which in my case isn't bash.)
2. Use make LLVM=1 nconfig
to enable LTO_CLANG_FULL
:
-> General architecture-dependent options
-> Link Time Optimization (LTO) (<choice> [=y])
3. Compile the kernel with LLVM=1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Arguing that you don't care about the right to privacy because you have nothing to hide is no different from saying you don't care about free speech because you have nothing to say. – Edward Snowden