Skip to content

Commit e6733e4

Browse files
author
Alexander Batashev
authored
[SYCL] Implement SYCL 2020 interoperability API part 3 (#3508)
Add support for sycl::kernel and sycl::kernel_bundle construction
1 parent 6fc78b6 commit e6733e4

26 files changed

+481
-6
lines changed

sycl/include/CL/sycl/backend.hpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
#include <CL/sycl/buffer.hpp>
1414
#include <CL/sycl/context.hpp>
1515
#include <CL/sycl/detail/backend_traits.hpp>
16+
#include <CL/sycl/detail/common.hpp>
17+
#include <CL/sycl/detail/export.hpp>
18+
#include <CL/sycl/detail/pi.h>
1619
#include <CL/sycl/detail/pi.hpp>
1720
#include <CL/sycl/device.hpp>
1821
#include <CL/sycl/event.hpp>
1922
#include <CL/sycl/exception.hpp>
23+
#include <CL/sycl/kernel_bundle.hpp>
2024
#include <CL/sycl/platform.hpp>
2125
#include <CL/sycl/queue.hpp>
2226

@@ -25,11 +29,25 @@
2529
__SYCL_INLINE_NAMESPACE(cl) {
2630
namespace sycl {
2731

32+
namespace detail {
33+
template <backend Backend, typename T> struct BackendInput {
34+
// TODO replace usage of interop with specializations.
35+
using type = typename interop<Backend, T>::type;
36+
};
37+
38+
template <backend Backend, typename T> struct BackendReturn {
39+
// TODO replace usage of interop with specializations.
40+
using type = typename interop<Backend, T>::type;
41+
};
42+
} // namespace detail
43+
2844
template <backend Backend> class backend_traits {
2945
public:
30-
template <class T> using input_type = typename interop<Backend, T>::type;
46+
template <class T>
47+
using input_type = typename detail::BackendInput<Backend, T>::type;
3148

32-
template <class T> using return_type = typename interop<Backend, T>::type;
49+
template <class T>
50+
using return_type = typename detail::BackendReturn<Backend, T>::type;
3351

3452
// TODO define errc once SYCL2020-style exceptions are supported.
3553
};
@@ -54,6 +72,9 @@ auto get_native(const accessor<DataT, Dimensions, AccessMode, AccessTarget,
5472
delete;
5573

5674
namespace detail {
75+
// Forward declaration
76+
class kernel_bundle_impl;
77+
5778
__SYCL_EXPORT platform make_platform(pi_native_handle NativeHandle,
5879
backend Backend);
5980
__SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
@@ -66,6 +87,11 @@ __SYCL_EXPORT queue make_queue(pi_native_handle NativeHandle,
6687
const async_handler &Handler, backend Backend);
6788
__SYCL_EXPORT event make_event(pi_native_handle NativeHandle,
6889
const context &TargetContext, backend Backend);
90+
__SYCL_EXPORT kernel make_kernel(pi_native_handle NativeHandle,
91+
const context &TargetContext, backend Backend);
92+
__SYCL_EXPORT std::shared_ptr<detail::kernel_bundle_impl>
93+
make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
94+
bundle_state State, backend Backend);
6995
} // namespace detail
7096

7197
template <backend Backend>
@@ -131,5 +157,28 @@ make_buffer(const typename backend_traits<Backend>::template input_type<
131157
return buffer<T, Dimensions, AllocatorT>(
132158
reinterpret_cast<cl_mem>(BackendObject), TargetContext, AvailableEvent);
133159
}
160+
161+
template <backend Backend>
162+
kernel
163+
make_kernel(const typename backend_traits<Backend>::template input_type<kernel>
164+
&BackendObject,
165+
const context &TargetContext) {
166+
return detail::make_kernel(detail::pi::cast<pi_native_handle>(BackendObject),
167+
TargetContext, Backend);
168+
}
169+
170+
template <backend Backend, bundle_state State>
171+
typename std::enable_if<
172+
detail::InteropFeatureSupportMap<Backend>::MakeKernelBundle == true,
173+
kernel_bundle<State>>::type
174+
make_kernel_bundle(const typename backend_traits<Backend>::template input_type<
175+
kernel_bundle<State>> &BackendObject,
176+
const context &TargetContext) {
177+
std::shared_ptr<detail::kernel_bundle_impl> KBImpl =
178+
detail::make_kernel_bundle(
179+
detail::pi::cast<pi_native_handle>(BackendObject), TargetContext,
180+
State, Backend);
181+
return detail::createSyclObjFromImpl<kernel_bundle<State>>(KBImpl);
182+
}
134183
} // namespace sycl
135184
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/include/CL/sycl/backend/level_zero.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ struct interop<backend::level_zero, accessor<DataT, Dimensions, AccessMode,
5050
};
5151

5252
namespace detail {
53+
template <> class BackendReturn<backend::level_zero, kernel> {
54+
using type = ze_kernel_handle_t;
55+
};
56+
5357
template <> struct InteropFeatureSupportMap<backend::level_zero> {
5458
static constexpr bool MakePlatform = true;
5559
static constexpr bool MakeDevice = false;
@@ -58,6 +62,7 @@ template <> struct InteropFeatureSupportMap<backend::level_zero> {
5862
static constexpr bool MakeEvent = false;
5963
static constexpr bool MakeBuffer = false;
6064
static constexpr bool MakeKernel = false;
65+
static constexpr bool MakeKernelBundle = false;
6166
};
6267
} // namespace detail
6368

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#pragma once
1111

1212
#include <CL/sycl/accessor.hpp>
13+
#include <CL/sycl/backend.hpp>
1314
#include <CL/sycl/backend_types.hpp>
1415
#include <CL/sycl/detail/backend_traits.hpp>
1516
#include <CL/sycl/detail/cl.h>
17+
#include <CL/sycl/kernel_bundle.hpp>
1618

1719
__SYCL_INLINE_NAMESPACE(cl) {
1820
namespace sycl {
@@ -59,6 +61,24 @@ struct interop<backend::opencl, buffer<DataT, Dimensions, AllocatorT>> {
5961
};
6062

6163
namespace detail {
64+
template <bundle_state State>
65+
struct BackendInput<backend::opencl, kernel_bundle<State>> {
66+
using type = cl_program;
67+
};
68+
69+
template <bundle_state State>
70+
struct BackendReturn<backend::opencl, kernel_bundle<State>> {
71+
using type = cl_program;
72+
};
73+
74+
template <> struct BackendInput<backend::opencl, kernel> {
75+
using type = cl_kernel;
76+
};
77+
78+
template <> struct BackendReturn<backend::opencl, kernel> {
79+
using type = cl_kernel;
80+
};
81+
6282
template <> struct InteropFeatureSupportMap<backend::opencl> {
6383
static constexpr bool MakePlatform = true;
6484
static constexpr bool MakeDevice = true;
@@ -67,6 +87,7 @@ template <> struct InteropFeatureSupportMap<backend::opencl> {
6787
static constexpr bool MakeEvent = true;
6888
static constexpr bool MakeBuffer = true;
6989
static constexpr bool MakeKernel = true;
90+
static constexpr bool MakeKernelBundle = true;
7091
};
7192
} // namespace detail
7293

sycl/include/CL/sycl/detail/pi.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ _PI_API(piKernelRetain)
7979
_PI_API(piKernelRelease)
8080
_PI_API(piextKernelSetArgPointer)
8181
_PI_API(piKernelSetExecInfo)
82+
_PI_API(piextKernelCreateWithNativeHandle)
83+
_PI_API(piextKernelGetNativeHandle)
8284
// Event
8385
_PI_API(piEventCreate)
8486
_PI_API(piEventGetInfo)

sycl/include/CL/sycl/detail/pi.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
// pi_device_binary_property_set PropertySetsEnd;
3636
// 2. A number of types needed to define pi_device_binary_property_set added.
3737
// 3. Added new ownership argument to piextContextCreateWithNativeHandle.
38+
// 4. Add interoperability interfaces for kernel.
3839
//
40+
#include "CL/cl.h"
3941
#define _PI_H_VERSION_MAJOR 3
40-
#define _PI_H_VERSION_MINOR 4
42+
#define _PI_H_VERSION_MINOR 5
4143

4244
#define _PI_STRING_HELPER(a) #a
4345
#define _PI_CONCAT(a, b) _PI_STRING_HELPER(a.b)
@@ -125,7 +127,8 @@ typedef enum {
125127
typedef enum {
126128
PI_PROGRAM_BUILD_INFO_STATUS = CL_PROGRAM_BUILD_STATUS,
127129
PI_PROGRAM_BUILD_INFO_OPTIONS = CL_PROGRAM_BUILD_OPTIONS,
128-
PI_PROGRAM_BUILD_INFO_LOG = CL_PROGRAM_BUILD_LOG
130+
PI_PROGRAM_BUILD_INFO_LOG = CL_PROGRAM_BUILD_LOG,
131+
PI_PROGRAM_BUILD_INFO_BINARY_TYPE = CL_PROGRAM_BINARY_TYPE
129132
} _pi_program_build_info;
130133

131134
typedef enum {
@@ -135,6 +138,14 @@ typedef enum {
135138
PI_PROGRAM_BUILD_STATUS_IN_PROGRESS = CL_BUILD_IN_PROGRESS
136139
} _pi_program_build_status;
137140

141+
typedef enum {
142+
PI_PROGRAM_BINARY_TYPE_NONE = CL_PROGRAM_BINARY_TYPE_NONE,
143+
PI_PROGRAM_BINARY_TYPE_COMPILED_OBJECT =
144+
CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT,
145+
PI_PROGRAM_BINARY_TYPE_LIBRARY = CL_PROGRAM_BINARY_TYPE_LIBRARY,
146+
PI_PROGRAM_BINARY_TYPE_EXECUTABLE = CL_PROGRAM_BINARY_TYPE_EXECUTABLE
147+
} _pi_program_binary_type;
148+
138149
// NOTE: this is made 64-bit to match the size of cl_device_type to
139150
// make the translation to OpenCL transparent.
140151
//
@@ -1236,6 +1247,25 @@ __SYCL_EXPORT pi_result piKernelSetExecInfo(pi_kernel kernel,
12361247
size_t param_value_size,
12371248
const void *param_value);
12381249

1250+
/// Creates PI kernel object from a native handle.
1251+
/// NOTE: The created PI object takes ownership of the native handle.
1252+
///
1253+
/// \param nativeHandle is the native handle to create PI kernel from.
1254+
/// \param context is the PI context of the kernel.
1255+
/// \param ownNativeHandle tells if SYCL RT should assume the ownership of
1256+
/// the native handle, if it can.
1257+
/// \param kernel is the PI kernel created from the native handle.
1258+
__SYCL_EXPORT pi_result piextKernelCreateWithNativeHandle(
1259+
pi_native_handle nativeHandle, pi_context context, bool ownNativeHandle,
1260+
pi_kernel *kernel);
1261+
1262+
/// Gets the native handle of a PI kernel object.
1263+
///
1264+
/// \param kernel is the PI kernel to get the native handle of.
1265+
/// \param nativeHandle is the native handle of kernel.
1266+
__SYCL_EXPORT pi_result
1267+
piextKernelGetNativeHandle(pi_kernel kernel, pi_native_handle *nativeHandle);
1268+
12391269
//
12401270
// Events
12411271
//

sycl/include/CL/sycl/kernel.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <CL/sycl/detail/common.hpp>
1212
#include <CL/sycl/detail/export.hpp>
13+
#include <CL/sycl/detail/pi.h>
1314
#include <CL/sycl/info/info_desc.hpp>
1415
#include <CL/sycl/stl.hpp>
1516

@@ -20,6 +21,8 @@ namespace sycl {
2021
// Forward declaration
2122
class program;
2223
class context;
24+
template <backend Backend> class backend_traits;
25+
2326
namespace detail {
2427
class kernel_impl;
2528

@@ -57,6 +60,8 @@ class __SYCL_EXPORT kernel {
5760
///
5861
/// \param ClKernel is a valid OpenCL cl_kernel instance
5962
/// \param SyclContext is a valid SYCL context
63+
__SYCL2020_DEPRECATED(
64+
"OpenCL interop constructors are deprecated, use make_kernel() instead")
6065
kernel(cl_kernel ClKernel, const context &SyclContext);
6166

6267
kernel(const kernel &RHS) = default;
@@ -78,6 +83,8 @@ class __SYCL_EXPORT kernel {
7883
/// an invalid_object_error exception will be thrown.
7984
///
8085
/// \return a valid cl_kernel instance
86+
__SYCL2020_DEPRECATED(
87+
"OpenCL interop get() functions are deprecated, use get_native() instead")
8188
cl_kernel get() const;
8289

8390
/// Check if the associated SYCL context is a SYCL host context.
@@ -166,10 +173,19 @@ class __SYCL_EXPORT kernel {
166173
param>::input_type Value) const;
167174
// clang-format on
168175

176+
template <backend Backend>
177+
typename backend_traits<Backend>::template return_type<kernel>
178+
get_native() const {
179+
return detail::pi::cast<
180+
backend_traits<Backend>::template return_type<kernel>>(getNativeImpl());
181+
}
182+
169183
private:
170184
/// Constructs a SYCL kernel object from a valid kernel_impl instance.
171185
kernel(std::shared_ptr<detail::kernel_impl> Impl);
172186

187+
pi_native_handle getNativeImpl() const;
188+
173189
shared_ptr_class<detail::kernel_impl> impl;
174190

175191
template <class Obj>

sycl/include/CL/sycl/kernel_bundle.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <CL/sycl/context.hpp>
1212
#include <CL/sycl/detail/common.hpp>
1313
#include <CL/sycl/detail/kernel_desc.hpp>
14+
#include <CL/sycl/detail/pi.h>
15+
#include <CL/sycl/detail/pi.hpp>
1416
#include <CL/sycl/device.hpp>
1517
#include <CL/sycl/kernel.hpp>
1618

@@ -20,6 +22,8 @@
2022

2123
__SYCL_INLINE_NAMESPACE(cl) {
2224
namespace sycl {
25+
// Forward declaration
26+
template <backend Backend> class backend_traits;
2327

2428
enum class bundle_state : char { input = 0, object = 1, executable = 2 };
2529

@@ -81,6 +85,8 @@ class __SYCL_EXPORT device_image_plain {
8185

8286
bool has_kernel(const kernel_id &KernelID, const device &Dev) const noexcept;
8387

88+
pi_native_handle getNative() const;
89+
8490
protected:
8591
detail::DeviceImageImplPtr impl;
8692

@@ -303,6 +309,25 @@ class kernel_bundle : public detail::kernel_bundle_plain {
303309
return reinterpret_cast<device_image_iterator>(kernel_bundle_plain::end());
304310
}
305311

312+
template <backend Backend>
313+
std::vector<typename backend_traits<Backend>::template return_type<
314+
kernel_bundle<State>>>
315+
get_native() {
316+
std::vector<typename backend_traits<Backend>::template return_type<
317+
kernel_bundle<State>>>
318+
ReturnValue;
319+
ReturnValue.reserve(std::distance(begin(), end()));
320+
321+
for (const device_image<State> &DevImg : *this) {
322+
ReturnValue.push_back(
323+
detail::pi::cast<typename backend_traits<
324+
Backend>::template return_type<kernel_bundle<State>>>(
325+
DevImg.getNative()));
326+
}
327+
328+
return ReturnValue;
329+
}
330+
306331
private:
307332
kernel_bundle(detail::KernelBundleImplPtr Impl)
308333
: kernel_bundle_plain(std::move(Impl)) {}

sycl/include/CL/sycl/program.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ enum class program_state { none = 0, compiled = 1, linked = 2 };
3636
/// \sa queue
3737
///
3838
/// \ingroup sycl_api
39-
class __SYCL_EXPORT program {
39+
class __SYCL_EXPORT __SYCL2020_DEPRECATED(
40+
"program class is deprecated, use kernel_bundle instead") program {
4041
public:
4142
program() = delete;
4243

sycl/plugins/cuda/pi_cuda.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,12 @@ pi_result cuda_piEnqueueNativeKernel(
24922492
return {};
24932493
}
24942494

2495+
pi_result cuda_piextKernelCreateWithNativeHandle(pi_native_handle, pi_context,
2496+
pi_kernel *) {
2497+
sycl::detail::pi::die("Unsupported operation");
2498+
return PI_SUCCESS;
2499+
}
2500+
24952501
/// \TODO Not implemented
24962502
pi_result cuda_piMemImageCreate(pi_context context, pi_mem_flags flags,
24972503
const pi_image_format *image_format,
@@ -4711,6 +4717,8 @@ pi_result piPluginInit(pi_plugin *PluginInit) {
47114717
_PI_CL(piKernelRelease, cuda_piKernelRelease)
47124718
_PI_CL(piKernelSetExecInfo, cuda_piKernelSetExecInfo)
47134719
_PI_CL(piextKernelSetArgPointer, cuda_piextKernelSetArgPointer)
4720+
_PI_CL(piextKernelCreateWithNativeHandle,
4721+
cuda_piextKernelCreateWithNativeHandle)
47144722
// Event
47154723
_PI_CL(piEventCreate, cuda_piEventCreate)
47164724
_PI_CL(piEventGetInfo, cuda_piEventGetInfo)

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,6 +3878,22 @@ piEnqueueKernelLaunch(pi_queue Queue, pi_kernel Kernel, pi_uint32 WorkDim,
38783878
return PI_SUCCESS;
38793879
}
38803880

3881+
pi_result piextKernelCreateWithNativeHandle(pi_native_handle, pi_context, bool,
3882+
pi_kernel *) {
3883+
die("Unsupported operation");
3884+
return PI_SUCCESS;
3885+
}
3886+
3887+
pi_result piextKernelGetNativeHandle(pi_kernel Kernel,
3888+
pi_native_handle *NativeHandle) {
3889+
PI_ASSERT(Kernel, PI_INVALID_KERNEL);
3890+
PI_ASSERT(NativeHandle, PI_INVALID_VALUE);
3891+
3892+
auto *ZeKernel = pi_cast<ze_kernel_handle_t *>(NativeHandle);
3893+
*ZeKernel = Kernel->ZeKernel;
3894+
return PI_SUCCESS;
3895+
}
3896+
38813897
//
38823898
// Events
38833899
//

0 commit comments

Comments
 (0)