LLVMLinux: Embracing the Dragon Presented by: Behan Webster ( lead) Presentation Date: 2014.08.22
Clang/LLVM LLVM is a Toolchain Toolkit (libraries from which compilers and related technologies can be built) Clang is a C/C++ toolchain
New Clang Benchmarks (compilation) Timed ImageMagick Compilation v6.8.1-10 Time To Compile Seconds, Less Is Better OpenBenchmarking.org GCC 4.8.2 60.35 SE +/- 0.36 GCC 4.9.0 RC1 60.59 SE +/- 0.04 LLVM Clang 3.5 20140413 29.61 SE +/- 0.14 14 28 42 56 70 Powered By Phoronix Test Suite 5.0.1 http://www.phoronix.com/scan.php?page=article&item=gcc49_compiler_llvm35&num=1
New Clang Benchmarks (execution) ebizzy v0.3 Records/s Seconds, More Is Better OpenBenchmarking.org GCC 4.8.2 42849 SE +/- 109.85 GCC 4.9.0 RC1 42950 SE +/- 44.08 LLVM Clang 3.5 20140413 43202 SE +/- 53.03 9000 18000 27000 36000 45000 Powered By Phoronix Test Suite 5.0.1 1. (CC) gcc options: -pthread -lpthread -O3 -march=native http://www.phoronix.com/scan.php?page=article&item=gcc49_compiler_llvm35&num=1
Other Interesting LLVM Related Projects Clang Static Analyzer Energy consumption analysis of programs using LLVM CUDA llvmpipe (Galium3D) OpenCL (most implementations are based on LLVM) Clang is one of the Android NDK compilers Renderscript in Android is based on LLVM Code transformation tools
The LLVMLinux Project Goals Fully build the Linux kernel for multiple architectures, using the Clang/LLVM toolchain Discover LLVM/Kernel issues early and find fixes quickly across both communities Upstream patches to the Linux Kernel and LLVM projects Bring together like-minded developers Enable the kernel community to do more in depth analysis of the kernel code
LLVMLinux Build/Test System Fetches, patches, builds, tests: clang, kernel, qemu, etc git clone http://git.linuxfoundation.org/llvmlinux.git cd llvmlinux/target/vexpress (or x86_64) make
Patched Mainline Kernel Tree A mainline kernel tree with all LLVMLinux patches applied on top is now available: git://git.linuxfoundation.org/llvmlinux/kernel.git Dated llvmlinux branches remotes/origin/llvmlinux-2014.07.30 The master branch is rebased regularly
Supported Architectures X86 (i586 and x86_64) ARM AARCH64 And now MIPS (Work in progress)
Linux-Next now has a branch which is pulled into Linux-Next git://git.linuxfoundation.org/llvmlinux/kernel.git remotes/origin/for-next Broader testing of our patches by more people Last step before being submitted to mainline
Improved Buildbot Farm New better buildbot master 4 slave build servers (2 more on the way) More CI builds for targets are in the works Configs for known good compiler versions and kernels
Other Avenues of Interest Clang Static Analysis of the Linux kernel Kernel specific Checkers (GSoC) Compiling the Android kernel and AOSP with clang Supporting Linaro LLVM team Adding clang support to yocto Building better tools based on LLVM for the kernel community
LLVMLinux Project Status LLVM/clang: All LLVMLinux patches for LLVM are Upstream Newer LLVM patches to support the Linux kernel are mostly being added by upstream maintainers Linux Kernel: Roughly 47 kernel patches for various arches LLVMLinux branch in linux-next
Kernel Patches Patches still working their way upstream Architecture all arm aarch64 x86_64 TOTALS Number of patches 13 10 16 8 47 Submitted 1 8 4 0 13
Kbuild Basic Kbuild support for clang and x86 has been upstreamed Specific support for ARM and aarch64 ready to be upstreamed
Integrated Assembly Status David Woodhouse added.code16 support for X86 ASM Renato Golin, Vicinius Tinti, Saleem Abdulrasool and Stepan Dyatkovskiy are working on fixing IA issues in clang to support the Linux ARM kernel code (and ultimately AARCH64) For now we disable the IA and use GNU as instead
Different option passing gcc passes -march to GNU as clang doesn't... (Bug submitted PR) -CFLAGS_aes-ce-cipher.o += -march=armv8-a+crypto +CFLAGS_aes-ce-cipher.o += -march=armv8-a+crypto -Wa,-march=armv8-a+crypto
extern inline: Different for gnu89 and gnu99 GNU89/GNU90 (used by gcc) Function will be inlined where it is used No function definition is emitted A non-inlined function may also be provided GNU99/C99 (used by clang) Function will be inlined where it is used An external function is emitted No other function of the same name may be provided. Solution? Use static inline instead. Only still an issue for ARM support for ftrace (submitted)
Attribute Order gcc is less picky about placement of attribute (()) clang requires it at the end of the type or variable -struct read_mostly va_alignment va_align = { +struct va_alignment read_mostly va_align = {
Named Registers Named registers for X86 kernel have been fixed in mainline kernel by Andi Kleen clang now supports using a globally named register for the stack pointer For ARM/AARCH64 move to using a global in asm/thread_info.h register unsigned long current_stack_pointer asm ("sp");
ARM percpu patch One of the uses of Named Registers in the ARM code is due to a deficiency in gcc. The new code which works with gcc fails in clang. Solution, provide routines for both, and choose at compile time Gcc: asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (*sp)); Clang: asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : : "memory");
Section Mismatch Issues (MergedGlobals) By default clang merges globals with internal linkage into one: MergedGlobals Allows globals to be addressed using offsets from a base pointer Can reduce the number of registers used Modpost uses symbol names to look for section mismatches MergedGlobals breaks modpost (false positive section mismatches) Current solution: use -mno-global-merge to stop global merging Updates to modpost may allow this optimization to be enabled again
Deprecated clang options clang options have been deprecated -KBUILD_CFLAGS += $(call cc-option, -fcatch-undefined-behavior) +KBUILD_CFLAGS += $(call cc-option, -fsanitize=undefined-trap) +KBUILD_CFLAGS += $(call cc-option, -fsanitize-undefined-trap-on-error)
ARM eabi support Clang emits code which uses the aeabi ARM calls which are implemented in compiler-rt (equivalient to libgcc) Compiler-rt doesn't easily cross compile yet... void aeabi_memcpy(void *dest, const void *src, size_t n) void aeabi_memmove(void *dest, const void *src, size_t n) void aeabi_memset(void *s, size_t n, int c)
Variable Length Arrays In Structs VLAIS isn't supported by Clang (undocumented gcc extension) char vla[n]; /* Supported, C99/C11 */ struct { char flexible_member[]; /* Supported, C99/C11 */ } struct_with_flexible_member; struct { char vlais[n]; /* Explicitly not allowed by C99/C11 */ } variable_length_array_in_struct; VLAIS is used in the Linux kernel in a number of places, spreading mostly through reusing patterns from data structures found in crypto
VLAIS Removal Example - struct { - struct shash_desc shash; - char ctx[crypto_shash_descsize(hash)]; - } desc; + char desc[sizeof(struct shash_desc) + + crypto_shash_descsize(hash)] CRYPTO_MINALIGN_ATTR; + struct shash_desc *shash = (struct shash_desc *)desc; unsigned int i; - desc.shash.tfm = hash; + shash->tfm = hash; (from crypto/hmac.c)
VLAIS Removal Example (cont.) (the missing pieces) #define ARCH_KMALLOC_MINALIGN alignof (unsigned long long) #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN #define CRYPTO_MINALIGN_ATTR attribute (( aligned (CRYPTO_MINALIGN))) struct shash_desc { struct crypto_shash *tfm; u32 flags; void * ctx[] CRYPTO_MINALIGN_ATTR; };
Status of VLAIS in the Linux Kernel USB Gadget patch is in mainline Mac80211 patch is in mainline Netfilter patch is in mainline Only the VLAIS patches for crypto are left: (apparmor, btrfs, dm-crypt, hmac, libcrc32c, testmgr)
How Can You Help? Make it known you want to be able to use Clang to compile the kernel Test LLVMLinux patches Report bugs to the mailing list Help get LLVMLinux patches upstream Work on unsupported features and Bugs Submit new targets and arch support Patches welcome
Embrace the Dragon. He's cuddly. Thank you http://llvm.linuxfoundation.org
Contribute to the LLVMLinux Project Project wiki page Project Mailing List http://lists.linuxfoundation.org/mailman/listinfo/llvmlinux http://lists.linuxfoundation.org/pipermail/llvmlinux/ IRC Channel #llvmlinux on OFTC http://llvm.linuxfoundation.org http://buildbot.llvm.linuxfoundation.org/irclogs/oftc/%23llvmlinux/ LLVMLinux Community on Google Plus