• Stars
    star
    422
  • Rank 102,753 (Top 3 %)
  • Language LLVM
  • License
    Other
  • Created over 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool and a library for bi-directional translation between SPIR-V and LLVM IR

LLVM/SPIR-V Bi-Directional Translator

Out-of-tree build & tests In-tree build & tests

This repository contains source code for the LLVM/SPIR-V Bi-Directional Translator, a library and tool for translation between LLVM IR and SPIR-V.

The LLVM/SPIR-V Bi-Directional Translator is open source software. You may freely distribute it under the terms of the license agreement found in LICENSE.txt.

Directory Structure

The files/directories related to the translator:

Build Instructions

The main branch of this repo is aimed to be buildable with the latest LLVM main revision.

Build with pre-installed LLVM

The translator can be built with the latest(nightly) package of LLVM. For Ubuntu and Debian systems LLVM provides repositories with nightly builds at http://apt.llvm.org/. For example the latest package for Ubuntu 16.04 can be installed with the following commands:

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo add-apt-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main"
sudo apt-get update
sudo apt-get install llvm-18-dev llvm-18-tools clang-18 libclang-18-dev

The installed version of LLVM will be used by default for out-of-tree build of the translator.

git clone https://github.com/KhronosGroup/SPIRV-LLVM-Translator.git
mkdir SPIRV-LLVM-Translator/build && cd SPIRV-LLVM-Translator/build
cmake ..
make llvm-spirv -j`nproc`

Build with pre-built LLVM

If you have a custom build (based on the latest version) of LLVM libraries you can link the translator against it.

git clone https://github.com/KhronosGroup/SPIRV-LLVM-Translator.git
mkdir SPIRV-LLVM-Translator/build && cd SPIRV-LLVM-Translator/build
cmake .. -DLLVM_DIR=<llvm_build_dir>/lib/cmake/llvm/
make llvm-spirv -j`nproc`

If the translator is used as part of another CMake project, you will need to define LLVM_SPIRV_BUILD_EXTERNAL:

cmake .. -DLLVM_DIR=<llvm_build_dir>/lib/cmake/llvm/ -DLLVM_SPIRV_BUILD_EXTERNAL=YES

Where llvm_build_dir is the LLVM build directory.

LLVM in-tree build

The translator can be built as a regular LLVM subproject. To do that you need to clone it into the llvm/projects or llvm/tools directory.

git clone https://github.com/llvm/llvm-project.git
cd llvm-project/llvm/projects
git clone https://github.com/KhronosGroup/SPIRV-LLVM-Translator.git

Run (or re-run) cmake as usual for LLVM. After that you should have llvm-spirv and check-llvm-spirv targets available.

mkdir llvm-project/build && cd llvm-project/build
cmake ../llvm -DLLVM_ENABLE_PROJECTS="clang"
make llvm-spirv -j`nproc`

Note on enabling the clang project: there are tests in the translator that depend on clang binary, which makes clang a required dependency (search for LLVM_SPIRV_TEST_DEPS in test/CMakeLists.txt) for check-llvm-spirv target.

Building clang from sources takes time and resources and it can be avoided:

  • if you are not interested in launching unit-tests for the translator after build, you can disable generation of test targets by passing -DLLVM_SPIRV_INCLUDE_TESTS=OFF option.
  • if you are interested in launching unit-tests, but don't want to build clang you can pass -DSPIRV_SKIP_CLANG_BUILD cmake option to avoid adding clang as dependency for check-llvm-spirv target. However, LIT will search for clang binary when tests are launched and it should be available at this point.
  • building and testing completely without clang is not supported at the moment, see KhronosGroup/SPIRV-LLVM-Translator#477 to track progress, discuss and contribute.

Build with SPIRV-Tools

The translator can use SPIRV-Tools to generate assembly with widely adopted syntax. If SPIRV-Tools have been installed prior to the build it will be detected and used automatically. However it is also possible to enable use of SPIRV-Tools from a custom location using the following instructions:

  1. Checkout, build and install SPIRV-Tools using the following instructions. Example using CMake with Ninja:
