Skip to content

Commit 4f0aacb

Browse files
MrSidimsbader
authored andcommitted
Sync with Khronos SPIR-V Translator 4805372e24
Last patch is called: "Fix invalid .spv->.bc translation of Intel FPGA merge attribute (#291)" Conflicted with: "[SYCL] Enable build and LIT tests on windows (#463)" Resolved to "Ours". Commits excluded from the sync as it breakes SYCL build: "Use std::make_unique after LLVM change r369130" "Move to C++14" both of these changes should be cherry-pick after our next LLVM update. Signed-off-by: Dmitry Sidorov <[email protected]>
1 parent 3432def commit 4f0aacb

File tree

87 files changed

+2536
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2536
-243
lines changed

llvm-spirv/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ endif(LLVM_INCLUDE_TESTS)
6363
install(
6464
FILES
6565
${LLVM_SPIRV_INCLUDE_DIRS}/LLVMSPIRVLib.h
66+
${LLVM_SPIRV_INCLUDE_DIRS}/LLVMSPIRVOpts.h
67+
${LLVM_SPIRV_INCLUDE_DIRS}/LLVMSPIRVExtensions.inc
6668
DESTINATION
6769
${CMAKE_INSTALL_PREFIX}/include/LLVMSPIRVLib
6870
)

llvm-spirv/README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The files/directories related to the translator:
1818

1919
## Build Instructions
2020

21-
Master branch of this repo is aimed to be buildable with the latest LLVM version.
21+
The `master` branch of this repo is aimed to be buildable with the latest LLVM `master` or `trunk` revision.
2222

2323
### Build with pre-installed LLVM
2424

@@ -64,7 +64,8 @@ make llvm-spirv -j`nproc`
6464

6565
## Test instructions
6666

67-
All tests related to the translator are placed in the [test](test) directory. Optionally the tests can make use of spirv-val (part of SPIRV-Tools) in order to validate the generated SPIR-V against the official SPIR-V specification.
67+
All tests related to the translator are placed in the [test](test) directory. A number of the tests require spirv-as (part of SPIR-V Tools) to run, but the remainder of the tests can still be run without this. Optionally the tests can make use of spirv-val (part of SPIRV-Tools) in order to validate the generated SPIR-V against the official SPIR-V specification.
68+
6869
In case tests are failing due to SPIRV-Tools not supporting certain SPIR-V features, please get an updated package. The `PKG_CONFIG_PATH` environmental variable can be used to let cmake point to a custom installation.
6970

