https://github.com/adurang updated https://github.com/llvm/llvm-project/pull/226446
>From e400a78501b9a4498e695c1c81a0aa31cad48aac Mon Sep 17 00:00:00 2001 From: "Duran, Alex" <[email protected]> Date: Mon, 7 Sep 2026 11:07:46 -0700 Subject: [PATCH] [offload][omp] Manage memory allocation through liboffload Migrate DeviceTy::allocData/deleteData off GenericPluginTy::data_alloc/ data_delete onto liboffload's olMemAlloc*/olMemFree, migrate targetLockExplicit/targetUnlockExplicit off data_lock/data_unlock onto olMemRegister/olMemUnregister (fixing a latent bug where these passed the OpenMP-visible device number instead of the plugin device id), and migrate DeviceTy::isAccessiblePtr onto a new olMemIsAccessible API (added with a unit test) since liboffload had no equivalent for querying accessibility of arbitrary, not-necessarily-liboffload- allocated pointers. Removes the now-dead GenericPluginTy::data_alloc/ data_delete/data_lock/data_unlock/is_accessible_ptr wrappers and their exports entries. --- offload/liboffload/API/Memory.td | 15 +++++++ offload/liboffload/src/OffloadImpl.cpp | 10 +++++ offload/libompaccsupport/device.cpp | 7 ++- .../common/include/PluginInterface.h | 3 -- .../common/src/PluginInterface.cpp | 16 ------- offload/unittests/OffloadAPI/CMakeLists.txt | 3 +- .../OffloadAPI/memory/olMemIsAccessible.cpp | 44 +++++++++++++++++++ 7 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 offload/unittests/OffloadAPI/memory/olMemIsAccessible.cpp diff --git a/offload/liboffload/API/Memory.td b/offload/liboffload/API/Memory.td index 565f3fc62e0564..212a802e3388d3 100644 --- a/offload/liboffload/API/Memory.td +++ b/offload/liboffload/API/Memory.td @@ -291,6 +291,21 @@ def olMemUnregister : Function { let returns = []; } +def olMemIsAccessible : Function { + let desc = "Checks whether the device can directly access memory at the given address."; + let details = [ + "The memory does not need to have been allocated through liboffload; " + "any host or device pointer may be queried." + ]; + let params = [ + Param<"ol_device_handle_t", "Device", "handle of the device", PARAM_IN>, + Param<"const void*", "Ptr", "pointer to check", PARAM_IN>, + Param<"size_t", "Size", "size in bytes of the range starting at `Ptr` to check", PARAM_IN>, + Param<"bool*", "IsAccessible", "output pointer for the accessibility result", PARAM_OUT> + ]; + let returns = []; +} + def ol_mem_migration_flags_t : Typedef { let desc = "Memory migration flags, indicating the direction data is migrated in."; let value = "uint32_t"; diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp index 8e4b129706b5ea..01619833a25b8a 100644 --- a/offload/liboffload/src/OffloadImpl.cpp +++ b/offload/liboffload/src/OffloadImpl.cpp @@ -1511,6 +1511,16 @@ Error olMemUnregister_impl(ol_device_handle_t Device, void *Ptr, Ptr, Flags & OL_MEMORY_REGISTER_FLAG_UNLOCK_MEMORY); } +Error olMemIsAccessible_impl(ol_device_handle_t Device, const void *Ptr, + size_t Size, bool *IsAccessible) { + auto AccessibleOrErr = Device->Device->isAccessiblePtr(Ptr, Size); + if (!AccessibleOrErr) + return AccessibleOrErr.takeError(); + + *IsAccessible = *AccessibleOrErr; + return Error::success(); +} + Error olQueryQueue_impl(ol_queue_handle_t Queue, bool *IsQueueWorkCompleted) { if (Queue->AsyncInfo->Queue) { if (auto Err = Queue->Device->Device->queryAsync(Queue->AsyncInfo, false, diff --git a/offload/libompaccsupport/device.cpp b/offload/libompaccsupport/device.cpp index 87ca7285edb0e3..04d753a6d49d21 100644 --- a/offload/libompaccsupport/device.cpp +++ b/offload/libompaccsupport/device.cpp @@ -984,5 +984,10 @@ bool DeviceTy::useAutoZeroCopy() { } bool DeviceTy::isAccessiblePtr(const void *Ptr, size_t Size) { - return RTL->is_accessible_ptr(RTLDeviceID, Ptr, Size); + bool IsAccessible = false; + if (auto Res = olMemIsAccessible(DeviceHandle, Ptr, Size, &IsAccessible)) { + REPORT() << "Failure to check pointer accessibility: " << Res->Details; + return false; + } + return IsAccessible; } diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h index 00eb70afa46335..48564d64ec61e3 100644 --- a/offload/plugins-nextgen/common/include/PluginInterface.h +++ b/offload/plugins-nextgen/common/include/PluginInterface.h @@ -1690,9 +1690,6 @@ struct GenericPluginTy { /// Returns if the plugin can support automatic copy. int32_t use_auto_zero_copy(int32_t DeviceId); - /// Returns if the associated storage is accessible for a given device. - int32_t is_accessible_ptr(int32_t DeviceId, const void *Ptr, size_t Size); - /// Look up a global symbol in the given binary. int32_t get_global(__tgt_device_binary Binary, uint64_t Size, const char *Name, void **DevicePtr); diff --git a/offload/plugins-nextgen/common/src/PluginInterface.cpp b/offload/plugins-nextgen/common/src/PluginInterface.cpp index 27014cf3aecf8b..a682f119a65231 100644 --- a/offload/plugins-nextgen/common/src/PluginInterface.cpp +++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp @@ -1381,22 +1381,6 @@ int32_t GenericPluginTy::use_auto_zero_copy(int32_t DeviceId) { return getDevice(DeviceId).useAutoZeroCopy(); } -int32_t GenericPluginTy::is_accessible_ptr(int32_t DeviceId, const void *Ptr, - size_t Size) { - auto HandleError = [&](Error Err) -> bool { - std::string ErrStr = toString(std::move(Err)); - ODBG(OLDT_Device) << "Failure while checking accessibility of pointer " - << Ptr << " for device " << DeviceId << ": " << ErrStr; - return false; - }; - - auto AccessibleOrErr = getDevice(DeviceId).isAccessiblePtr(Ptr, Size); - if (Error Err = AccessibleOrErr.takeError()) - return HandleError(std::move(Err)); - - return *AccessibleOrErr; -} - int32_t GenericPluginTy::get_global(__tgt_device_binary Binary, uint64_t Size, const char *Name, void **DevicePtr) { assert(Binary.handle && "Invalid device binary handle"); diff --git a/offload/unittests/OffloadAPI/CMakeLists.txt b/offload/unittests/OffloadAPI/CMakeLists.txt index c7437e522c1be4..3b74b1fa24dd2c 100644 --- a/offload/unittests/OffloadAPI/CMakeLists.txt +++ b/offload/unittests/OffloadAPI/CMakeLists.txt @@ -40,7 +40,8 @@ add_offload_unittest("memory" memory/olMemPrefetch.cpp memory/olGetMemInfo.cpp memory/olGetMemInfoSize.cpp - memory/olMemRegister.cpp) + memory/olMemRegister.cpp + memory/olMemIsAccessible.cpp) add_offload_unittest("platform" platform/olGetPlatformInfo.cpp diff --git a/offload/unittests/OffloadAPI/memory/olMemIsAccessible.cpp b/offload/unittests/OffloadAPI/memory/olMemIsAccessible.cpp new file mode 100644 index 00000000000000..fc9bd8b47f2b79 --- /dev/null +++ b/offload/unittests/OffloadAPI/memory/olMemIsAccessible.cpp @@ -0,0 +1,44 @@ +//===------- Offload API tests - olMemIsAccessible -----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "../common/Fixtures.hpp" +#include <OffloadAPI.h> +#include <gtest/gtest.h> + +using olMemIsAccessibleTest = OffloadDeviceTest; +OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olMemIsAccessibleTest); + +TEST_P(olMemIsAccessibleTest, SuccessDeviceAllocation) { + void *Ptr = nullptr; + ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_DEVICE, 1024, &Ptr)); + ASSERT_NE(Ptr, nullptr); + + bool IsAccessible = false; + ASSERT_SUCCESS(olMemIsAccessible(Device, Ptr, 1024, &IsAccessible)); + ASSERT_TRUE(IsAccessible); + + ASSERT_SUCCESS(olMemFree(Ptr)); +} + +TEST_P(olMemIsAccessibleTest, SuccessHostAllocation) { + void *Ptr = nullptr; + ASSERT_SUCCESS(olMemAllocHost(Device, 1024, &Ptr)); + ASSERT_NE(Ptr, nullptr); + + bool IsAccessible = false; + ASSERT_SUCCESS(olMemIsAccessible(Device, Ptr, 1024, &IsAccessible)); + ASSERT_TRUE(IsAccessible); + + ASSERT_SUCCESS(olMemFree(Ptr)); +} + +TEST_P(olMemIsAccessibleTest, InvalidNullPointer) { + bool IsAccessible = false; + ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, + olMemIsAccessible(Device, nullptr, 1024, &IsAccessible)); +} _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
