Skip to content

Commit 2020d8e

Browse files
[SYCL] Enforce constraints from sycl_ext_oneapi_reduction_properties
1 parent 3ac3e1b commit 2020d8e

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

sycl/include/sycl/ext/oneapi/experimental/reduction_properties.hpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ struct initialize_to_identity_key
3232
};
3333
inline constexpr initialize_to_identity_key::value_t initialize_to_identity;
3434

35+
namespace detail {
36+
struct reduction_property_check_anchor {};
37+
} // namespace detail
38+
39+
template <>
40+
struct is_property_key_of<deterministic_key,
41+
detail::reduction_property_check_anchor>
42+
: std::true_type {};
43+
44+
template <>
45+
struct is_property_key_of<initialize_to_identity_key,
46+
detail::reduction_property_check_anchor>
47+
: std::true_type {};
48+
3549
} // namespace experimental
3650
} // namespace oneapi
3751
} // namespace ext
@@ -83,9 +97,17 @@ template <typename BinaryOperation>
8397
struct IsDeterministicOperator<DeterministicOperatorWrapper<BinaryOperation>>
8498
: std::true_type {};
8599

100+
template <typename PropertyList>
101+
inline constexpr bool is_valid_reduction_prop_list =
102+
ext::oneapi::experimental::detail::all_are_properties_of_v<
103+
ext::oneapi::experimental::detail::reduction_property_check_anchor,
104+
PropertyList>;
105+
86106
} // namespace detail
87107

88-
template <typename BufferT, typename BinaryOperation, typename PropertyList>
108+
template <typename BufferT, typename BinaryOperation, typename PropertyList,
109+
typename = std::enable_if_t<
110+
detail::is_valid_reduction_prop_list<PropertyList>>>
89111
auto reduction(BufferT vars, handler &cgh, BinaryOperation combiner,
90112
PropertyList properties) {
91113
detail::CheckReductionIdentity<typename BufferT::value_type, BinaryOperation>(
@@ -95,16 +117,20 @@ auto reduction(BufferT vars, handler &cgh, BinaryOperation combiner,
95117
return reduction(vars, cgh, WrappedOp, RuntimeProps);
96118
}
97119

98-
template <typename T, typename BinaryOperation, typename PropertyList>
120+
template <typename T, typename BinaryOperation, typename PropertyList,
121+
typename = std::enable_if_t<
122+
detail::is_valid_reduction_prop_list<PropertyList>>>
99123
auto reduction(T *var, BinaryOperation combiner, PropertyList properties) {
100124
detail::CheckReductionIdentity<T, BinaryOperation>(properties);
101125
auto WrappedOp = detail::WrapOp(combiner, properties);
102126
auto RuntimeProps = detail::GetReductionPropertyList(properties);
103127
return reduction(var, WrappedOp, RuntimeProps);
104128
}
105129

106-
template <typename T, size_t Extent, typename BinaryOperation,
107-
typename PropertyList>
130+
template <
131+
typename T, size_t Extent, typename BinaryOperation, typename PropertyList,
132+
typename =
133+
std::enable_if_t<detail::is_valid_reduction_prop_list<PropertyList>>>
108134
auto reduction(span<T, Extent> vars, BinaryOperation combiner,
109135
PropertyList properties) {
110136
detail::CheckReductionIdentity<T, BinaryOperation>(properties);
@@ -113,7 +139,9 @@ auto reduction(span<T, Extent> vars, BinaryOperation combiner,
113139
return reduction(vars, WrappedOp, RuntimeProps);
114140
}
115141

116-
template <typename BufferT, typename BinaryOperation, typename PropertyList>
142+
template <typename BufferT, typename BinaryOperation, typename PropertyList,
143+
typename = std::enable_if_t<
144+
detail::is_valid_reduction_prop_list<PropertyList>>>
117145
auto reduction(BufferT vars, handler &cgh,
118146
const typename BufferT::value_type &identity,
119147
BinaryOperation combiner, PropertyList properties) {
@@ -122,16 +150,20 @@ auto reduction(BufferT vars, handler &cgh,
122150
return reduction(vars, cgh, identity, WrappedOp, RuntimeProps);
123151
}
124152

125-
template <typename T, typename BinaryOperation, typename PropertyList>
153+
template <typename T, typename BinaryOperation, typename PropertyList,
154+
typename = std::enable_if_t<
155+
detail::is_valid_reduction_prop_list<PropertyList>>>
126156
auto reduction(T *var, const T &identity, BinaryOperation combiner,
127157
PropertyList properties) {
128158
auto WrappedOp = detail::WrapOp(combiner, properties);
129159
auto RuntimeProps = detail::GetReductionPropertyList(properties);
130160
return reduction(var, identity, WrappedOp, RuntimeProps);
131161
}
132162

133-
template <typename T, size_t Extent, typename BinaryOperation,
134-
typename PropertyList>
163+
template <
164+
typename T, size_t Extent, typename BinaryOperation, typename PropertyList,
165+
typename =
166+
std::enable_if_t<detail::is_valid_reduction_prop_list<PropertyList>>>
135167
auto reduction(span<T, Extent> vars, const T &identity,
136168
BinaryOperation combiner, PropertyList properties) {
137169
auto WrappedOp = detail::WrapOp(combiner, properties);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note %s
2+
3+
#include <sycl/sycl.hpp>
4+
5+
int main() {
6+
int *r = nullptr;
7+
// Must not use `sycl_ext_oneapi_reduction_properties`'s overloads:
8+
std::ignore =
9+
sycl::reduction(r, sycl::plus<int>{},
10+
sycl::property::reduction::initialize_to_identity{});
11+
12+
namespace sycl_exp = sycl::ext::oneapi::experimental;
13+
std::ignore =
14+
sycl::reduction(r, sycl::plus<int>{},
15+
sycl_exp::properties(sycl_exp::initialize_to_identity));
16+
17+
// Not a property list:
18+
// expected-error@+2 {{no matching function for call to 'reduction'}}
19+
std::ignore =
20+
sycl::reduction(r, sycl::plus<int>{}, sycl_exp::initialize_to_identity);
21+
22+
// Not a reduction property:
23+
// expected-error@+2 {{no matching function for call to 'reduction'}}
24+
std::ignore =
25+
sycl::reduction(r, sycl::plus<int>{},
26+
sycl_exp::properties(sycl_exp::initialize_to_identity,
27+
sycl_exp::full_group));
28+
}

0 commit comments

Comments
 (0)