OpenCL内核源代码的离线编译

Offline Compilation of OpenCL Kernel Sources

OpenCL内核源代码的离线编译

Aside from online compilation during application execution, OpenCL kernel sources can be compiled offline into binaries that can be loaded into the drivers using special API calls (e.g. clCreateProgramWithBinary or clCreateProgramWithIL).

除了应用程序执行期间的在线编译外,OpenCL内核源代码还可以离线编译为二进制文件,这些二进制文件可以使用特殊的API调用(例如clCreateProgramWithBinary或clCreateProgramWithIL)加载到驱动程序中。

This section describes available open source tools for offline compilation of OpenCL kernels.

本节介绍用于离线编译OpenCL内核的可用开源工具。

Open Source Tools

开源工具

  • clang is a compiler front-end for the C/C++ family of languages, including OpenCL C and C++ for OpenCL. It can produce executable binaries (e.g. AMDGPU), or portable binaries (e.g. SPIR). It is part of the LLVM compiler infrastructure project, and there is information regarding OpenCL kernel language support and standard headers.
  • clang是C/C++系列语言的编译器前端,包括OpenCL C和用于OpenCL的C++。它可以生成可执行二进制文件(例如AMDGPU)或可移植二进制文件(如SPIR)。它是LLVM编译器基础设施项目的一部分,并且有关于OpenCL内核语言支持和标准头的信息。
  • SPIRV-LLVM Translator provides a library and the llvm-spirv tool for bidirectional translation between LLVM IR and SPIR-V.
  • SPIRV-LLVM翻译器提供了一个库和LLVM-SPIRV工具,用于LLVM-IR和SPIR-V之间的双向翻译。
  • clspv compiler and clvk runtime layer enable OpenCL applications to be executed with Vulkan drivers.
  • clspv编译器和clvk运行时层使OpenCL应用程序能够使用Vulkan驱动程序执行。
  • SPIR-V Tools provide a set of utilities to process SPIR-V binaries including spirv-opt optimizer, spirv-link linker, spirv-dis/spirv-as (dis-)assembler, and spirv-val validator.
  • SPIR-V工具提供了一组处理SPIR-V二进制文件的实用程序,包括spirv-opt优化器、spirv-link链接器、spirv-dis/spirv-as(dis-)汇编程序和spirv-val验证器。

Compiling Kernels to SPIR-V

将内核编译为SPIR-V

The open source tools can be used to perform full compilation from OpenCL kernel sources into SPIR-V.

开放源代码工具可用于执行从OpenCL内核源代码到SPIR-V的完整编译。



Offline compilation flow for OpenCL kernels into SPIR-V

OpenCL内核到SPIR-V的离线编译流程

When tools are used to target OpenCL drivers they operate on OpenCL flavor of SPIR-V and when tools target Vulkan drivers they operate on Vulkan flavor of SPIR-V instead. Some tools can ingest both SPIR-V dialects. However, it is generally not possible to transform or mix the modules of different SPIR-V flavors. For example, it is generally not possible to use spirv-link to link the modules in both OpenCL and Vulkan formats of SPIR-V.

当工具用于针对OpenCL驱动程序时,它们在SPIR-V的OpenCL风格上操作,而当工具针对Vulkan驱动程序时则在SPIR-V的Vulkan风格上操作。有些工具可以同时使用SPIR-V方言。然而,通常不可能转换或混合不同SPIR-V口味的模块。例如,通常不可能使用spirv链接来链接SPIR-V的OpenCL和Vulkan格式的模块。

Examples
范例

If you want to try the above compilation flow for yourself, after installing the tools, you can use the following commands.

如果想自己尝试上述编译流程,安装工具后,可以使用以下命令。

Compile for OpenCL runtime
为OpenCL运行时编译

(i) Compiling OpenCL C/C++ for OpenCL file into SPIR flavor LLVM IR (for 32 bit targets) formats:

(i) 将OpenCL文件的OpenCL C/C++编译为SPIR风格的LLVM IR(用于32位目标)格式:

clang -c -target spir -O0 -emit-llvm -o test.bc test.cl

Using -cl-std changes the language version compiled for.

使用-cl-std会更改编译的语言版本。

To compile C++ for OpenCL source pass -cl-std=CLC++ or use the file extension .clcpp.

要为OpenCL源代码编译C++,请传递-cl std=CLC++或使用文件扩展名.clcpp。

clang -cl-std=CLC++ -c -target spir -O0 -emit-llvm -o test.bc test.cl
clang -c -target spir -O0 -emit-llvm -o test.bc test.clcpp

If debugging support is needed then the -g flag can be used in the clang invocation:

如果需要调试支持,那么可以在clang调用中使用-g标志:

clang -c test.cl -target spir -o test.bc -g

Note: In clang releases up to 12.0, calling most builtin functions requires the extra flags -Xclang -finclude-default-header to be passed to clang. Refer to the release documentation for more details.

注意:在高达12.0的clang版本中,调用大多数内置函数需要将额外的标志-Xclang-finclude默认标头传递给clang。有关更多详细信息,请参阅发布文档。

(ii) Converting LLVM IR into SPIR-V.

(ii)将LLVM IR转换为SPIR-V。

llvm-spirv test.bc -o test.spv

Note: Converting IR produced with optimization levels other than -O0 is only available as an experimental feature and it is not guaranteed to work. In the majority of cases, the conversion is expected to succeed when optimizations are enabled. Developers are encouraged to file a bug report when issues are encountered. As a workaround when encountering an issue in translating modules obtained with optimizations, generate LLVM IR with optimizations disabled, and then use the stand-alone spirv-opt tool to optimize at the SPIR-V level.

注意:转换使用除-O0以外的优化级别生成的IR仅作为实验功能可用,不能保证有效。在大多数情况下,启用优化后,转换有望成功。鼓励开发人员在遇到问题时提交错误报告。当在翻译通过优化获得的模块时遇到问题时,作为一种解决方法,在禁用优化的情况下生成LLVM IR,然后使用独立的spirv-opt工具在SPIR-V级别进行优化。

(iii) Linking multiple modules can be done on LLVM IR level by passing multiple .bc files to llvm-link.

(iii)通过将多个.bc文件传递到LLVM链接,可以在LLVM IR级别上完成多个模块的链接。

llvm-link test1.bc test2.bc -o app.bc
llvm-spirv app.bc -o app.spv

Alternatively, spirv-link can be used to link SPIR-V modules of the same flavor either OpenCL or Vulkan:

或者,spirv链路可用于连接OpenCL或Vulkan相同风味的SPIR-V模块:

spirv-link -o app.spv test1.spv test2.spv

(iv) Once the SPIR-V binary is produced, it can be loaded in OpenCL applications using the clCreateProgramWithIL API call.

(iv)一旦生成SPIR-V二进制文件,就可以使用clCreateProgramWithIL API调用将其加载到OpenCL应用程序中。

Compile for Vulkan runtime
为Vulkan运行时编译

Compiling OpenCL sources directly to SPIR-V binary format:

将OpenCL源代码直接编译为SPIR-V二进制格式:

clspv test.cl -o test.spv

The SPIR-V binary files can be further optimized or linked using spirv-opt and spirv-link respectively before loading into the Vulkan runtime using vkCreateShaderModule from Vulkan API or (if clvk is used) clCreateProgramWithIL from OpenCL API.

​在使用Vulkan API的vkCreateShaderModule或(如果使用clvk)OpenCL API的clCreateProgramWithIL加载到Vulkan运行时之前,可以分别使用spirv-opt和spirv-link进一步优化或链接SPIR-V二进制文件。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值