• Home
Name Date Size #Lines LOC

..--

.github/04-Jul-2025-274206

android_test/04-Jul-2025-2912

build_overrides/04-Jul-2025-9682

cmake/04-Jul-2025-5850

docs/04-Jul-2025-482370

examples/04-Jul-2025-12286

external/04-Jul-2025-188166

include/spirv-tools/04-Jul-2025-2,638881

kokoro/04-Jul-2025-1,529961

source/04-Jul-2025-195,152132,385

test/04-Jul-2025-386,186330,718

tools/04-Jul-2025-13,40811,143

utils/04-Jul-2025-48,01042,305

.bazelrcD04-Jul-2025202 86

.bazelversionD04-Jul-20256 21

.clang-formatD04-Jul-202592 76

.gitignoreD04-Jul-2025501 3834

.gnD04-Jul-2025706 2118

BUILD.bazelD04-Jul-202516.8 KiB729662

BUILD.gnD04-Jul-202562.4 KiB1,6221,534

CHANGESD04-Jul-202583.7 KiB1,7401,680

CMakeLists.txtD04-Jul-202515.2 KiB402350

CODE_OF_CONDUCT.mdD04-Jul-2025280 21

CONTRIBUTING.mdD04-Jul-20257.1 KiB150118

DEPSD04-Jul-20251.1 KiB4126

LICENSED04-Jul-202511.1 KiB203169

MODULE.bazelD04-Jul-2025867 3427

PRESUBMIT.pyD04-Jul-20251.3 KiB4322

README.mdD04-Jul-202530.4 KiB807597

SECURITY.mdD04-Jul-2025697 147

WORKSPACED04-Jul-2025165 108

build_defs.bzlD04-Jul-20257.4 KiB235205

codereview.settingsD04-Jul-2025101 32

README.md

