@@ -151,7 +151,6 @@ ur_result_t urContextCreateWithNativeHandle(
151151 ur_context_handle_t_ *UrContext = new ur_context_handle_t_ (
152152 ZeContext, NumDevices, Devices, OwnNativeHandle);
153153 UrContext->initialize ();
154- UrContext->IsInteropNativeHandle = true ;
155154 *Context = reinterpret_cast <ur_context_handle_t >(UrContext);
156155 } catch (const std::bad_alloc &) {
157156 return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
@@ -263,11 +262,8 @@ ur_result_t ContextReleaseHelper(ur_context_handle_t Context) {
263262 Contexts.erase (It);
264263 }
265264 ze_context_handle_t DestroyZeContext =
266- ((Context->OwnNativeHandle && !Context->IsInteropNativeHandle ) ||
267- (Context->OwnNativeHandle && Context->IsInteropNativeHandle &&
268- checkL0LoaderTeardown ()))
269- ? Context->ZeContext
270- : nullptr ;
265+ (Context->OwnNativeHandle && checkL0LoaderTeardown ()) ? Context->ZeContext
266+ : nullptr ;
271267
272268 // Clean up any live memory associated with Context
273269 ur_result_t Result = Context->finalize ();
@@ -284,8 +280,12 @@ ur_result_t ContextReleaseHelper(ur_context_handle_t Context) {
284280 if (DestroyZeContext) {
285281 auto ZeResult = ZE_CALL_NOCHECK (zeContextDestroy, (DestroyZeContext));
286282 // Gracefully handle the case that L0 was already unloaded.
287- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
283+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
284+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
288285 return ze2urResult (ZeResult);
286+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
287+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
288+ }
289289 }
290290
291291 return Result;
@@ -304,12 +304,15 @@ ur_result_t ur_context_handle_t_::finalize() {
304304 std::scoped_lock<ur_mutex> Lock (EventCacheMutex);
305305 for (auto &EventCache : EventCaches) {
306306 for (auto &Event : EventCache) {
307- if (!Event->IsInteropNativeHandle ||
308- (Event->IsInteropNativeHandle && checkL0LoaderTeardown ())) {
307+ if (checkL0LoaderTeardown ()) {
309308 auto ZeResult = ZE_CALL_NOCHECK (zeEventDestroy, (Event->ZeEvent ));
310309 // Gracefully handle the case that L0 was already unloaded.
311- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
310+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
311+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
312312 return ze2urResult (ZeResult);
313+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
314+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
315+ }
313316 }
314317 Event->ZeEvent = nullptr ;
315318 delete Event;
@@ -321,41 +324,61 @@ ur_result_t ur_context_handle_t_::finalize() {
321324 std::scoped_lock<ur_mutex> Lock (ZeEventPoolCacheMutex);
322325 for (auto &ZePoolCache : ZeEventPoolCache) {
323326 for (auto &ZePool : ZePoolCache) {
324- auto ZeResult = ZE_CALL_NOCHECK (zeEventPoolDestroy, (ZePool));
325- // Gracefully handle the case that L0 was already unloaded.
326- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
327- return ze2urResult (ZeResult);
327+ if (checkL0LoaderTeardown ()) {
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+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
332+ return ze2urResult (ZeResult);
333+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
334+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
335+ }
336+ }
328337 }
329338 ZePoolCache.clear ();
330339 }
331340 }
332341
333- // Destroy the command list used for initializations
334- auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandListInit));
335- // Gracefully handle the case that L0 was already unloaded.
336- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
337- return ze2urResult (ZeResult);
342+ if (checkL0LoaderTeardown ()) {
343+ // Destroy the command list used for initializations
344+ auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandListInit));
345+ // Gracefully handle the case that L0 was already unloaded.
346+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
347+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
348+ return ze2urResult (ZeResult);
349+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
350+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
351+ }
352+ }
338353
339354 std::scoped_lock<ur_mutex> Lock (ZeCommandListCacheMutex);
340355 for (auto &List : ZeComputeCommandListCache) {
341356 for (auto &Item : List.second ) {
342357 ze_command_list_handle_t ZeCommandList = Item.first ;
343- if (ZeCommandList) {
358+ if (ZeCommandList && checkL0LoaderTeardown () ) {
344359 auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandList));
345360 // Gracefully handle the case that L0 was already unloaded.
346- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
361+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
362+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
347363 return ze2urResult (ZeResult);
364+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
365+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
366+ }
348367 }
349368 }
350369 }
351370 for (auto &List : ZeCopyCommandListCache) {
352371 for (auto &Item : List.second ) {
353372 ze_command_list_handle_t ZeCommandList = Item.first ;
354- if (ZeCommandList) {
373+ if (ZeCommandList && checkL0LoaderTeardown () ) {
355374 auto ZeResult = ZE_CALL_NOCHECK (zeCommandListDestroy, (ZeCommandList));
356375 // Gracefully handle the case that L0 was already unloaded.
357- if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
376+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED ||
377+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
358378 return ze2urResult (ZeResult);
379+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
380+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
381+ }
359382 }
360383 }
361384 }
0 commit comments