Skip to content

[SYCL][Headers cleanup] Introduce <sycl/detail/is_device_copyable.hpp> #13211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2024
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
41 changes: 20 additions & 21 deletions sycl/include/sycl/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@

#pragma once

#include <sycl/access/access.hpp> // for placeholder
#include <sycl/backend_types.hpp> // for backend, backe...
#include <sycl/context.hpp> // for context
#include <sycl/detail/array.hpp> // for array
#include <sycl/detail/common.hpp> // for code_location
#include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEP...
#include <sycl/detail/export.hpp> // for __SYCL_EXPORT
#include <sycl/detail/helpers.hpp> // for buffer_impl
#include <sycl/detail/owner_less_base.hpp> // for OwnerLessBase
#include <sycl/access/access.hpp>
#include <sycl/backend_types.hpp>
#include <sycl/context.hpp>
#include <sycl/detail/array.hpp>
#include <sycl/detail/common.hpp>
#include <sycl/detail/defines_elementary.hpp>
#include <sycl/detail/export.hpp>
#include <sycl/detail/helpers.hpp>
#include <sycl/detail/is_device_copyable.hpp>
#include <sycl/detail/owner_less_base.hpp>
#include <sycl/detail/pi.h> // for pi_native_handle and PI_ERROR_INVAL
#include <sycl/detail/property_helper.hpp> // for PropWithDataKind
#include <sycl/detail/stl_type_traits.hpp> // for iterator_value...
#include <sycl/detail/stl_type_traits.hpp> // for iterator_to_const_type_t
#include <sycl/detail/sycl_mem_obj_allocator.hpp> // for SYCLMemObjAllo...
#include <sycl/detail/type_traits.hpp> // for remove_pointer_t
#include <sycl/event.hpp> // for event
#include <sycl/exception.hpp> // for invalid_object...
#include <sycl/ext/oneapi/accessor_property_list.hpp> // for accessor_prope...
#include <sycl/id.hpp> // for id
#include <sycl/property_list.hpp> // for property_list
#include <sycl/range.hpp> // for range, rangeTo...
#include <sycl/types.hpp> // for is_device_copyable
#include <sycl/detail/property_helper.hpp>
#include <sycl/detail/stl_type_traits.hpp>
#include <sycl/detail/sycl_mem_obj_allocator.hpp>
#include <sycl/detail/type_traits.hpp>
#include <sycl/event.hpp>
#include <sycl/exception.hpp>
#include <sycl/ext/oneapi/accessor_property_list.hpp>
#include <sycl/id.hpp>
#include <sycl/property_list.hpp>
#include <sycl/range.hpp>

#include <cstddef> // for size_t, nullptr_t
#include <functional> // for function
Expand Down
84 changes: 84 additions & 0 deletions sycl/include/sycl/detail/is_device_copyable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//==------------ is_device_copyable.hpp - ----------------------------------==//
//
// 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 <array>
#include <optional>
#include <type_traits>
#include <variant>

/// This macro must be defined to 1 when SYCL implementation allows user
/// applications to explicitly declare certain class types as device copyable
/// by adding specializations of is_device_copyable type trait class.
#define SYCL_DEVICE_COPYABLE 1

namespace sycl {
inline namespace _V1 {
/// is_device_copyable is a user specializable class template to indicate
/// that a type T is device copyable, which means that SYCL implementation
/// may copy objects of the type T between host and device or between two
/// devices.
/// Specializing is_device_copyable such a way that
/// is_device_copyable_v<T> == true on a T that does not satisfy all
/// the requirements of a device copyable type is undefined behavior.
template <typename T> struct is_device_copyable;

namespace detail {
template <typename T, typename = void>
struct is_device_copyable_impl : std::is_trivially_copyable<T> {};

template <typename T>
struct is_device_copyable_impl<
T, std::enable_if_t<!std::is_same_v<T, std::remove_cv_t<T>>>>
// Cannot express this "recursion" (to take user's partial non-cv
// specializations into account) without this helper struct.
: is_device_copyable<std::remove_cv_t<T>> {};
} // namespace detail

template <typename T>
struct is_device_copyable : detail::is_device_copyable_impl<T> {};

// std::array<T, 0> is implicitly device copyable type.
template <typename T>
struct is_device_copyable<std::array<T, 0>> : std::true_type {};

// std::array<T, N> is implicitly device copyable type if T is device copyable.
template <typename T, std::size_t N>
struct is_device_copyable<std::array<T, N>> : is_device_copyable<T> {};

// std::optional<T> is implicitly device copyable type if T is device copyable.
template <typename T>
struct is_device_copyable<std::optional<T>> : is_device_copyable<T> {};

// std::pair<T1, T2> is implicitly device copyable type if T1 and T2 are device
// copyable.
template <typename T1, typename T2>
struct is_device_copyable<std::pair<T1, T2>>
: std::bool_constant<is_device_copyable<T1>::value &&
is_device_copyable<T2>::value> {};

// std::tuple<Ts...> is implicitly device copyable type if each type T of Ts...
// is device copyable.
template <typename... Ts>
struct is_device_copyable<std::tuple<Ts...>>
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};

// std::variant<Ts...> is implicitly device copyable type if each type T of
// Ts... is device copyable.
template <typename... Ts>
struct is_device_copyable<std::variant<Ts...>>
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};

