Offline Compilation of OpenCL Kernels into SPIR-V Using Open Source Tooling

With an increasing number of OpenCL run-times supporting ingestion of SPIR-V, OpenCL developers may wish to use offline compilation to precompile SPIR-V kernels that can be used portably across multiple OpenCL implementations. Consistently using the same front-end compiler can enhance cross-vendor deployment consistency, while reducing overall compile times and eliminating the need to ship OpenCL C source code. Kernel development may also be more streamlined as compiler error messages don't have to be queried using OpenCL API calls.

A number of open source tools have been developed in recent years that can be used together to compile OpenCL C or C++ for OpenCL kernels into SPIR-V - for example as shown below. An extensive of OpenCL tools and compiler is available in the OpenCL Resource Guide.

Using open source tools for offline compilation of OpenCL kernels into SPIR-V

If you want to try the above flow for yourself, after installing the tools, the set of commands you would use are:

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

clang -cc1 -triple spir -O0 -emit-llvm-bc -o test.bc

(ii) Converting LLVM IR into SPIR-V. Note that converting optimized IR is currently unsupported.

llvm-spirv test.bc -o test.spv

(iii) There are several options for optimizing the kernels through this flow. LLVM IR optimization can be undertaken on LLVM IR by reverse-converting from SPIR-V using spirv-llvm, however, the conversion of optimized IR into SPIR-V may sometimes have issues. Alternatively, llvm-opt can be used to optimize SPIR-V modules.

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

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

llvm-spirv app.bc -o app.spv

Alternatively, spirv-link can be used:

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

(v) Debugging support can be obtained regularly by passing -g to Clang:

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

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



Khronos® and Vulkan® are registered trademarks, and ANARI™, WebGL™, glTF™, NNEF™, OpenVX™, SPIR™, SPIR-V™, SYCL™, OpenVG™ and 3D Commerce™ are trademarks of The Khronos Group Inc. OpenXR™ is a trademark owned by The Khronos Group Inc. and is registered as a trademark in China, the European Union, Japan and the United Kingdom. OpenCL™ is a trademark of Apple Inc. and OpenGL® is a registered trademark and the OpenGL ES™ and OpenGL SC™ logos are trademarks of Hewlett Packard Enterprise used under license by Khronos. All other product names, trademarks, and/or company names are used solely for identification and belong to their respective owners.