Skip to content

Commit e98d732

Browse files
[SYCL] Implement sycl::opencl::cl_* aliases (#7931)
This resolves #7242
1 parent e0f6391 commit e98d732

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

sycl/include/sycl/aliases.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ using cl_half = half;
106106
using cl_float = float;
107107
using cl_double = double;
108108

109+
namespace opencl {
110+
// Strictly speaking, cl_* aliases should not be defined in opencl namespace in
111+
// SYCL 1.2.1 mode, but we do so to simplify our implementation
112+
using cl_bool = bool;
113+
using cl_char = std::int8_t;
114+
using cl_uchar = std::uint8_t;
115+
using cl_short = std::int16_t;
116+
using cl_ushort = std::uint16_t;
117+
using cl_int = std::int32_t;
118+
using cl_uint = std::uint32_t;
119+
using cl_long = std::int64_t;
120+
using cl_ulong = std::uint64_t;
121+
using cl_half = half;
122+
using cl_float = float;
123+
using cl_double = double;
124+
} // namespace opencl
125+
109126
// Vector aliases are different between SYCL 1.2.1 and SYCL 2020
110127
#if SYCL_LANGUAGE_VERSION >= 202001
111128
__SYCL_2020_MAKE_VECTOR_ALIASES_FOR_VECTOR_LENGTH(2)

sycl/test/aliases-opencl.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// RUN: %clangxx -fsycl -fsyntax-only %s
2+
//
3+
// This test aims to check that the implementation provides aliases for
4+
// interoperability with OpenCL in accordance with section C.7.1 Data types of
5+
// SYCL 2020 specification, revision 6
6+
7+
#include <sycl/sycl.hpp>
8+
9+
#include <type_traits>
10+
11+
int main() {
12+
// cl_bool
13+
// Alias to a conditional data type which can be either true or false.
14+
// The value true expands to the integer constant 1 and the value false
15+
// expands to the integer constant 0.
16+
constexpr sycl::opencl::cl_bool True = true;
17+
constexpr sycl::opencl::cl_bool False = false;
18+
static_assert((int)True == 1);
19+
static_assert((int)False == 0);
20+
21+
// cl_char
22+
// Alias to a signed 1-bit integer, as defined by the C++ core language.
23+
static_assert(sizeof(sycl::opencl::cl_char) == 1);
24+
static_assert(std::is_signed_v<sycl::opencl::cl_char>);
25+
static_assert(std::is_integral_v<sycl::opencl::cl_char>);
26+
27+
// cl_uchar
28+
// Alias to an unsigned 1-bit integer, as defined by the C++ core language.
29+
static_assert(sizeof(sycl::opencl::cl_uchar) == 1);
30+
static_assert(std::is_unsigned_v<sycl::opencl::cl_uchar>);
31+
static_assert(std::is_integral_v<sycl::opencl::cl_uchar>);
32+
33+
// cl_short
34+
// Alias to a signed 2-bit integer, as defined by the C++ core language.
35+
static_assert(sizeof(sycl::opencl::cl_short) == 2);
36+
static_assert(std::is_signed_v<sycl::opencl::cl_short>);
37+
static_assert(std::is_integral_v<sycl::opencl::cl_short>);
38+
39+
// cl_ushort
40+
// Alias to an unsigned 2-bit integer, as defined by the C++ core language.
41+
static_assert(sizeof(sycl::opencl::cl_ushort) == 2);
42+
static_assert(std::is_unsigned_v<sycl::opencl::cl_ushort>);
43+
static_assert(std::is_integral_v<sycl::opencl::cl_ushort>);
44+
45+
// cl_int
46+
// Alias to a signed 4-bit integer, as defined by the C++ core language.
47+
static_assert(sizeof(sycl::opencl::cl_int) == 4);
48+
static_assert(std::is_signed_v<sycl::opencl::cl_int>);
49+
static_assert(std::is_integral_v<sycl::opencl::cl_int>);
50+
51+
// cl_uint
52+
// Alias to an unsigned 4-bit integer, as defined by the C++ core language.
53+
static_assert(sizeof(sycl::opencl::cl_uint) == 4);
54+
static_assert(std::is_unsigned_v<sycl::opencl::cl_uint>);
55+
static_assert(std::is_integral_v<sycl::opencl::cl_uint>);
56+
57+
// cl_long
58+
// Alias to a signed 8-bit integer, as defined by the C++ core language.
59+
static_assert(sizeof(sycl::opencl::cl_long) == 8);
60+
static_assert(std::is_signed_v<sycl::opencl::cl_long>);
61+
static_assert(std::is_integral_v<sycl::opencl::cl_long>);
62+
63+
// cl_ulong
64+
// Alias to an unsigned 8-bit integer, as defined by the C++ core language.
65+
static_assert(sizeof(sycl::opencl::cl_ulong) == 8);
66+
static_assert(std::is_unsigned_v<sycl::opencl::cl_ulong>);
67+
static_assert(std::is_integral_v<sycl::opencl::cl_ulong>);
68+
69+
// TODO: how can we check that the type conforms to IEEE 754?
70+
// There is std::numeric_limits<T>::is_iec559 - is it enough?
71+
72+
// cl_float
73+
// Alias to a 4-bit floating-point. The float data type must conform to the
74+
// IEEE 754 single precision storage format.
75+
static_assert(sizeof(sycl::opencl::cl_float) == 4);
76+
static_assert(std::is_floating_point_v<sycl::opencl::cl_float>);
77+
78+
// cl_double
79+
// Alias to a 8-bit floating-point. The double data type must conform to the
80+
// IEEE 754 double precision storage format.
81+
static_assert(sizeof(sycl::opencl::cl_double) == 8);
82+
static_assert(std::is_floating_point_v<sycl::opencl::cl_double>);
83+
84+
// cl_half
85+
// Alias to a 2-bit floating-point. The half data type must conform to the
86+
// IEEE 754-2001 half precision storage format.
87+
static_assert(sizeof(sycl::opencl::cl_half) == 2);
88+
// TODO: do we need std::is_floating_point to return true for
89+
// sycl::opencl::cl_half?
90+
// We alias it to our custom class sycl::half and C++ spec says it is UB
91+
// to provide specializations for std::is_floating_point type trait
92+
// static_assert(std::is_floating_point_v<sycl::opencl::cl_half>);
93+
94+
return 0;
95+
}

0 commit comments

Comments
 (0)