How to enable a LLVM backend?

EDIT (2021-07-23): This answer was updated to use "Motorola 68000" instead of "WebAssembly" as an example experimental target (since Wasm is now stable).

LLVM is not very configurable once it is built. If you need a configuration beyond the defaults, you have to compile LLVM yourself.

LLVM has a few articles explaining how to compile it, but it does not describe exactly how to enable additional targets:

  • Getting Started
  • Building LLVM with CMake

The enabled targets are controlled by two variables that you need to define when invoking CMake to prepare the build directory: LLVM_TARGETS_TO_BUILD and LLVM_EXPERIMENTAL_TARGETS_TO_BUILD.

  • LLVM_TARGETS_TO_BUILD controls only the stable targets. You can either use the special value all to enable all the stable targets or provide a semicolon-separated list of targets such as ARM;PowerPC;X86. There is an old request to rename the special value to stable and use all for all the targets. Its default value is all (see below for the list of targets).

  • LLVM_EXPERIMENTAL_TARGETS_TO_BUILD is an undocumented (or well hidden) variable that allows you to enable any target you want. This is also a semicolon-separated list of targets.

The enabled targets will correspond to the union of both lists.

Now, you need to find out the actual name of your target and if it is a stable or experimental target. The list of stable targets can be found in the Getting Started article.

The default value includes: AArch64, AMDGPU, ARM, AVR, BPF, Hexagon, Lanai, Mips, MSP430, NVPTX, PowerPC, RISCV, Sparc, SystemZ, WebAssembly, X86, XCore.

This list is defined in the main CMakeFile (permalink).

As you can see, WebAssembly is in the list now (in 2021), so it should already be enabled by default. When the question was first asked, it was still an experimental target.

When the question was first asked, WebAssembly was still an experimental target so the rest of the answer will more generally describe how to enable any target. As an example, we'll use "Motorola 68000" instead of wasm.

"Motorola 68000" is not in the list of stable targets. We'll have to find the name used by LLVM and then use LLVM_EXPERIMENTAL_TARGETS_TO_BUILD. Unfortunately, since this variable is not documented, I wasn't able to find the list of all the targets on their website. After some trial and error, it seems that the available targets correspond to the names of the directories in /lib/Target. This directory contains a subdirectory named M68k: this is likely the name of the target.

To use LLVM for "Motorola 68000", you'll need to enable the M68k target using the LLVM_EXPERIMENTAL_TARGETS_TO_BUILD variable when preparing the build directory with CMake.


Here are the steps to compile LLVM with "Motorola 68000" support (adapt it to your own requirements). I used a Linux machine but you should be able to adapt it to your environment.

Requirements:

  • CMake
  • Git
  • GCC, CLang or Visual Studio depending on your platform
  • zlib
  1. Clone the LLVM repo. I'll use the /opt/llvm-project directory for the home directory of my custom version of LLVM (this is the last argument to the command, replace it by the path you want to use).

    git clone https://github.com/llvm/llvm-project.git /opt/llvm-project

  2. Navigate to the LLVM sources:

    cd /opt/llvm-project/llvm

  3. Create your build directory and navigate to it.

    mkdir build && cd build

  4. Use CMake to prepare your build directory. This is the step where you need take care of setting the variables. In my case I'll use LLVM_EXPERIMENTAL_TARGETS_TO_BUILD="M68k" and leave LLVM_TARGETS_TO_BUILD to its default value (all stable targets). Another important variable that I'll set is CMAKE_BUILD_TYPE=Release to get an optimized build and CMAKE_INSTALL_PREFIX=/opt/llvm-project/llvm/bin to keep this version of LLVM in its directory and do not interfere with the version I already have on my system (I'll just add this directory to the $PATH when I'll need it).

    cmake -G "Unix Makefiles" -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="M68k" -DCMAKE_INSTALL_PREFIX=/opt/llvm-project/llvm/bin -DCMAKE_BUILD_TYPE=Release /opt/llvm-project/llvm

  5. Build LLVM, this may take a while:

    cmake --build .

  6. Install LLVM:

    cmake --build . --target install


You should compile your backend from source. The only pluggable things in LLVM are passes currently.

Tags:

Llvm