Skip to content

Commit 0659eb9

Browse files
[NFCI][SYCL] Prefer raw ptr/ref for queue_impl
Continuation of the refactoring in #18715 #18748
1 parent db06e61 commit 0659eb9

38 files changed

+360
-370
lines changed

sycl/source/detail/event_impl.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ event_impl::event_impl(ur_event_handle_t Event, const context &SyclContext)
159159
}
160160
}
161161

162-
event_impl::event_impl(const QueueImplPtr &Queue)
163-
: MQueue{Queue}, MIsProfilingEnabled{!Queue || Queue->MIsProfilingEnabled} {
162+
// TODO: split + setQueue?
163+
event_impl::event_impl(queue_impl *Queue)
164+
: MQueue{Queue ? Queue->weak_from_this() : std::weak_ptr<queue_impl>()},
165+
MIsProfilingEnabled{!Queue || Queue->MIsProfilingEnabled} {
164166
if (Queue)
165167
this->setContextImpl(Queue->getContextImplPtr());
166168
else {
@@ -212,7 +214,7 @@ void *event_impl::instrumentationProlog(std::string &Name, int32_t StreamID,
212214
// queue is available with the wait events. We check to see if the
213215
// TraceEvent is available in the Queue object.
214216
void *TraceEvent = nullptr;
215-
if (QueueImplPtr Queue = MQueue.lock()) {
217+
if (auto Queue = MQueue.lock()) {
216218
TraceEvent = Queue->getTraceEvent();
217219
WaitEvent =
218220
(TraceEvent ? static_cast<xpti_td *>(TraceEvent) : GSYCLGraphEvent);
@@ -281,7 +283,7 @@ void event_impl::wait_and_throw(
281283
std::shared_ptr<sycl::detail::event_impl> Self) {
282284
wait(Self);
283285

284-
if (QueueImplPtr SubmittedQueue = MSubmittedQueue.lock())
286+
if (auto SubmittedQueue = MSubmittedQueue.lock())
285287
SubmittedQueue->throw_asynchronous();
286288
}
287289

@@ -426,7 +428,7 @@ event_impl::get_backend_info<info::platform::version>() const {
426428
"the info::platform::version info descriptor can "
427429
"only be queried with an OpenCL backend");
428430
}
429-
if (QueueImplPtr Queue = MQueue.lock()) {
431+
if (auto Queue = MQueue.lock()) {
430432
return Queue->getDeviceImpl()
431433
.get_platform()
432434
.get_info<info::platform::version>();
@@ -449,7 +451,7 @@ event_impl::get_backend_info<info::device::version>() const {
449451
"the info::device::version info descriptor can only "
450452
"be queried with an OpenCL backend");
451453
}
452-
if (QueueImplPtr Queue = MQueue.lock()) {
454+
if (auto Queue = MQueue.lock()) {
453455
return Queue->getDeviceImpl().get_info<info::device::version>();
454456
}
455457
return ""; // If the queue has been released, no device will be associated so
@@ -516,21 +518,21 @@ std::vector<EventImplPtr> event_impl::getWaitList() {
516518
return Result;
517519
}
518520

519-
void event_impl::flushIfNeeded(const QueueImplPtr &UserQueue) {
521+
void event_impl::flushIfNeeded(queue_impl *UserQueue) {
520522
// Some events might not have a native handle underneath even at this point,
521523
// e.g. those produced by memset with 0 size (no UR call is made).
522524
auto Handle = this->getHandle();
523525
if (MIsFlushed || !Handle)
524526
return;
525527

526-
QueueImplPtr Queue = MQueue.lock();
528+
auto Queue = MQueue.lock();
527529
// If the queue has been released, all of the commands have already been
528530
// implicitly flushed by urQueueRelease.
529531
if (!Queue) {
530532
MIsFlushed = true;
531533
return;
532534
}
533-
if (Queue == UserQueue)
535+
if (Queue.get() == UserQueue)
534536
return;
535537

536538
// Check if the task for this event has already been submitted.
@@ -568,7 +570,7 @@ void event_impl::setSubmissionTime() {
568570
if (!MIsProfilingEnabled && !MProfilingTagEvent)
569571
return;
570572

571-
if (QueueImplPtr Queue = MQueue.lock()) {
573+
if (auto Queue = MQueue.lock()) {
572574
try {
573575
MSubmitTime = Queue->getDeviceImpl().getCurrentDeviceTime();
574576
} catch (sycl::exception &e) {

sycl/source/detail/event_impl.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class Adapter;
3131
class context_impl;
3232
using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
3333
class queue_impl;
34-
using QueueImplPtr = std::shared_ptr<sycl::detail::queue_impl>;
3534
class event_impl;
3635
using EventImplPtr = std::shared_ptr<sycl::detail::event_impl>;
3736

@@ -66,7 +65,7 @@ class event_impl {
6665
/// \param Event is a valid instance of UR event.
6766
/// \param SyclContext is an instance of SYCL context.
6867
event_impl(ur_event_handle_t Event, const context &SyclContext);
69-
event_impl(const QueueImplPtr &Queue);
68+
event_impl(queue_impl *Queue);
7069

7170
/// Sets a queue associated with the event
7271
///
@@ -209,7 +208,7 @@ class event_impl {
209208
/// Performs a flush on the queue associated with this event if the user queue
210209
/// is different and the task associated with this event hasn't been submitted
211210
/// to the device yet.
212-
void flushIfNeeded(const QueueImplPtr &UserQueue);
211+
void flushIfNeeded(queue_impl *UserQueue);
213212

214213
/// Cleans dependencies of this event_impl.
215214
void cleanupDependencyEvents();
@@ -229,7 +228,9 @@ class event_impl {
229228
///
230229
/// @return shared_ptr to MWorkerQueue, please be aware it can be empty
231230
/// pointer
232-
QueueImplPtr getWorkerQueue() { return MWorkerQueue.lock(); };
231+
std::shared_ptr<sycl::detail::queue_impl> getWorkerQueue() {
232+
return MWorkerQueue.lock();
233+
};
233234

234235
/// Sets worker queue for command.
235236
///
@@ -258,7 +259,9 @@ class event_impl {
258259
/// @return Submission time for command associated with this event
259260
uint64_t getSubmissionTime();
260261

261-
QueueImplPtr getSubmittedQueue() const { return MSubmittedQueue.lock(); };
262+
std::shared_ptr<sycl::detail::queue_impl> getSubmittedQueue() const {
263+
return MSubmittedQueue.lock();
264+
};
262265

263266
/// Checks if this event is complete.
264267
///

sycl/source/detail/graph_impl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ exec_graph_impl::enqueueNode(ur_exp_command_buffer_handle_t CommandBuffer,
897897

898898
sycl::detail::EventImplPtr Event =
899899
sycl::detail::Scheduler::getInstance().addCG(
900-
Node->getCGCopy(), MQueueImpl,
900+
Node->getCGCopy(), *MQueueImpl,
901901
/*EventNeeded=*/true, CommandBuffer, Deps);
902902

903903
if (MIsUpdatable) {
@@ -1037,8 +1037,7 @@ exec_graph_impl::enqueue(sycl::detail::queue_impl &Queue,
10371037
PartitionsExecutionEvents;
10381038

10391039
auto CreateNewEvent([&]() {
1040-
auto NewEvent =
1041-
std::make_shared<sycl::detail::event_impl>(Queue.shared_from_this());
1040+
auto NewEvent = std::make_shared<sycl::detail::event_impl>(&Queue);
10421041
NewEvent->setContextImpl(Queue.getContextImplPtr());
10431042
NewEvent->setStateIncomplete();
10441043
return NewEvent;
@@ -1122,7 +1121,7 @@ exec_graph_impl::enqueue(sycl::detail::queue_impl &Queue,
11221121
CommandBuffer, nullptr, std::move(CGData));
11231122

11241123
NewEvent = sycl::detail::Scheduler::getInstance().addCG(
1125-
std::move(CommandGroup), Queue.shared_from_this(),
1124+
std::move(CommandGroup), Queue,
11261125
/*EventNeeded=*/true);
11271126
}
11281127
NewEvent->setEventFromSubmittedExecCommandBuffer(true);
@@ -1142,7 +1141,7 @@ exec_graph_impl::enqueue(sycl::detail::queue_impl &Queue,
11421141
.MQueue = Queue.shared_from_this();
11431142

11441143
NewEvent = sycl::detail::Scheduler::getInstance().addCG(
1145-
NodeImpl->getCGCopy(), Queue.shared_from_this(),
1144+
NodeImpl->getCGCopy(), Queue,
11461145
/*EventNeeded=*/true);
11471146
}
11481147
PartitionsExecutionEvents[CurrentPartition] = NewEvent;
@@ -1424,7 +1423,7 @@ void exec_graph_impl::update(
14241423
// other scheduler commands
14251424
auto UpdateEvent =
14261425
sycl::detail::Scheduler::getInstance().addCommandGraphUpdate(
1427-
this, Nodes, MQueueImpl, std::move(UpdateRequirements),
1426+
this, Nodes, MQueueImpl.get(), std::move(UpdateRequirements),
14281427
MExecutionEvents);
14291428

14301429
MExecutionEvents.push_back(UpdateEvent);

sycl/source/detail/helpers.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class event;
2121
namespace detail {
2222
class CGExecKernel;
2323
class queue_impl;
24-
using QueueImplPtr = std::shared_ptr<sycl::detail::queue_impl>;
2524
class RTDeviceBinaryImage;
2625

2726
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES

sycl/source/detail/memory_manager.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class queue_impl;
2828
class event_impl;
2929
class context_impl;
3030

31-
using QueueImplPtr = std::shared_ptr<detail::queue_impl>;
3231
using EventImplPtr = std::shared_ptr<detail::event_impl>;
3332
using ContextImplPtr = std::shared_ptr<detail::context_impl>;
3433

sycl/source/detail/queue_impl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ queue_impl::get_backend_info<info::device::backend_version>() const {
114114
}
115115
#endif
116116

117-
static event prepareSYCLEventAssociatedWithQueue(
118-
const std::shared_ptr<detail::queue_impl> &QueueImpl) {
119-
auto EventImpl = std::make_shared<detail::event_impl>(QueueImpl);
120-
EventImpl->setContextImpl(detail::getSyclObjImpl(QueueImpl->get_context()));
117+
static event
118+
prepareSYCLEventAssociatedWithQueue(detail::queue_impl &QueueImpl) {
119+
auto EventImpl = std::make_shared<detail::event_impl>(&QueueImpl);
120+
EventImpl->setContextImpl(detail::getSyclObjImpl(QueueImpl.get_context()));
121121
EventImpl->setStateIncomplete();
122122
return detail::createSyclObjFromImpl<event>(EventImpl);
123123
}
@@ -462,7 +462,7 @@ event queue_impl::submitMemOpHelper(const std::vector<event> &DepEvents,
462462
return createDiscardedEvent();
463463
}
464464

465-
event ResEvent = prepareSYCLEventAssociatedWithQueue(shared_from_this());
465+
event ResEvent = prepareSYCLEventAssociatedWithQueue(*this);
466466
const auto &EventImpl = detail::getSyclObjImpl(ResEvent);
467467
{
468468
NestedCallsTracker tracker;

sycl/source/detail/queue_impl.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,12 @@ class queue_impl : public std::enable_shared_from_this<queue_impl> {
648648
// for in order ones.
649649
void revisitUnenqueuedCommandsState(const EventImplPtr &CompletedHostTask);
650650

651-
static ContextImplPtr getContext(const QueueImplPtr &Queue) {
651+
static ContextImplPtr getContext(queue_impl *Queue) {
652652
return Queue ? Queue->getContextImplPtr() : nullptr;
653653
}
654+
static ContextImplPtr getContext(const std::shared_ptr<queue_impl> &Queue) {
655+
return getContext(Queue.get());
656+
}
654657

655658
// Must be called under MMutex protection
656659
void doUnenqueuedCommandCleanup(
@@ -663,7 +666,7 @@ class queue_impl : public std::enable_shared_from_this<queue_impl> {
663666
/// will wait for the completion of all work in the queue at the time of the
664667
/// insertion, but will not act as a barrier unless the queue is in-order.
665668
EventImplPtr insertMarkerEvent() {
666-
auto ResEvent = std::make_shared<detail::event_impl>(shared_from_this());
669+
auto ResEvent = std::make_shared<detail::event_impl>(this);
667670
ur_event_handle_t UREvent = nullptr;
668671
getAdapter()->call<UrApiKind::urEnqueueEventsWait>(getHandleRef(), 0,
669672
nullptr, &UREvent);
@@ -687,9 +690,8 @@ class queue_impl : public std::enable_shared_from_this<queue_impl> {
687690
protected:
688691
template <typename HandlerType = handler>
689692
EventImplPtr insertHelperBarrier(const HandlerType &Handler) {
690-
auto &Queue = Handler.impl->get_queue();
691-
auto ResEvent =
692-
std::make_shared<detail::event_impl>(Queue.shared_from_this());
693+
queue_impl &Queue = Handler.impl->get_queue();
694+
auto ResEvent = std::make_shared<detail::event_impl>(&Queue);
693695
ur_event_handle_t UREvent = nullptr;
694696
getAdapter()->call<UrApiKind::urEnqueueEventsWaitWithBarrier>(
695697
Queue.getHandleRef(), 0, nullptr, &UREvent);

sycl/source/detail/reduction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ __SYCL_EXPORT size_t reduGetPreferredWGSize(std::shared_ptr<queue_impl> &Queue,
207207
__SYCL_EXPORT void
208208
addCounterInit(handler &CGH, std::shared_ptr<sycl::detail::queue_impl> &Queue,
209209
std::shared_ptr<int> &Counter) {
210-
auto EventImpl = std::make_shared<detail::event_impl>(Queue);
210+
auto EventImpl = std::make_shared<detail::event_impl>(Queue.get());
211211
EventImpl->setContextImpl(detail::getSyclObjImpl(Queue->get_context()));
212212
EventImpl->setStateIncomplete();
213213
ur_event_handle_t UREvent = nullptr;

0 commit comments

Comments
 (0)