Skip to content

[NFC][SYCL] Prefer context_impl & as locals/temporaries #19148

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 1 commit into from
Jun 26, 2025
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: 2 additions & 0 deletions sycl/include/sycl/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3545,7 +3545,9 @@ class __SYCL_EXPORT handler {
UserRange, KernelFunc};
}

#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
const std::shared_ptr<detail::context_impl> &getContextImplPtr() const;
#endif
detail::context_impl &getContextImpl() const;

// Checks if 2D memory operations are supported by the underlying platform.
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/detail/async_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void *async_malloc(sycl::handler &h, sycl::usm::alloc kind, size_t size) {
sycl::make_error_code(sycl::errc::feature_not_supported),
"Only device backed asynchronous allocations are supported!");

auto &Adapter = h.getContextImplPtr()->getAdapter();
auto &Adapter = h.getContextImpl().getAdapter();

// Get CG event dependencies for this allocation.
const auto &DepEvents = h.impl->CGData.MEvents;
Expand Down Expand Up @@ -118,7 +118,7 @@ __SYCL_EXPORT void *async_malloc(const sycl::queue &q, sycl::usm::alloc kind,
__SYCL_EXPORT void *async_malloc_from_pool(sycl::handler &h, size_t size,
const memory_pool &pool) {

auto &Adapter = h.getContextImplPtr()->getAdapter();
auto &Adapter = h.getContextImpl().getAdapter();
auto &memPoolImpl = sycl::detail::getSyclObjImpl(pool);

// Get CG event dependencies for this allocation.
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/backend_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ inline namespace _V1 {
namespace detail {

template <class T> backend getImplBackend(const T &Impl) {
return Impl->getContextImplPtr()->getBackend();
return Impl->getContextImpl().getBackend();
}

} // namespace detail
Expand Down
26 changes: 13 additions & 13 deletions sycl/source/detail/bindless_images.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,9 @@ get_image_memory_support(const image_descriptor &imageDescriptor,
const sycl::context &syclContext) {
std::shared_ptr<sycl::detail::device_impl> DevImpl =
sycl::detail::getSyclObjImpl(syclDevice);
std::shared_ptr<sycl::detail::context_impl> CtxImpl =
sycl::detail::getSyclObjImpl(syclContext);
const sycl::detail::AdapterPtr &Adapter = CtxImpl->getAdapter();
sycl::detail::context_impl &CtxImpl =
*sycl::detail::getSyclObjImpl(syclContext);
const sycl::detail::AdapterPtr &Adapter = CtxImpl.getAdapter();

ur_image_desc_t urDesc;
ur_image_format_t urFormat;
Expand All @@ -825,15 +825,15 @@ get_image_memory_support(const image_descriptor &imageDescriptor,
Adapter->call<sycl::errc::runtime,
sycl::detail::UrApiKind::
urBindlessImagesGetImageMemoryHandleTypeSupportExp>(
CtxImpl->getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
CtxImpl.getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
ur_exp_image_mem_type_t::UR_EXP_IMAGE_MEM_TYPE_USM_POINTER,
&supportsPointerAllocation);

ur_bool_t supportsOpaqueAllocation{0};
Adapter->call<sycl::errc::runtime,
sycl::detail::UrApiKind::
urBindlessImagesGetImageMemoryHandleTypeSupportExp>(
CtxImpl->getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
CtxImpl.getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
ur_exp_image_mem_type_t::UR_EXP_IMAGE_MEM_TYPE_OPAQUE_HANDLE,
&supportsOpaqueAllocation);

Expand Down Expand Up @@ -864,9 +864,9 @@ __SYCL_EXPORT bool is_image_handle_supported<unsampled_image_handle>(
const sycl::device &syclDevice, const sycl::context &syclContext) {
std::shared_ptr<sycl::detail::device_impl> DevImpl =
sycl::detail::getSyclObjImpl(syclDevice);
std::shared_ptr<sycl::detail::context_impl> CtxImpl =
sycl::detail::getSyclObjImpl(syclContext);
const sycl::detail::AdapterPtr &Adapter = CtxImpl->getAdapter();
sycl::detail::context_impl &CtxImpl =
*sycl::detail::getSyclObjImpl(syclContext);
const sycl::detail::AdapterPtr &Adapter = CtxImpl.getAdapter();

ur_image_desc_t urDesc;
ur_image_format_t urFormat;
Expand All @@ -881,7 +881,7 @@ __SYCL_EXPORT bool is_image_handle_supported<unsampled_image_handle>(
Adapter->call<sycl::errc::runtime,
sycl::detail::UrApiKind::
urBindlessImagesGetImageUnsampledHandleSupportExp>(
CtxImpl->getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
CtxImpl.getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
memHandleType, &supportsUnsampledHandle);

return supportsUnsampledHandle;
Expand All @@ -904,9 +904,9 @@ __SYCL_EXPORT bool is_image_handle_supported<sampled_image_handle>(
const sycl::device &syclDevice, const sycl::context &syclContext) {
std::shared_ptr<sycl::detail::device_impl> DevImpl =
sycl::detail::getSyclObjImpl(syclDevice);
std::shared_ptr<sycl::detail::context_impl> CtxImpl =
sycl::detail::getSyclObjImpl(syclContext);
const sycl::detail::AdapterPtr &Adapter = CtxImpl->getAdapter();
sycl::detail::context_impl &CtxImpl =
*sycl::detail::getSyclObjImpl(syclContext);
const sycl::detail::AdapterPtr &Adapter = CtxImpl.getAdapter();

ur_image_desc_t urDesc;
ur_image_format_t urFormat;
Expand All @@ -921,7 +921,7 @@ __SYCL_EXPORT bool is_image_handle_supported<sampled_image_handle>(
Adapter->call<
sycl::errc::runtime,
sycl::detail::UrApiKind::urBindlessImagesGetImageSampledHandleSupportExp>(
CtxImpl->getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
CtxImpl.getHandleRef(), DevImpl->getHandleRef(), &urDesc, &urFormat,
memHandleType, &supportsSampledHandle);

return supportsSampledHandle;
Expand Down
6 changes: 3 additions & 3 deletions sycl/source/detail/device_image_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,13 @@ class device_image_impl {

ur_native_handle_t getNative() const {
assert(MProgram);
const auto &ContextImplPtr = detail::getSyclObjImpl(MContext);
const AdapterPtr &Adapter = ContextImplPtr->getAdapter();
context_impl &ContextImpl = *detail::getSyclObjImpl(MContext);
const AdapterPtr &Adapter = ContextImpl.getAdapter();

ur_native_handle_t NativeProgram = 0;
Adapter->call<UrApiKind::urProgramGetNativeHandle>(MProgram,
&NativeProgram);
if (ContextImplPtr->getBackend() == backend::opencl)
if (ContextImpl.getBackend() == backend::opencl)
__SYCL_OCL_CALL(clRetainProgram, ur::cast<cl_program>(NativeProgram));

return NativeProgram;
Expand Down
6 changes: 0 additions & 6 deletions sycl/source/detail/event_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,6 @@ class event_impl {

bool isDefaultConstructed() const noexcept { return MIsDefaultConstructed; }

ContextImplPtr getContextImplPtr() {
if (MIsDefaultConstructed)
initContextIfNeeded();
return MContext;
}

// Sets a sync point which is used when this event represents an enqueue to a
// Command Buffer.
void setSyncPoint(ur_exp_command_buffer_sync_point_t SyncPoint) {
Expand Down
5 changes: 5 additions & 0 deletions sycl/source/detail/graph/graph_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,16 @@ class graph_impl : public std::enable_shared_from_this<graph_impl> {
/// @return Context associated with graph.
sycl::context getContext() const { return MContext; }

#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
/// Query for the context impl tied to this graph.
/// @return shared_ptr ref for the context impl associated with graph.
const std::shared_ptr<sycl::detail::context_impl> &getContextImplPtr() const {
return sycl::detail::getSyclObjImpl(MContext);
}
#endif
sycl::detail::context_impl &getContextImpl() const {
return *sycl::detail::getSyclObjImpl(MContext);
}

/// Query for the device_impl tied to this graph.
/// @return device_impl shared ptr reference associated with graph.
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/detail/handler_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ class handler_impl {
template <typename Self = handler_impl> context_impl &get_context() {
Self *self = this;
if (auto *Queue = self->get_queue_or_null())
return *Queue->getContextImplPtr();
return Queue->getContextImpl();
else
return *self->get_graph().getContextImplPtr();
return self->get_graph().getContextImpl();
}

/// If we are submitting a graph using ext_oneapi_graph this will be the graph
Expand Down
11 changes: 5 additions & 6 deletions sycl/source/detail/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

namespace sycl {
inline namespace _V1 {
using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
namespace detail {
void waitEvents(std::vector<sycl::event> DepEvents) {
for (auto SyclEvent : DepEvents) {
Expand Down Expand Up @@ -59,10 +58,10 @@ retrieveKernelBinary(queue_impl &Queue, KernelNameStrRefT KernelName,
if (DeviceImage == DeviceImages.end()) {
return {nullptr, nullptr};
}
auto ContextImpl = Queue.getContextImplPtr();
context_impl &ContextImpl = Queue.getContextImpl();
ur_program_handle_t Program =
detail::ProgramManager::getInstance().createURProgram(
**DeviceImage, *ContextImpl, {createSyclObjFromImpl<device>(Dev)});
**DeviceImage, ContextImpl, {createSyclObjFromImpl<device>(Dev)});
return {*DeviceImage, Program};
}

Expand All @@ -79,11 +78,11 @@ retrieveKernelBinary(queue_impl &Queue, KernelNameStrRefT KernelName,
DeviceImage = SyclKernelImpl->getDeviceImage()->get_bin_image_ref();
Program = SyclKernelImpl->getDeviceImage()->get_ur_program_ref();
} else {
auto ContextImpl = Queue.getContextImplPtr();
context_impl &ContextImpl = Queue.getContextImpl();
DeviceImage = &detail::ProgramManager::getInstance().getDeviceImage(
KernelName, *ContextImpl, Dev);
KernelName, ContextImpl, Dev);
Program = detail::ProgramManager::getInstance().createURProgram(
*DeviceImage, *ContextImpl, {createSyclObjFromImpl<device>(Dev)});
*DeviceImage, ContextImpl, {createSyclObjFromImpl<device>(Dev)});
}
return {DeviceImage, Program};
}
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/kernel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class kernel_impl {
bool isInterop() const { return MIsInterop; }

ur_program_handle_t getProgramRef() const { return MProgram; }
ContextImplPtr getContextImplPtr() const { return MContext; }
context_impl &getContextImpl() const { return *MContext; }

std::mutex &getNoncacheableEnqueueMutex() const {
return MNoncacheableEnqueueMutex;
Expand Down
8 changes: 4 additions & 4 deletions sycl/source/detail/queue_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ template <> device queue_impl::get_info<info::queue::device>() const {
template <>
typename info::platform::version::return_type
queue_impl::get_backend_info<info::platform::version>() const {
if (getContextImplPtr()->getBackend() != backend::opencl) {
if (getContextImpl().getBackend() != backend::opencl) {
throw sycl::exception(errc::backend_mismatch,
"the info::platform::version info descriptor can "
"only be queried with an OpenCL backend");
Expand All @@ -93,7 +93,7 @@ queue_impl::get_backend_info<info::platform::version>() const {
template <>
typename info::device::version::return_type
queue_impl::get_backend_info<info::device::version>() const {
if (getContextImplPtr()->getBackend() != backend::opencl) {
if (getContextImpl().getBackend() != backend::opencl) {
throw sycl::exception(errc::backend_mismatch,
"the info::device::version info descriptor can only "
"be queried with an OpenCL backend");
Expand All @@ -106,7 +106,7 @@ queue_impl::get_backend_info<info::device::version>() const {
template <>
typename info::device::backend_version::return_type
queue_impl::get_backend_info<info::device::backend_version>() const {
if (getContextImplPtr()->getBackend() != backend::ext_oneapi_level_zero) {
if (getContextImpl().getBackend() != backend::ext_oneapi_level_zero) {
throw sycl::exception(errc::backend_mismatch,
"the info::device::backend_version info descriptor "
"can only be queried with a Level Zero backend");
Expand Down Expand Up @@ -734,7 +734,7 @@ ur_native_handle_t queue_impl::getNative(int32_t &NativeHandleDesc) const {

Adapter->call<UrApiKind::urQueueGetNativeHandle>(MQueue, &UrNativeDesc,
&Handle);
if (getContextImplPtr()->getBackend() == backend::opencl)
if (getContextImpl().getBackend() == backend::opencl)
__SYCL_OCL_CALL(clRetainCommandQueue, ur::cast<cl_command_queue>(Handle));

return Handle;
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ void instrumentationAddExtraKernelMetadata(
// by graph API, when a modifiable graph is finalized.
FastKernelCacheValPtr FastKernelCacheVal =
detail::ProgramManager::getInstance().getOrCreateKernel(
*Queue->getContextImplPtr(), Queue->getDeviceImpl(), KernelName,
Queue->getContextImpl(), Queue->getDeviceImpl(), KernelName,
KernelNameBasedCachePtr);
EliminatedArgMask = FastKernelCacheVal->MKernelArgMask;
}
Expand Down
6 changes: 4 additions & 2 deletions sycl/source/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ void handler::depends_on(const detail::EventImplPtr &EventImpl) {
if (Queue && EventGraph) {
auto QueueGraph = Queue->getCommandGraph();

if (EventGraph->getContextImplPtr().get() != &impl->get_context()) {
if (&EventGraph->getContextImpl() != &impl->get_context()) {
throw sycl::exception(
make_error_code(errc::invalid),
"Cannot submit to a queue with a dependency from a graph that is "
Expand Down Expand Up @@ -2213,17 +2213,19 @@ void handler::memcpyFromHostOnlyDeviceGlobal(void *Dest,
});
}

#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
const std::shared_ptr<detail::context_impl> &
handler::getContextImplPtr() const {
if (auto *Graph = impl->get_graph_or_null()) {
return Graph->getContextImplPtr();
}
return impl->get_queue().getContextImplPtr();
}
#endif

detail::context_impl &handler::getContextImpl() const {
if (auto *Graph = impl->get_graph_or_null()) {
return *Graph->getContextImplPtr();
return Graph->getContextImpl();
}
return impl->get_queue().getContextImpl();
}
Expand Down