Skip to content

[NFC][SYCL] Pass context_impl by raw ptr/ref in kernel_impl.hpp #18979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ kernel make_kernel(const context &TargetContext,

// Construct the SYCL queue from UR queue.
return detail::createSyclObjFromImpl<kernel>(
std::make_shared<kernel_impl>(UrKernel, ContextImpl, KernelBundleImpl));
std::make_shared<kernel_impl>(UrKernel, *ContextImpl, KernelBundleImpl));
}

kernel make_kernel(ur_native_handle_t NativeHandle,
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/detail/device_image_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ class device_image_impl {
auto [UrKernel, CacheMutex, ArgMask] =
PM.getOrCreateKernel(Context, AdjustedName,
/*PropList=*/{}, UrProgram);
return std::make_shared<kernel_impl>(UrKernel, getSyclObjImpl(Context),
return std::make_shared<kernel_impl>(UrKernel, *getSyclObjImpl(Context),
Self, OwnerBundle, ArgMask,
UrProgram, CacheMutex);
}
Expand All @@ -653,7 +653,7 @@ class device_image_impl {
// Kernel created by urKernelCreate is implicitly retained.

return std::make_shared<kernel_impl>(
UrKernel, detail::getSyclObjImpl(Context), Self, OwnerBundle,
UrKernel, *detail::getSyclObjImpl(Context), Self, OwnerBundle,
/*ArgMask=*/nullptr, UrProgram, /*CacheMutex=*/nullptr);
}

Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/kernel_bundle_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ class kernel_bundle_impl {
SelectedImage->get_ur_program_ref());

return std::make_shared<kernel_impl>(
Kernel, detail::getSyclObjImpl(MContext), SelectedImage, Self, ArgMask,
Kernel, *detail::getSyclObjImpl(MContext), SelectedImage, Self, ArgMask,
SelectedImage->get_ur_program_ref(), CacheMutex);
}

Expand Down
15 changes: 8 additions & 7 deletions sycl/source/detail/kernel_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ namespace sycl {
inline namespace _V1 {
namespace detail {

kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, ContextImplPtr Context,
kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, context_impl &Context,
KernelBundleImplPtr KernelBundleImpl,
const KernelArgMask *ArgMask)
: MKernel(Kernel), MContext(Context),
MProgram(ProgramManager::getInstance().getUrProgramFromUrKernel(
Kernel, *Context)),
: MKernel(Kernel), MContext(Context.shared_from_this()),
MProgram(ProgramManager::getInstance().getUrProgramFromUrKernel(Kernel,
Context)),
MCreatedFromSource(true), MKernelBundleImpl(std::move(KernelBundleImpl)),
MIsInterop(true), MKernelArgMaskPtr{ArgMask} {
ur_context_handle_t UrContext = nullptr;
// Using the adapter from the passed ContextImpl
getAdapter()->call<UrApiKind::urKernelGetInfo>(
MKernel, UR_KERNEL_INFO_CONTEXT, sizeof(UrContext), &UrContext, nullptr);
if (Context->getHandleRef() != UrContext)
if (Context.getHandleRef() != UrContext)
throw sycl::exception(
make_error_code(errc::invalid),
"Input context must be the same as the context of cl_kernel");
Expand All @@ -37,12 +37,13 @@ kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, ContextImplPtr Context,
enableUSMIndirectAccess();
}

kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, ContextImplPtr ContextImpl,
kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, context_impl &ContextImpl,
DeviceImageImplPtr DeviceImageImpl,
KernelBundleImplPtr KernelBundleImpl,
const KernelArgMask *ArgMask,
ur_program_handle_t Program, std::mutex *CacheMutex)
: MKernel(Kernel), MContext(std::move(ContextImpl)), MProgram(Program),
: MKernel(Kernel), MContext(ContextImpl.shared_from_this()),
MProgram(Program),
MCreatedFromSource(DeviceImageImpl->isNonSYCLSourceBased()),
MDeviceImageImpl(std::move(DeviceImageImpl)),
MKernelBundleImpl(std::move(KernelBundleImpl)),
Expand Down
7 changes: 3 additions & 4 deletions sycl/source/detail/kernel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace detail {
// Forward declaration
class kernel_bundle_impl;

using ContextImplPtr = std::shared_ptr<context_impl>;
using KernelBundleImplPtr = std::shared_ptr<kernel_bundle_impl>;
class kernel_impl {
public:
Expand All @@ -40,7 +39,7 @@ class kernel_impl {
/// \param Kernel is a valid UrKernel instance
/// \param Context is a valid SYCL context
/// \param KernelBundleImpl is a valid instance of kernel_bundle_impl
kernel_impl(ur_kernel_handle_t Kernel, ContextImplPtr Context,
kernel_impl(ur_kernel_handle_t Kernel, context_impl &Context,
KernelBundleImplPtr KernelBundleImpl,
const KernelArgMask *ArgMask = nullptr);

Expand All @@ -50,7 +49,7 @@ class kernel_impl {
/// \param Kernel is a valid UrKernel instance
/// \param ContextImpl is a valid SYCL context
/// \param KernelBundleImpl is a valid instance of kernel_bundle_impl
kernel_impl(ur_kernel_handle_t Kernel, ContextImplPtr ContextImpl,
kernel_impl(ur_kernel_handle_t Kernel, context_impl &ContextImpl,
DeviceImageImplPtr DeviceImageImpl,
KernelBundleImplPtr KernelBundleImpl,
const KernelArgMask *ArgMask, ur_program_handle_t Program,
Expand Down Expand Up @@ -245,7 +244,7 @@ class kernel_impl {

private:
ur_kernel_handle_t MKernel = nullptr;
const ContextImplPtr MContext;
const std::shared_ptr<context_impl> MContext;
const ur_program_handle_t MProgram = nullptr;
bool MCreatedFromSource = true;
const DeviceImageImplPtr MDeviceImageImpl;
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ kernel::kernel(cl_kernel ClKernel, const context &SyclContext) {
nativeHandle, detail::getSyclObjImpl(SyclContext)->getHandleRef(),
nullptr, nullptr, &hKernel);
impl = std::make_shared<detail::kernel_impl>(
hKernel, detail::getSyclObjImpl(SyclContext), nullptr, nullptr);
hKernel, *detail::getSyclObjImpl(SyclContext), nullptr, nullptr);
// This is a special interop constructor for OpenCL, so the kernel must be
// retained.
if (get_backend() == backend::opencl) {
Expand Down