Finalize v2016.3
[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, and
12 validator for SPIR-V, all based on a common static library. The library contains
13 all of the implementation details, and is used in the standalone tools whilst
14 also enabling integration into other code bases directly.
15
16 The interfaces are still under development, and are expected to change.
17
18 SPIR-V is defined by the Khronos Group Inc.
19 See the [SPIR-V Registry][spirv-registry] for the SPIR-V specification,
20 headers, and XML registry.
21
22 ## Verisoning SPIRV-Tools
23
24 See [`CHANGES`](CHANGES) for a high level summary of recent changes, by version.
25
26 SPIRV-Tools project version numbers are of the form `v`*year*`.`*index* and with
27 an optional `-dev` suffix to indicate work in progress.  For exampe, the
28 following versions are ordered from oldest to newest:
29
30 * `v2016.0`
31 * `v2016.1-dev`
32 * `v2016.1`
33 * `v2016.2-dev`
34 * `v2016.2`
35
36 Use the `--version` option on each command line tool to see the software
37 version.  An API call reports the software version as a C-style string.
38
39 ## Supported features
40
41 ### Assembler, binary parser, and disassembler
42
43 * Based on SPIR-V version 1.1 Rev 3
44 * Support for extended instruction sets:
45   * GLSL std450 version 1.0 Rev 3
46   * OpenCL version 1.0 Rev 2
47 * Support for SPIR-V 1.0 (with or without additional restrictions from Vulkan 1.0)
48 * Assembler only does basic syntax checking.  No cross validation of
49   IDs or types is performed, except to check literal arguments to
50   `OpConstant`, `OpSpecConstant`, and `OpSwitch`.
51
52 See [`syntax.md`](syntax.md) for the assembly language syntax.
53
54 ### Validator
55
56 *Warning:* The validator is incomplete.
57
58 ## Source code
59
60 The SPIR-V Tools are maintained by members of the The Khronos Group Inc.,
61 at https://github.com/KhronosGroup/SPIRV-Tools.
62
63 Contributions via merge request are welcome. Changes should:
64 * Be provided under the [Khronos license](#license).
65 * Include tests to cover updated functionality.
66 * C++ code should follow the [Google C++ Style Guide][cpp-style-guide].
67 * Code should be formatted with `clang-format`.  Settings are defined by
68   the included [.clang-format](.clang-format) file.
69
70 We intend to maintain a linear history on the GitHub `master` branch.
71
72 ### Source code organization
73
74 * `external/googletest`: Intended location for the
75   [googletest][googletest] sources, not provided
76 * `include/`: API clients should add this directory to the include search path
77 * `external/spirv-headers`: Intended location for
78   [SPIR-V headers][spirv-headers], not provided
79 * `include/spirv-tools/libspirv.h`: C API public interface
80 * `source/`: API implementation
81 * `test/`: Tests, using the [googletest][googletest] framework
82 * `tools/`: Command line executables
83
84 ### Tests
85
86 The project contains a number of tests, used to drive development
87 and ensure correctness.  The tests are written using the
88 [googletest][googletest] framework.  The `googletest`
89 source is not provided with this project.  There are two ways to enable
90 tests:
91 * If SPIR-V Tools is configured as part of an enclosing project, then the
92   enclosing project should configure `googletest` before configuring SPIR-V Tools.
93 * If SPIR-V Tools is configured as a standalone project, then download the
94   `googletest` source into the `<spirv-dir>/external/googletest` directory before
95   configuring and building the project.
96
97 *Note*: You must use a version of googletest that includes
98 [a fix][googletest-pull-612] for [googletest issue 610][googletest-issue-610].
99 The fix is included on the googletest master branch any time after 2015-11-10.
100 In particular, googletest must be newer than version 1.7.0.
101
102 ## Build
103
104 The project uses [CMake][cmake] to generate platform-specific build
105 configurations. Assume that `<spirv-dir>` is the root directory of the checked
106 out code:
107
108 ```
109 cd <spirv-dir>
110 git clone https://github.com/KhronosGroup/SPIRV-Headers.git external/spirv-headers
111 git clone https://github.com/google/googletest.git external/googletest # optional
112
113 mkdir build && cd build
114 cmake [-G <platform-generator>] <spirv-dir>
115 ```
116
117 Once the build files have been generated, build using your preferred
118 development environment.
119
120 ### CMake options
121
122 The following CMake options are supported:
123
124 * `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output.
125 * `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not
126   the command line tools.  This will also prevent the tests from being built.
127 * `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX
128   platforms with an appropriate version of `clang` this option enables the use
129   of the sanitizers documented [here][clang-sanitizers].
130   This should only be used with a debug build.
131 * `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable
132   more strict warnings.  The code might not compile with this option enabled.
133   For Clang, enables `-Weverything`.  For GCC, enables `-Wpedantic`.
134   See [`CMakeLists.txt`](CMakeLists.txt) for details.
135 * `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any
136   warnings encountered by enabling the compiler-specific compiler front-end
137   option.
138
139 ## Library
140
141 ### Usage
142
143 The library provides a C API, but the internals use C++11.
144
145 In order to use the library from an application, the include path should point
146 to `<spirv-dir>/include`, which will enable the application to include the
147 header `<spirv-dir>/include/spirv-tools/libspirv.h` then linking against the
148 static library in `<spirv-build-dir>/source/libSPIRV-Tools.a` or
149 `<spirv-build-dir>/source/SPIRV-Tools.lib`.
150
151 * `SPIRV-Tools` CMake target: Creates the static library:
152   * `<spirv-build-dir>/source/libSPIRV-Tools.a` on Linux and OS X.
153   * `<spirv-build-dir>/source/libSPIRV-Tools.lib` on Windows.
154
155 #### Entry points
156
157 The interfaces are still under development, and are expected to change.
158
159 There are three main entry points into the library.
160
161 * `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module.
162 * `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to
163   text.
164 * `spvBinaryParse`: The entry point to a binary parser API.  It issues callbacks
165   for the header and each parsed instruction.  The disassembler is implemented
166   as a client of `spvBinaryParse`.
167 * `spvValidate` implements the validator functionality. *Incomplete*
168
169 ## Command line tools
170
171 Command line tools, which wrap the above library functions, are provided to
172 assemble or disassemble shader files.  It's a convention to name SPIR-V
173 assembly and binary files with suffix `.spvasm` and `.spv`, respectively.
174
175 ### Assembler tool
176
177 The assembler reads the assembly language text, and emits the binary form.
178
179 The standalone assembler is the exectuable called `spirv-as`, and is located in
180 `<spirv-build-dir>/tools/spirv-as`.  The functionality of the assembler is implemented
181 by the `spvTextToBinary` library function.
182
183 * `spirv-as` - the standalone assembler
184   * `<spirv-dir>/tools/as`
185
186 Use option `-h` to print help.
187
188 ### Disassembler tool
189
190 The disassembler reads the binary form, and emits assembly language text.
191
192 The standalone disassembler is the executable called `spirv-dis`, and is located in
193 `<spirv-build-dir>/tools/spirv-dis`. The functionality of the disassembler is implemented
194 by the `spvBinaryToText` library function.
195
196 * `spirv-dis` - the standalone disassembler
197   * `<spirv-dir>/tools/dis`
198
199 Use option `-h` to print help.
200
201 The output includes syntax colouring when printing to the standard output stream,
202 on Linux, Windows, and OS X.
203
204 ### Optimizer tool
205
206 The optimizer processes a SPIR-V binary module, applying transformations
207 in the specified order.
208
209 This is a work in progress, with initially only few available transformations.
210
211 * `spirv-opt` - the standalone optimizer
212   * `<spirv-dir>/tools/opt`
213
214 ### Validator tool
215
216 *Warning:* This functionality is under development, and is incomplete.
217
218 The standalone validator is the executable called `spirv-val`, and is located in
219 `<spirv-build-dir>/tools/spirv-val`. The functionality of the validator is implemented
220 by the `spvValidate` library function.
221
222 The validator operates on the binary form.
223
224 * `spirv-val` - the standalone validator
225   * `<spirv-dir>/tools/val`
226
227 ### Control flow dumper tool
228
229 The control flow dumper prints the control flow graph for a SPIR-V module as a
230 [GraphViz](http://www.graphviz.org/) graph.
231
232 This is experimental.
233
234 * `spirv-cfg` - the control flow graph dumper
235   * `<spirv-dir>/tools/cfg`
236
237 ### Utility filters
238
239 * `spirv-lesspipe.sh` - Automatically disassembles `.spv` binary files for the
240   `less` program, on compatible systems.  For example, set the `LESSOPEN`
241   environment variable as follows, assuming both `spirv-lesspipe.sh` and
242   `spirv-dis` are on your executable search path:
243   ```
244    export LESSOPEN='| spirv-lesspipe.sh "%s"'
245   ```
246   Then you page through a disassembled module as follows:
247   ```
248   less foo.spv
249   ```
250   * The `spirv-lesspipe.sh` script will pass through any extra arguments to
251     `spirv-dis`.  So, for example, you can turn off colours and friendly ID
252     naming as follows:
253     ```
254     export LESSOPEN='| spirv-lesspipe.sh "%s" --no-color --raw-id'
255     ```
256
257 ### Tests
258
259 Tests are only built when googletest is found.
260
261 The `<spirv-build-dir>/test/UnitSPIRV` executable runs the project tests.
262 It supports the standard `googletest` command line options.
263
264 The project also adds a CMake test `spirv-tools-testsuite`, which executes
265 `UnitSPIRV`.  That way it's possible to run the tests using `ctest`.
266
267 ## Future Work
268 <a name="future"></a>
269
270 ### Assembler and disassembler
271
272 * The disassembler could emit helpful annotations in comments.  For example:
273   * Use variable name information from debug instructions to annotate
274     key operations on variables.
275   * Show control flow information by annotating `OpLabel` instructions with
276     that basic block's predecessors.
277 * Error messages could be improved.
278
279 ### Validator
280
281 This is a work in progress.
282
283 ## Licence
284 <a name="license"></a>
285 ```
286 Copyright (c) 2015-2016 The Khronos Group Inc.
287
288 Permission is hereby granted, free of charge, to any person obtaining a
289 copy of this software and/or associated documentation files (the
290 "Materials"), to deal in the Materials without restriction, including
291 without limitation the rights to use, copy, modify, merge, publish,
292 distribute, sublicense, and/or sell copies of the Materials, and to
293 permit persons to whom the Materials are furnished to do so, subject to
294 the following conditions:
295
296 The above copyright notice and this permission notice shall be included
297 in all copies or substantial portions of the Materials.
298
299 MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
300 KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
301 SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
302    https://www.khronos.org/registry/
303
304 THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
305 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
306 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
307 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
308 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
309 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
310 MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
311 ```
312
313 [spirv-registry]: https://www.khronos.org/registry/spir-v/
314 [spirv-headers]: https://github.com/KhronosGroup/SPIRV-Headers
315 [googletest]: https://github.com/google/googletest
316 [googletest-pull-612]: https://github.com/google/googletest/pull/612
317 [googletest-issue-610]: https://github.com/google/googletest/issues/610
318 [CMake]: https://cmake.org/
319 [cpp-style-guide]: https://google.github.io/styleguide/cppguide.html
320 [clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation