Skip to content

Commit dff3d00

Browse files
[SYCL] Add property validation to sycl object ctors (#15253)
Similar checks for extensions will be covered in a separate commit. --------- Signed-off-by: Tikhomirova, Kseniya <[email protected]>
1 parent 65898a3 commit dff3d00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+994
-118
lines changed

sycl/include/sycl/detail/property_helper.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class PropertyWithDataBase {
9090
PropertyWithDataBase(int ID) : MID(ID) {}
9191
bool isSame(int ID) const { return ID == MID; }
9292
virtual ~PropertyWithDataBase() = default;
93+
int getKind() const { return MID; }
9394

9495
private:
9596
int MID = -1;
@@ -101,7 +102,7 @@ class PropertyWithDataBase {
101102
template <int ID> class PropertyWithData : public PropertyWithDataBase {
102103
public:
103104
PropertyWithData() : PropertyWithDataBase(ID) {}
104-
static int getKind() { return ID; }
105+
static constexpr int getKind() { return ID; }
105106
};
106107

107108
} // namespace detail

sycl/include/sycl/detail/property_list_base.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ class PropertyListBase {
126126
}
127127
}
128128

129+
void checkPropsAndThrow(std::function<bool(int)> FunctionForDataless,
130+
std::function<bool(int)> FunctionForData) const {
131+
static const auto ErrorCode = sycl::make_error_code(errc::invalid);
132+
static const auto ErrorMessage = "The property list contains property "
133+
"unsupported for the current object";
134+
135+
for (int PropertyKind = 0;
136+
PropertyKind < static_cast<int>(MDataLessProps.size());
137+
PropertyKind++) {
138+
if (MDataLessProps[PropertyKind] && !FunctionForDataless(PropertyKind))
139+
throw sycl::exception(ErrorCode, ErrorMessage);
140+
}
141+
142+
for (const auto &PropertyItem : MPropsWithData) {
143+
if (!FunctionForData(PropertyItem->getKind()))
144+
throw sycl::exception(ErrorCode, ErrorMessage);
145+
}
146+
}
147+
129148
// Stores enabled/disabled for simple properties
130149
std::bitset<DataLessPropKind::DataLessPropKindSize> MDataLessProps;
131150
// Stores shared_ptrs to complex properties

sycl/include/sycl/properties/accessor_properties.hpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@
2020

2121
namespace sycl {
2222
inline namespace _V1 {
23-
namespace property {
24-
25-
class no_init : public detail::DataLessProperty<detail::NoInit> {};
26-
27-
class __SYCL2020_DEPRECATED("spelling is now: no_init") noinit
28-
: public detail::DataLessProperty<detail::NoInit> {};
29-
30-
} // namespace property
31-
32-
inline constexpr property::no_init no_init;
3323

34-
__SYCL2020_DEPRECATED("spelling is now: no_init")
35-
inline constexpr property::noinit noinit;
24+
#define __SYCL_DATA_LESS_PROP_DEPRECATED_ALIAS(NS_QUALIFIER, PROP_NAME, \
25+
ENUM_VAL, WARNING) \
26+
namespace NS_QUALIFIER { \
27+
class WARNING PROP_NAME \
28+
: public sycl::detail::DataLessProperty<sycl::detail::ENUM_VAL> {}; \
29+
} \
30+
WARNING inline constexpr NS_QUALIFIER::PROP_NAME PROP_NAME;
31+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
32+
__SYCL_DATA_LESS_PROP_DEPRECATED_ALIAS(NS_QUALIFIER, PROP_NAME, ENUM_VAL, )
33+
#include <sycl/properties/runtime_accessor_properties.def>
3634

3735
namespace ext::intel {
3836
namespace property {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// --*- c++ -*---
2+
#ifndef __SYCL_DATA_LESS_PROP
3+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL)
4+
#endif
5+
#ifndef __SYCL_MANUALLY_DEFINED_PROP
6+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
7+
#endif
8+
9+
__SYCL_DATA_LESS_PROP(property::buffer, use_host_ptr, BufferUseHostPtr)
10+
__SYCL_DATA_LESS_PROP(ext::oneapi::property::buffer, use_pinned_host_memory, BufferUsePinnedHostMemory)
11+
12+
// Contains data field, defined explicitly.
13+
__SYCL_MANUALLY_DEFINED_PROP(property::buffer, use_mutex)
14+
__SYCL_MANUALLY_DEFINED_PROP(property::buffer, context_bound)
15+
__SYCL_MANUALLY_DEFINED_PROP(property::buffer, mem_channel)
16+
__SYCL_MANUALLY_DEFINED_PROP(property::buffer::detail, buffer_location)
17+
18+
#undef __SYCL_DATA_LESS_PROP
19+
#undef __SYCL_MANUALLY_DEFINED_PROP

sycl/include/sycl/properties/buffer_properties.hpp

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
namespace sycl {
2121
inline namespace _V1 {
22+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
23+
namespace NS_QUALIFIER { \
24+
class PROP_NAME \
25+
: public sycl::detail::DataLessProperty<sycl::detail::ENUM_VAL> {}; \
26+
}
27+
#include <sycl/properties/buffer_properties.def>
2228

2329
namespace property::buffer {
24-
class use_host_ptr : public detail::DataLessProperty<detail::BufferUseHostPtr> {
25-
};
26-
2730
class use_mutex : public detail::PropertyWithData<detail::BufferUseMutex> {
2831
public:
2932
use_mutex(std::mutex &MutexRef) : MMutex(MutexRef) {}
@@ -69,41 +72,19 @@ class buffer_location
6972
} // namespace detail
7073
} // namespace property::buffer
7174

72-
namespace ext::oneapi::property::buffer {
73-
74-
class use_pinned_host_memory : public sycl::detail::DataLessProperty<
75-
sycl::detail::BufferUsePinnedHostMemory> {};
76-
} // namespace ext::oneapi::property::buffer
77-
7875
// Forward declaration
7976
template <typename T, int Dimensions, typename AllocatorT, typename Enable>
8077
class buffer;
8178

82-
// Buffer property trait specializations
83-
template <typename T, int Dimensions, typename AllocatorT>
84-
struct is_property_of<property::buffer::use_host_ptr,
85-
buffer<T, Dimensions, AllocatorT, void>>
86-
: std::true_type {};
87-
template <typename T, int Dimensions, typename AllocatorT>
88-
struct is_property_of<property::buffer::use_mutex,
89-
buffer<T, Dimensions, AllocatorT, void>>
90-
: std::true_type {};
91-
template <typename T, int Dimensions, typename AllocatorT>
92-
struct is_property_of<property::buffer::detail::buffer_location,
93-
buffer<T, Dimensions, AllocatorT, void>>
94-
: std::true_type {};
95-
template <typename T, int Dimensions, typename AllocatorT>
96-
struct is_property_of<property::buffer::context_bound,
97-
buffer<T, Dimensions, AllocatorT, void>>
98-
: std::true_type {};
99-
template <typename T, int Dimensions, typename AllocatorT>
100-
struct is_property_of<property::buffer::mem_channel,
101-
buffer<T, Dimensions, AllocatorT, void>>
102-
: std::true_type {};
103-
template <typename T, int Dimensions, typename AllocatorT>
104-
struct is_property_of<ext::oneapi::property::buffer::use_pinned_host_memory,
105-
buffer<T, Dimensions, AllocatorT, void>>
106-
: std::true_type {};
79+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME) \
80+
template <typename T, int Dimensions, typename AllocatorT> \
81+
struct is_property_of<NS_QUALIFIER::PROP_NAME, \
82+
buffer<T, Dimensions, AllocatorT, void>> \
83+
: std::true_type {};
84+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
85+
__SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
86+
87+
#include <sycl/properties/buffer_properties.def>
10788

10889
} // namespace _V1
10990
} // namespace sycl
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// --*- c++ -*---
2+
#ifndef __SYCL_DATA_LESS_PROP
3+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL)
4+
#endif
5+
#ifndef __SYCL_MANUALLY_DEFINED_PROP
6+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
7+
#endif
8+
9+
__SYCL_DATA_LESS_PROP(property::image, use_host_ptr, ImageUseHostPtr)
10+
11+
// Contains data field, defined explicitly.
12+
__SYCL_MANUALLY_DEFINED_PROP(property::image, use_mutex)
13+
__SYCL_MANUALLY_DEFINED_PROP(property::image, context_bound)
14+
15+
#undef __SYCL_DATA_LESS_PROP
16+
#undef __SYCL_MANUALLY_DEFINED_PROP

sycl/include/sycl/properties/image_properties.hpp

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818

1919
namespace sycl {
2020
inline namespace _V1 {
21-
namespace property::image {
22-
class use_host_ptr : public detail::DataLessProperty<detail::ImageUseHostPtr> {
23-
};
21+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
22+
namespace NS_QUALIFIER { \
23+
class PROP_NAME \
24+
: public sycl::detail::DataLessProperty<sycl::detail::ENUM_VAL> {}; \
25+
}
26+
#include <sycl/properties/image_properties.def>
2427

28+
namespace property::image {
2529
class use_mutex : public detail::PropertyWithData<detail::ImageUseMutex> {
2630
public:
2731
use_mutex(std::mutex &MutexRef) : MMutex(MutexRef) {}
@@ -50,41 +54,32 @@ template <int Dimensions, typename AllocatorT> class sampled_image;
5054
template <int Dimensions, typename AllocatorT> class unsampled_image;
5155

5256
// SYCL 1.2.1 image property trait specializations
53-
template <int Dimensions, typename AllocatorT>
54-
struct is_property_of<property::image::use_host_ptr,
55-
image<Dimensions, AllocatorT>> : std::true_type {};
56-
template <int Dimensions, typename AllocatorT>
57-
struct is_property_of<property::image::use_mutex, image<Dimensions, AllocatorT>>
58-
: std::true_type {};
59-
template <int Dimensions, typename AllocatorT>
60-
struct is_property_of<property::image::context_bound,
61-
image<Dimensions, AllocatorT>> : std::true_type {};
57+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME) \
58+
template <int Dimensions, typename AllocatorT> \
59+
struct is_property_of<NS_QUALIFIER::PROP_NAME, \
60+
image<Dimensions, AllocatorT>> : std::true_type {};
61+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
62+
__SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
63+
#include <sycl/properties/image_properties.def>
6264

6365
// SYCL 2020 image property trait specializations
64-
template <int Dimensions, typename AllocatorT>
65-
struct is_property_of<property::image::use_host_ptr,
66-
sampled_image<Dimensions, AllocatorT>> : std::true_type {
67-
};
68-
template <int Dimensions, typename AllocatorT>
69-
struct is_property_of<property::image::use_mutex,
70-
sampled_image<Dimensions, AllocatorT>> : std::true_type {
71-
};
72-
template <int Dimensions, typename AllocatorT>
73-
struct is_property_of<property::image::context_bound,
74-
sampled_image<Dimensions, AllocatorT>> : std::true_type {
75-
};
76-
template <int Dimensions, typename AllocatorT>
77-
struct is_property_of<property::image::use_host_ptr,
78-
unsampled_image<Dimensions, AllocatorT>>
79-
: std::true_type {};
80-
template <int Dimensions, typename AllocatorT>
81-
struct is_property_of<property::image::use_mutex,
82-
unsampled_image<Dimensions, AllocatorT>>
83-
: std::true_type {};
84-
template <int Dimensions, typename AllocatorT>
85-
struct is_property_of<property::image::context_bound,
86-
unsampled_image<Dimensions, AllocatorT>>
87-
: std::true_type {};
66+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME) \
67+
template <int Dimensions, typename AllocatorT> \
68+
struct is_property_of<NS_QUALIFIER::PROP_NAME, \
69+
sampled_image<Dimensions, AllocatorT>> \
70+
: std::true_type {};
71+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
72+
__SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
73+
#include <sycl/properties/image_properties.def>
74+
75+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME) \
76+
template <int Dimensions, typename AllocatorT> \
77+
struct is_property_of<NS_QUALIFIER::PROP_NAME, \
78+
unsampled_image<Dimensions, AllocatorT>> \
79+
: std::true_type {};
80+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
81+
__SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
82+
#include <sycl/properties/image_properties.def>
8883

8984
} // namespace _V1
9085
} // namespace sycl
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// --*- c++ -*---
2+
#ifndef __SYCL_DATA_LESS_PROP
3+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL)
4+
#endif
5+
#ifndef __SYCL_MANUALLY_DEFINED_PROP
6+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
7+
#endif
8+
9+
__SYCL_DATA_LESS_PROP(property::reduction, initialize_to_identity, InitializeToIdentity)
10+
11+
#undef __SYCL_DATA_LESS_PROP
12+
#undef __SYCL_MANUALLY_DEFINED_PROP

