@@ -264,9 +264,7 @@ ur_result_t ContextReleaseHelper(ur_context_handle_t Context) {
264264 Contexts.erase (It);
265265 }
266266 ze_context_handle_t DestroyZeContext =
267- ((Context->OwnNativeHandle && !Context->IsInteropNativeHandle ) ||
268- (Context->OwnNativeHandle && Context->IsInteropNativeHandle &&
269- checkL0LoaderTeardown ()))
267+ (Context->OwnNativeHandle && checkL0LoaderTeardown ())
270268 ? Context->ZeContext
271269 : nullptr ;
272270
@@ -310,8 +308,7 @@ ur_result_t ur_context_handle_t_::finalize() {
310308 std::scoped_lock<ur_mutex> Lock (EventCacheMutex);
311309 for (auto &EventCache : EventCaches) {
312310 for (auto &Event : EventCache) {
313- if (!Event->IsInteropNativeHandle ||
314- (Event->IsInteropNativeHandle && checkL0LoaderTeardown ())) {
311+ if (checkL0LoaderTeardown ()) {
315312 auto ZeResult = ZE_CALL_NOCHECK (zeEventDestroy, (Event->ZeEvent ));
316313 // Gracefully handle the case that L0 was already unloaded.
317314 if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED || ZeResult != ZE_RESULT_ERROR_UNKNOWN))
@@ -330,32 +327,36 @@ ur_result_t ur_context_handle_t_::finalize() {
330327 std::scoped_lock<ur_mutex> Lock (ZeEventPoolCacheMutex);
331328 for (auto &ZePoolCache : ZeEventPoolCache) {
332329 for (auto &ZePool : ZePoolCache) {
333- auto ZeResult = ZE_CALL_NOCHECK (zeEventPoolDestroy, (ZePool));
334- // Gracefully handle the case that L0 was already unloaded.
335- if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED || ZeResult != ZE_RESULT_ERROR_UNKNOWN))
336- return ze2urResult (ZeResult);
337- if ( ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
338- ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
330+ if (checkL0LoaderTeardown ()) {
331+ auto ZeResult = ZE_CALL_NOCHECK (zeEventPoolDestroy, (ZePool));
332+ // Gracefully handle the case that L0 was already unloaded.
333+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED || ZeResult != ZE_RESULT_ERROR_UNKNOWN))
334+ return ze2urResult (ZeResult);
335+ if ( ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
336+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
337+ }
339338 }
340339 }
341340 ZePoolCache.clear ();
342341 }
343342 }
344343
345- // Destroy the command list used for initializations
346- auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandListInit));
347- // Gracefully handle the case that L0 was already unloaded.
348- if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED || ZeResult != ZE_RESULT_ERROR_UNKNOWN))
349- return ze2urResult (ZeResult);
350- if ( ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
351- ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
344+ if (checkL0LoaderTeardown ()) {
345+ // Destroy the command list used for initializations
346+ auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandListInit));
347+ // Gracefully handle the case that L0 was already unloaded.
348+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED || ZeResult != ZE_RESULT_ERROR_UNKNOWN))
349+ return ze2urResult (ZeResult);
350+ if ( ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
351+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
352+ }
352353 }
353354
354355 std::scoped_lock<ur_mutex> Lock (ZeCommandListCacheMutex);
355356 for (auto &List : ZeComputeCommandListCache) {
356357 for (auto &Item : List.second ) {
357358 ze_command_list_handle_t ZeCommandList = Item.first ;
358- if (ZeCommandList) {
359+ if (ZeCommandList && checkL0LoaderTeardown () ) {
359360 auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandList));
360361 // Gracefully handle the case that L0 was already unloaded.
361362 if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED || ZeResult != ZE_RESULT_ERROR_UNKNOWN))
@@ -369,7 +370,7 @@ ur_result_t ur_context_handle_t_::finalize() {
369370 for (auto &List : ZeCopyCommandListCache) {
370371 for (auto &Item : List.second ) {
371372 ze_command_list_handle_t ZeCommandList = Item.first ;
372- if (ZeCommandList) {
373+ if (ZeCommandList && checkL0LoaderTeardown () ) {
373374 auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandList));
374375 // Gracefully handle the case that L0 was already unloaded.
375376 if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED || ZeResult != ZE_RESULT_ERROR_UNKNOWN))
0 commit comments