// array is device copyable if element type is device copyable.
template <typename T, std::size_t N>
struct is_device_copyable<T[N]> : is_device_copyable<T> {};

template <typename T>
inline constexpr bool is_device_copyable_v = is_device_copyable<T>::value;
} // namespace _V1
} // namespace sycl
21 changes: 13 additions & 8 deletions sycl/include/sycl/marray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

#pragma once

#include <sycl/aliases.hpp> // for half
#include <sycl/detail/common.hpp> // for ArrayCreator
#include <sycl/half_type.hpp> // for half
#include <sycl/aliases.hpp>
#include <sycl/detail/common.hpp>
#include <sycl/detail/is_device_copyable.hpp>
#include <sycl/half_type.hpp>

#include <array> // for array
#include <cstddef> // for size_t
#include <cstdint> // for int64_t, int8_t, uint64_t, int16_t
#include <type_traits> // for enable_if_t, remove_const, is_conv...
#include <utility> // for index_sequence, make_index_sequence
#include <array>
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <utility>

namespace sycl {
inline namespace _V1 {
Expand Down Expand Up @@ -363,6 +364,10 @@ template <typename Type, std::size_t NumElements> class marray {
}
};

// marray is device copyable if element type is device copyable.
template <typename T, std::size_t N>
struct is_device_copyable<sycl::marray<T, N>> : is_device_copyable<T> {};

#define __SYCL_MAKE_MARRAY_ALIAS(ALIAS, TYPE, N) \
using ALIAS##N = sycl::marray<TYPE, N>;

Expand Down
72 changes: 1 addition & 71 deletions sycl/include/sycl/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
#include <sycl/detail/generic_type_lists.hpp> // for vector_basic_list
#include <sycl/detail/generic_type_traits.hpp> // for is_sigeninteger, is_s...
#include <sycl/detail/is_device_copyable.hpp>
#include <sycl/detail/memcpy.hpp> // for memcpy
#include <sycl/detail/type_list.hpp> // for is_contained
#include <sycl/detail/type_traits.hpp> // for is_floating_point
Expand Down Expand Up @@ -2195,77 +2196,6 @@ __SYCL_DEFINE_BF16_VECSTORAGE(16)
#undef __SYCL_DEFINE_BF16_VECSTORAGE
} // namespace detail

/// This macro must be defined to 1 when SYCL implementation allows user
/// applications to explicitly declare certain class types as device copyable
/// by adding specializations of is_device_copyable type trait class.
#define SYCL_DEVICE_COPYABLE 1

/// is_device_copyable is a user specializable class template to indicate
/// that a type T is device copyable, which means that SYCL implementation
/// may copy objects of the type T between host and device or between two
/// devices.
/// Specializing is_device_copyable such a way that
/// is_device_copyable_v<T> == true on a T that does not satisfy all
/// the requirements of a device copyable type is undefined behavior.
template <typename T> struct is_device_copyable;

namespace detail {
template <typename T, typename = void>
struct is_device_copyable_impl : std::is_trivially_copyable<T> {};

template <typename T>
struct is_device_copyable_impl<
T, std::enable_if_t<!std::is_same_v<T, std::remove_cv_t<T>>>>
// Cannot express this "recursion" (to take user's partial non-cv
// specializations into account) without this helper struct.
: is_device_copyable<std::remove_cv_t<T>> {};
} // namespace detail

template <typename T>
struct is_device_copyable : detail::is_device_copyable_impl<T> {};

// std::array<T, 0> is implicitly device copyable type.
template <typename T>
struct is_device_copyable<std::array<T, 0>> : std::true_type {};

// std::array<T, N> is implicitly device copyable type if T is device copyable.
template <typename T, std::size_t N>
struct is_device_copyable<std::array<T, N>> : is_device_copyable<T> {};

// std::optional<T> is implicitly device copyable type if T is device copyable.
template <typename T>
struct is_device_copyable<std::optional<T>> : is_device_copyable<T> {};

// std::pair<T1, T2> is implicitly device copyable type if T1 and T2 are device
// copyable.
template <typename T1, typename T2>
struct is_device_copyable<std::pair<T1, T2>>
: std::bool_constant<is_device_copyable<T1>::value &&
is_device_copyable<T2>::value> {};

// std::tuple<Ts...> is implicitly device copyable type if each type T of Ts...
// is device copyable.
template <typename... Ts>
struct is_device_copyable<std::tuple<Ts...>>
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};

// std::variant<Ts...> is implicitly device copyable type if each type T of
// Ts... is device copyable.
template <typename... Ts>
struct is_device_copyable<std::variant<Ts...>>
: std::bool_constant<(... && is_device_copyable<Ts>::value)> {};

// marray is device copyable if element type is device copyable.
template <typename T, std::size_t N>
struct is_device_copyable<sycl::marray<T, N>> : is_device_copyable<T> {};