sycl/include/sycl/properties/reduction_properties.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
namespace sycl {
1414
inline namespace _V1 {
15-
namespace property::reduction {
16-
class initialize_to_identity
17-
: public detail::DataLessProperty<detail::InitializeToIdentity> {};
18-
} // namespace property::reduction
15+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL) \
16+
namespace NS_QUALIFIER { \
17+
class PROP_NAME \
18+
: public sycl::detail::DataLessProperty<sycl::detail::ENUM_VAL> {}; \
19+
}
20+
#include <sycl/properties/reduction_properties.def>
1921

2022
// Reduction property trait specializations
2123
} // namespace _V1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// --*- c++ -*---
2+
#ifndef __SYCL_DATA_LESS_PROP
3+
#define __SYCL_DATA_LESS_PROP(NS_QUALIFIER, PROP_NAME, ENUM_VAL)
4+
#endif
5+
#ifndef __SYCL_MANUALLY_DEFINED_PROP
6+
#define __SYCL_MANUALLY_DEFINED_PROP(NS_QUALIFIER, PROP_NAME)
7+
#endif
8+
#ifndef __SYCL_DATA_LESS_PROP_DEPRECATED_ALIAS
9+
#define __SYCL_DATA_LESS_PROP_DEPRECATED_ALIAS(NS_QUALIFIER, PROP_NAME, ENUM_VAL, WARNING)
10+
#endif
11+
12+
__SYCL_DATA_LESS_PROP(property, no_init, NoInit)
13+
__SYCL_DATA_LESS_PROP_DEPRECATED_ALIAS(property, noinit, NoInit, __SYCL2020_DEPRECATED("spelling is now: no_init"))
14+
15+
#undef __SYCL_DATA_LESS_PROP
16+
#undef __SYCL_MANUALLY_DEFINED_PROP
17+
#undef __SYCL_DATA_LESS_PROP_DEPRECATED_ALIAS