cmake -G Ninja <SPIRV-Tools source location> -DCMAKE_INSTALL_PREFIX=<SPIRV-Tools installation location>
ninja install
  1. Point pkg-config to the SPIR-V tools installation when configuring the translator by setting PKG_CONFIG_PATH=<SPIRV-Tools installation location>/lib/pkgconfig/ variable before the cmake line invocation. Example:
PKG_CONFIG_PATH=<SPIRV-Tools installation location>/lib/pkgconfig/ cmake <other options>

To verify the SPIR-V Tools integration in the translator build, run the following line

llvm-spirv --spirv-tools-dis input.bc -o -

The output should be printed in the standard assembly syntax.

Configuring SPIR-V Headers

The translator build is dependent on the official Khronos header file spirv.hpp that maps SPIR-V extensions, decorations, instructions, etc. onto numeric tokens. The official header version is available at KhronosGroup/SPIRV-Headers. There are several options for accessing the header file:

  • By default, the header file repository will be downloaded from Khronos Group GitHub and put into <build_dir>/SPIRV-Headers.
  • If you are building the translator in-tree, you can manually download the SPIR-V Headers repo into llvm/projects - this location will be automatically picked up by the LLVM build scripts. Make sure the folder retains its default naming in that of SPIRV-Headers.
  • Any build type can also use an external installation of SPIR-V Headers - if you have the headers downloaded somewhere in your system and want to use that version, simply extend your CMake command with -DLLVM_EXTERNAL_PROJECTS="SPIRV-Headers" -DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=</path/to/headers_dir>.

Test instructions

All tests related to the translator are placed in the test directory. A number of the tests require spirv-as (part of SPIR-V Tools) to run, but the remainder of the tests can still be run without this. Optionally the tests can make use of spirv-val (part of SPIRV-Tools) in order to validate the generated SPIR-V against the official SPIR-V specification.

In case tests are failing due to SPIRV-Tools not supporting certain SPIR-V features, please get an updated package. The PKG_CONFIG_PATH environmental variable can be used to let cmake point to a custom installation.

Execute the following command inside the build directory to run translator tests:

make test

This requires that the -DLLVM_SPIRV_INCLUDE_TESTS=ON argument is passed to CMake during the build step. Additionally, -DLLVM_EXTERNAL_LIT="/usr/lib/llvm-18/build/utils/lit/lit.py" is needed when building with a pre-installed version of LLVM.

The translator test suite can be disabled by passing -DLLVM_SPIRV_INCLUDE_TESTS=OFF to CMake.

Run Instructions for llvm-spirv

To translate between LLVM IR and SPIR-V:

  1. Execute the following command to translate input.bc to input.spv

    llvm-spirv input.bc
    
  2. Execute the following command to translate input.spv to input.bc

    llvm-spirv -r input.spv
    

    Recommended options:

    • -spirv-target-env - to specify target version of OpenCL builtins to translate to (default CL1.2)
  3. Other options accepted by llvm-spirv

    • -o file_name - to specify output name
    • -spirv-debug - output debugging information
    • -spirv-text - read/write SPIR-V in an internal textual format for debugging purpose. The textual format is not defined by SPIR-V spec.
    • --spirv-tools-dis - print SPIR-V assembly in SPIRV-Tools format. Only available on builds with SPIRV-Tools.
    • -help - to see full list of options

Translation from LLVM IR to SPIR-V and then back to LLVM IR is not guaranteed to produce the original LLVM IR. In particular, LLVM intrinsic call instructions may get replaced by function calls to OpenCL builtins and metadata may be dropped.

Handling SPIR-V versions generated by the translator

