diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 5806b9ba70d3..25b96c80e41d 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -330,6 +330,22 @@ void shutdown_early(bool CanJoinThreads = true) { GlobalHandler::RTGlobalObjHandler->MHostTaskThreadPool.Inst.reset(nullptr); } + // Reset in-memory cache before releasing default contexts. + { + // Keeping the default context for platforms in the global cache to avoid + // shared_ptr based circular dependency between platform and context classes + std::lock_guard Lock{ + GlobalHandler::RTGlobalObjHandler + ->getPlatformToDefaultContextCacheMutex()}; + + auto &PlatformToDefaultContextCache = + GlobalHandler::RTGlobalObjHandler->getPlatformToDefaultContextCache(); + + for (auto &Pair : PlatformToDefaultContextCache) { + Pair.second->getKernelProgramCache().reset(); + } + } + // This releases OUR reference to the default context, but // other may yet have refs GlobalHandler::RTGlobalObjHandler->releaseDefaultContexts(); diff --git a/sycl/source/detail/kernel_program_cache.hpp b/sycl/source/detail/kernel_program_cache.hpp index 55919b74a6e3..04930b6edb55 100644 --- a/sycl/source/detail/kernel_program_cache.hpp +++ b/sycl/source/detail/kernel_program_cache.hpp @@ -573,9 +573,8 @@ class KernelProgramCache { traceProgram("Program evicted.", CacheKey); } else - // This should never happen. - throw sycl::exception(sycl::make_error_code(sycl::errc::runtime), - "Program not found in the cache."); + // This can happen if we try to remove cache entries after early shutdown. + return 0; return MCachedPrograms.ProgramCacheSizeInBytes; } @@ -684,9 +683,9 @@ class KernelProgramCache { std::lock_guard L1(MProgramCacheMutex); std::lock_guard L2(MKernelsPerProgramCacheMutex); FastKernelCacheWriteLockT L3(MFastKernelCacheMutex); - MCachedPrograms = ProgramCache{}; - MKernelsPerProgramCache = KernelCacheT{}; - MFastKernelCache = FastKernelCacheT{}; + MCachedPrograms.Cache.clear(); + MKernelsPerProgramCache.clear(); + MFastKernelCache.clear(); MProgramToFastKernelCacheKeyMap.clear(); // Clear the eviction lists and its mutexes. MEvictionList.clear();