1# SPIR-V Tools
2[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/KhronosGroup/SPIRV-Tools/badge)](https://securityscorecards.dev/viewer/?uri=github.com/KhronosGroup/SPIRV-Tools)
3
4NEWS 2023-01-11: Development occurs on the `main` branch.
5
6## Overview
7
8The SPIR-V Tools project provides an API and commands for processing SPIR-V
9modules.
10
11The project includes an assembler, binary module parser, disassembler,
12validator, and optimizer for SPIR-V. Except for the optimizer, all are based
13on a common static library.  The library contains all of the implementation
14details, and is used in the standalone tools whilst also enabling integration
15into other code bases directly. The optimizer implementation resides in its
16own library, which depends on the core library.
17
18The interfaces have stabilized:
19We don't anticipate making a breaking change for existing features.
20
21SPIR-V is defined by the Khronos Group Inc.
22See the [SPIR-V Registry][spirv-registry] for the SPIR-V specification,
23headers, and XML registry.
24
25## Downloads
26
27The official releases for SPIRV-Tools can be found on LunarG's
28[SDK download page](https://vulkan.lunarg.com/sdk/home).
29
30For convenience, here are also links to the latest builds (HEAD).
31Those are untested automated builds. Those are not official releases, nor
32are guaranteed to work. Official releases builds are in the Vulkan SDK.
33
34<img alt="Linux" src="kokoro/img/linux.png" width="20px" height="20px" hspace="2px"/>[![Linux Build Status](https://storage.googleapis.com/spirv-tools/badges/build_status_linux_clang_release.svg)](https://storage.googleapis.com/spirv-tools/badges/build_link_linux_clang_release.html)
35<img alt="MacOS" src="kokoro/img/macos.png" width="20px" height="20px" hspace="2px"/>[![MacOS Build Status](https://storage.googleapis.com/spirv-tools/badges/build_status_macos_clang_release.svg)](https://storage.googleapis.com/spirv-tools/badges/build_link_macos_clang_release.html)
36<img alt="Windows" src="kokoro/img/windows.png" width="20px" height="20px" hspace="2px"/>[![Windows Build Status](https://storage.googleapis.com/spirv-tools/badges/build_status_windows_vs2022_release.svg)](https://storage.googleapis.com/spirv-tools/badges/build_link_windows_vs2022_release.html)
37
38[More downloads](docs/downloads.md)
39
40## Versioning SPIRV-Tools
41
42See [`CHANGES`](CHANGES) for a high level summary of recent changes, by version.
43
44SPIRV-Tools project version numbers are of the form `v`*year*`.`*index* and with
45an optional `-dev` suffix to indicate work in progress.  For example, the
46following versions are ordered from oldest to newest:
47
48* `v2016.0`
49* `v2016.1-dev`
50* `v2016.1`
51* `v2016.2-dev`
52* `v2016.2`
53
54Use the `--version` option on each command line tool to see the software
55version.  An API call reports the software version as a C-style string.
56
57## Releases
58
59The official releases for SPIRV-Tools can be found on LunarG's
60[SDK download page](https://vulkan.lunarg.com/sdk/home).
61
62You can find either the prebuilt, and QA tested binaries, or download the
63SDK Config, which lists the commits to use to build the release from scratch.
64
65GitHub releases are deprecated, and we will not publish new releases until
66further notice.
67
68## Supported features
69
70### Assembler, binary parser, and disassembler
71
72* Support for SPIR-V 1.0, through 1.5
73  * Based on SPIR-V syntax described by JSON grammar files in the
74    [SPIRV-Headers](https://github.com/KhronosGroup/SPIRV-Headers) repository.
75  * Usually, support for a new version of SPIR-V is ready within days after
76    publication.
77* Support for extended instruction sets:
78  * GLSL std450 version 1.0 Rev 3
79  * OpenCL version 1.0 Rev 2
80* Assembler only does basic syntax checking.  No cross validation of
81  IDs or types is performed, except to check literal arguments to
82  `OpConstant`, `OpSpecConstant`, and `OpSwitch`.
83* Where tools expect binary input, a hex stream may be provided instead.  See
84  `spirv-dis --help`.
85
86See [`docs/syntax.md`](docs/syntax.md) for the assembly language syntax.
87
88### Validator
89
90The validator checks validation rules described by the SPIR-V specification.
91
92Khronos recommends that tools that create or transform SPIR-V modules use the
93validator to ensure their outputs are valid, and that tools that consume SPIR-V
94modules optionally use the validator to protect themselves from bad inputs.
95This is especially encouraged for debug and development scenarios.
96
97The validator has one-sided error: it will only return an error when it has
98implemented a rule check and the module violates that rule.
99
100The validator is incomplete.
101See the [CHANGES](CHANGES) file for reports on completed work, and
102the [Validator
103sub-project](https://github.com/KhronosGroup/SPIRV-Tools/projects/1) for planned
104and in-progress work.
105
106*Note*: The validator checks some Universal Limits, from section 2.17 of the SPIR-V spec.
107The validator will fail on a module that exceeds those minimum upper bound limits.
108The validator has been parameterized to allow larger values, for use when targeting
109a more-than-minimally-capable SPIR-V consumer.
110
111See [`tools/val/val.cpp`](tools/val/val.cpp) or run `spirv-val --help` for the command-line help.
112
113### Optimizer
114
115The optimizer is a collection of code transforms, or "passes".
116Transforms are written for a diverse set of reasons:
117
118* To restructure, simplify, or normalize the code for further processing.
119* To eliminate undesirable code.
120* To improve code quality in some metric such as size or performance.
121  **Note**: These transforms are not guaranteed to actually improve any
122  given metric. Users should always measure results for their own situation.
123
124As of this writing, there are 67 transforms including examples such as:
125* Simplification
126  * Strip debug info
127  * Strip reflection info
128* Specialization Constants
129  * Set spec constant default value
130  * Freeze spec constant to default value
131  * Fold `OpSpecConstantOp` and `OpSpecConstantComposite`
132  * Unify constants
133  * Eliminate dead constant
134* Code Reduction
135  * Inline all function calls exhaustively
136  * Convert local access chains to inserts/extracts
137  * Eliminate local load/store in single block
138  * Eliminate local load/store with single store
139  * Eliminate local load/store with multiple stores
140  * Eliminate local extract from insert
141  * Eliminate dead instructions (aggressive)
142  * Eliminate dead branches
143  * Merge single successor / single predecessor block pairs
144  * Eliminate common uniform loads
145  * Remove duplicates: Capabilities, extended instruction imports, types, and
146    decorations.
147* Normalization
148  * Compact IDs
149  * CFG cleanup
150  * Flatten decorations
151  * Merge returns
152  * Convert AMD-specific instructions to KHR instructions
153* Code improvement
154  * Conditional constant propagation
155  * If-conversion
156  * Loop fission
157  * Loop fusion
158  * Loop-invariant code motion
159  * Loop unroll
160* Other
161  * Graphics robust access
162  * Upgrade memory model to VulkanKHR
163
164Additionally, certain sets of transformations have been packaged into
165higher-level recipes.  These include:
166
167* Optimization for size (`spirv-opt -Os`)
168* Optimization for performance (`spirv-opt -O`)
169
170For the latest list with detailed documentation, please refer to
171[`include/spirv-tools/optimizer.hpp`](include/spirv-tools/optimizer.hpp).
172
173For suggestions on using the code reduction options, please refer to this [white paper](https://www.lunarg.com/shader-compiler-technologies/white-paper-spirv-opt/).
174
175
176### Linker
177
178*Note:* The linker is still under development.
179
180Current features:
181* Combine multiple SPIR-V binary modules together.
182* Combine into a library (exports are retained) or an executable (no symbols
183  are exported).
184
185See the [CHANGES](CHANGES) file for reports on completed work, and the [General
186sub-project](https://github.com/KhronosGroup/SPIRV-Tools/projects/2) for
187planned and in-progress work.
188
189
190### Reducer
191
192*Note:* The reducer is still under development.
193
194The reducer simplifies and shrinks a SPIR-V module with respect to a
195user-supplied *interestingness function*.  For example, given a large
196SPIR-V module that cause some SPIR-V compiler to fail with a given
197fatal error message, the reducer could be used to look for a smaller
198version of the module that causes the compiler to fail with the same
199fatal error message.
200
201To suggest an additional capability for the reducer, [file an
202issue](https://github.com/KhronosGroup/SPIRV-Tools/issues]) with
203"Reducer:" as the start of its title.
204
205
206### Fuzzer
207
208*Note:* The fuzzer is still under development.
209
210The fuzzer applies semantics-preserving transformations to a SPIR-V binary
211module, to produce an equivalent module.  The original and transformed modules
212should produce essentially identical results when executed on identical inputs:
213their results should differ only due to floating-point round-off, if at all.
214Significant differences in results can pinpoint bugs in tools that process
215SPIR-V binaries, such as miscompilations.  This *metamorphic testing* approach
216is similar to the method used by the [GraphicsFuzz
217project](https://github.com/google/graphicsfuzz) for fuzzing of GLSL shaders.
218
219To suggest an additional capability for the fuzzer, [file an
220issue](https://github.com/KhronosGroup/SPIRV-Tools/issues]) with
221"Fuzzer:" as the start of its title.
222
223
224### Diff
225
226*Note:* The diff tool is still under development.
227
228The diff tool takes two SPIR-V files, either in binary or text format and
229produces a diff-style comparison between the two.  The instructions between the
230src and dst modules are matched as best as the tool can, and output is produced
231(in src id-space) that shows which instructions are removed in src, added in dst
232or modified between them.  The order of instructions are not retained.
233
234Matching instructions between two SPIR-V modules is not trivial, and thus a
235number of heuristics are applied in this tool.  In particular, without debug
236information, match functions is nontrivial as they can be reordered.  As such,
237this tool is primarily useful to produce the diff of two SPIR-V modules derived
238from the same source, for example before and after a modification to the shader,
239before and after a transformation, or SPIR-V produced from different tools.
240
241
242### Extras
243
244* [Utility filters](#utility-filters)
245* Build target `spirv-tools-vimsyntax` generates file `spvasm.vim`.
246  Copy that file into your `$HOME/.vim/syntax` directory to get SPIR-V assembly syntax
247  highlighting in Vim.  This build target is not built by default.
248
249## Contributing
250
251The SPIR-V Tools project is maintained by members of the The Khronos Group Inc.,
252and is hosted at https://github.com/KhronosGroup/SPIRV-Tools.
253
254Consider joining the `public_spirv_tools_dev@khronos.org` mailing list, via
255[https://www.khronos.org/spir/spirv-tools-mailing-list/](https://www.khronos.org/spir/spirv-tools-mailing-list/).
256The mailing list is used to discuss development plans for the SPIRV-Tools as an open source project.
257Once discussion is resolved,
258specific work is tracked via issues and sometimes in one of the
259[projects][spirv-tools-projects].
260
261(To provide feedback on the SPIR-V _specification_, file an issue on the
262[SPIRV-Headers][spirv-headers] GitHub repository.)
263
264See [`docs/projects.md`](docs/projects.md) to see how we use the
265[GitHub Project
266feature](https://help.github.com/articles/tracking-the-progress-of-your-work-with-projects/)
267to organize planned and in-progress work.
268
269Contributions via merge request are welcome. Changes should:
270* Be provided under the [Apache 2.0](#license).
271* You'll be prompted with a one-time "click-through"
272  [Khronos Open Source Contributor License Agreement][spirv-tools-cla]
273  (CLA) dialog as part of submitting your pull request or
274  other contribution to GitHub.
275* Include tests to cover updated functionality.
276* C++ code should follow the [Google C++ Style Guide][cpp-style-guide].
277* Code should be formatted with `clang-format`.
278  [kokoro/check-format/build.sh](kokoro/check-format/build.sh)
279  shows how to download it. Note that we currently use
280  `clang-format version 5.0.0` for SPIRV-Tools. Settings are defined by
281  the included [.clang-format](.clang-format) file.
282
283We intend to maintain a linear history on the GitHub `main` branch.
284
285### Getting the source
286
287Example of getting sources, assuming SPIRV-Tools is configured as a standalone project:
288
289    git clone https://github.com/KhronosGroup/SPIRV-Tools.git   spirv-tools
290    cd spirv-tools
291
292    # Check out sources for dependencies, at versions known to work together,
293    # as listed in the DEPS file.
294    python3 utils/git-sync-deps
295
296For some kinds of development, you may need the latest sources from the third-party projects:
297
298    git clone https://github.com/KhronosGroup/SPIRV-Headers.git spirv-tools/external/spirv-headers
299    git clone https://github.com/google/googletest.git          spirv-tools/external/googletest
300    git clone https://github.com/google/effcee.git              spirv-tools/external/effcee
301    git clone https://github.com/google/re2.git                 spirv-tools/external/re2
302    git clone https://github.com/abseil/abseil-cpp.git          spirv-tools/external/abseil_cpp
303
304#### Dependency on Effcee
305
306Some tests depend on the [Effcee][effcee] library for stateful matching.
307Effcee itself depends on [RE2][re2], and RE2 depends on [Abseil][abseil-cpp].
308
309* If SPIRV-Tools is configured as part of a larger project that already uses
310  Effcee, then that project should include Effcee before SPIRV-Tools.
311* Otherwise, SPIRV-Tools expects Effcee sources to appear in `external/effcee`,
312  RE2 sources to appear in `external/re2`, and Abseil sources to appear in
313  `external/abseil_cpp`.
314
315### Source code organization
316
317* `example`: demo code of using SPIRV-Tools APIs
318* `external/googletest`: Intended location for the
319  [googletest][googletest] sources, not provided
320* `external/effcee`: Location of [Effcee][effcee] sources, if the `effcee` library
321  is not already configured by an enclosing project.
322* `external/re2`: Location of [RE2][re2] sources, if the `re2` library is not already
323  configured by an enclosing project.
324  (The Effcee project already requires RE2.)
325* `external/abseil_cpp`: Location of [Abseil][abseil-cpp] sources, if Abseil is
326   not already configured by an enclosing project.
327  (The RE2 project already requires Abseil.)
328* `include/`: API clients should add this directory to the include search path
329* `external/spirv-headers`: Intended location for
330  [SPIR-V headers][spirv-headers], not provided
331* `include/spirv-tools/libspirv.h`: C API public interface
332* `source/`: API implementation
333* `test/`: Tests, using the [googletest][googletest] framework
334* `tools/`: Command line executables
335
336### Tests
337
338The project contains a number of tests, used to drive development
339and ensure correctness.  The tests are written using the
340[googletest][googletest] framework.  The `googletest`
341source is not provided with this project.  There are two ways to enable
342tests:
343* If SPIR-V Tools is configured as part of an enclosing project, then the
344  enclosing project should configure `googletest` before configuring SPIR-V Tools.
345* If SPIR-V Tools is configured as a standalone project, then download the
346  `googletest` source into the `<spirv-dir>/external/googletest` directory before
347  configuring and building the project.
348
349## Build
350
351*Note*: Prebuilt binaries are available from the [downloads](docs/downloads.md) page.
352
353First [get the sources](#getting-the-source).
354Then build using CMake, Bazel, Android ndk-build, or the Emscripten SDK.
355
356### Build using CMake
357You can build the project using [CMake][cmake]:
358
359```sh
360cd <spirv-dir>
361mkdir build && cd build
362cmake [-G <platform-generator>] <spirv-dir>
363```
364
365Once the build files have been generated, build using the appropriate build
366command (e.g. `ninja`, `make`, `msbuild`, etc.; this depends on the platform
367generator used above), or use your IDE, or use CMake to run the appropriate build
368command for you:
369
370```sh
371cmake --build . [--config Debug]  # runs `make` or `ninja` or `msbuild` etc.
372```
373
374#### Note about the fuzzer
375
376The SPIR-V fuzzer, `spirv-fuzz`, can only be built via CMake, and is disabled by
377default. To build it, clone protobuf and use the `SPIRV_BUILD_FUZZER` CMake
378option, like so:
379
380```sh
381# In <spirv-dir> (the SPIRV-Tools repo root):
382git clone --depth=1 --branch v3.13.0.1 https://github.com/protocolbuffers/protobuf external/protobuf
383
384# In your build directory:
385cmake [-G <platform-generator>] <spirv-dir> -DSPIRV_BUILD_FUZZER=ON
386cmake --build . --config Debug
387```
388
389You can also add `-DSPIRV_ENABLE_LONG_FUZZER_TESTS=ON` to build additional
390fuzzer tests.
391
392
393### Build using Bazel
394You can also use [Bazel](https://bazel.build/) to build the project.
395
396```sh
397bazel build :all
398```
399
400### Build a node.js package using Emscripten
401
402The SPIRV-Tools core library can be built to a WebAssembly [node.js](https://nodejs.org)
403module. The resulting `SpirvTools` WebAssembly module only exports methods to
404assemble and disassemble SPIR-V modules.
405
406First, make sure you have the [Emscripten SDK](https://emscripten.org).
407Then:
408
409```sh
410cd <spirv-dir>
411./source/wasm/build.sh
412```
413
414The resulting node package, with JavaScript and TypeScript bindings, is
415written to `<spirv-dir>/out/web`.
416
417Note: This builds the package locally. It does *not* publish it to [npm](https://npmjs.org).
418
419To test the result:
420
421```sh
422node ./test/wasm/test.js
423```
424
425### Tools you'll need
426
427For building and testing SPIRV-Tools, the following tools should be
428installed regardless of your OS:
429
430- [CMake](http://www.cmake.org/): if using CMake for generating compilation
431targets, you need to install CMake Version 2.8.12 or later.
432- [Python 3](http://www.python.org/): for utility scripts and running the test
433suite.
434- [Bazel](https://bazel.build/) (optional): if building the source with Bazel,
435you need to install Bazel Version 7.0.2 on your machine. Other versions may
436also work, but are not verified.
437- [Emscripten SDK](https://emscripten.org) (optional): if building the
438  WebAssembly module.
439
440SPIRV-Tools is regularly tested with the following compilers:
441
442On Linux
443- GCC version 9.4
444- Clang version 10.0
445
446On MacOS
447- AppleClang 15.0
448
449On Windows
450- Visual Studio 2019
451- Visual Studio 2022
452
453Note: Other compilers or later versions may work, but they are not tested.
454
455### CMake options
456
457The following CMake options are supported:
458
459* `SPIRV_BUILD_FUZZER={ON|OFF}`, default `OFF` - Build the spirv-fuzz tool.
460* `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output.
461* `SPIRV_SKIP_TESTS={ON|OFF}`, default `OFF`- Build only the library and
462  the command line tools.  This will prevent the tests from being built.
463* `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not
464  the command line tools and tests.
465* `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX
466  platforms with an appropriate version of `clang` this option enables the use
467  of the sanitizers documented [here][clang-sanitizers].
468  This should only be used with a debug build.
469* `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable
470  more strict warnings.  The code might not compile with this option enabled.
471  For Clang, enables `-Weverything`.  For GCC, enables `-Wpedantic`.
472  See [`CMakeLists.txt`](CMakeLists.txt) for details.
473* `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any
474  warnings encountered by enabling the compiler-specific compiler front-end
475  option.  No compiler front-end options are enabled when this option is OFF.
476
477Additionally, you can pass additional C preprocessor definitions to SPIRV-Tools
478via setting `SPIRV_TOOLS_EXTRA_DEFINITIONS`. For example, by setting it to
479`/D_ITERATOR_DEBUG_LEVEL=0` on Windows, you can disable checked iterators and
480iterator debugging.
481
482### Android ndk-build
483
484SPIR-V Tools supports building static libraries `libSPIRV-Tools.a` and
485`libSPIRV-Tools-opt.a` for Android.  Using the Android NDK r25c or later:
486
487```
488cd <spirv-dir>
489
490export ANDROID_NDK=/path/to/your/ndk   # NDK r25c or later
491
492mkdir build && cd build
493mkdir libs
494mkdir app
495
496$ANDROID_NDK/ndk-build -C ../android_test     \
497                      NDK_PROJECT_PATH=.      \
498                      NDK_LIBS_OUT=`pwd`/libs \
499                      NDK_APP_OUT=`pwd`/app
500```
501
502### Updating DEPS
503
504Occasionally the entries in [DEPS](DEPS) will need to be updated. This is done on
505demand when there is a request to do this, often due to downstream breakages.
506To update `DEPS`, run `utils/roll_deps.sh` and confirm that tests pass.
507The script requires Chromium's
508[`depot_tools`](https://chromium.googlesource.com/chromium/tools/depot_tools).
509
510## Library
511
512### Usage
513
514The internals of the library use C++17 features, and are exposed via both a C
515and C++ API.
516
517In order to use the library from an application, the include path should point
518to `<spirv-dir>/include`, which will enable the application to include the
519header `<spirv-dir>/include/spirv-tools/libspirv.h{|pp}` then linking against
520the static library in `<spirv-build-dir>/source/libSPIRV-Tools.a` or
521`<spirv-build-dir>/source/SPIRV-Tools.lib`.
522For optimization, the header file is
523`<spirv-dir>/include/spirv-tools/optimizer.hpp`, and the static library is
524`<spirv-build-dir>/source/libSPIRV-Tools-opt.a` or
525`<spirv-build-dir>/source/SPIRV-Tools-opt.lib`.
526
527* `SPIRV-Tools` CMake target: Creates the static library:
528  * `<spirv-build-dir>/source/libSPIRV-Tools.a` on Linux and OS X.
529  * `<spirv-build-dir>/source/libSPIRV-Tools.lib` on Windows.
530* `SPIRV-Tools-opt` CMake target: Creates the static library:
531  * `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` on Linux and OS X.
532  * `<spirv-build-dir>/source/libSPIRV-Tools-opt.lib` on Windows.
533
534#### Entry points
535
536The interfaces are still under development, and are expected to change.
537
538There are five main entry points into the library in the C interface:
539
540* `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module.
541* `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to
542  text.
543* `spvBinaryParse`: The entry point to a binary parser API.  It issues callbacks
544  for the header and each parsed instruction.  The disassembler is implemented
545  as a client of `spvBinaryParse`.
546* `spvValidate` implements the validator functionality. *Incomplete*
547* `spvValidateBinary` implements the validator functionality. *Incomplete*
548
549The C++ interface is comprised of three classes, `SpirvTools`, `Optimizer` and
550`Linker`, all in the `spvtools` namespace.
551* `SpirvTools` provides `Assemble`, `Disassemble`, and `Validate` methods.
552* `Optimizer` provides methods for registering and running optimization passes.
553* `Linker` provides methods for combining together multiple binaries.
554
555## Command line tools
556
557Command line tools, which wrap the above library functions, are provided to
558assemble or disassemble shader files.  It's a convention to name SPIR-V
559assembly and binary files with suffix `.spvasm` and `.spv`, respectively.
560
561### Assembler tool
562
563The assembler reads the assembly language text, and emits the binary form.
564
565The standalone assembler is the executable called `spirv-as`, and is located in
566`<spirv-build-dir>/tools/spirv-as`.  The functionality of the assembler is implemented
567by the `spvTextToBinary` library function.
568
569* `spirv-as` - the standalone assembler
570  * `<spirv-dir>/tools/as`
571
572Use option `-h` to print help.
573
574### Disassembler tool
575
576The disassembler reads the binary form, and emits assembly language text.
577
578The standalone disassembler is the executable called `spirv-dis`, and is located in
579`<spirv-build-dir>/tools/spirv-dis`. The functionality of the disassembler is implemented
580by the `spvBinaryToText` library function.
581
582* `spirv-dis` - the standalone disassembler
583  * `<spirv-dir>/tools/dis`
584
585Use option `-h` to print help.
586
587The output includes syntax colouring when printing to the standard output stream,
588on Linux, Windows, and OS X.
589
590### Linker tool
591
592The linker combines multiple SPIR-V binary modules together, resulting in a single
593binary module as output.
594
595This is a work in progress.
596The linker does not support OpenCL program linking options related to math
597flags. (See section 5.6.5.2 in OpenCL 1.2)
598
599* `spirv-link` - the standalone linker
600  * `<spirv-dir>/tools/link`
601
602### Optimizer tool
603
604The optimizer processes a SPIR-V binary module, applying transformations
605in the specified order.
606
607This is a work in progress, with initially only few available transformations.
608
609* `spirv-opt` - the standalone optimizer
610  * `<spirv-dir>/tools/opt`
611
612### Validator tool
613
614*Warning:* This functionality is under development, and is incomplete.
615
616The standalone validator is the executable called `spirv-val`, and is located in
617`<spirv-build-dir>/tools/spirv-val`. The functionality of the validator is implemented
618by the `spvValidate` library function.
619
620The validator operates on the binary form.
621
622* `spirv-val` - the standalone validator
623  * `<spirv-dir>/tools/val`
624
625### Reducer tool
626
627The reducer shrinks a SPIR-V binary module, guided by a user-supplied
628*interestingness test*.
629
630This is a work in progress, with initially only shrinks a module in a few ways.
631
632* `spirv-reduce` - the standalone reducer
633  * `<spirv-dir>/tools/reduce`
634
635Run `spirv-reduce --help` to see how to specify interestingness.
636
637### Fuzzer tool
638
639The fuzzer transforms a SPIR-V binary module into a semantically-equivalent
640SPIR-V binary module by applying transformations in a randomized fashion.
641
642This is a work in progress, with initially only a few semantics-preserving
643transformations.
644
645* `spirv-fuzz` - the standalone fuzzer
646  * `<spirv-dir>/tools/fuzz`
647
648Run `spirv-fuzz --help` for a detailed list of options.
649
650### Control flow dumper tool
651
652The control flow dumper prints the control flow graph for a SPIR-V module as a
653[GraphViz](http://www.graphviz.org/) graph.
654
655This is experimental.
656
657* `spirv-cfg` - the control flow graph dumper
658  * `<spirv-dir>/tools/cfg`
659
660### Diff tool
661
662*Warning:* This functionality is under development, and is incomplete.
663
664The diff tool produces a diff-style comparison between two SPIR-V modules.
665
666* `spirv-diff` - the standalone diff tool
667  * `<spirv-dir>`/tools/diff`
668
669### Utility filters
670
671* `spirv-lesspipe.sh` - Automatically disassembles `.spv` binary files for the
672  `less` program, on compatible systems.  For example, set the `LESSOPEN`
673  environment variable as follows, assuming both `spirv-lesspipe.sh` and
674  `spirv-dis` are on your executable search path:
675  ```
676   export LESSOPEN='| spirv-lesspipe.sh "%s"'
677  ```
678  Then you page through a disassembled module as follows:
679  ```
680  less foo.spv
681  ```
682  * The `spirv-lesspipe.sh` script will pass through any extra arguments to
683    `spirv-dis`.  So, for example, you can turn off colours and friendly ID
684    naming as follows:
685    ```
686    export LESSOPEN='| spirv-lesspipe.sh "%s" --no-color --raw-id'
687    ```
688
689* [vim-spirv](https://github.com/kbenzie/vim-spirv) - A vim plugin which
690  supports automatic disassembly of `.spv` files using the `:edit` command and
691  assembly using the `:write` command. The plugin also provides additional
692  features which include; syntax highlighting; highlighting of all ID's matching
693  the ID under the cursor; and highlighting errors where the `Instruction`
694  operand of `OpExtInst` is used without an appropriate `OpExtInstImport`.
695
696* `50spirv-tools.el` - Automatically disassembles '.spv' binary files when
697  loaded into the emacs text editor, and re-assembles them when saved,
698  provided any modifications to the file are valid.  This functionality
699  must be explicitly requested by defining the symbol
700  SPIRV_TOOLS_INSTALL_EMACS_HELPERS as follows:
701  ```
702  cmake -DSPIRV_TOOLS_INSTALL_EMACS_HELPERS=true ...
703  ```
704
705  In addition, this helper is only installed if the directory /etc/emacs/site-start.d
706  exists, which is typically true if emacs is installed on the system.
707
708  Note that symbol IDs are not currently preserved through a load/edit/save operation.
709  This may change if the ability is added to spirv-as.
710
711
712### Tests
713
714Tests are only built when googletest is found.
715
716#### Running test with CMake
717
718Use `ctest -j <num threads>` to run all the tests. To run tests using all threads:
719```shell
720ctest -j$(nproc)
721```
722
723To run a single test target, use `ctest [-j <N>] -R <test regex>`. For example,
724you can run all `opt` tests with:
725```shell
726ctest -R 'spirv-tools-test_opt'
727```
728
729#### Running test with Bazel
730
731Use `bazel test :all` to run all tests. This will run tests in parallel by default.
732
733To run a single test target, specify `:my_test_target` instead of `:all`. Test target
734names get printed when you run `bazel test :all`. For example, you can run
735`opt_def_use_test` with:
736
737on linux:
738```shell
739bazel test --cxxopt=-std=c++17 :opt_def_use_test
740```
741
742on windows:
743```shell
744bazel test --cxxopt=/std:c++17 :opt_def_use_test
745```
746
747## Future Work
748<a name="future"></a>
749
750_See the [projects pages](https://github.com/KhronosGroup/SPIRV-Tools/projects)
751for more information._
752
753### Assembler and disassembler
754
755* The disassembler could emit helpful annotations in comments.  For example:
756  * Use variable name information from debug instructions to annotate
757    key operations on variables.
758  * Show control flow information by annotating `OpLabel` instructions with
759    that basic block's predecessors.
760* Error messages could be improved.
761
762### Validator
763
764This is a work in progress.
765
766### Linker
767
768* The linker could accept math transformations such as allowing MADs, or other
769  math flags passed at linking-time in OpenCL.
770* Linkage attributes can not be applied through a group.
771* Check decorations of linked functions attributes.
772* Remove dead instructions, such as OpName targeting imported symbols.
773
774## Licence
775<a name="license"></a>
776Full license terms are in [LICENSE](LICENSE)
777```
778Copyright (c) 2015-2016 The Khronos Group Inc.
779
780Licensed under the Apache License, Version 2.0 (the "License");
781you may not use this file except in compliance with the License.
782You may obtain a copy of the License at
783
784    http://www.apache.org/licenses/LICENSE-2.0
785
786Unless required by applicable law or agreed to in writing, software
787distributed under the License is distributed on an "AS IS" BASIS,
788WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
789See the License for the specific language governing permissions and
790limitations under the License.
791```
792
793[spirv-tools-cla]: https://cla-assistant.io/KhronosGroup/SPIRV-Tools
794[spirv-tools-projects]: https://github.com/KhronosGroup/SPIRV-Tools/projects
795[spirv-tools-mailing-list]: https://www.khronos.org/spir/spirv-tools-mailing-list
796[spirv-registry]: https://www.khronos.org/registry/spir-v/
797[spirv-headers]: https://github.com/KhronosGroup/SPIRV-Headers
798[googletest]: https://github.com/google/googletest
799[googletest-pull-612]: https://github.com/google/googletest/pull/612
800[googletest-issue-610]: https://github.com/google/googletest/issues/610
801[effcee]: https://github.com/google/effcee
802[re2]: https://github.com/google/re2
803[abseil-cpp]: https://github.com/abseil/abseil-cpp
804[CMake]: https://cmake.org/
805[cpp-style-guide]: https://google.github.io/styleguide/cppguide.html
806[clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation
807