How to enable a LLVM backend?

旧时模样 提交于 2021-02-07 06:48:42

问题


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?


回答1:


As Arrowd's message mentions, 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 a pending 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, BPF, Hexagon, Mips, MSP430, NVPTX, PowerPC, Sparc, SystemZ, X86, XCore.

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

As you can see, Wasm is not in this list. 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.

To sum up: to use LLVM for wasm, you'll need to enable the WebAssembly target using the LLVM_EXPERIMENTAL_TARGETS_TO_BUILD variable when preparing the build directory with CMake.


Here are the exact steps I used to compile my version of LLVM. I used a Linux machine but you should be able to adapt it to your environment.

Requirements:

  • CMake
  • Subversion
  • GCC, CLang or Visual Studio depending on your platform
  • zlib

    1. Checkout (clone) the LLVM repo using subversion. I'll use the /opt/llvm 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).

    svn co http://llvm.org/svn/llvm-project/llvm/trunk /opt/llvm

    1. Navigate to the LLVM sources:

    cd /opt/llvm

    1. Checkout the additional tools. clang is required but the others are optional. See Getting Started

    svn co http://llvm.org/svn/llvm-project/cfe/trunk tools/clang

    1. Create your build directory and navigate to it.

    mkdir build && cd build

    1. 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="WebAssembly" 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/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="WebAssembly" -DCMAKE_INSTALL_PREFIX=/opt/llvm/bin -DCMAKE_BUILD_TYPE=Release /opt/llvm

    1. Build LLVM, this may take a while:

    cmake --build .

    1. Install LLVM:

    cmake --build . --target install




回答2:


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




回答3:


Your llc --version command says that you installed LLVM version 5.0.0. WASM wasn't integrated into LLVM until LLVM 8.0.0. It was experimental before that.

Changes to the WebAssembly Target

The WebAssembly target is no longer “experimental”! It’s now built by default, rather than needing to be enabled with LLVM_EXPERIMENTAL_TARGETS_TO_BUILD.

The object file format and core C ABI are now considered stable. That said, the object file format has an ABI versioning capability, and one anticipated use for it will be to add support for returning small structs as multiple return values, once the underlying WebAssembly platform itself supports it. Additionally, multithreading support is not yet included in the stable ABI.

https://releases.llvm.org/8.0.1/docs/ReleaseNotes.html#changes-to-the-webassembly-target



来源:https://stackoverflow.com/questions/46905464/how-to-enable-a-llvm-backend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!