Skip to content

[SYCL] Make Level-Zero interop API SYCL-2020 compliant for queue, event, and kernel_bundle(was program). #4512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions sycl/include/CL/sycl/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ using backend_return_t =
typename backend_traits<Backend>::template return_type<SyclType>;

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

template <backend Backend>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_queue free function")
typename std::enable_if<
detail::InteropFeatureSupportMap<Backend>::MakeQueue == true, queue>::type
make_queue(const typename backend_traits<Backend>::template input_type<queue>
&BackendObject,
const context &TargetContext, bool KeepOwnership,
const async_handler Handler = {}) {
make_queue(
const typename backend_traits<Backend>::template input_type<queue>
&BackendObject,
const context &TargetContext, bool KeepOwnership,
const async_handler Handler = {}) {
return detail::make_queue(detail::pi::cast<pi_native_handle>(BackendObject),
TargetContext, KeepOwnership, Handler, Backend);
}

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

template <backend Backend>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_event free function")
typename std::enable_if<
detail::InteropFeatureSupportMap<Backend>::MakeEvent == true, event>::type
make_event(const typename backend_traits<Backend>::template input_type<event>
&BackendObject,
const context &TargetContext, bool KeepOwnership) {
make_event(
const typename backend_traits<Backend>::template input_type<event>
&BackendObject,
const context &TargetContext, bool KeepOwnership) {
return detail::make_event(detail::pi::cast<pi_native_handle>(BackendObject),
TargetContext, KeepOwnership, Backend);
}
Expand Down
1 change: 1 addition & 0 deletions sycl/include/CL/sycl/backend/opencl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct BackendInput<backend::opencl, kernel_bundle<State>> {

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

Expand Down
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class __SYCL_EXPORT context {
///
/// \return a native handle, the type of which defined by the backend.
template <backend BackendName>
__SYCL_DEPRECATED("Use SYCL-2020 sycl::get_native free function")
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
auto get_native() const -> typename interop<BackendName, context>::type {
return reinterpret_cast<typename interop<BackendName, context>::type>(
getNative());
Expand Down
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class __SYCL_EXPORT device {
///
/// \return a native handle, the type of which defined by the backend.
template <backend BackendName>
__SYCL_DEPRECATED("Use SYCL-2020 sycl::get_native free function")
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
auto get_native() const -> typename interop<BackendName, device>::type {
return (typename interop<BackendName, device>::type)getNative();
}
Expand Down
2 changes: 2 additions & 0 deletions sycl/include/CL/sycl/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ class __SYCL_EXPORT event {
///
/// \return a native handle, the type of which defined by the backend.
template <backend BackendName>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
auto get_native() const -> typename interop<BackendName, event>::type {
return reinterpret_cast<typename interop<BackendName, event>::type>(
getNative());
}

private:
event(std::shared_ptr<detail::event_impl> EventImpl);

Expand Down
6 changes: 3 additions & 3 deletions sycl/include/CL/sycl/interop_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class interop_handle {
template <backend Backend = backend::opencl>
backend_return_t<Backend, queue> get_native_queue() const {
#ifndef __SYCL_DEVICE_ONLY__
// TODO: replace the exception thrown below with the SYCL-2020 exception
// TODO: replace the exception thrown below with the SYCL 2020 exception
// with the error code 'errc::backend_mismatch' when those new exceptions
// are ready to be used.
if (Backend != get_backend())
Expand All @@ -107,7 +107,7 @@ class interop_handle {
template <backend Backend = backend::opencl>
backend_return_t<Backend, device> get_native_device() const {
#ifndef __SYCL_DEVICE_ONLY__
// TODO: replace the exception thrown below with the SYCL-2020 exception
// TODO: replace the exception thrown below with the SYCL 2020 exception
// with the error code 'errc::backend_mismatch' when those new exceptions
// are ready to be used.
if (Backend != get_backend())
Expand All @@ -129,7 +129,7 @@ class interop_handle {
template <backend Backend = backend::opencl>
backend_return_t<Backend, context> get_native_context() const {
#ifndef __SYCL_DEVICE_ONLY__
// TODO: replace the exception thrown below with the SYCL-2020 exception
// TODO: replace the exception thrown below with the SYCL 2020 exception
// with the error code 'errc::backend_mismatch' when those new exceptions
// are ready to be used.
if (Backend != get_backend())
Expand Down
31 changes: 26 additions & 5 deletions sycl/include/CL/sycl/kernel_bundle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ __SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
// Forward declaration
template <backend Backend> class backend_traits;
template <backend Backend, class SyclT>
auto get_native(const SyclT &Obj) -> backend_return_t<Backend, SyclT>;

namespace detail {
class kernel_id_impl;
Expand Down Expand Up @@ -176,8 +178,8 @@ class __SYCL_EXPORT kernel_bundle_plain {
void set_specialization_constant_impl(const char *SpecName, void *Value,
size_t Size) noexcept;

void get_specialization_constant_impl(const char *SpecName, void *Value) const
noexcept;
void get_specialization_constant_impl(const char *SpecName,
void *Value) const noexcept;

bool is_specialization_constant_set(const char *SpecName) const noexcept;

Expand Down Expand Up @@ -308,9 +310,9 @@ class kernel_bundle : public detail::kernel_bundle_plain {
}

template <backend Backend>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
std::vector<typename backend_traits<Backend>::template return_type<
kernel_bundle<State>>>
get_native() {
kernel_bundle<State>>> get_native() {
std::vector<typename backend_traits<Backend>::template return_type<
kernel_bundle<State>>>
ReturnValue;
Expand All @@ -335,6 +337,25 @@ class kernel_bundle : public detail::kernel_bundle_plain {

template <class T>
friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);

template <backend Backend, class SyclT>
friend auto get_native(const SyclT &Obj) -> backend_return_t<Backend, SyclT>;

template <backend Backend>
backend_return_t<Backend, kernel_bundle<State>> getNative() const {
// NOTE: implementation assumes that the return type is a
// derivative of std::vector.
backend_return_t<Backend, kernel_bundle<State>> ReturnValue;
ReturnValue.reserve(std::distance(begin(), end()));

for (const device_image<State> &DevImg : *this) {
ReturnValue.push_back(
detail::pi::cast<typename decltype(ReturnValue)::value_type>(
DevImg.getNative()));
}

return ReturnValue;
}
};

/////////////////////////
Expand Down Expand Up @@ -604,7 +625,7 @@ __SYCL_EXPORT std::vector<sycl::device> find_device_intersection(
__SYCL_EXPORT std::shared_ptr<detail::kernel_bundle_impl>
link_impl(const std::vector<kernel_bundle<bundle_state::object>> &ObjectBundles,
const std::vector<device> &Devs, const property_list &PropList);
}
} // namespace detail

/// \returns a new kernel_bundle which contains the device images from the
/// ObjectBundles that are translated into one or more new device images of
Expand Down
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class __SYCL_EXPORT platform {
///
/// \return a native handle, the type of which defined by the backend.
template <backend BackendName>
__SYCL_DEPRECATED("Use SYCL-2020 sycl::get_native free function")
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
auto get_native() const -> typename interop<BackendName, platform>::type {
return reinterpret_cast<typename interop<BackendName, platform>::type>(
getNative());
Expand Down
1 change: 1 addition & 0 deletions sycl/include/CL/sycl/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class __SYCL_EXPORT __SYCL2020_DEPRECATED(
///
/// \return a native handle, the type of which defined by the backend.
template <backend BackendName>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
auto get_native() const -> typename interop<BackendName, program>::type {
return reinterpret_cast<typename interop<BackendName, program>::type>(
getNative());
Expand Down
3 changes: 2 additions & 1 deletion sycl/include/CL/sycl/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AssertInfoCopier;
static event submitAssertCapture(queue &, event &, queue *,
const detail::code_location &);
#endif
}
} // namespace detail

/// Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
///
Expand Down Expand Up @@ -1020,6 +1020,7 @@ class __SYCL_EXPORT queue {
///
/// \return a native handle, the type of which defined by the backend.
template <backend BackendName>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
auto get_native() const -> typename interop<BackendName, queue>::type {
return reinterpret_cast<typename interop<BackendName, queue>::type>(
getNative());
Expand Down
75 changes: 67 additions & 8 deletions sycl/include/sycl/ext/oneapi/backend/level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ template <> struct BackendInput<backend::level_zero, context> {
};
};

template <> struct BackendInput<backend::level_zero, queue> {
using type = struct {
interop<backend::level_zero, queue>::type NativeHandle;
ext::oneapi::level_zero::ownership Ownership;
};
};

template <> struct BackendInput<backend::level_zero, event> {
using type = struct {
interop<backend::level_zero, event>::type NativeHandle;
ext::oneapi::level_zero::ownership Ownership;
};
};

template <bundle_state State>
struct BackendInput<backend::level_zero, kernel_bundle<State>> {
using type = ze_module_handle_t;
};

template <bundle_state State>
struct BackendReturn<backend::level_zero, kernel_bundle<State>> {
using type = std::vector<ze_module_handle_t>;
};

template <> struct BackendReturn<backend::level_zero, kernel> {
using type = ze_kernel_handle_t;
};
Expand All @@ -90,11 +114,11 @@ template <> struct InteropFeatureSupportMap<backend::level_zero> {
static constexpr bool MakePlatform = true;
static constexpr bool MakeDevice = true;
static constexpr bool MakeContext = true;
static constexpr bool MakeQueue = false;
static constexpr bool MakeQueue = true;
static constexpr bool MakeEvent = true;
static constexpr bool MakeKernelBundle = true;
static constexpr bool MakeBuffer = false;
static constexpr bool MakeKernel = false;
static constexpr bool MakeKernelBundle = false;
};
} // namespace detail

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

// Construction of SYCL device.
template <typename T, typename detail::enable_if_t<
std::is_same<T, device>::value> * = nullptr>
__SYCL_DEPRECATED("Use SYCL-2020 sycl::make_device free function")
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_device free function")
T make(const platform &Platform,
typename interop<backend::level_zero, T>::type Interop) {
return make_device(Platform, reinterpret_cast<pi_native_handle>(Interop));
Expand All @@ -147,7 +171,7 @@ T make(const platform &Platform,
///
template <typename T, typename std::enable_if<
std::is_same<T, context>::value>::type * = nullptr>
__SYCL_DEPRECATED("Use SYCL-2020 sycl::make_context free function")
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_context free function")
T make(const std::vector<device> &DeviceList,
typename interop<backend::level_zero, T>::type Interop,
ownership Ownership = ownership::transfer) {
Expand All @@ -158,6 +182,7 @@ T make(const std::vector<device> &DeviceList,
// Construction of SYCL program.
template <typename T, typename detail::enable_if_t<
std::is_same<T, program>::value> * = nullptr>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_kernel_bundle free function")
T make(const context &Context,
typename interop<backend::level_zero, T>::type Interop) {
return make_program(Context, reinterpret_cast<pi_native_handle>(Interop));
Expand All @@ -166,6 +191,7 @@ T make(const context &Context,
// Construction of SYCL queue.
template <typename T, typename detail::enable_if_t<
std::is_same<T, queue>::value> * = nullptr>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_queue free function")
T make(const context &Context,
typename interop<backend::level_zero, T>::type Interop,
ownership Ownership = ownership::transfer) {
Expand All @@ -176,6 +202,7 @@ T make(const context &Context,
// Construction of SYCL event.
template <typename T, typename detail::enable_if_t<
std::is_same<T, event>::value> * = nullptr>
__SYCL_DEPRECATED("Use SYCL 2020 sycl::make_event free function")
T make(const context &Context,
typename interop<backend::level_zero, T>::type Interop,
ownership Ownership = ownership::transfer) {
Expand All @@ -197,9 +224,41 @@ context make_context<backend::level_zero>(
BackendObject.Ownership == ext::oneapi::level_zero::ownership::keep);
}

namespace __SYCL2020_DEPRECATED("use 'ext::oneapi::level_zero' instead")
level_zero {
using namespace ext::oneapi::level_zero;
// Specialization of sycl::make_queue for Level-Zero backend.
template <>
queue make_queue<backend::level_zero>(
const backend_input_t<backend::level_zero, queue> &BackendObject,
const context &TargetContext, const async_handler Handler) {
return ext::oneapi::level_zero::make_queue(
TargetContext,
detail::pi::cast<pi_native_handle>(BackendObject.NativeHandle),
BackendObject.Ownership == ext::oneapi::level_zero::ownership::keep);
}

// Specialization of sycl::make_event for Level-Zero backend.
template <>
event make_event<backend::level_zero>(
const backend_input_t<backend::level_zero, event> &BackendObject,
const context &TargetContext) {
return ext::oneapi::level_zero::make_event(
TargetContext,
detail::pi::cast<pi_native_handle>(BackendObject.NativeHandle),
BackendObject.Ownership == ext::oneapi::level_zero::ownership::keep);
}

// TODO: remove this specialization when generic is changed to call
// .GetNative() instead of .get_native() member of kernel_bundle.
template <>
auto get_native<backend::level_zero>(
const kernel_bundle<bundle_state::executable> &Obj)
-> backend_return_t<backend::level_zero,
kernel_bundle<bundle_state::executable>> {
// TODO use SYCL 2020 exception when implemented
if (Obj.get_backend() != backend::level_zero)
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);

return Obj.template getNative<backend::level_zero>();
}

} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
Loading