Skip to content

Commit 36e123d

Browse files
authored
[SYCL] Implement sycl_ext_oneapi_kernel_compiler_spirv (#12291)
Implements the extension described in #11954. This PR includes the following changes: - Adds a `create_kernel_bundle_from_source` overload for `std::vector<std::byte>` kernel sources. - Adds new `source_language::spirv`. - Defines `SYCL_EXT_ONEAPI_KERNEL_COMPILER_SPIRV`. - Adds support for SPIR-V kernels created from `std::vector<std::byte>` sources. - Moves `sycl_ext_oneapi_kernel_compiler_spirv.asciidoc` from `proposed` to `experimental`. --------- Signed-off-by: Michael Aziz <[email protected]>
1 parent daa110a commit 36e123d

File tree

12 files changed

+296
-28
lines changed

12 files changed

+296
-28
lines changed

sycl/doc/extensions/proposed/sycl_ext_oneapi_kernel_compiler_spirv.asciidoc renamed to sycl/doc/extensions/experimental/sycl_ext_oneapi_kernel_compiler_spirv.asciidoc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@ This extension also depends on the following other SYCL extensions:
5252

5353
== Status
5454

55-
This is a proposed extension specification, intended to gather community
56-
feedback.
57-
Interfaces defined in this specification may not be implemented yet or may be
58-
in a preliminary state.
59-
The specification itself may also change in incompatible ways before it is
60-
finalized.
61-
*Shipping software products should not rely on APIs defined in this
62-
specification.*
55+
This is an experimental extension specification, intended to provide early
56+
access to features and gather community feedback. Interfaces defined in
57+
this specification are implemented in DPC++, but they are not finalized
58+
and may change incompatibly in future versions of DPC++ without prior notice.
59+
*Shipping software products should not rely on APIs defined in
60+
this specification.*
6361

6462

6563
== Overview

sycl/include/sycl/kernel_bundle.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <sycl/ext/oneapi/properties/property_value.hpp> // and log
2626

2727
#include <array> // for array
28+
#include <cstddef> // for std::byte
2829
#include <cstring> // for size_t, memcpy
2930
#include <functional> // for function
3031
#include <iterator> // for distance
@@ -46,7 +47,7 @@ auto get_native(const kernel_bundle<State> &Obj)
4647
namespace detail {
4748
class kernel_id_impl;
4849
class kernel_impl;
49-
}
50+
} // namespace detail
5051

5152
template <typename KernelName> kernel_id get_kernel_id();
5253

@@ -886,11 +887,18 @@ __SYCL_EXPORT bool is_source_kernel_bundle_supported(backend BE,
886887
/////////////////////////
887888
// syclex::create_kernel_bundle_from_source
888889
/////////////////////////
890+
891+
__SYCL_EXPORT kernel_bundle<bundle_state::ext_oneapi_source>
892+
create_kernel_bundle_from_source(const context &SyclContext,
893+
source_language Language,
894+
const std::string &Source);
895+
896+
#if (!defined(_HAS_STD_BYTE) || _HAS_STD_BYTE != 0)
889897
__SYCL_EXPORT kernel_bundle<bundle_state::ext_oneapi_source>
890-
create_kernel_bundle_from_source(
891-
const context &SyclContext,
892-
sycl::ext::oneapi::experimental::source_language Language,
893-
const std::string &Source);
898+
create_kernel_bundle_from_source(const context &SyclContext,
899+
source_language Language,
900+
const std::vector<std::byte> &Bytes);
901+
#endif
894902

895903
/////////////////////////
896904
// syclex::build(source_kb) => exe_kb

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 /* sycl , spir-v, cuda */ };
23+
enum class source_language : int { opencl = 0, spirv = 1 /* sycl, cuda */ };
2424

2525
} // namespace ext::oneapi::experimental
2626

sycl/source/detail/kernel_bundle_impl.hpp

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

