Skip to content

Commit 9c45a93

Browse files
[SYCL][NFC] Move SizeListToStr to utilities (#7200)
This commit moves the utility from the kernel properties headers to the common property utiliites headers. This will allow for use in other properties that seek to use multiple integral values in their compile-time values. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 6fe7cc1 commit 9c45a93

File tree

4 files changed

+148
-43
lines changed

4 files changed

+148
-43
lines changed

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

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <sycl/aspects.hpp>
1212
#include <sycl/ext/oneapi/properties/property.hpp>
13+
#include <sycl/ext/oneapi/properties/property_utils.hpp>
1314
#include <sycl/ext/oneapi/properties/property_value.hpp>
1415

1516
#include <array>
@@ -27,49 +28,6 @@ template <size_t... Xs> struct AllNonZero {
2728
template <size_t X, size_t... Xs> struct AllNonZero<X, Xs...> {
2829
static inline constexpr bool value = X > 0 && AllNonZero<Xs...>::value;
2930
};
30-
31-
// Simple helpers for containing primitive types as template arguments.
32-
template <size_t... Sizes> struct SizeList {};
33-
template <char... Sizes> struct CharList {};
34-
35-
// Helper for converting characters to a constexpr string.
36-
template <char... Chars> struct CharsToStr {
37-
static inline constexpr const char value[] = {Chars..., '\0'};
38-
};
39-
40-
// Helper for converting a list of size_t values to a comma-separated string
41-
// representation. This is done by extracting the digit one-by-one and when
42-
// finishing a value, the parsed result is added to a separate list of
43-
// "parsed" characters with the delimiter.
44-
template <typename List, typename ParsedList, char... Chars>
45-
struct SizeListToStrHelper;
46-
template <size_t Value, size_t... Values, char... ParsedChars, char... Chars>
47-
struct SizeListToStrHelper<SizeList<Value, Values...>, CharList<ParsedChars...>,
48-
Chars...>
49-
: SizeListToStrHelper<SizeList<Value / 10, Values...>,
50-
CharList<ParsedChars...>, '0' + (Value % 10),
51-
Chars...> {};
52-
template <size_t... Values, char... ParsedChars, char... Chars>
53-
struct SizeListToStrHelper<SizeList<0, Values...>, CharList<ParsedChars...>,
54-
Chars...>
55-
: SizeListToStrHelper<SizeList<Values...>,
56-
CharList<ParsedChars..., Chars..., ','>> {};
57-
template <size_t... Values, char... ParsedChars>
58-
struct SizeListToStrHelper<SizeList<0, Values...>, CharList<ParsedChars...>>
59-
: SizeListToStrHelper<SizeList<Values...>,
60-
CharList<ParsedChars..., '0', ','>> {};
61-
template <char... ParsedChars, char... Chars>
62-
struct SizeListToStrHelper<SizeList<0>, CharList<ParsedChars...>, Chars...>
63-
: CharsToStr<ParsedChars..., Chars...> {};
64-
template <char... ParsedChars>
65-
struct SizeListToStrHelper<SizeList<0>, CharList<ParsedChars...>>
66-
: CharsToStr<ParsedChars..., '0'> {};
67-
template <>
68-
struct SizeListToStrHelper<SizeList<>, CharList<>> : CharsToStr<> {};
69-
70-
// Converts size_t values to a comma-separated string representation.
71-
template <size_t... Sizes>
72-
struct SizeListToStr : SizeListToStrHelper<SizeList<Sizes...>, CharList<>> {};
7331
} // namespace detail
7432

7533
struct properties_tag {};

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,114 @@ struct MergeProperties<std::tuple<LHSPropertyTs...>,
272272
using type = typename PrependTuple<min, merge_tails>::type;
273273
};
274274

