Skip to content

Commit 1778ad5

Browse files
committed
[SYCL] Fix use-after-release kernel issue
After the changes in intel#17380, kernels looked up from a kernel bundle could cause lifetime issues, as the lifetime of the returned objects had to be handled differently based on the path taken. These changes move back to only look up non-source-based kernels and use the stored kernel objects otherwise. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent d653b20 commit 1778ad5

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

sycl/source/detail/graph_impl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,11 +1474,10 @@ void exec_graph_impl::populateURKernelUpdateStructs(
14741474
ur_kernel_handle_t UrKernel = nullptr;
14751475
auto Kernel = ExecCG.MSyclKernel;
14761476
auto KernelBundleImplPtr = ExecCG.MKernelBundle;
1477-
std::shared_ptr<sycl::detail::kernel_impl> SyclKernelImpl = nullptr;
14781477
const sycl::detail::KernelArgMask *EliminatedArgMask = nullptr;
14791478

14801479
if (auto SyclKernelImpl = KernelBundleImplPtr
1481-
? KernelBundleImplPtr->tryGetKernel(
1480+
? KernelBundleImplPtr->tryGetOfflineKernel(
14821481
ExecCG.MKernelName, KernelBundleImplPtr)
14831482
: std::shared_ptr<kernel_impl>{nullptr}) {
14841483
UrKernel = SyclKernelImpl->getHandleRef();

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ class kernel_bundle_impl {
537537
get_kernel(const kernel_id &KernelID,
538538
const std::shared_ptr<detail::kernel_bundle_impl> &Self) const {
539539
if (std::shared_ptr<kernel_impl> KernelImpl =
540-
tryGetOfflineKernel(KernelID, Self))
540+
tryGetOfflineKernelImpl(KernelID, Self))
541541
return detail::createSyclObjFromImpl<kernel>(std::move(KernelImpl));
542542
throw sycl::exception(make_error_code(errc::invalid),
543543
"The kernel bundle does not contain the kernel "
@@ -692,7 +692,7 @@ class kernel_bundle_impl {
692692
});
693693
}
694694

695-
std::shared_ptr<kernel_impl> tryGetOfflineKernel(
695+
std::shared_ptr<kernel_impl> tryGetOfflineKernelImpl(
696696
const kernel_id &KernelID,
697697
const std::shared_ptr<detail::kernel_bundle_impl> &Self) const {
698698
using ImageImpl = std::shared_ptr<detail::device_image_impl>;
@@ -758,6 +758,17 @@ class kernel_bundle_impl {
758758
SelectedImage->get_ur_program_ref(), CacheMutex);
759759
}
760760

761+
std::shared_ptr<kernel_impl>
762+
tryGetOfflineKernel(detail::KernelNameStrRefT Name,
763+
const std::shared_ptr<kernel_bundle_impl> &Self) const {
764+
// Fall back to regular offline compiled kernel_bundle look-up.
765+
if (std::optional<kernel_id> MaybeKernelID =
766+
sycl::detail::ProgramManager::getInstance().tryGetSYCLKernelID(
767+
Name))
768+
return tryGetOfflineKernelImpl(*MaybeKernelID, Self);
769+
return nullptr;
770+
}
771+
761772
std::shared_ptr<kernel_impl>
762773
tryGetKernel(detail::KernelNameStrRefT Name,
763774
const std::shared_ptr<kernel_bundle_impl> &Self) const {
@@ -774,11 +785,7 @@ class kernel_bundle_impl {
774785
}
775786

776787
// Fall back to regular offline compiled kernel_bundle look-up.
777-
if (std::optional<kernel_id> MaybeKernelID =
778-
sycl::detail::ProgramManager::getInstance().tryGetSYCLKernelID(
779-
Name))
780-
return tryGetOfflineKernel(*MaybeKernelID, Self);
781-
return nullptr;
788+
return tryGetOfflineKernel(Name, Self);
782789
}
783790

784791
private:

sycl/source/detail/scheduler/commands.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,8 +2523,8 @@ getCGKernelInfo(const CGExecKernel &CommandGroup, ContextImplPtr ContextImpl,
25232523

25242524
if (auto SyclKernelImpl =
25252525
KernelBundleImplPtr
2526-
? KernelBundleImplPtr->tryGetKernel(CommandGroup.MKernelName,
2527-
KernelBundleImplPtr)
2526+
? KernelBundleImplPtr->tryGetOfflineKernel(
2527+
CommandGroup.MKernelName, KernelBundleImplPtr)
25282528
: std::shared_ptr<kernel_impl>{nullptr}) {
25292529
UrKernel = SyclKernelImpl->getHandleRef();
25302530
DeviceImageImpl = SyclKernelImpl->getDeviceImage();

sycl/source/handler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,8 @@ event handler::finalize() {
445445
// Make sure implicit non-interop kernel bundles have the kernel
446446
if (!impl->isStateExplicitKernelBundle() &&
447447
!(MKernel && MKernel->isInterop()) &&
448-
(KernelBundleImpPtr->empty() ||
449-
KernelBundleImpPtr->hasSYCLOfflineImages()) &&
450-
!KernelBundleImpPtr->tryGetKernel(MKernelName.data(),
451-
KernelBundleImpPtr)) {
448+
!KernelBundleImpPtr->tryGetOfflineKernel(MKernelName.data(),
449+
KernelBundleImpPtr)) {
452450
auto Dev =
453451
impl->MGraph ? impl->MGraph->getDevice() : MQueue->get_device();
454452
kernel_id KernelID =

0 commit comments

Comments
 (0)