Skip to content

Sync with Khronos SPIR-V Translator 4805372e24 #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6647,8 +6647,10 @@ void SPIRVTranslator::ConstructJob(Compilation &C, const JobAction &JA,

TranslatorArgs.push_back("-o");
TranslatorArgs.push_back(Output.getFilename());
if (getToolChain().getTriple().isSYCLDeviceEnvironment())
TranslatorArgs.push_back("-spirv-no-deref-attr");
if (getToolChain().getTriple().isSYCLDeviceEnvironment()) {
TranslatorArgs.push_back("-spirv-max-version=1.0");
TranslatorArgs.push_back("-spirv-ext=+all");
}
for (auto I : Inputs) {
TranslatorArgs.push_back(I.getFilename());
}
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/SYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const char *SYCL::Linker::constructLLVMSpirvCommand(Compilation &C,
CmdArgs.push_back("-o");
CmdArgs.push_back(OutputFileName);
} else {
CmdArgs.push_back("-spirv-no-deref-attr");
CmdArgs.push_back("-spirv-max-version=1.0");
CmdArgs.push_back("-spirv-ext=+all");
CmdArgs.push_back("-o");
CmdArgs.push_back(Output.getFilename());
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Driver/sycl-offload-intelfpga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// CHK-FPGA-LINK: clang-offload-bundler{{.*}} "-type=o" "-targets=fpga-fpga_aocr-intel-{{.*}}-sycldevice" "-inputs=[[INPUT]]" "-check-section"
// CHK-FPGA-LINK: clang-offload-bundler{{.*}} "-type=o" "-targets=host-x86_64-unknown-linux-gnu,sycl-spir64_fpga-unknown-{{.*}}-sycldevice" "-inputs=[[INPUT]]" "-outputs=[[OUTPUT1:.+\.o]],[[OUTPUT2:.+\.o]]" "-unbundle"
// CHK-FPGA-LINK: llvm-link{{.*}} "[[OUTPUT2]]" "-o" "[[OUTPUT3:.+\.bc]]"
// CHK-FPGA-LINK: llvm-spirv{{.*}} "-spirv-no-deref-attr" "-o" "[[OUTPUT4:.+\.spv]]" "[[OUTPUT3]]"
// CHK-FPGA-LINK: llvm-spirv{{.*}} "-spirv-max-version=1.0" "-spirv-ext=+all" "-o" "[[OUTPUT4:.+\.spv]]" "[[OUTPUT3]]"
// CHK-FPGA-EARLY: aoc{{.*}} "-o" "[[OUTPUT5:.+\.aocr]]" "[[OUTPUT4]]" "-sycl" "-rtl"
// CHK-FPGA-IMAGE: aoc{{.*}} "-o" "[[OUTPUT5:.+\.aocx]]" "[[OUTPUT4]]" "-sycl"
// CHK-FPGA-LINK: ld{{.*}} "-r" "[[INPUT]]" "-o" "[[OUTPUT6:.+\.o]]"
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Driver/sycl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// DEFAULT: "-triple" "spir64-unknown-{{.*}}-sycldevice"{{.*}} "-fsycl-is-device"{{.*}} "-emit-llvm-bc"
// DEFAULT: "-internal-isystem" "{{.*lib.*clang.*include}}"
// DEFAULT-NOT: "{{.*}}llvm-spirv"{{.*}} "-spirv-no-deref-attr"
// DEFAULT-NOT: "{{.*}}llvm-spirv"{{.*}} "-spirv-max-version=1.0"{{.*}} "-spirv-ext=+all"
// NO-BITCODE: "-triple" "spir64-unknown-{{.*}}-sycldevice"{{.*}} "-fsycl-is-device"{{.*}} "-emit-llvm-bc"
// NO-BITCODE: "{{.*}}llvm-spirv"{{.*}} "-spirv-no-deref-attr"
// NO-BITCODE: "{{.*}}llvm-spirv"{{.*}} "-spirv-max-version=1.0"{{.*}} "-spirv-ext=+all"
// TARGET: "-triple" "spir64-unknown-linux-sycldevice"{{.*}} "-fsycl-is-device"{{.*}} "-emit-llvm-bc"
// COMBINED: "-triple" "spir64-unknown-{{.*}}-sycldevice"{{.*}} "-fsycl-is-device"{{.*}} "-emit-llvm-bc"

Expand Down
2 changes: 2 additions & 0 deletions llvm-spirv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ endif(LLVM_INCLUDE_TESTS)
install(
FILES
${LLVM_SPIRV_INCLUDE_DIRS}/LLVMSPIRVLib.h
${LLVM_SPIRV_INCLUDE_DIRS}/LLVMSPIRVOpts.h
${LLVM_SPIRV_INCLUDE_DIRS}/LLVMSPIRVExtensions.inc
DESTINATION
${CMAKE_INSTALL_PREFIX}/include/LLVMSPIRVLib
)
Expand Down
49 changes: 46 additions & 3 deletions llvm-spirv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The files/directories related to the translator:

## Build Instructions

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

### Build with pre-installed LLVM

Expand Down Expand Up @@ -64,7 +64,8 @@ make llvm-spirv -j`nproc`

## Test instructions

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.
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.

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.

Execute the following command inside the build directory to run translator tests:
Expand Down Expand Up @@ -99,10 +100,52 @@ To translate between LLVM IR and SPIR-V:
* `-spirv-text` - read/write SPIR-V in an internal textual format for debugging purpose. The textual format is not defined by SPIR-V spec.
* `-help` - to see full list of options

### Handling SPIR-V versions generated by the translator

There is one option to control the behavior of the translator with respect to
the version of the SPIR-V file which is being generated/consumed.

* `-spirv-max-version=` - this option allows restricting the
SPIRV-LLVM-Translator **not** to generate a SPIR-V with a version which is
higher than the one specified via this option.

If the `-r` option was also specified, the SPIRV-LLVM-Translator will reject
the input file and emit an error if the SPIR-V version in it is higher than
one specified via this option.

Allowed values are `1.0`/`1.1`.

More information can be found in
[SPIR-V versions and extensions handling](docs/SPIRVVersionsAndExtensionsHandling.rst)

### Handling SPIR-V extensions generated by the translator

By default, during SPIR-V generation, the translator doesn't use any extensions.
However, during SPIR-V consumption, the translator accepts input files that use
any known extensions.

If certain extensions are required to be enabled or disabled, the following
command line option can be used:

* ``--spirv-ext=`` - this options allows controlling which extensions are
allowed/disallowed

Valid value for this option is comma-separated list of extension names prefixed
with ``+`` or ``-`` - plus means allow to use extension, minus means disallow
to use extension. There is one more special value which can be used as extension
name in this option: ``all`` - it affects all extension which are known to the
translator.

If ``--spirv-ext`` contains the name of an extension which is not known for the
translator, it will emit an error.

More information can be found in
[SPIR-V versions and extensions handling](docs/SPIRVVersionsAndExtensionsHandling.rst)

## Branching strategy

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.

All new development should be done on the master branch.

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.
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.
243 changes: 243 additions & 0 deletions llvm-spirv/docs/SPIRVVersionsAndExtensionsHandling.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
=======================================
SPIR-V versions and extensions handling
=======================================

.. contents::
:local:

Overview
========

This document describes how the translator makes decisions about using
instructions from different version of the SPIR-V core and extension
specifications.

Being able to control the resulting SPIR-V version is important: the target
consumer might be quite old, without support for new SPIR-V versions and there
must be the possibility to control which version of the SPIR-V specification
that will be used during translation.

SPIR-V extensions is another thing which must be controllable. Extensions
can update and re-define semantics and validation rules for existing SPIR-V
entries and it is important to ensure that the translator is able to generate
valid SPIR-V according to the core spec, without uses of any extensions if such
SPIR-V was requested by user.

For example, without such infrastructure it is impossible to disable use of
``SPV_KHR_no_integer_wrap_decoration`` - it will be always generated if
corresponding LLVM IR counterparts are encountered in input module.

It is worth mentioning that SPIR-V versions and extensions the handling of
SPIR-V versions and extension is mostly important for the SPIR-V generation
step. On the consumer side it is the responsibility of the consumer to analyze
the incoming SPIR-V file and reject it if it contains something that is not
supported by the consumer.

However, translator simplifies this step for downstream users by checking
version and extensions in SPIR-V module during ``readSpirv``/``readSpirvModule``
phases.

SPIR-V Versions
===============

SPIR-V Generation step
----------------------

By default translator selects version of generated SPIR-V file based on features
used in this file. For example, if it contains ``dereferencable`` LLVM IR
attribute, ``MaxByteOffset`` decoration will be generated and resulting SPIR-V
version will be raised to 1.1.

.. note::
There is no documentation about which exact features from newest
SPIR-V spec versions will be used by the translator. If you are interested
when or why a particular SPIR-V instruction is generated, please check this
in the source code. Consider this as an implementation detail and if you
disagree with something, you can always open an issue or submit pull request
- contributions are welcome!

There is one option to control the behavior of the translator with respect to
the version of the SPIR-V file which is being generated/consumed.

* ``--spirv-max-version=`` - instructs the translator to generate SPIR-V file
corresponding to any spec version which is less than or equal to the
specified one. Behavior of the translator is the same as by default with only
one exception: resulting SPIR-V version cannot be raised higher than
specified by this option.

Allowed values are ``1.0`` and ``1.1``.

.. warning::
These two options are mutually exclusive and cannot be specified at the
same time.

If the translator encounters something that cannot be represented by set of
allowed SPIR-V versions (which might contain only one version), it does one of
the following things:

* ignores LLVM IR entity in the input file.

For example, ``dereferencable`` LLVM IR attribute can be ignored if it is not
allowed to generate SPIR-V 1.1 and higher.

* tries to represent LLVM IR entity with allowed instructions.

For example, ``OpPtrEqual`` can be used if SPIR-V 1.4 is not allowed and can
be emulated via ``OpConvertPtrToU`` + ``OpIEqual`` sequence.

* emits error if LLVM IR entity cannot be ignored and cannot be emulated using
available instructions.

For example, if global constructors/destructors
(represented by @llvm.global_ctors/@llvm.global_dtors) are present in a module
then the translator should emit error if it cannot use SPIR-V 1.1 and higher
where ``Initializer`` and ``Finalizer`` execution modes are described.

SPIR-V Consumption step
-----------------------

By default, translator consumes SPIR-V of any version which is supported.

This behavior, however, can be controlled via the same switches described in
the previous section.

If one of the switches present and translator encountered SPIR-V file
corresponding to a spec version which is not included into set of allowed
SPIR-V versions, translator emits error.

SPIR-V Extensions
=================

SPIR-V Generation step
----------------------

By default, translator doesn't use any extensions. If it required to enable
certain extension, the following command line option can be used:

* ``--spirv-ext=`` - allows to control list of allowed/disallowed extensions.

Valid value for this option is comma-separated list of extension names prefixed
with ``+`` or ``-`` - plus means allow to use extension, minus means disallow
to use extension. There is one more special value which can be used as extension
name in this option: ``all`` - it affects all extension which are known to the
translator.

If ``--spirv-ext`` contains name of extension which is not know for the
translator, it will emit error.

Examples:

* ``--spirv-ext=+SPV_KHR_no_integer_wrap_decoration,+SPV_INTEL_subgroups``
* ``--spirv-ext=+all,-SPV_INTEL_fpga_loop_controls``

.. warning::
Extension name cannot be allowed and disallowed at the same time: for inputs
like ``--spirv-ext=+SPV_INTEL_subgroups,-SPV_INTEL_subgroups`` translator
will emit error about invalid arguments.

.. note::
Since by default during SPIR-V generation all extensions are disabled, this
means that ``-all,`` is implicitly added at the beggining of the
``-spirv-ext`` value.

If the translator encounters something that cannot be represented by set of
allowed SPIR-V extensions (which might be empty), it does one of the following
things:

* ignores LLVM IR entity in the input file.

For example, ``nsw``/``nuw`` LLVM IR attributes can be ignored if it is not
allowed to generate SPIR-V 1.4 and ``SPV_KHR_no_integer_wrap_decoration``
extension is disallowed.

* tries to represent LLVM IR entity with allowed instructions.

Translator could translate calls to a new built-in functions defined by some
extensions as usual call instructions without using special SPIR-V
instructions.

However, this could result in a strange SPIR-V and most likely will lead to
errors during consumption. Having that, translator should emit errors if it
encounters a call to a built-in function from an extension which must be
represented as a special SPIR-V instruction from extension which wasn't
allowed to be used. I.e. if translator knows that this certain LLVM IR entity
belongs to an extension functionality and this extension is disallowed, it
should emit error rather than emulating it.

* emits error if LLVM IR entity cannot be ignored and cannot be emulated using
available instructions.

For example, new built-in types defined by
``cl_intel_device_side_avc_motion_estimation`` cannot be represented in SPIR-V
if ``SPV_INTEL_device_side_avc_motion_estimation`` is disallowed.

SPIR-V Consumption step
-----------------------

By default, translator consumes SPIR-V regardless of list extensions which are
used by the input file, i.e. all extensions are allowed by default during
consumption step.

.. note::
This is opposite to the generation step and this is done on purpose: to not
broke workflows of existing users of the translator.

.. note::
Since by default during SPIR-V consumption all extensions are enabled, this
means that ``+all,`` is implicitly added at the beggining of the
``-spirv-ext`` value.

This behavior, however, can be controlled via the same switches described in
the previous section.

If ``--spirv-ext`` switch presents, translator will emit error if it finds out
that input SPIR-V file uses disallowed extension.

.. note::
If the translator encounters unknown extension in the input SPIR-V file, it
will emit error regardless of ``-spirv-ext`` option value.

If one of the switches present and translator encountered SPIR-V file
corresponding to a spec version which is not included into set of allowed
SPIR-V versions, translator emits error.

How to control translator behavior when using it as library
===========================================================

When using translator as library it can be controlled via bunch of alternative
APIs that have additional argument: ``TranslatorOpts`` object which
encapsulates information about available SPIR-V versions and extensions.

List of new APIs is: ``readSpirvModule``, ``writeSpirv`` and ``readSpirv``.

.. note::
See ``LLVMSPIRVOpts.h`` for more details.

How to get ``TranslatorOpts`` object
------------------------------------

1. Default constructor. Equal to:

``--spirv-max-version=MaxKnownVersion --spirv-ext=-all``

.. note::
There is method ``TranslatorOpts::enableAllExtensions()`` that allows you
to quickly enable all known extensions if it is needed.

2. Constructor which accepts all parameters

Consumes both max SPIR-V version and optional map with extensions status
(i.e. which one is allowed and which one is disallowed)

Extensions status map
^^^^^^^^^^^^^^^^^^^^^

This map is defined as ``std::map<ExtensionID, bool>`` and it is intended to
show which extension is allowed to be used (``true`` as value) and which is not
(``false`` as value).

.. note::
If certain ``ExtensionID`` value is missed in the map, it automatically means
that extension is not allowed to be used.

This implies that by default, all extensions are disallowed.
15 changes: 15 additions & 0 deletions llvm-spirv/include/LLVMSPIRVExtensions.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#ifndef EXT
#error "EXT macro must be defined"
#endif

EXT(SPV_KHR_no_integer_wrap_decoration)
EXT(SPV_INTEL_subgroups)
EXT(SPV_INTEL_media_block_io)
EXT(SPV_INTEL_device_side_avc_motion_estimation)
EXT(SPV_INTEL_fpga_loop_controls)
EXT(SPV_INTEL_fpga_memory_attributes)
EXT(SPV_INTEL_unstructured_loop_controls)
EXT(SPV_INTEL_fpga_reg)
EXT(SPV_INTEL_blocking_pipes)
EXT(SPV_INTEL_function_pointers)
Loading