3 [![Build Status](https://travis-ci.org/KhronosGroup/SPIRV-Tools.svg?branch=master)](https://travis-ci.org/KhronosGroup/SPIRV-Tools)
4 [![Build status](https://ci.appveyor.com/api/projects/status/gpue87cesrx3pi0d/branch/master?svg=true)](https://ci.appveyor.com/project/Khronoswebmaster/spirv-tools/branch/master)
8 The SPIR-V Tools project provides an API and commands for processing SPIR-V
11 The project includes an assembler, binary module parser, disassembler,
12 validator, and optimizer for SPIR-V. Except for the optimizer, all are based
13 on a common static library. The library contains all of the implementation
14 details, and is used in the standalone tools whilst also enabling integration
15 into other code bases directly. The optimizer implementation resides in its
16 own library, which depends on the core library.
18 The interfaces are still under development, and are expected to change.
20 SPIR-V is defined by the Khronos Group Inc.
21 See the [SPIR-V Registry][spirv-registry] for the SPIR-V specification,
22 headers, and XML registry.
24 ## Verisoning SPIRV-Tools
26 See [`CHANGES`](CHANGES) for a high level summary of recent changes, by version.
28 SPIRV-Tools project version numbers are of the form `v`*year*`.`*index* and with
29 an optional `-dev` suffix to indicate work in progress. For exampe, the
30 following versions are ordered from oldest to newest:
38 Use the `--version` option on each command line tool to see the software
39 version. An API call reports the software version as a C-style string.
43 ### Assembler, binary parser, and disassembler
45 * Based on SPIR-V version 1.1 Rev 3
46 * Support for extended instruction sets:
47 * GLSL std450 version 1.0 Rev 3
48 * OpenCL version 1.0 Rev 2
49 * Support for SPIR-V 1.0 (with or without additional restrictions from Vulkan 1.0)
50 * Assembler only does basic syntax checking. No cross validation of
51 IDs or types is performed, except to check literal arguments to
52 `OpConstant`, `OpSpecConstant`, and `OpSwitch`.
54 See [`syntax.md`](syntax.md) for the assembly language syntax.
58 *Warning:* The validator is incomplete.
59 Check the [CHANGES](CHANGES) file for reports on progress.
61 *Note*: The validator checks some Universal Limits, from section 2.17 of the SPIR-V spec.
62 The validator will fail on a module that exceeds those minimum upper bound limits.
63 It is future work to parameterize the validator to allow larger
64 limits accepted by a more than minimally capable SPIR-V consumer.
69 *Warning:* The optimizer is still under development.
71 Currently supported optimizations:
73 * Set spec constant default value
74 * Freeze spec constant
75 * Fold `OpSpecConstantOp` and `OpSpecConstantComposite`
77 * Eliminate dead constant
79 For the latest list with detailed documentation, please refer to
80 [`include/spirv-tools/optimizer.hpp`](include/spirv-tools/optimizer.hpp).
84 The SPIR-V Tools are maintained by members of the The Khronos Group Inc.,
85 at https://github.com/KhronosGroup/SPIRV-Tools.
87 Contributions via merge request are welcome. Changes should:
88 * Be provided under the [Apache 2.0](#license).
89 You'll be prompted with a one-time "click-through" Contributor's License
90 Agreement (CLA) dialog as part of submitting your pull request or
91 other contribution to GitHub.
92 * Include tests to cover updated functionality.
93 * C++ code should follow the [Google C++ Style Guide][cpp-style-guide].
94 * Code should be formatted with `clang-format`. Settings are defined by
95 the included [.clang-format](.clang-format) file.
97 We intend to maintain a linear history on the GitHub `master` branch.
99 ### Source code organization
101 * `example`: demo code of using SPIRV-Tools APIs
102 * `external/googletest`: Intended location for the
103 [googletest][googletest] sources, not provided
104 * `include/`: API clients should add this directory to the include search path
105 * `external/spirv-headers`: Intended location for
106 [SPIR-V headers][spirv-headers], not provided
107 * `include/spirv-tools/libspirv.h`: C API public interface
108 * `source/`: API implementation
109 * `test/`: Tests, using the [googletest][googletest] framework
110 * `tools/`: Command line executables
114 The project contains a number of tests, used to drive development
115 and ensure correctness. The tests are written using the
116 [googletest][googletest] framework. The `googletest`
117 source is not provided with this project. There are two ways to enable
119 * If SPIR-V Tools is configured as part of an enclosing project, then the
120 enclosing project should configure `googletest` before configuring SPIR-V Tools.
121 * If SPIR-V Tools is configured as a standalone project, then download the
122 `googletest` source into the `<spirv-dir>/external/googletest` directory before
123 configuring and building the project.
125 *Note*: You must use a version of googletest that includes
126 [a fix][googletest-pull-612] for [googletest issue 610][googletest-issue-610].
127 The fix is included on the googletest master branch any time after 2015-11-10.
128 In particular, googletest must be newer than version 1.7.0.
132 The project uses [CMake][cmake] to generate platform-specific build
133 configurations. Assume that `<spirv-dir>` is the root directory of the checked
138 git clone https://github.com/KhronosGroup/SPIRV-Headers.git external/spirv-headers
139 git clone https://github.com/google/googletest.git external/googletest # optional
141 mkdir build && cd build
142 cmake [-G <platform-generator>] <spirv-dir>
145 Once the build files have been generated, build using your preferred
146 development environment.
150 The following CMake options are supported:
152 * `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output.
153 * `SPIRV_SKIP_TESTS={ON|OFF}`, default `OFF`- Build only the library and
154 the command line tools. This will prevent the tests from being built.
155 * `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not
156 the command line tools and tests.
157 * `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX
158 platforms with an appropriate version of `clang` this option enables the use
159 of the sanitizers documented [here][clang-sanitizers].
160 This should only be used with a debug build.
161 * `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable
162 more strict warnings. The code might not compile with this option enabled.
163 For Clang, enables `-Weverything`. For GCC, enables `-Wpedantic`.
164 See [`CMakeLists.txt`](CMakeLists.txt) for details.
165 * `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any
166 warnings encountered by enabling the compiler-specific compiler front-end
173 The internals of the library use C++11 features, and are exposed via both a C
176 In order to use the library from an application, the include path should point
177 to `<spirv-dir>/include`, which will enable the application to include the
178 header `<spirv-dir>/include/spirv-tools/libspirv.h{|pp}` then linking against
179 the static library in `<spirv-build-dir>/source/libSPIRV-Tools.a` or
180 `<spirv-build-dir>/source/SPIRV-Tools.lib`.
181 For optimization, the header file is
182 `<spirv-dir>/include/spirv-tools/optimizer.hpp`, and the static library is
183 `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` or
184 `<spirv-build-dir>/source/SPIRV-Tools-opt.lib`.
186 * `SPIRV-Tools` CMake target: Creates the static library:
187 * `<spirv-build-dir>/source/libSPIRV-Tools.a` on Linux and OS X.
188 * `<spirv-build-dir>/source/libSPIRV-Tools.lib` on Windows.
189 * `SPIRV-Tools-opt` CMake target: Creates the static library:
190 * `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` on Linux and OS X.
191 * `<spirv-build-dir>/source/libSPIRV-Tools-opt.lib` on Windows.
195 The interfaces are still under development, and are expected to change.
197 There are five main entry points into the library in the C interface:
199 * `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module.
200 * `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to
202 * `spvBinaryParse`: The entry point to a binary parser API. It issues callbacks
203 for the header and each parsed instruction. The disassembler is implemented
204 as a client of `spvBinaryParse`.
205 * `spvValidate` implements the validator functionality. *Incomplete*
206 * `spvValidateBinary` implements the validator functionality. *Incomplete*
208 The C++ interface is comprised of two classes, `SpirvTools` and `Optimizer`,
209 both in the `spvtools` namespace.
210 * `SpirvTools` provides `Assemble`, `Disassemble`, and `Validate` methods.
211 * `Optimizer` provides methods for registering and running optimization passes.
213 ## Command line tools
215 Command line tools, which wrap the above library functions, are provided to
216 assemble or disassemble shader files. It's a convention to name SPIR-V
217 assembly and binary files with suffix `.spvasm` and `.spv`, respectively.
221 The assembler reads the assembly language text, and emits the binary form.
223 The standalone assembler is the exectuable called `spirv-as`, and is located in
224 `<spirv-build-dir>/tools/spirv-as`. The functionality of the assembler is implemented
225 by the `spvTextToBinary` library function.
227 * `spirv-as` - the standalone assembler
228 * `<spirv-dir>/tools/as`
230 Use option `-h` to print help.
232 ### Disassembler tool
234 The disassembler reads the binary form, and emits assembly language text.
236 The standalone disassembler is the executable called `spirv-dis`, and is located in
237 `<spirv-build-dir>/tools/spirv-dis`. The functionality of the disassembler is implemented
238 by the `spvBinaryToText` library function.
240 * `spirv-dis` - the standalone disassembler
241 * `<spirv-dir>/tools/dis`
243 Use option `-h` to print help.
245 The output includes syntax colouring when printing to the standard output stream,
246 on Linux, Windows, and OS X.
250 The optimizer processes a SPIR-V binary module, applying transformations
251 in the specified order.
253 This is a work in progress, with initially only few available transformations.
255 * `spirv-opt` - the standalone optimizer
256 * `<spirv-dir>/tools/opt`
260 *Warning:* This functionality is under development, and is incomplete.
262 The standalone validator is the executable called `spirv-val`, and is located in
263 `<spirv-build-dir>/tools/spirv-val`. The functionality of the validator is implemented
264 by the `spvValidate` library function.
266 The validator operates on the binary form.
268 * `spirv-val` - the standalone validator
269 * `<spirv-dir>/tools/val`
271 ### Control flow dumper tool
273 The control flow dumper prints the control flow graph for a SPIR-V module as a
274 [GraphViz](http://www.graphviz.org/) graph.
276 This is experimental.
278 * `spirv-cfg` - the control flow graph dumper
279 * `<spirv-dir>/tools/cfg`
283 * `spirv-lesspipe.sh` - Automatically disassembles `.spv` binary files for the
284 `less` program, on compatible systems. For example, set the `LESSOPEN`
285 environment variable as follows, assuming both `spirv-lesspipe.sh` and
286 `spirv-dis` are on your executable search path:
288 export LESSOPEN='| spirv-lesspipe.sh "%s"'
290 Then you page through a disassembled module as follows:
294 * The `spirv-lesspipe.sh` script will pass through any extra arguments to
295 `spirv-dis`. So, for example, you can turn off colours and friendly ID
298 export LESSOPEN='| spirv-lesspipe.sh "%s" --no-color --raw-id'
301 * `50spirv-tools.el` - Automatically disassembles '.spv' binary files when
302 loaded into the emacs text editor, and re-assembles them when saved,
303 provided any modifications to the file are valid. This functionality
304 must be explicitly requested by defining the symbol
305 SPIRV_TOOLS_INSTALL_EMACS_HELPERS as follows:
307 cmake -DSPIRV_TOOLS_INSTALL_EMACS_HELPERS=true ...
310 In addition, this helper is only installed if the directory /etc/emacs/site-start.d
311 exists, which is typically true if emacs is installed on the system.
313 Note that symbol IDs are not currently preserved through a load/edit/save operation.
314 This may change if the ability is added to spirv-as.
319 Tests are only built when googletest is found. Use `ctest` to run all the
323 <a name="future"></a>
325 ### Assembler and disassembler
327 * The disassembler could emit helpful annotations in comments. For example:
328 * Use variable name information from debug instructions to annotate
329 key operations on variables.
330 * Show control flow information by annotating `OpLabel` instructions with
331 that basic block's predecessors.
332 * Error messages could be improved.
336 This is a work in progress.
339 <a name="license"></a>
340 Full license terms are in [LICENSE](LICENSE)
342 Copyright (c) 2015-2016 The Khronos Group Inc.
344 Licensed under the Apache License, Version 2.0 (the "License");
345 you may not use this file except in compliance with the License.
346 You may obtain a copy of the License at
348 http://www.apache.org/licenses/LICENSE-2.0
350 Unless required by applicable law or agreed to in writing, software
351 distributed under the License is distributed on an "AS IS" BASIS,
352 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
353 See the License for the specific language governing permissions and
354 limitations under the License.
357 [spirv-registry]: https://www.khronos.org/registry/spir-v/
358 [spirv-headers]: https://github.com/KhronosGroup/SPIRV-Headers
359 [googletest]: https://github.com/google/googletest
360 [googletest-pull-612]: https://github.com/google/googletest/pull/612
361 [googletest-issue-610]: https://github.com/google/googletest/issues/610
362 [CMake]: https://cmake.org/
363 [cpp-style-guide]: https://google.github.io/styleguide/cppguide.html
364 [clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation