Skip to content

Commit 5dfc457

Browse files
[SYCL] Do not lock unconditionally while access queue_iml members
queue_impl::MMissedCleanupRequests and queue_impl::MInOrderExternalEvent are empty on hot path, check a flag instead or before the locking.
1 parent 7f2971a commit 5dfc457

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

sycl/source/detail/queue_impl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,9 @@ void queue_impl::wait(const detail::code_location &CodeLoc) {
618618
WeakEvents.swap(MEventsWeak);
619619
SharedEvents.swap(MEventsShared);
620620

621-
{
621+
if (MAreCleanupRequestsMissed.load(std::memory_order_acquire)) {
622622
std::lock_guard<std::mutex> RequestLock(MMissedCleanupRequestsMtx);
623+
MAreCleanupRequestsMissed.store(false, std::memory_order_release);
623624
for (auto &UpdatedGraph : MMissedCleanupRequests)
624625
doUnenqueuedCommandCleanup(UpdatedGraph);
625626
MMissedCleanupRequests.clear();
@@ -801,6 +802,7 @@ void queue_impl::revisitUnenqueuedCommandsState(
801802
else {
802803
std::lock_guard<std::mutex> RequestLock(MMissedCleanupRequestsMtx);
803804
MMissedCleanupRequests.push_back(CompletedHostTask->getCommandGraph());
805+
MAreCleanupRequestsMissed.store(true, std::memory_order_release);
804806
}
805807
}
806808

sycl/source/detail/queue_impl.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,18 @@ class queue_impl {
711711

712712
void setExternalEvent(const event &Event) {
713713
std::lock_guard<std::mutex> Lock(MInOrderExternalEventMtx);
714+
MHasValueInOrderExternalEvent.store(true, std::memory_order_release);
714715
MInOrderExternalEvent = Event;
715716
}
716717

717718
std::optional<event> popExternalEvent() {
718-
std::lock_guard<std::mutex> Lock(MInOrderExternalEventMtx);
719719
std::optional<event> Result = std::nullopt;
720-
std::swap(Result, MInOrderExternalEvent);
720+
721+
if (MHasValueInOrderExternalEvent.load(std::memory_order_acquire)) {
722+
std::lock_guard<std::mutex> Lock(MInOrderExternalEventMtx);
723+
MHasValueInOrderExternalEvent.store(false, std::memory_order_release);
724+
std::swap(Result, MInOrderExternalEvent);
725+
}
721726
return Result;
722727
}
723728

@@ -833,8 +838,10 @@ class queue_impl {
833838
// dependency so in the case where some commands were not enqueued
834839
// (blocked), we track them to prevent barrier from being enqueued
835840
// earlier.
841+
if (MAreCleanupRequestsMissed.load(std::memory_order_acquire))
836842
{
837843
std::lock_guard<std::mutex> RequestLock(MMissedCleanupRequestsMtx);
844+
MAreCleanupRequestsMissed.store(false, std::memory_order_release);
838845
for (auto &UpdatedGraph : MMissedCleanupRequests)
839846
doUnenqueuedCommandCleanup(UpdatedGraph);
840847
MMissedCleanupRequests.clear();
@@ -850,12 +857,12 @@ class queue_impl {
850857
auto EventRet = Handler.finalize();
851858
EventImplPtr EventRetImpl = getSyclObjImpl(EventRet);
852859
if (Type == CGType::CodeplayHostTask)
853-
Deps.UnenqueuedCmdEvents.push_back(EventRetImpl);
860+
Deps.UnenqueuedCmdEvents.push_back(std::move(EventRetImpl));
854861
else if (Type == CGType::Barrier || Type == CGType::BarrierWaitlist) {
855-
Deps.LastBarrier = EventRetImpl;
862+
Deps.LastBarrier = std::move(EventRetImpl);
856863
Deps.UnenqueuedCmdEvents.clear();
857864
} else if (!EventRetImpl->isEnqueued()) {
858-
Deps.UnenqueuedCmdEvents.push_back(EventRetImpl);
865+
Deps.UnenqueuedCmdEvents.push_back(std::move(EventRetImpl));
859866
}
860867

861868
return EventRet;
@@ -1040,6 +1047,8 @@ class queue_impl {
10401047
// the fallback implementation of profiling info
10411048
bool MFallbackProfiling = false;
10421049

1050+
// Is value presented in MInOrderExternalEvent?
1051+
std::atomic_bool MHasValueInOrderExternalEvent = false;
10431052
// This event can be optionally provided by users for in-order queues to add
10441053
// an additional dependency for the subsequent submission in to the queue.
10451054
// Access to the event should be guarded with MInOrderExternalEventMtx.
@@ -1071,6 +1080,7 @@ class queue_impl {
10711080
std::deque<std::shared_ptr<ext::oneapi::experimental::detail::graph_impl>>
10721081
MMissedCleanupRequests;
10731082
std::mutex MMissedCleanupRequestsMtx;
1083+
std::atomic_bool MAreCleanupRequestsMissed = false;
10741084

10751085
friend class sycl::ext::oneapi::experimental::detail::node_impl;
10761086

0 commit comments

Comments
 (0)