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

Michael Klemm via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 16 02:08:59 PDT 2025


================
@@ -196,6 +196,34 @@ EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {
   return Rc;
 }
 
+/// Check whether a pointer is accessible from a device.
+/// Returns true when accessibility is guaranteed otherwise returns false.
+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) {
+    DP("Call to omp_target_is_accessible with NULL ptr returning false\n");
+    return false;
+  }
+
+  if (DeviceNum == omp_get_initial_device() || DeviceNum == -1) {
+    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());
----------------
mjklemm wrote:

I think crashing is the better behavior for several reasons.

1) It is in line with what the other routines do and also how kernel invocations fail when the device number does not exist.

2) While returning would be OK, because passing an incorrect device number does constitute unspecified behavior and the implementation is allowed to not check for this to happen, it may make debugging much harder. The failing code will return a seemingly useful result and thus introduce some inconsistent state in the program. I would expect that shortly after the code would re-use incorrect device number to invoke a kernel and thus fail there.  So, we can also fail early.

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


More information about the cfe-commits mailing list