Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable a LLVM backend?

Tags:

llvm

I am using Archlinux and installed LLVM using the official package (using pacman -S llvm). I'd like to use it with the wasm-32 backend (available according to the source code).

However, this backend is not enabled on my computer:

$ llc --version
LLVM (http://llvm.org/):
  LLVM version 5.0.0
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: skylake

  Registered Targets:
    aarch64    - AArch64 (little endian)
    aarch64_be - AArch64 (big endian)
    amdgcn     - AMD GCN GPUs
    arm        - ARM
    arm64      - ARM64 (little endian)
    armeb      - ARM (big endian)
    bpf        - BPF (host endian)
    bpfeb      - BPF (big endian)
    bpfel      - BPF (little endian)
    hexagon    - Hexagon
    lanai      - Lanai
    mips       - Mips
    mips64     - Mips64 [experimental]
    mips64el   - Mips64el [experimental]
    mipsel     - Mipsel
    msp430     - MSP430 [experimental]
    nvptx      - NVIDIA PTX 32-bit
    nvptx64    - NVIDIA PTX 64-bit
    ppc32      - PowerPC 32
    ppc64      - PowerPC 64
    ppc64le    - PowerPC 64 LE
    r600       - AMD GPUs HD2XXX-HD6XXX
    sparc      - Sparc
    sparcel    - Sparc LE
    sparcv9    - Sparc V9
    systemz    - SystemZ
    thumb      - Thumb
    thumbeb    - Thumb (big endian)
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64
    xcore      - XCore

How can I enable LLVM backends?

like image 618
Demurgos Avatar asked Oct 24 '17 08:10

Demurgos


People also ask

How does LLVM backend work?

The backend of LLVM features a target-independent code generator that may create output for several types of target CPUs — including X86, PowerPC, ARM, and SPARC. The backend may also be used to generate code targeted at SPUs of the Cell processor or GPUs to support the execution of compute kernels.

How do I enable LLVM in mono?

The llvm back end can be enabled by passing --enable-llvm=yes or --with-llvm=<llvm prefix> to configure.


1 Answers

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

like image 103
Demurgos Avatar answered Sep 17 '22 22:09

Demurgos