Skip to content

Commit 61f1ae6

Browse files
author
Pavel Samolysov
authored
[SYCL] Check if the underlying buffer type is device copyable (#4914)
The implementation should diagnose an error if the underlying type "T" of a buffer is not device copyable. The spec says that this is a requirement for all buffers. It does not make any exception for buffers that are used only on the host: "The underlying data type of a buffer T must be device copyable as defined in Section 3.13.1." (https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#subsec:buffers)
1 parent f4d01cd commit 61f1ae6

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

sycl/include/CL/sycl/buffer.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <CL/sycl/exception.hpp>
1515
#include <CL/sycl/property_list.hpp>
1616
#include <CL/sycl/stl.hpp>
17+
#include <CL/sycl/types.hpp>
1718
#include <sycl/ext/oneapi/accessor_property_list.hpp>
1819

1920
__SYCL_INLINE_NAMESPACE(cl) {
@@ -44,6 +45,10 @@ template <typename T, int dimensions = 1,
4445
typename __Enabled = typename detail::enable_if_t<(dimensions > 0) &&
4546
(dimensions <= 3)>>
4647
class buffer {
48+
static_assert(
49+
is_device_copyable<T>::value,
50+
"The underlying data type of a buffer 'T' must be device copyable");
51+
4752
public:
4853
using value_type = T;
4954
using reference = value_type &;

sycl/include/CL/sycl/types.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,13 @@ struct is_device_copyable<sycl::marray<T, N>,
24202420
std::enable_if_t<is_device_copyable<T>::value>>
24212421
: std::true_type {};
24222422

2423+
// vec is device copyable on host, on device vec is trivially copyable
2424+
// and therefore is device copyable too.
2425+
#ifndef __SYCL_DEVICE_ONLY__
2426+
template <typename T, int N>
2427+
struct is_device_copyable<sycl::vec<T, N>> : std::true_type {};
2428+
#endif
2429+
24232430
namespace detail {
24242431
template <typename T, typename = void>
24252432
struct IsDeprecatedDeviceCopyable : std::false_type {};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clangxx -fsycl -fsyntax-only -Xclang -verify %s -Xclang -verify-ignore-unexpected=note,warning
2+
3+
#include <string>
4+
#include <sycl/sycl.hpp>
5+
6+
using namespace sycl;
7+
8+
int main() {
9+
static_assert(is_device_copyable_v<int>);
10+
std::vector<int> iv(5, 1);
11+
buffer b1(iv.data(), range<1>(5));
12+
13+
static_assert(!is_device_copyable_v<std::string>);
14+
std::vector<std::string> sv{"hello", "sycl", "world"};
15+
buffer b2(sv.data(), range<1>(3));
16+
//expected-error@CL/sycl/buffer.hpp:* {{"The underlying data type of a buffer 'T' must be device copyable"}}
17+
18+
static_assert(is_device_copyable<sycl::vec<int, 4>>::value);
19+
vec<int, 4> iVec;
20+
buffer b3(&iVec, range<1>(1));
21+
return 0;
22+
}

0 commit comments

Comments
 (0)