[llvm-branch-commits] [llvm] [offload][omp] Manage memory allocation through liboffload (PR #221798)

Alex Duran via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Sep 7 11:08:41 PDT 2026


https://github.com/adurang created https://github.com/llvm/llvm-project/pull/221798

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.

---

<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

>From 783dd9d30b89ab4685d70bdc275aef76d881f5f9 Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
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/exports                    |  1 -
 offload/liboffload/src/OffloadImpl.cpp        | 14 ++++++
 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 +++++++++++++++++++
 8 files changed, 81 insertions(+), 22 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 17eabeccce9e0..740f051c96441 100644
--- a/offload/liboffload/API/Memory.td
+++ b/offload/liboffload/API/Memory.td
@@ -266,6 +266,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/exports b/offload/liboffload/exports
index 8cdef0ac747ad..cb0ec9a8380a2 100644
--- a/offload/liboffload/exports
+++ b/offload/liboffload/exports
@@ -22,7 +22,6 @@ global:
     "llvm::omp::target::plugin::GenericPluginTy::initialize_record_replay(int, long, void*, bool, bool, bool, bool, char const*, char const*)";
     "llvm::omp::target::plugin::GenericPluginTy::isDeviceCompatible(int, llvm::StringRef)";
     "llvm::omp::target::plugin::GenericPluginTy::isPluginCompatible(llvm::StringRef)";
-    "llvm::omp::target::plugin::GenericPluginTy::is_accessible_ptr(int, void const*, unsigned long)";
     "llvm::omp::target::plugin::GenericPluginTy::is_data_exchangable(int, int)";
     "llvm::omp::target::plugin::GenericPluginTy::is_initialized() const";
     "llvm::omp::target::plugin::GenericPluginTy::launch_kernel(int, void*, llvm::omp::target::plugin::KernelLaunchArgsTy&, __tgt_async_info*)";
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index 6a4626a613d29..352944852d214 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1652,6 +1652,20 @@ Error olMemUnregister_impl(ol_device_handle_t Device, void *Ptr,
       ->unregisterMemory(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 DeviceOrErr = Device->getDevice();
+  if (!DeviceOrErr)
+    return DeviceOrErr.takeError();
+
+  auto AccessibleOrErr = (*DeviceOrErr)->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) {
     auto DeviceOrErr = Queue->Device->getDevice();
diff --git a/offload/libompaccsupport/device.cpp b/offload/libompaccsupport/device.cpp
index 583773fcee091..5d15469705b70 100644
--- a/offload/libompaccsupport/device.cpp
+++ b/offload/libompaccsupport/device.cpp
@@ -574,5 +574,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 a5918718d09b3..0d282d2d73038 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -1758,9 +1758,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 9615e33cac306..053e15df5cb17 100644
--- a/offload/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp
@@ -1673,22 +1673,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 8bed11be4d85e..6e123b7861da0 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 0000000000000..fc9bd8b47f2b7
--- /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));
+}



More information about the llvm-branch-commits mailing list