Skip to content

Commit 37a43f9

Browse files
[SYCL] Default initialize group_local_memory_for_overwrite (#8472)
In accordance with the extension introducing it, the group_local_memory_for_overwrite should default initialize its data. This commit adds this behavior, making it only initialize and synchronize when needed. Additionally it changes the local ID calculation in group_local_memory use the group, as suggested in the TODO. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent bab7c33 commit 37a43f9

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

sycl/include/sycl/ext/oneapi/group_local_memory.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ std::enable_if_t<
3232
#ifdef __SYCL_DEVICE_ONLY__
3333
__attribute__((opencl_local)) std::uint8_t *AllocatedMem =
3434
__sycl_allocateLocalMemory(sizeof(T), alignof(T));
35+
// If the type is non-trivial we need to default initialize it.
36+
if constexpr (!std::is_trivial_v<T>) {
37+
if (g.get_local_linear_id() == 0)
38+
new (AllocatedMem) T; // Default initialize.
39+
sycl::detail::workGroupBarrier();
40+
}
3541
return reinterpret_cast<__attribute__((opencl_local)) T *>(AllocatedMem);
3642
#else
3743
throw feature_not_supported(
@@ -46,19 +52,16 @@ std::enable_if_t<
4652
sycl::detail::is_group<Group>::value,
4753
multi_ptr<T, access::address_space::local_space, access::decorated::legacy>>
4854
__SYCL_ALWAYS_INLINE group_local_memory(Group g, Args &&...args) {
49-
(void)g;
5055
#ifdef __SYCL_DEVICE_ONLY__
5156
__attribute__((opencl_local)) std::uint8_t *AllocatedMem =
5257
__sycl_allocateLocalMemory(sizeof(T), alignof(T));
53-
54-
// TODO switch to using group::get_local_linear_id here once it's implemented
55-
id<3> Id = __spirv::initLocalInvocationId<3, id<3>>();
56-
if (Id == id<3>(0, 0, 0))
58+
if (g.get_local_linear_id() == 0)
5759
new (AllocatedMem) T(std::forward<Args>(args)...);
5860
sycl::detail::workGroupBarrier();
5961
return reinterpret_cast<__attribute__((opencl_local)) T *>(AllocatedMem);
6062
#else
6163
// Silence unused variable warning
64+
(void)g;
6265
[&args...] {}();
6366
throw feature_not_supported(
6467
"sycl_ext_oneapi_local_memory extension is not supported on host device",

0 commit comments

Comments
 (0)