Skip to content

Commit 36c0b7e

Browse files
Refactoring of program support in dpctl and SyclInterface
Removed uses of `sycl::program` throughout the code. In SyclInterface replace DPCTLSyclProgramRef with DPCTLSyclKernelBundleRef which is reference to `sycl::kernel_bundle<sycl::bundle_state::executable>`. Functions `DPCTLProgram_*` were replaced with `DCPTLKernelBundle_*`. Functions to create program now take both context and device. Tests were modified. dpctl.SyclProgram stays with this name, but it now encapsulates DPCTLSyclKernelBundleRef instead of removed DPCTLSyclProgramRef. OpenCL functions are no longer directly used (instead of used via loader, like in the case of level-zero backend), hence removed linkage to OpenCL library.
1 parent 05d6bfd commit 36c0b7e

12 files changed

+747
-290
lines changed

dpctl/_backend.pxd

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ cdef extern from "syclinterface/dpctl_sycl_types.h":
118118
cdef struct DPCTLOpaqueSyclEvent
119119
cdef struct DPCTLOpaqueSyclKernel
120120
cdef struct DPCTLOpaqueSyclPlatform
121-
cdef struct DPCTLOpaqueSyclProgram
121+
cdef struct DPCTLOpaqueSyclKernelBundle
122122
cdef struct DPCTLOpaqueSyclQueue
123123
cdef struct DPCTLOpaqueSyclUSM
124124

@@ -128,7 +128,7 @@ cdef extern from "syclinterface/dpctl_sycl_types.h":
128128
ctypedef DPCTLOpaqueSyclEvent *DPCTLSyclEventRef
129129
ctypedef DPCTLOpaqueSyclKernel *DPCTLSyclKernelRef
130130
ctypedef DPCTLOpaqueSyclPlatform *DPCTLSyclPlatformRef
131-
ctypedef DPCTLOpaqueSyclProgram *DPCTLSyclProgramRef
131+
ctypedef DPCTLOpaqueSyclKernelBundle *DPCTLSyclKernelBundleRef
132132
ctypedef DPCTLOpaqueSyclQueue *DPCTLSyclQueueRef
133133
ctypedef DPCTLOpaqueSyclUSM *DPCTLSyclUSMRef
134134

@@ -303,21 +303,23 @@ cdef extern from "syclinterface/dpctl_sycl_context_interface.h":
303303

304304

