Skip to content

Commit dd7f82c

Browse files
[SYCL] Make Level-Zero interop API SYCL-2020 compliant for queue, event, and kernel_bundle(was program). (#4512)
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent ebbd568 commit dd7f82c

File tree

12 files changed

+181
-40
lines changed

12 files changed

+181
-40
lines changed

sycl/include/CL/sycl/backend.hpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ using backend_return_t =
6666
typename backend_traits<Backend>::template return_type<SyclType>;
6767

6868
template <backend BackendName, class SyclObjectT>
69-
auto get_native(const SyclObjectT &Obj) ->
70-
typename interop<BackendName, SyclObjectT>::type {
69+
auto get_native(const SyclObjectT &Obj)
70+
-> backend_return_t<BackendName, SyclObjectT> {
7171
// TODO use SYCL 2020 exception when implemented
7272
if (Obj.get_backend() != BackendName)
7373
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
@@ -146,17 +146,18 @@ make_context(
146146
}
147147

148148
template <backend Backend>
149+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_queue free function")
149150
typename std::enable_if<
150151
detail::InteropFeatureSupportMap<Backend>::MakeQueue == true, queue>::type
151-
make_queue(const typename backend_traits<Backend>::template input_type<queue>
152-
&BackendObject,
153-
const context &TargetContext, bool KeepOwnership,
154-
const async_handler Handler = {}) {
152+
make_queue(
153+
const typename backend_traits<Backend>::template input_type<queue>
154+
&BackendObject,
155+
const context &TargetContext, bool KeepOwnership,
156+
const async_handler Handler = {}) {
155157
return detail::make_queue(detail::pi::cast<pi_native_handle>(BackendObject),
156158
TargetContext, KeepOwnership, Handler, Backend);
157159
}
158160

159-
// TODO: remove this version (without ownership) when allowed to break ABI.
160161
template <backend Backend>
161162
typename std::enable_if<
162163
detail::InteropFeatureSupportMap<Backend>::MakeQueue == true, queue>::type
@@ -178,11 +179,13 @@ make_event(const typename backend_traits<Backend>::template input_type<event>
178179
}
179180

180181
template <backend Backend>
182+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_event free function")
181183
typename std::enable_if<
182184
detail::InteropFeatureSupportMap<Backend>::MakeEvent == true, event>::type
183-
make_event(const typename backend_traits<Backend>::template input_type<event>
184-
&BackendObject,
185-
const context &TargetContext, bool KeepOwnership) {
185+
make_event(
186+
const typename backend_traits<Backend>::template input_type<event>
187+
&BackendObject,
188+
const context &TargetContext, bool KeepOwnership) {
186189
return detail::make_event(detail::pi::cast<pi_native_handle>(BackendObject),
187190
TargetContext, KeepOwnership, Backend);
188191
}

sycl/include/CL/sycl/backend/opencl.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct BackendInput<backend::opencl, kernel_bundle<State>> {
7575

7676
template <bundle_state State>
7777
struct BackendReturn<backend::opencl, kernel_bundle<State>> {
78+
// TODO: Per SYCL 2020 this should be std::vector<cl_program>
7879
using type = cl_program;
7980
};
8081

sycl/include/CL/sycl/context.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class __SYCL_EXPORT context {
216216
///
217217
/// \return a native handle, the type of which defined by the backend.
218218
template <backend BackendName>
219-
__SYCL_DEPRECATED("Use SYCL-2020 sycl::get_native free function")
219+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
220220
auto get_native() const -> typename interop<BackendName, context>::type {
221221
return reinterpret_cast<typename interop<BackendName, context>::type>(
222222
getNative());

sycl/include/CL/sycl/device.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class __SYCL_EXPORT device {
184184
///
185185
/// \return a native handle, the type of which defined by the backend.
186186
template <backend BackendName>
187-
__SYCL_DEPRECATED("Use SYCL-2020 sycl::get_native free function")
187+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
188188
auto get_native() const -> typename interop<BackendName, device>::type {
189189
return (typename interop<BackendName, device>::type)getNative();
190190
}

sycl/include/CL/sycl/event.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ class __SYCL_EXPORT event {
129129
///
130130
/// \return a native handle, the type of which defined by the backend.
131131
template <backend BackendName>
132+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
132133
auto get_native() const -> typename interop<BackendName, event>::type {
133134
return reinterpret_cast<typename interop<BackendName, event>::type>(
134135
getNative());
135136
}
137+
136138
private:
137139
event(std::shared_ptr<detail::event_impl> EventImpl);
138140

sycl/include/CL/sycl/interop_handle.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class interop_handle {
8686
template <backend Backend = backend::opencl>
8787
backend_return_t<Backend, queue> get_native_queue() const {
8888
#ifndef __SYCL_DEVICE_ONLY__
89-
// TODO: replace the exception thrown below with the SYCL-2020 exception
89+
// TODO: replace the exception thrown below with the SYCL 2020 exception
9090
// with the error code 'errc::backend_mismatch' when those new exceptions
9191
// are ready to be used.
9292
if (Backend != get_backend())
@@ -107,7 +107,7 @@ class interop_handle {
107107
template <backend Backend = backend::opencl>
108108
backend_return_t<Backend, device> get_native_device() const {
109109
#ifndef __SYCL_DEVICE_ONLY__
110-
// TODO: replace the exception thrown below with the SYCL-2020 exception
110+
// TODO: replace the exception thrown below with the SYCL 2020 exception
111111
// with the error code 'errc::backend_mismatch' when those new exceptions
112112
// are ready to be used.
113113
if (Backend != get_backend())
@@ -129,7 +129,7 @@ class interop_handle {
129129
template <backend Backend = backend::opencl>
130130
backend_return_t<Backend, context> get_native_context() const {
131131
#ifndef __SYCL_DEVICE_ONLY__
132-
// TODO: replace the exception thrown below with the SYCL-2020 exception
132+
// TODO: replace the exception thrown below with the SYCL 2020 exception
133133
// with the error code 'errc::backend_mismatch' when those new exceptions
134134
// are ready to be used.
135135
if (Backend != get_backend())

sycl/include/CL/sycl/kernel_bundle.hpp

+26-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ __SYCL_INLINE_NAMESPACE(cl) {
2525
namespace sycl {
2626
// Forward declaration
2727
template <backend Backend> class backend_traits;
28+
template <backend Backend, class SyclT>
29+
auto get_native(const SyclT &Obj) -> backend_return_t<Backend, SyclT>;
2830

2931
namespace detail {
3032
class kernel_id_impl;
@@ -176,8 +178,8 @@ class __SYCL_EXPORT kernel_bundle_plain {
176178
void set_specialization_constant_impl(const char *SpecName, void *Value,
177179
size_t Size) noexcept;
178180

179-
void get_specialization_constant_impl(const char *SpecName, void *Value) const
180-
noexcept;
181+
void get_specialization_constant_impl(const char *SpecName,
182+
void *Value) const noexcept;
181183

182184
bool is_specialization_constant_set(const char *SpecName) const noexcept;
183185

@@ -308,9 +310,9 @@ class kernel_bundle : public detail::kernel_bundle_plain {
308310
}
309311

310312
template <backend Backend>
313+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
311314
std::vector<typename backend_traits<Backend>::template return_type<
312-
kernel_bundle<State>>>
313-
get_native() {
315+
kernel_bundle<State>>> get_native() {
314316
std::vector<typename backend_traits<Backend>::template return_type<
315317
kernel_bundle<State>>>
316318
ReturnValue;
@@ -335,6 +337,25 @@ class kernel_bundle : public detail::kernel_bundle_plain {
335337

336338
template <class T>
337339
friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
340+
341+
template <backend Backend, class SyclT>
342+
friend auto get_native(const SyclT &Obj) -> backend_return_t<Backend, SyclT>;
343+
344+
template <backend Backend>
345+
backend_return_t<Backend, kernel_bundle<State>> getNative() const {
346+
// NOTE: implementation assumes that the return type is a
347+
// derivative of std::vector.
348+
backend_return_t<Backend, kernel_bundle<State>> ReturnValue;
349+
ReturnValue.reserve(std::distance(begin(), end()));
350+
351+
for (const device_image<State> &DevImg : *this) {
352+
ReturnValue.push_back(
353+
detail::pi::cast<typename decltype(ReturnValue)::value_type>(
354+
DevImg.getNative()));
355+
}
356+
357+
return ReturnValue;
358+
}
338359
};
339360

340361
/////////////////////////
@@ -604,7 +625,7 @@ __SYCL_EXPORT std::vector<sycl::device> find_device_intersection(
604625
__SYCL_EXPORT std::shared_ptr<detail::kernel_bundle_impl>
605626
link_impl(const std::vector<kernel_bundle<bundle_state::object>> &ObjectBundles,
606627
const std::vector<device> &Devs, const property_list &PropList);
607-
}
628+
} // namespace detail
608629

609630
/// \returns a new kernel_bundle which contains the device images from the
610631
/// ObjectBundles that are translated into one or more new device images of

sycl/include/CL/sycl/platform.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class __SYCL_EXPORT platform {
121121
///
122122
/// \return a native handle, the type of which defined by the backend.
123123
template <backend BackendName>
124-
__SYCL_DEPRECATED("Use SYCL-2020 sycl::get_native free function")
124+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
125125
auto get_native() const -> typename interop<BackendName, platform>::type {
126126
return reinterpret_cast<typename interop<BackendName, platform>::type>(
127127
getNative());

sycl/include/CL/sycl/program.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ class __SYCL_EXPORT __SYCL2020_DEPRECATED(
365365
///
366366
/// \return a native handle, the type of which defined by the backend.
367367
template <backend BackendName>
368+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
368369
auto get_native() const -> typename interop<BackendName, program>::type {
369370
return reinterpret_cast<typename interop<BackendName, program>::type>(
370371
getNative());

sycl/include/CL/sycl/queue.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class AssertInfoCopier;
8383
static event submitAssertCapture(queue &, event &, queue *,
8484
const detail::code_location &);
8585
#endif
86-
}
86+
} // namespace detail
8787

8888
/// Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
8989
///
@@ -1024,6 +1024,7 @@ class __SYCL_EXPORT queue {
10241024
///
10251025
/// \return a native handle, the type of which defined by the backend.
10261026
template <backend BackendName>
1027+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
10271028
auto get_native() const -> typename interop<BackendName, queue>::type {
10281029
return reinterpret_cast<typename interop<BackendName, queue>::type>(
10291030
getNative());

sycl/include/sycl/ext/oneapi/backend/level_zero.hpp

+69-5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ template <> struct BackendInput<backend::level_zero, context> {
8282
};
8383
};
8484

85+
template <> struct BackendInput<backend::level_zero, queue> {
86+
using type = struct {
87+
interop<backend::level_zero, queue>::type NativeHandle;
88+
ext::oneapi::level_zero::ownership Ownership;
89+
};
90+
};
91+
92+
template <> struct BackendInput<backend::level_zero, event> {
93+
using type = struct {
94+
interop<backend::level_zero, event>::type NativeHandle;
95+
ext::oneapi::level_zero::ownership Ownership;
96+
};
97+
};
98+
99+
template <bundle_state State>
100+
struct BackendInput<backend::level_zero, kernel_bundle<State>> {
101+
using type = ze_module_handle_t;
102+
};
103+
104+
template <bundle_state State>
105+
struct BackendReturn<backend::level_zero, kernel_bundle<State>> {
106+
using type = std::vector<ze_module_handle_t>;
107+
};
108+
85109
template <> struct BackendReturn<backend::level_zero, kernel> {
86110
using type = ze_kernel_handle_t;
87111
};
@@ -90,11 +114,11 @@ template <> struct InteropFeatureSupportMap<backend::level_zero> {
90114
static constexpr bool MakePlatform = true;
91115
static constexpr bool MakeDevice = true;
92116
static constexpr bool MakeContext = true;
93-
static constexpr bool MakeQueue = false;
117+
static constexpr bool MakeQueue = true;
94118
static constexpr bool MakeEvent = true;
119+
static constexpr bool MakeKernelBundle = true;
95120
static constexpr bool MakeBuffer = false;
96121
static constexpr bool MakeKernel = false;
97-
static constexpr bool MakeKernelBundle = false;
98122
};
99123
} // namespace detail
100124

@@ -122,15 +146,15 @@ __SYCL_EXPORT event make_event(const context &Context,
122146
// Construction of SYCL platform.
123147
template <typename T, typename detail::enable_if_t<
124148
std::is_same<T, platform>::value> * = nullptr>
125-
__SYCL_DEPRECATED("Use SYCL-2020 sycl::make_platform free function")
149+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_platform free function")
126150
T make(typename interop<backend::level_zero, T>::type Interop) {
127151
return make_platform(reinterpret_cast<pi_native_handle>(Interop));
128152
}
129153

130154
// Construction of SYCL device.
131155
template <typename T, typename detail::enable_if_t<
132156
std::is_same<T, device>::value> * = nullptr>
133-
__SYCL_DEPRECATED("Use SYCL-2020 sycl::make_device free function")
157+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_device free function")
134158
T make(const platform &Platform,
135159
typename interop<backend::level_zero, T>::type Interop) {
136160
return make_device(Platform, reinterpret_cast<pi_native_handle>(Interop));
@@ -147,7 +171,7 @@ T make(const platform &Platform,
147171
///
148172
template <typename T, typename std::enable_if<
149173
std::is_same<T, context>::value>::type * = nullptr>
150-
__SYCL_DEPRECATED("Use SYCL-2020 sycl::make_context free function")
174+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_context free function")
151175
T make(const std::vector<device> &DeviceList,
152176
typename interop<backend::level_zero, T>::type Interop,
153177
ownership Ownership = ownership::transfer) {
@@ -158,6 +182,7 @@ T make(const std::vector<device> &DeviceList,
158182
// Construction of SYCL program.
159183
template <typename T, typename detail::enable_if_t<
160184
std::is_same<T, program>::value> * = nullptr>
185+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_kernel_bundle free function")
161186
T make(const context &Context,
162187
typename interop<backend::level_zero, T>::type Interop) {
163188
return make_program(Context, reinterpret_cast<pi_native_handle>(Interop));
@@ -166,6 +191,7 @@ T make(const context &Context,
166191
// Construction of SYCL queue.
167192
template <typename T, typename detail::enable_if_t<
168193
std::is_same<T, queue>::value> * = nullptr>
194+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_queue free function")
169195
T make(const context &Context,
170196
typename interop<backend::level_zero, T>::type Interop,
171197
ownership Ownership = ownership::transfer) {
@@ -176,6 +202,7 @@ T make(const context &Context,
176202
// Construction of SYCL event.
177203
template <typename T, typename detail::enable_if_t<
178204
std::is_same<T, event>::value> * = nullptr>
205+
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_event free function")
179206
T make(const context &Context,
180207
typename interop<backend::level_zero, T>::type Interop,
181208
ownership Ownership = ownership::transfer) {
@@ -197,9 +224,46 @@ context make_context<backend::level_zero>(
197224
BackendObject.Ownership == ext::oneapi::level_zero::ownership::keep);
198225
}
199226

227+
// Specialization of sycl::make_queue for Level-Zero backend.
228+
template <>
229+
queue make_queue<backend::level_zero>(
230+
const backend_input_t<backend::level_zero, queue> &BackendObject,
231+
const context &TargetContext, const async_handler Handler) {
232+
return ext::oneapi::level_zero::make_queue(
233+
TargetContext,
234+
detail::pi::cast<pi_native_handle>(BackendObject.NativeHandle),
235+
BackendObject.Ownership == ext::oneapi::level_zero::ownership::keep);
236+
}
237+
238+
// Specialization of sycl::make_event for Level-Zero backend.
239+
template <>
240+
event make_event<backend::level_zero>(
241+
const backend_input_t<backend::level_zero, event> &BackendObject,
242+
const context &TargetContext) {
243+
return ext::oneapi::level_zero::make_event(
244+
TargetContext,
245+
detail::pi::cast<pi_native_handle>(BackendObject.NativeHandle),
246+
BackendObject.Ownership == ext::oneapi::level_zero::ownership::keep);
247+
}
248+
249+
// TODO: remove this specialization when generic is changed to call
250+
// .GetNative() instead of .get_native() member of kernel_bundle.
251+
template <>
252+
auto get_native<backend::level_zero>(
253+
const kernel_bundle<bundle_state::executable> &Obj)
254+
-> backend_return_t<backend::level_zero,
255+
kernel_bundle<bundle_state::executable>> {
256+
// TODO use SYCL 2020 exception when implemented
257+
if (Obj.get_backend() != backend::level_zero)
258+
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
259+
260+
return Obj.template getNative<backend::level_zero>();
261+
}
262+
200263
namespace __SYCL2020_DEPRECATED("use 'ext::oneapi::level_zero' instead")
201264
level_zero {
202265
using namespace ext::oneapi::level_zero;
203266
}
267+
204268
} // namespace sycl
205269
} // __SYCL_INLINE_NAMESPACE(cl)

0 commit comments

Comments
 (0)