• Home
Name Date Size #Lines LOC

..--

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

android_test/03-May-2024-2912

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-188166

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

kokoro/03-May-2024-1,523965

source/03-May-2024-188,863127,166

test/03-May-2024-362,106308,307

tools/03-May-2024-12,97910,845

utils/03-May-2024-38,45832,824

.bazelrcD03-May-2024138 54

.bazelversionD03-May-20246 21

.clang-formatD03-May-202492 76

.gitignoreD03-May-2024483 3733

.gnD03-May-2024706 2118

BUILD.bazelD03-May-202417.4 KiB740673

BUILD.gnD03-May-202461.4 KiB1,5891,502

CHANGESD03-May-202475.9 KiB1,5761,523

CMakeLists.txtD03-May-202415.6 KiB414363

CODE_OF_CONDUCT.mdD03-May-2024280 21

CONTRIBUTING.mdD03-May-20247.1 KiB150118

DEPSD03-May-20241.1 KiB4026

LICENSED03-May-202411.1 KiB203169

PRESUBMIT.pyD03-May-20241.3 KiB4322

README.mdD03-May-202430.1 KiB803597

SECURITY.mdD03-May-2024697 147

WORKSPACED03-May-2024662 3326

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    git clone https://github.com/abseil/abseil-cpp.git          spirv-tools/external/abseil_cpp
296
297#### Dependency on Effcee
298
299Some tests depend on the [Effcee][effcee] library for stateful matching.
300Effcee itself depends on [RE2][re2], and RE2 depends on [Abseil][abseil-cpp].
301
302* If SPIRV-Tools is configured as part of a larger project that already uses
303  Effcee, then that project should include Effcee before SPIRV-Tools.
304* Otherwise, SPIRV-Tools expects Effcee sources to appear in `external/effcee`,
305  RE2 sources to appear in `external/re2`, and Abseil sources to appear in
306  `external/abseil_cpp`.
307
308### Source code organization
309
310* `example`: demo code of using SPIRV-Tools APIs
311* `external/googletest`: Intended location for the
312  [googletest][googletest] sources, not provided
313* `external/effcee`: Location of [Effcee][effcee] sources, if the `effcee` library
314  is not already configured by an enclosing project.
315* `external/re2`: Location of [RE2][re2] sources, if the `re2` library is not already
316  configured by an enclosing project.
317  (The Effcee project already requires RE2.)
318* `external/abseil_cpp`: Location of [Abseil][abseil-cpp] sources, if Abseil is
319   not already configured by an enclosing project.
320  (The RE2 project already requires Abseil.)
321* `include/`: API clients should add this directory to the include search path
322* `external/spirv-headers`: Intended location for
323  [SPIR-V headers][spirv-headers], not provided
324* `include/spirv-tools/libspirv.h`: C API public interface
325* `source/`: API implementation
326* `test/`: Tests, using the [googletest][googletest] framework
327* `tools/`: Command line executables
328
329### Tests
330
331The project contains a number of tests, used to drive development
332and ensure correctness.  The tests are written using the
333[googletest][googletest] framework.  The `googletest`
334source is not provided with this project.  There are two ways to enable
335tests:
336* If SPIR-V Tools is configured as part of an enclosing project, then the
337  enclosing project should configure `googletest` before configuring SPIR-V Tools.
338* If SPIR-V Tools is configured as a standalone project, then download the
339  `googletest` source into the `<spirv-dir>/external/googletest` directory before
340  configuring and building the project.
341
342## Build
343
344*Note*: Prebuilt binaries are available from the [downloads](docs/downloads.md) page.
345
346First [get the sources](#getting-the-source).
347Then build using CMake, Bazel, Android ndk-build, or the Emscripten SDK.
348
349### Build using CMake
350You can build the project using [CMake][cmake]:
351
352```sh
353cd <spirv-dir>
354mkdir build && cd build
355cmake [-G <platform-generator>] <spirv-dir>
356```
357
358Once the build files have been generated, build using the appropriate build
359command (e.g. `ninja`, `make`, `msbuild`, etc.; this depends on the platform
360generator used above), or use your IDE, or use CMake to run the appropriate build
361command for you:
362
363```sh
364cmake --build . [--config Debug]  # runs `make` or `ninja` or `msbuild` etc.
365```
366
367#### Note about the fuzzer
368
369The SPIR-V fuzzer, `spirv-fuzz`, can only be built via CMake, and is disabled by
370default. To build it, clone protobuf and use the `SPIRV_BUILD_FUZZER` CMake
371option, like so:
372
373```sh
374# In <spirv-dir> (the SPIRV-Tools repo root):
375git clone --depth=1 --branch v3.13.0.1 https://github.com/protocolbuffers/protobuf external/protobuf
376
377# In your build directory:
378cmake [-G <platform-generator>] <spirv-dir> -DSPIRV_BUILD_FUZZER=ON
379cmake --build . --config Debug
380```
381
382You can also add `-DSPIRV_ENABLE_LONG_FUZZER_TESTS=ON` to build additional
383fuzzer tests.
384
385
386### Build using Bazel
387You can also use [Bazel](https://bazel.build/) to build the project.
388
389```sh
390bazel build :all
391```
392
393### Build a node.js package using Emscripten
394
395The SPIRV-Tools core library can be built to a WebAssembly [node.js](https://nodejs.org)
396module. The resulting `SpirvTools` WebAssembly module only exports methods to
397assemble and disassemble SPIR-V modules.
398
399First, make sure you have the [Emscripten SDK](https://emscripten.org).
400Then:
401
402```sh
403cd <spirv-dir>
404./source/wasm/build.sh
405```
406
407The resulting node package, with JavaScript and TypeScript bindings, is
408written to `<spirv-dir>/out/web`.
409
410Note: This builds the package locally. It does *not* publish it to [npm](https://npmjs.org).
411
412To test the result:
413
414```sh
415node ./test/wasm/test.js
416```
417
418### Tools you'll need
419
420For building and testing SPIRV-Tools, the following tools should be
421installed regardless of your OS:
422
423- [CMake](http://www.cmake.org/): if using CMake for generating compilation
424targets, you need to install CMake Version 2.8.12 or later.
425- [Python 3](http://www.python.org/): for utility scripts and running the test
426suite.
427- [Bazel](https://bazel.build/) (optional): if building the source with Bazel,
428you need to install Bazel Version 5.0.0 on your machine. Other versions may
429also work, but are not verified.
430- [Emscripten SDK](https://emscripten.org) (optional): if building the
431  WebAssembly module.
432
433SPIRV-Tools is regularly tested with the following compilers:
434
435On Linux
436- GCC version 9.3
437- Clang version 10.0
438
439On MacOS
440- AppleClang 11.0
441
442On Windows
443- Visual Studio 2017
444- Visual Studio 2019
445- Visual Studio 2022
446
447Note: Visual Studio 2017 has incomplete c++17 support. We might stop
448testing it soon. Other compilers or later versions may work, but they are not
449tested.
450
451### CMake options
452
453The following CMake options are supported:
454
455* `SPIRV_BUILD_FUZZER={ON|OFF}`, default `OFF` - Build the spirv-fuzz tool.
456* `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output.
457* `SPIRV_SKIP_TESTS={ON|OFF}`, default `OFF`- Build only the library and
458  the command line tools.  This will prevent the tests from being built.
459* `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not
460  the command line tools and tests.
461* `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX
462  platforms with an appropriate version of `clang` this option enables the use
463  of the sanitizers documented [here][clang-sanitizers].
464  This should only be used with a debug build.
465* `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable
466  more strict warnings.  The code might not compile with this option enabled.
467  For Clang, enables `-Weverything`.  For GCC, enables `-Wpedantic`.
468  See [`CMakeLists.txt`](CMakeLists.txt) for details.
469* `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any
470  warnings encountered by enabling the compiler-specific compiler front-end
471  option.  No compiler front-end options are enabled when this option is OFF.
472
473Additionally, you can pass additional C preprocessor definitions to SPIRV-Tools
474via setting `SPIRV_TOOLS_EXTRA_DEFINITIONS`. For example, by setting it to
475`/D_ITERATOR_DEBUG_LEVEL=0` on Windows, you can disable checked iterators and
476iterator debugging.
477
478### Android ndk-build
479
480SPIR-V Tools supports building static libraries `libSPIRV-Tools.a` and
481`libSPIRV-Tools-opt.a` for Android.  Using the Android NDK r25c or later:
482
483```
484cd <spirv-dir>
485
486export ANDROID_NDK=/path/to/your/ndk   # NDK r25c or later
487
488mkdir build && cd build
489mkdir libs
490mkdir app
491
492$ANDROID_NDK/ndk-build -C ../android_test     \
493                      NDK_PROJECT_PATH=.      \
494                      NDK_LIBS_OUT=`pwd`/libs \
495                      NDK_APP_OUT=`pwd`/app
496```
497
498### Updating DEPS
499
500Occasionally the entries in [DEPS](DEPS) will need to be updated. This is done on
501demand when there is a request to do this, often due to downstream breakages.
502To update `DEPS`, run `utils/roll_deps.sh` and confirm that tests pass.
503The script requires Chromium's
504[`depot_tools`](https://chromium.googlesource.com/chromium/tools/depot_tools).
505
506## Library
507
508### Usage
509
510The internals of the library use C++17 features, and are exposed via both a C
511and C++ API.
512
513In order to use the library from an application, the include path should point
514to `<spirv-dir>/include`, which will enable the application to include the
515header `<spirv-dir>/include/spirv-tools/libspirv.h{|pp}` then linking against
516the static library in `<spirv-build-dir>/source/libSPIRV-Tools.a` or
517`<spirv-build-dir>/source/SPIRV-Tools.lib`.
518For optimization, the header file is
519`<spirv-dir>/include/spirv-tools/optimizer.hpp`, and the static library is
520`<spirv-build-dir>/source/libSPIRV-Tools-opt.a` or
521`<spirv-build-dir>/source/SPIRV-Tools-opt.lib`.
522
523* `SPIRV-Tools` CMake target: Creates the static library:
524  * `<spirv-build-dir>/source/libSPIRV-Tools.a` on Linux and OS X.
525  * `<spirv-build-dir>/source/libSPIRV-Tools.lib` on Windows.
526* `SPIRV-Tools-opt` CMake target: Creates the static library:
527  * `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` on Linux and OS X.
528  * `<spirv-build-dir>/source/libSPIRV-Tools-opt.lib` on Windows.
529
530#### Entry points
531
532The interfaces are still under development, and are expected to change.
533
534There are five main entry points into the library in the C interface:
535
536* `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module.
537* `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to
538  text.
539* `spvBinaryParse`: The entry point to a binary parser API.  It issues callbacks
540  for the header and each parsed instruction.  The disassembler is implemented
541  as a client of `spvBinaryParse`.
542* `spvValidate` implements the validator functionality. *Incomplete*
543* `spvValidateBinary` implements the validator functionality. *Incomplete*
544
545The C++ interface is comprised of three classes, `SpirvTools`, `Optimizer` and
546`Linker`, all in the `spvtools` namespace.
547* `SpirvTools` provides `Assemble`, `Disassemble`, and `Validate` methods.
548* `Optimizer` provides methods for registering and running optimization passes.
549* `Linker` provides methods for combining together multiple binaries.
550
551## Command line tools
552
553Command line tools, which wrap the above library functions, are provided to
554assemble or disassemble shader files.  It's a convention to name SPIR-V
555assembly and binary files with suffix `.spvasm` and `.spv`, respectively.
556
557### Assembler tool
558
559The assembler reads the assembly language text, and emits the binary form.
560
561The standalone assembler is the executable called `spirv-as`, and is located in
562`<spirv-build-dir>/tools/spirv-as`.  The functionality of the assembler is implemented
563by the `spvTextToBinary` library function.
564
565* `spirv-as` - the standalone assembler
566  * `<spirv-dir>/tools/as`
567
568Use option `-h` to print help.
569
570### Disassembler tool
571
572The disassembler reads the binary form, and emits assembly language text.
573
574The standalone disassembler is the executable called `spirv-dis`, and is located in
575`<spirv-build-dir>/tools/spirv-dis`. The functionality of the disassembler is implemented
576by the `spvBinaryToText` library function.
577
578* `spirv-dis` - the standalone disassembler
579  * `<spirv-dir>/tools/dis`
580
581Use option `-h` to print help.
582
583The output includes syntax colouring when printing to the standard output stream,
584on Linux, Windows, and OS X.
585
586### Linker tool
587
588The linker combines multiple SPIR-V binary modules together, resulting in a single
589binary module as output.
590
591This is a work in progress.
592The linker does not support OpenCL program linking options related to math
593flags. (See section 5.6.5.2 in OpenCL 1.2)
594
595* `spirv-link` - the standalone linker
596  * `<spirv-dir>/tools/link`
597
598### Optimizer tool
599
600The optimizer processes a SPIR-V binary module, applying transformations
601in the specified order.
602
603This is a work in progress, with initially only few available transformations.
604
605* `spirv-opt` - the standalone optimizer
606  * `<spirv-dir>/tools/opt`
607
608### Validator tool
609
610*Warning:* This functionality is under development, and is incomplete.
611
612The standalone validator is the executable called `spirv-val`, and is located in
613`<spirv-build-dir>/tools/spirv-val`. The functionality of the validator is implemented
614by the `spvValidate` library function.
615
616The validator operates on the binary form.
617
618* `spirv-val` - the standalone validator
619  * `<spirv-dir>/tools/val`
620
621### Reducer tool
622
623The reducer shrinks a SPIR-V binary module, guided by a user-supplied
624*interestingness test*.
625
626This is a work in progress, with initially only shrinks a module in a few ways.
627
628* `spirv-reduce` - the standalone reducer
629  * `<spirv-dir>/tools/reduce`
630
631Run `spirv-reduce --help` to see how to specify interestingness.
632
633### Fuzzer tool
634
635The fuzzer transforms a SPIR-V binary module into a semantically-equivalent
636SPIR-V binary module by applying transformations in a randomized fashion.
637
638This is a work in progress, with initially only a few semantics-preserving
639transformations.
640
641* `spirv-fuzz` - the standalone fuzzer
642  * `<spirv-dir>/tools/fuzz`
643
644Run `spirv-fuzz --help` for a detailed list of options.
645
646### Control flow dumper tool
647
648The control flow dumper prints the control flow graph for a SPIR-V module as a
649[GraphViz](http://www.graphviz.org/) graph.
650
651This is experimental.
652
653* `spirv-cfg` - the control flow graph dumper
654  * `<spirv-dir>/tools/cfg`
655
656### Diff tool
657
658*Warning:* This functionality is under development, and is incomplete.
659
660The diff tool produces a diff-style comparison between two SPIR-V modules.
661
662* `spirv-diff` - the standalone diff tool
663  * `<spirv-dir>`/tools/diff`
664
665### Utility filters
666
667* `spirv-lesspipe.sh` - Automatically disassembles `.spv` binary files for the
668  `less` program, on compatible systems.  For example, set the `LESSOPEN`
669  environment variable as follows, assuming both `spirv-lesspipe.sh` and
670  `spirv-dis` are on your executable search path:
671  ```
672   export LESSOPEN='| spirv-lesspipe.sh "%s"'
673  ```
674  Then you page through a disassembled module as follows:
675  ```
676  less foo.spv
677  ```
678  * The `spirv-lesspipe.sh` script will pass through any extra arguments to
679    `spirv-dis`.  So, for example, you can turn off colours and friendly ID
680    naming as follows:
681    ```
682    export LESSOPEN='| spirv-lesspipe.sh "%s" --no-color --raw-id'
683    ```
684
685* [vim-spirv](https://github.com/kbenzie/vim-spirv) - A vim plugin which
686  supports automatic disassembly of `.spv` files using the `:edit` command and
687  assembly using the `:write` command. The plugin also provides additional
688  features which include; syntax highlighting; highlighting of all ID's matching
689  the ID under the cursor; and highlighting errors where the `Instruction`
690  operand of `OpExtInst` is used without an appropriate `OpExtInstImport`.
691
692* `50spirv-tools.el` - Automatically disassembles '.spv' binary files when
693  loaded into the emacs text editor, and re-assembles them when saved,
694  provided any modifications to the file are valid.  This functionality
695  must be explicitly requested by defining the symbol
696  SPIRV_TOOLS_INSTALL_EMACS_HELPERS as follows:
697  ```
698  cmake -DSPIRV_TOOLS_INSTALL_EMACS_HELPERS=true ...
699  ```
700
701  In addition, this helper is only installed if the directory /etc/emacs/site-start.d
702  exists, which is typically true if emacs is installed on the system.
703
704  Note that symbol IDs are not currently preserved through a load/edit/save operation.
705  This may change if the ability is added to spirv-as.
706
707
708### Tests
709
710Tests are only built when googletest is found.
711
712#### Running test with CMake
713
714Use `ctest -j <num threads>` to run all the tests. To run tests using all threads:
715```shell
716ctest -j$(nproc)
717```
718
719To run a single test target, use `ctest [-j <N>] -R <test regex>`. For example,
720you can run all `opt` tests with:
721```shell
722ctest -R 'spirv-tools-test_opt'
723```
724
725#### Running test with Bazel
726
727Use `bazel test :all` to run all tests. This will run tests in parallel by default.
728
729To run a single test target, specify `:my_test_target` instead of `:all`. Test target
730names get printed when you run `bazel test :all`. For example, you can run
731`opt_def_use_test` with:
732
733on linux:
734```shell
735bazel test --cxxopt=-std=c++17 :opt_def_use_test
736```
737
738on windows:
739```shell
740bazel test --cxxopt=/std:c++17 :opt_def_use_test
741```
742
743## Future Work
744<a name="future"></a>
745
746_See the [projects pages](https://github.com/KhronosGroup/SPIRV-Tools/projects)
747for more information._
748
749### Assembler and disassembler
750
751* The disassembler could emit helpful annotations in comments.  For example:
752  * Use variable name information from debug instructions to annotate
753    key operations on variables.
754  * Show control flow information by annotating `OpLabel` instructions with
755    that basic block's predecessors.
756* Error messages could be improved.
757
758### Validator
759
760This is a work in progress.
761
762### Linker
763
764* The linker could accept math transformations such as allowing MADs, or other
765  math flags passed at linking-time in OpenCL.
766* Linkage attributes can not be applied through a group.
767* Check decorations of linked functions attributes.
768* Remove dead instructions, such as OpName targeting imported symbols.
769
770## Licence
771<a name="license"></a>
772Full license terms are in [LICENSE](LICENSE)
773```
774Copyright (c) 2015-2016 The Khronos Group Inc.
775
776Licensed under the Apache License, Version 2.0 (the "License");
777you may not use this file except in compliance with the License.
778You may obtain a copy of the License at
779
780    http://www.apache.org/licenses/LICENSE-2.0
781
782Unless required by applicable law or agreed to in writing, software
783distributed under the License is distributed on an "AS IS" BASIS,
784WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
785See the License for the specific language governing permissions and
786limitations under the License.
787```
788
789[spirv-tools-cla]: https://cla-assistant.io/KhronosGroup/SPIRV-Tools
790[spirv-tools-projects]: https://github.com/KhronosGroup/SPIRV-Tools/projects
791[spirv-tools-mailing-list]: https://www.khronos.org/spir/spirv-tools-mailing-list
792[spirv-registry]: https://www.khronos.org/registry/spir-v/
793[spirv-headers]: https://github.com/KhronosGroup/SPIRV-Headers
794[googletest]: https://github.com/google/googletest
795[googletest-pull-612]: https://github.com/google/googletest/pull/612
796[googletest-issue-610]: https://github.com/google/googletest/issues/610
797[effcee]: https://github.com/google/effcee
798[re2]: https://github.com/google/re2
799[abseil-cpp]: https://github.com/abseil/abseil-cpp
800[CMake]: https://cmake.org/
801[cpp-style-guide]: https://google.github.io/styleguide/cppguide.html
802[clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation
803