Skip to content

Commit 01fc7f9

Browse files
committed
Enable conditionally and make API C-compatible
Enable SYCL source compilation only for DPC++ versions that actually support the compilation, based on the __SYCL_COMPILER_VERSION reported. Use the correct naming for the property based on DPC++ version. Remove all mentions of `std::vector` and other STL types from the header and use opaque pointers instead. Signed-off-by: Lukas Sommer <[email protected]>
1 parent 3c294f3 commit 01fc7f9

File tree

6 files changed

+91
-54
lines changed

6 files changed

+91
-54
lines changed

dpctl/_backend.pxd

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,18 +454,18 @@ cdef extern from "syclinterface/dpctl_sycl_kernel_bundle_interface.h":
454454
cdef DPCTLBuildOptionListRef DPCTLBuildOptionList_Create()
455455
cdef void DPCTLBuildOptionList_Delete(DPCTLBuildOptionListRef Ref)
456456
cdef void DPCTLBuildOptionList_Append(DPCTLBuildOptionListRef Ref,
457-
const char *Option)
457+
const char *Option)
458458

459459
cdef DPCTLKernelNameListRef DPCTLKernelNameList_Create()
460460
cdef void DPCTLKernelNameList_Delete(DPCTLKernelNameListRef Ref)
461461
cdef void DPCTLKernelNameList_Append(DPCTLKernelNameListRef Ref,
462-
const char *Option)
462+
const char *Option)
463463

464464
cdef DPCTLVirtualHeaderListRef DPCTLVirtualHeaderList_Create()
465465
cdef void DPCTLVirtualHeaderList_Delete(DPCTLVirtualHeaderListRef Ref)
466466
cdef void DPCTLVirtualHeaderList_Append(DPCTLVirtualHeaderListRef Ref,
467-
const char *Name,
468-
const char *Content)
467+
const char *Name,
468+
const char *Content)
469469

470470
cdef DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
471471
const DPCTLSyclContextRef Ctx,
@@ -475,11 +475,12 @@ cdef extern from "syclinterface/dpctl_sycl_kernel_bundle_interface.h":
475475
DPCTLKernelNameListRef Names,
476476
DPCTLBuildOptionListRef BuildOptions)
477477

478-
cdef DPCTLSyclKernelRef DPCTLKernelBundle_GetSyclKernel(DPCTLSyclKernelBundleRef KBRef,
479-
const char *KernelName)
478+
cdef DPCTLSyclKernelRef DPCTLKernelBundle_GetSyclKernel(
479+
DPCTLSyclKernelBundleRef KBRef,
480+
const char *KernelName)
480481

481482
cdef bool DPCTLKernelBundle_HasSyclKernel(DPCTLSyclKernelBundleRef KBRef,
482-
const char *KernelName);
483+
const char *KernelName)
483484

484485

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

