Compiling the Linux Kernel with Clang

I decided to give Clang a spin to compile the kernel, and given that I’m not exactly an early adopter in attempting this, it was pretty smooth and straightforward overall. All it took was:
$ make CC=clang HOSTCC=clang -j12
I also did a quick comparison of the output of the two compilers with a standard ‘defconfig’ with CONFIG_DEBUG_INFO=y enabled on x86_64:
With GCC 9.2.1:
-
Build #1: real 3m38.073s, user 31m22.669s, sys 3m17.740s
-
Build #2: real 3m46.561s, user 31m58.313s, sys 3m22.958s
-
Build #3: real 3m38.174s, user 31m33.390s, sys 3m19.014s With Clang 9.0.0:
-
Build #1: real 5m21.978s, user 50m45.085s, sys 3m55.455s
-
Build #2: real 5m22.094s, user 50m44.977s, sys 3m53.522s
-
Build #3: real 5m17.531s, user 50m29.478s, sys 3m52.730s It appears that the compile time was up by nearly 50% with Clang.
Next, a comparison of the vmlinux file generated by the two compilers. CONFIG_DEBUG_INFO is enabled:
-
GCC 9.2.1:** 739M**
-
Clang 9.0.0:** 544M** This was a little interesting at first since the GCC generated kernel is 35% bigger than the Clang generated one. I decided to do another test with CONFIG_DEBUG_INFO disabled for comparison.
-
GCC 9.2.1:** vmlinux 57M (31M stripped), bzImage 8.7M**
-
Clang 9.0.0:** vmlinux 57M (31M stripped), bzImage 8.9M** With debug information stripped, it comes around to the same size, so both are even on this. The compile-time performance was similarly noticeable in this case as well. While faster than with debugging symbols enabled, Clang took longer to build compared to GCC.
-
GCC 9.2.1 : real 2m40.687s, user 26m0.946s, sys 2m36.858s
-
Clang 9.0.0 : real 4m41.045s, user 46m9.836s, sys 3m18.187s Next, I took a very crude look at kernel + systemd start time to login prompt on a qemu emulator, to see if there was any difference to execution speeds and here’s what I saw during three iterations:
-
GCC 9.2.1 : 3.85s, 3.98s, 3.74s
-
Clang : 3.70s, 3.62s, 3.70s While it’s a somewhat crude metric, I think the perceived variance is not significant or perceptible at least for the casual user, and would also claim things to be more or less even on this front.
Overall, I think it’s good to have options for open source compilers and I’m glad that Clang has come into the picture as a serious contender for the domination that GCC endured over many decades.
