Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/docs/SPIRVVersionsAndExtensionsHandling.rst

raw
1 =======================================
2 SPIR-V versions and extensions handling
3 =======================================
4
5 .. contents::
6 :local:
7
8 Overview
9 ========
10
11 This document describes how the translator makes decisions about using
12 instructions from different version of the SPIR-V core and extension
13 specifications.
14
15 Being able to control the resulting SPIR-V version is important: the target
16 consumer might be quite old, without support for new SPIR-V versions and there
17 must be the possibility to control which version of the SPIR-V specification
18 that will be used during translation.
19
20 SPIR-V extensions is another thing which must be controllable. Extensions
21 can update and re-define semantics and validation rules for existing SPIR-V
22 entries and it is important to ensure that the translator is able to generate
23 valid SPIR-V according to the core spec, without uses of any extensions if such
24 SPIR-V was requested by user.
25
26 For example, without such infrastructure it is impossible to disable use of
27 ``SPV_KHR_no_integer_wrap_decoration`` - it will be always generated if
28 corresponding LLVM IR counterparts are encountered in input module.
29
30 It is worth mentioning that SPIR-V versions and extensions the handling of
31 SPIR-V versions and extension is mostly important for the SPIR-V generation
32 step. On the consumer side it is the responsibility of the consumer to analyze
33 the incoming SPIR-V file and reject it if it contains something that is not
34 supported by the consumer.
35
36 However, translator simplifies this step for downstream users by checking
37 version and extensions in SPIR-V module during ``readSpirv``/``readSpirvModule``
38 phases.
39
40 SPIR-V Versions
41 ===============
42
43 SPIR-V Generation step
44 ----------------------
45
46 By default translator selects version of generated SPIR-V file based on features
47 used in this file. For example, if it contains ``dereferencable`` LLVM IR
48 attribute, ``MaxByteOffset`` decoration will be generated and resulting SPIR-V
49 version will be raised to 1.1.
50
51 .. note::
52 There is no documentation about which exact features from newest
53 SPIR-V spec versions will be used by the translator. If you are interested
54 when or why a particular SPIR-V instruction is generated, please check this
55 in the source code. Consider this as an implementation detail and if you
56 disagree with something, you can always open an issue or submit pull request
57 - contributions are welcome!
58
59 There is one option to control the behavior of the translator with respect to
60 the version of the SPIR-V file which is being generated/consumed.
61
62 * ``--spirv-max-version=`` - instructs the translator to generate SPIR-V file
63 corresponding to any spec version which is less than or equal to the
64 specified one. Behavior of the translator is the same as by default with only
65 one exception: resulting SPIR-V version cannot be raised higher than
66 specified by this option.
67
68 Allowed values are ``1.0``, ``1.1``, ``1.2``, ``1.3``, ``1.4``, ``1.5`` and
69 ``1.6``.
70
71 .. warning::
72 These two options are mutually exclusive and cannot be specified at the
73 same time.
74
75 If the translator encounters something that cannot be represented by set of
76 allowed SPIR-V versions (which might contain only one version), it does one of
77 the following things:
78
79 * ignores LLVM IR entity in the input file.
80
81 For example, ``dereferencable`` LLVM IR attribute can be ignored if it is not
82 allowed to generate SPIR-V 1.1 and higher.
83
84 * tries to represent LLVM IR entity with allowed instructions.
85
86 For example, ``OpPtrEqual`` can be used if SPIR-V 1.4 is not allowed and can
87 be emulated via ``OpConvertPtrToU`` + ``OpIEqual`` sequence.
88
89 * emits error if LLVM IR entity cannot be ignored and cannot be emulated using
90 available instructions.
91
92 For example, if global constructors/destructors
93 (represented by @llvm.global_ctors/@llvm.global_dtors) are present in a module
94 then the translator should emit error if it cannot use SPIR-V 1.1 and higher
95 where ``Initializer`` and ``Finalizer`` execution modes are described.
96
97 SPIR-V Consumption step
98 -----------------------
99
100 By default, translator consumes SPIR-V of any version which is supported.
101
102 This behavior, however, can be controlled via the same switches described in
103 the previous section.
104
105 If one of the switches present and translator encountered SPIR-V file
106 corresponding to a spec version which is not included into set of allowed
107 SPIR-V versions, translator emits error.
108
109 SPIR-V Extensions
110 =================
111
112 SPIR-V Generation step
113 ----------------------
114
115 By default, translator doesn't use any extensions. If it required to enable
116 certain extension, the following command line option can be used:
117
118 * ``--spirv-ext=`` - allows to control list of allowed/disallowed extensions.
119
120 Valid value for this option is comma-separated list of extension names prefixed
121 with ``+`` or ``-`` - plus means allow to use extension, minus means disallow
122 to use extension. There is one more special value which can be used as extension
123 name in this option: ``all`` - it affects all extension which are known to the
124 translator.
125
126 If ``--spirv-ext`` contains name of extension which is not know for the
127 translator, it will emit error.
128
129 Examples:
130
131 * ``--spirv-ext=+SPV_KHR_no_integer_wrap_decoration,+SPV_INTEL_subgroups``
132 * ``--spirv-ext=+all,-SPV_INTEL_fpga_loop_controls``
133
134 .. warning::
135 Extension name cannot be allowed and disallowed at the same time: for inputs
136 like ``--spirv-ext=+SPV_INTEL_subgroups,-SPV_INTEL_subgroups`` translator
137 will emit error about invalid arguments.
138
139 .. note::
140 Since by default during SPIR-V generation all extensions are disabled, this
141 means that ``-all,`` is implicitly added at the beggining of the
142 ``-spirv-ext`` value.
143
144 If the translator encounters something that cannot be represented by set of
145 allowed SPIR-V extensions (which might be empty), it does one of the following
146 things:
147
148 * ignores LLVM IR entity in the input file.
149
150 For example, ``nsw``/``nuw`` LLVM IR attributes can be ignored if it is not
151 allowed to generate SPIR-V 1.4 and ``SPV_KHR_no_integer_wrap_decoration``
152 extension is disallowed.
153
154 * tries to represent LLVM IR entity with allowed instructions.
155
156 Translator could translate calls to a new built-in functions defined by some
157 extensions as usual call instructions without using special SPIR-V
158 instructions.
159
160 However, this could result in a strange SPIR-V and most likely will lead to
161 errors during consumption. Having that, translator should emit errors if it
162 encounters a call to a built-in function from an extension which must be
163 represented as a special SPIR-V instruction from extension which wasn't
164 allowed to be used. I.e. if translator knows that this certain LLVM IR entity
165 belongs to an extension functionality and this extension is disallowed, it
166 should emit error rather than emulating it.
167
168 * emits error if LLVM IR entity cannot be ignored and cannot be emulated using
169 available instructions.
170
171 For example, new built-in types defined by
172 ``cl_intel_device_side_avc_motion_estimation`` cannot be represented in SPIR-V
173 if ``SPV_INTEL_device_side_avc_motion_estimation`` is disallowed.
174
175 SPIR-V Consumption step
176 -----------------------
177
178 By default, translator consumes SPIR-V regardless of list extensions which are
179 used by the input file, i.e. all extensions are allowed by default during
180 consumption step.
181
182 .. note::
183 This is opposite to the generation step and this is done on purpose: to not
184 broke workflows of existing users of the translator.
185
186 .. note::
187 Since by default during SPIR-V consumption all extensions are enabled, this
188 means that ``+all,`` is implicitly added at the beggining of the
189 ``-spirv-ext`` value.
190
191 This behavior, however, can be controlled via the same switches described in
192 the previous section.
193
194 If ``--spirv-ext`` switch presents, translator will emit error if it finds out
195 that input SPIR-V file uses disallowed extension.
196
197 .. note::
198 If the translator encounters unknown extension in the input SPIR-V file, it
199 will emit error regardless of ``-spirv-ext`` option value.
200
201 If one of the switches present and translator encountered SPIR-V file
202 corresponding to a spec version which is not included into set of allowed
203 SPIR-V versions, translator emits error.
204
205 How to control translator behavior when using it as library
206 ===========================================================
207
208 When using translator as library it can be controlled via bunch of alternative
209 APIs that have additional argument: ``TranslatorOpts`` object which
210 encapsulates information about available SPIR-V versions and extensions.
211
212 List of new APIs is: ``readSpirvModule``, ``writeSpirv`` and ``readSpirv``.
213
214 .. note::
215 See ``LLVMSPIRVOpts.h`` for more details.
216
217 How to get ``TranslatorOpts`` object
218 ------------------------------------
219
220 1. Default constructor. Equal to:
221
222 ``--spirv-max-version=MaxKnownVersion --spirv-ext=-all``
223
224 .. note::
225 There is method ``TranslatorOpts::enableAllExtensions()`` that allows you
226 to quickly enable all known extensions if it is needed.
227
228 2. Constructor which accepts all parameters
229
230 Consumes both max SPIR-V version and optional map with extensions status
231 (i.e. which one is allowed and which one is disallowed)
232
233 Extensions status map
234 ^^^^^^^^^^^^^^^^^^^^^
235
236 This map is defined as ``std::map<ExtensionID, bool>`` and it is intended to
237 show which extension is allowed to be used (``true`` as value) and which is not
238 (``false`` as value).
239
240 .. note::
241 If certain ``ExtensionID`` value is missed in the map, it automatically means
242 that extension is not allowed to be used.
243
244 This implies that by default, all extensions are disallowed.