[llvm] [OpenMP][Offload] Handle `present/to/from` when a different entry did `alloc/delete`. (PR #165494)

Robert Imschweiler via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 01:51:12 PST 2026


================
@@ -495,28 +495,118 @@ struct AttachMapInfo {
         MapType(Type), Pointername(Name) {}
 };
 
-/// Structure to track ATTACH entries and new allocations across recursive calls
-/// (for handling mappers) to targetDataBegin for a given construct.
-struct AttachInfoTy {
-  /// ATTACH map entries for deferred processing.
+/// Structure to track new allocations, ATTACH entries, DELETE entries and
+/// skipped FROM data transfer information for a given construct, across
+/// recursive calls (for handling mappers) to targetDataBegin/targetDataEnd.
+struct StateInfoTy {
+  /// ATTACH map entries for deferred processing until all other maps are done.
   llvm::SmallVector<AttachMapInfo> AttachEntries;
 
+  /// Host pointers for which new memory was allocated.
   /// Key: host pointer, Value: allocation size.
   llvm::DenseMap<void *, int64_t> NewAllocations;
 
-  AttachInfoTy() = default;
+  /// Host pointers that had a FROM entry, but for which a data transfer was
+  /// skipped due to the ref-count not being zero.
+  /// Key: host pointer, Value: data size.
+  llvm::DenseMap<void *, int64_t> SkippedFromEntries;
+
+  /// Host pointers for which we have triggered a FROM transfer at some point
+  /// during targetDataEnd. It's used to avoid duplicate transfers.
+  /// Key: host pointer, Value: transferred size.
+  llvm::DenseMap<void *, int64_t> TransferredFromEntries;
+
+  /// Starting host address and size of entries whose ref-count went to zero.
+  /// This includes entries released through explicit DELETE, or normal
+  /// ref-count decrements. It's used to ensure transfers are performed for FROM
+  /// entries whose ref-count is already zero when the entry is encountered.
+  /// Key: host pointer, Value: size.
+  llvm::DenseMap<void *, int64_t> ReleasedEntries;
+
+  StateInfoTy() = default;
 
   // Delete copy constructor and copy assignment operator to prevent copying
-  AttachInfoTy(const AttachInfoTy &) = delete;
-  AttachInfoTy &operator=(const AttachInfoTy &) = delete;
+  StateInfoTy(const StateInfoTy &) = delete;
+  StateInfoTy &operator=(const StateInfoTy &) = delete;
+
+private:
+  /// Helper to find an entry in \p EntryMap that contains the pointer.
+  /// Returns the matching entry if found, otherwise std::nullopt.
+  std::optional<std::pair<void *, int64_t>>
+  findEntryForPtr(void *Ptr,
+                  const llvm::DenseMap<void *, int64_t> &EntryMap) const {
+    for (const auto &Entry : EntryMap) {
+      void *EntryBegin = Entry.first;
+      int64_t EntrySize = Entry.second;
+      if (Ptr >= EntryBegin &&
+          Ptr < static_cast<void *>(static_cast<char *>(EntryBegin) +
+                                    EntrySize)) {
+        return Entry;
+      }
+    }
+    return std::nullopt;
+  }
+
+public:
+  /// Check if a pointer falls within any of the newly allocated ranges.
+  /// Returns the matching entry if found, otherwise std::nullopt.
+  std::optional<std::pair<void *, int64_t>> wasNewlyAllocated(void *Ptr) const {
+    return findEntryForPtr(Ptr, NewAllocations);
+  }
+
+  /// Check if a pointer range [Ptr, Ptr+Size) is fully contained within any
+  /// previously completed FROM transfer.
+  /// Returns the matching entry if found, otherwise std::nullopt.
+  std::optional<std::pair<void *, int64_t>>
+  wasTransferredFrom(void *Ptr, int64_t Size) const {
+    uintptr_t CheckBegin = reinterpret_cast<uintptr_t>(Ptr);
----------------
ro-i wrote:

nit: I think you could use `static_cast` everywhere you're currently using `reinterpret_cast`

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


More information about the llvm-commits mailing list