275+
//******************************************************************************
276+
// Property value tooling
277+
//******************************************************************************
278+
279+
// Simple helpers for containing primitive types as template arguments.
280+
template <size_t... Sizes> struct SizeList {};
281+
template <char... Sizes> struct CharList {};
282+
283+
// Helper for converting characters to a constexpr string.
284+
template <char... Chars> struct CharsToStr {
285+
static inline constexpr const char value[] = {Chars..., '\0'};
286+
};
287+
288+
// Helper for converting a list of size_t values to a comma-separated string
289+
// representation. This is done by extracting the digit one-by-one and when
290+
// finishing a value, the parsed result is added to a separate list of
291+
// "parsed" characters with the delimiter.
292+
template <typename List, typename ParsedList, char... Chars>
293+
struct SizeListToStrHelper;
294+
295+
// Specialization for when we are in the process of converting a non-zero value
296+
// (Value). Chars will have the already converted digits of the original value
297+
// being converted. Instantiation of this will convert the least significant
298+
// digit in Value.
299+
// Example:
300+
// - Current: SizeListToStrHelper<SizeList<12>, CharList<'1', '0', ','>, '3'>
301+
// - Next: SizeListToStrHelper<SizeList<1>, CharList<'1', '0', ','>, '2', '3'>
302+
// - Outermost: SizeListToStrHelper<SizeList<10,123>, CharList<>>
303+
// - Final: SizeListToStrHelper<SizeList<0>,
304+
// CharList<'1', '0', ','>, '1', '2', '3'>>
305+
// - Result string: "10,123"
306+
template <size_t Value, size_t... Values, char... ParsedChars, char... Chars>
307+
struct SizeListToStrHelper<SizeList<Value, Values...>, CharList<ParsedChars...>,
308+
Chars...>
309+
: SizeListToStrHelper<SizeList<Value / 10, Values...>,
310+
CharList<ParsedChars...>, '0' + (Value % 10),
311+
Chars...> {};
312+
313+
// Specialization for when we have reached 0 in the current value we are
314+
// converting. In this case we are done with converting the current value and
315+
// we insert the converted digits from Chars into ParsedChars.
316+
// Example:
317+
// - Current: SizeListToStrHelper<SizeList<0,123>, CharList<>, '1', '0'>
318+
// - Next: SizeListToStrHelper<SizeList<123>, CharList<'1', '0', ','>>
319+
// - Outermost: SizeListToStrHelper<SizeList<10,123>, CharList<>>
320+
// - Final: SizeListToStrHelper<SizeList<0>,
321+
// CharList<'1', '0', ','>, '1', '2', '3'>>
322+
// - Result string: "10,123"
323+
template <size_t... Values, char... ParsedChars, char... Chars>
324+
struct SizeListToStrHelper<SizeList<0, Values...>, CharList<ParsedChars...>,
325+
Chars...>
326+
: SizeListToStrHelper<SizeList<Values...>,
327+
CharList<ParsedChars..., Chars..., ','>> {};
328+
329+
// Specialization for the special case where the value we are converting is 0
330+
// but the list of converted digits is empty. This means there was a 0 value in
331+
// the list and we can add it to ParsedChars directly.
332+
// Example:
333+
// - Current: SizeListToStrHelper<SizeList<0,123>, CharList<>>
334+
// - Next: SizeListToStrHelper<SizeList<123>, CharList<'0', ','>>
335+
// - Outermost: SizeListToStrHelper<SizeList<0,123>, CharList<>>
336+
// - Final: SizeListToStrHelper<SizeList<0>,
337+
// CharList<'0', ','>, '1', '2', '3'>>
338+
// - Result string: "0,123"
339+
template <size_t... Values, char... ParsedChars>
340+
struct SizeListToStrHelper<SizeList<0, Values...>, CharList<ParsedChars...>>
341+
: SizeListToStrHelper<SizeList<Values...>,
342+
CharList<ParsedChars..., '0', ','>> {};
343+
344+
// Specialization for when we have reached 0 in the current value we are
345+
// converting and there a no more values to parse. In this case we are done with
346+
// converting the current value and we insert the converted digits from Chars
347+
// into ParsedChars. We do not add a ',' as it is the end of the list.
348+
// Example:
349+
// - Current: SizeListToStrHelper<SizeList<0>, CharList<'1', '0', ','>, '1',
350+
// '2', '3'>>
351+
// - Next: None.
352+
// - Outermost: SizeListToStrHelper<SizeList<10,123>, CharList<>>
353+
// - Final: SizeListToStrHelper<SizeList<0>,
354+
// CharList<'1', '0', ','>, '1', '2', '3'>>
355+
// - Result string: "10,123"
356+
template <char... ParsedChars, char... Chars>
357+
struct SizeListToStrHelper<SizeList<0>, CharList<ParsedChars...>, Chars...>
358+
: CharsToStr<ParsedChars..., Chars...> {};
359+
360+
// Specialization for when we have reached 0 in the current value we are
361+
// converting and there a no more values to parse, but the list of converted
362+
// digits is empty. This means the last value in the list was a 0 so we can add
363+
// that to the ParsedChars and finish.
364+
// Example:
365+
// - Current: SizeListToStrHelper<SizeList<0>, CharList<'1', '0', ','>>>
366+
// - Next: None.
367+
// - Outermost: SizeListToStrHelper<SizeList<10,0>, CharList<>>
368+
// - Final: SizeListToStrHelper<SizeList<0>, CharList<>, '1', '0'>>
369+
// - Result string: "10,0"
370+
template <char... ParsedChars>
371+
struct SizeListToStrHelper<SizeList<0>, CharList<ParsedChars...>>
372+
: CharsToStr<ParsedChars..., '0'> {};
373+
374+
// Specialization for the empty list of values to convert. This results in an
375+
// empty string.
376+
template <>
377+
struct SizeListToStrHelper<SizeList<>, CharList<>> : CharsToStr<> {};
378+
379+
// Converts size_t values to a comma-separated string representation.
380+
template <size_t... Sizes>
381+
struct SizeListToStr : SizeListToStrHelper<SizeList<Sizes...>, CharList<>> {};
382+
275383
} // namespace detail
276384
} // namespace experimental
277385
} // namespace oneapi

