如何启用 LLVM 后端?
How to enable a LLVM backend?
我正在使用 Archlinux 并使用官方软件包安装了 LLVM(使用 pacman -S llvm
)。
我想将它与 wasm-32
后端 (available according to the source code) 一起使用。
但是,我的计算机上没有启用此后端:
$ 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
如何启用 LLVM 后端?
您应该从源代码编译您的后端。目前 LLVM 中唯一可插入的东西是传递。
编辑 (2021-07-23):此答案已更新为使用“Motorola 68000”而不是“WebAssembly”作为示例实验目标(因为 Wasm 现在稳定).
LLVM 一旦构建就不是很容易配置。
如果您需要超出默认值的配置,您必须自己编译 LLVM。
LLVM 有几篇文章解释了如何编译它,但没有具体描述
如何启用其他目标:
启用的目标由调用 CMake 时需要定义的两个变量控制
准备构建目录:LLVM_TARGETS_TO_BUILD
和 LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
.
LLVM_TARGETS_TO_BUILD
仅控制 稳定的 目标。
您可以使用特殊值 all
来启用所有 stable 目标,或者提供一个以分号分隔的目标列表,例如 ARM;PowerPC;X86
。有 an old request
将特殊值重命名为 stable
并将 all
用于 all 目标。
它的默认值为 all
(请参阅下面的目标列表)。
LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
是一个未记录(或隐藏得很好)的变量,它允许您
启用您想要的任何目标。这也是一个以分号分隔的目标列表。
启用的目标将对应于两个列表的并集。
现在,您需要找出目标的实际名称,以及它是稳定目标还是实验目标。
可以找到稳定目标列表 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
.
此列表在 the main CMakeFile (permalink) 中定义。
如您所见,WebAssembly
现在(2021 年)在列表中,因此它应该已经默认启用。刚问的时候还是个实验目标
第一次问这个问题时,WebAssembly
仍然是一个实验目标,所以答案的其余部分将更笼统地描述如何启用任何目标。例如,我们将使用“Motorola 68000”而不是 wasm。
“Motorola 68000”不在稳定目标列表中。我们必须找到 LLVM 使用的名称,然后使用 LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
。
不幸的是,由于没有记录这个变量,我无法在他们的网站上找到 all 目标的列表。
经过反复试验,似乎 可用目标对应于 /lib/Target
中的目录名称。此目录包含一个名为 M68k
的子目录:这可能是目标的名称。
要将 LLVM 用于“摩托罗拉 68000”,您需要使用 LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
启用 M68k
目标
使用 CMake 准备构建目录时的变量。
以下是编译支持“Motorola 68000”的 LLVM 的步骤(根据您自己的要求进行调整)。我用的是 Linux 机器
但你应该能够使它适应你的环境。
要求:
- CMake
- Git
- GCC、CLang 或 Visual Studio 取决于您的平台
- zlib
克隆 LLVM 存储库。我将使用 /opt/llvm-project
目录作为主目录
我自定义版本的 LLVM(这是命令的最后一个参数,将其替换为您要使用的路径)。
git clone https://github.com/llvm/llvm-project.git /opt/llvm-project
导航到 LLVM 源:
cd /opt/llvm-project/llvm
创建您的构建目录并导航到它。
mkdir build && cd build
使用 CMake 准备构建目录。这是您需要注意的步骤
设置变量。在我的例子中,我将使用 LLVM_EXPERIMENTAL_TARGETS_TO_BUILD="M68k"
和
将 LLVM_TARGETS_TO_BUILD
保留为其默认值(所有稳定目标)。
我将设置的另一个重要变量是 CMAKE_BUILD_TYPE=Release
以获得优化的构建和
CMAKE_INSTALL_PREFIX=/opt/llvm-project/llvm/bin
将此版本的 LLVM 保留在其目录中并执行
不干扰我系统上已有的版本(我只是将此目录添加到 $PATH
当我需要它时)。
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
构建 LLVM,这可能需要一段时间:
cmake --build .
安装LLVM:
cmake --build . --target install
您的 llc --version 命令表明您安装了 LLVM 版本 5.0.0。 WASM 直到 LLVM 8.0.0 才集成到 LLVM 中。在那之前是实验性的。
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
我正在使用 Archlinux 并使用官方软件包安装了 LLVM(使用 pacman -S llvm
)。
我想将它与 wasm-32
后端 (available according to the source code) 一起使用。
但是,我的计算机上没有启用此后端:
$ 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
如何启用 LLVM 后端?
您应该从源代码编译您的后端。目前 LLVM 中唯一可插入的东西是传递。
编辑 (2021-07-23):此答案已更新为使用“Motorola 68000”而不是“WebAssembly”作为示例实验目标(因为 Wasm 现在稳定).
LLVM 一旦构建就不是很容易配置。 如果您需要超出默认值的配置,您必须自己编译 LLVM。
LLVM 有几篇文章解释了如何编译它,但没有具体描述 如何启用其他目标:
启用的目标由调用 CMake 时需要定义的两个变量控制
准备构建目录:LLVM_TARGETS_TO_BUILD
和 LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
.
LLVM_TARGETS_TO_BUILD
仅控制 稳定的 目标。 您可以使用特殊值all
来启用所有 stable 目标,或者提供一个以分号分隔的目标列表,例如ARM;PowerPC;X86
。有 an old request 将特殊值重命名为stable
并将all
用于 all 目标。 它的默认值为all
(请参阅下面的目标列表)。LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
是一个未记录(或隐藏得很好)的变量,它允许您 启用您想要的任何目标。这也是一个以分号分隔的目标列表。
启用的目标将对应于两个列表的并集。
现在,您需要找出目标的实际名称,以及它是稳定目标还是实验目标。 可以找到稳定目标列表 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
.
此列表在 the main CMakeFile (permalink) 中定义。
如您所见,WebAssembly
现在(2021 年)在列表中,因此它应该已经默认启用。刚问的时候还是个实验目标
第一次问这个问题时,WebAssembly
仍然是一个实验目标,所以答案的其余部分将更笼统地描述如何启用任何目标。例如,我们将使用“Motorola 68000”而不是 wasm。
“Motorola 68000”不在稳定目标列表中。我们必须找到 LLVM 使用的名称,然后使用 LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
。
不幸的是,由于没有记录这个变量,我无法在他们的网站上找到 all 目标的列表。
经过反复试验,似乎 可用目标对应于 /lib/Target
中的目录名称。此目录包含一个名为 M68k
的子目录:这可能是目标的名称。
要将 LLVM 用于“摩托罗拉 68000”,您需要使用 LLVM_EXPERIMENTAL_TARGETS_TO_BUILD
启用 M68k
目标
使用 CMake 准备构建目录时的变量。
以下是编译支持“Motorola 68000”的 LLVM 的步骤(根据您自己的要求进行调整)。我用的是 Linux 机器 但你应该能够使它适应你的环境。
要求:
- CMake
- Git
- GCC、CLang 或 Visual Studio 取决于您的平台
- zlib
克隆 LLVM 存储库。我将使用
/opt/llvm-project
目录作为主目录 我自定义版本的 LLVM(这是命令的最后一个参数,将其替换为您要使用的路径)。git clone https://github.com/llvm/llvm-project.git /opt/llvm-project
导航到 LLVM 源:
cd /opt/llvm-project/llvm
创建您的构建目录并导航到它。
mkdir build && cd build
使用 CMake 准备构建目录。这是您需要注意的步骤 设置变量。在我的例子中,我将使用
LLVM_EXPERIMENTAL_TARGETS_TO_BUILD="M68k"
和 将LLVM_TARGETS_TO_BUILD
保留为其默认值(所有稳定目标)。 我将设置的另一个重要变量是CMAKE_BUILD_TYPE=Release
以获得优化的构建和CMAKE_INSTALL_PREFIX=/opt/llvm-project/llvm/bin
将此版本的 LLVM 保留在其目录中并执行 不干扰我系统上已有的版本(我只是将此目录添加到 $PATH 当我需要它时)。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
构建 LLVM,这可能需要一段时间:
cmake --build .
安装LLVM:
cmake --build . --target install
您的 llc --version 命令表明您安装了 LLVM 版本 5.0.0。 WASM 直到 LLVM 8.0.0 才集成到 LLVM 中。在那之前是实验性的。
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