top of page
Writer's picturejjfumero

SPIR-V Beehive Toolkit: A Java library to ease SPIR-V Code Generation

Takeaways

  • SPIR-V is an intermediate portable representation for heterogeneous computing and graphics processing.

  • SPIR-V Beehive Toolkit is a composable and functional Java library for facilitating SPIR-V code generation.

  • SPIR-V Beehive Toolkit is being used by the TornadoVM JIT compiler.


News

We have released a new library for generating SPIR-V binary code for graphics and general-purpose computing on heterogeneous hardware (e.g., for GPUs). This library has been released as an open-source project, under the MIT license, and it is hosted on GitHub.


The development of this library has been performed at the University of Manchester in collaboration with Intel to support a SPIR-V backend using the oneAPI Level-Zero API as a dispatcher and resource management.


SPIR-V is an intermediate portable representation in a binary format that can be used to express kernel functions for general purpose computing as well as graphics. Currently, it is being used by the TornadoVM JIT compiler, which generates SPIR-V code from Java bytecode. With the help of the SPIR-V Beehive toolkit library, TornadoVM can offload code from Java to run on Intel HD graphics. In addition, the library supports the full specification of SPIR-V 1.2.


In contrast to the other back-ends that TornadoVM supports (OpenCL and PTX), working at the binary level with SPIR-V is a challenging and error-prone task. For this reason, we have designed a functional and composable Java library that can be used to emit SPIR-V binary code at runtime from an object-oriented programming language using modern software engineering patterns. Furthermore, the binary emitted by this library can be disassembled with standard tools, such as spirv-dis from Khronos. This post shows a few examples that illustrate the function composition of this library.


How to use it?


To install the project:


$ git clone https://github.com/beehive-lab/spirv-beehive-toolkit.git
$ cd spirv-beehive-toolkit
$ mvn clean install

Then, you can use it by importing a maven dependency:


<dependency>
     <groupId>beehive-lab</groupId>
      <artifactId>spirv-beehive-lib</artifactId>
      <version>0.0.1</version>
 </dependency>

Some examples:


SPIRVModule module = new SPIRVModule(new SPIRVHeader(
       1,
       2,
       32,
       0,
       0));

This will generate the SPIR-V header:


; SPIR-V
; Version: 1.2
; Generator: Khronos; 32
; Bound: <BOUND> once the SPIRV is closed
; Schema: 0

For example, to add computing capabilities to a kernel:


module.add(new SPIRVOpCapability(SPIRVCapability.Addresses()));
module.add(new SPIRVOpCapability(SPIRVCapability.Linkage()));
module.add(new SPIRVOpCapability(SPIRVCapability.Kernel()));
module.add(new SPIRVOpMemoryModel(SPIRVAddressingModel.Physical64(), SPIRVMemoryModel.OpenCL()));

This configuration will generate the following SPIR-V code:


OpCapability Addresses
OpCapability Linkage
OpCapability Kernel
OpMemoryModel Physical64 OpenCL

References


311 views
bottom of page