Skip to content

[SYCL] SYCL 2020 backend interoperability part 1 #3354

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 8 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
82 changes: 82 additions & 0 deletions sycl/include/CL/sycl/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@

#include <CL/sycl/accessor.hpp>
#include <CL/sycl/backend_types.hpp>
#include <CL/sycl/buffer.hpp>
#include <CL/sycl/context.hpp>
#include <CL/sycl/detail/backend_traits.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/device.hpp>
#include <CL/sycl/event.hpp>
#include <CL/sycl/exception.hpp>
#include <CL/sycl/platform.hpp>
#include <CL/sycl/queue.hpp>

#include <type_traits>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
Expand All @@ -34,5 +44,77 @@ auto get_native(const accessor<DataT, Dimensions, AccessMode, AccessTarget,
AccessTarget, IsPlaceholder>>::type =
delete;

namespace detail {
__SYCL_EXPORT platform make_platform(pi_native_handle NativeHandle,
backend Backend);
__SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
backend Backend);
__SYCL_EXPORT context make_context(pi_native_handle NativeHandle,
const async_handler &Handler,
backend Backend);
__SYCL_EXPORT queue make_queue(pi_native_handle NativeHandle,
const context &TargetContext,
const async_handler &Handler, backend Backend);
__SYCL_EXPORT event make_event(pi_native_handle NativeHandle,
const context &TargetContext, backend Backend);
} // namespace detail

template <backend Backend>
typename std::enable_if<
detail::InteropFeatureSupportMap<Backend>::MakePlatform == true,
platform>::type
make_platform(const typename interop<Backend, platform>::type &BackendObject) {
return detail::make_platform(
detail::pi::cast<pi_native_handle>(BackendObject), Backend);
}

template <backend Backend>
typename std::enable_if<
detail::InteropFeatureSupportMap<Backend>::MakeDevice == true, device>::type
make_device(const typename interop<Backend, device>::type &BackendObject) {
return detail::make_device(detail::pi::cast<pi_native_handle>(BackendObject),
Backend);
}

template <backend Backend>
typename std::enable_if<
detail::InteropFeatureSupportMap<Backend>::MakeContext == true,
context>::type
make_context(const typename interop<Backend, context>::type &BackendObject,
const async_handler &Handler = {}) {
return detail::make_context(detail::pi::cast<pi_native_handle>(BackendObject),
Handler, Backend);
}

template <backend Backend>
typename std::enable_if<
detail::InteropFeatureSupportMap<Backend>::MakeQueue == true, queue>::type
make_queue(const typename interop<Backend, queue>::type &BackendObject,
const context &TargetContext, const async_handler Handler = {}) {
return detail::make_queue(detail::pi::cast<pi_native_handle>(BackendObject),
TargetContext, Handler, Backend);
}

template <backend Backend>
typename std::enable_if<
detail::InteropFeatureSupportMap<Backend>::MakeEvent == true, event>::type
make_event(const typename interop<Backend, event>::type &BackendObject,
const context &TargetContext) {
return detail::make_event(detail::pi::cast<pi_native_handle>(BackendObject),
TargetContext, Backend);
}

template <backend Backend, typename T, int Dimensions = 1,
typename AllocatorT = buffer_allocator>
typename std::enable_if<detail::InteropFeatureSupportMap<Backend>::MakeBuffer ==
true,
buffer<T, Dimensions, AllocatorT>>::type
make_buffer(
const typename interop<Backend, buffer<T, Dimensions, AllocatorT>>::type
&BackendObject,
const context &TargetContext, event AvailableEvent = {}) {
return buffer<T, Dimensions, AllocatorT>(
reinterpret_cast<cl_mem>(BackendObject), TargetContext, AvailableEvent);
}
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
12 changes: 12 additions & 0 deletions sycl/include/CL/sycl/backend/level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ struct interop<backend::level_zero, accessor<DataT, Dimensions, AccessMode,
using type = char *;
};

namespace detail {
template <> struct InteropFeatureSupportMap<backend::level_zero> {
static constexpr bool MakePlatform = true;
static constexpr bool MakeDevice = false;
static constexpr bool MakeContext = false;
static constexpr bool MakeQueue = false;
static constexpr bool MakeEvent = false;
static constexpr bool MakeBuffer = false;
static constexpr bool MakeKernel = false;
};
} // namespace detail

