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