[llvm] [Offload] Unlock mapping entry before deleting it in disassociatePtr (PR #223319)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 02:43:01 PDT 2026


================
@@ -107,32 +107,39 @@ int MappingInfoTy::disassociatePtr(void *HstPtrBegin) {
     REPORT() << "Association not found";
     return OFFLOAD_FAIL;
   }
-  // Mapping exists
-  HostDataToTargetTy &HDTT = *It->HDTT;
-  std::lock_guard<HostDataToTargetTy> LG(HDTT);
-
-  if (HDTT.getHoldRefCount()) {
-    // This is based on OpenACC 3.1, sec 3.2.33 "acc_unmap_data", L3656-3657:
-    // "It is an error to call acc_unmap_data if the structured reference
-    // count for the pointer is not zero."
-    REPORT() << "Trying to disassociate a pointer with a non-zero "
-             << "hold reference count";
-    return OFFLOAD_FAIL;
-  }
 
-  if (HDTT.isDynRefCountInf()) {
+  // Mapping exists. The per-entry mutex is a member of HostDataToTargetTy, so
+  // it must be released before the entry is destroyed.
+  HostDataToTargetTy *Entry = It->HDTT;
+  void *Event = nullptr;
+  {
+    std::lock_guard<HostDataToTargetTy> LG(*Entry);
+
+    if (Entry->getHoldRefCount()) {
+      // This is based on OpenACC 3.1, sec 3.2.33 "acc_unmap_data", L3656-3657:
+      // "It is an error to call acc_unmap_data if the structured reference
+      // count for the pointer is not zero."
+      REPORT() << "Trying to disassociate a pointer with a non-zero "
+               << "hold reference count";
+      return OFFLOAD_FAIL;
+    }
+
+    if (!Entry->isDynRefCountInf()) {
+      REPORT() << "Trying to disassociate a pointer which was not mapped via "
+               << "omp_target_associate_ptr";
+      return OFFLOAD_FAIL;
+    }
+
     ODBG(ODT_Mapping) << "Association found, removing it";
-    void *Event = HDTT.getEvent();
-    delete &HDTT;
-    if (Event)
-      Device.destroyEvent(Event);
-    HDTTMap->erase(It);
-    return Device.notifyDataUnmapped(HstPtrBegin);
+    Event = Entry->getEvent();
   }
 
-  REPORT() << "Trying to disassociate a pointer which was not mapped via "
-           << "omp_target_associate_ptr";
-  return OFFLOAD_FAIL;
+  HDTTMap->erase(It);
----------------
StevenYangCC wrote:

Yes. Once we disassociate, erase always runs. `if (Event)` only guards destroyEvent; the event is optional. I added a comment to make that clear.

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


More information about the llvm-commits mailing list