There is one option to control the behavior of the translator with respect to the version of the SPIR-V file which is being generated/consumed.

  • -spirv-max-version= - this option allows restricting the SPIRV-LLVM-Translator not to generate a SPIR-V with a version which is higher than the one specified via this option.

    If the -r option was also specified, the SPIRV-LLVM-Translator will reject the input file and emit an error if the SPIR-V version in it is higher than one specified via this option.

Allowed values are 1.0, 1.1, 1.2, 1.3, and 1.4.

More information can be found in SPIR-V versions and extensions handling

Handling SPIR-V extensions generated by the translator

By default, during SPIR-V generation, the translator doesn't use any extensions. However, during SPIR-V consumption, the translator accepts input files that use any known extensions.

If certain extensions are required to be enabled or disabled, the following command line option can be used:

  • --spirv-ext= - this options allows controlling which extensions are allowed/disallowed

Valid value for this option is comma-separated list of extension names prefixed with + or - - plus means allow to use extension, minus means disallow to use extension. There is one more special value which can be used as extension name in this option: all - it affects all extension which are known to the translator.

If --spirv-ext contains the name of an extension which is not known for the translator, it will emit an error.

More information can be found in SPIR-V versions and extensions handling

Branching strategy

Code on the main branch in this repository is intended to be compatible with the main branch of the llvm project. That is, for an OpenCL kernel compiled to llvm bitcode by the latest git revision of Clang it should be possible to translate it to SPIR-V with the llvm-spirv tool.

All new development should be done on the main branch.

To have versions compatible with released versions of LLVM and Clang, corresponding tags are available in this repository. For example, to build the translator with LLVM 7.0.0 one should use the v7.0.0-1 tag. The 7.x releases are maintained on the llvm_release_70 branch. As a general rule, commits from the main branch may be backported to the release branches as long as they do not depend on features from a later LLVM/Clang release and there are no objections from the maintainer(s). There is no guarantee that older release branches are proactively kept up to date with main, but you can request specific commits on older release branches by creating a pull request or raising an issue on GitHub.

More Repositories

1

glTF

glTF – Runtime 3D Asset Delivery
HTML
7,152
star
2

MoltenVK

MoltenVK is a Vulkan Portability implementation. It layers a subset of the high-performance, industry-standard Vulkan graphics and compute API over Apple's Metal graphics framework, enabling Vulkan applications to run on macOS, iOS and tvOS.
Objective-C++
4,788
star
3

Vulkan-Samples

One stop solution for all Vulkan samples
C++
4,176
star
4

glslang

Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
C++
3,009
star
5

Vulkan-Hpp

Open-Source Vulkan C++ API
C++
2,691
star
6

WebGL

The Official Khronos WebGL Repository
HTML
2,637
star
7

glTF-Sample-Models

glTF Sample Models
Mathematica
2,593
star
8

Vulkan-Docs

The Vulkan API Specification and related tools
JavaScript
2,527
star
9

UnityGLTF

Runtime glTF 2.0 Loader for Unity3D
C#
1,818
star
10

SPIRV-Cross

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
GLSL
1,748
star
11

Vulkan-Guide

One stop shop for getting started with the Vulkan API
Makefile
1,586
star
12

glTF-Blender-IO

Blender glTF 2.0 importer and exporter
Python
1,488
star
13

glTF-Sample-Viewer

Physically-Based Rendering in glTF 2.0 using WebGL
JavaScript
1,084
star
14

SPIRV-Tools

C++
1,071
star
15

glTF-Tutorials

glTF Tutorials
Python
951
star
16

KTX-Software

KTX (Khronos Texture) Library and Tools
C++
866
star
17

Vulkan-Headers

Vulkan header files and API registry
C++
838
star
18

glTF-Blender-Exporter

Moved to https://github.com/KhronosGroup/glTF-Blender-IO.
Python
836
star
19

Vulkan-ValidationLayers

Vulkan Validation Layers (VVL)
C++
739
star
20

Khronosdotorg

Website resource pages for Khronos.org. Community is encouraged to help keep up-to-date
HTML
716
star
21

