proglangdesign.net

LLVM

The LLVM library facilitates the generation of native code on many platforms. Many compilers (e.g., clang, swift and rustc) rely on LLVM in their code generation phase. LLVM may also be used to create a JIT interpreter.

API bindings for accessing the library’s functionality exist for a number of languages beyond C++ and C. The C binding is a thin shim over the native C++ API. The bindings for other languages typically are FFI’s into the C binding. The API names are usually very similar between bindings.

A compiler’s back-end code generator:

Resources for Learning

LLVM is quite comprehensive, rich and helpful. However, its richness can make it feel intimidating to learn. The following resources can help:

Caveats

Despite the backing and dependencies of major players (Apple, Mozilla, etc.), use of the open-source LLVM is at your own risk. Historical experience suggests caution is warranted in these areas:

Tips for Installing LLVM

Although the LLVM website provides excellent detailed installation instructions, it can be valuable to know:

WebAssembly support

LLVM does include an experimental code generator for WebAssembly. However, since this generator is experimental, it is not built as part of the standard build process. The way you ask for it is by adding -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly to your CMake invocation. Once built, you can generate WebAssembly code using the target triple of “wasm32-unknown-unknown-wasm”.

Using the C API

When you are using the LLVM-C API there are a few important things to take note of.

This API is essentially just an extern "C" block which provides C-compatible functions which interact with the C++ ones.

Bindings

Since LLVM is such a popular platform, there are a number of bindings for popular languages.

Many of these libraries use the LLVM-C API because it is a lot easier to interface with C than C++ which means you should be taking the precautions noted in the paragraph above.