1# SPIR-V Tools 2[](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"/>[](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"/>[](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"/>[](https://storage.googleapis.com/spirv-tools/badges/build_link_windows_vs2019_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 `[email protected]` 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.3 444- Clang version 10.0 445 446On MacOS 447- AppleClang 11.0 448 449On Windows 450- Visual Studio 2017 451- Visual Studio 2019 452- Visual Studio 2022 453 454Note: Visual Studio 2017 has incomplete c++17 support. We might stop 455testing it soon. Other compilers or later versions may work, but they are not 456tested. 457 458### CMake options 459 460The following CMake options are supported: 461 462* `SPIRV_BUILD_FUZZER={ON|OFF}`, default `OFF` - Build the spirv-fuzz tool. 463* `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output. 464* `SPIRV_SKIP_TESTS={ON|OFF}`, default `OFF`- Build only the library and 465 the command line tools. This will prevent the tests from being built. 466* `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not 467 the command line tools and tests. 468* `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX 469 platforms with an appropriate version of `clang` this option enables the use 470 of the sanitizers documented [here][clang-sanitizers]. 471 This should only be used with a debug build. 472* `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable 473 more strict warnings. The code might not compile with this option enabled. 474 For Clang, enables `-Weverything`. For GCC, enables `-Wpedantic`. 475 See [`CMakeLists.txt`](CMakeLists.txt) for details. 476* `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any 477 warnings encountered by enabling the compiler-specific compiler front-end 478 option. No compiler front-end options are enabled when this option is OFF. 479 480Additionally, you can pass additional C preprocessor definitions to SPIRV-Tools 481via setting `SPIRV_TOOLS_EXTRA_DEFINITIONS`. For example, by setting it to 482`/D_ITERATOR_DEBUG_LEVEL=0` on Windows, you can disable checked iterators and 483iterator debugging. 484 485### Android ndk-build 486 487SPIR-V Tools supports building static libraries `libSPIRV-Tools.a` and 488`libSPIRV-Tools-opt.a` for Android. Using the Android NDK r25c or later: 489 490``` 491cd <spirv-dir> 492 493export ANDROID_NDK=/path/to/your/ndk # NDK r25c or later 494 495mkdir build && cd build 496mkdir libs 497mkdir app 498 499$ANDROID_NDK/ndk-build -C ../android_test \ 500 NDK_PROJECT_PATH=. \ 501 NDK_LIBS_OUT=`pwd`/libs \ 502 NDK_APP_OUT=`pwd`/app 503``` 504 505### Updating DEPS 506 507Occasionally the entries in [DEPS](DEPS) will need to be updated. This is done on 508demand when there is a request to do this, often due to downstream breakages. 509To update `DEPS`, run `utils/roll_deps.sh` and confirm that tests pass. 510The script requires Chromium's 511[`depot_tools`](https://chromium.googlesource.com/chromium/tools/depot_tools). 512 513## Library 514 515### Usage 516 517The internals of the library use C++17 features, and are exposed via both a C 518and C++ API. 519 520In order to use the library from an application, the include path should point 521to `<spirv-dir>/include`, which will enable the application to include the 522header `<spirv-dir>/include/spirv-tools/libspirv.h{|pp}` then linking against 523the static library in `<spirv-build-dir>/source/libSPIRV-Tools.a` or 524`<spirv-build-dir>/source/SPIRV-Tools.lib`. 525For optimization, the header file is 526`<spirv-dir>/include/spirv-tools/optimizer.hpp`, and the static library is 527`<spirv-build-dir>/source/libSPIRV-Tools-opt.a` or 528`<spirv-build-dir>/source/SPIRV-Tools-opt.lib`. 529 530* `SPIRV-Tools` CMake target: Creates the static library: 531 * `<spirv-build-dir>/source/libSPIRV-Tools.a` on Linux and OS X. 532 * `<spirv-build-dir>/source/libSPIRV-Tools.lib` on Windows. 533* `SPIRV-Tools-opt` CMake target: Creates the static library: 534 * `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` on Linux and OS X. 535 * `<spirv-build-dir>/source/libSPIRV-Tools-opt.lib` on Windows. 536 537#### Entry points 538 539The interfaces are still under development, and are expected to change. 540 541There are five main entry points into the library in the C interface: 542 543* `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module. 544* `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to 545 text. 546* `spvBinaryParse`: The entry point to a binary parser API. It issues callbacks 547 for the header and each parsed instruction. The disassembler is implemented 548 as a client of `spvBinaryParse`. 549* `spvValidate` implements the validator functionality. *Incomplete* 550* `spvValidateBinary` implements the validator functionality. *Incomplete* 551 552The C++ interface is comprised of three classes, `SpirvTools`, `Optimizer` and 553`Linker`, all in the `spvtools` namespace. 554* `SpirvTools` provides `Assemble`, `Disassemble`, and `Validate` methods. 555* `Optimizer` provides methods for registering and running optimization passes. 556* `Linker` provides methods for combining together multiple binaries. 557 558## Command line tools 559 560Command line tools, which wrap the above library functions, are provided to 561assemble or disassemble shader files. It's a convention to name SPIR-V 562assembly and binary files with suffix `.spvasm` and `.spv`, respectively. 563 564### Assembler tool 565 566The assembler reads the assembly language text, and emits the binary form. 567 568The standalone assembler is the executable called `spirv-as`, and is located in 569`<spirv-build-dir>/tools/spirv-as`. The functionality of the assembler is implemented 570by the `spvTextToBinary` library function. 571 572* `spirv-as` - the standalone assembler 573 * `<spirv-dir>/tools/as` 574 575Use option `-h` to print help. 576 577### Disassembler tool 578 579The disassembler reads the binary form, and emits assembly language text. 580 581The standalone disassembler is the executable called `spirv-dis`, and is located in 582`<spirv-build-dir>/tools/spirv-dis`. The functionality of the disassembler is implemented 583by the `spvBinaryToText` library function. 584 585* `spirv-dis` - the standalone disassembler 586 * `<spirv-dir>/tools/dis` 587 588Use option `-h` to print help. 589 590The output includes syntax colouring when printing to the standard output stream, 591on Linux, Windows, and OS X. 592 593### Linker tool 594 595The linker combines multiple SPIR-V binary modules together, resulting in a single 596binary module as output. 597 598This is a work in progress. 599The linker does not support OpenCL program linking options related to math 600flags. (See section 5.6.5.2 in OpenCL 1.2) 601 602* `spirv-link` - the standalone linker 603 * `<spirv-dir>/tools/link` 604 605### Optimizer tool 606 607The optimizer processes a SPIR-V binary module, applying transformations 608in the specified order. 609 610This is a work in progress, with initially only few available transformations. 611 612* `spirv-opt` - the standalone optimizer 613 * `<spirv-dir>/tools/opt` 614 615### Validator tool 616 617*Warning:* This functionality is under development, and is incomplete. 618 619The standalone validator is the executable called `spirv-val`, and is located in 620`<spirv-build-dir>/tools/spirv-val`. The functionality of the validator is implemented 621by the `spvValidate` library function. 622 623The validator operates on the binary form. 624 625* `spirv-val` - the standalone validator 626 * `<spirv-dir>/tools/val` 627 628### Reducer tool 629 630The reducer shrinks a SPIR-V binary module, guided by a user-supplied 631*interestingness test*. 632 633This is a work in progress, with initially only shrinks a module in a few ways. 634 635* `spirv-reduce` - the standalone reducer 636 * `<spirv-dir>/tools/reduce` 637 638Run `spirv-reduce --help` to see how to specify interestingness. 639 640### Fuzzer tool 641 642The fuzzer transforms a SPIR-V binary module into a semantically-equivalent 643SPIR-V binary module by applying transformations in a randomized fashion. 644 645This is a work in progress, with initially only a few semantics-preserving 646transformations. 647 648* `spirv-fuzz` - the standalone fuzzer 649 * `<spirv-dir>/tools/fuzz` 650 651Run `spirv-fuzz --help` for a detailed list of options. 652 653### Control flow dumper tool 654 655The control flow dumper prints the control flow graph for a SPIR-V module as a 656[GraphViz](http://www.graphviz.org/) graph. 657 658This is experimental. 659 660* `spirv-cfg` - the control flow graph dumper 661 * `<spirv-dir>/tools/cfg` 662 663### Diff tool 664 665*Warning:* This functionality is under development, and is incomplete. 666 667The diff tool produces a diff-style comparison between two SPIR-V modules. 668 669* `spirv-diff` - the standalone diff tool 670 * `<spirv-dir>`/tools/diff` 671 672### Utility filters 673 674* `spirv-lesspipe.sh` - Automatically disassembles `.spv` binary files for the 675 `less` program, on compatible systems. For example, set the `LESSOPEN` 676 environment variable as follows, assuming both `spirv-lesspipe.sh` and 677 `spirv-dis` are on your executable search path: 678 ``` 679 export LESSOPEN='| spirv-lesspipe.sh "%s"' 680 ``` 681 Then you page through a disassembled module as follows: 682 ``` 683 less foo.spv 684 ``` 685 * The `spirv-lesspipe.sh` script will pass through any extra arguments to 686 `spirv-dis`. So, for example, you can turn off colours and friendly ID 687 naming as follows: 688 ``` 689 export LESSOPEN='| spirv-lesspipe.sh "%s" --no-color --raw-id' 690 ``` 691 692* [vim-spirv](https://github.com/kbenzie/vim-spirv) - A vim plugin which 693 supports automatic disassembly of `.spv` files using the `:edit` command and 694 assembly using the `:write` command. The plugin also provides additional 695 features which include; syntax highlighting; highlighting of all ID's matching 696 the ID under the cursor; and highlighting errors where the `Instruction` 697 operand of `OpExtInst` is used without an appropriate `OpExtInstImport`. 698 699* `50spirv-tools.el` - Automatically disassembles '.spv' binary files when 700 loaded into the emacs text editor, and re-assembles them when saved, 701 provided any modifications to the file are valid. This functionality 702 must be explicitly requested by defining the symbol 703 SPIRV_TOOLS_INSTALL_EMACS_HELPERS as follows: 704 ``` 705 cmake -DSPIRV_TOOLS_INSTALL_EMACS_HELPERS=true ... 706 ``` 707 708 In addition, this helper is only installed if the directory /etc/emacs/site-start.d 709 exists, which is typically true if emacs is installed on the system. 710 711 Note that symbol IDs are not currently preserved through a load/edit/save operation. 712 This may change if the ability is added to spirv-as. 713 714 715### Tests 716 717Tests are only built when googletest is found. 718 719#### Running test with CMake 720 721Use `ctest -j <num threads>` to run all the tests. To run tests using all threads: 722```shell 723ctest -j$(nproc) 724``` 725 726To run a single test target, use `ctest [-j <N>] -R <test regex>`. For example, 727you can run all `opt` tests with: 728```shell 729ctest -R 'spirv-tools-test_opt' 730``` 731 732#### Running test with Bazel 733 734Use `bazel test :all` to run all tests. This will run tests in parallel by default. 735 736To run a single test target, specify `:my_test_target` instead of `:all`. Test target 737names get printed when you run `bazel test :all`. For example, you can run 738`opt_def_use_test` with: 739 740on linux: 741```shell 742bazel test --cxxopt=-std=c++17 :opt_def_use_test 743``` 744 745on windows: 746```shell 747bazel test --cxxopt=/std:c++17 :opt_def_use_test 748``` 749 750## Future Work 751<a name="future"></a> 752 753_See the [projects pages](https://github.com/KhronosGroup/SPIRV-Tools/projects) 754for more information._ 755 756### Assembler and disassembler 757 758* The disassembler could emit helpful annotations in comments. For example: 759 * Use variable name information from debug instructions to annotate 760 key operations on variables. 761 * Show control flow information by annotating `OpLabel` instructions with 762 that basic block's predecessors. 763* Error messages could be improved. 764 765### Validator 766 767This is a work in progress. 768 769### Linker 770 771* The linker could accept math transformations such as allowing MADs, or other 772 math flags passed at linking-time in OpenCL. 773* Linkage attributes can not be applied through a group. 774* Check decorations of linked functions attributes. 775* Remove dead instructions, such as OpName targeting imported symbols. 776 777## Licence 778<a name="license"></a> 779Full license terms are in [LICENSE](LICENSE) 780``` 781Copyright (c) 2015-2016 The Khronos Group Inc. 782 783Licensed under the Apache License, Version 2.0 (the "License"); 784you may not use this file except in compliance with the License. 785You may obtain a copy of the License at 786 787 http://www.apache.org/licenses/LICENSE-2.0 788 789Unless required by applicable law or agreed to in writing, software 790distributed under the License is distributed on an "AS IS" BASIS, 791WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 792See the License for the specific language governing permissions and 793limitations under the License. 794``` 795 796[spirv-tools-cla]: https://cla-assistant.io/KhronosGroup/SPIRV-Tools 797[spirv-tools-projects]: https://github.com/KhronosGroup/SPIRV-Tools/projects 798[spirv-tools-mailing-list]: https://www.khronos.org/spir/spirv-tools-mailing-list 799[spirv-registry]: https://www.khronos.org/registry/spir-v/ 800[spirv-headers]: https://github.com/KhronosGroup/SPIRV-Headers 801[googletest]: https://github.com/google/googletest 802[googletest-pull-612]: https://github.com/google/googletest/pull/612 803[googletest-issue-610]: https://github.com/google/googletest/issues/610 804[effcee]: https://github.com/google/effcee 805[re2]: https://github.com/google/re2 806[abseil-cpp]: https://github.com/abseil/abseil-cpp 807[CMake]: https://cmake.org/ 808[cpp-style-guide]: https://google.github.io/styleguide/cppguide.html 809[clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation 810