dpctl/_sycl_device.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ from ._backend cimport ( # noqa: E211
2626
DPCTLDefaultSelector_Create,
2727
DPCTLDevice_AreEq,
2828
DPCTLDevice_CanCompileOpenCL,
29-
DPCTLDevice_CanCompileSPIRV,
3029
DPCTLDevice_CanCompileSYCL,
3130
DPCTLDevice_Copy,
3231
DPCTLDevice_CreateFromSelector,
@@ -2192,7 +2191,6 @@ cdef class SyclDevice(_SyclDevice):
21922191
raise ValueError(f"Unknown source language {language}")
21932192

21942193

2195-
21962194
cdef api DPCTLSyclDeviceRef SyclDevice_GetDeviceRef(SyclDevice dev):
21972195
"""
21982196
C-API function to get opaque device reference from

dpctl/program/_program.pxd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ cdef api class SyclProgram [object PySyclProgramObject, type PySyclProgramType]:
5252
cdef bint _is_sycl_source
5353

5454
@staticmethod
55-
cdef SyclProgram _create (DPCTLSyclKernelBundleRef pref, bint _is_sycl_source)
55+
cdef SyclProgram _create (DPCTLSyclKernelBundleRef pref,
56+
bint _is_sycl_source)
5657
cdef DPCTLSyclKernelBundleRef get_program_ref (self)
5758
cpdef SyclKernel get_sycl_kernel(self, str kernel_name)
5859

dpctl/program/_program.pyx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ cdef class SyclProgram:
211211
"""
212212

213213
@staticmethod
214-
cdef SyclProgram _create(DPCTLSyclKernelBundleRef KBRef, bint is_sycl_source):
214+
cdef SyclProgram _create(DPCTLSyclKernelBundleRef KBRef,
215+
bint is_sycl_source):
215216
cdef SyclProgram ret = SyclProgram.__new__(SyclProgram)
216217
ret._program_ref = KBRef
217218
ret._is_sycl_source = is_sycl_source
@@ -342,7 +343,10 @@ cpdef create_program_from_spirv(SyclQueue q, const unsigned char[:] IL,
342343
return SyclProgram._create(KBref, False)
343344

344345

345-
cpdef create_program_from_sycl_source(SyclQueue q, unicode source, list headers=[], list registered_names=[], list copts=[]):
346+
cpdef create_program_from_sycl_source(SyclQueue q, unicode source,
347+
list headers=None,
348+
list registered_names=None,
349+
list copts=None):
346350
"""
347351
Creates an executable SYCL kernel_bundle from SYCL source code.
348352
@@ -384,7 +388,7 @@ cpdef create_program_from_sycl_source(SyclQueue q, unicode source, list headers=
384388
cdef DPCTLSyclKernelBundleRef KBref
385389
cdef DPCTLSyclContextRef CRef = q.get_sycl_context().get_context_ref()
386390
cdef DPCTLSyclDeviceRef DRef = q.get_sycl_device().get_device_ref()
387-
cdef bytes bSrc = source.encode('utf8')
391+
cdef bytes bSrc = source.encode("utf8")
388392
cdef const char *Src = <const char*>bSrc
389393
cdef DPCTLBuildOptionListRef BuildOpts = DPCTLBuildOptionList_Create()
390394
cdef bytes bOpt
@@ -397,7 +401,7 @@ cpdef create_program_from_sycl_source(SyclQueue q, unicode source, list headers=
397401
if not isinstance(opt, unicode):
398402
DPCTLBuildOptionList_Delete(BuildOpts)
399403
raise SyclProgramCompilationError()
400-
bOpt = opt.encode('utf8')
404+
bOpt = opt.encode("utf8")
401405
sOpt = <const char*>bOpt
402406
DPCTLBuildOptionList_Append(BuildOpts, sOpt)
403407

@@ -407,21 +411,22 @@ cpdef create_program_from_sycl_source(SyclQueue q, unicode source, list headers=
407411
DPCTLBuildOptionList_Delete(BuildOpts)
408412
DPCTLKernelNameList_Delete(KernelNames)
409413
raise SyclProgramCompilationError()
410-
bName = name.encode('utf8')
414+
bName = name.encode("utf8")
411415
sName = <const char*>bName
412416
DPCTLKernelNameList_Append(KernelNames, sName)
413417

418+
cdef DPCTLVirtualHeaderListRef VirtualHeaders
419+
VirtualHeaders = DPCTLVirtualHeaderList_Create()
414420

415-
cdef DPCTLVirtualHeaderListRef VirtualHeaders = DPCTLVirtualHeaderList_Create()
416421
for name, content in headers:
417422
if not isinstance(name, unicode) or not isinstance(content, unicode):
418423
DPCTLBuildOptionList_Delete(BuildOpts)
419424
DPCTLKernelNameList_Delete(KernelNames)
420425
DPCTLVirtualHeaderList_Delete(VirtualHeaders)
421426
raise SyclProgramCompilationError()
422-
bName = name.encode('utf8')
427+
bName = name.encode("utf8")
423428
sName = <const char*>bName
424-
bContent = content.encode('utf8')
429+
bContent = content.encode("utf8")
425430
sContent = <const char*>bContent
426431
DPCTLVirtualHeaderList_Append(VirtualHeaders, sName, sContent)
427432

libsyclinterface/include/syclinterface/dpctl_sycl_kernel_bundle_interface.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
#include "Support/MemOwnershipAttrs.h"
3333
#include "dpctl_data_types.h"
3434
#include "dpctl_sycl_types.h"
35-
#include <string>
36-
#include <vector>
3735

3836
DPCTL_C_EXTERN_C_BEGIN
3937

@@ -131,24 +129,9 @@ DPCTL_API
131129
__dpctl_give DPCTLSyclKernelBundleRef
132130
DPCTLKernelBundle_Copy(__dpctl_keep const DPCTLSyclKernelBundleRef KBRef);
133131

134-
struct DPCTLBuildOptionList
135-
{
136-
std::vector<std::string> options;
137-
};
138-
139-
struct DPCTLKernelNameList
140-
{
141-
std::vector<std::string> names;
142-
};
143-
144-
struct DPCTLVirtualHeaderList
145-
{
146-
std::vector<std::pair<std::string, std::string>> headers;
147-
};
148-
149-
using DPCTLBuildOptionListRef = DPCTLBuildOptionList *;
150-
using DPCTLKernelNameListRef = DPCTLKernelNameList *;
151-
using DPCTLVirtualHeaderListRef = DPCTLVirtualHeaderList *;
132+
typedef struct DPCTLBuildOptionList *DPCTLBuildOptionListRef;
133+
typedef struct DPCTLKernelNameList *DPCTLKernelNameListRef;
134+
typedef struct DPCTLVirtualHeaderList *DPCTLVirtualHeaderListRef;
152135

153136
/*!
154137
* @brief Create an empty list of build options.

libsyclinterface/source/dpctl_sycl_kernel_bundle_interface.cpp

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -762,58 +762,102 @@ DPCTLKernelBundle_Copy(__dpctl_keep const DPCTLSyclKernelBundleRef KBRef)
762762
}
763763
}
764764

765+
using build_option_list_t = std::vector<std::string>;
766+
765767
__dpctl_give DPCTLBuildOptionListRef DPCTLBuildOptionList_Create()
766768
{
767-
return new DPCTLBuildOptionList;
769+
auto BuildOptionList =
770+
std::unique_ptr<build_option_list_t>(new build_option_list_t());
771+
auto *RetVal =
772+
reinterpret_cast<DPCTLBuildOptionListRef>(BuildOptionList.get());
773+
BuildOptionList.release();
774+
return RetVal;
768775
}
769776

770777
void DPCTLBuildOptionList_Delete(__dpctl_take DPCTLBuildOptionListRef Ref)
771778
{
772-
delete Ref;
779+
delete reinterpret_cast<build_option_list_t *>(Ref);
773780
}
774781

775782
void DPCTLBuildOptionList_Append(__dpctl_keep DPCTLBuildOptionListRef Ref,
776783
__dpctl_keep const char *Option)
777784
{
778-
Ref->options.emplace_back(Option);
785+
reinterpret_cast<build_option_list_t *>(Ref)->emplace_back(Option);
779786
}
780787

788+
using kernel_name_list_t = std::vector<std::string>;
789+
781790
__dpctl_give DPCTLKernelNameListRef DPCTLKernelNameList_Create()
782791
{
783-
return new DPCTLKernelNameList;
792+
auto KernelNameList =
793+
std::unique_ptr<kernel_name_list_t>(new kernel_name_list_t());
794+
auto *RetVal =
795+
reinterpret_cast<DPCTLKernelNameListRef>(KernelNameList.get());
796+
KernelNameList.release();
797+
return RetVal;
784798
}
785799

786800
void DPCTLKernelNameList_Delete(__dpctl_take DPCTLKernelNameListRef Ref)
787801
{
788-
delete Ref;
802+
delete reinterpret_cast<kernel_name_list_t *>(Ref);
789803
}
790804

791805
void DPCTLKernelNameList_Append(__dpctl_keep DPCTLKernelNameListRef Ref,
792806
__dpctl_keep const char *Option)
793807
{
794-
Ref->names.emplace_back(Option);
808+
reinterpret_cast<kernel_name_list_t *>(Ref)->emplace_back(Option);
795809
}
796810

811+
using virtual_header_list_t = std::vector<std::pair<std::string, std::string>>;
812+
797813
__dpctl_give DPCTLVirtualHeaderListRef DPCTLVirtualHeaderList_Create()
798814
{
799-
return new DPCTLVirtualHeaderList;
815+
auto HeaderList =
816+
std::unique_ptr<virtual_header_list_t>(new virtual_header_list_t());
817+
auto *RetVal =
818+
reinterpret_cast<DPCTLVirtualHeaderListRef>(HeaderList.get());
819+
HeaderList.release();
820+
return RetVal;
800821
}
801822

802823
void DPCTLVirtualHeaderList_Delete(__dpctl_take DPCTLVirtualHeaderListRef Ref)
803824
{
804-
delete Ref;
825+
delete reinterpret_cast<virtual_header_list_t *>(Ref);
805826
}
806827

807828
void DPCTLVirtualHeaderList_Append(__dpctl_keep DPCTLVirtualHeaderListRef Ref,
808829
__dpctl_keep const char *Name,
809830
__dpctl_keep const char *Content)
810831
{
811832
auto Header = std::make_pair<std::string, std::string>(Name, Content);
812-
Ref->headers.push_back(Header);
833+
reinterpret_cast<virtual_header_list_t *>(Ref)->push_back(Header);
813834
}
814835

815836
namespace syclex = sycl::ext::oneapi::experimental;
816837

838+
#if defined(SYCL_EXT_ONEAPI_KERNEL_COMPILER) && \
839+
defined(__SYCL_COMPILER_VERSION) && !defined(SUPPORTS_SYCL_COMPILATION)
840+
// SYCL source code compilation is supported from 2025.1 onwards.
841+
#if __SYCL_COMPILER_VERSION >= 20250317u
842+
#define SUPPORTS_SYCL_COMPILATION 1
843+
#else
844+
#define SUPPORTS_SYCL_COMPILATION 0
845+
#endif
846+
#endif
847+
848+
#if (SUPPORTS_SYCL_COMPILATION > 0)
849+
#ifndef __SYCL_COMPILER_VERSION
850+
#error SYCL compiler version not defined
851+
#else
852+
// The property was renamed to `registered_names` after 2025.1
853+
#if __SYCL_COMPILER_VERSION > 20250317u
854+
using registered_names_property_t = syclex::registered_names;
855+
#else
856+
using registered_names_property_t = syclex::registered_kernel_names;
857+
#endif
858+
#endif
859+
#endif
860+
817861
__dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
818862
__dpctl_keep const DPCTLSyclContextRef Ctx,
819863
__dpctl_keep const DPCTLSyclDeviceRef Dev,
@@ -822,15 +866,17 @@ __dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
822866
__dpctl_keep DPCTLKernelNameListRef Names,
823867
__dpctl_keep DPCTLBuildOptionListRef BuildOptions)
824868
{
825-
#ifdef SYCL_EXT_ONEAPI_KERNEL_COMPILER
869+
#if (SUPPORTS_SYCL_COMPILATION > 0)
826870
context *SyclCtx = unwrap<context>(Ctx);
827871
device *SyclDev = unwrap<device>(Dev);
828872
if (!SyclDev->ext_oneapi_can_compile(syclex::source_language::sycl)) {
829873
return nullptr;
830874
}
831875
try {
832876
syclex::include_files IncludeFiles;
833-
for (auto &Include : Headers->headers) {
877+
for (auto &Include :
878+
*reinterpret_cast<virtual_header_list_t *>(Headers))
879+
{
834880
const auto &[Name, Content] = Include;
835881
IncludeFiles.add(Name, Content);
836882
}
@@ -840,12 +886,15 @@ __dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
840886
*SyclCtx, syclex::source_language::sycl, Src,
841887
syclex::properties{IncludeFiles});
842888

843-
syclex::registered_names RegisteredNames;
844-
for (const std::string &Name : Names->names) {
889+
registered_names_property_t RegisteredNames;
890+
for (const std::string &Name :
891+
*reinterpret_cast<kernel_name_list_t *>(Names))
892+
{
845893
RegisteredNames.add(Name);
846894
}
847895

848-
syclex::build_options Opts{BuildOptions->options};
896+
syclex::build_options Opts{
897+
*reinterpret_cast<build_option_list_t *>(BuildOptions)};
849898

850899
std::vector<sycl::device> Devices({*SyclDev});
851900

@@ -869,7 +918,7 @@ __dpctl_give DPCTLSyclKernelRef
869918
DPCTLKernelBundle_GetSyclKernel(__dpctl_keep DPCTLSyclKernelBundleRef KBRef,
870919
__dpctl_keep const char *KernelName)
871920
{
872-
#ifdef SYCL_EXT_ONEAPI_KERNEL_COMPILER
921+
#if (SUPPORTS_SYCL_COMPILATION > 0)
873922
try {
874923
auto KernelBundle =
875924
unwrap<sycl::kernel_bundle<bundle_state::executable>>(KBRef);
@@ -888,7 +937,7 @@ bool DPCTLKernelBundle_HasSyclKernel(__dpctl_keep DPCTLSyclKernelBundleRef
888937
KBRef,
889938
__dpctl_keep const char *KernelName)
890939
{
891-
#ifdef SYCL_EXT_ONEAPI_KERNEL_COMPILER
940+
#if (SUPPORTS_SYCL_COMPILATION > 0)
892941
try {
893942
auto KernelBundle =
894943
unwrap<sycl::kernel_bundle<bundle_state::executable>>(KBRef);

0 commit comments

Comments
 (0)