1.. raw:: html 2 3 <style type="text/css"> 4 .none { background-color: #FFCCCC } 5 .partial { background-color: #FFFF99 } 6 .good { background-color: #CCFF99 } 7 </style> 8 9.. role:: none 10.. role:: partial 11.. role:: good 12 13.. contents:: 14 :local: 15 16================== 17OpenCL Support 18================== 19 20Clang has complete support of OpenCL C versions from 1.0 to 2.0. 21 22Clang also supports :ref:`the C++ for OpenCL kernel language <cxx_for_opencl_impl>`. 23 24There is an ongoing work to support :ref:`OpenCL 3.0 <opencl_300>`. 25 26There are also other :ref:`new and experimental features <opencl_experimenal>` available. 27 28For general issues and bugs with OpenCL in clang refer to `Bugzilla 29<https://bugs.llvm.org/buglist.cgi?component=OpenCL&list_id=172679&product=clang&resolution=--->`__. 30 31Internals Manual 32================ 33 34This section acts as internal documentation for OpenCL features design 35as well as some important implementation aspects. It is primarily targeted 36at the advanced users and the toolchain developers integrating frontend 37functionality as a component. 38 39OpenCL Metadata 40--------------- 41 42Clang uses metadata to provide additional OpenCL semantics in IR needed for 43backends and OpenCL runtime. 44 45Each kernel will have function metadata attached to it, specifying the arguments. 46Kernel argument metadata is used to provide source level information for querying 47at runtime, for example using the `clGetKernelArgInfo 48<https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf#167>`_ 49call. 50 51Note that ``-cl-kernel-arg-info`` enables more information about the original 52kernel code to be added e.g. kernel parameter names will appear in the OpenCL 53metadata along with other information. 54 55The IDs used to encode the OpenCL's logical address spaces in the argument info 56metadata follows the SPIR address space mapping as defined in the SPIR 57specification `section 2.2 58<https://www.khronos.org/registry/spir/specs/spir_spec-2.0.pdf#18>`_ 59 60OpenCL Specific Options 61----------------------- 62 63In addition to the options described in :doc:`UsersManual` there are the 64following options specific to the OpenCL frontend. 65 66All the options in this section are frontend-only and therefore if used 67with regular clang driver they require frontend forwarding, e.g. ``-cc1`` 68or ``-Xclang``. 69 70.. _opencl_cl_ext: 71 72.. option:: -cl-ext 73 74Disables support of OpenCL extensions. All OpenCL targets provide a list 75of extensions that they support. Clang allows to amend this using the ``-cl-ext`` 76flag with a comma-separated list of extensions prefixed with ``'+'`` or ``'-'``. 77The syntax: ``-cl-ext=<(['-'|'+']<extension>[,])+>``, where extensions 78can be either one of `the OpenCL published extensions 79<https://www.khronos.org/registry/OpenCL>`_ 80or any vendor extension. Alternatively, ``'all'`` can be used to enable 81or disable all known extensions. 82 83Example disabling double support for the 64-bit SPIR target: 84 85 .. code-block:: console 86 87 $ clang -cc1 -triple spir64-unknown-unknown -cl-ext=-cl_khr_fp64 test.cl 88 89Enabling all extensions except double support in R600 AMD GPU can be done using: 90 91 .. code-block:: console 92 93 $ clang -cc1 -triple r600-unknown-unknown -cl-ext=-all,+cl_khr_fp16 test.cl 94 95.. _opencl_finclude_default_header: 96 97.. option:: -finclude-default-header 98 99Adds most of builtin types and function declarations during compilations. By 100default the OpenCL headers are not loaded by the frontend and therefore certain 101builtin types and most of builtin functions are not declared. To load them 102automatically this flag can be passed to the frontend (see also :ref:`the 103section on the OpenCL Header <opencl_header>`): 104 105 .. code-block:: console 106 107 $ clang -Xclang -finclude-default-header test.cl 108 109Alternatively the internal header `opencl-c.h` containing the declarations 110can be included manually using ``-include`` or ``-I`` followed by the path 111to the header location. The header can be found in the clang source tree or 112installation directory. 113 114 .. code-block:: console 115 116 $ clang -I<path to clang sources>/lib/Headers/opencl-c.h test.cl 117 $ clang -I<path to clang installation>/lib/clang/<llvm version>/include/opencl-c.h/opencl-c.h test.cl 118 119In this example it is assumed that the kernel code contains 120``#include <opencl-c.h>`` just as a regular C include. 121 122Because the header is very large and long to parse, PCH (:doc:`PCHInternals`) 123and modules (:doc:`Modules`) can be used internally to improve the compilation 124speed. 125 126To enable modules for OpenCL: 127 128 .. code-block:: console 129 130 $ clang -target spir-unknown-unknown -c -emit-llvm -Xclang -finclude-default-header -fmodules -fimplicit-module-maps -fm odules-cache-path=<path to the generated module> test.cl 131 132Another way to circumvent long parsing latency for the OpenCL builtin 133declarations is to use mechanism enabled by :ref:`-fdeclare-opencl-builtins 134<opencl_fdeclare_opencl_builtins>` flag that is available as an alternative 135feature. 136 137.. _opencl_fdeclare_opencl_builtins: 138 139.. option:: -fdeclare-opencl-builtins 140 141In addition to regular header includes with builtin types and functions using 142:ref:`-finclude-default-header <opencl_finclude_default_header>`, clang 143supports a fast mechanism to declare builtin functions with 144``-fdeclare-opencl-builtins``. This does not declare the builtin types and 145therefore it has to be used in combination with ``-finclude-default-header`` 146if full functionality is required. 147 148**Example of Use**: 149 150 .. code-block:: console 151 152 $ clang -Xclang -fdeclare-opencl-builtins test.cl 153 154.. _opencl_fake_address_space_map: 155 156.. option:: -ffake-address-space-map 157 158Overrides the target address space map with a fake map. 159This allows adding explicit address space IDs to the bitcode for non-segmented 160memory architectures that do not have separate IDs for each of the OpenCL 161logical address spaces by default. Passing ``-ffake-address-space-map`` will 162add/override address spaces of the target compiled for with the following values: 163``1-global``, ``2-constant``, ``3-local``, ``4-generic``. The private address 164space is represented by the absence of an address space attribute in the IR (see 165also :ref:`the section on the address space attribute <opencl_addrsp>`). 166 167 .. code-block:: console 168 169 $ clang -cc1 -ffake-address-space-map test.cl 170 171OpenCL builtins 172--------------- 173 174**Clang builtins** 175 176There are some standard OpenCL functions that are implemented as Clang builtins: 177 178- All pipe functions from `section 6.13.16.2/6.13.16.3 179 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#160>`_ of 180 the OpenCL v2.0 kernel language specification. 181 182- Address space qualifier conversion functions ``to_global``/``to_local``/``to_private`` 183 from `section 6.13.9 184 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#101>`_. 185 186- All the ``enqueue_kernel`` functions from `section 6.13.17.1 187 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#164>`_ and 188 enqueue query functions from `section 6.13.17.5 189 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#171>`_. 190 191**Fast builtin function declarations** 192 193The implementation of the fast builtin function declarations (available via the 194:ref:`-fdeclare-opencl-builtins option <opencl_fdeclare_opencl_builtins>`) consists 195of the following main components: 196 197- A TableGen definitions file ``OpenCLBuiltins.td``. This contains a compact 198 representation of the supported builtin functions. When adding new builtin 199 function declarations, this is normally the only file that needs modifying. 200 201- A Clang TableGen emitter defined in ``ClangOpenCLBuiltinEmitter.cpp``. During 202 Clang build time, the emitter reads the TableGen definition file and 203 generates ``OpenCLBuiltins.inc``. This generated file contains various tables 204 and functions that capture the builtin function data from the TableGen 205 definitions in a compact manner. 206 207- OpenCL specific code in ``SemaLookup.cpp``. When ``Sema::LookupBuiltin`` 208 encounters a potential builtin function, it will check if the name corresponds 209 to a valid OpenCL builtin function. If so, all overloads of the function are 210 inserted using ``InsertOCLBuiltinDeclarationsFromTable`` and overload 211 resolution takes place. 212 213.. _opencl_addrsp: 214 215Address spaces attribute 216------------------------ 217 218Clang has arbitrary address space support using the ``address_space(N)`` 219attribute, where ``N`` is an integer number in the range specified in the 220Clang source code. This addresses spaces can be used along with the OpenCL 221address spaces however when such addresses spaces converted to/from OpenCL 222address spaces the behavior is not governed by OpenCL specification. 223 224An OpenCL implementation provides a list of standard address spaces using 225keywords: ``private``, ``local``, ``global``, and ``generic``. In the AST and 226in the IR each of the address spaces will be represented by unique number 227provided in the Clang source code. The specific IDs for an address space do not 228have to match between the AST and the IR. Typically in the AST address space 229numbers represent logical segments while in the IR they represent physical 230segments. 231Therefore, machines with flat memory segments can map all AST address space 232numbers to the same physical segment ID or skip address space attribute 233completely while generating the IR. However, if the address space information 234is needed by the IR passes e.g. to improve alias analysis, it is recommended 235to keep it and only lower to reflect physical memory segments in the late 236machine passes. The mapping between logical and target address spaces is 237specified in the Clang's source code. 238 239.. _cxx_for_opencl_impl: 240 241C++ for OpenCL Implementation Status 242==================================== 243 244Clang implements language version 1.0 published in `the official 245release of C++ for OpenCL Documentation 246<https://github.com/KhronosGroup/OpenCL-Docs/releases/tag/cxxforopencl-v1.0-r1>`_. 247 248Limited support of experimental C++ libraries is described in the :ref:`experimental features <opencl_experimenal>`. 249 250Bugzilla bugs for this functionality are typically prefixed 251with '[C++4OpenCL]' - click `here 252<https://bugs.llvm.org/buglist.cgi?component=OpenCL&list_id=204139&product=clang&query_format=advanced&resolution=---&sh ort_desc=%5BC%2B%2B4OpenCL%5D&short_desc_type=allwordssubstr>`_ 253to view the full bug list. 254 255 256Missing features or with limited support 257---------------------------------------- 258 259- Use of ObjC blocks is disabled and therefore the ``enqueue_kernel`` builtin 260 function is not supported currently. It is expected that if support for this 261 feature is added in the future, it will utilize C++ lambdas instead of ObjC 262 blocks. 263 264- IR generation for global destructors is incomplete (See: 265 `PR48047 <https://llvm.org/PR48047>`_). 266 267- There is no distinct file extension for sources that are to be compiled 268 in C++ for OpenCL mode (See: `PR48097 <https://llvm.org/PR48097>`_) 269 270.. _opencl_300: 271 272OpenCL 3.0 Implementation Status 273================================ 274 275The following table provides an overview of features in OpenCL C 3.0 and their 276implementation status. 277 278+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 279| Category | Feature | Status | Reviews | 280+==============================+==============================================================+======================+===========================================================================+ 281| Command line interface | New value for ``-cl-std`` flag | :good:`done` | https://reviews.llvm.org/D88300 | 282+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 283| Predefined macros | New version macro | :good:`done` | https://reviews.llvm.org/D88300 | 284+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 285| Predefined macros | Feature macros | :part:`worked on` | https://reviews.llvm.org/D89869 | 286+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 287| Feature optionality | Generic address space | :none:`unclaimed` | | 288+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 289| Feature optionality | Builtin function overloads with generic address space | :part:`worked on` | https://reviews.llvm.org/D92004 | 290+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 291| Feature optionality | Program scope variables in global memory | :none:`unclaimed` | | 292+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 293| Feature optionality | 3D image writes including builtin functions | :none:`unclaimed` | | 294+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 295| Feature optionality | read_write images including builtin functions | :none:`unclaimed` | | 296+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 297| Feature optionality | C11 atomics memory scopes, ordering and builtin function | :part:`worked on` | https://reviews.llvm.org/D92004 (functions only) | 298+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 299| Feature optionality | Device-side kernel enqueue including builtin functions | :none:`unclaimed` | | 300+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 301| Feature optionality | Pipes including builtin functions | :part:`worked on` | https://reviews.llvm.org/D92004 (functions only) | 302+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 303| Feature optionality | Work group collective functions | :part:`worked on` | https://reviews.llvm.org/D92004 | 304+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 305| New functionality | RGBA vector components | :none:`unclaimed` | | 306+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 307| New functionality | Subgroup functions | :part:`worked on` | https://reviews.llvm.org/D92004 | 308+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 309| New functionality | Atomic mem scopes: subgroup, all devices including functions | :part:`worked on` | https://reviews.llvm.org/D92004 (functions only) | 310+------------------------------+--------------------------------------------------------------+----------------------+---------------------------------------------------------------------------+ 311 312.. _opencl_experimenal: 313 314Experimental features 315===================== 316 317Clang provides the following new WIP features for the developers to experiment 318and provide early feedback or contribute with further improvements. 319Feel free to contact us on `cfe-dev 320<https://lists.llvm.org/mailman/listinfo/cfe-dev>`_ or via `Bugzilla 321<https://bugs.llvm.org/>`__. 322 323C++ libraries for OpenCL 324------------------------ 325 326There is ongoing work to support C++ standard libraries from `LLVM's libcxx 327<https://libcxx.llvm.org/>`_ in OpenCL kernel code using C++ for OpenCL mode. 328 329It is currently possible to include `type_traits` from C++17 in the kernel 330sources when the following clang extensions are enabled 331``__cl_clang_function_pointers`` and ``__cl_clang_variadic_functions``, 332see :doc:`LanguageExtensions` for more details. The use of non-conformant 333features enabled by the extensions does not expose non-conformant behavior 334beyond the compilation i.e. does not get generated in IR or binary. 335The extension only appear in metaprogramming 336mechanism to identify or verify the properties of types. This allows to provide 337the full C++ functionality without a loss of portability. To avoid unsafe use 338of the extensions it is recommended that the extensions are disabled directly 339after the header include. 340 341**Example of Use**: 342 343The example of kernel code with `type_traits` is illustrated here. 344 345.. code-block:: c++ 346 347 #pragma OPENCL EXTENSION __cl_clang_function_pointers : enable 348 #pragma OPENCL EXTENSION __cl_clang_variadic_functions : enable 349 #include <type_traits> 350 #pragma OPENCL EXTENSION __cl_clang_function_pointers : disable 351 #pragma OPENCL EXTENSION __cl_clang_variadic_functions : disable 352 353 using sint_type = std::make_signed<unsigned int>::type; 354 355 __kernel void foo() { 356 static_assert(!std::is_same<sint_type, unsigned int>::value); 357 } 358 359The possible clang invocation to compile the example is as follows: 360 361 .. code-block:: console 362 363 $ clang -cl-std=clc++ -I<path to libcxx checkout or installation>/include test.cl 364 365Note that `type_traits` is a header only library and therefore no extra 366linking step against the standard libraries is required. 367