[LLVMdev] "SPIR" – A Standard Portable IR for OpenCL Kernel Language

Ouriel, Boaz boaz.ouriel at intel.com
Thu Sep 6 14:05:58 CDT 2012

Greetings All,
I am sending this mail on behalf of the OpenCL Khronos members. 

**** Introduction ****
Lately, Khronos has ratified a new provisional specification which is called SPIR. 
This specification standardizes an intermediate representation for the OpenCL kernel language.
It is based on LLVM infrastructure and this is why I am sending this mail to the LLVM mailing list. 
Khronos members would like to initiate a review on the specification with the LLVM community. 

**** What is SPIR? ****
The SPIR specification standardizes an intermediate representation for OpenCL programs, 
which a hypothetical frontend can target to generate binaries that can be consumed and executed by OpenCL drivers supporting SPIR. 
The SPIR specification, however, does not standardize the design and implementation of such a frontend.

**** SPIR and LLVM ****
Khronos members chose SPIR to be layered on top of LLVM. 
Why?  Portability is a key goal of SPIR, and LLVM has proven to be highly portable, given its many backends. 
Defining a robust IR for OpenCL from scratch is difficult and requires skills which are not the core competency of the OpenCL Khronos members. 
In addition, after the IR is defined, implementing the necessary SW stack around it is a huge investment.  LLVM thus provides a time-to-market advantage for SPIR.  
Today, many of the OpenCL vendors base their technology on LLVM. This makes LLVM IR the de facto OpenCL IR and the immediate candidate to be considered by the Khronos members. 
An analysis showed that LLVM IR has its limitations but in general provides a very good solution for SPIR. 

**** Minimal Changes to LLVM ****
When defining SPIR, Khronos set a goal to keep the changes in LLVM minimal. 
Most of the changes made during prototyping were in the frontends that the different OpenCL Khronos members used.
The only changes required by SPIR in LLVM are a new target for SPIR, a new calling convention for regular OpenCL functions, and another one for OpenCL kernels. 
LLVM IR language definition remains unmodified.

**** Why is SPIR important for OpenCL? ****
SPIR offers binary portability between OpenCL implementations, and a stable target for 3rd party compilers without having to go through OpenCL "C".  

Binary compatibility simplifies the support burden for developers delivering applications that use OpenCL.  
The same application can be delivered in fully binary form and work across existing and future OpenCL implementations supporting SPIR. 
This helps the entire OpenCL ecosystem.

Generally speaking OpenCL is a JIT environment and as such deserves and requires an intermediate representation like other major JIT environments already have.

Also, some developers using OpenCL have requested portability at binary level.  Today OpenCL offers portability only at the source level with OpenCL “C”. 
They are concerned with protecting their IP by meeting “Digital Millennium Copyright Act” requirements. 
Today, those companies are forced to distribute their OpenCL code using device specific binaries. This leads to many difficulties for SW developers and end users.
 In addition, the binaries are not guaranteed to be functionally working as new devices and vendors appear in the market. 
This constraint places OpenCL standard in a disadvantage compared to other standards which already have a portable binary distribution form. 

More information about the LLVMdev mailing list