2323
#include <algorithm>
2424
#include <cassert>
25+
#include <cstdint>
2526
#include <cstring>
2627
#include <memory>
2728
#include <vector>
@@ -334,6 +335,14 @@ class kernel_bundle_impl {
334335
: MContext(Context), MDevices(Context.get_devices()),
335336
MState(bundle_state::ext_oneapi_source), Language(Lang), Source(Src) {}
336337

338+
// oneapi_ext_kernel_compiler
339+
// construct from source bytes
340+
kernel_bundle_impl(const context &Context, syclex::source_language Lang,
341+
const std::vector<std::byte> &Bytes)
342+
: MContext(Context), MDevices(Context.get_devices()),
343+
MState(bundle_state::ext_oneapi_source), Language(Lang), Source(Bytes) {
344+
}
345+
337346
// oneapi_ext_kernel_compiler
338347
// interop constructor
339348
kernel_bundle_impl(context Ctx, std::vector<device> Devs,
@@ -350,17 +359,27 @@ class kernel_bundle_impl {
350359
std::string *LogPtr) {
351360
assert(MState == bundle_state::ext_oneapi_source &&
352361
"bundle_state::ext_oneapi_source required");
353-
assert(Language == syclex::source_language::opencl &&
354-
"TODO: add other Languages. Must be OpenCL");
355-
if (Language != syclex::source_language::opencl)
362+
363+
const auto spirv = [&]() -> std::vector<uint8_t> {
364+
if (Language == syclex::source_language::opencl) {
365+
// if successful, the log is empty. if failed, throws an error with the
366+
// compilation log.
367+
const auto &SourceStr = std::get<std::string>(this->Source);
368+
return syclex::detail::OpenCLC_to_SPIRV(SourceStr, BuildOptions,
369+
LogPtr);
370+
}
371+
if (Language == syclex::source_language::spirv) {
372+
const auto &SourceBytes =
373+
std::get<std::vector<std::byte>>(this->Source);
374+
std::vector<uint8_t> Result(SourceBytes.size());
375+
std::transform(SourceBytes.cbegin(), SourceBytes.cend(), Result.begin(),
376+
[](std::byte B) { return static_cast<uint8_t>(B); });
377+
return Result;
378+
}
356379
throw sycl::exception(
357380
make_error_code(errc::invalid),
358-
"OpenCL C is the only supported language at this time");
359-
360-
// if successful, the log is empty. if failed, throws an error with the
361-
// compilation log.
362-
auto spirv =
363-
syclex::detail::OpenCLC_to_SPIRV(this->Source, BuildOptions, LogPtr);
381+
"OpenCL C and SPIR-V are the only supported languages at this time");
382+
}();
364383

365384
// see also program_manager.cpp::createSpirvProgram()
366385
using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
@@ -682,7 +701,7 @@ class kernel_bundle_impl {
682701
bundle_state MState;
683702
// ext_oneapi_kernel_compiler : Source, Languauge, KernelNames
684703
const syclex::source_language Language = syclex::source_language::opencl;
685-
const std::string Source;
704+
const std::variant<std::string, std::vector<std::byte>> Source;
686705
// only kernel_bundles created from source have KernelNames member.
687706
std::vector<std::string> KernelNames;
688707
};

sycl/source/feature_test.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ inline namespace _V1 {
4545
#define SYCL_EXT_ONEAPI_GROUP_SORT 1
4646
#define SYCL_EXT_ONEAPI_KERNEL_COMPILER 1
4747
#define SYCL_EXT_ONEAPI_KERNEL_COMPILER_OPENCL 1
48+
#define SYCL_EXT_ONEAPI_KERNEL_COMPILER_SPIRV 1
4849
#define SYCL_EXT_ONEAPI_MAX_WORK_GROUP_QUERY 1
4950
#define SYCL_EXT_ONEAPI_ND_RANGE_REDUCTIONS 1
5051
#define SYCL_EXT_ONEAPI_DEFAULT_CONTEXT 1

sycl/source/kernel_bundle.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include <detail/kernel_id_impl.hpp>
1313
#include <detail/program_manager/program_manager.hpp>
1414

15+
#include <cstddef>
1516
#include <set>
17+
#include <vector>
1618

1719
namespace sycl {
1820
inline namespace _V1 {
@@ -368,12 +370,16 @@ using kernel_bundle_impl = sycl::detail::kernel_bundle_impl;
368370
// syclex::is_source_kernel_bundle_supported
369371
/////////////////////////
370372
bool is_source_kernel_bundle_supported(backend BE, source_language Language) {
371-
// at the moment, OpenCL is the only language supported
372-
// and it's support is limited to the opencl and level_zero backends.
373+
// Support is limited to the opencl and level_zero backends.
373374
bool BE_Acceptable = (BE == sycl::backend::ext_oneapi_level_zero) ||
374375
(BE == sycl::backend::opencl);
375-
if ((Language == source_language::opencl) && BE_Acceptable) {
376-
return detail::OpenCLC_Compilation_Available();
376+
if (BE_Acceptable) {
377+
// At the moment, OpenCL and SPIR-V are the only supported languages.
378+
if (Language == source_language::opencl) {
379+
return detail::OpenCLC_Compilation_Available();
380+
} else if (Language == source_language::spirv) {
381+
return true;
382+
}
377383
}
378384

379385
// otherwise
@@ -383,6 +389,7 @@ bool is_source_kernel_bundle_supported(backend BE, source_language Language) {
383389
/////////////////////////
384390
// syclex::create_kernel_bundle_from_source
385391
/////////////////////////
392+
386393
source_kb create_kernel_bundle_from_source(const context &SyclContext,
387394
source_language Language,
388395
const std::string &Source) {
@@ -399,6 +406,20 @@ source_kb create_kernel_bundle_from_source(const context &SyclContext,
399406
return sycl::detail::createSyclObjFromImpl<source_kb>(KBImpl);
400407
}
401408

409+
source_kb
410+
create_kernel_bundle_from_source(const context &SyclContext,
411+
source_language Language,
412+
const std::vector<std::byte> &Bytes) {
413+
backend BE = SyclContext.get_backend();
414+
if (!is_source_kernel_bundle_supported(BE, Language))
415+
throw sycl::exception(make_error_code(errc::invalid),
416+
"kernel_bundle creation from source not supported");
417+
418+
std::shared_ptr<kernel_bundle_impl> KBImpl =
419+
std::make_shared<kernel_bundle_impl>(SyclContext, Language, Bytes);
420+
return sycl::detail::createSyclObjFromImpl<source_kb>(KBImpl);
421+
}
422+
402423
/////////////////////////
403424
// syclex::detail::build_from_source(source_kb) => exe_kb
404425
/////////////////////////
5.21 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)