What is compiler-rt for?

The name and documentation both suggest it contains essential runtime functions for use by generated code. However, it’s offered as an extra optional download, and when I omit it, the entire cycle of compiling a program with clang and running the resulting executable still seems to work fine. What am I missing?

You most likely have something equivalent (e.g. libgcc) installed already.


I’m probably not 100% right here, but here’s how I’d explain it:
It is there to allow the compiler to run on platforms that would otherwise not have the relevant functionality. If you already have a C++ compiler to build Clang with on the target system, you (most likely) have everything you need. But what if you are cross-compiling to a target with limited/no support for C++ on the target?

It’s not just cross compiling. On most operating systems, the OS vendor is responsible for providing a set of libraries that perform functionality expected by the compiler. This includes functions that provide missing CPU functionality (and which would be too expensive to inline everywhere, or which all programs must do in precisely the same way, irrespective of the compiler used), for example some complex floating point operations or atomics, and the generic unwind frameworks.

Most OS vendors *do* provide these (and so do some third-party toolchains, such as MinGW). GCC provides an implementation of these that most Linux distributions ship. On FreeBSD or OS X, these are provided by compiler-rt. If you are bringing up a new platform, then compiler-rt gives you some of these components.

Note that nothing in compiler-rt is specific to C++. The closest thing is the unwind library, which provides the generic part of the unwinder, which is also used for other languages (Objective-C, Ada, Java with GCJ, and so on).


Thanks for the clarification, David!

I didn’t even think about atomics - or floating point, for that matter.