Skip to content

Commit cdf561a

Browse files
[SYCL] Make properties constructor constexpr (#5928)
According to the compile-time properties specification, the constructor for `properties` should be `constexpr`. This was missed in the initial implementation. This commit adds `constexpr` to the constructor and its constituents.
1 parent df0af38 commit cdf561a

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

sycl/include/sycl/ext/oneapi/properties/properties.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ struct ExtractProperties<std::tuple<PropertiesTs...>> {
9595
using ExtractedPropertiesT = std::tuple<>;
9696

9797
template <typename... PropertyValueTs>
98-
static ExtractedPropertiesT<PropertyValueTs...>
98+
static constexpr ExtractedPropertiesT<PropertyValueTs...>
9999
Extract(std::tuple<PropertyValueTs...>) {
100100
return {};
101101
}
@@ -112,7 +112,7 @@ struct ExtractProperties<std::tuple<PropertyT, PropertiesTs...>> {
112112
NextExtractedPropertiesT<PropertyValueTs...>>::type;
113113

114114
template <typename... PropertyValueTs>
115-
static ExtractedPropertiesT<PropertyValueTs...>
115+
static constexpr ExtractedPropertiesT<PropertyValueTs...>
116116
Extract(std::tuple<PropertyValueTs...> PropertyValues) {
117117
PropertyT ThisExtractedProperty = std::get<PropertyT>(PropertyValues);
118118
NextExtractedPropertiesT<PropertyValueTs...> NextExtractedProperties =
@@ -137,7 +137,7 @@ template <typename PropertiesT> class properties {
137137

138138
public:
139139
template <typename... PropertyValueTs>
140-
properties(PropertyValueTs... props)
140+
constexpr properties(PropertyValueTs... props)
141141
: Storage(detail::ExtractProperties<StorageT>::Extract(
142142
std::tuple<PropertyValueTs...>{props...})) {}
143143

sycl/test/extensions/properties/mock_compile_time_properties.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct boo_key {
3131
};
3232

3333
struct foo {
34-
foo(int v) : value(v) {}
34+
constexpr foo(int v) : value(v) {}
3535
int value;
3636
};
3737

@@ -41,9 +41,9 @@ inline bool operator==(const foo &lhs, const foo &rhs) {
4141
inline bool operator!=(const foo &lhs, const foo &rhs) { return !(lhs == rhs); }
4242

4343
struct foz {
44-
foz(float v1, bool v2) : value1(v1), value2(v2) {}
44+
constexpr foz(float v1, bool v2) : value1(v1), value2(v2) {}
4545
// Define copy constructor to make foz non-trivially copyable
46-
foz(const foz &f) {
46+
constexpr foz(const foz &f) {
4747
value1 = f.value1;
4848
value2 = f.value2;
4949
}
@@ -57,6 +57,7 @@ inline bool operator==(const foz &lhs, const foz &rhs) {
5757
inline bool operator!=(const foz &lhs, const foz &rhs) { return !(lhs == rhs); }
5858

5959
struct fir {
60+
// Intentionally not constexpr to test for properties that cannot be constexpr
6061
fir(float v1, bool v2) : value1(v1), value2(v2) {}
6162
// Define copy constructor to make foz non-trivially copyable
6263
fir(const foz &f) {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note,warning %s
2+
3+
#include <CL/sycl.hpp>
4+
5+
#include "mock_compile_time_properties.hpp"
6+
7+
int main() {
8+
// Empty properties
9+
constexpr decltype(sycl::ext::oneapi::experimental::properties{})
10+
EmptyProps1{};
11+
constexpr auto EmptyProps2 = sycl::ext::oneapi::experimental::properties{};
12+
13+
// Compile-time value properties
14+
constexpr decltype(sycl::ext::oneapi::experimental::properties{
15+
sycl::ext::oneapi::experimental::bar,
16+
sycl::ext::oneapi::experimental::baz<1>,
17+
sycl::ext::oneapi::experimental::boo<int, bool>}) CTProps1{
18+
sycl::ext::oneapi::experimental::bar,
19+
sycl::ext::oneapi::experimental::baz<1>,
20+
sycl::ext::oneapi::experimental::boo<int, bool>};
21+
constexpr auto CTProps2 = sycl::ext::oneapi::experimental::properties{
22+
sycl::ext::oneapi::experimental::bar,
23+
sycl::ext::oneapi::experimental::baz<1>,
24+
sycl::ext::oneapi::experimental::boo<int, bool>};
25+
26+
// Runtime value properties
27+
constexpr decltype(sycl::ext::oneapi::experimental::properties{
28+
sycl::ext::oneapi::experimental::foo(42),
29+
sycl::ext::oneapi::experimental::foz(3.14, false)}) RTProps1{
30+
sycl::ext::oneapi::experimental::foo(42),
31+
sycl::ext::oneapi::experimental::foz(3.14, false)};
32+
constexpr auto RTProps2 = sycl::ext::oneapi::experimental::properties{
33+
sycl::ext::oneapi::experimental::foo(42),
34+
sycl::ext::oneapi::experimental::foz(3.14, false)};
35+
36+
// Mixed compile-time and runtime value properties
37+
constexpr decltype(sycl::ext::oneapi::experimental::properties{
38+
sycl::ext::oneapi::experimental::bar,
39+
sycl::ext::oneapi::experimental::baz<1>,
40+
sycl::ext::oneapi::experimental::boo<int, bool>,
41+
sycl::ext::oneapi::experimental::foo(42),
42+
sycl::ext::oneapi::experimental::foz(3.14, false)}) MixProps1{
43+
sycl::ext::oneapi::experimental::bar,
44+
sycl::ext::oneapi::experimental::baz<1>,
45+
sycl::ext::oneapi::experimental::boo<int, bool>,
46+
sycl::ext::oneapi::experimental::foo(42),
47+
sycl::ext::oneapi::experimental::foz(3.14, false)};
48+
constexpr auto MixProps2 = sycl::ext::oneapi::experimental::properties{
49+
sycl::ext::oneapi::experimental::bar,
50+
sycl::ext::oneapi::experimental::baz<1>,
51+
sycl::ext::oneapi::experimental::boo<int, bool>,
52+
sycl::ext::oneapi::experimental::foo(42),
53+
sycl::ext::oneapi::experimental::foz(3.14, false)};
54+
55+
// Runtime value property without constexpr ctors
56+
// expected-error@+2 {{constexpr variable cannot have non-literal type}}
57+
constexpr decltype(sycl::ext::oneapi::experimental::properties{
58+
sycl::ext::oneapi::experimental::fir(3.14, false)}) NCRTProps1{
59+
sycl::ext::oneapi::experimental::fir(3.14, false)};
60+
// expected-error@+1 {{constexpr variable cannot have non-literal type}}
61+
constexpr auto NCRTProps2 = sycl::ext::oneapi::experimental::properties{
62+
sycl::ext::oneapi::experimental::fir(3.14, false)};
63+
int RTIntValue = 1;
64+
// expected-error@+2 {{constexpr variable 'NCRTProps3' must be initialized by a constant expression}}
65+
constexpr decltype(sycl::ext::oneapi::experimental::properties{
66+
sycl::ext::oneapi::experimental::foo(RTIntValue)}) NCRTProps3{
67+
sycl::ext::oneapi::experimental::foo(RTIntValue)};
68+
// expected-error@+1 {{constexpr variable 'NCRTProps4' must be initialized by a constant expressio}}
69+
constexpr auto NCRTProps4 = sycl::ext::oneapi::experimental::properties{
70+
sycl::ext::oneapi::experimental::foo(RTIntValue)};
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)