Skip to content

Commit

Permalink
[UR] call urGetAdapter before creating a platform from native handle (#…
Browse files Browse the repository at this point in the history
…12432)

Calling urAdapterGet is required for the global adapter to be
initialized, but it was missing when creating a platform from a native
handle. This meant that the adapter's state never got initialized and
its refcount remained at 0 (and overflowed on teardown).

This had no visible side-effects until changes in #12403.
  • Loading branch information
pbalcer authored and kbenzie committed Jan 22, 2024
1 parent 4fd28dd commit 5671d66
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions sycl/plugins/unified_runtime/pi2ur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,12 +798,7 @@ inline pi_result piTearDown(void *PluginParameter) {
return PI_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////
// Platform
inline pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
pi_uint32 *NumPlatforms) {

urInit(0, nullptr);
inline pi_result PiGetAdapter(ur_adapter_handle_t &adapter) {
// We're not going through the UR loader so we're guaranteed to have exactly
// one adapter (whichever is statically linked). The PI plugin for UR has its
// own implementation of piPlatformsGet.
Expand All @@ -814,9 +809,23 @@ inline pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
[&Ret]() { Ret = urAdapterGet(1, &Adapter, nullptr); });
HANDLE_ERRORS(Ret);

adapter = Adapter;

return PI_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////
// Platform
inline pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
pi_uint32 *NumPlatforms) {
ur_adapter_handle_t adapter = nullptr;
if (auto res = PiGetAdapter(adapter); res != PI_SUCCESS) {
return res;
}

auto phPlatforms = reinterpret_cast<ur_platform_handle_t *>(Platforms);
HANDLE_ERRORS(
urPlatformGet(&Adapter, 1, NumEntries, phPlatforms, NumPlatforms));
urPlatformGet(&adapter, 1, NumEntries, phPlatforms, NumPlatforms));
return PI_SUCCESS;
}

Expand All @@ -843,6 +852,12 @@ piextPlatformCreateWithNativeHandle(pi_native_handle NativeHandle,
PI_ASSERT(Platform, PI_ERROR_INVALID_PLATFORM);
PI_ASSERT(NativeHandle, PI_ERROR_INVALID_VALUE);

ur_adapter_handle_t adapter = nullptr;
if (auto res = PiGetAdapter(adapter); res != PI_SUCCESS) {
return res;
}
(void)adapter;

ur_platform_handle_t UrPlatform{};
ur_native_handle_t UrNativeHandle =
reinterpret_cast<ur_native_handle_t>(NativeHandle);
Expand Down

0 comments on commit 5671d66

Please sign in to comment.