diff --git a/openmp/libomptarget/src/omptarget.cpp b/openmp/libomptarget/src/omptarget.cpp --- a/openmp/libomptarget/src/omptarget.cpp +++ b/openmp/libomptarget/src/omptarget.cpp @@ -927,6 +927,19 @@ return OFFLOAD_FAIL; } + // This synchronization makes sure that before we deallocate any memory, the + // kernel has already finished. Otherwise, data will be freed when they're + // still being used. + // TODO: If we can separate the memory free and data transfer, we can avoid + // this synchronization. + 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,6 +957,8 @@ return OFFLOAD_FAIL; } + // This synchronization makes sure that all data transfer have been done + // before returning from this function. if (Device.RTL->synchronize) return Device.RTL->synchronize(device_id, &AsyncInfo);