// array is device copyable if element type is device copyable.
template <typename T, std::size_t N>
struct is_device_copyable<T[N]> : is_device_copyable<T> {};

template <typename T>
inline constexpr bool is_device_copyable_v = is_device_copyable<T>::value;

namespace detail {
template <typename T, typename = void>
struct IsDeprecatedDeviceCopyable : std::false_type {};
Expand Down
13 changes: 7 additions & 6 deletions sycl/test/include_deps/sycl_accessor.hpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,24 @@
// CHECK-NEXT: detail/property_helper.hpp
// CHECK-NEXT: detail/property_list_base.hpp
// CHECK-NEXT: properties/property_traits.hpp
// CHECK-NEXT: detail/is_device_copyable.hpp
// CHECK-NEXT: detail/stl_type_traits.hpp
// CHECK-NEXT: detail/sycl_mem_obj_allocator.hpp
// CHECK-NEXT: detail/aligned_allocator.hpp
// CHECK-NEXT: event.hpp
// CHECK-NEXT: ext/oneapi/accessor_property_list.hpp
// CHECK-NEXT: types.hpp
// CHECK-NEXT: detail/generic_type_traits.hpp
// CHECK-NEXT: detail/memcpy.hpp
// CHECK-NEXT: detail/vector_convert.hpp
// CHECK-NEXT: marray.hpp
// CHECK-NEXT: swizzles.def
// CHECK-NEXT: detail/accessor_iterator.hpp
// CHECK-NEXT: detail/generic_type_traits.hpp
// CHECK-NEXT: detail/handler_proxy.hpp
// CHECK-NEXT: detail/image_accessor_util.hpp
// CHECK-NEXT: image.hpp
// CHECK-NEXT: detail/backend_traits.hpp
// CHECK-NEXT: sampler.hpp
// CHECK-NEXT: types.hpp
// CHECK-NEXT: detail/memcpy.hpp
// CHECK-NEXT: detail/vector_convert.hpp
// CHECK-NEXT: marray.hpp
// CHECK-NEXT: swizzles.def
// CHECK-NEXT: pointers.hpp
// CHECK-NEXT: properties/accessor_properties.hpp
// CHECK-NEXT: properties/buffer_properties.hpp
Expand Down
8 changes: 1 addition & 7 deletions sycl/test/include_deps/sycl_buffer.hpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,10 @@
// CHECK-NEXT: detail/property_helper.hpp
// CHECK-NEXT: detail/property_list_base.hpp
// CHECK-NEXT: properties/property_traits.hpp
// CHECK-NEXT: detail/is_device_copyable.hpp
// CHECK-NEXT: detail/stl_type_traits.hpp
// CHECK-NEXT: detail/sycl_mem_obj_allocator.hpp
// CHECK-NEXT: detail/aligned_allocator.hpp
// CHECK-NEXT: event.hpp
// CHECK-NEXT: ext/oneapi/accessor_property_list.hpp
// CHECK-NEXT: types.hpp
// CHECK-NEXT: detail/generic_type_traits.hpp
// CHECK-NEXT: multi_ptr.hpp
// CHECK-NEXT: detail/memcpy.hpp
// CHECK-NEXT: detail/vector_convert.hpp
// CHECK-NEXT: marray.hpp
// CHECK-NEXT: swizzles.def
// CHECK-EMPTY:
13 changes: 7 additions & 6 deletions sycl/test/include_deps/sycl_detail_core.hpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,24 @@
// CHECK-NEXT: detail/property_helper.hpp
// CHECK-NEXT: detail/property_list_base.hpp
// CHECK-NEXT: properties/property_traits.hpp
// CHECK-NEXT: detail/is_device_copyable.hpp
// CHECK-NEXT: detail/stl_type_traits.hpp
// CHECK-NEXT: detail/sycl_mem_obj_allocator.hpp
// CHECK-NEXT: detail/aligned_allocator.hpp
// CHECK-NEXT: event.hpp
// CHECK-NEXT: ext/oneapi/accessor_property_list.hpp
// CHECK-NEXT: types.hpp
// CHECK-NEXT: detail/generic_type_traits.hpp
// CHECK-NEXT: detail/memcpy.hpp
// CHECK-NEXT: detail/vector_convert.hpp
// CHECK-NEXT: marray.hpp
// CHECK-NEXT: swizzles.def
// CHECK-NEXT: detail/accessor_iterator.hpp
// CHECK-NEXT: detail/generic_type_traits.hpp
// CHECK-NEXT: detail/handler_proxy.hpp
// CHECK-NEXT: detail/image_accessor_util.hpp
// CHECK-NEXT: image.hpp
// CHECK-NEXT: detail/backend_traits.hpp
// CHECK-NEXT: sampler.hpp
// CHECK-NEXT: types.hpp
// CHECK-NEXT: detail/memcpy.hpp
// CHECK-NEXT: detail/vector_convert.hpp
// CHECK-NEXT: marray.hpp
// CHECK-NEXT: swizzles.def
// CHECK-NEXT: pointers.hpp
// CHECK-NEXT: properties/accessor_properties.hpp
// CHECK-NEXT: properties/buffer_properties.hpp
Expand Down