120 lines · plain
1=============================================2SYCL Compiler and Runtime architecture design3=============================================4 5.. contents::6 :local:7 8Introduction9============10 11This document describes the architecture of the SYCL compiler and runtime12library. More details are provided in13`external document <https://github.com/intel/llvm/blob/sycl/sycl/doc/design/CompilerAndRuntimeDesign.md>`_\ ,14which are going to be added to clang documentation in the future.15 16Address space handling17======================18 19The SYCL specification represents pointers to disjoint memory regions using C++20wrapper classes on an accelerator to enable compilation with a standard C++21toolchain and a SYCL compiler toolchain. Section 3.8.2 of SYCL 202022specification defines23`memory model <https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#_sycl_device_memory_model>`_\ ,24section 4.7.7 - `address space classes <https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#_address_space_classes>`_25and section 5.9 covers `address space deduction <https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#_address_space_deduction>`_.26The SYCL specification allows two modes of address space deduction: "generic as27default address space" (see section 5.9.3) and "inferred address space" (see28section 5.9.4). Current implementation supports only "generic as default address29space" mode.30 31SYCL borrows its memory model from OpenCL however SYCL doesn't perform32the address space qualifier inference as detailed in33`OpenCL C v3.0 6.7.8 <https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html#addr-spaces-inference>`_.34 35The default address space is "generic-memory", which is a virtual address space36that overlaps the global, local, and private address spaces. SYCL mode enables37following conversions:38 39- explicit conversions to/from the default address space from/to the address40 space-attributed type41- implicit conversions from the address space-attributed type to the default42 address space43- explicit conversions to/from the global address space from/to the44 ``__attribute__((opencl_global_device))`` or45 ``__attribute__((opencl_global_host))`` address space-attributed type46- implicit conversions from the ``__attribute__((opencl_global_device))`` or47 ``__attribute__((opencl_global_host))`` address space-attributed type to the48 global address space49 50All named address spaces are disjoint and sub-sets of default address space.51 52The SPIR target allocates SYCL namespace scope variables in the global address53space.54 55Pointers to default address space should get lowered into a pointer to a generic56address space (or flat to reuse more general terminology). But depending on the57allocation context, the default address space of a non-pointer type is assigned58to a specific address space. This is described in59`common address space deduction rules <https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#subsec:commonAddressSpace>`_60section.61 62This is also in line with the behaviour of CUDA (`small example63<https://godbolt.org/z/veqTfo9PK>`_).64 65``multi_ptr`` class implementation example:66 67.. code-block:: C++68 69 // check that SYCL mode is ON and we can use non-standard decorations70 #if defined(__SYCL_DEVICE_ONLY__)71 // GPU/accelerator implementation72 template <typename T, address_space AS> class multi_ptr {73 // DecoratedType applies corresponding address space attribute to the type T74 // DecoratedType<T, global_space>::type == "__attribute__((opencl_global)) T"75 // See sycl/include/CL/sycl/access/access.hpp for more details76 using pointer_t = typename DecoratedType<T, AS>::type *;77 78 pointer_t m_Pointer;79 public:80 pointer_t get() { return m_Pointer; }81 T& operator* () { return *reinterpret_cast<T*>(m_Pointer); }82 }83 #else84 // CPU/host implementation85 template <typename T, address_space AS> class multi_ptr {86 T *m_Pointer; // regular undecorated pointer87 public:88 T *get() { return m_Pointer; }89 T& operator* () { return *m_Pointer; }90 }91 #endif92 93Depending on the compiler mode, ``multi_ptr`` will either decorate its internal94data with the address space attribute or not.95 96To utilize clang's existing functionality, we reuse the following OpenCL address97space attributes for pointers:98 99.. list-table::100 :header-rows: 1101 102 * - Address space attribute103 - SYCL address_space enumeration104 * - ``__attribute__((opencl_global))``105 - global_space, constant_space106 * - ``__attribute__((opencl_global_device))``107 - global_space108 * - ``__attribute__((opencl_global_host))``109 - global_space110 * - ``__attribute__((opencl_local))``111 - local_space112 * - ``__attribute__((opencl_private))``113 - private_space114 115 116.. code-block:: C++117 118 //TODO: add support for __attribute__((opencl_global_host)) and __attribute__((opencl_global_device)).119 120