7071
Execute the following command inside the build directory to run translator tests:
@@ -99,10 +100,52 @@ To translate between LLVM IR and SPIR-V:
99100
* `-spirv-text` - read/write SPIR-V in an internal textual format for debugging purpose. The textual format is not defined by SPIR-V spec.
100101
* `-help` - to see full list of options
101102
103+
### Handling SPIR-V versions generated by the translator
104+
105+
There is one option to control the behavior of the translator with respect to
106+
the version of the SPIR-V file which is being generated/consumed.
107+
108+
* `-spirv-max-version=` - this option allows restricting the
109+
SPIRV-LLVM-Translator **not** to generate a SPIR-V with a version which is
110+
higher than the one specified via this option.
111+
112+
If the `-r` option was also specified, the SPIRV-LLVM-Translator will reject
113+
the input file and emit an error if the SPIR-V version in it is higher than
114+
one specified via this option.
115+
116+
Allowed values are `1.0`/`1.1`.
117+
118+
More information can be found in
119+
[SPIR-V versions and extensions handling](docs/SPIRVVersionsAndExtensionsHandling.rst)
120+
121+
### Handling SPIR-V extensions generated by the translator
122+
123+
By default, during SPIR-V generation, the translator doesn't use any extensions.
124+
However, during SPIR-V consumption, the translator accepts input files that use
125+
any known extensions.
126+
127+
If certain extensions are required to be enabled or disabled, the following
128+
command line option can be used:
129+
130+
* ``--spirv-ext=`` - this options allows controlling which extensions are
131+
allowed/disallowed
132+
133+
Valid value for this option is comma-separated list of extension names prefixed
134+
with ``+`` or ``-`` - plus means allow to use extension, minus means disallow
135+
to use extension. There is one more special value which can be used as extension
136+
name in this option: ``all`` - it affects all extension which are known to the
137+
translator.
138+
139+
If ``--spirv-ext`` contains the name of an extension which is not known for the
140+
translator, it will emit an error.
141+
142+
More information can be found in
143+
[SPIR-V versions and extensions handling](docs/SPIRVVersionsAndExtensionsHandling.rst)
144+
102145
## Branching strategy
103146
104147
Code on the master branch in this repository is intended to be compatible with master/trunk branch of the [llvm](https://github.com/llvm-mirror/llvm) project. That is, for an OpenCL kernel compiled to llvm bitcode by the latest version(built with the latest git commit or svn revision) of Clang it should be possible to translate it to SPIR-V with the llvm-spirv tool.
105148
106149
All new development should be done on the master branch.
107150
108-
To have versions compatible with released versions of LLVM and Clang, corresponding branches are created in this repository. For example, to build translator with LLVM 7.0 ([release_70](https://github.com/llvm-mirror/llvm/tree/release_70)) one should use [llvm_release_70](https://github.com/KhronosGroup/SPIRV-LLVM-Translator/tree/llvm_release_70) branch.
151+
To have versions compatible with released versions of LLVM and Clang, corresponding branches are available in this repository. For example, to build the translator with LLVM 7.0 ([release_70](https://github.com/llvm-mirror/llvm/tree/release_70)) one should use the [llvm_release_70](https://github.com/KhronosGroup/SPIRV-LLVM-Translator/tree/llvm_release_70) branch. As a general rule, commits from the master branch may be backported to the release branches as long as they do not depend on features from a later LLVM/Clang release and there are no objections from the maintainer(s). There is no guarantee that older release branches are proactively kept up to date with master, but you can request certain commits on older release branches by creating a pull request or raising an issue on GitHub.
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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`` and ``1.1``.
69+
70+
.. warning::
71+
These two options are mutually exclusive and cannot be specified at the
72+
same time.
73+
74+
If the translator encounters something that cannot be represented by set of
75+
allowed SPIR-V versions (which might contain only one version), it does one of
76+
the following things:
77+
78+
* ignores LLVM IR entity in the input file.
79+
80+
For example, ``dereferencable`` LLVM IR attribute can be ignored if it is not
81+
allowed to generate SPIR-V 1.1 and higher.
82+
83+
* tries to represent LLVM IR entity with allowed instructions.
84+
85+
For example, ``OpPtrEqual`` can be used if SPIR-V 1.4 is not allowed and can
86+
be emulated via ``OpConvertPtrToU`` + ``OpIEqual`` sequence.
87+
88+
* emits error if LLVM IR entity cannot be ignored and cannot be emulated using
89+
available instructions.
90+
91+
For example, if global constructors/destructors
92+
(represented by @llvm.global_ctors/@llvm.global_dtors) are present in a module
93+
then the translator should emit error if it cannot use SPIR-V 1.1 and higher
94+
where ``Initializer`` and ``Finalizer`` execution modes are described.
95+
96+
SPIR-V Consumption step
97+
-----------------------
98+
99+
By default, translator consumes SPIR-V of any version which is supported.
100+
101+
This behavior, however, can be controlled via the same switches described in
102+
the previous section.
103+
104+
If one of the switches present and translator encountered SPIR-V file
105+
corresponding to a spec version which is not included into set of allowed
106+
SPIR-V versions, translator emits error.
107+
108+
SPIR-V Extensions
109+
=================
110+
111+
SPIR-V Generation step
112+
----------------------
113+
114+
By default, translator doesn't use any extensions. If it required to enable
115+
certain extension, the following command line option can be used:
116+
117+
* ``--spirv-ext=`` - allows to control list of allowed/disallowed extensions.
118+
119+
Valid value for this option is comma-separated list of extension names prefixed
120+
with ``+`` or ``-`` - plus means allow to use extension, minus means disallow
121+
to use extension. There is one more special value which can be used as extension
122+
name in this option: ``all`` - it affects all extension which are known to the
123+
translator.
124+
125+
If ``--spirv-ext`` contains name of extension which is not know for the
126+
translator, it will emit error.
127+
128+
Examples:
129+
130+
* ``--spirv-ext=+SPV_KHR_no_integer_wrap_decoration,+SPV_INTEL_subgroups``
131+
* ``--spirv-ext=+all,-SPV_INTEL_fpga_loop_controls``
132+
133+
.. warning::
134+
Extension name cannot be allowed and disallowed at the same time: for inputs
135+
like ``--spirv-ext=+SPV_INTEL_subgroups,-SPV_INTEL_subgroups`` translator
136+
will emit error about invalid arguments.
137+
138+
.. note::
139+
Since by default during SPIR-V generation all extensions are disabled, this
140+
means that ``-all,`` is implicitly added at the beggining of the
141+
``-spirv-ext`` value.
142+
143+
If the translator encounters something that cannot be represented by set of
144+
allowed SPIR-V extensions (which might be empty), it does one of the following
145+
things:
146+
147+
* ignores LLVM IR entity in the input file.
148+
149+
For example, ``nsw``/``nuw`` LLVM IR attributes can be ignored if it is not
150+
allowed to generate SPIR-V 1.4 and ``SPV_KHR_no_integer_wrap_decoration``
151+
extension is disallowed.
152+
153+
* tries to represent LLVM IR entity with allowed instructions.
154+
155+
Translator could translate calls to a new built-in functions defined by some
156+
extensions as usual call instructions without using special SPIR-V
157+
instructions.
158+
159+
However, this could result in a strange SPIR-V and most likely will lead to
160+
errors during consumption. Having that, translator should emit errors if it
161+
encounters a call to a built-in function from an extension which must be
162+
represented as a special SPIR-V instruction from extension which wasn't
163+
allowed to be used. I.e. if translator knows that this certain LLVM IR entity
164+
belongs to an extension functionality and this extension is disallowed, it
165+
should emit error rather than emulating it.
166+
167+
* emits error if LLVM IR entity cannot be ignored and cannot be emulated using
168+
available instructions.
169+
170+
For example, new built-in types defined by
171+
``cl_intel_device_side_avc_motion_estimation`` cannot be represented in SPIR-V
172+
if ``SPV_INTEL_device_side_avc_motion_estimation`` is disallowed.
173+
174+
SPIR-V Consumption step
175+
-----------------------
176+
177+
By default, translator consumes SPIR-V regardless of list extensions which are
178+
used by the input file, i.e. all extensions are allowed by default during
179+
consumption step.
180+
181+
.. note::
182+
This is opposite to the generation step and this is done on purpose: to not
183+
broke workflows of existing users of the translator.
184+
185+
.. note::
186+
Since by default during SPIR-V consumption all extensions are enabled, this
187+
means that ``+all,`` is implicitly added at the beggining of the
188+
``-spirv-ext`` value.
189+
190+
This behavior, however, can be controlled via the same switches described in
191+
the previous section.
192+
193+
If ``--spirv-ext`` switch presents, translator will emit error if it finds out
194+
that input SPIR-V file uses disallowed extension.
195+
196+
.. note::
197+
If the translator encounters unknown extension in the input SPIR-V file, it
198+
will emit error regardless of ``-spirv-ext`` option value.
199+
200+
If one of the switches present and translator encountered SPIR-V file
201+
corresponding to a spec version which is not included into set of allowed
202+
SPIR-V versions, translator emits error.
203+
204+
How to control translator behavior when using it as library
205+
===========================================================
206+
207+
When using translator as library it can be controlled via bunch of alternative
208+
APIs that have additional argument: ``TranslatorOpts`` object which
209+
encapsulates information about available SPIR-V versions and extensions.
210+
211+
List of new APIs is: ``readSpirvModule``, ``writeSpirv`` and ``readSpirv``.
212+
213+
.. note::
214+
See ``LLVMSPIRVOpts.h`` for more details.
215+
216+
How to get ``TranslatorOpts`` object
217+
------------------------------------
218+
219+
1. Default constructor. Equal to:
220+
221+
``--spirv-max-version=MaxKnownVersion --spirv-ext=-all``
222+
223+
.. note::
224+
There is method ``TranslatorOpts::enableAllExtensions()`` that allows you
225+
to quickly enable all known extensions if it is needed.
226+
227+
2. Constructor which accepts all parameters
228+
229+
Consumes both max SPIR-V version and optional map with extensions status
230+
(i.e. which one is allowed and which one is disallowed)
231+
232+
Extensions status map
233+
^^^^^^^^^^^^^^^^^^^^^
234+
235+
This map is defined as ``std::map<ExtensionID, bool>`` and it is intended to
236+
show which extension is allowed to be used (``true`` as value) and which is not
237+
(``false`` as value).
238+
239+
.. note::
240+
If certain ``ExtensionID`` value is missed in the map, it automatically means
241+
that extension is not allowed to be used.
242+
243+
This implies that by default, all extensions are disallowed.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef EXT
3+
#error "EXT macro must be defined"
4+
#endif
5+
6+
EXT(SPV_KHR_no_integer_wrap_decoration)
7+
EXT(SPV_INTEL_subgroups)
8+
EXT(SPV_INTEL_media_block_io)
9+
EXT(SPV_INTEL_device_side_avc_motion_estimation)
10+
EXT(SPV_INTEL_fpga_loop_controls)
11+
EXT(SPV_INTEL_fpga_memory_attributes)
12+
EXT(SPV_INTEL_unstructured_loop_controls)
13+
EXT(SPV_INTEL_fpga_reg)
14+
EXT(SPV_INTEL_blocking_pipes)
15+
EXT(SPV_INTEL_function_pointers)

llvm-spirv/include/LLVMSPIRVLib.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#ifndef SPIRV_H
4242
#define SPIRV_H
4343

44+
#include "LLVMSPIRVOpts.h"
45+
4446
#include <iostream>
4547
#include <string>
4648

@@ -66,6 +68,7 @@ void initializePreprocessMetadataPass(PassRegistry &);
6668
#include "llvm/IR/Module.h"
6769

6870
namespace SPIRV {
71+
6972
class SPIRVModule;
7073

7174
/// \brief Check if a string contains SPIR-V binary.
@@ -94,6 +97,12 @@ bool isSpirvText(std::string &Img);
9497
std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
9598
std::string &ErrMsg);
9699

100+
/// \brief Load SPIR-V from istream as a SPIRVModule.
101+
/// \returns null on failure.
102+
std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
103+
const SPIRV::TranslatorOpts &Opts,
104+
std::string &ErrMsg);
105+
97106
} // End namespace SPIRV
98107

99108
namespace llvm {
@@ -107,6 +116,16 @@ bool writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg);
107116
bool readSpirv(LLVMContext &C, std::istream &IS, Module *&M,
108117
std::string &ErrMsg);
109118

119+
/// \brief Translate LLVM module to SPIR-V and write to ostream.
120+
/// \returns true if succeeds.
121+
bool writeSpirv(Module *M, const SPIRV::TranslatorOpts &Opts, std::ostream &OS,
122+
std::string &ErrMsg);
123+
124+
/// \brief Load SPIR-V from istream and translate to LLVM module.
125+
/// \returns true if succeeds.
126+
bool readSpirv(LLVMContext &C, const SPIRV::TranslatorOpts &Opts,
127+
std::istream &IS, Module *&M, std::string &ErrMsg);
128+
110129
/// \brief Convert a SPIRVModule into LLVM IR.
111130
/// \returns null on failure.
112131
std::unique_ptr<Module>

0 commit comments

Comments
 (0)