sycl/include/sycl/property_list.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ inline namespace _V1 {
2323
namespace ext::oneapi {
2424
template <typename... PropsT> class accessor_property_list;
2525
} // namespace ext::oneapi
26+
namespace detail {
27+
class PropertyValidator;
28+
} // namespace detail
2629

2730
/// Objects of the property_list class are containers for the SYCL properties
2831
///
@@ -72,7 +75,19 @@ class property_list : protected detail::PropertyListBase {
7275

7376
template <typename... PropsT>
7477
friend class ext::oneapi::accessor_property_list;
78+
friend class detail::PropertyValidator;
7579
};
7680

81+
namespace detail {
82+
class PropertyValidator {
83+
public:
84+
static void checkPropsAndThrow(const property_list &PropList,
85+
std::function<bool(int)> FunctionForDataless,
86+
std::function<bool(int)> FunctionForData) {
87+
PropList.checkPropsAndThrow(FunctionForDataless, FunctionForData);
88+
}
89+
};
90+
} // namespace detail
91+
7792
} // namespace _V1
7893
} // namespace sycl

sycl/include/sycl/reduction.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ class ReducerElement {
503503
private:
504504
value_type MValue;
505505
};
506+
507+
__SYCL_EXPORT void verifyReductionProps(const property_list &Props);
506508
} // namespace detail
507509

