Skip to content

[UR][Offload] Native handle wrappers for device and platform #19563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions unified-runtime/source/adapters/offload/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ urContextRelease(ur_context_handle_t hContext) {
return UR_RESULT_SUCCESS;
}

// Offload currently doesn't have an equivalent to context handles
UR_APIEXPORT ur_result_t UR_APICALL
urContextGetNativeHandle(ur_context_handle_t, ur_native_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
Expand Down
29 changes: 24 additions & 5 deletions unified-runtime/source/adapters/offload/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <ur/ur.hpp>
#include <ur_api.h>

#include "adapters/offload/adapter.hpp"
#include "device.hpp"
#include "platform.hpp"
#include "ur2offload.hpp"
Expand Down Expand Up @@ -207,14 +208,32 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceSelectBinary(
return UR_RESULT_ERROR_INVALID_BINARY;
}

UR_APIEXPORT ur_result_t UR_APICALL
urDeviceGetNativeHandle(ur_device_handle_t, ur_native_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetNativeHandle(
ur_device_handle_t UrDevice, ur_native_handle_t *Handle) {
*Handle = reinterpret_cast<ur_native_handle_t>(UrDevice->OffloadDevice);
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
ur_native_handle_t, ur_adapter_handle_t,
const ur_device_native_properties_t *, ur_device_handle_t *) {
ur_native_handle_t hNativeDevice, ur_adapter_handle_t hAdapter,
const ur_device_native_properties_t *, ur_device_handle_t *phDevice) {
ol_device_handle_t OlDevice =
reinterpret_cast<ol_device_handle_t>(hNativeDevice);

// Currently, all devices are found at initialization, there is no way to
// create sub devices yet
for (auto &P : hAdapter->Platforms) {
auto Found =
std::find_if(P->Devices.begin(), P->Devices.end(),
[&](std::unique_ptr<ur_device_handle_t_> &PDevice) {
return PDevice->OffloadDevice == OlDevice;
});
if (Found != P->Devices.end()) {
*phDevice = Found->get();
return UR_RESULT_SUCCESS;
}
}

return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

Expand Down
23 changes: 18 additions & 5 deletions unified-runtime/source/adapters/offload/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,27 @@ urPlatformGetBackendOption(ur_platform_handle_t, const char *pFrontendOption,
return UR_RESULT_ERROR_INVALID_VALUE;
}

UR_APIEXPORT ur_result_t UR_APICALL
urPlatformGetNativeHandle(ur_platform_handle_t, ur_native_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetNativeHandle(
ur_platform_handle_t hAdapter, ur_native_handle_t *phNativePlatform) {
*phNativePlatform =
reinterpret_cast<ur_native_handle_t>(hAdapter->OffloadPlatform);
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
ur_native_handle_t, ur_adapter_handle_t,
const ur_platform_native_properties_t *, ur_platform_handle_t *) {
ur_native_handle_t hNativePlatform, ur_adapter_handle_t hAdapter,
const ur_platform_native_properties_t *, ur_platform_handle_t *phPlatform) {

auto Found = std::find_if(
hAdapter->Platforms.begin(), hAdapter->Platforms.end(), [&](auto &P) {
return P->OffloadPlatform ==
reinterpret_cast<ol_platform_handle_t>(hNativePlatform);
});
if (Found != hAdapter->Platforms.end()) {
*phPlatform = Found->get();
return UR_RESULT_SUCCESS;
}

return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

Expand Down