551 lines · cpp
1//===-- OpenMP/Mapping.cpp - OpenMP/OpenACC pointer mapping impl. ---------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9//===----------------------------------------------------------------------===//10 11#include "OpenMP/Mapping.h"12 13#include "PluginManager.h"14#include "Shared/Debug.h"15#include "Shared/Requirements.h"16#include "device.h"17 18/// Dump a table of all the host-target pointer pairs on failure19void dumpTargetPointerMappings(const ident_t *Loc, DeviceTy &Device,20 bool toStdOut) {21 MappingInfoTy::HDTTMapAccessorTy HDTTMap =22 Device.getMappingInfo().HostDataToTargetMap.getExclusiveAccessor();23 if (HDTTMap->empty()) {24 DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,25 "OpenMP Host-Device pointer mappings table empty\n");26 return;27 }28 29 SourceInfo Kernel(Loc);30 DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,31 "OpenMP Host-Device pointer mappings after block at %s:%d:%d:\n",32 Kernel.getFilename(), Kernel.getLine(), Kernel.getColumn());33 DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,34 "%-18s %-18s %s %s %s %s\n", "Host Ptr", "Target Ptr", "Size (B)",35 "DynRefCount", "HoldRefCount", "Declaration");36 for (const auto &It : *HDTTMap) {37 HostDataToTargetTy &HDTT = *It.HDTT;38 SourceInfo Info(HDTT.HstPtrName);39 DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,40 DPxMOD " " DPxMOD " %-8" PRIuPTR " %-11s %-12s %s at %s:%d:%d\n",41 DPxPTR(HDTT.HstPtrBegin), DPxPTR(HDTT.TgtPtrBegin),42 HDTT.HstPtrEnd - HDTT.HstPtrBegin,43 HDTT.dynRefCountToStr().c_str(), HDTT.holdRefCountToStr().c_str(),44 Info.getName(), Info.getFilename(), Info.getLine(),45 Info.getColumn());46 }47}48 49int MappingInfoTy::associatePtr(void *HstPtrBegin, void *TgtPtrBegin,50 int64_t Size) {51 HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();52 53 // Check if entry exists54 auto It = HDTTMap->find(HstPtrBegin);55 if (It != HDTTMap->end()) {56 HostDataToTargetTy &HDTT = *It->HDTT;57 std::lock_guard<HostDataToTargetTy> LG(HDTT);58 // Mapping already exists59 bool IsValid = HDTT.HstPtrEnd == (uintptr_t)HstPtrBegin + Size &&60 HDTT.TgtPtrBegin == (uintptr_t)TgtPtrBegin;61 if (IsValid) {62 DP("Attempt to re-associate the same device ptr+offset with the same "63 "host ptr, nothing to do\n");64 return OFFLOAD_SUCCESS;65 }66 REPORT("Not allowed to re-associate a different device ptr+offset with "67 "the same host ptr\n");68 return OFFLOAD_FAIL;69 }70 71 // Mapping does not exist, allocate it with refCount=INF72 const HostDataToTargetTy &NewEntry =73 *HDTTMap74 ->emplace(new HostDataToTargetTy(75 /*HstPtrBase=*/(uintptr_t)HstPtrBegin,76 /*HstPtrBegin=*/(uintptr_t)HstPtrBegin,77 /*HstPtrEnd=*/(uintptr_t)HstPtrBegin + Size,78 /*TgtAllocBegin=*/(uintptr_t)TgtPtrBegin,79 /*TgtPtrBegin=*/(uintptr_t)TgtPtrBegin,80 /*UseHoldRefCount=*/false, /*Name=*/nullptr,81 /*IsRefCountINF=*/true))82 .first->HDTT;83 DP("Creating new map entry: HstBase=" DPxMOD ", HstBegin=" DPxMOD84 ", HstEnd=" DPxMOD ", TgtBegin=" DPxMOD ", DynRefCount=%s, "85 "HoldRefCount=%s\n",86 DPxPTR(NewEntry.HstPtrBase), DPxPTR(NewEntry.HstPtrBegin),87 DPxPTR(NewEntry.HstPtrEnd), DPxPTR(NewEntry.TgtPtrBegin),88 NewEntry.dynRefCountToStr().c_str(), NewEntry.holdRefCountToStr().c_str());89 (void)NewEntry;90 91 // Notify the plugin about the new mapping.92 return Device.notifyDataMapped(HstPtrBegin, Size);93}94 95int MappingInfoTy::disassociatePtr(void *HstPtrBegin) {96 HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();97 98 auto It = HDTTMap->find(HstPtrBegin);99 if (It == HDTTMap->end()) {100 REPORT("Association not found\n");101 return OFFLOAD_FAIL;102 }103 // Mapping exists104 HostDataToTargetTy &HDTT = *It->HDTT;105 std::lock_guard<HostDataToTargetTy> LG(HDTT);106 107 if (HDTT.getHoldRefCount()) {108 // This is based on OpenACC 3.1, sec 3.2.33 "acc_unmap_data", L3656-3657:109 // "It is an error to call acc_unmap_data if the structured reference110 // count for the pointer is not zero."111 REPORT("Trying to disassociate a pointer with a non-zero hold reference "112 "count\n");113 return OFFLOAD_FAIL;114 }115 116 if (HDTT.isDynRefCountInf()) {117 DP("Association found, removing it\n");118 void *Event = HDTT.getEvent();119 delete &HDTT;120 if (Event)121 Device.destroyEvent(Event);122 HDTTMap->erase(It);123 return Device.notifyDataUnmapped(HstPtrBegin);124 }125 126 REPORT("Trying to disassociate a pointer which was not mapped via "127 "omp_target_associate_ptr\n");128 return OFFLOAD_FAIL;129}130 131LookupResult MappingInfoTy::lookupMapping(HDTTMapAccessorTy &HDTTMap,132 void *HstPtrBegin, int64_t Size,133 HostDataToTargetTy *OwnedTPR) {134 135 uintptr_t HP = (uintptr_t)HstPtrBegin;136 LookupResult LR;137 138 DP("Looking up mapping(HstPtrBegin=" DPxMOD ", Size=%" PRId64 ")...\n",139 DPxPTR(HP), Size);140 141 if (HDTTMap->empty())142 return LR;143 144 // HDTTMap is std::set, ordered by HstPtrBegin.145 // Upper is the first element whose HstPtrBegin > HP.146 auto Upper = HDTTMap->upper_bound(HP);147 148 if (Size == 0) {149 // HP satisfies150 // std::prev(Upper)->HDTT.HstPtrBegin <= HP < Upper->HDTT.HstPtrBegin151 if (Upper != HDTTMap->begin()) {152 LR.TPR.setEntry(std::prev(Upper)->HDTT, OwnedTPR);153 // We know that HP >= LR.TPR.getEntry()->HstPtrBegin154 LR.Flags.IsContained = HP < LR.TPR.getEntry()->HstPtrEnd;155 }156 157 if (!LR.Flags.IsContained && Upper != HDTTMap->end()) {158 LR.TPR.setEntry(Upper->HDTT, OwnedTPR);159 // This is a special case: HP is not really contained in the mapped160 // address range, but it's contained in the extended address range,161 // which suffices to get the mapping of the base pointer.162 // We know that HP < LR.TPR.getEntry()->HstPtrBegin163 LR.Flags.IsContained = HP >= LR.TPR.getEntry()->HstPtrBase;164 }165 } else {166 if (Upper != HDTTMap->begin()) {167 LR.TPR.setEntry(std::prev(Upper)->HDTT, OwnedTPR);168 // We know that HP >= LR.TPR.getEntry()->HstPtrBegin169 LR.Flags.IsContained = HP < LR.TPR.getEntry()->HstPtrEnd &&170 (HP + Size) <= LR.TPR.getEntry()->HstPtrEnd;171 // Does it extend beyond the mapped address range?172 LR.Flags.ExtendsAfter = HP < LR.TPR.getEntry()->HstPtrEnd &&173 (HP + Size) > LR.TPR.getEntry()->HstPtrEnd;174 }175 176 if (!(LR.Flags.IsContained || LR.Flags.ExtendsAfter) &&177 Upper != HDTTMap->end()) {178 LR.TPR.setEntry(Upper->HDTT, OwnedTPR);179 // Does it extend into an already mapped address range?180 // We know that HP < LR.TPR.getEntry()->HstPtrBegin181 LR.Flags.ExtendsBefore = (HP + Size) > LR.TPR.getEntry()->HstPtrBegin;182 // Does it extend beyond the mapped address range?183 LR.Flags.ExtendsAfter = HP < LR.TPR.getEntry()->HstPtrEnd &&184 (HP + Size) > LR.TPR.getEntry()->HstPtrEnd;185 }186 187 if (LR.Flags.ExtendsBefore) {188 DP("WARNING: Pointer is not mapped but section extends into already "189 "mapped data\n");190 }191 if (LR.Flags.ExtendsAfter) {192 DP("WARNING: Pointer is already mapped but section extends beyond mapped "193 "region\n");194 }195 }196 197 return LR;198}199 200TargetPointerResultTy MappingInfoTy::getTargetPointer(201 HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin, void *HstPtrBase,202 int64_t TgtPadding, int64_t Size, map_var_info_t HstPtrName, bool HasFlagTo,203 bool HasFlagAlways, bool IsImplicit, bool UpdateRefCount,204 bool HasCloseModifier, bool HasPresentModifier, bool HasHoldModifier,205 AsyncInfoTy &AsyncInfo, HostDataToTargetTy *OwnedTPR, bool ReleaseHDTTMap) {206 207 LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size, OwnedTPR);208 LR.TPR.Flags.IsPresent = true;209 210 // Release the mapping table lock only after the entry is locked by211 // attaching it to TPR. Once TPR is destroyed it will release the lock212 // on entry. If it is returned the lock will move to the returned object.213 // If LR.Entry is already owned/locked we avoid trying to lock it again.214 215 // Check if the pointer is contained.216 // If a variable is mapped to the device manually by the user - which would217 // lead to the IsContained flag to be true - then we must ensure that the218 // device address is returned even under unified memory conditions.219 if (LR.Flags.IsContained ||220 ((LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) && IsImplicit)) {221 const char *RefCountAction;222 if (UpdateRefCount) {223 // After this, reference count >= 1. If the reference count was 0 but the224 // entry was still there we can reuse the data on the device and avoid a225 // new submission.226 LR.TPR.getEntry()->incRefCount(HasHoldModifier);227 RefCountAction = " (incremented)";228 } else {229 // It might have been allocated with the parent, but it's still new.230 LR.TPR.Flags.IsNewEntry = LR.TPR.getEntry()->getTotalRefCount() == 1;231 RefCountAction = " (update suppressed)";232 }233 const char *DynRefCountAction = HasHoldModifier ? "" : RefCountAction;234 const char *HoldRefCountAction = HasHoldModifier ? RefCountAction : "";235 uintptr_t Ptr = LR.TPR.getEntry()->TgtPtrBegin +236 ((uintptr_t)HstPtrBegin - LR.TPR.getEntry()->HstPtrBegin);237 INFO(OMP_INFOTYPE_MAPPING_EXISTS, Device.DeviceID,238 "Mapping exists%s with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD239 ", Size=%" PRId64 ", DynRefCount=%s%s, HoldRefCount=%s%s, Name=%s\n",240 (IsImplicit ? " (implicit)" : ""), DPxPTR(HstPtrBegin), DPxPTR(Ptr),241 Size, LR.TPR.getEntry()->dynRefCountToStr().c_str(), DynRefCountAction,242 LR.TPR.getEntry()->holdRefCountToStr().c_str(), HoldRefCountAction,243 (HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "unknown");244 LR.TPR.TargetPointer = (void *)Ptr;245 } else if ((LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) && !IsImplicit) {246 // Explicit extension of mapped data - not allowed.247 MESSAGE("explicit extension not allowed: host address specified is " DPxMOD248 " (%" PRId64249 " bytes), but device allocation maps to host at " DPxMOD250 " (%" PRId64 " bytes)",251 DPxPTR(HstPtrBegin), Size, DPxPTR(LR.TPR.getEntry()->HstPtrBegin),252 LR.TPR.getEntry()->HstPtrEnd - LR.TPR.getEntry()->HstPtrBegin);253 if (HasPresentModifier)254 MESSAGE("device mapping required by 'present' map type modifier does not "255 "exist for host address " DPxMOD " (%" PRId64 " bytes)",256 DPxPTR(HstPtrBegin), Size);257 } else if ((PM->getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY &&258 !HasCloseModifier) ||259 (PM->getRequirements() & OMPX_REQ_AUTO_ZERO_COPY)) {260 261 // If unified shared memory is active, implicitly mapped variables that are262 // not privatized use host address. Any explicitly mapped variables also use263 // host address where correctness is not impeded. In all other cases maps264 // are respected.265 // In addition to the mapping rules above, the close map modifier forces the266 // mapping of the variable to the device.267 if (Size) {268 INFO(OMP_INFOTYPE_MAPPING_CHANGED, Device.DeviceID,269 "Return HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared "270 "memory\n",271 DPxPTR((uintptr_t)HstPtrBegin), Size);272 DP("Return HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared "273 "memory\n",274 DPxPTR((uintptr_t)HstPtrBegin), Size);275 LR.TPR.Flags.IsPresent = false;276 LR.TPR.Flags.IsHostPointer = true;277 LR.TPR.TargetPointer = HstPtrBegin;278 }279 } else if (HasPresentModifier) {280 DP("Mapping required by 'present' map type modifier does not exist for "281 "HstPtrBegin=" DPxMOD ", Size=%" PRId64 "\n",282 DPxPTR(HstPtrBegin), Size);283 MESSAGE("device mapping required by 'present' map type modifier does not "284 "exist for host address " DPxMOD " (%" PRId64 " bytes)",285 DPxPTR(HstPtrBegin), Size);286 } else if (Size) {287 // If it is not contained and Size > 0, we should create a new entry for it.288 LR.TPR.Flags.IsNewEntry = true;289 uintptr_t TgtAllocBegin =290 (uintptr_t)Device.allocData(TgtPadding + Size, HstPtrBegin);291 uintptr_t TgtPtrBegin = TgtAllocBegin + TgtPadding;292 // Release the mapping table lock only after the entry is locked by293 // attaching it to TPR.294 LR.TPR.setEntry(HDTTMap295 ->emplace(new HostDataToTargetTy(296 (uintptr_t)HstPtrBase, (uintptr_t)HstPtrBegin,297 (uintptr_t)HstPtrBegin + Size, TgtAllocBegin,298 TgtPtrBegin, HasHoldModifier, HstPtrName))299 .first->HDTT);300 INFO(OMP_INFOTYPE_MAPPING_CHANGED, Device.DeviceID,301 "Creating new map entry with HstPtrBase=" DPxMOD302 ", HstPtrBegin=" DPxMOD ", TgtAllocBegin=" DPxMOD303 ", TgtPtrBegin=" DPxMOD304 ", Size=%ld, DynRefCount=%s, HoldRefCount=%s, Name=%s\n",305 DPxPTR(HstPtrBase), DPxPTR(HstPtrBegin), DPxPTR(TgtAllocBegin),306 DPxPTR(TgtPtrBegin), Size,307 LR.TPR.getEntry()->dynRefCountToStr().c_str(),308 LR.TPR.getEntry()->holdRefCountToStr().c_str(),309 (HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "unknown");310 LR.TPR.TargetPointer = (void *)TgtPtrBegin;311 312 // Notify the plugin about the new mapping.313 if (Device.notifyDataMapped(HstPtrBegin, Size))314 return TargetPointerResultTy{};315 } else {316 // This entry is not present and we did not create a new entry for it.317 LR.TPR.Flags.IsPresent = false;318 }319 320 // All mapping table modifications have been made. If the user requested it we321 // give up the lock.322 if (ReleaseHDTTMap)323 HDTTMap.destroy();324 325 // If the target pointer is valid, and we need to transfer data, issue the326 // data transfer.327 if (LR.TPR.TargetPointer && !LR.TPR.Flags.IsHostPointer && HasFlagTo &&328 (LR.TPR.Flags.IsNewEntry || HasFlagAlways) && Size != 0) {329 330 // If we have something like:331 // #pragma omp target map(to: s.myarr[0:10]) map(to: s.myarr[0:10])332 // then we see two "new" mappings of the struct member s.myarr here --333 // and both have the "IsNewEntry" flag set, so trigger the copy to device334 // below. But, the shadow pointer is only initialised on the target for335 // the first copy, and the second copy clobbers it. So, this condition336 // avoids the (second) copy here if we have already set shadow pointer info.337 auto FailOnPtrFound = [HstPtrBegin, Size](ShadowPtrInfoTy &SP) {338 if (SP.HstPtrAddr >= HstPtrBegin &&339 SP.HstPtrAddr < (void *)((char *)HstPtrBegin + Size))340 return OFFLOAD_FAIL;341 return OFFLOAD_SUCCESS;342 };343 if (LR.TPR.getEntry()->foreachShadowPointerInfo(FailOnPtrFound) ==344 OFFLOAD_FAIL) {345 DP("Multiple new mappings of %" PRId64 " bytes detected (hst:" DPxMOD346 ") -> (tgt:" DPxMOD ")\n",347 Size, DPxPTR(HstPtrBegin), DPxPTR(LR.TPR.TargetPointer));348 return std::move(LR.TPR);349 }350 351 DP("Moving %" PRId64 " bytes (hst:" DPxMOD ") -> (tgt:" DPxMOD ")\n", Size,352 DPxPTR(HstPtrBegin), DPxPTR(LR.TPR.TargetPointer));353 354 int Ret = Device.submitData(LR.TPR.TargetPointer, HstPtrBegin, Size,355 AsyncInfo, LR.TPR.getEntry());356 if (Ret != OFFLOAD_SUCCESS) {357 REPORT("Copying data to device failed.\n");358 // We will also return nullptr if the data movement fails because that359 // pointer points to a corrupted memory region so it doesn't make any360 // sense to continue to use it.361 LR.TPR.TargetPointer = nullptr;362 } else if (LR.TPR.getEntry()->addEventIfNecessary(Device, AsyncInfo) !=363 OFFLOAD_SUCCESS)364 return TargetPointerResultTy{};365 } else {366 // If not a host pointer and no present modifier, we need to wait for the367 // event if it exists.368 // Note: Entry might be nullptr because of zero length array section.369 if (LR.TPR.getEntry() && !LR.TPR.Flags.IsHostPointer &&370 !HasPresentModifier) {371 void *Event = LR.TPR.getEntry()->getEvent();372 if (Event) {373 int Ret = Device.waitEvent(Event, AsyncInfo);374 if (Ret != OFFLOAD_SUCCESS) {375 // If it fails to wait for the event, we need to return nullptr in376 // case of any data race.377 REPORT("Failed to wait for event " DPxMOD ".\n", DPxPTR(Event));378 return TargetPointerResultTy{};379 }380 }381 }382 }383 384 return std::move(LR.TPR);385}386 387TargetPointerResultTy MappingInfoTy::getTgtPtrBegin(388 void *HstPtrBegin, int64_t Size, bool UpdateRefCount, bool UseHoldRefCount,389 bool MustContain, bool ForceDelete, bool FromDataEnd) {390 HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();391 392 LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size);393 394 LR.TPR.Flags.IsPresent = true;395 396 if (LR.Flags.IsContained ||397 (!MustContain && (LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter))) {398 LR.TPR.Flags.IsLast =399 LR.TPR.getEntry()->decShouldRemove(UseHoldRefCount, ForceDelete);400 401 if (ForceDelete) {402 LR.TPR.getEntry()->resetRefCount(UseHoldRefCount);403 assert(LR.TPR.Flags.IsLast ==404 LR.TPR.getEntry()->decShouldRemove(UseHoldRefCount) &&405 "expected correct IsLast prediction for reset");406 }407 408 // Increment the number of threads that is using the entry on a409 // targetDataEnd, tracking the number of possible "deleters". A thread may410 // come to own the entry deletion even if it was not the last one querying411 // for it. Thus, we must track every query on targetDataEnds to ensure only412 // the last thread that holds a reference to an entry actually deletes it.413 if (FromDataEnd)414 LR.TPR.getEntry()->incDataEndThreadCount();415 416 const char *RefCountAction;417 if (!UpdateRefCount) {418 RefCountAction = " (update suppressed)";419 } else if (LR.TPR.Flags.IsLast) {420 LR.TPR.getEntry()->decRefCount(UseHoldRefCount);421 assert(LR.TPR.getEntry()->getTotalRefCount() == 0 &&422 "Expected zero reference count when deletion is scheduled");423 if (ForceDelete)424 RefCountAction = " (reset, delayed deletion)";425 else426 RefCountAction = " (decremented, delayed deletion)";427 } else {428 LR.TPR.getEntry()->decRefCount(UseHoldRefCount);429 RefCountAction = " (decremented)";430 }431 const char *DynRefCountAction = UseHoldRefCount ? "" : RefCountAction;432 const char *HoldRefCountAction = UseHoldRefCount ? RefCountAction : "";433 uintptr_t TP = LR.TPR.getEntry()->TgtPtrBegin +434 ((uintptr_t)HstPtrBegin - LR.TPR.getEntry()->HstPtrBegin);435 INFO(OMP_INFOTYPE_MAPPING_EXISTS, Device.DeviceID,436 "Mapping exists with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD ", "437 "Size=%" PRId64 ", DynRefCount=%s%s, HoldRefCount=%s%s\n",438 DPxPTR(HstPtrBegin), DPxPTR(TP), Size,439 LR.TPR.getEntry()->dynRefCountToStr().c_str(), DynRefCountAction,440 LR.TPR.getEntry()->holdRefCountToStr().c_str(), HoldRefCountAction);441 LR.TPR.TargetPointer = (void *)TP;442 } else if (PM->getRequirements() & OMP_REQ_UNIFIED_SHARED_MEMORY ||443 PM->getRequirements() & OMPX_REQ_AUTO_ZERO_COPY) {444 // If the value isn't found in the mapping and unified shared memory445 // is on then it means we have stumbled upon a value which we need to446 // use directly from the host.447 DP("Get HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared "448 "memory\n",449 DPxPTR((uintptr_t)HstPtrBegin), Size);450 LR.TPR.Flags.IsPresent = false;451 LR.TPR.Flags.IsHostPointer = true;452 LR.TPR.TargetPointer = HstPtrBegin;453 } else {454 // OpenMP Specification v5.2: if a matching list item is not found, the455 // pointer retains its original value as per firstprivate semantics.456 LR.TPR.Flags.IsPresent = false;457 LR.TPR.Flags.IsHostPointer = false;458 LR.TPR.TargetPointer = HstPtrBegin;459 }460 461 return std::move(LR.TPR);462}463 464// Return the target pointer begin (where the data will be moved).465void *MappingInfoTy::getTgtPtrBegin(HDTTMapAccessorTy &HDTTMap,466 void *HstPtrBegin, int64_t Size) {467 uintptr_t HP = (uintptr_t)HstPtrBegin;468 LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size);469 if (LR.Flags.IsContained || LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) {470 uintptr_t TP =471 LR.TPR.getEntry()->TgtPtrBegin + (HP - LR.TPR.getEntry()->HstPtrBegin);472 return (void *)TP;473 }474 475 return NULL;476}477 478int MappingInfoTy::eraseMapEntry(HDTTMapAccessorTy &HDTTMap,479 HostDataToTargetTy *Entry, int64_t Size) {480 assert(Entry && "Trying to delete a null entry from the HDTT map.");481 assert(Entry->getTotalRefCount() == 0 &&482 Entry->getDataEndThreadCount() == 0 &&483 "Trying to delete entry that is in use or owned by another thread.");484 485 INFO(OMP_INFOTYPE_MAPPING_CHANGED, Device.DeviceID,486 "Removing map entry with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD487 ", Size=%" PRId64 ", Name=%s\n",488 DPxPTR(Entry->HstPtrBegin), DPxPTR(Entry->TgtPtrBegin), Size,489 (Entry->HstPtrName) ? getNameFromMapping(Entry->HstPtrName).c_str()490 : "unknown");491 492 if (HDTTMap->erase(Entry) == 0) {493 REPORT("Trying to remove a non-existent map entry\n");494 return OFFLOAD_FAIL;495 }496 497 return OFFLOAD_SUCCESS;498}499 500int MappingInfoTy::deallocTgtPtrAndEntry(HostDataToTargetTy *Entry,501 int64_t Size) {502 assert(Entry && "Trying to deallocate a null entry.");503 504 DP("Deleting tgt data " DPxMOD " of size %" PRId64 " by freeing allocation "505 "starting at " DPxMOD "\n",506 DPxPTR(Entry->TgtPtrBegin), Size, DPxPTR(Entry->TgtAllocBegin));507 508 void *Event = Entry->getEvent();509 if (Event && Device.destroyEvent(Event) != OFFLOAD_SUCCESS) {510 REPORT("Failed to destroy event " DPxMOD "\n", DPxPTR(Event));511 return OFFLOAD_FAIL;512 }513 514 int Ret = Device.deleteData((void *)Entry->TgtAllocBegin);515 516 // Notify the plugin about the unmapped memory.517 Ret |= Device.notifyDataUnmapped((void *)Entry->HstPtrBegin);518 519 delete Entry;520 521 return Ret;522}523 524static void printCopyInfoImpl(int DeviceId, bool H2D, void *SrcPtrBegin,525 void *DstPtrBegin, int64_t Size,526 HostDataToTargetTy *HT) {527 528 INFO(OMP_INFOTYPE_DATA_TRANSFER, DeviceId,529 "Copying data from %s to %s, %sPtr=" DPxMOD ", %sPtr=" DPxMOD530 ", Size=%" PRId64 ", Name=%s\n",531 H2D ? "host" : "device", H2D ? "device" : "host", H2D ? "Hst" : "Tgt",532 DPxPTR(H2D ? SrcPtrBegin : DstPtrBegin), H2D ? "Tgt" : "Hst",533 DPxPTR(H2D ? DstPtrBegin : SrcPtrBegin), Size,534 (HT && HT->HstPtrName) ? getNameFromMapping(HT->HstPtrName).c_str()535 : "unknown");536}537 538void MappingInfoTy::printCopyInfo(539 void *TgtPtrBegin, void *HstPtrBegin, int64_t Size, bool H2D,540 HostDataToTargetTy *Entry, MappingInfoTy::HDTTMapAccessorTy *HDTTMapPtr) {541 auto HDTTMap =542 HostDataToTargetMap.getExclusiveAccessor(!!Entry || !!HDTTMapPtr);543 LookupResult LR;544 if (!Entry) {545 LR = lookupMapping(HDTTMapPtr ? *HDTTMapPtr : HDTTMap, HstPtrBegin, Size);546 Entry = LR.TPR.getEntry();547 }548 printCopyInfoImpl(Device.DeviceID, H2D, HstPtrBegin, TgtPtrBegin, Size,549 Entry);550}551