Skip to content

Commit 1a6dd8c

Browse files
[SYCL][NFC] Consolidate all uses of OpenCL types, functions (#22532)
Refactoring needed to remove inclusion of OpenCL headers when compiling for SYCL device. This PR does mechanical renaming of every OpenCL interop host type to a new alias in `detail/cl.h` — `cl_context`→`OpenCLContextT`, `cl_mem`→`OpenCLMemT`, etc. — across headers and sources. This alias resolves to the corresponding OpenCL type, except when JIT compiling for SYCL device.
1 parent 21650c8 commit 1a6dd8c

40 files changed

Lines changed: 160 additions & 108 deletions

sycl/include/sycl/context.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class __SYCL_EXPORT context : public detail::OwnerLessBase<context> {
170170
/// \param ClContext is an instance of OpenCL cl_context.
171171
/// \param AsyncHandler is an instance of async_handler.
172172
#ifdef __SYCL_INTERNAL_API
173-
context(cl_context ClContext, async_handler AsyncHandler = {});
173+
context(OpenCLContextT ClContext, async_handler AsyncHandler = {});
174174
#endif
175175

176176
/// Queries this SYCL context for information.
@@ -230,7 +230,7 @@ class __SYCL_EXPORT context : public detail::OwnerLessBase<context> {
230230
///
231231
/// \return a valid instance of OpenCL cl_context.
232232
#ifdef __SYCL_INTERNAL_API
233-
cl_context get() const;
233+
OpenCLContextT get() const;
234234
#endif
235235

236236
/// Returns the backend associated with this context.

sycl/include/sycl/detail/backend_traits_opencl.hpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,92 +42,92 @@ namespace detail {
4242
// may be removed after removing the deprecated 'get_native()' methods
4343
// from the corresponding classes.
4444
template <> struct interop<backend::opencl, context> {
45-
using type = cl_context;
45+
using type = OpenCLContextT;
4646
};
4747

4848
template <> struct interop<backend::opencl, device> {
49-
using type = cl_device_id;
49+
using type = OpenCLDeviceIdT;
5050
};
5151

5252
template <> struct interop<backend::opencl, queue> {
53-
using type = cl_command_queue;
53+
using type = OpenCLCommandQueueT;
5454
};
5555

5656
template <> struct interop<backend::opencl, platform> {
57-
using type = cl_platform_id;
57+
using type = OpenCLPlatformT;
5858
};
5959

6060
template <typename DataT, int Dimensions, typename AllocatorT>
6161
struct BackendInput<backend::opencl, buffer<DataT, Dimensions, AllocatorT>> {
62-
using type = cl_mem;
62+
using type = OpenCLMemT;
6363
};
6464

6565
template <typename DataT, int Dimensions, typename AllocatorT>
6666
struct BackendReturn<backend::opencl, buffer<DataT, Dimensions, AllocatorT>> {
67-
using type = std::vector<cl_mem>;
67+
using type = std::vector<OpenCLMemT>;
6868
};
6969

7070
template <> struct BackendInput<backend::opencl, context> {
71-
using type = cl_context;
71+
using type = OpenCLContextT;
7272
};
7373

7474
template <> struct BackendReturn<backend::opencl, context> {
75-
using type = cl_context;
75+
using type = OpenCLContextT;
7676
};
7777

7878
template <> struct BackendInput<backend::opencl, device> {
79-
using type = cl_device_id;
79+
using type = OpenCLDeviceIdT;
8080
};
8181

8282
template <> struct BackendReturn<backend::opencl, device> {
83-
using type = cl_device_id;
83+
using type = OpenCLDeviceIdT;
8484
};
8585

8686
template <> struct interop<backend::opencl, event> {
87-
using type = std::vector<cl_event>;
88-
using value_type = cl_event;
87+
using type = std::vector<OpenCLEventT>;
88+
using value_type = OpenCLEventT;
8989
};
9090
template <> struct BackendInput<backend::opencl, event> {
91-
using type = std::vector<cl_event>;
92-
using value_type = cl_event;
91+
using type = std::vector<OpenCLEventT>;
92+
using value_type = OpenCLEventT;
9393
};
9494
template <> struct BackendReturn<backend::opencl, event> {
95-
using type = std::vector<cl_event>;
96-
using value_type = cl_event;
95+
using type = std::vector<OpenCLEventT>;
96+
using value_type = OpenCLEventT;
9797
};
9898

9999
template <> struct BackendInput<backend::opencl, queue> {
100-
using type = cl_command_queue;
100+
using type = OpenCLCommandQueueT;
101101
};
102102

103103
template <> struct BackendReturn<backend::opencl, queue> {
104-
using type = cl_command_queue;
104+
using type = OpenCLCommandQueueT;
105105
};
106106

107107
template <> struct BackendInput<backend::opencl, platform> {
108-
using type = cl_platform_id;
108+
using type = OpenCLPlatformT;
109109
};
110110

111111
template <> struct BackendReturn<backend::opencl, platform> {
112-
using type = cl_platform_id;
112+
using type = OpenCLPlatformT;
113113
};
114114

115115
template <bundle_state State>
116116
struct BackendInput<backend::opencl, kernel_bundle<State>> {
117-
using type = cl_program;
117+
using type = OpenCLProgramT;
118118
};
119119

120120
template <bundle_state State>
121121
struct BackendReturn<backend::opencl, kernel_bundle<State>> {
122-
using type = std::vector<cl_program>;
122+
using type = std::vector<OpenCLProgramT>;
123123
};
124124

125125
template <> struct BackendInput<backend::opencl, kernel> {
126-
using type = cl_kernel;
126+
using type = OpenCLKernelT;
127127
};
128128

129129
template <> struct BackendReturn<backend::opencl, kernel> {
130-
using type = cl_kernel;
130+
using type = OpenCLKernelT;
131131
};
132132

133133
using graph = ext::oneapi::experimental::command_graph<
@@ -156,7 +156,7 @@ template <> struct InteropFeatureSupportMap<backend::opencl> {
156156
namespace ur {
157157
// Cast for std::vector<cl_event>, according to the spec, make_event
158158
// should create one(?) event from a vector of cl_event
159-
template <class To> inline To cast(std::vector<cl_event> value) {
159+
template <class To> inline To cast(std::vector<OpenCLEventT> value) {
160160
assert(value.size() == 1 &&
161161
"Temporary workaround requires that the "
162162
"size of the input vector for make_event be equal to one.");
@@ -166,11 +166,11 @@ template <class To> inline To cast(std::vector<cl_event> value) {
166166
// These conversions should use UR interop API.
167167
template <>
168168
inline ur_program_handle_t
169-
cast(cl_program) = delete; // Use urProgramCreateWithNativeHandle
169+
cast(OpenCLProgramT) = delete; // Use urProgramCreateWithNativeHandle
170170

171171
template <>
172172
inline ur_device_handle_t
173-
cast(cl_device_id) = delete; // Use urDeviceCreateWithNativeHandle
173+
cast(OpenCLDeviceIdT) = delete; // Use urDeviceCreateWithNativeHandle
174174
} // namespace ur
175175
} // namespace detail
176176
} // namespace _V1

sycl/include/sycl/detail/cl.h

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#pragma once
10+
#include <sycl/detail/ur.hpp>
1011

1112
// Suppress a compiler message about undefined CL_TARGET_OPENCL_VERSION
1213
// and define all symbols up to OpenCL 3.0
@@ -19,20 +20,68 @@
1920
#define CL_ENABLE_BETA_EXTENSIONS
2021
#endif
2122

23+
#if !defined(__SYCL_DEVICE_ONLY__) || !defined(__SYCL_JIT__)
24+
#include <CL/cl.h>
25+
#include <CL/cl_ext.h>
26+
#endif
27+
28+
namespace sycl {
29+
inline namespace _V1 {
2230
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SYCL_JIT__)
2331
// Don't include the OpenCL headers when compiling for SYCL device, as they only
2432
// define the host-side API. Instead, define the necessary types as opaque
2533
// pointers to not break the SYCL headers that include this header.
26-
using cl_command_queue = void *;
27-
using cl_context = void *;
28-
using cl_device_id = void *;
29-
using cl_event = void *;
30-
using cl_kernel = void *;
31-
using cl_mem = void *;
32-
using cl_platform_id = void *;
33-
using cl_program = void *;
34-
using cl_sampler = void *;
35-
#else // !defined(__SYCL_DEVICE_ONLY__) || !defined(__SYCL_JIT__)
36-
#include <CL/cl.h>
37-
#include <CL/cl_ext.h>
38-
#endif // defined(__SYCL_DEVICE_ONLY__) && defined(__SYCL_JIT__)
34+
using OpenCLCommandQueueT = void *;
35+
using OpenCLContextT = void *;
36+
using OpenCLDeviceIdT = void *;
37+
using OpenCLEventT = void *;
38+
using OpenCLKernelT = void *;
39+
using OpenCLMemT = void *;
40+
using OpenCLPlatformT = void *;
41+
using OpenCLProgramT = void *;
42+
using OpenCLSamplerT = void *;
43+
namespace detail {
44+
inline void retainOpenCLCommandQueue(ur_native_handle_t) {}
45+
inline void retainOpenCLContext(ur_native_handle_t) {}
46+
inline void retainOpenCLDevice(ur_native_handle_t) {}
47+
inline void retainOpenCLEvent(ur_native_handle_t) {}
48+
inline void retainOpenCLKernel(ur_native_handle_t) {}
49+
inline void retainOpenCLMemObject(ur_native_handle_t) {}
50+
inline void retainOpenCLProgram(ur_native_handle_t) {}
51+
} // namespace detail
52+
#else // !defined(__SYCL_DEVICE_ONLY__)
53+
using OpenCLCommandQueueT = cl_command_queue;
54+
using OpenCLContextT = cl_context;
55+
using OpenCLDeviceIdT = cl_device_id;
56+
using OpenCLEventT = cl_event;
57+
using OpenCLKernelT = cl_kernel;
58+
using OpenCLMemT = cl_mem;
59+
using OpenCLPlatformT = cl_platform_id;
60+
using OpenCLProgramT = cl_program;
61+
using OpenCLSamplerT = cl_sampler;
62+
namespace detail {
63+
inline void retainOpenCLCommandQueue(ur_native_handle_t Queue) {
64+
__SYCL_OCL_CALL(clRetainCommandQueue, ur::cast<OpenCLCommandQueueT>(Queue));
65+
}
66+
inline void retainOpenCLContext(ur_native_handle_t Context) {
67+
__SYCL_OCL_CALL(clRetainContext, ur::cast<OpenCLContextT>(Context));
68+
}
69+
inline void retainOpenCLDevice(ur_native_handle_t Device) {
70+
__SYCL_OCL_CALL(clRetainDevice, ur::cast<OpenCLDeviceIdT>(Device));
71+
}
72+
inline void retainOpenCLEvent(ur_native_handle_t Event) {
73+
__SYCL_OCL_CALL(clRetainEvent, ur::cast<OpenCLEventT>(Event));
74+
}
75+
inline void retainOpenCLKernel(ur_native_handle_t Kernel) {
76+
__SYCL_OCL_CALL(clRetainKernel, ur::cast<OpenCLKernelT>(Kernel));
77+
}
78+
inline void retainOpenCLMemObject(ur_native_handle_t MemObject) {
79+
__SYCL_OCL_CALL(clRetainMemObject, ur::cast<OpenCLMemT>(MemObject));
80+
}
81+
inline void retainOpenCLProgram(ur_native_handle_t Program) {
82+
__SYCL_OCL_CALL(clRetainProgram, ur::cast<OpenCLProgramT>(Program));
83+
}
84+
} // namespace detail
85+
#endif // defined(__SYCL_DEVICE_ONLY__)
86+
} // namespace _V1
87+
} // namespace sycl

sycl/include/sycl/device.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class __SYCL_STANDALONE_DEBUG __SYCL_EXPORT device {
7474
///
7575
/// \param DeviceId is OpenCL device represented with cl_device_id
7676
#ifdef __SYCL_INTERNAL_API
77-
explicit device(cl_device_id DeviceId);
77+
explicit device(OpenCLDeviceIdT DeviceId);
7878
#endif
7979

8080
/// Constructs a SYCL device instance using the device selected
@@ -134,7 +134,7 @@ class __SYCL_STANDALONE_DEBUG __SYCL_EXPORT device {
134134
/// \return a valid cl_device_id instance in accordance with the requirements
135135
/// described in 4.3.1.
136136
#ifdef __SYCL_INTERNAL_API
137-
cl_device_id get() const;
137+
OpenCLDeviceIdT get() const;
138138
#endif
139139

140140
/// Check if device is a CPU device

sycl/include/sycl/event.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class __SYCL_EXPORT event : public detail::OwnerLessBase<event> {
5757
/// \param ClEvent is a valid instance of OpenCL cl_event.
5858
/// \param SyclContext is an instance of SYCL context.
5959
#ifdef __SYCL_INTERNAL_API
60-
event(cl_event ClEvent, const context &SyclContext);
60+
event(OpenCLEventT ClEvent, const context &SyclContext);
6161
#endif
6262

6363
event(const event &rhs) = default;

sycl/include/sycl/handler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ class __SYCL_EXPORT handler {
505505
template <typename T> void setArgHelper(int ArgIndex, T &&Arg) {
506506
void *StoredArg = storePlainArg(Arg);
507507

508-
if (!std::is_same<cl_mem, T>::value && std::is_pointer<T>::value) {
508+
if (!std::is_same<OpenCLMemT, T>::value && std::is_pointer<T>::value) {
509509
addArg(detail::kernel_param_kind_t::kind_pointer, StoredArg, sizeof(T),
510510
ArgIndex);
511511
} else if (ext::oneapi::experimental::detail::is_struct_with_special_type<
@@ -1150,9 +1150,9 @@ class __SYCL_EXPORT handler {
11501150
&& std::is_standard_layout<std::remove_reference_t<T>>::value
11511151
#endif
11521152
|| is_same_type<sampler, T>::value // Sampler
1153-
|| (!is_same_type<cl_mem, T>::value &&
1153+
|| (!is_same_type<OpenCLMemT, T>::value &&
11541154
std::is_pointer_v<remove_cv_ref_t<T>>) // USM
1155-
|| is_same_type<cl_mem, T>::value // Interop
1155+
|| is_same_type<OpenCLMemT, T>::value // Interop
11561156
|| is_same_type<stream, T>::value // Stream
11571157
|| sycl::is_device_copyable_v<remove_cv_ref_t<T>>;
11581158
};

sycl/include/sycl/image.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class __SYCL_EXPORT image_plain {
238238
uint8_t Dimensions, const property_list &PropList);
239239

240240
#ifdef __SYCL_INTERNAL_API
241-
image_plain(cl_mem ClMemObject, const context &SyclContext,
241+
image_plain(OpenCLMemT ClMemObject, const context &SyclContext,
242242
event AvailableEvent,
243243
std::unique_ptr<SYCLMemObjAllocator> Allocator,
244244
uint8_t Dimensions);
@@ -583,7 +583,7 @@ class __SYCL2020_DEPRECATED(
583583
Dimensions, PropList, /*IsConstPtr*/ false) {}
584584

585585
#ifdef __SYCL_INTERNAL_API
586-
image(cl_mem ClMemObject, const context &SyclContext,
586+
image(OpenCLMemT ClMemObject, const context &SyclContext,
587587
event AvailableEvent = {})
588588
: common_base(ClMemObject, SyclContext, AvailableEvent,
589589
std::make_unique<

sycl/include/sycl/kernel.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class __SYCL_EXPORT kernel : public detail::OwnerLessBase<kernel> {
7878
/// \param ClKernel is a valid OpenCL cl_kernel instance
7979
/// \param SyclContext is a valid SYCL context
8080
#ifdef __SYCL_INTERNAL_API
81-
kernel(cl_kernel ClKernel, const context &SyclContext);
81+
kernel(OpenCLKernelT ClKernel, const context &SyclContext);
8282
#endif
8383

8484
kernel() = delete;
@@ -114,7 +114,7 @@ class __SYCL_EXPORT kernel : public detail::OwnerLessBase<kernel> {
114114
///
115115
/// \return a valid cl_kernel instance
116116
#ifdef __SYCL_INTERNAL_API
117-
cl_kernel get() const;
117+
OpenCLKernelT get() const;
118118
#endif
119119

120120
/// Get the context that this kernel is defined for.

sycl/include/sycl/platform.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class __SYCL_EXPORT platform : public detail::OwnerLessBase<platform> {
8282
///
8383
/// \param PlatformId is an OpenCL cl_platform_id instance.
8484
#ifdef __SYCL_INTERNAL_API
85-
explicit platform(cl_platform_id PlatformId);
85+
explicit platform(OpenCLPlatformT PlatformId);
8686
#endif
8787

8888
/// Constructs a SYCL platform instance using a device_selector.
@@ -132,7 +132,7 @@ class __SYCL_EXPORT platform : public detail::OwnerLessBase<platform> {
132132
///
133133
/// \return an instance of OpenCL cl_platform_id.
134134
#ifdef __SYCL_INTERNAL_API
135-
cl_platform_id get() const;
135+
OpenCLPlatformT get() const;
136136
#endif
137137

138138
/// Checks if platform supports specified extension.

sycl/include/sycl/queue.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> {
365365
/// \param SyclContext is a valid SYCL context.
366366
/// \param AsyncHandler is a SYCL asynchronous exception handler.
367367
#ifdef __SYCL_INTERNAL_API
368-
queue(cl_command_queue ClQueue, const context &SyclContext,
368+
queue(OpenCLCommandQueueT ClQueue, const context &SyclContext,
369369
const async_handler &AsyncHandler = {});
370370
#endif
371371

@@ -393,7 +393,7 @@ class __SYCL_EXPORT queue : public detail::OwnerLessBase<queue> {
393393
/// \return a valid instance of OpenCL queue, which is retained before being
394394
/// returned.
395395
#ifdef __SYCL_INTERNAL_API
396-
cl_command_queue get() const;
396+
OpenCLCommandQueueT get() const;
397397
#endif
398398

399399
/// \return an associated SYCL context.

0 commit comments

Comments
 (0)