Llvm

Introduction
LLVM can be used as an alternative to GNU's compiler, GCC. The main benefit of using LLVM compilers instead of GCC is their lower memory usage, faster compile time and better diagnostics. There are some Benchmarks on the Clang and Phoronix homepages.

It may happen that some programs do not compile (like glibc) because they depend on GCC-specific language extensions (this is why the whole BSD code can be compiled with LLVM but some GNU code cannot) or segfault after successful compilation with LLVM (like xorg-server) but after following this guide, the system will still be able to compile packages with gcc. So if something goes wrong, it can be switched back to gcc for the particular package by uncommenting lines in /etc/make.conf and the bug should be reported. Linux software that can (not) be compiled can be seen on http://clang.debian.net/

LLVM Frontends
To be able to compile some sourcecode of a specific language, LLVM needs an appropriate frontend. There are clang and dragonegg in portage.

Clang is a C, C++, Objective C and Objective C++ front-end for LLVM and it currently implements all of the ISO C++ 1998 standard (including the defects addressed in the ISO C++ 2003 standard) except for 'export' (which has been removed in C++11) and is considered a production-quality C++ compiler. Clang is stable enough to be self-hosting and compile Boost, Qt , LibreOffice , FreeBSD , some parts of the Linux kernel  and more.

DragonEgg is a gcc plugin that replaces GCC's optimizers and code generators with those from LLVM. At the time of writing, DragonEgg requires llvm 3.0 and llvm 3.1 is out, which is preferred, especially if you intend to use clang. With DragonEgg Fortran works very well. Ada, C and C++ also work well. It can compile a reasonable amount of Obj-C, Obj-C++ and Go. It can compile simple Java programs, but they don't execute properly (this is a consequence of the java front-end not supporting GCC's LTO). Debug info is poor.

Since LLVM 3.0 the old llvm-gcc package is deprecated and replaced by dragonegg, so it has been removed from portage.

So after installing llvm, clang and dragonegg, you will be able to choose between gcc and clang whenever you like or use gcc with LLVM backend, aka DragonEgg.

Gold Plugin, LTO
You can use link time optimization (LTO) with clang and you also need it for dragonegg. You need a recent binutils version and configure your ld linker to replace the default linker ld.bfd with ld.gold. See the Gentoo Wiki for information about how to configure your system for LTO via Gold. You also have to use the lto useflag for gcc.

Check whether you have gold by running It will report “GNU gold” or else “GNU ld” if not. If you have gold, check for plugin support by running If it complains “missing argument” then you have plugin support.

Similarly, ar needs plugin support as well. It is possible to write an ar wrapper that passes some needed options to ar (see ), but there is a more elegant solution as binutils has an autoload function, which helps to compile more packages:

(Unfortunately, binutils only searchs $install_dir/../lib/bfd-plugins for its plugins necessitating an awkward symlink in /usr/x86_64-pc-linux-gnu/binutils-bin.) You have to take care, that the last symlink in this listing uses the correct libLLVM-3.FOO.so, where FOO is your LLVM version.

Then you can set the gold useflag for llvm and after everything is emerged you can use clang with LTO (see below). There are packages, that seem to refuse to link with LTO though, like texinfo, so it is wise to create an environment override for thse packages:

Install LLVM and its Frontends
Simply emerge the packages on ~arch systems. On arch systems you have to unmask some packages first. dragonegg requires gcc's lto USE-flag to be set and works with gcc 4.5 and gcc 4.6.

Note, that for clang++ the C++ headers search path is hardcoded to the active gcc profile. If you change the active gcc profile, or update gcc to a new version, you will have to remerge clang to update the search path.

To use dragonegg, run gcc as usual, with an extra command line argument "-fplugin=/usr/lib/llvm/dragonegg.so" If you change the active gcc profile, or update gcc to a new version, you will have to remerge dragonegg to update the plugin.

After the installation, check which CPUs are supported by using the command llvm-as < /dev/null | llc -mcpu=help and then add the following lines to /etc/make.conf (uncommenting the lines you need) to enable compilation via LLVM, adapting the march-option according to the previous command:

Fallback
If some package does not compile or segfaults, just comment out the LLVM-regarded lines and uncomment the old CFLAGS/CXXFLAGS and re-emerge this package. Then, please create a bugreport in one of the LLVM-, Gentoo-, package-bugtrackers. This will help to improve the software quality of the package or LLVM, depending on which one caused the bug.