sycl/unittests/misc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ add_definitions(-DSYCL_LIB_DIR="${sycl_lib_dir}")
33
add_sycl_unittest(MiscTests SHARED
44
CircularBuffer.cpp
55
OsUtils.cpp
6+
PropertyUtils.cpp
67
)

sycl/unittests/misc/PropertyUtils.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//==---- PropertyUtils.cpp -------------------------------------------------==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <gtest/gtest.h>
10+
11+
#include <string_view>
12+
13+
#include <sycl/ext/oneapi/properties/property_utils.hpp>
14+
15+
using namespace sycl::ext::oneapi::experimental::detail;
16+
17+
void checkEquality(std::string_view LHS, std::string_view RHS) {
18+
ASSERT_EQ(LHS, RHS);
19+
}
20+
21+
TEST(PropertyUtilsTest, SizeListToStrTest) {
22+
checkEquality(SizeListToStr<>::value, "");
23+
checkEquality(SizeListToStr<0>::value, "0");
24+
checkEquality(SizeListToStr<1>::value, "1");
25+
checkEquality(SizeListToStr<42>::value, "42");
26+
checkEquality(SizeListToStr<123>::value, "123");
27+
checkEquality(SizeListToStr<4321>::value, "4321");
28+
checkEquality(SizeListToStr<0, 1>::value, "0,1");
29+
checkEquality(SizeListToStr<1, 0>::value, "1,0");
30+
checkEquality(SizeListToStr<42, 43>::value, "42,43");
31+
checkEquality(SizeListToStr<0, 1, 42>::value, "0,1,42");
32+
checkEquality(SizeListToStr<1, 0, 42>::value, "1,0,42");
33+
checkEquality(SizeListToStr<1, 42, 0>::value, "1,42,0");
34+
checkEquality(SizeListToStr<0, 1, 42, 4321>::value, "0,1,42,4321");
35+
checkEquality(SizeListToStr<1, 0, 42, 4321>::value, "1,0,42,4321");
36+
checkEquality(SizeListToStr<1, 42, 0, 4321>::value, "1,42,0,4321");
37+
checkEquality(SizeListToStr<1, 42, 4321, 0>::value, "1,42,4321,0");
38+
}

0 commit comments

Comments
 (0)