Skip to content
Hugh Perkins edited this page Jun 26, 2017 · 20 revisions

SPIR and SPIR-V

'SPIR' is the original 'thing', that was based directly on LLVM bytecode. 'SPIR-V' is the later 'thing', that is orthogonal to llvm bytecode, but based loosely on llvm 3.2.

Version history:

In order of earlier to later:

  • SPIR 1.2
  • SPIR 2.0
  • SPIRV 1.1
  • SPIRV 1.2

Repositories

  • SPIR:
    • contains the clang compiler, that can take eg OpenCL source-code, and output SPIR-V binary form
    • be careful to choose teh appropriate branch: by default it chagnes to spir_12, which is not SPIR-V, and is pretty old
    • the branch for the latest vailable verison of SPIR-V is spirv-1.1
  • SPIRV-LLVM:
    • fork of llvm
    • contain LLVM <-> SPIR-V converter
    • serves as a foundation for LLVM-based front-end compilers targeting SPIR-V
  • SPIRV-Tools
    • contains assembler and disassembler, to convert between SPIR-V binary and textual form
    • SPIR-V validator
    • binary parser, usable from C/C++ code, to read SPIR-V binary files
  • SPIRV-Headers
    • header files for various languages.
    • JSON files describing the grammar for the SPIR-V core instruction set
    • The XML registry file
  • SPIRV-Cross
    • library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages

Building the various tools etc

I want to ...

Compile OpenCL to SPIR-V

Convert between SPIR-V and llvm bytecode, in either direction

Read SPIR-V from C++

#include "llvm/Support/SPIRV.h"
...
    llvm::LLVMContext Context;
    std::ifstream IFS("somefile.spv", std::ios::binary);
    llvm::Module *M;
    std::string Err;

    if (!llvm::ReadSPIRV(Context, IFS, M, Err)) {
        llvm::errs() << "Fails to load SPIRV as LLVM Module: " << Err << '\n';
        return;
    }

I'm not sure the difference between these, but I assume that:

  • the SPIRV-LLVM ReadSPIRV function allows one to bring the full power of llvm to bear on parsing and walking the tree
  • the SPIRV-Tools method possibly is lighter-weight, does not need one to link with llvm?