OpenXR-SDK

Generated headers and sources for OpenXR loader.
C++
688
star
22

OpenGL-Registry

OpenGL, OpenGL ES, and OpenGL ES-SC API and Extension Registry
C
683
star
23

SPIRV-Reflect

SPIRV-Reflect is a lightweight library that provides a C/C++ reflection API for SPIR-V shader bytecode in Vulkan applications.
C
665
star
24

OpenCOLLADA

C++
652
star
25

OpenCL-Headers

Khronos OpenCL-Headers
C
620
star
26

OpenCL-SDK

OpenCL SDK
C++
539
star
27

OpenXR-SDK-Source

Sources for OpenXR loader, basic API layers, and example code.
Python
518
star
28

SyclParallelSTL

Open Source Parallel STL implementation
C++
515
star
29

Vulkan-Loader

Vulkan Loader
C
508
star
30

COLLADA2GLTF

COLLADA to glTF converter
C++
499
star
31

VK-GL-CTS

Khronos Vulkan, OpenGL, and OpenGL ES Conformance Tests
C++
464
star
32

Vulkan-Samples-Deprecated

Vulkan sample code
454
star
33

Vulkan-LoaderAndValidationLayers

**Deprecated repository** for Vulkan loader and validation layers
C++
414
star
34

OpenGL-Refpages

OpenGL and OpenGL ES reference page sources, and generated HTML used as backing store for khronos.org
HTML
392
star
35

OpenCL-CLHPP

Khronos OpenCL-CLHPP
C++
329
star
36

glTF-Validator

Tool to validate glTF assets.
Dart
321
star
37

OpenCL-Docs

OpenCL API, OpenCL C, Extensions, SPIR-V Environment Specs, Ref page, and C++ for OpenCL doc sources.
Python
296
star
38

OpenCL-Guide

A guide to help developers get up and running quickly with the OpenCL programming framework
CMake
295
star
39

GLSL

GLSL Shading Language Issue Tracker
282
star
40

Vulkan-Tools

Vulkan Utilities and Tools
C++
270
star
41

SPIRV-LLVM

This project is no longer active. Please join us at
C++
259
star
42

SPIRV-Headers

SPIRV-Headers
C++
229
star
43

OpenCL-ICD-Loader

The OpenCL ICD Loader project.
C
226
star
44

NNEF-Tools

The NNEF Tools repository contains tools to generate and consume NNEF documents
Python
220
star
45

ANARI-SDK

ANARI Software Development Kit (SDK)
C++
207
star
46

WebGLDeveloperTools

JavaScript
200
star
47

glTF-CSharp-Loader

C# Reference Loader for glTF
C#
193
star
48

SPIR

C++
178
star
49

glTF-Asset-Generator

Tool for generating various glTF assets for importer validation
C#
160
star
50

OpenCL-CTS

The OpenCL Conformance Tests
C++
149
star
51

Vulkan-Ecosystem

Public repository for Vulkan Ecosystem issues
134
star
52

WebGLNext-Proposals

Proposals for the design of the WebGL Next API.
WebIDL
132
star
53

OpenXR-Docs

OpenXR Specification sources and related material
Python
128
star
54

Vulkan-ExtensionLayer

Layer providing Vulkan features when native support is unavailable
C
128
star
55

SYCL-Docs

SYCL Open Source Specification
JavaScript
114
star
56

libclcxx

OpenCL specific C++ libraries implemented in C++ for OpenCL kernel language published in releases of OpenCL-Docs
109
star
57

SPIRV-Registry

SPIR-V specs
HTML
108
star
58

SPIRV-Guide

One stop shop for getting started with SPIR-V.
104
star
59

OpenCL-Registry

OpenCL API and Extension Registry.
HTML
100
star
60

openvx-samples

OpenVX Samples to use with any conformant implementation of OpenVX
C++
99
star
61

Vulkan-MemoryModel

