[Openmp-commits] [PATCH] D84381: [OpenMP] Fixed an issue that memory free might be called inappropriately when the kernel is still running

Shilei Tian via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Wed Jul 22 19:27:31 PDT 2020


tianshilei1992 created this revision.
Herald added subscribers: openmp-commits, sstefan1, guansong, yaxunl.
Herald added a reviewer: jdoerfert.
Herald added a project: OpenMP.

In the function `target`, memory deallocation and target_data_end is called
immediately returning from launching kernel. This might cause a race condition
that the corresponding memory is still being used by the kernel, and a optential
issue that when the kernel starts to execute, its required data have already
been deallocated, especially when multiple kernels running concurrently. Since
nevertheless we will block the thread issuing the target offloading at the end
of `target`, we just move the synchronization ahead a little bit to make sure
the correctness.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84381

Files:
  openmp/libomptarget/src/omptarget.cpp


Index: openmp/libomptarget/src/omptarget.cpp
===================================================================
--- openmp/libomptarget/src/omptarget.cpp
+++ openmp/libomptarget/src/omptarget.cpp
@@ -927,6 +927,14 @@
     return OFFLOAD_FAIL;
   }
 
+  if (Device.RTL->synchronize) {
+    rc = Device.RTL->synchronize(device_id, &AsyncInfo);
+    if (rc != OFFLOAD_SUCCESS) {
+      DP("Failed to synchronize.\n");
+      return OFFLOAD_FAIL;
+    }
+  }
+
   // Deallocate (first-)private arrays
   for (auto it : fpArrays) {
     int rt = Device.RTL->data_delete(Device.RTLDeviceID, it);
@@ -944,8 +952,5 @@
     return OFFLOAD_FAIL;
   }
 
-  if (Device.RTL->synchronize)
-    return Device.RTL->synchronize(device_id, &AsyncInfo);
-
   return OFFLOAD_SUCCESS;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84381.280007.patch
Type: text/x-patch
Size: 768 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20200723/6bd34945/attachment-0001.bin>


More information about the Openmp-commits mailing list