Generate spvasm.vim
[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 *Warning:* The validator is incomplete.
65 Check the [CHANGES](CHANGES) file for reports on completed work, and
66 the [Validator
67 sub-project](https://github.com/KhronosGroup/SPIRV-Tools/projects/1) for planned
68 and in-progress work.
69
70 *Note*: The validator checks some Universal Limits, from section 2.17 of the SPIR-V spec.
71 The validator will fail on a module that exceeds those minimum upper bound limits.
72 It is [future work](https://github.com/KhronosGroup/SPIRV-Tools/projects/1#card-1052403)
73 to parameterize the validator to allow larger
74 limits accepted by a more than minimally capable SPIR-V consumer.
75
76
77 ### Optimizer
78
79 *Warning:* The optimizer is still under development.
80
81 Currently supported optimizations:
82 * Strip debug info
83 * Set spec constant default value
84 * Freeze spec constant
85 * Fold `OpSpecConstantOp` and `OpSpecConstantComposite`
86 * Unify constants
87 * Eliminate dead constant
88
89 For the latest list with detailed documentation, please refer to
90 [`include/spirv-tools/optimizer.hpp`](include/spirv-tools/optimizer.hpp).
91
92 ### Extras
93
94 * [Utility filters](#utility-filters)
95 * Build target `spirv-tools-vimsyntax` generates file `spvasm.vim`.
96   Copy that file into your `$HOME/.vim/syntax` directory to get SPIR-V assembly syntax
97   highlighting in Vim.  This build target is not built by default.
98
99 ## Source code
100
101 The SPIR-V Tools are maintained by members of the The Khronos Group Inc.,
102 at https://github.com/KhronosGroup/SPIRV-Tools.
103
104 Contributions via merge request are welcome. Changes should:
105 * Be provided under the [Apache 2.0](#license).
106   You'll be prompted with a one-time "click-through" Contributor's License
107   Agreement (CLA) dialog as part of submitting your pull request or
108   other contribution to GitHub.
109 * Include tests to cover updated functionality.
110 * C++ code should follow the [Google C++ Style Guide][cpp-style-guide].
111 * Code should be formatted with `clang-format`.  Settings are defined by
112   the included [.clang-format](.clang-format) file.
113
114 We intend to maintain a linear history on the GitHub `master` branch.
115
116 ### Source code organization
117
118 * `example`: demo code of using SPIRV-Tools APIs
119 * `external/googletest`: Intended location for the
120   [googletest][googletest] sources, not provided
121 * `include/`: API clients should add this directory to the include search path
122 * `external/spirv-headers`: Intended location for
123   [SPIR-V headers][spirv-headers], not provided
124 * `include/spirv-tools/libspirv.h`: C API public interface
125 * `source/`: API implementation
126 * `test/`: Tests, using the [googletest][googletest] framework
127 * `tools/`: Command line executables
128
129 ### Tests
130
131 The project contains a number of tests, used to drive development
132 and ensure correctness.  The tests are written using the
133 [googletest][googletest] framework.  The `googletest`
134 source is not provided with this project.  There are two ways to enable
135 tests:
136 * If SPIR-V Tools is configured as part of an enclosing project, then the
137   enclosing project should configure `googletest` before configuring SPIR-V Tools.
138 * If SPIR-V Tools is configured as a standalone project, then download the
139   `googletest` source into the `<spirv-dir>/external/googletest` directory before
140   configuring and building the project.
141
142 *Note*: You must use a version of googletest that includes
143 [a fix][googletest-pull-612] for [googletest issue 610][googletest-issue-610].
144 The fix is included on the googletest master branch any time after 2015-11-10.
145 In particular, googletest must be newer than version 1.7.0.
146
147 ## Build
148
149 The project uses [CMake][cmake] to generate platform-specific build
150 configurations. Assume that `<spirv-dir>` is the root directory of the checked
151 out code:
152
153 ```sh
154 cd <spirv-dir>
155 git clone https://github.com/KhronosGroup/SPIRV-Headers.git external/spirv-headers
156 git clone https://github.com/google/googletest.git external/googletest # optional
157
158 mkdir build && cd build
159 cmake [-G <platform-generator>] <spirv-dir>
160 ```
161
162 Once the build files have been generated, build using your preferred
163 development environment.
164
165 ### CMake options
166
167 The following CMake options are supported:
168
169 * `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output.
170 * `SPIRV_SKIP_TESTS={ON|OFF}`, default `OFF`- Build only the library and
171   the command line tools.  This will prevent the tests from being built.
172 * `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not
173   the command line tools and tests.
174 * `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX
175   platforms with an appropriate version of `clang` this option enables the use
176   of the sanitizers documented [here][clang-sanitizers].
177   This should only be used with a debug build.
178 * `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable
179   more strict warnings.  The code might not compile with this option enabled.
180   For Clang, enables `-Weverything`.  For GCC, enables `-Wpedantic`.
181   See [`CMakeLists.txt`](CMakeLists.txt) for details.
182 * `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any
183   warnings encountered by enabling the compiler-specific compiler front-end
184   option.
185
186 ## Library
187
188 ### Usage
189
190 The internals of the library use C++11 features, and are exposed via both a C
191 and C++ API.
192
193 In order to use the library from an application, the include path should point
194 to `<spirv-dir>/include`, which will enable the application to include the
195 header `<spirv-dir>/include/spirv-tools/libspirv.h{|pp}` then linking against
196 the static library in `<spirv-build-dir>/source/libSPIRV-Tools.a` or
197 `<spirv-build-dir>/source/SPIRV-Tools.lib`.
198 For optimization, the header file is
199 `<spirv-dir>/include/spirv-tools/optimizer.hpp`, and the static library is
200 `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` or
201 `<spirv-build-dir>/source/SPIRV-Tools-opt.lib`.
202
203 * `SPIRV-Tools` CMake target: Creates the static library:
204   * `<spirv-build-dir>/source/libSPIRV-Tools.a` on Linux and OS X.
205   * `<spirv-build-dir>/source/libSPIRV-Tools.lib` on Windows.
206 * `SPIRV-Tools-opt` CMake target: Creates the static library:
207   * `<spirv-build-dir>/source/libSPIRV-Tools-opt.a` on Linux and OS X.
208   * `<spirv-build-dir>/source/libSPIRV-Tools-opt.lib` on Windows.
209
210 #### Entry points
211
212 The interfaces are still under development, and are expected to change.
213
214 There are five main entry points into the library in the C interface:
215
216 * `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module.
217 * `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to
218   text.
219 * `spvBinaryParse`: The entry point to a binary parser API.  It issues callbacks
220   for the header and each parsed instruction.  The disassembler is implemented
221   as a client of `spvBinaryParse`.
222 * `spvValidate` implements the validator functionality. *Incomplete*
223 * `spvValidateBinary` implements the validator functionality. *Incomplete*
224
225 The C++ interface is comprised of two classes, `SpirvTools` and `Optimizer`,
226 both in the `spvtools` namespace.
227 * `SpirvTools` provides `Assemble`, `Disassemble`, and `Validate` methods.
228 * `Optimizer` provides methods for registering and running optimization passes.
229
230 ## Command line tools
231
232 Command line tools, which wrap the above library functions, are provided to
233 assemble or disassemble shader files.  It's a convention to name SPIR-V
234 assembly and binary files with suffix `.spvasm` and `.spv`, respectively.
235
236 ### Assembler tool
237
238 The assembler reads the assembly language text, and emits the binary form.
239
240 The standalone assembler is the exectuable called `spirv-as`, and is located in
241 `<spirv-build-dir>/tools/spirv-as`.  The functionality of the assembler is implemented
242 by the `spvTextToBinary` library function.
243
244 * `spirv-as` - the standalone assembler
245   * `<spirv-dir>/tools/as`
246
247 Use option `-h` to print help.
248
249 ### Disassembler tool
250
251 The disassembler reads the binary form, and emits assembly language text.
252
253 The standalone disassembler is the executable called `spirv-dis`, and is located in
254 `<spirv-build-dir>/tools/spirv-dis`. The functionality of the disassembler is implemented
255 by the `spvBinaryToText` library function.
256
257 * `spirv-dis` - the standalone disassembler
258   * `<spirv-dir>/tools/dis`
259
260 Use option `-h` to print help.
261
262 The output includes syntax colouring when printing to the standard output stream,
263 on Linux, Windows, and OS X.
264
265 ### Optimizer tool
266
267 The optimizer processes a SPIR-V binary module, applying transformations
268 in the specified order.
269
270 This is a work in progress, with initially only few available transformations.
271
272 * `spirv-opt` - the standalone optimizer
273   * `<spirv-dir>/tools/opt`
274
275 ### Validator tool
276
277 *Warning:* This functionality is under development, and is incomplete.
278
279 The standalone validator is the executable called `spirv-val`, and is located in
280 `<spirv-build-dir>/tools/spirv-val`. The functionality of the validator is implemented
281 by the `spvValidate` library function.
282
283 The validator operates on the binary form.
284
285 * `spirv-val` - the standalone validator
286   * `<spirv-dir>/tools/val`
287
288 ### Control flow dumper tool
289
290 The control flow dumper prints the control flow graph for a SPIR-V module as a
291 [GraphViz](http://www.graphviz.org/) graph.
292
293 This is experimental.
294
295 * `spirv-cfg` - the control flow graph dumper
296   * `<spirv-dir>/tools/cfg`
297
298 ### Utility filters
299
300 * `spirv-lesspipe.sh` - Automatically disassembles `.spv` binary files for the
301   `less` program, on compatible systems.  For example, set the `LESSOPEN`
302   environment variable as follows, assuming both `spirv-lesspipe.sh` and
303   `spirv-dis` are on your executable search path:
304   ```
305    export LESSOPEN='| spirv-lesspipe.sh "%s"'
306   ```
307   Then you page through a disassembled module as follows:
308   ```
309   less foo.spv
310   ```
311   * The `spirv-lesspipe.sh` script will pass through any extra arguments to
312     `spirv-dis`.  So, for example, you can turn off colours and friendly ID
313     naming as follows:
314     ```
315     export LESSOPEN='| spirv-lesspipe.sh "%s" --no-color --raw-id'
316     ```
317
318 * `50spirv-tools.el` - Automatically disassembles '.spv' binary files when
319   loaded into the emacs text editor, and re-assembles them when saved,
320   provided any modifications to the file are valid.  This functionality
321   must be explicitly requested by defining the symbol
322   SPIRV_TOOLS_INSTALL_EMACS_HELPERS as follows:
323   ```
324   cmake -DSPIRV_TOOLS_INSTALL_EMACS_HELPERS=true ...
325   ```
326
327   In addition, this helper is only installed if the directory /etc/emacs/site-start.d
328   exists, which is typically true if emacs is installed on the system.
329
330   Note that symbol IDs are not currently preserved through a load/edit/save operation.
331   This may change if the ability is added to spirv-as.
332
333
334 ### Tests
335
336 Tests are only built when googletest is found. Use `ctest` to run all the
337 tests.
338
339 ## Future Work
340 <a name="future"></a>
341
342 _See the [projects pages](https://github.com/KhronosGroup/SPIRV-Tools/projects)
343 for more information._
344
345 ### Assembler and disassembler
346
347 * The disassembler could emit helpful annotations in comments.  For example:
348   * Use variable name information from debug instructions to annotate
349     key operations on variables.
350   * Show control flow information by annotating `OpLabel` instructions with
351     that basic block's predecessors.
352 * Error messages could be improved.
353
354 ### Validator
355
356 This is a work in progress.
357
358 ## Licence
359 <a name="license"></a>
360 Full license terms are in [LICENSE](LICENSE)
361 ```
362 Copyright (c) 2015-2016 The Khronos Group Inc.
363
364 Licensed under the Apache License, Version 2.0 (the "License");
365 you may not use this file except in compliance with the License.
366 You may obtain a copy of the License at
367
368     http://www.apache.org/licenses/LICENSE-2.0
369
370 Unless required by applicable law or agreed to in writing, software
371 distributed under the License is distributed on an "AS IS" BASIS,
372 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
373 See the License for the specific language governing permissions and
374 limitations under the License.
375 ```
376
377 [spirv-registry]: https://www.khronos.org/registry/spir-v/
378 [spirv-headers]: https://github.com/KhronosGroup/SPIRV-Headers
379 [googletest]: https://github.com/google/googletest
380 [googletest-pull-612]: https://github.com/google/googletest/pull/612
381 [googletest-issue-610]: https://github.com/google/googletest/issues/610
382 [CMake]: https://cmake.org/
383 [cpp-style-guide]: https://google.github.io/styleguide/cppguide.html
384 [clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation