[llvm] [OpenMP] Adds omp_target_is_accessible routine (PR #138294)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 14 07:36:08 PDT 2025


================
@@ -195,6 +195,49 @@ EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {
   return Rc;
 }
 
+/// Check whether a pointer is accessible from a device.
+/// the functionality is available in OpenMP 5.1 and later
+/// OpenMP 5.1
+/// omp_target_is_accessible checks whether a host pointer is accessible from a
+/// device OpenMP 6.0 removes restriction on pointer, allowing any pointer
+/// interpreted as a pointer in the address space of the given device.
+EXTERN int omp_target_is_accessible(const void *Ptr, size_t Size,
+                                    int DeviceNum) {
+  TIMESCOPE();
+  OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));
+  DP("Call to omp_target_is_accessible for device %d, address " DPxMOD
+     ", size %zu\n",
+     DeviceNum, DPxPTR(Ptr), Size);
+
+  if (!Ptr || Size == 0) {
+    DP("Call to omp_target_is_accessible with NULL ptr or size 0, returning "
+       "false\n");
+    return false;
+  }
+
+  if (DeviceNum == omp_get_initial_device()) {
+    DP("Call to omp_target_is_accessible on host, returning true\n");
+    return true;
+  }
+
+  // the device number must refer to a valid device
+  auto DeviceOrErr = PM->getDevice(DeviceNum);
+  if (!DeviceOrErr)
+    FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());
+
+  // for OpenMP 5.1 the routine checks whether a host pointer is accessible from
+  // the device this requires for the device to support unified shared memory
----------------
shiltian wrote:

ditto

https://github.com/llvm/llvm-project/pull/138294


More information about the llvm-commits mailing list