xref: /llvm-project/clang/docs/OpenCLSupport.rst (revision 11e2975810acd6abde9071818e03634d99492b54)
15e962e8dSSven van Haastregt.. raw:: html
25e962e8dSSven van Haastregt
35e962e8dSSven van Haastregt  <style type="text/css">
45e962e8dSSven van Haastregt    .none { background-color: #FFCCCC }
596ef4f4aSAaron Ballman    .part { background-color: #FFFF99 }
65e962e8dSSven van Haastregt    .good { background-color: #CCFF99 }
75e962e8dSSven van Haastregt  </style>
85e962e8dSSven van Haastregt
95e962e8dSSven van Haastregt.. role:: none
1096ef4f4aSAaron Ballman.. role:: part
115e962e8dSSven van Haastregt.. role:: good
125e962e8dSSven van Haastregt
135e962e8dSSven van Haastregt.. contents::
145e962e8dSSven van Haastregt   :local:
155e962e8dSSven van Haastregt
165e962e8dSSven van Haastregt==================
175e962e8dSSven van HaastregtOpenCL Support
185e962e8dSSven van Haastregt==================
195e962e8dSSven van Haastregt
20fdd615d4SAnastasia StulovaClang has complete support of OpenCL C versions from 1.0 to 3.0.
21fdd615d4SAnastasia StulovaSupport for OpenCL 3.0 is in experimental phase (:ref:`OpenCL 3.0 <opencl_300>`).
225e962e8dSSven van Haastregt
23adb77a74SAnastasia StulovaClang also supports :ref:`the C++ for OpenCL kernel language <cxx_for_opencl_impl>`.
245e962e8dSSven van Haastregt
2530ad1742SAnastasia StulovaThere are also other :ref:`new and experimental features <opencl_experimenal>`
2630ad1742SAnastasia Stulovaavailable.
27adb77a74SAnastasia Stulova
287df25978SAnastasia StulovaDetails about usage of clang for OpenCL can be found in :doc:`UsersManual`.
293087afb4SAnastasia Stulova
303087afb4SAnastasia StulovaMissing features or with limited support
313087afb4SAnastasia Stulova========================================
323087afb4SAnastasia Stulova
333087afb4SAnastasia Stulova- For general issues and bugs with OpenCL in clang refer to `the GitHub issue
3430ad1742SAnastasia Stulova  list
3530ad1742SAnastasia Stulova  <https://github.com/llvm/llvm-project/issues?q=is%3Aopen+is%3Aissue+label%3Aopencl>`__.
36adb77a74SAnastasia Stulova
37799b6b9fSKAWASHIMA Takahiro- Command-line flag :option:`-cl-ext` (used to override extensions/
383087afb4SAnastasia Stulova  features supported by a target) is missing support of some functionality i.e. that is
393087afb4SAnastasia Stulova  implemented fully through libraries (see :ref:`library-based features and
403087afb4SAnastasia Stulova  extensions <opencl_ext_libs>`).
413087afb4SAnastasia Stulova
42d7cc3a08SAnastasia StulovaInternals Manual
43d7cc3a08SAnastasia Stulova================
44d7cc3a08SAnastasia Stulova
45d7cc3a08SAnastasia StulovaThis section acts as internal documentation for OpenCL features design
46d7cc3a08SAnastasia Stulovaas well as some important implementation aspects. It is primarily targeted
47d7cc3a08SAnastasia Stulovaat the advanced users and the toolchain developers integrating frontend
48d7cc3a08SAnastasia Stulovafunctionality as a component.
49d7cc3a08SAnastasia Stulova
50d7cc3a08SAnastasia StulovaOpenCL Metadata
51d7cc3a08SAnastasia Stulova---------------
52d7cc3a08SAnastasia Stulova
53d7cc3a08SAnastasia StulovaClang uses metadata to provide additional OpenCL semantics in IR needed for
54d7cc3a08SAnastasia Stulovabackends and OpenCL runtime.
55d7cc3a08SAnastasia Stulova
56d7cc3a08SAnastasia StulovaEach kernel will have function metadata attached to it, specifying the arguments.
57d7cc3a08SAnastasia StulovaKernel argument metadata is used to provide source level information for querying
58d7cc3a08SAnastasia Stulovaat runtime, for example using the `clGetKernelArgInfo
59d7cc3a08SAnastasia Stulova<https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf#167>`_
60d7cc3a08SAnastasia Stulovacall.
61d7cc3a08SAnastasia Stulova
62d7cc3a08SAnastasia StulovaNote that ``-cl-kernel-arg-info`` enables more information about the original
63d7cc3a08SAnastasia Stulovakernel code to be added e.g. kernel parameter names will appear in the OpenCL
64d7cc3a08SAnastasia Stulovametadata along with other information.
65d7cc3a08SAnastasia Stulova
66d7cc3a08SAnastasia StulovaThe IDs used to encode the OpenCL's logical address spaces in the argument info
67d7cc3a08SAnastasia Stulovametadata follows the SPIR address space mapping as defined in the SPIR
68d7cc3a08SAnastasia Stulovaspecification `section 2.2
69d7cc3a08SAnastasia Stulova<https://www.khronos.org/registry/spir/specs/spir_spec-2.0.pdf#18>`_
70d7cc3a08SAnastasia Stulova
71d7cc3a08SAnastasia StulovaOpenCL Specific Options
72d7cc3a08SAnastasia Stulova-----------------------
73d7cc3a08SAnastasia Stulova
74d7cc3a08SAnastasia StulovaIn addition to the options described in :doc:`UsersManual` there are the
75d7cc3a08SAnastasia Stulovafollowing options specific to the OpenCL frontend.
76d7cc3a08SAnastasia Stulova
7790355d6fSAnastasia StulovaAll the options in this section are frontend-only and therefore if used
7890355d6fSAnastasia Stulovawith regular clang driver they require frontend forwarding, e.g. ``-cc1``
7990355d6fSAnastasia Stulovaor ``-Xclang``.
8090355d6fSAnastasia Stulova
8190355d6fSAnastasia Stulova.. _opencl_finclude_default_header:
8290355d6fSAnastasia Stulova
8390355d6fSAnastasia Stulova.. option:: -finclude-default-header
8490355d6fSAnastasia Stulova
8590355d6fSAnastasia StulovaAdds most of builtin types and function declarations during compilations. By
8690355d6fSAnastasia Stulovadefault the OpenCL headers are not loaded by the frontend and therefore certain
8790355d6fSAnastasia Stulovabuiltin types and most of builtin functions are not declared. To load them
8890355d6fSAnastasia Stulovaautomatically this flag can be passed to the frontend (see also :ref:`the
8990355d6fSAnastasia Stulovasection on the OpenCL Header <opencl_header>`):
9090355d6fSAnastasia Stulova
9190355d6fSAnastasia Stulova   .. code-block:: console
9290355d6fSAnastasia Stulova
9390355d6fSAnastasia Stulova     $ clang -Xclang -finclude-default-header test.cl
9490355d6fSAnastasia Stulova
9590355d6fSAnastasia StulovaAlternatively the internal header `opencl-c.h` containing the declarations
9690355d6fSAnastasia Stulovacan be included manually using ``-include`` or ``-I`` followed by the path
9790355d6fSAnastasia Stulovato the header location. The header can be found in the clang source tree or
9890355d6fSAnastasia Stulovainstallation directory.
9990355d6fSAnastasia Stulova
10090355d6fSAnastasia Stulova   .. code-block:: console
10190355d6fSAnastasia Stulova
10290355d6fSAnastasia Stulova     $ clang -I<path to clang sources>/lib/Headers/opencl-c.h test.cl
10390355d6fSAnastasia Stulova     $ clang -I<path to clang installation>/lib/clang/<llvm version>/include/opencl-c.h/opencl-c.h test.cl
10490355d6fSAnastasia Stulova
10590355d6fSAnastasia StulovaIn this example it is assumed that the kernel code contains
10690355d6fSAnastasia Stulova``#include <opencl-c.h>`` just as a regular C include.
10790355d6fSAnastasia Stulova
10890355d6fSAnastasia StulovaBecause the header is very large and long to parse, PCH (:doc:`PCHInternals`)
10990355d6fSAnastasia Stulovaand modules (:doc:`Modules`) can be used internally to improve the compilation
11090355d6fSAnastasia Stulovaspeed.
11190355d6fSAnastasia Stulova
11290355d6fSAnastasia StulovaTo enable modules for OpenCL:
11390355d6fSAnastasia Stulova
11490355d6fSAnastasia Stulova   .. code-block:: console
11590355d6fSAnastasia Stulova
11626182dfaSFangrui Song     $ clang --target=spir-unknown-unknown -c -emit-llvm -Xclang -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=<path to the generated module> test.cl
11790355d6fSAnastasia Stulova
11890355d6fSAnastasia StulovaAnother way to circumvent long parsing latency for the OpenCL builtin
11990355d6fSAnastasia Stulovadeclarations is to use mechanism enabled by :ref:`-fdeclare-opencl-builtins
12090355d6fSAnastasia Stulova<opencl_fdeclare_opencl_builtins>` flag that is available as an alternative
12190355d6fSAnastasia Stulovafeature.
12290355d6fSAnastasia Stulova
12390355d6fSAnastasia Stulova.. _opencl_fdeclare_opencl_builtins:
12490355d6fSAnastasia Stulova
12590355d6fSAnastasia Stulova.. option:: -fdeclare-opencl-builtins
12690355d6fSAnastasia Stulova
12790355d6fSAnastasia StulovaIn addition to regular header includes with builtin types and functions using
12890355d6fSAnastasia Stulova:ref:`-finclude-default-header <opencl_finclude_default_header>`, clang
12990355d6fSAnastasia Stulovasupports a fast mechanism to declare builtin functions with
13090355d6fSAnastasia Stulova``-fdeclare-opencl-builtins``. This does not declare the builtin types and
13190355d6fSAnastasia Stulovatherefore it has to be used in combination with ``-finclude-default-header``
13290355d6fSAnastasia Stulovaif full functionality is required.
13390355d6fSAnastasia Stulova
13490355d6fSAnastasia Stulova**Example of Use**:
13590355d6fSAnastasia Stulova
13690355d6fSAnastasia Stulova    .. code-block:: console
13790355d6fSAnastasia Stulova
13890355d6fSAnastasia Stulova      $ clang -Xclang -fdeclare-opencl-builtins test.cl
13990355d6fSAnastasia Stulova
140d7cc3a08SAnastasia Stulova.. _opencl_fake_address_space_map:
141d7cc3a08SAnastasia Stulova
142d7cc3a08SAnastasia Stulova.. option:: -ffake-address-space-map
143d7cc3a08SAnastasia Stulova
144d7cc3a08SAnastasia StulovaOverrides the target address space map with a fake map.
145d7cc3a08SAnastasia StulovaThis allows adding explicit address space IDs to the bitcode for non-segmented
146d7cc3a08SAnastasia Stulovamemory architectures that do not have separate IDs for each of the OpenCL
147d7cc3a08SAnastasia Stulovalogical address spaces by default. Passing ``-ffake-address-space-map`` will
148d7cc3a08SAnastasia Stulovaadd/override address spaces of the target compiled for with the following values:
149d7cc3a08SAnastasia Stulova``1-global``, ``2-constant``, ``3-local``, ``4-generic``. The private address
150d7cc3a08SAnastasia Stulovaspace is represented by the absence of an address space attribute in the IR (see
151d7cc3a08SAnastasia Stulovaalso :ref:`the section on the address space attribute <opencl_addrsp>`).
152d7cc3a08SAnastasia Stulova
153d7cc3a08SAnastasia Stulova   .. code-block:: console
154d7cc3a08SAnastasia Stulova
155d7cc3a08SAnastasia Stulova     $ clang -cc1 -ffake-address-space-map test.cl
156d7cc3a08SAnastasia Stulova
157bafcb4c6SAnastasia Stulova.. _opencl_builtins:
158bafcb4c6SAnastasia Stulova
159d7cc3a08SAnastasia StulovaOpenCL builtins
160d7cc3a08SAnastasia Stulova---------------
161d7cc3a08SAnastasia Stulova
16218a70797SSven van Haastregt**Clang builtins**
16318a70797SSven van Haastregt
164d7cc3a08SAnastasia StulovaThere are some standard OpenCL functions that are implemented as Clang builtins:
165d7cc3a08SAnastasia Stulova
166d7cc3a08SAnastasia Stulova- All pipe functions from `section 6.13.16.2/6.13.16.3
167d7cc3a08SAnastasia Stulova  <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#160>`_ of
16818f16c94SSven van Haastregt  the OpenCL v2.0 kernel language specification.
169d7cc3a08SAnastasia Stulova
170d7cc3a08SAnastasia Stulova- Address space qualifier conversion functions ``to_global``/``to_local``/``to_private``
171d7cc3a08SAnastasia Stulova  from `section 6.13.9
172d7cc3a08SAnastasia Stulova  <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#101>`_.
173d7cc3a08SAnastasia Stulova
174d7cc3a08SAnastasia Stulova- All the ``enqueue_kernel`` functions from `section 6.13.17.1
175d7cc3a08SAnastasia Stulova  <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#164>`_ and
176d7cc3a08SAnastasia Stulova  enqueue query functions from `section 6.13.17.5
177d7cc3a08SAnastasia Stulova  <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#171>`_.
178d7cc3a08SAnastasia Stulova
17918a70797SSven van Haastregt**Fast builtin function declarations**
18018a70797SSven van Haastregt
18118a70797SSven van HaastregtThe implementation of the fast builtin function declarations (available via the
18290355d6fSAnastasia Stulova:ref:`-fdeclare-opencl-builtins option <opencl_fdeclare_opencl_builtins>`) consists
18390355d6fSAnastasia Stulovaof the following main components:
18418a70797SSven van Haastregt
18518a70797SSven van Haastregt- A TableGen definitions file ``OpenCLBuiltins.td``.  This contains a compact
18618a70797SSven van Haastregt  representation of the supported builtin functions.  When adding new builtin
18718a70797SSven van Haastregt  function declarations, this is normally the only file that needs modifying.
18818a70797SSven van Haastregt
18918a70797SSven van Haastregt- A Clang TableGen emitter defined in ``ClangOpenCLBuiltinEmitter.cpp``.  During
19018a70797SSven van Haastregt  Clang build time, the emitter reads the TableGen definition file and
19118a70797SSven van Haastregt  generates ``OpenCLBuiltins.inc``.  This generated file contains various tables
19218a70797SSven van Haastregt  and functions that capture the builtin function data from the TableGen
19318a70797SSven van Haastregt  definitions in a compact manner.
19418a70797SSven van Haastregt
19518a70797SSven van Haastregt- OpenCL specific code in ``SemaLookup.cpp``.  When ``Sema::LookupBuiltin``
19618a70797SSven van Haastregt  encounters a potential builtin function, it will check if the name corresponds
19718a70797SSven van Haastregt  to a valid OpenCL builtin function.  If so, all overloads of the function are
19818a70797SSven van Haastregt  inserted using ``InsertOCLBuiltinDeclarationsFromTable`` and overload
19918a70797SSven van Haastregt  resolution takes place.
20018a70797SSven van Haastregt
201bafcb4c6SAnastasia StulovaOpenCL Extensions and Features
202bafcb4c6SAnastasia Stulova------------------------------
203bafcb4c6SAnastasia Stulova
204bafcb4c6SAnastasia StulovaClang implements various extensions to OpenCL kernel languages.
205bafcb4c6SAnastasia Stulova
206bafcb4c6SAnastasia StulovaNew functionality is accepted as soon as the documentation is detailed to the
207bafcb4c6SAnastasia Stulovalevel sufficient to be implemented. There should be an evidence that the
208bafcb4c6SAnastasia Stulovaextension is designed with implementation feasibility in consideration and
209bafcb4c6SAnastasia Stulovaassessment of complexity for C/C++ based compilers. Alternatively, the
210bafcb4c6SAnastasia Stulovadocumentation can be accepted in a format of a draft that can be further
211bafcb4c6SAnastasia Stulovarefined during the implementation.
212bafcb4c6SAnastasia Stulova
213bafcb4c6SAnastasia StulovaImplementation guidelines
214bafcb4c6SAnastasia Stulova^^^^^^^^^^^^^^^^^^^^^^^^^
215bafcb4c6SAnastasia Stulova
216bafcb4c6SAnastasia StulovaThis section explains how to extend clang with the new functionality.
217bafcb4c6SAnastasia Stulova
218bafcb4c6SAnastasia Stulova**Parsing functionality**
219bafcb4c6SAnastasia Stulova
220bafcb4c6SAnastasia StulovaIf an extension modifies the standard parsing it needs to be added to
221bafcb4c6SAnastasia Stulovathe clang frontend source code. This also means that the associated macro
222bafcb4c6SAnastasia Stulovaindicating the presence of the extension should be added to clang.
223bafcb4c6SAnastasia Stulova
224bafcb4c6SAnastasia StulovaThe default flow for adding a new extension into the frontend is to
225bafcb4c6SAnastasia Stulovamodify `OpenCLExtensions.def
2263087afb4SAnastasia Stulova<https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Basic/OpenCLExtensions.def>`__,
2273087afb4SAnastasia Stulovacontaining the list of all extensions and optional features supported by
2283087afb4SAnastasia Stulovathe frontend.
229bafcb4c6SAnastasia Stulova
230bafcb4c6SAnastasia StulovaThis will add the macro automatically and also add a field in the target
231bafcb4c6SAnastasia Stulovaoptions ``clang::TargetOptions::OpenCLFeaturesMap`` to control the exposure
232bafcb4c6SAnastasia Stulovaof the new extension during the compilation.
233bafcb4c6SAnastasia Stulova
2343087afb4SAnastasia StulovaNote that by default targets like `SPIR-V`, `SPIR` or `X86` expose all the OpenCL
235bafcb4c6SAnastasia Stulovaextensions. For all other targets the configuration has to be made explicitly.
236bafcb4c6SAnastasia Stulova
237bafcb4c6SAnastasia StulovaNote that the target extension support performed by clang can be overridden
238799b6b9fSKAWASHIMA Takahirowith :option:`-cl-ext` command-line flags.
239bafcb4c6SAnastasia Stulova
2403087afb4SAnastasia Stulova.. _opencl_ext_libs:
2413087afb4SAnastasia Stulova
242bafcb4c6SAnastasia Stulova**Library functionality**
243bafcb4c6SAnastasia Stulova
244bafcb4c6SAnastasia StulovaIf an extension adds functionality that does not modify standard language
24522fdf617SSven van Haastregtparsing it should not require modifying anything other than header files and
246bafcb4c6SAnastasia Stulova``OpenCLBuiltins.td`` detailed in :ref:`OpenCL builtins <opencl_builtins>`.
247bafcb4c6SAnastasia StulovaMost commonly such extensions add functionality via libraries (by adding
248bafcb4c6SAnastasia Stulovanon-native types or functions) parsed regularly. Similar to other languages this
249bafcb4c6SAnastasia Stulovais the most common way to add new functionality.
250bafcb4c6SAnastasia Stulova
251bafcb4c6SAnastasia StulovaClang has standard headers where new types and functions are being added,
252bafcb4c6SAnastasia Stulovafor more details refer to
253bafcb4c6SAnastasia Stulova:ref:`the section on the OpenCL Header <opencl_header>`. The macros indicating
254bafcb4c6SAnastasia Stulovathe presence of such extensions can be added in the standard header files
255bafcb4c6SAnastasia Stulovaconditioned on target specific predefined macros or/and language version
2563087afb4SAnastasia Stulovapredefined macros (see `feature/extension preprocessor macros defined in
2573087afb4SAnastasia Stulovaopencl-c-base.h
2583087afb4SAnastasia Stulova<https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/opencl-c-base.h>`__).
259bafcb4c6SAnastasia Stulova
260bafcb4c6SAnastasia Stulova**Pragmas**
261bafcb4c6SAnastasia Stulova
262bafcb4c6SAnastasia StulovaSome extensions alter standard parsing dynamically via pragmas.
263bafcb4c6SAnastasia Stulova
264bafcb4c6SAnastasia StulovaClang provides a mechanism to add the standard extension pragma
265bafcb4c6SAnastasia Stulova``OPENCL EXTENSION`` by setting a dedicated flag in the extension list entry of
266bafcb4c6SAnastasia Stulova``OpenCLExtensions.def``. Note that there is no default behavior for the
267bafcb4c6SAnastasia Stulovastandard extension pragmas as it is not specified (for the standards up to and
268bafcb4c6SAnastasia Stulovaincluding version 3.0) in a sufficient level of detail and, therefore,
269bafcb4c6SAnastasia Stulovathere is no default functionality provided by clang.
270bafcb4c6SAnastasia Stulova
271bafcb4c6SAnastasia StulovaPragmas without detailed information of their behavior (e.g. an explanation of
272bafcb4c6SAnastasia Stulovachanges it triggers in the parsing) should not be added to clang. Moreover, the
273bafcb4c6SAnastasia Stulovapragmas should provide useful functionality to the user. For example, such
274bafcb4c6SAnastasia Stulovafunctionality should address a practical use case and not be redundant i.e.
275bafcb4c6SAnastasia Stulovacannot be achieved using existing features.
276bafcb4c6SAnastasia Stulova
277bafcb4c6SAnastasia StulovaNote that some legacy extensions (published prior to OpenCL 3.0) still
278bafcb4c6SAnastasia Stulovaprovide some non-conformant functionality for pragmas e.g. add diagnostics on
279bafcb4c6SAnastasia Stulovathe use of types or functions. This functionality is not guaranteed to remain in
280bafcb4c6SAnastasia Stulovafuture releases. However, any future changes should not affect backward
281bafcb4c6SAnastasia Stulovacompatibility.
282bafcb4c6SAnastasia Stulova
283d7cc3a08SAnastasia Stulova.. _opencl_addrsp:
284d7cc3a08SAnastasia Stulova
285d7cc3a08SAnastasia StulovaAddress spaces attribute
286d7cc3a08SAnastasia Stulova------------------------
287d7cc3a08SAnastasia Stulova
288d7cc3a08SAnastasia StulovaClang has arbitrary address space support using the ``address_space(N)``
289d7cc3a08SAnastasia Stulovaattribute, where ``N`` is an integer number in the range specified in the
290d7cc3a08SAnastasia StulovaClang source code. This addresses spaces can be used along with the OpenCL
291d7cc3a08SAnastasia Stulovaaddress spaces however when such addresses spaces converted to/from OpenCL
292d7cc3a08SAnastasia Stulovaaddress spaces the behavior is not governed by OpenCL specification.
293d7cc3a08SAnastasia Stulova
294d7cc3a08SAnastasia StulovaAn OpenCL implementation provides a list of standard address spaces using
295d7cc3a08SAnastasia Stulovakeywords: ``private``, ``local``, ``global``, and ``generic``. In the AST and
296d7cc3a08SAnastasia Stulovain the IR each of the address spaces will be represented by unique number
297d7cc3a08SAnastasia Stulovaprovided in the Clang source code. The specific IDs for an address space do not
298d7cc3a08SAnastasia Stulovahave to match between the AST and the IR. Typically in the AST address space
299d7cc3a08SAnastasia Stulovanumbers represent logical segments while in the IR they represent physical
300d7cc3a08SAnastasia Stulovasegments.
301d7cc3a08SAnastasia StulovaTherefore, machines with flat memory segments can map all AST address space
302d7cc3a08SAnastasia Stulovanumbers to the same physical segment ID or skip address space attribute
303d7cc3a08SAnastasia Stulovacompletely while generating the IR. However, if the address space information
304d7cc3a08SAnastasia Stulovais needed by the IR passes e.g. to improve alias analysis, it is recommended
305d7cc3a08SAnastasia Stulovato keep it and only lower to reflect physical memory segments in the late
306d7cc3a08SAnastasia Stulovamachine passes. The mapping between logical and target address spaces is
307d7cc3a08SAnastasia Stulovaspecified in the Clang's source code.
308d7cc3a08SAnastasia Stulova
309adb77a74SAnastasia Stulova.. _cxx_for_opencl_impl:
3105e962e8dSSven van Haastregt
3115e962e8dSSven van HaastregtC++ for OpenCL Implementation Status
3125e962e8dSSven van Haastregt====================================
3135e962e8dSSven van Haastregt
31430ad1742SAnastasia StulovaClang implements language versions 1.0 and 2021 published in `the official
315adb77a74SAnastasia Stulovarelease of C++ for OpenCL Documentation
31630ad1742SAnastasia Stulova<https://github.com/KhronosGroup/OpenCL-Docs/releases/tag/cxxforopencl-docrev2021.12>`_.
317adb77a74SAnastasia Stulova
318bc84f89cSAnastasia StulovaLimited support of experimental C++ libraries is described in the :ref:`experimental features <opencl_experimenal>`.
319adb77a74SAnastasia Stulova
32030ad1742SAnastasia StulovaGitHub issues for this functionality are typically prefixed
321adb77a74SAnastasia Stulovawith '[C++4OpenCL]' - click `here
32230ad1742SAnastasia Stulova<https://github.com/llvm/llvm-project/issues?q=is%3Aissue+is%3Aopen+%5BC%2B%2B4OpenCL%5D>`__
323adb77a74SAnastasia Stulovato view the full bug list.
3245e962e8dSSven van Haastregt
3255e962e8dSSven van Haastregt
3265e962e8dSSven van HaastregtMissing features or with limited support
3275e962e8dSSven van Haastregt----------------------------------------
3285e962e8dSSven van Haastregt
32930ad1742SAnastasia Stulova- Support of C++ for OpenCL 2021 is currently in experimental phase. Refer to
33030ad1742SAnastasia Stulova  :ref:`OpenCL 3.0 status <opencl_300>` for details of common missing
33130ad1742SAnastasia Stulova  functionality from OpenCL 3.0.
33230ad1742SAnastasia Stulova
33330ad1742SAnastasia Stulova- IR generation for non-trivial global destructors is incomplete (See:
334adb77a74SAnastasia Stulova  `PR48047 <https://llvm.org/PR48047>`_).
3355e962e8dSSven van Haastregt
336*11e29758SKazu Hirata- Support of `destructors with non-default address spaces
33730ad1742SAnastasia Stulova  <https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html#_construction_initialization_and_destruction>`_
33830ad1742SAnastasia Stulova  is incomplete (See: `D109609 <https://reviews.llvm.org/D109609>`_).
33930ad1742SAnastasia Stulova
340adb77a74SAnastasia Stulova.. _opencl_300:
341adb77a74SAnastasia Stulova
34282690578SAnton ZabaznovOpenCL C 3.0 Usage
3435ccc79dcSAnastasia Stulova==================
34482690578SAnton Zabaznov
34582690578SAnton ZabaznovOpenCL C 3.0 language standard makes most OpenCL C 2.0 features optional. Optional
34682690578SAnton Zabaznovfunctionality in OpenCL C 3.0 is indicated with the presence of feature-test macros
34796ef4f4aSAaron Ballman(list of feature-test macros is `here <https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html#features>`__).
348799b6b9fSKAWASHIMA TakahiroCommand-line flag :option:`-cl-ext` can be used to override features supported by a target.
34982690578SAnton Zabaznov
35082690578SAnton ZabaznovFor cases when there is an associated extension for a specific feature (fp64 and 3d image writes)
35182690578SAnton Zabaznovuser should specify both (extension and feature) in command-line flag:
35282690578SAnton Zabaznov
35382690578SAnton Zabaznov   .. code-block:: console
35482690578SAnton Zabaznov
3553087afb4SAnastasia Stulova     $ clang -cl-std=CL3.0 -cl-ext=+cl_khr_fp64,+__opencl_c_fp64 ...
3563087afb4SAnastasia Stulova     $ clang -cl-std=CL3.0 -cl-ext=-cl_khr_fp64,-__opencl_c_fp64 ...
3573087afb4SAnastasia Stulova
35882690578SAnton Zabaznov
35982690578SAnton Zabaznov
36082690578SAnton ZabaznovOpenCL C 3.0 Implementation Status
3615ccc79dcSAnastasia Stulova----------------------------------
362adb77a74SAnastasia Stulova
363adb77a74SAnastasia StulovaThe following table provides an overview of features in OpenCL C 3.0 and their
364adb77a74SAnastasia Stulovaimplementation status.
365adb77a74SAnastasia Stulova
366fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
367adb77a74SAnastasia Stulova| Category                     | Feature                                                           | Status               | Reviews                                                                                                                        |
368fdd615d4SAnastasia Stulova+==============================+=========================+=========================================+======================+================================================================================================================================+
369adb77a74SAnastasia Stulova| Command line interface       | New value for ``-cl-std`` flag                                    | :good:`done`         | https://reviews.llvm.org/D88300                                                                                                |
370fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
371adb77a74SAnastasia Stulova| Predefined macros            | New version macro                                                 | :good:`done`         | https://reviews.llvm.org/D88300                                                                                                |
372fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
373a8192566SAnastasia Stulova| Predefined macros            | Feature macros                                                    | :good:`done`         | https://reviews.llvm.org/D95776                                                                                                |
374fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
375cff03d5fSAnastasia Stulova| Feature optionality          | Generic address space                                             | :good:`done`         | https://reviews.llvm.org/D95778 and https://reviews.llvm.org/D103401                                                           |
376fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
377fdd615d4SAnastasia Stulova| Feature optionality          | Builtin function overloads with generic address space             | :good:`done`         | https://reviews.llvm.org/D105526, https://reviews.llvm.org/D107769                                                             |
378fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
379cff03d5fSAnastasia Stulova| Feature optionality          | Program scope variables in global memory                          | :good:`done`         | https://reviews.llvm.org/D103191                                                                                               |
380fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
381fdd615d4SAnastasia Stulova| Feature optionality          | 3D image writes including builtin functions                       | :good:`done`         | https://reviews.llvm.org/D106260 (frontend)                                                                                    |
382fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
383fdd615d4SAnastasia Stulova| Feature optionality          | read_write images including builtin functions                     | :good:`done`         | https://reviews.llvm.org/D104915 (frontend) and https://reviews.llvm.org/D107539, https://reviews.llvm.org/D117899 (functions) |
384fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
385fdd615d4SAnastasia Stulova| Feature optionality          | C11 atomics memory scopes, ordering and builtin function          | :good:`done`         | https://reviews.llvm.org/D106111, https://reviews.llvm.org/D119420                                                             |
386fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
387fdd615d4SAnastasia Stulova| Feature optionality          | Blocks and Device-side kernel enqueue including builtin functions | :good:`done`         | https://reviews.llvm.org/D115640, https://reviews.llvm.org/D118605                                                             |
388fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
389fbe00c68SAnastasia Stulova| Feature optionality          | Pipes including builtin functions                                 | :good:`done`         | https://reviews.llvm.org/D107154 (frontend) and https://reviews.llvm.org/D105858 (functions)                                   |
390fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
391fbe00c68SAnastasia Stulova| Feature optionality          | Work group collective builtin functions                           | :good:`done`         | https://reviews.llvm.org/D105858                                                                                               |
392fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
393cff03d5fSAnastasia Stulova| Feature optionality          | Image types and builtin functions                                 | :good:`done`         | https://reviews.llvm.org/D103911 (frontend) and https://reviews.llvm.org/D107539 (functions)                                   |
394fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
395cff03d5fSAnastasia Stulova| Feature optionality          | Double precision floating point type                              | :good:`done`         | https://reviews.llvm.org/D96524                                                                                                |
396fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
397856c49d7SSven van Haastregt| New functionality            | RGBA vector components                                            | :good:`done`         | https://reviews.llvm.org/D99969                                                                                                |
398fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
399fdd615d4SAnastasia Stulova| New functionality            | Subgroup functions                                                | :good:`done`         | https://reviews.llvm.org/D105858, https://reviews.llvm.org/D118999                                                             |
400fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
401fdd615d4SAnastasia Stulova| New functionality            | Atomic mem scopes: subgroup, all devices including functions      | :good:`done`         | https://reviews.llvm.org/D103241                                                                                               |
402fdd615d4SAnastasia Stulova+------------------------------+-------------------------+-----------------------------------------+----------------------+--------------------------------------------------------------------------------------------------------------------------------+
403adb77a74SAnastasia Stulova
404adb77a74SAnastasia Stulova.. _opencl_experimenal:
4050ef2b68fSAnastasia Stulova
4060ef2b68fSAnastasia StulovaExperimental features
4070ef2b68fSAnastasia Stulova=====================
4080ef2b68fSAnastasia Stulova
4090ef2b68fSAnastasia StulovaClang provides the following new WIP features for the developers to experiment
4100ef2b68fSAnastasia Stulovaand provide early feedback or contribute with further improvements.
411eb1ffd81StlattnerFeel free to contact us on `the Discourse forums (Clang Frontend category)
412eb1ffd81Stlattner<https://discourse.llvm.org/c/clang/6>`_ or file `a GitHub issue
41330ad1742SAnastasia Stulova<https://github.com/llvm/llvm-project/issues/new>`_.
4140ef2b68fSAnastasia Stulova
4156e8601ffSAnastasia Stulova.. _opencl_experimental_cxxlibs:
4167c541a19SAnastasia Stulova
4170ef2b68fSAnastasia StulovaC++ libraries for OpenCL
4180ef2b68fSAnastasia Stulova------------------------
4190ef2b68fSAnastasia Stulova
4200ef2b68fSAnastasia StulovaThere is ongoing work to support C++ standard libraries from `LLVM's libcxx
4210ef2b68fSAnastasia Stulova<https://libcxx.llvm.org/>`_ in OpenCL kernel code using C++ for OpenCL mode.
4220ef2b68fSAnastasia Stulova
4230ef2b68fSAnastasia StulovaIt is currently possible to include `type_traits` from C++17 in the kernel
4240ef2b68fSAnastasia Stulovasources when the following clang extensions are enabled
4250ef2b68fSAnastasia Stulova``__cl_clang_function_pointers`` and ``__cl_clang_variadic_functions``,
4260ef2b68fSAnastasia Stulovasee :doc:`LanguageExtensions` for more details. The use of non-conformant
4270ef2b68fSAnastasia Stulovafeatures enabled by the extensions does not expose non-conformant behavior
4280ef2b68fSAnastasia Stulovabeyond the compilation i.e. does not get generated in IR or binary.
4290ef2b68fSAnastasia StulovaThe extension only appear in metaprogramming
4300ef2b68fSAnastasia Stulovamechanism to identify or verify the properties of types. This allows to provide
4310ef2b68fSAnastasia Stulovathe full C++ functionality without a loss of portability. To avoid unsafe use
4320ef2b68fSAnastasia Stulovaof the extensions it is recommended that the extensions are disabled directly
4330ef2b68fSAnastasia Stulovaafter the header include.
4340ef2b68fSAnastasia Stulova
4350ef2b68fSAnastasia Stulova**Example of Use**:
4360ef2b68fSAnastasia Stulova
4370ef2b68fSAnastasia StulovaThe example of kernel code with `type_traits` is illustrated here.
4380ef2b68fSAnastasia Stulova
4390ef2b68fSAnastasia Stulova.. code-block:: c++
4400ef2b68fSAnastasia Stulova
4410ef2b68fSAnastasia Stulova  #pragma OPENCL EXTENSION __cl_clang_function_pointers : enable
4420ef2b68fSAnastasia Stulova  #pragma OPENCL EXTENSION __cl_clang_variadic_functions : enable
4430ef2b68fSAnastasia Stulova  #include <type_traits>
4440ef2b68fSAnastasia Stulova  #pragma OPENCL EXTENSION __cl_clang_function_pointers : disable
4450ef2b68fSAnastasia Stulova  #pragma OPENCL EXTENSION __cl_clang_variadic_functions : disable
4460ef2b68fSAnastasia Stulova
4470ef2b68fSAnastasia Stulova  using sint_type = std::make_signed<unsigned int>::type;
4480ef2b68fSAnastasia Stulova
4490ef2b68fSAnastasia Stulova  __kernel void foo() {
4500ef2b68fSAnastasia Stulova    static_assert(!std::is_same<sint_type, unsigned int>::value);
4510ef2b68fSAnastasia Stulova  }
4520ef2b68fSAnastasia Stulova
4530ef2b68fSAnastasia StulovaThe possible clang invocation to compile the example is as follows:
4540ef2b68fSAnastasia Stulova
4550ef2b68fSAnastasia Stulova   .. code-block:: console
4560ef2b68fSAnastasia Stulova
457f372ff17SOle Strohm     $ clang -I<path to libcxx checkout or installation>/include test.clcpp
4580ef2b68fSAnastasia Stulova
4590ef2b68fSAnastasia StulovaNote that `type_traits` is a header only library and therefore no extra
4607c541a19SAnastasia Stulovalinking step against the standard libraries is required. See full example
4617c541a19SAnastasia Stulovain `Compiler Explorer <https://godbolt.org/z/5WbnTfb65>`_.
4629685631cSAnastasia Stulova
4639685631cSAnastasia StulovaMore OpenCL specific C++ library implementations built on top of libcxx
4649685631cSAnastasia Stulovaare available in `libclcxx <https://github.com/KhronosGroup/libclcxx>`_
4659685631cSAnastasia Stulovaproject.
466