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