diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index fbe8a8aef8005..f7c225b5eb35c 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -168,6 +168,8 @@ Scheduler &GlobalHandler::getScheduler() { return *MScheduler.Inst; } +bool GlobalHandler::isSchedulerAlive() const { return MScheduler.Inst.get(); } + void GlobalHandler::registerSchedulerUsage(bool ModifyCounter) { thread_local ObjectUsageCounter SchedulerCounter(ModifyCounter); } diff --git a/sycl/source/detail/global_handler.hpp b/sycl/source/detail/global_handler.hpp index d10decf8f79b9..ffa334d691027 100644 --- a/sycl/source/detail/global_handler.hpp +++ b/sycl/source/detail/global_handler.hpp @@ -58,6 +58,7 @@ class GlobalHandler { void registerSchedulerUsage(bool ModifyCounter = true); Scheduler &getScheduler(); + bool isSchedulerAlive() const; ProgramManager &getProgramManager(); Sync &getSync(); std::vector &getPlatformCache(); diff --git a/sycl/source/detail/queue_impl.cpp b/sycl/source/detail/queue_impl.cpp index ce4dd83609ab9..2078e0da9cadc 100644 --- a/sycl/source/detail/queue_impl.cpp +++ b/sycl/source/detail/queue_impl.cpp @@ -559,7 +559,9 @@ pi_native_handle queue_impl::getNative(int32_t &NativeHandleDesc) const { } void queue_impl::cleanup_fusion_cmd() { - detail::Scheduler::getInstance().cleanUpCmdFusion(this); + // Clean up only if a scheduler instance exits. + if (detail::Scheduler::isInstanceAlive()) + detail::Scheduler::getInstance().cleanUpCmdFusion(this); } bool queue_impl::ext_oneapi_empty() const { diff --git a/sycl/source/detail/scheduler/scheduler.cpp b/sycl/source/detail/scheduler/scheduler.cpp index 3cda9aed93c1d..5656bf8e3cf8f 100644 --- a/sycl/source/detail/scheduler/scheduler.cpp +++ b/sycl/source/detail/scheduler/scheduler.cpp @@ -261,6 +261,10 @@ Scheduler &Scheduler::getInstance() { return GlobalHandler::instance().getScheduler(); } +bool Scheduler::isInstanceAlive() { + return GlobalHandler::instance().isSchedulerAlive(); +} + void Scheduler::waitForEvent(const EventImplPtr &Event) { ReadLockT Lock = acquireReadLock(); // It's fine to leave the lock unlocked upon return from waitForEvent as diff --git a/sycl/source/detail/scheduler/scheduler.hpp b/sycl/source/detail/scheduler/scheduler.hpp index 6c872abc4ef7a..4619f2ac2b4f8 100644 --- a/sycl/source/detail/scheduler/scheduler.hpp +++ b/sycl/source/detail/scheduler/scheduler.hpp @@ -438,6 +438,8 @@ class Scheduler { /// \return an instance of the scheduler object. static Scheduler &getInstance(); + /// \return true if an instance of the scheduler object exists. + static bool isInstanceAlive(); QueueImplPtr getDefaultHostQueue() { return DefaultHostQueue; }