• Home
Name Date Size #Lines LOC

..--

.github/workflows/03-May-2024-9979

android_test/03-May-2024-237

build_overrides/03-May-2024-9682

cmake/03-May-2024-5850

docs/03-May-2024-435335

examples/03-May-2024-12286

external/03-May-2024-176157

include/spirv-tools/03-May-2024-2,734882

kokoro/03-May-2024-1,508956

source/03-May-2024-186,582125,454

test/03-May-2024-356,483303,436

tools/03-May-2024-12,45410,661

utils/03-May-2024-38,38932,793

.bazelrcD03-May-202426 21

.bazelversionD03-May-20245 11

.clang-formatD03-May-202492 76

.gitignoreD03-May-2024461 3632

.gnD03-May-2024706 2118

Android.bpD03-May-20248.4 KiB155146

BUILD.bazelD03-May-202416.8 KiB713647

BUILD.gnD03-May-202461.3 KiB1,5791,493

CHANGESD03-May-202472.9 KiB1,5121,461

CMakeLists.txtD03-May-202415.1 KiB400350

CODE_OF_CONDUCT.mdD03-May-2024280 21

CONTRIBUTING.mdD03-May-20247.4 KiB186142

DEPSD03-May-2024970 3523

LICENSED03-May-202411.1 KiB203169

PRESUBMIT.pyD03-May-20241.3 KiB4322

README.mdD03-May-202429.5 KiB790587

WORKSPACED03-May-2024347 2016

build_defs.bzlD03-May-20247.4 KiB235205

codereview.settingsD03-May-2024101 32

README.md

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