Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sycl/include/CL/sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <CL/sycl/exception.hpp>
#include <CL/sycl/feature_test.hpp>
#include <CL/sycl/group.hpp>
#include <CL/sycl/group_local_memory.hpp>
#include <CL/sycl/handler.hpp>
#include <CL/sycl/id.hpp>
#include <CL/sycl/image.hpp>
Expand Down
7 changes: 7 additions & 0 deletions sycl/include/CL/sycl/detail/sycl_fe_intrins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#pragma once

#include <cstddef>
#include <cstdint>

#ifdef __SYCL_DEVICE_ONLY__

// Get the value of the specialization constant with given name.
Expand Down Expand Up @@ -42,4 +45,8 @@ SYCL_EXTERNAL T __sycl_getComposite2020SpecConstantValue(const char *SymbolicID,
void *DefaultValue,
void *RTBuffer);

// Request a fixed-size allocation in local address space at kernel scope.
extern "C" SYCL_EXTERNAL __attribute__((opencl_local)) std::uint8_t *
__sycl_allocateLocalMemory(std::size_t Size, std::size_t Alignment);

#endif
67 changes: 67 additions & 0 deletions sycl/include/CL/sycl/group_local_memory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//==----- group_local_memory.hpp --- SYCL group local memory extension -----==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#pragma once

#include <CL/__spirv/spirv_vars.hpp>
#include <CL/sycl/detail/defines_elementary.hpp>
#include <CL/sycl/detail/sycl_fe_intrins.hpp>
#include <CL/sycl/detail/type_traits.hpp>
#include <CL/sycl/exception.hpp>
#include <CL/sycl/group.hpp>
#include <CL/sycl/multi_ptr.hpp>

#include <cstdint>
#include <type_traits>
#include <utility>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {

template <typename T, typename Group>
std::enable_if_t<std::is_trivially_destructible<T>::value &&
detail::is_group<Group>::value,
multi_ptr<T, access::address_space::local_space>>
__SYCL_ALWAYS_INLINE group_local_memory_for_overwrite(Group g) {
(void)g;
#ifdef __SYCL_DEVICE_ONLY__
__attribute__((opencl_local)) std::uint8_t *AllocatedMem =
__sycl_allocateLocalMemory(sizeof(T), alignof(T));
return reinterpret_cast<__attribute__((opencl_local)) T *>(AllocatedMem);
#else
throw feature_not_supported(
"SYCL_INTEL_local_memory extension is not supported on host device",
PI_INVALID_OPERATION);
#endif
}

template <typename T, typename Group, typename... Args>
std::enable_if_t<std::is_trivially_destructible<T>::value &&
detail::is_group<Group>::value,
multi_ptr<T, access::address_space::local_space>>
__SYCL_ALWAYS_INLINE group_local_memory(Group g, Args &&... args) {
(void)g;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: descriptive macro for this - UNUSED_ARG(g) ?

Copy link
Contributor Author

@sergey-semenov sergey-semenov Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the change, but if it is to be made, it would be more appropriate as a separate one for the whole runtime codebase, since direct casting to void is used extensively right now.

#ifdef __SYCL_DEVICE_ONLY__
__attribute__((opencl_local)) std::uint8_t *AllocatedMem =
__sycl_allocateLocalMemory(sizeof(T), alignof(T));

// TODO switch to using group::get_local_linear_id here once it's implemented
id<3> Id = __spirv::initLocalInvocationId<3, id<3>>();
if (Id == id<3>(0, 0, 0))
new (AllocatedMem) T(std::forward<Args>(args)...);
detail::workGroupBarrier();
return reinterpret_cast<__attribute__((opencl_local)) T *>(AllocatedMem);
#else
// Silence unused variable warning
[&args...] {}();
throw feature_not_supported(
"SYCL_INTEL_local_memory extension is not supported on host device",
PI_INVALID_OPERATION);
#endif
}
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)