SPIR-V is a better SPIR with Intel® OpenCL™ Code Builder

Download the pdf version of the article

Table of Contents


In this short tutorial we are going to give you a brief introduction to Khronos SPIR-V, touch on the differences between a SPIR-V binary and a SPIR binary, and then are going to demonstrate couple of ways of creating SPIR-V binaries using tools shipped with the latest Intel® SDK for OpenCL Applications and way of consuming SPIR-V binaries in your OpenCL program. Please read Using SPIR for Fun and Profit with Intel® OpenCL™ Code Builder first, since you can consider this article as the second in a series. You will need Microsoft Visual Studio 2015 or later (note, that Community edition should work just fine) and Intel® SDK for OpenCL Applications for Windows.

What is SPIR-V and how is it different from SPIR?

SPIR-V is an evolution of the SPIR standard. You could read more about it at Khronos website at https://www.khronos.org/spir . While SPIR was designed as an intermediate binary representation for OpenCL programs only, SPIR-V acts as a cross-API intermediate language for both OpenCL 2.1 and 2.2 and the new Khronos Vulkan graphics and compute API.

How to produce SPIR-V binary with an Intel® OpenCL* command line compiler?

Run the following from the command line to produce a 64-bit SPIR-V binary:

ioc64 -cmd=build -input=SobelKernels.cl -spirv64=SobelKernels_x64.spirv

To produce a 32-bit SPIR-V binary, use the following command line:

ioc64 -cmd=build -input=SobelKernels.cl -spirv32=SobelKernels_x86.spirv

How to produce SPIR-V binary with Intel® Code Builder?

Start Microsoft Visual Studio and create a new Code Builder Session:

and give the location of the sample code and the SobelKernels.cl file. Make sure to name the session SobelKernels:

Click on the resulting Session in the Code Builder Session Explorer and select Build:

Once you build the session, you should see the following:

Note, that both x86 and x64 SPIR-V binaries and their textual representations are produced by default. The textual representation is useful for informational purposes only. It should match the representation produced by the Khronos SPIR-V tools. SPIR-V binaries could be distributed with the applications and replace SPIR binaries and/or OpenCL source code.

How to produce SPIR-V binary right in your Microsoft Visual Studio Project?

Make sure to set the following flags in your MS Visual Studio Project:

Resulting SPIR-V binaries (both 32-bit and 64-bit versions) will be generated in the project build directory.

How to consume SPIR-V binary in your OpenCL program?

Currently, only Experimental OpenCL 2.1 CPU Only Platform supports SPIR-V binaries on Intel® processors. To make sure that the device supports SPIR-V, use clGetDeviceInfo call and query for CL_DEVICE_IL_VERSION. It should return “SPIR-V_1.0”. Note that the device version should be “OpenCL 2.1” or higher.

You will need to read a binary file (in our case SobelKernels_x64.spirv) into a character array with regular C or C++ APIs. Then you need to create a program with clCreateProgramWithIL call. You will then need to build the program with clBuildProgram and provide regular optimization flags, like “-cl-mad-enable”. For a description of how to build a regular SPIR or a native device binary please see Using SPIR for fun and profit with Intel® OpenCL™ Code Builder.

Advantages of a SPIR-V binary

You should be able to compile kernels written for OpenCL 1.2, 2.0, 2.1, and 2.2 in both OpenCL C and OpenCL C++ (coming soon). Other advantages include increase compatibility between compiler frontends and across vendors; some degree of security, since it is fairly hard to disassemble the SPIR-V binary and understand the resulting code; marginally faster compile time.

Disadvantages of a SPIR-V binary

SPIR-V binary is larger than a corresponding SPIR binary. Since SPIR-V is a relatively new feature, it might not be supported by as many vendors yet. As with SPIR binaries, SPIR-V binaries need to ship in both 32-bit and 64-bit form and it is the responsibility of the programmer to write the logic to select between them. 32-bit SPIR-V binaries are needed for 32-bit devices (both CPU and GPU could be both a 32-bit device or a 64-bit device - to figure out which one you have, please use clGetDeviceInfo to query CL_DEVICE_ADDRESS_BITS property as shown in the sample code), 64-bit SPIR-V binaries are needed for 64-bit devices. Another issue is that extensions or anything else that’s discoverable at compile time can be problematic.

Building and Running a SPIR-V Sample

Building SPIR-V sample is very similar to building a SPIR sample, but when running it, specify the following on the command line:

.\x64\Release\Sobel_OCL.exe 100 cpu Experimental 2048 2048 no_show_CL spirv

The result of running it should be 

Notice, that four *_validation.ppm files appeared in your directory. When you examine them, they all should contain the following picture of a nicely Sobelized dog:


In this short tutorial we gave a brief overview of SPIR-V and its differences with SPIR. For an article on how to use SPIR, please see Using SPIR for Fun and Profit with Intel® OpenCL™ Code Builder.


Thank you to Uri Levy, Oleg Maslov, Alexey Sotkin, Vladimir Lazarev and Ben Ashbaugh for reviewing the sample code and this article!


  1. Khronos SPIR website: https://www.khronos.org/spir
  2. Khronos SPIR FAQ: https://www.khronos.org/faq/spir
  3. Khronos SPIR-V Tools: https://github.com/KhronosGroup/SPIRV-Tools
  4. Khronos LLVM framework with SPIR-V support: https://github.com/KhronosGroup/SPIRV-LLVM
  5. Intel® SDK for OpenCL Applications: https://software.intel.com/en-us/intel-opencl
  6. Intel® Media Server Studio: https://software.intel.com/en-us/intel-media-server-studio
  7. Intel®  Code Builder for OpenCL™ API for Linux*: https://software.intel.com/en-us/articles/intel-code-builder-for-opencl-api
  8. User Manual for OpenCL™ Code Builder: https://software.intel.com/en-us/code-builder-user-manual
  9. Using SPIR for Fun and Profit with Intel® OpenCL™ Code Builder by Robert Ioffe

About the Author

Robert Ioffe is a Technical Consulting Engineer at Intel’s Software and Solutions Group. He is an expert in OpenCL programming and OpenCL workload optimization on Intel® Iris™ and Intel® Iris Pro™ Graphics with deep knowledge of Intel Graphics Hardware. He was heavily involved in Khronos standards work, focusing on prototyping the latest features and making sure they can run well on Intel architecture. Most recently he has been working on prototyping Nested Parallelism (enqueue_kernel functions) feature of OpenCL 2.0 and wrote a number of samples that demonstrate Nested Parallelism functionality, including GPU-Quicksort for OpenCL 2.0. He also recorded and released two Optimizing Simple OpenCL Kernels videos and a video on Nested Parallelism.

You might also be interested in the following:

Optimizing Simple OpenCL Kernels: Modulate Kernel Optimization

Optimizing Simple OpenCL Kernels: Sobel Kernel Optimization

GPU-Quicksort in OpenCL 2.0: Nested Parallelism and Work-Group Scan Functions

Sierpiński Carpet in OpenCL 2.0

Legal Information

* Other names and brands may be claimed as the property of others.
OpenCL and the OpenCL logo are trademarks of Apple Inc. used by permission by Khronos.
Copyright © 2016, Intel Corporation. All rights reserved.

Download the Sample

Есть файлы для загрузки, доступные на условиях лицензии Intel Sample Source Code License Agreement. Загрузить сейчас
Для получения подробной информации о возможностях оптимизации компилятора обратитесь к нашему Уведомлению об оптимизации.
Возможность комментирования русскоязычного контента была отключена. Узнать подробнее.