508510
// We explicitly claim std::optional as device-copyable in sycl/types.hpp.
@@ -2842,6 +2844,7 @@ template <typename T, typename AllocatorT, typename BinaryOperation>
28422844
auto reduction(buffer<T, 1, AllocatorT> Var, handler &CGH,
28432845
BinaryOperation Combiner, const property_list &PropList = {}) {
28442846
std::ignore = CGH;
2847+
detail::verifyReductionProps(PropList);
28452848
bool InitializeToIdentity =
28462849
PropList.has_property<property::reduction::initialize_to_identity>();
28472850
return detail::make_reduction<BinaryOperation, 0, 1, false>(
@@ -2856,6 +2859,7 @@ auto reduction(buffer<T, 1, AllocatorT> Var, handler &CGH,
28562859
template <typename T, typename BinaryOperation>
28572860
auto reduction(T *Var, BinaryOperation Combiner,
28582861
const property_list &PropList = {}) {
2862+
detail::verifyReductionProps(PropList);
28592863
bool InitializeToIdentity =
28602864
PropList.has_property<property::reduction::initialize_to_identity>();
28612865
return detail::make_reduction<BinaryOperation, 0, 1, false>(
@@ -2869,6 +2873,7 @@ template <typename T, typename AllocatorT, typename BinaryOperation>
28692873
auto reduction(buffer<T, 1, AllocatorT> Var, handler &CGH, const T &Identity,
28702874
BinaryOperation Combiner, const property_list &PropList = {}) {
28712875
std::ignore = CGH;
2876+
detail::verifyReductionProps(PropList);
28722877
bool InitializeToIdentity =
28732878
PropList.has_property<property::reduction::initialize_to_identity>();
28742879
return detail::make_reduction<BinaryOperation, 0, 1, true>(
@@ -2881,6 +2886,7 @@ auto reduction(buffer<T, 1, AllocatorT> Var, handler &CGH, const T &Identity,
28812886
template <typename T, typename BinaryOperation>
28822887
auto reduction(T *Var, const T &Identity, BinaryOperation Combiner,
28832888
const property_list &PropList = {}) {
2889+
detail::verifyReductionProps(PropList);
28842890
bool InitializeToIdentity =
28852891
PropList.has_property<property::reduction::initialize_to_identity>();
28862892
return detail::make_reduction<BinaryOperation, 0, 1, true>(
@@ -2896,6 +2902,7 @@ template <typename T, size_t Extent, typename BinaryOperation,
28962902
typename = std::enable_if_t<Extent != dynamic_extent>>
28972903
auto reduction(span<T, Extent> Span, BinaryOperation Combiner,
28982904
const property_list &PropList = {}) {
2905+
detail::verifyReductionProps(PropList);
28992906
bool InitializeToIdentity =
29002907
PropList.has_property<property::reduction::initialize_to_identity>();
29012908
return detail::make_reduction<BinaryOperation, 1, Extent, false>(
@@ -2909,6 +2916,7 @@ template <typename T, size_t Extent, typename BinaryOperation,
29092916
typename = std::enable_if_t<Extent != dynamic_extent>>
29102917
auto reduction(span<T, Extent> Span, const T &Identity,
29112918
BinaryOperation Combiner, const property_list &PropList = {}) {
2919+
detail::verifyReductionProps(PropList);
29122920
bool InitializeToIdentity =
29132921
PropList.has_property<property::reduction::initialize_to_identity>();
29142922
return detail::make_reduction<BinaryOperation, 1, Extent, true>(

0 commit comments

Comments
 (0)