305305
cdef extern from "syclinterface/dpctl_sycl_program_interface.h":
306-
cdef DPCTLSyclProgramRef DPCTLProgram_CreateFromSpirv(
306+
cdef DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSpirv(
307307
const DPCTLSyclContextRef Ctx,
308+
const DPCTLSyclDeviceRef Dev,
308309
const void *IL,
309310
size_t Length,
310311
const char *CompileOpts)
311-
cdef DPCTLSyclProgramRef DPCTLProgram_CreateFromOCLSource(
312+
cdef DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromOCLSource(
312313
const DPCTLSyclContextRef Ctx,
314+
const DPCTLSyclDeviceRef Dev,
313315
const char *Source,
314316
const char *CompileOpts)
315-
cdef DPCTLSyclKernelRef DPCTLProgram_GetKernel(
316-
DPCTLSyclProgramRef PRef,
317+
cdef DPCTLSyclKernelRef DPCTLKernelBundle_GetKernel(
318+
DPCTLSyclKernelBundleRef KBRef,
317319
const char *KernelName)
318-
cdef bool DPCTLProgram_HasKernel(DPCTLSyclProgramRef PRef,
320+
cdef bool DPCTLKernelBundle_HasKernel(DPCTLSyclKernelBundleRef KBRef,
319321
const char *KernelName)
320-
cdef void DPCTLProgram_Delete(DPCTLSyclProgramRef PRef)
322+
cdef void DPCTLKernelBundle_Delete(DPCTLSyclKernelBundleRef KBRef)
321323

322324

323325
cdef extern from "syclinterface/dpctl_sycl_queue_interface.h":

dpctl/program/_program.pxd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"""
2323

2424

25-
from .._backend cimport DPCTLSyclKernelRef, DPCTLSyclProgramRef
25+
from .._backend cimport DPCTLSyclKernelBundleRef, DPCTLSyclKernelRef
2626
from .._sycl_context cimport SyclContext
2727
from .._sycl_device cimport SyclDevice
2828
from .._sycl_queue cimport SyclQueue
@@ -41,18 +41,18 @@ cdef class SyclKernel:
4141

4242

4343
cdef class SyclProgram:
44-
''' Wraps a sycl::program object created from an OpenCL interoperability
45-
program.
44+
''' Wraps a sycl::kernel_bundle<sycl::bundle_state::executable> object created from
45+
using SYCL interoperability layer for OpenCL and Level-Zero backends.
4646
4747
SyclProgram exposes the C API from dpctl_sycl_program_interface.h. A
4848
SyclProgram can be created from either a source string or a SPIR-V
4949
binary file.
5050
'''
51-
cdef DPCTLSyclProgramRef _program_ref
51+
cdef DPCTLSyclKernelBundleRef _program_ref
5252

5353
@staticmethod
54-
cdef SyclProgram _create (DPCTLSyclProgramRef pref)
55-
cdef DPCTLSyclProgramRef get_program_ref (self)
54+
cdef SyclProgram _create (DPCTLSyclKernelBundleRef pref)
55+
cdef DPCTLSyclKernelBundleRef get_program_ref (self)
5656
cpdef SyclKernel get_sycl_kernel(self, str kernel_name)
5757

5858

dpctl/program/_program.pyx

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ from dpctl._backend cimport ( # noqa: E211, E402
3131
DPCTLCString_Delete,
3232
DPCTLKernel_Delete,
3333
DPCTLKernel_GetNumArgs,
34-
DPCTLProgram_CreateFromOCLSource,
35-
DPCTLProgram_CreateFromSpirv,
36-
DPCTLProgram_Delete,
37-
DPCTLProgram_GetKernel,
38-
DPCTLProgram_HasKernel,
34+
DPCTLKernelBundle_CreateFromOCLSource,
35+
DPCTLKernelBundle_CreateFromSpirv,
36+
DPCTLKernelBundle_Delete,
37+
DPCTLKernelBundle_GetKernel,
38+
DPCTLKernelBundle_HasKernel,
3939
DPCTLSyclContextRef,
40+
DPCTLSyclDeviceRef,
41+
DPCTLSyclKernelBundleRef,
4042
DPCTLSyclKernelRef,
41-
DPCTLSyclProgramRef,
4243
)
4344

4445
__all__ = [
@@ -50,8 +51,8 @@ __all__ = [
5051
]
5152

5253
cdef class SyclProgramCompilationError(Exception):
53-
"""This exception is raised when a ``sycl::program`` could not be built from
54-
either a SPIR-V binary file or a string source.
54+
"""This exception is raised when a ``sycl::kernel_bundle`` could not be
55+
built from either a SPIR-V binary file or a string source.
5556
"""
5657
pass
5758

@@ -105,33 +106,35 @@ cdef class SyclProgram:
105106
"""
106107

107108
@staticmethod
108-
cdef SyclProgram _create(DPCTLSyclProgramRef pref):
109+
cdef SyclProgram _create(DPCTLSyclKernelBundleRef KBRef):
109110
cdef SyclProgram ret = SyclProgram.__new__(SyclProgram)
110-
ret._program_ref = pref
111+
ret._program_ref = KBRef
111112
return ret
112113

113114
def __dealloc__(self):
114-
DPCTLProgram_Delete(self._program_ref)
115+
DPCTLKernelBundle_Delete(self._program_ref)
115116

116-
cdef DPCTLSyclProgramRef get_program_ref(self):
117+
cdef DPCTLSyclKernelBundleRef get_program_ref(self):
117118
return self._program_ref
118119

119120
cpdef SyclKernel get_sycl_kernel(self, str kernel_name):
120121
name = kernel_name.encode('utf8')
121-
return SyclKernel._create(DPCTLProgram_GetKernel(self._program_ref,
122-
name), kernel_name)
122+
return SyclKernel._create(
123+
DPCTLKernelBundle_GetKernel(self._program_ref, name),
124+
kernel_name
125+
)
123126

124127
def has_sycl_kernel(self, str kernel_name):
125128
name = kernel_name.encode('utf8')
126-
return DPCTLProgram_HasKernel(self._program_ref, name)
129+
return DPCTLKernelBundle_HasKernel(self._program_ref, name)
127130

128131
def addressof_ref(self):
129-
"""Returns the address of the C API DPCTLSyclProgramRef pointer
132+
"""Returns the address of the C API DPCTLSyclKernelBundleRef pointer
130133
as a long.
131134
132135
Returns:
133-
The address of the ``DPCTLSyclProgramRef`` pointer used to create
134-
this :class:`dpctl.SyclProgram` object cast to a ``size_t``.
136+
The address of the ``DPCTLSyclKernelBundleRef`` pointer used to
137+
create this :class:`dpctl.SyclProgram` object cast to a ``size_t``.
135138
"""
136139
return int(<size_t>self._program_ref)
137140

@@ -140,9 +143,10 @@ cpdef create_program_from_source(SyclQueue q, unicode src, unicode copts=""):
140143
"""
141144
Creates a Sycl interoperability program from an OpenCL source string.
142145
143-
We use the ``DPCTLProgram_CreateFromOCLSource()`` C API function to
144-
create a ``sycl::program`` from an OpenCL source program that can
145-
contain multiple kernels. Note currently only supported for OpenCL.
146+
We use the ``DPCTLKernelBundle_CreateFromOCLSource()`` C API function
147+
to create a ``sycl::kernel_bundle<sycl::bundle_state::executable>``
148+
from an OpenCL source program that can contain multiple kernels.
149+
Note: This function is currently only supported for the OpenCL backend.
146150
147151
Parameters:
148152
q (SyclQueue) : The :class:`SyclQueue` for which the
@@ -153,33 +157,37 @@ cpdef create_program_from_source(SyclQueue q, unicode src, unicode copts=""):
153157
154158
Returns:
155159
program (SyclProgram): A :class:`SyclProgram` object wrapping the
156-
``sycl::program`` returned by the C API.
160+
``sycl::kernel_bundle<sycl::bundle_state::executable>`` returned
161+
by the C API.
157162
158163
Raises:
159-
SyclProgramCompilationError: If a SYCL program could not be created.
164+
SyclProgramCompilationError: If a SYCL kernel bundle could not be
165+
created.
160166
"""
161167

162-
cdef DPCTLSyclProgramRef Pref
168+
cdef DPCTLSyclKernelBundleRef KBref
163169
cdef bytes bSrc = src.encode('utf8')
164170
cdef bytes bCOpts = copts.encode('utf8')
165171
cdef const char *Src = <const char*>bSrc
166172
cdef const char *COpts = <const char*>bCOpts
167173
cdef DPCTLSyclContextRef CRef = q.get_sycl_context().get_context_ref()
168-
Pref = DPCTLProgram_CreateFromOCLSource(CRef, Src, COpts)
174+
cdef DPCTLSyclDeviceRef DRef = q.get_sycl_device().get_device_ref()
175+
KBref = DPCTLKernelBundle_CreateFromOCLSource(CRef, DRef, Src, COpts)
169176

170-
if Pref is NULL:
177+
if KBref is NULL:
171178
raise SyclProgramCompilationError()
172179

173-
return SyclProgram._create(Pref)
180+
return SyclProgram._create(KBref)
174181

175182

176183
cpdef create_program_from_spirv(SyclQueue q, const unsigned char[:] IL,
177184
unicode copts=""):
178185
"""
179186
Creates a Sycl interoperability program from an SPIR-V binary.
180187
181-
We use the ``DPCTLProgram_CreateFromOCLSpirv()`` C API function to
182-
create a ``sycl::program`` object from an compiled SPIR-V binary file.
188+
We use the ``DPCTLKernelBundle_CreateFromOCLSpirv()`` C API function to
189+
create a ``sycl::kernel_bundle<sycl::bundle_state::executable>`` object
190+
from an compiled SPIR-V binary file.
183191
184192
Parameters:
185193
q (SyclQueue): The :class:`SyclQueue` for which the
@@ -190,20 +198,25 @@ cpdef create_program_from_spirv(SyclQueue q, const unsigned char[:] IL,
190198
191199
Returns:
192200
program (SyclProgram): A :class:`SyclProgram` object wrapping the
193-
``sycl::program`` returned by the C API.
201+
``sycl::kernel_bundle<sycl::bundle_state::executable>`` returned by
202+
the C API.
194203
195204
Raises:
196-
SyclProgramCompilationError: If a SYCL program could not be created.
205+
SyclProgramCompilationError: If a SYCL kernel bundle could not be
206+
created.
197207
"""
198208

199-
cdef DPCTLSyclProgramRef Pref
209+
cdef DPCTLSyclKernelBundleRef KBref
200210
cdef const unsigned char *dIL = &IL[0]
201211
cdef DPCTLSyclContextRef CRef = q.get_sycl_context().get_context_ref()
212+
cdef DPCTLSyclDeviceRef DRef = q.get_sycl_device().get_device_ref()
202213
cdef size_t length = IL.shape[0]
203214
cdef bytes bCOpts = copts.encode('utf8')
204215
cdef const char *COpts = <const char*>bCOpts
205-
Pref = DPCTLProgram_CreateFromSpirv(CRef, <const void*>dIL, length, COpts)
206-
if Pref is NULL:
216+
KBref = DPCTLKernelBundle_CreateFromSpirv(
217+
CRef, DRef, <const void*>dIL, length, COpts
218+
)
219+
if KBref is NULL:
207220
raise SyclProgramCompilationError()
208221

209-
return SyclProgram._create(Pref)
222+
return SyclProgram._create(KBref)

libsyclinterface/CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ if(DPCTL_ENABLE_L0_PROGRAM_CREATION)
7171
endif()
7272
endif()
7373

74+
if (UNIX)
75+
find_library(PI_OPENCL_LIB
76+
NAMES pi_opencl
77+
HINTS ${IntelSycl_LIBRARY_DIR}
78+
)
79+
find_program(READELF_PROG readelf)
80+
find_program(GREP_PROG grep)
81+
execute_process(
82+
COMMAND ${READELF_PROG} -d ${PI_OPENCL_LIB}
83+
COMMAND ${GREP_PROG} OpenCL
84+
COMMAND ${GREP_PROG} -Po "libOpenCL[^\]]*"
85+
OUTPUT_VARIABLE LIBCL_LOADER_FILENAME
86+
OUTPUT_STRIP_TRAILING_WHITESPACE
87+
ERROR_STRIP_TRAILING_WHITESPACE
88+
)
89+
endif()
90+
7491
configure_file(
7592
${CMAKE_CURRENT_SOURCE_DIR}/include/Config/dpctl_config.h.in
7693
${CMAKE_CURRENT_SOURCE_DIR}/include/Config/dpctl_config.h
@@ -193,7 +210,6 @@ target_include_directories(DPCTLSyclInterface
193210
)
194211
target_link_libraries(DPCTLSyclInterface
195212
PRIVATE ${IntelSycl_SYCL_LIBRARY}
196-
PRIVATE ${IntelSycl_OPENCL_LIBRARY}
197213
)
198214

199215
if(DPCTL_ENABLE_GLOG)

libsyclinterface/helper/include/dpctl_dynamic_lib_helper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ class DynamicLibHelper final
7575
void *sym = dlsym(_handle, symName);
7676
char *error = dlerror();
7777

78-
if (NULL != error) {
78+
if (nullptr != error) {
7979
return nullptr;
8080
}
8181
#elif defined(_WIN32) || defined(_WIN64)
8282
void *sym = (void *)GetProcAddress((HMODULE)_handle, symName);
8383

84-
if (NULL == sym) {
84+
if (nullptr == sym) {
8585
return nullptr;
8686
}
8787
#endif

libsyclinterface/include/Config/dpctl_config.h.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
#pragma once
2727

2828
/* Defined when dpctl was built with level zero program creation enabled. */
29-
#cmakedefine DPCTL_ENABLE_L0_PROGRAM_CREATION @DPCTL_ENABLE_L0_PROGRAM_CREATION@
29+
#cmakedefine DPCTL_ENABLE_L0_PROGRAM_CREATION \
30+
@DPCTL_ENABLE_L0_PROGRAM_CREATION @
3031

3132
/* The DPCPP version used to build dpctl */
3233
#define DPCTL_DPCPP_VERSION "@IntelSycl_VERSION@"
3334

3435
#define DPCTL_LIBZE_LOADER_FILENAME "@LIBZE_LOADER_FILENAME@"
36+
#define DPCTL_LIBCL_LOADER_FILENAME "@LIBCL_LOADER_FILENAME@"

0 commit comments

Comments
 (0)