编译和使用
该项目会编译出libSPIRV-Tools-shared.so和spirv-as, spirv-dis等工具。
1. 单独编译(不推荐)¶
此种方式不讲SPIRV-Tools放于llvm/tools目录更好,
Bash
cd SPIRV-Tools
python3 utils/git-sync-deps # 从github下载依赖如spirv-headers放与external目录
cmake -B build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=install
cd build
make -j12
make install
Bash
ken@llvm-gw001m1aftf:~/workspace/SPIRV-Tools/install$ ls bin/
spirv-as spirv-cfg spirv-dis spirv-lesspipe.sh spirv-link spirv-lint spirv-opt spirv-reduce spirv-val
ken@llvm-gw001m1aftf:~/workspace/SPIRV-Tools/install$ ls lib/
cmake libSPIRV-Tools.a libSPIRV-Tools-diff.a libSPIRV-Tools-link.a libSPIRV-Tools-lint.a libSPIRV-Tools-opt.a libSPIRV-Tools-reduce.a libSPIRV-Tools-shared.so pkgconfig
ken@llvm-gw001m1aftf:~/workspace/SPIRV-Tools/install$ ls include/
spirv-tools
2 在llvm9/17中增加对SPIRV的编译支持¶
将下载好的SPIRV-Tools放于llvm/tools/spirv-tools。spirv-tools是从llvm18开始集成到llvm中的。
Diff
diff --git a/llvm/tools/CMakeLists.txt b/llvm/tools/CMakeLists.txt
index c6116ac81d12..d2046eaae1a7 100644
--- a/llvm/tools/CMakeLists.txt
+++ b/llvm/tools/CMakeLists.txt
@@ -35,6 +35,8 @@ add_llvm_tool_subdirectory(llvm-config)
add_llvm_tool_subdirectory(llvm-lto)
add_llvm_tool_subdirectory(llvm-profdata)
+add_llvm_tool_subdirectory(spirv-tools) # 编译spirv-as spirv-dis等工具
+
# Projects supported via LLVM_EXTERNAL_*_SOURCE_DIR need to be explicitly
# specified.
add_llvm_external_project(clang)
3 在llvm17中增加对SPIRV的编译支持¶
LLVM17已经集成了该项目编译LLVM时需要打开SPIRV编译,这样llc --version时会出现spirv32 - SPIR-V 32-bit, spirv64 - SPIR-V 64-bit。但依然没有llvm-tools
Bash
mkdir build
mkdir install
cmake ../llvm -DCMAKE_INSTALL_PREFIX=../install -DLLVM_ENABLE_ASSERTIONS=On -DCMAKE_BUILD_TYPE=RELEASE -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="SPIRV" -DLLVM_INCLUDE_SPIRV_TOOLS_TESTS=ON | tee cmake.log
-DLLVM_TARGETS_TO_BUILD="AArch64" 减少编译
make -j16 --trace VERBOSE=2 | tee make.log
make install | tee install.log
4. 工具的使用¶
4.1 spirv-as¶
spirv-as 是 SPIRV-Tools 中的一个工具,其用途是把文本格式的 SPIR-V 代码(即 .spvasm 文件)转换为二进制格式的 SPIR-V 代码(即 .spv 文件)。下面为你详细介绍其使用方法和命令行示例。
Bash
spirv-as - Create a SPIR-V binary module from SPIR-V assembly text
Usage: spirv-as [options] [<filename>]
The SPIR-V assembly text is read from <filename>. If no file is specified,
or if the filename is "-", then the assembly text is read from standard input.
The SPIR-V binary module is written to file "out.spv", unless the -o option
is used.
Options:
-h, --help Print this help.
-o <filename> Set the output filename. Use '-' to mean stdout.
--version Display assembler version information.
--preserve-numeric-ids
Numeric IDs in the binary will have the same values as in the
source. Non-numeric IDs are allocated by filling in the gaps,
starting with 1 and going up.
--target-env {vulkan1.1spv1.4|vulkan1.0|vulkan1.1|vulkan1.2|vulkan1.3
|spv1.0|spv1.1|spv1.2|spv1.3|spv1.4|spv1.5|spv1.6
|opencl1.2embedded|opencl1.2|opencl2.0embedded|opencl2.0
|opencl2.1embedded|opencl2.1|opencl2.2embedded|opencl2.2
|opengl4.0|opengl4.1|opengl4.2|opengl4.3|opengl4.5}
Use specified environment.
示例 1:将文本格式的 SPIR-V 代码转换为二进制格式¶
假定你有一个名为 example.spvasm 的文本格式 SPIR-V 文件,要把它转换为二进制格式的 example.spv 文件,可使用如下命令:
spvasm格式就是dis格式
Bash
spirv-as result_spv.dis --target-env spv1.5 -o test.spv
spirv-dis test.spv -o test.dis
假设result_spv.dis是1.4版本的,这样就将其 转化成了1.5版本的test.dis
示例 2:指定目标环境¶
若要将 example.spvasm 文件转换为适用于 Vulkan 1.1 环境的二进制格式,可使用以下命令: