Skip to content

Mirror intel/llvm commits #2793

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 3 commits into from
Jun 23, 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
2 changes: 1 addition & 1 deletion .github/intel-llvm-mirror-base-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72829cdeb123d5b7f0171903b7aac4d14fc963ec
80b06481ca7e5012ab687d0d53115df5e5a855af
46 changes: 31 additions & 15 deletions source/adapters/offload/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#include "device.hpp"
#include "platform.hpp"
#include "ur/ur.hpp"
#include "ur2offload.hpp"
#include "ur_api.h"

ur_adapter_handle_t_ Adapter{};
ur_adapter_handle_t Adapter = nullptr;

// Initialize liboffload and perform the initial platform and device discovery
ur_result_t ur_adapter_handle_t_::init() {
Expand All @@ -30,7 +31,7 @@ ur_result_t ur_adapter_handle_t_::init() {
Res = olIterateDevices(
[](ol_device_handle_t D, void *UserData) {
auto *Platforms =
reinterpret_cast<decltype(Adapter.Platforms) *>(UserData);
reinterpret_cast<decltype(Adapter->Platforms) *>(UserData);

ol_platform_handle_t Platform;
olGetDeviceInfo(D, OL_DEVICE_INFO_PLATFORM, sizeof(Platform),
Expand All @@ -39,7 +40,7 @@ ur_result_t ur_adapter_handle_t_::init() {
olGetPlatformInfo(Platform, OL_PLATFORM_INFO_BACKEND, sizeof(Backend),
&Backend);
if (Backend == OL_PLATFORM_BACKEND_HOST) {
Adapter.HostDevice = D;
Adapter->HostDevice = D;
} else if (Backend != OL_PLATFORM_BACKEND_UNKNOWN) {
auto URPlatform =
std::find_if(Platforms->begin(), Platforms->end(), [&](auto &P) {
Expand All @@ -57,37 +58,52 @@ ur_result_t ur_adapter_handle_t_::init() {
}
return false;
},
&Adapter.Platforms);
&Adapter->Platforms);

(void)Res;

return UR_RESULT_SUCCESS;
return offloadResultToUR(Res);
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
uint32_t, ur_adapter_handle_t *phAdapters, uint32_t *pNumAdapters) {
std::mutex InitMutex{};

if (phAdapters) {
if (++Adapter.RefCount == 1) {
Adapter.init();
std::lock_guard Guard{InitMutex};

// We explicitly only initialize the adapter when outputting it
if (!Adapter) {
Adapter = new ur_adapter_handle_t_{};
auto Res = Adapter->init();
if (Res) {
delete Adapter;
Adapter = nullptr;
return Res;
}
}
*phAdapters = &Adapter;
Adapter->RefCount++;
*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) {
// Doesn't need protecting by a lock - There is no way to reinitialize the
// adapter after the final reference is released
if (--Adapter->RefCount == 0) {
// This can crash when tracing is enabled.
// olShutDown();
delete Adapter;
};
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
Adapter.RefCount++;
Adapter->RefCount++;
return UR_RESULT_SUCCESS;
}

Expand All @@ -102,7 +118,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
case UR_ADAPTER_INFO_BACKEND:
return ReturnValue(UR_BACKEND_OFFLOAD);
case UR_ADAPTER_INFO_REFERENCE_COUNT:
return ReturnValue(Adapter.RefCount.load());
return ReturnValue(Adapter->RefCount.load());
case UR_ADAPTER_INFO_VERSION:
return ReturnValue(1);
default:
Expand All @@ -124,15 +140,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterSetLoggerCallback(
ur_adapter_handle_t, ur_logger_callback_t pfnLoggerCallback,
void *pUserData, ur_logger_level_t level = UR_LOGGER_LEVEL_QUIET) {

Adapter.Logger.setCallbackSink(pfnLoggerCallback, pUserData, level);
Adapter->Logger.setCallbackSink(pfnLoggerCallback, pUserData, level);

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL
urAdapterSetLoggerCallbackLevel(ur_adapter_handle_t, ur_logger_level_t level) {

Adapter.Logger.setCallbackLevel(level);
Adapter->Logger.setCallbackLevel(level);

return UR_RESULT_SUCCESS;
}
2 changes: 1 addition & 1 deletion source/adapters/offload/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ struct ur_adapter_handle_t_ : ur::offload::handle_base {
ur_result_t init();
};

extern ur_adapter_handle_t_ Adapter;
extern ur_adapter_handle_t Adapter;
4 changes: 2 additions & 2 deletions source/adapters/offload/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferRead(
char *DevPtr =
reinterpret_cast<char *>(std::get<BufferMem>(hBuffer->Mem).Ptr);

olMemcpy(hQueue->OffloadQueue, pDst, Adapter.HostDevice, DevPtr + offset,
olMemcpy(hQueue->OffloadQueue, pDst, Adapter->HostDevice, DevPtr + offset,
hQueue->OffloadDevice, size, phEvent ? &EventOut : nullptr);

if (blockingRead) {
Expand Down Expand Up @@ -145,7 +145,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWrite(

auto Res =
olMemcpy(hQueue->OffloadQueue, DevPtr + offset, hQueue->OffloadDevice,
pSrc, Adapter.HostDevice, size, phEvent ? &EventOut : nullptr);
pSrc, Adapter->HostDevice, size, phEvent ? &EventOut : nullptr);
if (Res) {
return offloadResultToUR(Res);
}
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/offload/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(

if (PerformInitialCopy) {
auto Res = olMemcpy(nullptr, Ptr, OffloadDevice, HostPtr,
Adapter.HostDevice, size, nullptr);
Adapter->HostDevice, size, nullptr);
if (Res) {
return offloadResultToUR(Res);
}
Expand Down
4 changes: 2 additions & 2 deletions source/adapters/offload/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ urPlatformGet(ur_adapter_handle_t, uint32_t NumEntries,
ur_platform_handle_t *phPlatforms, uint32_t *pNumPlatforms) {

if (pNumPlatforms) {
*pNumPlatforms = Adapter.Platforms.size();
*pNumPlatforms = Adapter->Platforms.size();
}

if (phPlatforms) {
size_t PlatformIndex = 0;
for (auto &Platform : Adapter.Platforms) {
for (auto &Platform : Adapter->Platforms) {
phPlatforms[PlatformIndex++] = Platform.get();
if (PlatformIndex == NumEntries) {
break;
Expand Down
4 changes: 0 additions & 4 deletions source/adapters/opencl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ struct ur_context_handle_t_ : ur::opencl::handle_base {
Devices.emplace_back(phDevices[i]);
urDeviceRetain(phDevices[i]);
}
// The context retains a reference to the adapter so it can clear the
// function ptr cache on destruction
urAdapterRetain(ur::cl::getAdapter());
RefCount = 1;
}

Expand All @@ -51,7 +48,6 @@ struct ur_context_handle_t_ : ur::opencl::handle_base {
// should drastically reduce the chances of the pathological case described
// in the comments in common.hpp.
ur::cl::getAdapter()->fnCache.clearCache(CLContext);
urAdapterRelease(ur::cl::getAdapter());

for (uint32_t i = 0; i < DeviceCount; i++) {
urDeviceRelease(Devices[i]);
Expand Down