Skip to content

Commit 04585d8

Browse files
committed
[SYCL] Add host kernel instantiation for debuggers
The changes in intel#14460 removed the seemingly unused functions for running kernels on on host. However, this turned out to be used by debuggers as they need the kernel code to be in the host executable. This commit adds a simplified version of the kernel instantiation that the aforementioned patch removed. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 101307f commit 04585d8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

sycl/include/sycl/detail/cg_types.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ runKernelWithArg(KernelType KernelName, ArgType Arg) {
154154
// The pure virtual class aimed to store lambda/functors of any type.
155155
class HostKernelBase {
156156
public:
157+
// NOTE: InstatitateKernelOnHost() should not be called.
158+
virtual void InstatitateKernelOnHost() = 0;
157159
// Return pointer to the lambda object.
158160
// Used to extract captured variables.
159161
virtual char *getPtr() = 0;
@@ -172,6 +174,50 @@ class HostKernel : public HostKernelBase {
172174
public:
173175
HostKernel(KernelType Kernel) : MKernel(Kernel) {}
174176

177+
// This function is needed for host-side compilation to keep kernels
178+
// instantitated. This is important for debuggers to be able to associate
179+
// kernel code instructions with source code lines.
180+
// NOTE: InstatitateKernelOnHost() should not be called.
181+
void InstatitateKernelOnHost() override {
182+
if constexpr (std::is_same_v<KernelArgType, void>) {
183+
runKernelWithoutArg(MKernel);
184+
} else if constexpr (std::is_same_v<KernelArgType, sycl::id<Dims>>) {
185+
sycl::id ID = InitializedVal<Dims, id>::template get<0>();
186+
runKernelWithArg<const KernelArgType &>(MKernel, ID);
187+
} else if constexpr (std::is_same_v<KernelArgType, item<Dims, true>> ||
188+
std::is_same_v<KernelArgType, item<Dims, false>>) {
189+
constexpr bool HasOffset =
190+
std::is_same_v<KernelArgType, item<Dims, true>>;
191+
KernelArgType Item = IDBuilder::createItem<Dims, HasOffset>(
192+
InitializedVal<Dims, range>::template get<1>(),
193+
InitializedVal<Dims, id>::template get<0>());
194+
runKernelWithArg<KernelArgType>(MKernel, Item);
195+
} else if constexpr (std::is_same_v<KernelArgType, nd_item<Dims>>) {
196+
sycl::range<Dims> Range = InitializedVal<Dims, range>::template get<1>();
197+
sycl::id<Dims> ID = InitializedVal<Dims, id>::template get<0>();
198+
sycl::group<Dims> Group =
199+
IDBuilder::createGroup<Dims>(Range, Range, Range, ID);
200+
sycl::item<Dims, true> GlobalItem =
201+
IDBuilder::createItem<Dims, true>(Range, ID, ID);
202+
sycl::item<Dims, false> LocalItem =
203+
IDBuilder::createItem<Dims, false>(Range, ID);
204+
KernelArgType NDItem =
205+
IDBuilder::createNDItem<Dims>(GlobalItem, LocalItem, Group);
206+
runKernelWithArg<const KernelArgType>(MKernel, NDItem);
207+
} else if constexpr (std::is_same_v<KernelArgType, sycl::group<Dims>>) {
208+
sycl::range<Dims> Range = InitializedVal<Dims, range>::template get<1>();
209+
sycl::id<Dims> ID = InitializedVal<Dims, id>::template get<0>();
210+
KernelArgType Group =
211+
IDBuilder::createGroup<Dims>(Range, Range, Range, ID);
212+
runKernelWithArg<KernelArgType>(MKernel, Group);
213+
} else {
214+
// Assume that anything else can be default-constructed. If not, this
215+
// should fail to compile and the implementor should implement a generic
216+
// case for the new argument type.
217+
runKernelWithArg<KernelArgType>(MKernel, KernelArgType{});
218+
}
219+
}
220+
175221
char *getPtr() override { return reinterpret_cast<char *>(&MKernel); }
176222

177223
~HostKernel() = default;

sycl/test/abi/vtable.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
// Changing vtable breaks ABI. If this test fails, please, refer to ABI Policy
99
// Guide for further instructions.
1010

11+
void foo(sycl::detail::HostKernelBase &HKB) {
12+
HKB.InstatitateKernelOnHost();
13+
}
14+
// CHECK: Vtable for 'sycl::detail::HostKernelBase' (6 entries).
15+
// CHECK-NEXT: 0 | offset_to_top (0)
16+
// CHECK-NEXT: 1 | sycl::detail::HostKernelBase RTTI
17+
// CHECK-NEXT: -- (sycl::detail::HostKernelBase, 0) vtable address --
18+
// CHECK-NEXT: 2 | void sycl::detail::HostKernelBase::InstatitateKernelOnHost() [pure]
19+
// CHECK-NEXT: 3 | char *sycl::detail::HostKernelBase::getPtr() [pure]
20+
// CHECK-NEXT: 4 | sycl::detail::HostKernelBase::~HostKernelBase() [complete]
21+
// CHECK-NEXT: 5 | sycl::detail::HostKernelBase::~HostKernelBase() [deleting]
22+
1123
void foo(sycl::detail::PropertyWithDataBase *Prop) { delete Prop; }
1224
// CHECK: Vtable for 'sycl::detail::PropertyWithDataBase' (4 entries).
1325
// CHECK-NEXT: 0 | offset_to_top (0)

0 commit comments

Comments
 (0)