diff --git a/sycl/include/CL/sycl/detail/pi.h b/sycl/include/CL/sycl/detail/pi.h index 3ab941823e603..28af917d3f51e 100644 --- a/sycl/include/CL/sycl/detail/pi.h +++ b/sycl/include/CL/sycl/detail/pi.h @@ -962,7 +962,7 @@ piextDeviceGetNativeHandle(pi_device device, pi_native_handle *nativeHandle); /// NOTE: The created PI object takes ownership of the native handle. /// /// \param nativeHandle is the native handle to create PI device from. -/// \param platform is the platform of the device. +/// \param platform is the platform of the device (optional). /// \param device is the PI device created from the native handle. __SYCL_EXPORT pi_result piextDeviceCreateWithNativeHandle( pi_native_handle nativeHandle, pi_platform platform, pi_device *device); diff --git a/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp b/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp index 9cc87f424326e..e1e9a6746934e 100644 --- a/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp +++ b/sycl/include/sycl/ext/oneapi/backend/level_zero.hpp @@ -78,21 +78,24 @@ template <> struct BackendInput { using type = struct { interop::type NativeHandle; std::vector DeviceList; - ext::oneapi::level_zero::ownership Ownership; + ext::oneapi::level_zero::ownership Ownership{ + ext::oneapi::level_zero::ownership::transfer}; }; }; template <> struct BackendInput { using type = struct { interop::type NativeHandle; - ext::oneapi::level_zero::ownership Ownership; + ext::oneapi::level_zero::ownership Ownership{ + ext::oneapi::level_zero::ownership::transfer}; }; }; template <> struct BackendInput { using type = struct { interop::type NativeHandle; - ext::oneapi::level_zero::ownership Ownership; + ext::oneapi::level_zero::ownership Ownership{ + ext::oneapi::level_zero::ownership::transfer}; }; }; diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index ff934dee2be1f..55f7053d5f56f 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -2528,18 +2528,33 @@ pi_result piextDeviceCreateWithNativeHandle(pi_native_handle NativeHandle, pi_device *Device) { PI_ASSERT(Device, PI_INVALID_DEVICE); PI_ASSERT(NativeHandle, PI_INVALID_VALUE); - PI_ASSERT(Platform, PI_INVALID_PLATFORM); auto ZeDevice = pi_cast(NativeHandle); // The SYCL spec requires that the set of devices must remain fixed for the // duration of the application's execution. We assume that we found all of the - // Level Zero devices when we initialized the device cache, so the + // Level Zero devices when we initialized the platforms/devices cache, so the // "NativeHandle" must already be in the cache. If it is not, this must not be // a valid Level Zero device. - pi_device Dev = Platform->getDeviceFromNativeHandle(ZeDevice); + // + // TODO: maybe we should populate cache of platforms if it wasn't already. + // For now assert that is was populated. + PI_ASSERT(PiPlatformCachePopulated, PI_INVALID_VALUE); + const std::lock_guard Lock{*PiPlatformsCacheMutex}; + + pi_device Dev = nullptr; + for (auto &ThePlatform : *PiPlatformsCache) { + Dev = ThePlatform->getDeviceFromNativeHandle(ZeDevice); + if (Dev) { + // Check that the input Platform, if was given, matches the found one. + PI_ASSERT(!Platform || Platform == ThePlatform, PI_INVALID_PLATFORM); + break; + } + } + if (Dev == nullptr) return PI_INVALID_VALUE; + *Device = Dev; return PI_SUCCESS; }