Skip to content

Commit 69a68a6

Browse files
authored
[SYCL] Add sycl::kernel::get_kernel_bundle method (#3855)
1 parent 25aee28 commit 69a68a6

File tree

9 files changed

+130
-4
lines changed

9 files changed

+130
-4
lines changed

sycl/include/CL/sycl/kernel.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <CL/sycl/detail/export.hpp>
1313
#include <CL/sycl/detail/pi.h>
1414
#include <CL/sycl/info/info_desc.hpp>
15+
#include <CL/sycl/kernel_bundle_enums.hpp>
1516
#include <CL/sycl/stl.hpp>
1617

1718
#include <memory>
@@ -22,6 +23,7 @@ namespace sycl {
2223
class program;
2324
class context;
2425
template <backend Backend> class backend_traits;
26+
template <bundle_state State> class kernel_bundle;
2527

2628
namespace detail {
2729
class kernel_impl;
@@ -100,6 +102,11 @@ class __SYCL_EXPORT kernel {
100102
/// \return a valid SYCL context
101103
context get_context() const;
102104

105+
/// Get the kernel_bundle associated with this kernel.
106+
///
107+
/// \return a valid kernel_bundle<bundle_state::executable>
108+
kernel_bundle<bundle_state::executable> get_kernel_bundle() const;
109+
103110
/// Get the program that this kernel is defined for.
104111
///
105112
/// The value returned must be equal to that returned by

sycl/include/CL/sycl/kernel_bundle.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <CL/sycl/detail/pi.hpp>
1616
#include <CL/sycl/device.hpp>
1717
#include <CL/sycl/kernel.hpp>
18+
#include <CL/sycl/kernel_bundle_enums.hpp>
1819

1920
#include <cassert>
2021
#include <memory>
@@ -25,8 +26,6 @@ namespace sycl {
2526
// Forward declaration
2627
template <backend Backend> class backend_traits;
2728

28-
enum class bundle_state : char { input = 0, object = 1, executable = 2 };
29-
3029
namespace detail {
3130
class kernel_id_impl;
3231
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//==------- kernel_bundle_enums.hpp - SYCL kernel_bundle related enums -----==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
__SYCL_INLINE_NAMESPACE(cl) {
12+
namespace sycl {
13+
14+
enum class bundle_state : char { input = 0, object = 1, executable = 2 };
15+
16+
}
17+
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/source/detail/kernel_impl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ class kernel_impl {
198198
return NativeKernel;
199199
}
200200

201+
KernelBundleImplPtr get_kernel_bundle() const { return MKernelBundleImpl; }
202+
201203
private:
202204
RT::PiKernel MKernel;
203205
const ContextImplPtr MContext;

sycl/source/kernel.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <CL/sycl/detail/pi.h>
1111
#include <CL/sycl/kernel.hpp>
1212
#include <CL/sycl/program.hpp>
13+
#include <detail/kernel_bundle_impl.hpp>
1314
#include <detail/kernel_impl.hpp>
1415

1516
__SYCL_INLINE_NAMESPACE(cl) {
@@ -28,6 +29,12 @@ context kernel::get_context() const {
2829
return impl->get_info<info::kernel::context>();
2930
}
3031

32+
kernel_bundle<sycl::bundle_state::executable>
33+
kernel::get_kernel_bundle() const {
34+
return detail::createSyclObjFromImpl<
35+
kernel_bundle<sycl::bundle_state::executable>>(impl->get_kernel_bundle());
36+
}
37+
3138
program kernel::get_program() const {
3239
return impl->get_info<info::kernel::program>();
3340
}

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4133,6 +4133,7 @@ _ZNK2cl4sycl6device9getNativeEv
41334133
_ZNK2cl4sycl6kernel11get_contextEv
41344134
_ZNK2cl4sycl6kernel11get_programEv
41354135
_ZNK2cl4sycl6kernel13getNativeImplEv
4136+
_ZNK2cl4sycl6kernel17get_kernel_bundleEv
41364137
_ZNK2cl4sycl6kernel18get_sub_group_infoILNS0_4info16kernel_sub_groupE16650EEENS3_12param_traitsIS4_XT_EE11return_typeERKNS0_6deviceE
41374138
_ZNK2cl4sycl6kernel18get_sub_group_infoILNS0_4info16kernel_sub_groupE4537EEENS3_12param_traitsIS4_XT_EE11return_typeERKNS0_6deviceE
41384139
_ZNK2cl4sycl6kernel18get_sub_group_infoILNS0_4info16kernel_sub_groupE4538EEENS3_12param_traitsIS4_XT_EE11return_typeERKNS0_6deviceE

sycl/unittests/SYCL2020/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ set(LLVM_REQUIRES_EH 1)
55
add_sycl_unittest(SYCL2020Tests OBJECT
66
GetNativeOpenCL.cpp
77
SpecConstDefaultValues.cpp
8+
KernelBundle.cpp
89
)
910

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//==---- DefaultValues.cpp --- Spec constants default values unit test -----==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <CL/sycl.hpp>
10+
11+
#include <helpers/CommonRedefinitions.hpp>
12+
#include <helpers/PiImage.hpp>
13+
#include <helpers/PiMock.hpp>
14+
15+
#include <gtest/gtest.h>
16+
17+
class TestKernel;
18+
19+
__SYCL_INLINE_NAMESPACE(cl) {
20+
namespace sycl {
21+
namespace detail {
22+
template <> struct KernelInfo<TestKernel> {
23+
static constexpr unsigned getNumParams() { return 0; }
24+
static const kernel_param_desc_t &getParamDesc(int) {
25+
static kernel_param_desc_t Dummy;
26+
return Dummy;
27+
}
28+
static constexpr const char *getName() { return "TestKernel"; }
29+
static constexpr bool isESIMD() { return false; }
30+
static constexpr bool callsThisItem() { return false; }
31+
static constexpr bool callsAnyThisFreeFunction() { return false; }
32+
};
33+
34+
} // namespace detail
35+
} // namespace sycl
36+
} // __SYCL_INLINE_NAMESPACE(cl)
37+
38+
static sycl::unittest::PiImage generateDefaultImage() {
39+
using namespace sycl::unittest;
40+
41+
PiPropertySet PropSet;
42+
43+
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
44+
45+
PiArray<PiOffloadEntry> Entries = makeEmptyKernels({"TestKernel"});
46+
47+
PiImage Img{PI_DEVICE_BINARY_TYPE_SPIRV, // Format
48+
__SYCL_PI_DEVICE_BINARY_TARGET_SPIRV64, // DeviceTargetSpec
49+
"", // Compile options
50+
"", // Link options
51+
std::move(Bin),
52+
std::move(Entries),
53+
std::move(PropSet)};
54+
55+
return Img;
56+
}
57+
58+
static sycl::unittest::PiImage Img = generateDefaultImage();
59+
static sycl::unittest::PiImageArray<1> ImgArray{&Img};
60+
61+
TEST(KernelBundle, GetKernelBundleFromKernel) {
62+
sycl::platform Plt{sycl::default_selector()};
63+
if (Plt.is_host()) {
64+
std::cout << "Test is not supported on host, skipping\n";
65+
return; // test is not supported on host.
66+
}
67+
68+
if (Plt.get_backend() == sycl::backend::cuda) {
69+
std::cout << "Test is not supported on CUDA platform, skipping\n";
70+
return;
71+
}
72+
73+
sycl::unittest::PiMock Mock{Plt};
74+
setupDefaultMockAPIs(Mock);
75+
76+
const sycl::device Dev = Plt.get_devices()[0];
77+
78+
sycl::queue Queue{Dev};
79+
80+
const sycl::context Ctx = Queue.get_context();
81+
82+
sycl::kernel_bundle<sycl::bundle_state::executable> KernelBundle =
83+
sycl::get_kernel_bundle<sycl::bundle_state::executable>(Ctx, {Dev});
84+
85+
sycl::kernel Kernel =
86+
KernelBundle.get_kernel(sycl::get_kernel_id<TestKernel>());
87+
88+
sycl::kernel_bundle<sycl::bundle_state::executable> RetKernelBundle =
89+
Kernel.get_kernel_bundle();
90+
91+
EXPECT_EQ(KernelBundle, RetKernelBundle);
92+
}

sycl/unittests/SYCL2020/SpecConstDefaultValues.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ static sycl::unittest::PiImage generateImageWithSpecConsts() {
8181
return Img;
8282
}
8383

84-
sycl::unittest::PiImage Img = generateImageWithSpecConsts();
85-
sycl::unittest::PiImageArray<1> ImgArray{&Img};
84+
static sycl::unittest::PiImage Img = generateImageWithSpecConsts();
85+
static sycl::unittest::PiImageArray<1> ImgArray{&Img};
8686

8787
TEST(SpecConstDefaultValues, DISABLED_DefaultValuesAreSet) {
8888
sycl::platform Plt{sycl::default_selector()};

0 commit comments

Comments
 (0)