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