-
Notifications
You must be signed in to change notification settings - Fork 794
[UR][Offload] Add initial Offload adapter #18271
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
feb35eb
Add initial Offload adapter implementation
callumfare 1e64980
Support compiling on non-CUDA
RossBrunton 6a2fd7d
Add "offload" as an adapter to the various registries
RossBrunton c35f7e6
Quick CTS running for Offload
RossBrunton 458890b
Parse HIP "offload" bundles
RossBrunton ad382af
Use proper handle types
RossBrunton 6b94b60
Fix broken cast
RossBrunton 59f5601
Fix Offload build on CUDA and detect correct targets in the CTS
callumfare e9f2731
Add missing license text
callumfare f7c81bc
Address review feedback
callumfare e2d4415
Remove unneeded struct in olIterateDevices call
callumfare 6b9fa1e
Add ddi_getter support
RossBrunton f18ca23
Clean cmake file
RossBrunton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (C) 2025 Intel Corporation | ||
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. | ||
# See LICENSE.TXT | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
set(TARGET_NAME ur_adapter_offload) | ||
|
||
set(UR_OFFLOAD_INSTALL_DIR "" CACHE PATH "Path to the directory containing libomptarget.so etc") | ||
if (UR_OFFLOAD_INSTALL_DIR STREQUAL "") | ||
message(FATAL_ERROR "UR_OFFLOAD_INSTALL_DIR must be defined for the Offload adapter") | ||
endif() | ||
|
||
set(UR_OFFLOAD_INCLUDE_DIR "" CACHE PATH "Path to the directory containing LLVM headers") | ||
if (UR_OFFLOAD_INCLUDE_DIR STREQUAL "") | ||
message(FATAL_ERROR "UR_OFFLOAD_INCLUDE_DIR must be defined for the Offload adapter") | ||
endif() | ||
|
||
# When targetting CUDA devices, we need a workaround to avoid sending PTX to | ||
# liboffload as the CUDA plugin doesn't support it yet. The workaround is to | ||
# simply always link the incoming program so it ends up as CUBIN. Try to find | ||
# the cuda driver so we can enable this where possible. | ||
if (NOT TARGET cudadrv) | ||
find_package(CUDA 10.1) | ||
add_library(cudadrv SHARED IMPORTED GLOBAL) | ||
set_target_properties( | ||
cudadrv PROPERTIES | ||
IMPORTED_LOCATION ${CUDA_cuda_driver_LIBRARY} | ||
INTERFACE_INCLUDE_DIRECTORIES ${CUDAToolkit_INCLUDE_DIRS} | ||
) | ||
endif() | ||
|
||
add_ur_adapter(${TARGET_NAME} | ||
SHARED | ||
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/context.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/device.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/enqueue.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/event.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/kernel.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/program.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/queue.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/ur2offload.hpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/usm.cpp | ||
) | ||
|
||
set_target_properties(${TARGET_NAME} PROPERTIES | ||
VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" | ||
SOVERSION "${PROJECT_VERSION_MAJOR}" | ||
) | ||
|
||
set(ADDITIONAL_LINK_LIBS "") | ||
if (CUDA_cuda_driver_LIBRARY) | ||
list(APPEND ADDITIONAL_LINK_LIBS cudadrv) | ||
target_compile_definitions(${TARGET_NAME} PRIVATE UR_CUDA_ENABLED) | ||
endif() | ||
|
||
target_link_libraries(${TARGET_NAME} PRIVATE | ||
${PROJECT_NAME}::headers | ||
${PROJECT_NAME}::common | ||
${PROJECT_NAME}::umf | ||
${UR_OFFLOAD_INSTALL_DIR}/lib/libLLVMOffload.so | ||
${ADDITIONAL_LINK_LIBS} | ||
) | ||
|
||
target_include_directories(${TARGET_NAME} PRIVATE | ||
"${UR_OFFLOAD_INCLUDE_DIR}/offload" | ||
"${CMAKE_CURRENT_SOURCE_DIR}/../../" | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
//===----------- adapter.cpp - LLVM Offload Adapter ----------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <OffloadAPI.h> | ||
#include <atomic> | ||
#include <cstdint> | ||
#include <unordered_set> | ||
|
||
#include "adapter.hpp" | ||
#include "device.hpp" | ||
#include "platform.hpp" | ||
#include "ur/ur.hpp" | ||
#include "ur_api.h" | ||
|
||
ur_adapter_handle_t_ Adapter{}; | ||
|
||
// Initialize liboffload and perform the initial platform and device discovery | ||
ur_result_t ur_adapter_handle_t_::init() { | ||
auto Res = olInit(); | ||
(void)Res; | ||
|
||
// Discover every platform and device | ||
Res = olIterateDevices( | ||
[](ol_device_handle_t D, void *UserData) { | ||
auto *Platforms = | ||
reinterpret_cast<decltype(Adapter.Platforms) *>(UserData); | ||
|
||
ol_platform_handle_t Platform; | ||
olGetDeviceInfo(D, OL_DEVICE_INFO_PLATFORM, sizeof(Platform), | ||
&Platform); | ||
ol_platform_backend_t Backend; | ||
olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND, sizeof(Backend), | ||
&Backend); | ||
if (Backend == OL_PLATFORM_BACKEND_HOST) { | ||
Adapter.HostDevice = D; | ||
} else if (Backend != OL_PLATFORM_BACKEND_UNKNOWN) { | ||
auto URPlatform = | ||
std::find_if(Platforms->begin(), Platforms->end(), [&](auto &P) { | ||
return P.OffloadPlatform == Platform; | ||
}); | ||
|
||
if (URPlatform == Platforms->end()) { | ||
URPlatform = | ||
Platforms->insert(URPlatform, ur_platform_handle_t_(Platform)); | ||
} | ||
|
||
URPlatform->Devices.push_back(ur_device_handle_t_{&*URPlatform, D}); | ||
} | ||
return false; | ||
}, | ||
&Adapter.Platforms); | ||
|
||
(void)Res; | ||
|
||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet( | ||
uint32_t, ur_adapter_handle_t *phAdapters, uint32_t *pNumAdapters) { | ||
if (phAdapters) { | ||
if (++Adapter.RefCount == 1) { | ||
Adapter.init(); | ||
} | ||
*phAdapters = &Adapter; | ||
} | ||
if (pNumAdapters) { | ||
*pNumAdapters = 1; | ||
} | ||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) { | ||
if (--Adapter.RefCount == 0) { | ||
// This can crash when tracing is enabled. | ||
// olShutDown(); | ||
}; | ||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) { | ||
Adapter.RefCount++; | ||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t, | ||
ur_adapter_info_t propName, | ||
size_t propSize, | ||
void *pPropValue, | ||
size_t *pPropSizeRet) { | ||
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); | ||
|
||
switch (propName) { | ||
case UR_ADAPTER_INFO_BACKEND: | ||
return ReturnValue(UR_BACKEND_OFFLOAD); | ||
case UR_ADAPTER_INFO_REFERENCE_COUNT: | ||
return ReturnValue(Adapter.RefCount.load()); | ||
default: | ||
return UR_RESULT_ERROR_INVALID_ENUMERATION; | ||
} | ||
|
||
return UR_RESULT_SUCCESS; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===----------- adapter.hpp - LLVM Offload Adapter ----------------------===// | ||
// | ||
// Copyright (C) 2025 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
#include <cstdint> | ||
#include <unordered_set> | ||
|
||
#include <OffloadAPI.h> | ||
|
||
#include "common.hpp" | ||
#include "logger/ur_logger.hpp" | ||
#include "platform.hpp" | ||
|
||
struct ur_adapter_handle_t_ : ur::offload::handle_base { | ||
std::atomic_uint32_t RefCount = 0; | ||
logger::Logger &Logger = logger::get_logger("offload"); | ||
ol_device_handle_t HostDevice = nullptr; | ||
std::vector<ur_platform_handle_t_> Platforms; | ||
|
||
ur_result_t init(); | ||
}; | ||
|
||
extern ur_adapter_handle_t_ Adapter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===----------- common.hpp - LLVM Offload Adapter -----------------------===// | ||
// | ||
// Copyright (C) 2024 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include "ur/ur.hpp" | ||
#include <atomic> | ||
|
||
namespace ur::offload { | ||
struct ddi_getter { | ||
const static ur_dditable_t *value(); | ||
}; | ||
using handle_base = ur::handle_base<ur::offload::ddi_getter>; | ||
} // namespace ur::offload | ||
|
||
struct RefCounted : ur::offload::handle_base { | ||
std::atomic_uint32_t RefCount = 1; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//===----------- context.cpp - LLVM Offload Adapter ----------------------===// | ||
// | ||
// Copyright (C) 2025 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "context.hpp" | ||
#include <ur_api.h> | ||
|
||
UR_APIEXPORT ur_result_t UR_APICALL urContextCreate( | ||
uint32_t DeviceCount, const ur_device_handle_t *phDevices, | ||
const ur_context_properties_t *, ur_context_handle_t *phContext) { | ||
if (DeviceCount > 1) { | ||
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; | ||
} | ||
|
||
auto Ctx = new ur_context_handle_t_(*phDevices); | ||
*phContext = Ctx; | ||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
UR_APIEXPORT ur_result_t UR_APICALL | ||
urContextRetain(ur_context_handle_t hContext) { | ||
hContext->RefCount++; | ||
return UR_RESULT_SUCCESS; | ||
} | ||
|
||
UR_APIEXPORT ur_result_t UR_APICALL | ||
urContextRelease(ur_context_handle_t hContext) { | ||
if (--hContext->RefCount == 0) { | ||
delete hContext; | ||
} | ||
return UR_RESULT_SUCCESS; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===----------- context.hpp - LLVM Offload Adapter ----------------------===// | ||
// | ||
// Copyright (C) 2025 Intel Corporation | ||
// | ||
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See LICENSE.TXT | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include "common.hpp" | ||
#include <OffloadAPI.h> | ||
#include <unordered_map> | ||
#include <ur_api.h> | ||
|
||
struct ur_context_handle_t_ : RefCounted { | ||
ur_context_handle_t_(ur_device_handle_t hDevice) : Device{hDevice} { | ||
urDeviceRetain(Device); | ||
} | ||
~ur_context_handle_t_() { urDeviceRelease(Device); } | ||
|
||
ur_device_handle_t Device; | ||
std::unordered_map<void *, ol_alloc_type_t> AllocTypeMap; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.