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