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.

#linux #gaming #tech

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

  1. Add LLVM=1 arg to make
  2. Enable LTO_CLANG_FULL in make nconfig
  3. Compile

Prerequisites

  1. Make sure latest clang is installed
  2. Make sure it's in the $PATH aka. which clang returns a valid path.
  3. 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



Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

We can't live, work or learn in freedom unless the software we use is free.

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