| Issue |
243327
|
| Summary |
[Clang][SYCL] Add a `__builtin_sycl_dynamic_local_memory` builtin function
|
| Labels |
SYCL
|
| Assignees |
elizabethandrews
|
| Reporter |
tahonermann
|
The `sycl::local_accessor` class ([SYCL 2020 section 4.7.6.11, "Local accessor"](https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#sec:accessor.local)), provides access to dynamically allocated local memory (memory that is shared between all work-items of a work-group). Local accessors are constructed in a host context and copied to a device as a capture or data member of a SYCL kernel object, local memory is allocated as part of the kernel invocation. This requires the SYCL kernel interface functions to be able to introspect the members of a SYCL kernel object to identify the `sycl::local_accessor` members, query the amount of memory each requires, arrange for local memory to be allocated, copy the kernel object to the device, and then update each such member to point to local memory allocated for it.
The LLVM libsycl implementation builds on top of LLVM liboffload. liboffload provides primitives for launching kernels. Dynamic local memory is allocated by setting the `DynSharedMemory` member of `ol_kernel_launch_size_args_t` to the required number of bytes to allocate when calling `olLaunchKernel()`. Due to limitations in CUDA/HIP, only one dynamic allocation of local (shared) memory is provided. The amount of memory requested must suffice for all local accessors (including considerations for their alignment requirements).
Kernels running with the CUDA and HIP run-time libraries access dynamically allocated local memory by declaring an extern `__shared__` variable as an array of unknown size. When the kernel is invoked, the address of the variable corresponds to the dynamically allocated local memory. For example:
```CUDA
__global__ void k() {
extern __shared__ unsigned char dynamic_shared_memory[];
}
```
Kernels running with the OpenCL or Level Zero run-time libraries receive a pointer to dynamically allocated local memory via a kernel parameter. These run-time libraries support multiple dynamic local memory allocations, but for consistency with CUDA and HIP, only one such allocation will be supported by the Clang SYCL core language support features.
Generation of an offload kernel entry point function (e.g., the device stub function that would be generated for the CUDA code above) is performed by Clang SYCL core language features. That means that the compiler is responsible for naming the extern `__shared__` variable and sharing its address with the SYCL run-time library when targeting CUDA or HIP. Likewise, for OpenCL and Level Zero, the compiler is responsible for sharing the kernel parameter that points to the dynamically allocated local memory. A means for such sharing is therefore necessary.
Since dynamically allocated local memory is not always used, it is advantageous to let the SYCL run-time library request it when it is needed. This issue tracks adding a new builtin function, `__builtin_sycl_dynamic_local_memory` (or similar) to enable the SYCL run-time library to request it. The intent is that the builtin function may be called from the body of a SYCL kernel or any functions called by one; this gives the SYCL run-time library maximum flexibility for how and when it requests it. For CUDA and HIP, a builtin function that returns the address of an extern `__shared__` variable with a well known name should be relatively straight forward. However, doing similarly for OpenCL or Level Zero is more complicated; a builtin function would not know where to find the kernel parameter to return.
https://github.com/llvm/llvm-project/pull/231160 is an in-progress PR that implements an LLVM code generation pass when targeting a SPIR-V triple with a default of "opencl" OS component (Level Zero acts as an OpenCL run-time). That pass modifies kernel entry point functions to add an implicit additional parameter (to receive the address of a dynamic local memory allocation) and injects code to stash the parameter value in a `__spirv_dynamic_local_memory` variable statically allocated in the local address space. This mimics the behavior for CUDA and HIP; the `__builtin_sycl_dynamic_local_memory` builtin function can just return that variable for these targets.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs