[llvm-branch-commits] [llvm] [OpenMP][Offload] Add FB_NULLIFY map-type for `use_device_ptr(fb_nullify)`. (1/4) (PR #169603)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jan 6 14:10:47 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-offload

Author: Abhinav Gaba (abhinavgaba)

<details>
<summary>Changes</summary>

Depends on #<!-- -->174659.

This PR adds a new map-type bit to control the fallback behavior when
when a pointer lookup fails.

For now, this is only meaningful with `RETURN_PARAM`, and can be used
for `need_device_ptr` (for which the default is to use `nullptr` as the result
when lookup fails), and OpenMP 6.1's `use_device_ptr(fb_nullify)`.

Eventually, this can be extended to work with assumed-size maps on `target`
constructs, to control what the argument should be set to when lookup
fails (the OpenMP spec does not have a way to control that yet).

---
Full diff: https://github.com/llvm/llvm-project/pull/169603.diff


3 Files Affected:

- (modified) llvm/include/llvm/Frontend/OpenMP/OMPConstants.h (+4) 
- (modified) offload/include/omptarget.h (+4) 
- (modified) offload/libomptarget/omptarget.cpp (+14-8) 


``````````diff
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
index 58fd8a490c04a..d2a1b5209ecba 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
@@ -252,6 +252,10 @@ enum class OpenMPOffloadMappingFlags : uint64_t {
   // Attach pointer and pointee, after processing all other maps.
   // Applicable to map-entering directives. Does not change ref-count.
   OMP_MAP_ATTACH = 0x4000,
+  // When a lookup fails, fall back to using null as the translated pointer,
+  // instead of preserving the original pointer's value. Currently only
+  // useful in conjunction with RETURN_PARAM.
+  OMP_MAP_FB_NULLIFY = 0x8000,
   /// Signal that the runtime library should use args as an array of
   /// descriptor_dim pointers and use args_size as dims. Used when we have
   /// non-contiguous list items in target update directive
diff --git a/offload/include/omptarget.h b/offload/include/omptarget.h
index fd458fa0e5d89..5dddd008aff4d 100644
--- a/offload/include/omptarget.h
+++ b/offload/include/omptarget.h
@@ -80,6 +80,10 @@ enum tgt_map_type {
   // Attach pointer and pointee, after processing all other maps.
   // Applicable to map-entering directives. Does not change ref-count.
   OMP_TGT_MAPTYPE_ATTACH = 0x4000,
+  // When a lookup fails, fall back to using null as the translated pointer,
+  // instead of preserving the original pointer's value. Currently only
+  // useful in conjunction with RETURN_PARAM.
+  OMP_TGT_MAPTYPE_FB_NULLIFY = 0x8000,
   // descriptor for non-contiguous target-update
   OMP_TGT_MAPTYPE_NON_CONTIG = 0x100000000000,
   // member of struct, member given by [16 MSBs] - 1
diff --git a/offload/libomptarget/omptarget.cpp b/offload/libomptarget/omptarget.cpp
index 676fda5fc8671..960c5bc17df96 100644
--- a/offload/libomptarget/omptarget.cpp
+++ b/offload/libomptarget/omptarget.cpp
@@ -707,14 +707,20 @@ int targetDataBegin(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
         // to references to a local device pointer that refers to this device
         // address.
         //
-        // TODO: Add a new map-type bit to support OpenMP 6.1's `fb_nullify`
-        // and set the result to `nullptr - Delta`. Note that `fb_nullify` is
-        // already the default for `need_device_ptr`, but clang/flang do not
-        // support its codegen yet.
-        TgtPtrBase = reinterpret_cast<void *>(
-            reinterpret_cast<uintptr_t>(HstPtrBegin) - Delta);
-        ODBG(ODT_Mapping) << "Returning host pointer " << TgtPtrBase
-                          << " as fallback (lookup failed)";
+        // OpenMP 6.1's `fb_nullify` fallback behavior: when the FB_NULLIFY bit
+        // is set by the compiler, e.g. for `use/need_device_ptr(fb_nullify)`),
+        // return `nullptr - Delta` when lookup fails.
+        if (ArgTypes[I] & OMP_TGT_MAPTYPE_FB_NULLIFY) {
+          TgtPtrBase = reinterpret_cast<void *>(
+              reinterpret_cast<uintptr_t>(nullptr) - Delta);
+          ODBG(ODT_Mapping) << "Returning offsetted null pointer " << TgtPtrBase
+                            << " as fallback (lookup failed)";
+        } else {
+          TgtPtrBase = reinterpret_cast<void *>(
+              reinterpret_cast<uintptr_t>(HstPtrBegin) - Delta);
+          ODBG(ODT_Mapping) << "Returning host pointer " << TgtPtrBase
+                            << " as fallback (lookup failed)";
+        }
       }
       ArgsBase[I] = TgtPtrBase;
     }

``````````

</details>


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


More information about the llvm-branch-commits mailing list