Update README.md with new code-reduction passes
[platform/upstream/SPIRV-Tools.git] / README.md
1 # SPIR-V Tools
2
3 [![Build Status](https://travis-ci.org/KhronosGroup/SPIRV-Tools.svg?branch=master)](https://travis-ci.org/KhronosGroup/SPIRV-Tools)
4 [![Build status](https://ci.appveyor.com/api/projects/status/gpue87cesrx3pi0d/branch/master?svg=true)](https://ci.appveyor.com/project/Khronoswebmaster/spirv-tools/branch/master)
5
6 ## Overview
7
8 The SPIR-V Tools project provides an API and commands for processing SPIR-V
9 modules.
10
11 The project includes an assembler, binary module parser, disassembler,
12 validator, and optimizer for SPIR-V. Except for the optimizer, all are based
13 on a common static library.  The library contains all of the implementation
14 details, and is used in the standalone tools whilst also enabling integration
15 into other code bases directly. The optimizer implementation resides in its
16 own library, which depends on the core library.
17
18 The interfaces have stabilized:
19 We don't anticipate making a breaking change for existing features.
20
21 See [`projects.md`](projects.md) to see how we use the
22 [GitHub Project
23 feature](https://help.github.com/articles/tracking-the-progress-of-your-work-with-projects/)
24 to organize planned and in-progress work.
25
26 SPIR-V is defined by the Khronos Group Inc.
27 See the [SPIR-V Registry][spirv-registry] for the SPIR-V specification,
28 headers, and XML registry.
29
30 ## Verisoning SPIRV-Tools
31
32 See [`CHANGES`](CHANGES) for a high level summary of recent changes, by version.
33
34 SPIRV-Tools project version numbers are of the form `v`*year*`.`*index* and with
35 an optional `-dev` suffix to indicate work in progress.  For exampe, the
36 following versions are ordered from oldest to newest:
37
38 * `v2016.0`
39 * `v2016.1-dev`
40 * `v2016.1`
41 * `v2016.2-dev`
42 * `v2016.2`
43
44 Use the `--version` option on each command line tool to see the software
45 version.  An API call reports the software version as a C-style string.
46
47 ## Supported features
48
49 ### Assembler, binary parser, and disassembler
50
51 * Based on SPIR-V version 1.1 Rev 3
52 * Support for extended instruction sets:
53   * GLSL std450 version 1.0 Rev 3
54   * OpenCL version 1.0 Rev 2
55 * Support for SPIR-V 1.0 (with or without additional restrictions from Vulkan 1.0)
56 * Assembler only does basic syntax checking.  No cross validation of
57   IDs or types is performed, except to check literal arguments to
58   `OpConstant`, `OpSpecConstant`, and `OpSwitch`.
59
60 See [`syntax.md`](syntax.md) for the assembly language syntax.
61
62 ### Validator
63
64 The validator checks validation rules described by the SPIR-V specification.
65
66 Khronos recommends that tools that create or transform SPIR-V modules use the
67 validator to ensure their outputs are valid, and that tools that consume SPIR-V
68 modules optionally use the validator to protect themselves from bad inputs.
69 This is especially encouraged for debug and development scenarios.
70
71 The validator has one-sided error: it will only return an error when it has
72 implemented a rule check and the module violates that rule.
73
74 The validator is incomplete.
75 See the [CHANGES](CHANGES) file for reports on completed work, and
76 the [Validator
77 sub-project](https://github.com/KhronosGroup/SPIRV-Tools/projects/1) for planned
78 and in-progress work.
79
80 *Note*: The validator checks some Universal Limits, from section 2.17 of the SPIR-V spec.
81 The validator will fail on a module that exceeds those minimum upper bound limits.
82 It is [future work](https://github.com/KhronosGroup/SPIRV-Tools/projects/1#card-1052403)
83 to parameterize the validator to allow larger
84 limits accepted by a more than minimally capable SPIR-V consumer.
85
86
87 ### Optimizer
88
89 *Note:* The optimizer is still under development.
90
91 Currently supported optimizations:
92 * General
93   * Strip debug info
94 * Specialization Constants
95   * Set spec constant default value
96   * Freeze spec constant
97   * Fold `OpSpecConstantOp` and `OpSpecConstantComposite`
98   * Unify constants
99   * Eliminate dead constant
100 * Code Reduction (Entry Point Functions)
101   * Inline all function calls exhaustively
102   * Convert local access chains to inserts/extracts
103   * Eliminate local load/store in single block
104   * Eliminate local load/store with single store
105   * Eliminate local load/store with multiple stores
106   * Eliminate local extract from insert
107   * Eliminate dead instructions (aggressive)
108   * Eliminate dead branches
109   * Merge single successor / single predecessor block pairs
110   * Eliminate common uniform loads
111
112 For the latest list with detailed documentation, please refer to
113 [`include/spirv-tools/optimizer.hpp`](include/spirv-tools/optimizer.hpp).
114
115 ### Extras
116
117 * [Utility filters](#utility-filters)
118 * Build target `spirv-tools-vimsyntax` generates file `spvasm.vim`.
119   Copy that file into your `$HOME/.vim/syntax` directory to get SPIR-V assembly syntax
120   highlighting in Vim.  This build target is not built by default.
121
122 ## Source code
123
124 The SPIR-V Tools are maintained by members of the The Khronos Group Inc.,
125 at https://github.com/KhronosGroup/SPIRV-Tools.
126
127 Contributions via merge request are welcome. Changes should:
128 * Be provided under the [Apache 2.0](#license).
129   You'll be prompted with a one-time "click-through" Contributor's License
130   Agreement (CLA) dialog as part of submitting your pull request or
131   other contribution to GitHub.
132 * Include tests to cover updated functionality.
133 * C++ code should follow the [Google C++ Style Guide][cpp-style-guide].
134 * Code should be formatted with `clang-format`.  Settings are defined by
135   the included [.clang-format](.clang-format) file.
136
137 We intend to maintain a linear history on the GitHub `master` branch.
138
139 ### Source code organization
140
141 * `example`: demo code of using SPIRV-Tools APIs
142 * `external/googletest`: Intended location for the
143   [googletest][googletest] sources, not provided
144 * `include/`: API clients should add this directory to the include search path
145 * `external/spirv-headers`: Intended location for
146   [SPIR-V headers][spirv-headers], not provided
147 * `include/spirv-tools/libspirv.h`: C API public interface
148 * `source/`: API implementation
149 * `test/`: Tests, using the [googletest][googletest] framework
150 * `tools/`: Command line executables
151
152 ### Tests
153
154 The project contains a number of tests, used to drive development
155 and ensure correctness.  The tests are written using the
156 [googletest][googletest] framework.  The `googletest`
157 source is not provided with this project.  There are two ways to enable
158 tests:
159 * If SPIR-V Tools is configured as part of an enclosing project, then the
160   enclosing project should configure `googletest` before configuring SPIR-V Tools.
161 * If SPIR-V Tools is configured as a standalone project, then download the
162   `googletest` source into the `<spirv-dir>/external/googletest` directory before
163   configuring and building the project.
164
165 *Note*: You must use a version of googletest that includes
166 [a fix][googletest-pull-612] for [googletest issue 610][googletest-issue-610].
167 The fix is included on the googletest master branch any time after 2015-11-10.
168 In particular, googletest must be newer than version 1.7.0.
169
170 ## Build
171
172 The project uses [CMake][cmake] to generate platform-specific build
173 configurations. Assume that `<spirv-dir>` is the root directory of the checked
174 out code:
175
176 ```sh
177 cd <spirv-dir>
178 git clone https://github.com/KhronosGroup/SPIRV-Headers.git external/spirv-headers
179 git clone https://github.com/google/googletest.git external/googletest # optional
180
181 mkdir build && cd build
182 cmake [-G <platform-generator>] <spirv-dir>
183 ```
184
185 Once the build files have been generated, build using your preferred
186 development environment.
187
188 ### CMake options
189
190 The following CMake options are supported:
191
192 * `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output.
193 * `SPIRV_SKIP_TESTS={ON|OFF}`, default `OFF`- Build only the library and
194   the command line tools.  This will prevent the tests from being built.
195 * `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not
196   the command line tools and tests.
197 * `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX
198   platforms with an appropriate version of `clang` this option enables the use
199   of the sanitizers documented [here][clang-sanitizers].
200   This should only be used with a debug build.
201 * `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable
202   more strict warnings.  The code might not compile with this option enabled.
203   For Clang, enables `-Weverything`.  For GCC, enables `-Wpedantic`.
204   See [`CMakeLists.txt`](CMakeLists.txt) for details.
205 * `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any
206   warnings encountered by enabling the compiler-specific compiler front-end
207   option.
208
209 ## Library
210
211 ### Usage
212
213 The internals of the library use C++11 features, and are exposed via both a C
214 and C++ API.
215
216 In order to use the library from an application, the include path should point
217 to `<spirv-dir>/include`, which will enable the application to include the
218 header `<spirv-dir>/include/spirv-tools/libspirv.h{|pp}` then linking against
219 the static library in `<spirv-build-dir>/source/libSPIRV-Tools.a` or
220 `<spirv-build-dir>/source/SPIRV-Tools.lib`.
221 For optimization, the header file is
222 `<spirv-dir>/include/spirv-tools/optimizer.hpp`, and the static library is
223 `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` or
224 `<spirv-build-dir>/source/SPIRV-Tools-opt.lib`.
225
226 * `SPIRV-Tools` CMake target: Creates the static library:
227   * `<spirv-build-dir>/source/libSPIRV-Tools.a` on Linux and OS X.
228   * `<spirv-build-dir>/source/libSPIRV-Tools.lib` on Windows.
229 * `SPIRV-Tools-opt` CMake target: Creates the static library:
230   * `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` on Linux and OS X.
231   * `<spirv-build-dir>/source/libSPIRV-Tools-opt.lib` on Windows.
232
233 #### Entry points
234
235 The interfaces are still under development, and are expected to change.
236
237 There are five main entry points into the library in the C interface:
238
239 * `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module.
240 * `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to
241   text.
242 * `spvBinaryParse`: The entry point to a binary parser API.  It issues callbacks
243   for the header and each parsed instruction.  The disassembler is implemented
244   as a client of `spvBinaryParse`.
245 * `spvValidate` implements the validator functionality. *Incomplete*
246 * `spvValidateBinary` implements the validator functionality. *Incomplete*
247
248 The C++ interface is comprised of two classes, `SpirvTools` and `Optimizer`,
249 both in the `spvtools` namespace.
250 * `SpirvTools` provides `Assemble`, `Disassemble`, and `Validate` methods.
251 * `Optimizer` provides methods for registering and running optimization passes.
252
253 ## Command line tools
254
255 Command line tools, which wrap the above library functions, are provided to
256 assemble or disassemble shader files.  It's a convention to name SPIR-V
257 assembly and binary files with suffix `.spvasm` and `.spv`, respectively.
258
259 ### Assembler tool
260
261 The assembler reads the assembly language text, and emits the binary form.
262
263 The standalone assembler is the exectuable called `spirv-as`, and is located in
264 `<spirv-build-dir>/tools/spirv-as`.  The functionality of the assembler is implemented
265 by the `spvTextToBinary` library function.
266
267 * `spirv-as` - the standalone assembler
268   * `<spirv-dir>/tools/as`
269
270 Use option `-h` to print help.
271
272 ### Disassembler tool
273
274 The disassembler reads the binary form, and emits assembly language text.
275
276 The standalone disassembler is the executable called `spirv-dis`, and is located in
277 `<spirv-build-dir>/tools/spirv-dis`. The functionality of the disassembler is implemented
278 by the `spvBinaryToText` library function.
279
280 * `spirv-dis` - the standalone disassembler
281   * `<spirv-dir>/tools/dis`
282
283 Use option `-h` to print help.
284
285 The output includes syntax colouring when printing to the standard output stream,
286 on Linux, Windows, and OS X.
287
288 ### Optimizer tool
289
290 The optimizer processes a SPIR-V binary module, applying transformations
291 in the specified order.
292
293 This is a work in progress, with initially only few available transformations.
294
295 * `spirv-opt` - the standalone optimizer
296   * `<spirv-dir>/tools/opt`
297
298 ### Validator tool
299
300 *Warning:* This functionality is under development, and is incomplete.
301
302 The standalone validator is the executable called `spirv-val`, and is located in
303 `<spirv-build-dir>/tools/spirv-val`. The functionality of the validator is implemented
304 by the `spvValidate` library function.
305
306 The validator operates on the binary form.
307
308 * `spirv-val` - the standalone validator
309   * `<spirv-dir>/tools/val`
310
311 ### Control flow dumper tool
312
313 The control flow dumper prints the control flow graph for a SPIR-V module as a
314 [GraphViz](http://www.graphviz.org/) graph.
315
316 This is experimental.
317
318 * `spirv-cfg` - the control flow graph dumper
319   * `<spirv-dir>/tools/cfg`
320
321 ### Utility filters
322
323 * `spirv-lesspipe.sh` - Automatically disassembles `.spv` binary files for the
324   `less` program, on compatible systems.  For example, set the `LESSOPEN`
325   environment variable as follows, assuming both `spirv-lesspipe.sh` and
326   `spirv-dis` are on your executable search path:
327   ```
328    export LESSOPEN='| spirv-lesspipe.sh "%s"'
329   ```
330   Then you page through a disassembled module as follows:
331   ```
332   less foo.spv
333   ```
334   * The `spirv-lesspipe.sh` script will pass through any extra arguments to
335     `spirv-dis`.  So, for example, you can turn off colours and friendly ID
336     naming as follows:
337     ```
338     export LESSOPEN='| spirv-lesspipe.sh "%s" --no-color --raw-id'
339     ```
340
341 * [vim-spirv](https://github.com/kbenzie/vim-spirv) - A vim plugin which
342   supports automatic disassembly of `.spv` files using the `:edit` command and
343   assembly using the `:write` command. The plugin also provides additional
344   features which include; syntax highlighting; highlighting of all ID's matching
345   the ID under the cursor; and highlighting errors where the `Instruction`
346   operand of `OpExtInst` is used without an appropriate `OpExtInstImport`.
347
348 * `50spirv-tools.el` - Automatically disassembles '.spv' binary files when
349   loaded into the emacs text editor, and re-assembles them when saved,
350   provided any modifications to the file are valid.  This functionality
351   must be explicitly requested by defining the symbol
352   SPIRV_TOOLS_INSTALL_EMACS_HELPERS as follows:
353   ```
354   cmake -DSPIRV_TOOLS_INSTALL_EMACS_HELPERS=true ...
355   ```
356
357   In addition, this helper is only installed if the directory /etc/emacs/site-start.d
358   exists, which is typically true if emacs is installed on the system.
359
360   Note that symbol IDs are not currently preserved through a load/edit/save operation.
361   This may change if the ability is added to spirv-as.
362
363
364 ### Tests
365
366 Tests are only built when googletest is found. Use `ctest` to run all the
367 tests.
368
369 ## Future Work
370 <a name="future"></a>
371
372 _See the [projects pages](https://github.com/KhronosGroup/SPIRV-Tools/projects)
373 for more information._
374
375 ### Assembler and disassembler
376
377 * The disassembler could emit helpful annotations in comments.  For example:
378   * Use variable name information from debug instructions to annotate
379     key operations on variables.
380   * Show control flow information by annotating `OpLabel` instructions with
381     that basic block's predecessors.
382 * Error messages could be improved.
383
384 ### Validator
385
386 This is a work in progress.
387
388 ## Licence
389 <a name="license"></a>
390 Full license terms are in [LICENSE](LICENSE)
391 ```
392 Copyright (c) 2015-2016 The Khronos Group Inc.
393
394 Licensed under the Apache License, Version 2.0 (the "License");
395 you may not use this file except in compliance with the License.
396 You may obtain a copy of the License at
397
398     http://www.apache.org/licenses/LICENSE-2.0
399
400 Unless required by applicable law or agreed to in writing, software
401 distributed under the License is distributed on an "AS IS" BASIS,
402 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
403 See the License for the specific language governing permissions and
404 limitations under the License.
405 ```
406
407 [spirv-registry]: https://www.khronos.org/registry/spir-v/
408 [spirv-headers]: https://github.com/KhronosGroup/SPIRV-Headers
409 [googletest]: https://github.com/google/googletest
410 [googletest-pull-612]: https://github.com/google/googletest/pull/612
411 [googletest-issue-610]: https://github.com/google/googletest/issues/610
412 [CMake]: https://cmake.org/
413 [cpp-style-guide]: https://google.github.io/styleguide/cppguide.html
414 [clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation