Skip to content

Commit d26cdad

Browse files
aelovikov-intelandrei.elovikov
and
andrei.elovikov
authored
[NFC][SYCL] Ensure context_impl is always created via std::make_shared (#18795)
And once that is guaranteed, inherit it from `std::enable_shared_from_this`. Further simplifications based on `[shared|weak]_from_this` are left to subsequent PRs. Earlier related PRs: #18715 same for `queue_impl` #18227 same for `device_impl` #18141 similar for `platform_impl` Co-authored-by: andrei.elovikov <aeloviko@smtp.intel.com>
1 parent dcb6b7b commit d26cdad

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

sycl/source/backend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ __SYCL_EXPORT context make_context(ur_native_handle_t NativeHandle,
112112
NativeHandle, Adapter->getUrAdapter(), DeviceHandles.size(),
113113
DeviceHandles.data(), &Properties, &UrContext);
114114
// Construct the SYCL context from UR context.
115-
return detail::createSyclObjFromImpl<context>(std::make_shared<context_impl>(
115+
return detail::createSyclObjFromImpl<context>(context_impl::create(
116116
UrContext, Handler, Adapter, DeviceList, !KeepOwnership));
117117
}
118118

sycl/source/context.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ context::context(const std::vector<device> &DeviceList,
6969
throw exception(make_error_code(errc::invalid),
7070
"Can't add devices across platforms to a single context.");
7171
else
72-
impl = std::make_shared<detail::context_impl>(DeviceList, AsyncHandler,
73-
PropList);
72+
impl = detail::context_impl::create(DeviceList, AsyncHandler, PropList);
7473
}
7574
context::context(cl_context ClContext, async_handler AsyncHandler) {
7675
const auto &Adapter = sycl::detail::ur::getAdapter<backend::opencl>();
@@ -81,8 +80,7 @@ context::context(cl_context ClContext, async_handler AsyncHandler) {
8180
Adapter->call<detail::UrApiKind::urContextCreateWithNativeHandle>(
8281
nativeHandle, Adapter->getUrAdapter(), 0, nullptr, nullptr, &hContext);
8382

84-
impl =
85-
std::make_shared<detail::context_impl>(hContext, AsyncHandler, Adapter);
83+
impl = detail::context_impl::create(hContext, AsyncHandler, Adapter);
8684
}
8785

8886
template <typename Param>

sycl/source/detail/context_impl.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,9 @@ namespace sycl {
2727
inline namespace _V1 {
2828
namespace detail {
2929

30-
context_impl::context_impl(const device &Device, async_handler AsyncHandler,
31-
const property_list &PropList)
32-
: MOwnedByRuntime(true), MAsyncHandler(AsyncHandler), MDevices(1, Device),
33-
MContext(nullptr),
34-
MPlatform(detail::getSyclObjImpl(Device.get_platform())),
35-
MPropList(PropList), MSupportBufferLocationByDevices(NotChecked) {
36-
verifyProps(PropList);
37-
MKernelProgramCache.setContextPtr(this);
38-
}
39-
4030
context_impl::context_impl(const std::vector<sycl::device> Devices,
4131
async_handler AsyncHandler,
42-
const property_list &PropList)
32+
const property_list &PropList, private_tag)
4333
: MOwnedByRuntime(true), MAsyncHandler(AsyncHandler), MDevices(Devices),
4434
MContext(nullptr),
4535
MPlatform(detail::getSyclObjImpl(MDevices[0].get_platform())),
@@ -72,7 +62,7 @@ context_impl::context_impl(ur_context_handle_t UrContext,
7262
async_handler AsyncHandler,
7363
const AdapterPtr &Adapter,
7464
const std::vector<sycl::device> &DeviceList,
75-
bool OwnedByRuntime)
65+
bool OwnedByRuntime, private_tag)
7666
: MOwnedByRuntime(OwnedByRuntime), MAsyncHandler(AsyncHandler),
7767
MDevices(DeviceList), MContext(UrContext), MPlatform(),
7868
MSupportBufferLocationByDevices(NotChecked) {

sycl/source/detail/context_impl.hpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,12 @@ inline namespace _V1 {
2929
// Forward declaration
3030
class device;
3131
namespace detail {
32-
class context_impl {
33-
public:
34-
/// Constructs a context_impl using a single SYCL devices.
35-
///
36-
/// The constructed context_impl will use the AsyncHandler parameter to
37-
/// handle exceptions.
38-
/// PropList carries the properties of the constructed context_impl.
39-
///
40-
/// \param Device is an instance of SYCL device.
41-
/// \param AsyncHandler is an instance of async_handler.
42-
/// \param PropList is an instance of property_list.
43-
context_impl(const device &Device, async_handler AsyncHandler,
44-
const property_list &PropList);
32+
class context_impl : std::enable_shared_from_this<context_impl> {
33+
struct private_tag {
34+
explicit private_tag() = default;
35+
};
4536

37+
public:
4638
/// Constructs a context_impl using a list of SYCL devices.
4739
///
4840
/// Newly created instance will save each SYCL device in the list. This
@@ -56,7 +48,8 @@ class context_impl {
5648
/// \param AsyncHandler is an instance of async_handler.
5749
/// \param PropList is an instance of property_list.
5850
context_impl(const std::vector<sycl::device> DeviceList,
59-
async_handler AsyncHandler, const property_list &PropList);
51+
async_handler AsyncHandler, const property_list &PropList,
52+
private_tag);
6053

6154
/// Construct a context_impl using plug-in interoperability handle.
6255
///
@@ -70,8 +63,23 @@ class context_impl {
7063
/// transferred to runtime
7164
context_impl(ur_context_handle_t UrContext, async_handler AsyncHandler,
7265
const AdapterPtr &Adapter,
73-
const std::vector<sycl::device> &DeviceList = {},
74-
bool OwnedByRuntime = true);
66+
const std::vector<sycl::device> &DeviceList, bool OwnedByRuntime,
67+
private_tag);
68+
69+
context_impl(ur_context_handle_t UrContext, async_handler AsyncHandler,
70+
const AdapterPtr &Adapter, private_tag tag)
71+
: context_impl(UrContext, AsyncHandler, Adapter,
72+
std::vector<sycl::device>{},
73+
/*OwnedByRuntime*/ true, tag) {}
74+
75+
// Single variadic method works because all the ctors are expected to be
76+
// "public" except the `private_tag` part restricting the creation to
77+
// `std::shared_ptr` allocations.
78+
template <typename... Ts>
79+
static std::shared_ptr<context_impl> create(Ts &&...args) {
80+
return std::make_shared<context_impl>(std::forward<Ts>(args)...,
81+
private_tag{});
82+
}
7583

7684
~context_impl();
7785

0 commit comments

Comments
 (0)