namespace level_zero {

// Since Level-Zero is not doing any reference counting itself, we have to
Expand Down
18 changes: 18 additions & 0 deletions sycl/include/CL/sycl/backend/opencl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <CL/sycl/accessor.hpp>
#include <CL/sycl/backend_types.hpp>
#include <CL/sycl/detail/backend_traits.hpp>
#include <CL/sycl/detail/cl.h>

__SYCL_INLINE_NAMESPACE(cl) {
Expand Down Expand Up @@ -52,6 +53,23 @@ struct interop<backend::opencl, accessor<DataT, Dimensions, AccessMode,
using type = cl_mem;
};

template <typename DataT, int Dimensions, typename AllocatorT>
struct interop<backend::opencl, buffer<DataT, Dimensions, AllocatorT>> {
using type = cl_mem;
};

namespace detail {
template <> struct InteropFeatureSupportMap<backend::opencl> {
static constexpr bool MakePlatform = true;
static constexpr bool MakeDevice = true;
static constexpr bool MakeContext = true;
static constexpr bool MakeQueue = true;
static constexpr bool MakeEvent = true;
static constexpr bool MakeBuffer = true;
static constexpr bool MakeKernel = true;
};
} // namespace detail

namespace opencl {

// Implementation of various "make" functions resides in SYCL RT because
Expand Down
25 changes: 25 additions & 0 deletions sycl/include/CL/sycl/detail/backend_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//==-------------- backend_traits.hpp - SYCL backend traits ----------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#pragma once

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {
template <backend Backend> struct InteropFeatureSupportMap {
static constexpr bool MakePlatform = false;
static constexpr bool MakeDevice = false;
static constexpr bool MakeContext = false;
static constexpr bool MakeQueue = false;
static constexpr bool MakeEvent = false;
static constexpr bool MakeBuffer = false;
static constexpr bool MakeKernel = false;
};
} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
1 change: 1 addition & 0 deletions sycl/include/CL/sycl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#pragma once
#include <CL/sycl/aspects.hpp>
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/export.hpp>
#include <CL/sycl/stl.hpp>
Expand Down
1 change: 1 addition & 0 deletions sycl/source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ set(SYCL_SOURCES
"${sycl_inc_dir}/CL/sycl.hpp"
"backend/opencl.cpp"
"backend/level_zero.cpp"
"backend.cpp"
"detail/accessor_impl.cpp"
"detail/buffer_impl.cpp"
"detail/builtins_common.cpp"
Expand Down
99 changes: 99 additions & 0 deletions sycl/source/backend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//==------------------- backend.cpp ----------------------------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "detail/context_impl.hpp"
#include "detail/event_impl.hpp"
#include "detail/platform_impl.hpp"
#include "detail/plugin.hpp"
#include "detail/queue_impl.hpp"
#include <CL/sycl/backend.hpp>
#include <CL/sycl/detail/export.hpp>
#include <CL/sycl/detail/pi.h>
#include <CL/sycl/exception_list.hpp>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {

static const plugin &getPlugin(backend Backend) {
switch (Backend) {
case backend::opencl:
return pi::getPlugin<backend::opencl>();
case backend::level_zero:
return pi::getPlugin<backend::level_zero>();
default:
throw sycl::runtime_error{"Unsupported backend", PI_INVALID_OPERATION};
}
}

platform make_platform(pi_native_handle NativeHandle, backend Backend) {
const auto &Plugin = getPlugin(Backend);

// Create PI platform first.
pi::PiPlatform PiPlatform = nullptr;
Plugin.call<PiApiKind::piextPlatformCreateWithNativeHandle>(NativeHandle,
&PiPlatform);

return detail::createSyclObjFromImpl<platform>(
platform_impl::getOrMakePlatformImpl(PiPlatform, Plugin));
}

__SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
backend Backend) {
const auto &Plugin = getPlugin(Backend);

pi::PiDevice PiDevice = nullptr;
Plugin.call<PiApiKind::piextDeviceCreateWithNativeHandle>(NativeHandle,
nullptr, &PiDevice);
// Construct the SYCL device from PI device.
return detail::createSyclObjFromImpl<device>(
std::make_shared<device_impl>(PiDevice, Plugin));
}

__SYCL_EXPORT context make_context(pi_native_handle NativeHandle,
const async_handler &Handler,
backend Backend) {
const auto &Plugin = getPlugin(Backend);

pi::PiContext PiContext = nullptr;
Plugin.call<PiApiKind::piextContextCreateWithNativeHandle>(
NativeHandle, 0, nullptr, false, &PiContext);
// Construct the SYCL context from PI context.
return detail::createSyclObjFromImpl<context>(
std::make_shared<context_impl>(PiContext, Handler, Plugin));
}

__SYCL_EXPORT queue make_queue(pi_native_handle NativeHandle,
const context &Context,
const async_handler &Handler, backend Backend) {
const auto &Plugin = getPlugin(Backend);
const auto &ContextImpl = getSyclObjImpl(Context);
// Create PI queue first.
pi::PiQueue PiQueue = nullptr;
Plugin.call<PiApiKind::piextQueueCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), &PiQueue);
// Construct the SYCL queue from PI queue.
return detail::createSyclObjFromImpl<queue>(
std::make_shared<queue_impl>(PiQueue, ContextImpl, Handler));
}