Vulkan Memory Model
C++
98
star
62

3D-Formats-Guidelines

Guidelines for artists and developers using Khronos Group 3D formats.
98
star
63

EGL-Registry

EGL API and Extension Registry
HTML
98
star
64

glTF-IBL-Sampler

Sampler to create the glTF sample environments
C++
89
star
65

LLVM-SPIRV-Backend

An LLVM backend generating SPIR-V binary.
86
star
66

Vulkan-Profiles

Vulkan Profiles Tools
C++
84
star
67

glTF-External-Reference

glTF Experience Format (glXF)
73
star
68

Basis-Universal-Transcoders

A collection of optimized WebAssembly transcoders for Basis Universal compressed GPU texture formats.
WebAssembly
72
star
69

Vulkan-Samples-Assets

Vulkan Samples Assets
72
star
70

KTX-Specification

KTX file format source
CSS
70
star
71

OpenXR-Registry

Registry of OpenXR Specifications and related material
HTML
70
star
72

glTF-Sample-Environments

glTF sample environments for the glTF Sample Viewer
Batchfile
68
star
73

glTF-Project-Explorer

Tool to provide a filterable registry of glTF community projects.
TypeScript
67
star
74

3DC-Asset-Creation

Asset creation guidelines and workflows to streamline the creation of 3D digital content for use in e-commerce
Shell
66
star
75

SYCL-CTS

SYCL Conformance Tests
C++
61
star
76

OpenXR-CTS

Conformance test suite for OpenXR
C++
56
star
77

Vulkan-Utility-Libraries

Utility libraries for Vulkan developers
C++
55
star
78

siggraph2012course

Presentations for SIGGRAPH 2012 course "Graphics Programming on the Web" covering HTML5 technologies (Canvas, CSS, etc.), WebGL and WebCL
HTML
53
star
79

WebCL

The Official Khronos WebCL Repository
HTML
50
star
80

ToneMapping

A collection of tone mappers for the display of 3D graphics
JavaScript
47
star
81

OpenVX-Registry

OpenVX API and Extension Registry.
HTML
44
star
82

SPIR-Tools

SPIR-Tools
C
44
star
83

Vulkan-Portability

40
star
84

OpenXR-Hpp

Open-Source OpenXR C++ language projection
C++
40
star
85

OpenCL-CXX

OpenCL C++ Kernel Language Spec sources.
TeX
39
star
86

webcl-validator

WebCL Validator
C++
38
star
87

DataFormat

Khronos Data Format Specification
C
36
star
88

OpenCL-TTL

Tensor Tiling Library
C
33
star
89

OpenXR-Tutorials

OpenXR Tutorials [Work in progress, do not use to study OpenXR yet]
C++
32
star
90

WebGLPerf

WebGL performance regression tests
HTML
32
star
91

COLLADA-CTS

Welcome to the COLLADA Conformance Test Suite
Python
32
star
92

ANARI-Docs

ANARI Documentation
JavaScript
30
star
93

glTF-Compressonator

Fork of AMD GPUOpen Compressonator tool , for the purpose of further enhancing glTF support. and prototyping for ETC1S CRN, CTTF_128 Universal Formats, Transcoders, and Supercompression
C++
30
star
94

Education-Forum

content to support educators developing courses on Khronos technologies
29
star
95

khronosgroup.github.io

Visit https://github.khronos.org for a directory of all our GitHub Repositories
HTML
27
star
96

SPIRV-Visualizer

Client side only Javascript to visualize a SPIR-V Module binary
JavaScript
26
star
97

OpenVG-Docs

OpenVG Specification source
C
25
star
98

Vulkan-Site

Vulkan Documentation Project framework for integrated documentation site with spec, proposals, guide, and more
JavaScript
25
star
99

glTF-Generator-Registry

An open registry of tools that create glTF assets.
25
star
100

SYCL_Reference

SYCL Reference Manual
C++
25
star