Skip to content

Commit 91d6eba

Browse files
[SYCL] interim kernel compiler with SYCL support (#14172)
SYCL language support on the part of the kernel_compiler is specified here: #11985 However, that specification is not presently realizable. We need more support from the FE and post link tool to get the demangled names propagated through. But it is usable before that, with constraints about using extern "C" or knowing the mangled kernel name. We have folk that want to test in the interim. I've refrained from updating the spec, and instead this interim support in our experimental extension will be snuck in until it can be completed in full - then we'll update the spec to release it properly.
1 parent 3fb9f78 commit 91d6eba

19 files changed

+831
-115
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,18 +1111,24 @@ static std::pair<std::string, std::string> constructFreeFunctionKernelName(
11111111
SemaSYCL &SemaSYCLRef, const FunctionDecl *FreeFunc, MangleContext &MC) {
11121112
SmallString<256> Result;
11131113
llvm::raw_svector_ostream Out(Result);
1114+
std::string NewName;
11141115
std::string StableName;
11151116

1116-
MC.mangleName(FreeFunc, Out);
1117-
std::string MangledName(Out.str());
1118-
size_t StartNums = MangledName.find_first_of("0123456789");
1119-
size_t EndNums = MangledName.find_first_not_of("0123456789", StartNums);
1120-
size_t NameLength =
1121-
std::stoi(MangledName.substr(StartNums, EndNums - StartNums));
1122-
size_t NewNameLength = 14 /*length of __sycl_kernel_*/ + NameLength;
1123-
std::string NewName = MangledName.substr(0, StartNums) +
1124-
std::to_string(NewNameLength) + "__sycl_kernel_" +
1125-
MangledName.substr(EndNums);
1117+
// Handle extern "C"
1118+
if (FreeFunc->getLanguageLinkage() == CLanguageLinkage) {
1119+
const IdentifierInfo *II = FreeFunc->getIdentifier();
1120+
NewName = "__sycl_kernel_" + II->getName().str();
1121+
} else {
1122+
MC.mangleName(FreeFunc, Out);
1123+
std::string MangledName(Out.str());
1124+
size_t StartNums = MangledName.find_first_of("0123456789");
1125+
size_t EndNums = MangledName.find_first_not_of("0123456789", StartNums);
1126+
size_t NameLength =
1127+
std::stoi(MangledName.substr(StartNums, EndNums - StartNums));
1128+
size_t NewNameLength = 14 /*length of __sycl_kernel_*/ + NameLength;
1129+
NewName = MangledName.substr(0, StartNums) + std::to_string(NewNameLength) +
1130+
"__sycl_kernel_" + MangledName.substr(EndNums);
1131+
}
11261132
StableName = NewName;
11271133
return {NewName, StableName};
11281134
}

sycl/include/sycl/ext/oneapi/properties/property.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ enum PropKind : uint32_t {
209209
CallsIndirectly = 68,
210210
InputDataPlacement = 69,
211211
OutputDataPlacement = 70,
212+
IncludeFiles = 71,
213+
RegisteredKernelNames = 72,
212214
// PropKindSize must always be the last value.
213-
PropKindSize = 71,
215+
PropKindSize = 73,
214216
};
215217

216218
struct property_key_base_tag {};

sycl/include/sycl/kernel_bundle.hpp

Lines changed: 113 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,32 @@ build(const kernel_bundle<bundle_state::input> &InputBundle,
814814

815815
namespace ext::oneapi::experimental {
816816

817+
namespace detail {
818+
struct create_bundle_from_source_props;
819+
struct build_source_bundle_props;
820+
} // namespace detail
821+
822+
/////////////////////////
823+
// PropertyT syclex::include_files
824+
/////////////////////////
825+
struct include_files
826+
: detail::run_time_property_key<detail::PropKind::IncludeFiles> {
827+
include_files();
828+
include_files(const std::string &name, const std::string &content) {
829+
record.emplace_back(std::make_pair(name, content));
830+
}
831+
void add(const std::string &name, const std::string &content) {
832+
record.emplace_back(std::make_pair(name, content));
833+
}
834+
std::vector<std::pair<std::string, std::string>> record;
835+
};
836+
using include_files_key = include_files;
837+
838+
template <>
839+
struct is_property_key_of<include_files_key,
840+
detail::create_bundle_from_source_props>
841+
: std::true_type {};
842+
817843
/////////////////////////
818844
// PropertyT syclex::build_options
819845
/////////////////////////
@@ -826,8 +852,7 @@ struct build_options
826852
using build_options_key = build_options;
827853

828854
template <>
829-
struct is_property_key_of<build_options_key,
830-
sycl::kernel_bundle<bundle_state::ext_oneapi_source>>
855+
struct is_property_key_of<build_options_key, detail::build_source_bundle_props>
831856
: std::true_type {};
832857

833858
/////////////////////////
@@ -840,72 +865,132 @@ struct save_log : detail::run_time_property_key<detail::PropKind::BuildLog> {
840865
using save_log_key = save_log;
841866

842867
template <>
843-
struct is_property_key_of<save_log_key,
844-
sycl::kernel_bundle<bundle_state::ext_oneapi_source>>
868+
struct is_property_key_of<save_log_key, detail::build_source_bundle_props>
845869
: std::true_type {};
846870

847871
/////////////////////////
848-
// syclex::is_source_kernel_bundle_supported
872+
// PropertyT syclex::registered_kernel_names
849873
/////////////////////////
874+
struct registered_kernel_names
875+
: detail::run_time_property_key<detail::PropKind::RegisteredKernelNames> {
876+
std::vector<std::string> kernel_names;
877+
registered_kernel_names() {}
878+
registered_kernel_names(const std::string &knArg) : kernel_names{knArg} {}
879+
registered_kernel_names(const std::vector<std::string> &knsArg)
880+
: kernel_names(knsArg) {}
881+
void add(const std::string &name) { kernel_names.push_back(name); }
882+
};
883+
using registered_kernel_names_key = registered_kernel_names;
884+
885+
template <>
886+
struct is_property_key_of<registered_kernel_names_key,
887+
detail::build_source_bundle_props> : std::true_type {
888+
};
889+
890+
namespace detail {
891+
// forward decls
850892
__SYCL_EXPORT bool is_source_kernel_bundle_supported(backend BE,
851893
source_language Language);
852894

853-
/////////////////////////
854-
// syclex::create_kernel_bundle_from_source
855-
/////////////////////////
856-
857895
__SYCL_EXPORT kernel_bundle<bundle_state::ext_oneapi_source>
858-
create_kernel_bundle_from_source(const context &SyclContext,
859-
source_language Language,
860-
const std::string &Source);
896+
make_kernel_bundle_from_source(
897+
const context &SyclContext, source_language Language,
898+
const std::string &Source,
899+
std::vector<std::pair<std::string, std::string>> IncludePairsVec);
861900

862901
#if (!defined(_HAS_STD_BYTE) || _HAS_STD_BYTE != 0)
863902
__SYCL_EXPORT kernel_bundle<bundle_state::ext_oneapi_source>
864-
create_kernel_bundle_from_source(const context &SyclContext,
865-
source_language Language,
866-
const std::vector<std::byte> &Bytes);
903+
make_kernel_bundle_from_source(
904+
const context &SyclContext, source_language Language,
905+
const std::vector<std::byte> &Bytes,
906+
std::vector<std::pair<std::string, std::string>> IncludePairsVec);
867907
#endif
868908

869-
/////////////////////////
870-
// syclex::build(source_kb) => exe_kb
871-
/////////////////////////
872-
namespace detail {
873-
// forward decl
874909
__SYCL_EXPORT kernel_bundle<bundle_state::executable>
875910
build_from_source(kernel_bundle<bundle_state::ext_oneapi_source> &SourceKB,
876911
const std::vector<device> &Devices,
877912
const std::vector<std::string> &BuildOptions,
878-
std::string *LogPtr);
913+
std::string *LogPtr,
914+
const std::vector<std::string> &RegisteredKernelNames);
879915

880916
} // namespace detail
881917

918+
/////////////////////////
919+
// syclex::create_kernel_bundle_from_source
920+
/////////////////////////
921+
template <
922+
typename PropertyListT = empty_properties_t,
923+
typename = std::enable_if_t<
924+
is_property_list_v<PropertyListT> &&
925+
detail::all_props_are_keys_of<detail::create_bundle_from_source_props,
926+
PropertyListT>::value>>
927+
kernel_bundle<bundle_state::ext_oneapi_source> create_kernel_bundle_from_source(
928+
const context &SyclContext, source_language Language,
929+
const std::string &Source, PropertyListT props = {}) {
930+
std::vector<std::pair<std::string, std::string>> IncludePairsVec;
931+
if constexpr (props.template has_property<include_files>()) {
932+
IncludePairsVec = props.template get_property<include_files>().record;
933+
}
934+
935+
return detail::make_kernel_bundle_from_source(SyclContext, Language, Source,
936+
IncludePairsVec);
937+
}
938+
939+
#if (!defined(_HAS_STD_BYTE) || _HAS_STD_BYTE != 0)
940+
template <
941+
typename PropertyListT = empty_properties_t,
942+
typename = std::enable_if_t<
943+
is_property_list_v<PropertyListT> &&
944+
detail::all_props_are_keys_of<detail::create_bundle_from_source_props,
945+
PropertyListT>::value>>
946+
kernel_bundle<bundle_state::ext_oneapi_source> create_kernel_bundle_from_source(
947+
const context &SyclContext, source_language Language,
948+
const std::vector<std::byte> &Bytes, PropertyListT props = {}) {
949+
std::vector<std::pair<std::string, std::string>> IncludePairsVec;
950+
if constexpr (props.template has_property<include_files>()) {
951+
IncludePairsVec = props.template get_property<include_files>().record;
952+
}
953+
954+
return detail::make_kernel_bundle_from_source(SyclContext, Language, Bytes,
955+
IncludePairsVec);
956+
}
957+
#endif
958+
959+
/////////////////////////
960+
// syclex::build(source_kb) => exe_kb
961+
/////////////////////////
962+
882963
template <typename PropertyListT = empty_properties_t,
883964
typename = std::enable_if_t<
884965
is_property_list_v<PropertyListT> &&
885-
detail::all_props_are_keys_of<
886-
kernel_bundle<bundle_state::ext_oneapi_source>,
887-
PropertyListT>::value>>
966+
detail::all_props_are_keys_of<detail::build_source_bundle_props,
967+
PropertyListT>::value>>
888968

889969
kernel_bundle<bundle_state::executable>
890970
build(kernel_bundle<bundle_state::ext_oneapi_source> &SourceKB,
891971
const std::vector<device> &Devices, PropertyListT props = {}) {
892972
std::vector<std::string> BuildOptionsVec;
893973
std::string *LogPtr = nullptr;
974+
std::vector<std::string> RegisteredKernelNamesVec;
894975
if constexpr (props.template has_property<build_options>()) {
895976
BuildOptionsVec = props.template get_property<build_options>().opts;
896977
}
897978
if constexpr (props.template has_property<save_log>()) {
898979
LogPtr = props.template get_property<save_log>().log;
899980
}
900-
return detail::build_from_source(SourceKB, Devices, BuildOptionsVec, LogPtr);
981+
if constexpr (props.template has_property<registered_kernel_names>()) {
982+
RegisteredKernelNamesVec =
983+
props.template get_property<registered_kernel_names>().kernel_names;
984+
}
985+
return detail::build_from_source(SourceKB, Devices, BuildOptionsVec, LogPtr,
986+
RegisteredKernelNamesVec);
901987
}
902988

903989
template <typename PropertyListT = empty_properties_t,
904990
typename = std::enable_if_t<
905991
is_property_list_v<PropertyListT> &&
906-
detail::all_props_are_keys_of<
907-
kernel_bundle<bundle_state::ext_oneapi_source>,
908-
PropertyListT>::value>>
992+
detail::all_props_are_keys_of<detail::build_source_bundle_props,
993+
PropertyListT>::value>>
909994
kernel_bundle<bundle_state::executable>
910995
build(kernel_bundle<bundle_state::ext_oneapi_source> &SourceKB,
911996
PropertyListT props = {}) {

sycl/include/sycl/kernel_bundle_enums.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum class bundle_state : char {
2020

2121
namespace ext::oneapi::experimental {
2222

23-
enum class source_language : int { opencl = 0, spirv = 1 /* sycl, cuda */ };
23+
enum class source_language : int { opencl = 0, spirv = 1, sycl = 2 /* cuda */ };
2424

2525
// opencl versions
2626
struct cl_version {

sycl/source/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ set(SYCL_COMMON_SOURCES
217217
"detail/jit_compiler.cpp"
218218
"detail/jit_device_binaries.cpp"
219219
"detail/kernel_compiler/kernel_compiler_opencl.cpp"
220+
"detail/kernel_compiler/kernel_compiler_sycl.cpp"
220221
"detail/kernel_impl.cpp"
221222
"detail/kernel_program_cache.cpp"
222223
"detail/memory_manager.cpp"

sycl/source/detail/device_impl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ bool device_impl::isGetDeviceAndHostTimerSupported() {
824824
bool device_impl::extOneapiCanCompile(
825825
ext::oneapi::experimental::source_language Language) {
826826
try {
827-
return is_source_kernel_bundle_supported(getBackend(), Language);
827+
return sycl::ext::oneapi::experimental::detail::
828+
is_source_kernel_bundle_supported(getBackend(), Language);
828829
} catch (sycl::exception &) {
829830
return false;
830831
}

0 commit comments

Comments
 (0)