Skip to content

Commit 6c12649

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 <lukas.sommer@codeplay.com>
1 parent a80bd36 commit 6c12649

File tree

2 files changed

+68
-36
lines changed

2 files changed

+68
-36
lines changed

libsyclinterface/include/syclinterface/dpctl_sycl_kernel_bundle_interface.h

+3-20
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

+65-16
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)