[llvm-branch-commits] [llvm] [offload][omp] Move is_accessible_ptr to liboffload (PR #226446)

Alex Duran via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Sep 25 07:59:48 PDT 2026


https://github.com/adurang updated https://github.com/llvm/llvm-project/pull/226446

>From 8c436bc6ffbcb97908e81611e430f14f910afcbb 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/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 565f3fc62e056..212a802e3388d 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 5021ac21227b7..237356f460a8f 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -1505,6 +1505,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 5e2df4694f2ae..5a2a73a11b045 100644
--- a/offload/libompaccsupport/device.cpp
+++ b/offload/libompaccsupport/device.cpp
@@ -580,5 +580,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 30cf95ef61b12..fff91225ee8ff 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -1787,9 +1787,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 dc467bb431d98..48dc15cd0a31d 100644
--- a/offload/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp
@@ -1686,22 +1686,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