@@ -152,7 +152,6 @@ ur_result_t urContextCreateWithNativeHandle(
152152 ur_context_handle_t_ *UrContext = new ur_context_handle_t_ (
153153 ZeContext, NumDevices, Devices, OwnNativeHandle);
154154 UrContext->initialize ();
155- UrContext->IsInteropNativeHandle = true ;
156155 *Context = reinterpret_cast <ur_context_handle_t >(UrContext);
157156 } catch (const std::bad_alloc &) {
158157 return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
@@ -265,11 +264,8 @@ ur_result_t ContextReleaseHelper(ur_context_handle_t Context) {
265264 Contexts.erase (It);
266265 }
267266 ze_context_handle_t DestroyZeContext =
268- ((Context->OwnNativeHandle && !Context->IsInteropNativeHandle ) ||
269- (Context->OwnNativeHandle && Context->IsInteropNativeHandle &&
270- checkL0LoaderTeardown ()))
271- ? Context->ZeContext
272- : nullptr ;
267+ (Context->OwnNativeHandle && checkL0LoaderTeardown ()) ? Context->ZeContext
268+ : nullptr ;
273269
274270 // Clean up any live memory associated with Context
275271 ur_result_t Result = Context->finalize ();
@@ -286,8 +282,12 @@ ur_result_t ContextReleaseHelper(ur_context_handle_t Context) {
286282 if (DestroyZeContext) {
287283 auto ZeResult = ZE_CALL_NOCHECK (zeContextDestroy, (DestroyZeContext));
288284 // Gracefully handle the case that L0 was already unloaded.
289- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
285+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
286+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
290287 return ze2urResult (ZeResult);
288+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
289+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
290+ }
291291 }
292292
293293 return Result;
@@ -308,12 +308,15 @@ ur_result_t ur_context_handle_t_::finalize() {
308308 std::scoped_lock<ur_mutex> Lock (EventCacheMutex);
309309 for (auto &EventCache : EventCaches) {
310310 for (auto &Event : EventCache) {
311- if (!Event->IsInteropNativeHandle ||
312- (Event->IsInteropNativeHandle && checkL0LoaderTeardown ())) {
311+ if (checkL0LoaderTeardown ()) {
313312 auto ZeResult = ZE_CALL_NOCHECK (zeEventDestroy, (Event->ZeEvent ));
314313 // Gracefully handle the case that L0 was already unloaded.
315- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
314+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
315+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
316316 return ze2urResult (ZeResult);
317+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
318+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
319+ }
317320 }
318321 Event->ZeEvent = nullptr ;
319322 delete Event;
@@ -325,41 +328,61 @@ ur_result_t ur_context_handle_t_::finalize() {
325328 std::scoped_lock<ur_mutex> Lock (ZeEventPoolCacheMutex);
326329 for (auto &ZePoolCache : ZeEventPoolCache) {
327330 for (auto &ZePool : ZePoolCache) {
328- auto ZeResult = ZE_CALL_NOCHECK (zeEventPoolDestroy, (ZePool));
329- // Gracefully handle the case that L0 was already unloaded.
330- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
331- return ze2urResult (ZeResult);
331+ if (checkL0LoaderTeardown ()) {
332+ auto ZeResult = ZE_CALL_NOCHECK (zeEventPoolDestroy, (ZePool));
333+ // Gracefully handle the case that L0 was already unloaded.
334+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
335+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
336+ return ze2urResult (ZeResult);
337+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
338+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
339+ }
340+ }
332341 }
333342 ZePoolCache.clear ();
334343 }
335344 }
336345
337- // Destroy the command list used for initializations
338- auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandListInit));
339- // Gracefully handle the case that L0 was already unloaded.
340- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
341- return ze2urResult (ZeResult);
346+ if (checkL0LoaderTeardown ()) {
347+ // Destroy the command list used for initializations
348+ auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandListInit));
349+ // Gracefully handle the case that L0 was already unloaded.
350+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
351+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
352+ return ze2urResult (ZeResult);
353+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
354+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
355+ }
356+ }
342357
343358 std::scoped_lock<ur_mutex> Lock (ZeCommandListCacheMutex);
344359 for (auto &List : ZeComputeCommandListCache) {
345360 for (auto &Item : List.second ) {
346361 ze_command_list_handle_t ZeCommandList = Item.first ;
347- if (ZeCommandList) {
362+ if (ZeCommandList && checkL0LoaderTeardown () ) {
348363 auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandList));
349364 // Gracefully handle the case that L0 was already unloaded.
350- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
365+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
366+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
351367 return ze2urResult (ZeResult);
368+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
369+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
370+ }
352371 }
353372 }
354373 }
355374 for (auto &List : ZeCopyCommandListCache) {
356375 for (auto &Item : List.second ) {
357376 ze_command_list_handle_t ZeCommandList = Item.first ;
358- if (ZeCommandList) {
377+ if (ZeCommandList && checkL0LoaderTeardown () ) {
359378 auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandList));
360379 // Gracefully handle the case that L0 was already unloaded.
361- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
380+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
381+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
362382 return ze2urResult (ZeResult);
383+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
384+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
385+ }
363386 }
364387 }
365388 }
0 commit comments