__SYCL_EXPORT event make_event(pi_native_handle NativeHandle,
const context &Context, backend Backend) {
const auto &Plugin = getPlugin(Backend);

pi::PiEvent PiEvent = nullptr;
Plugin.call<PiApiKind::piextEventCreateWithNativeHandle>(NativeHandle,
&PiEvent);

return detail::createSyclObjFromImpl<event>(
std::make_shared<event_impl>(PiEvent, Context));
}

} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
21 changes: 5 additions & 16 deletions sycl/source/backend/level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include <CL/sycl.hpp>
#include <CL/sycl/backend.hpp>
#include <detail/platform_impl.hpp>
#include <detail/plugin.hpp>
#include <detail/program_impl.hpp>
Expand All @@ -20,14 +21,7 @@ using namespace detail;
//----------------------------------------------------------------------------
// Implementation of level_zero::make<platform>
__SYCL_EXPORT platform make_platform(pi_native_handle NativeHandle) {
const auto &Plugin = pi::getPlugin<backend::level_zero>();
// Create PI platform first.
pi::PiPlatform PiPlatform;
Plugin.call<PiApiKind::piextPlatformCreateWithNativeHandle>(NativeHandle,
&PiPlatform);

return detail::createSyclObjFromImpl<platform>(
platform_impl::getOrMakePlatformImpl(PiPlatform, Plugin));
return detail::make_platform(NativeHandle, backend::level_zero);
}

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -86,15 +80,10 @@ __SYCL_EXPORT program make_program(const context &Context,
// Implementation of level_zero::make<queue>
__SYCL_EXPORT queue make_queue(const context &Context,
pi_native_handle NativeHandle) {
const auto &Plugin = pi::getPlugin<backend::level_zero>();
const auto &ContextImpl = getSyclObjImpl(Context);
// Create PI queue first.
pi::PiQueue PiQueue;
Plugin.call<PiApiKind::piextQueueCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), &PiQueue);
// Construct the SYCL queue from PI queue.
return detail::createSyclObjFromImpl<queue>(std::make_shared<queue_impl>(
PiQueue, ContextImpl, ContextImpl->get_async_handler()));
return detail::make_queue(NativeHandle, Context,
ContextImpl->get_async_handler(),
backend::level_zero);
}

} // namespace level_zero
Expand Down
38 changes: 5 additions & 33 deletions sycl/source/backend/opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,19 @@ using namespace detail;
//----------------------------------------------------------------------------
// Implementation of opencl::make<platform>
__SYCL_EXPORT platform make_platform(pi_native_handle NativeHandle) {
const auto &Plugin = pi::getPlugin<backend::opencl>();
// Create PI platform first.
pi::PiPlatform PiPlatform;
Plugin.call<PiApiKind::piextPlatformCreateWithNativeHandle>(NativeHandle,
&PiPlatform);

// Construct the SYCL platform from PI platfrom.
return detail::createSyclObjFromImpl<platform>(
std::make_shared<platform_impl>(PiPlatform, Plugin));
return detail::make_platform(NativeHandle, backend::opencl);
}

//----------------------------------------------------------------------------
// Implementation of opencl::make<device>
__SYCL_EXPORT device make_device(pi_native_handle NativeHandle) {
const auto &Plugin = pi::getPlugin<backend::opencl>();
// Create PI device first.
pi::PiDevice PiDevice;
Plugin.call<PiApiKind::piextDeviceCreateWithNativeHandle>(NativeHandle,
nullptr, &PiDevice);
// Construct the SYCL device from PI device.
return detail::createSyclObjFromImpl<device>(
std::make_shared<device_impl>(PiDevice, Plugin));
return detail::make_device(NativeHandle, backend::opencl);
}

//----------------------------------------------------------------------------
// Implementation of opencl::make<context>
__SYCL_EXPORT context make_context(pi_native_handle NativeHandle) {
const auto &Plugin = pi::getPlugin<backend::opencl>();
// Create PI context first.
pi::PiContext PiContext;
Plugin.call<PiApiKind::piextContextCreateWithNativeHandle>(
NativeHandle, 0, nullptr, false, &PiContext);
// Construct the SYCL context from PI context.
return detail::createSyclObjFromImpl<context>(
std::make_shared<context_impl>(PiContext, async_handler{}, Plugin));
return detail::make_context(NativeHandle, async_handler{}, backend::opencl);
}

//----------------------------------------------------------------------------
Expand All @@ -72,15 +50,9 @@ __SYCL_EXPORT program make_program(const context &Context,
// Implementation of opencl::make<queue>
__SYCL_EXPORT queue make_queue(const context &Context,
pi_native_handle NativeHandle) {
const auto &Plugin = pi::getPlugin<backend::opencl>();
const auto &ContextImpl = getSyclObjImpl(Context);
// Create PI queue first.
pi::PiQueue PiQueue;
Plugin.call<PiApiKind::piextQueueCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), &PiQueue);
// Construct the SYCL queue from PI queue.
return detail::createSyclObjFromImpl<queue>(std::make_shared<queue_impl>(
PiQueue, ContextImpl, ContextImpl->get_async_handler()));
return detail::make_queue(NativeHandle, Context,
ContextImpl->get_async_handler(), backend::opencl);
}

} // namespace opencl
Expand Down
Loading