1 //===------ omptarget.cpp - Target independent OpenMP target RTL -- C++ -*-===// 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Implementation of the interface to be used by Clang during the codegen of a 10 // target region. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include <omptarget.h> 15 16 #include "device.h" 17 #include "private.h" 18 #include "rtl.h" 19 20 #include <cassert> 21 #include <vector> 22 23 #ifdef OMPTARGET_DEBUG 24 int DebugLevel = 0; 25 #endif // OMPTARGET_DEBUG 26 27 28 29 /* All begin addresses for partially mapped structs must be 8-aligned in order 30 * to ensure proper alignment of members. E.g. 31 * 32 * struct S { 33 * int a; // 4-aligned 34 * int b; // 4-aligned 35 * int *p; // 8-aligned 36 * } s1; 37 * ... 38 * #pragma omp target map(tofrom: s1.b, s1.p[0:N]) 39 * { 40 * s1.b = 5; 41 * for (int i...) s1.p[i] = ...; 42 * } 43 * 44 * Here we are mapping s1 starting from member b, so BaseAddress=&s1=&s1.a and 45 * BeginAddress=&s1.b. Let's assume that the struct begins at address 0x100, 46 * then &s1.a=0x100, &s1.b=0x104, &s1.p=0x108. Each member obeys the alignment 47 * requirements for its type. Now, when we allocate memory on the device, in 48 * CUDA's case cuMemAlloc() returns an address which is at least 256-aligned. 49 * This means that the chunk of the struct on the device will start at a 50 * 256-aligned address, let's say 0x200. Then the address of b will be 0x200 and 51 * address of p will be a misaligned 0x204 (on the host there was no need to add 52 * padding between b and p, so p comes exactly 4 bytes after b). If the device 53 * kernel tries to access s1.p, a misaligned address error occurs (as reported 54 * by the CUDA plugin). By padding the begin address down to a multiple of 8 and 55 * extending the size of the allocated chuck accordingly, the chuck on the 56 * device will start at 0x200 with the padding (4 bytes), then &s1.b=0x204 and 57 * &s1.p=0x208, as they should be to satisfy the alignment requirements. 58 */ 59 static const int64_t Alignment = 8; 60 61 /// Map global data and execute pending ctors 62 static int InitLibrary(DeviceTy& Device) { 63 /* 64 * Map global data 65 */ 66 int32_t device_id = Device.DeviceID; 67 int rc = OFFLOAD_SUCCESS; 68 69 Device.PendingGlobalsMtx.lock(); 70 TrlTblMtx->lock(); 71 for (HostEntriesBeginToTransTableTy::iterator 72 ii = HostEntriesBeginToTransTable->begin(); 73 ii != HostEntriesBeginToTransTable->end(); ++ii) { 74 TranslationTable *TransTable = &ii->second; 75 if (TransTable->HostTable.EntriesBegin == 76 TransTable->HostTable.EntriesEnd) { 77 // No host entry so no need to proceed 78 continue; 79 } 80 if (TransTable->TargetsTable[device_id] != 0) { 81 // Library entries have already been processed 82 continue; 83 } 84 85 // 1) get image. 86 assert(TransTable->TargetsImages.size() > (size_t)device_id && 87 "Not expecting a device ID outside the table's bounds!"); 88 __tgt_device_image *img = TransTable->TargetsImages[device_id]; 89 if (!img) { 90 DP("No image loaded for device id %d.\n", device_id); 91 rc = OFFLOAD_FAIL; 92 break; 93 } 94 // 2) load image into the target table. 95 __tgt_target_table *TargetTable = 96 TransTable->TargetsTable[device_id] = Device.load_binary(img); 97 // Unable to get table for this image: invalidate image and fail. 98 if (!TargetTable) { 99 DP("Unable to generate entries table for device id %d.\n", device_id); 100 TransTable->TargetsImages[device_id] = 0; 101 rc = OFFLOAD_FAIL; 102 break; 103 } 104 105 // Verify whether the two table sizes match. 106 size_t hsize = 107 TransTable->HostTable.EntriesEnd - TransTable->HostTable.EntriesBegin; 108 size_t tsize = TargetTable->EntriesEnd - TargetTable->EntriesBegin; 109 110 // Invalid image for these host entries! 111 if (hsize != tsize) { 112 DP("Host and Target tables mismatch for device id %d [%zx != %zx].\n", 113 device_id, hsize, tsize); 114 TransTable->TargetsImages[device_id] = 0; 115 TransTable->TargetsTable[device_id] = 0; 116 rc = OFFLOAD_FAIL; 117 break; 118 } 119 120 // process global data that needs to be mapped. 121 Device.DataMapMtx.lock(); 122 __tgt_target_table *HostTable = &TransTable->HostTable; 123 for (__tgt_offload_entry *CurrDeviceEntry = TargetTable->EntriesBegin, 124 *CurrHostEntry = HostTable->EntriesBegin, 125 *EntryDeviceEnd = TargetTable->EntriesEnd; 126 CurrDeviceEntry != EntryDeviceEnd; 127 CurrDeviceEntry++, CurrHostEntry++) { 128 if (CurrDeviceEntry->size != 0) { 129 // has data. 130 assert(CurrDeviceEntry->size == CurrHostEntry->size && 131 "data size mismatch"); 132 133 // Fortran may use multiple weak declarations for the same symbol, 134 // therefore we must allow for multiple weak symbols to be loaded from 135 // the fat binary. Treat these mappings as any other "regular" mapping. 136 // Add entry to map. 137 if (Device.getTgtPtrBegin(CurrHostEntry->addr, CurrHostEntry->size)) 138 continue; 139 DP("Add mapping from host " DPxMOD " to device " DPxMOD " with size %zu" 140 "\n", DPxPTR(CurrHostEntry->addr), DPxPTR(CurrDeviceEntry->addr), 141 CurrDeviceEntry->size); 142 Device.HostDataToTargetMap.emplace( 143 (uintptr_t)CurrHostEntry->addr /*HstPtrBase*/, 144 (uintptr_t)CurrHostEntry->addr /*HstPtrBegin*/, 145 (uintptr_t)CurrHostEntry->addr + CurrHostEntry->size /*HstPtrEnd*/, 146 (uintptr_t)CurrDeviceEntry->addr /*TgtPtrBegin*/, 147 true /*IsRefCountINF*/); 148 } 149 } 150 Device.DataMapMtx.unlock(); 151 } 152 TrlTblMtx->unlock(); 153 154 if (rc != OFFLOAD_SUCCESS) { 155 Device.PendingGlobalsMtx.unlock(); 156 return rc; 157 } 158 159 /* 160 * Run ctors for static objects 161 */ 162 if (!Device.PendingCtorsDtors.empty()) { 163 // Call all ctors for all libraries registered so far 164 for (auto &lib : Device.PendingCtorsDtors) { 165 if (!lib.second.PendingCtors.empty()) { 166 DP("Has pending ctors... call now\n"); 167 for (auto &entry : lib.second.PendingCtors) { 168 void *ctor = entry; 169 int rc = target(device_id, ctor, 0, NULL, NULL, NULL, NULL, NULL, 1, 170 1, true /*team*/); 171 if (rc != OFFLOAD_SUCCESS) { 172 DP("Running ctor " DPxMOD " failed.\n", DPxPTR(ctor)); 173 Device.PendingGlobalsMtx.unlock(); 174 return OFFLOAD_FAIL; 175 } 176 } 177 // Clear the list to indicate that this device has been used 178 lib.second.PendingCtors.clear(); 179 DP("Done with pending ctors for lib " DPxMOD "\n", DPxPTR(lib.first)); 180 } 181 } 182 } 183 Device.HasPendingGlobals = false; 184 Device.PendingGlobalsMtx.unlock(); 185 186 return OFFLOAD_SUCCESS; 187 } 188 189 // Check whether a device has been initialized, global ctors have been 190 // executed and global data has been mapped; do so if not already done. 191 int CheckDeviceAndCtors(int64_t device_id) { 192 // Is device ready? 193 if (!device_is_ready(device_id)) { 194 DP("Device %" PRId64 " is not ready.\n", device_id); 195 return OFFLOAD_FAIL; 196 } 197 198 // Get device info. 199 DeviceTy &Device = Devices[device_id]; 200 201 // Check whether global data has been mapped for this device 202 Device.PendingGlobalsMtx.lock(); 203 bool hasPendingGlobals = Device.HasPendingGlobals; 204 Device.PendingGlobalsMtx.unlock(); 205 if (hasPendingGlobals && InitLibrary(Device) != OFFLOAD_SUCCESS) { 206 DP("Failed to init globals on device %" PRId64 "\n", device_id); 207 return OFFLOAD_FAIL; 208 } 209 210 return OFFLOAD_SUCCESS; 211 } 212 213 static int32_t getParentIndex(int64_t type) { 214 return ((type & OMP_TGT_MAPTYPE_MEMBER_OF) >> 48) - 1; 215 } 216 217 /// Call the user-defined mapper function followed by the appropriate 218 // target_data_* function (target_data_{begin,end,update}). 219 int targetDataMapper(DeviceTy &Device, void *arg_base, void *arg, 220 int64_t arg_size, int64_t arg_type, void *arg_mapper, 221 TargetDataFuncPtrTy target_data_function) { 222 DP("Calling the mapper function " DPxMOD "\n", DPxPTR(arg_mapper)); 223 224 // The mapper function fills up Components. 225 MapperComponentsTy MapperComponents; 226 MapperFuncPtrTy MapperFuncPtr = (MapperFuncPtrTy)(arg_mapper); 227 (*MapperFuncPtr)((void *)&MapperComponents, arg_base, arg, arg_size, 228 arg_type); 229 230 // Construct new arrays for args_base, args, arg_sizes and arg_types 231 // using the information in MapperComponents and call the corresponding 232 // target_data_* function using these new arrays. 233 std::vector<void *> mapper_args_base; 234 std::vector<void *> mapper_args; 235 std::vector<int64_t> mapper_arg_sizes; 236 std::vector<int64_t> mapper_arg_types; 237 238 for (auto& C : MapperComponents.Components) { 239 mapper_args_base.push_back(C.Base); 240 mapper_args.push_back(C.Begin); 241 mapper_arg_sizes.push_back(C.Size); 242 mapper_arg_types.push_back(C.Type); 243 } 244 245 int rc = target_data_function(Device, MapperComponents.Components.size(), 246 mapper_args_base.data(), mapper_args.data(), mapper_arg_sizes.data(), 247 mapper_arg_types.data(), /*arg_mappers*/ nullptr, 248 /*__tgt_async_info*/ nullptr); 249 250 return rc; 251 } 252 253 /// Internal function to do the mapping and transfer the data to the device 254 int targetDataBegin(DeviceTy &Device, int32_t arg_num, void **args_base, 255 void **args, int64_t *arg_sizes, int64_t *arg_types, 256 void **arg_mappers, __tgt_async_info *async_info_ptr) { 257 // process each input. 258 for (int32_t i = 0; i < arg_num; ++i) { 259 // Ignore private variables and arrays - there is no mapping for them. 260 if ((arg_types[i] & OMP_TGT_MAPTYPE_LITERAL) || 261 (arg_types[i] & OMP_TGT_MAPTYPE_PRIVATE)) 262 continue; 263 264 if (arg_mappers && arg_mappers[i]) { 265 // Instead of executing the regular path of targetDataBegin, call the 266 // targetDataMapper variant which will call targetDataBegin again 267 // with new arguments. 268 DP("Calling targetDataMapper for the %dth argument\n", i); 269 270 int rc = targetDataMapper(Device, args_base[i], args[i], arg_sizes[i], 271 arg_types[i], arg_mappers[i], targetDataBegin); 272 273 if (rc != OFFLOAD_SUCCESS) { 274 DP("Call to targetDataBegin via targetDataMapper for custom mapper" 275 " failed.\n"); 276 return OFFLOAD_FAIL; 277 } 278 279 // Skip the rest of this function, continue to the next argument. 280 continue; 281 } 282 283 void *HstPtrBegin = args[i]; 284 void *HstPtrBase = args_base[i]; 285 int64_t data_size = arg_sizes[i]; 286 287 // Adjust for proper alignment if this is a combined entry (for structs). 288 // Look at the next argument - if that is MEMBER_OF this one, then this one 289 // is a combined entry. 290 int64_t padding = 0; 291 const int next_i = i+1; 292 if (getParentIndex(arg_types[i]) < 0 && next_i < arg_num && 293 getParentIndex(arg_types[next_i]) == i) { 294 padding = (int64_t)HstPtrBegin % Alignment; 295 if (padding) { 296 DP("Using a padding of %" PRId64 " bytes for begin address " DPxMOD 297 "\n", padding, DPxPTR(HstPtrBegin)); 298 HstPtrBegin = (char *) HstPtrBegin - padding; 299 data_size += padding; 300 } 301 } 302 303 // Address of pointer on the host and device, respectively. 304 void *Pointer_HstPtrBegin, *PointerTgtPtrBegin; 305 bool IsNew, Pointer_IsNew; 306 bool IsHostPtr = false; 307 bool IsImplicit = arg_types[i] & OMP_TGT_MAPTYPE_IMPLICIT; 308 // Force the creation of a device side copy of the data when: 309 // a close map modifier was associated with a map that contained a to. 310 bool HasCloseModifier = arg_types[i] & OMP_TGT_MAPTYPE_CLOSE; 311 bool HasPresentModifier = arg_types[i] & OMP_TGT_MAPTYPE_PRESENT; 312 // UpdateRef is based on MEMBER_OF instead of TARGET_PARAM because if we 313 // have reached this point via __tgt_target_data_begin and not __tgt_target 314 // then no argument is marked as TARGET_PARAM ("omp target data map" is not 315 // associated with a target region, so there are no target parameters). This 316 // may be considered a hack, we could revise the scheme in the future. 317 bool UpdateRef = !(arg_types[i] & OMP_TGT_MAPTYPE_MEMBER_OF); 318 if (arg_types[i] & OMP_TGT_MAPTYPE_PTR_AND_OBJ) { 319 DP("Has a pointer entry: \n"); 320 // Base is address of pointer. 321 // 322 // Usually, the pointer is already allocated by this time. For example: 323 // 324 // #pragma omp target map(s.p[0:N]) 325 // 326 // The map entry for s comes first, and the PTR_AND_OBJ entry comes 327 // afterward, so the pointer is already allocated by the time the 328 // PTR_AND_OBJ entry is handled below, and PointerTgtPtrBegin is thus 329 // non-null. However, "declare target link" can produce a PTR_AND_OBJ 330 // entry for a global that might not already be allocated by the time the 331 // PTR_AND_OBJ entry is handled below, and so the allocation might fail 332 // when HasPresentModifier. 333 PointerTgtPtrBegin = Device.getOrAllocTgtPtr( 334 HstPtrBase, HstPtrBase, sizeof(void *), Pointer_IsNew, IsHostPtr, 335 IsImplicit, UpdateRef, HasCloseModifier, HasPresentModifier); 336 if (!PointerTgtPtrBegin) { 337 DP("Call to getOrAllocTgtPtr returned null pointer (%s).\n", 338 HasPresentModifier ? "'present' map type modifier" 339 : "device failure or illegal mapping"); 340 return OFFLOAD_FAIL; 341 } 342 DP("There are %zu bytes allocated at target address " DPxMOD " - is%s new" 343 "\n", sizeof(void *), DPxPTR(PointerTgtPtrBegin), 344 (Pointer_IsNew ? "" : " not")); 345 Pointer_HstPtrBegin = HstPtrBase; 346 // modify current entry. 347 HstPtrBase = *(void **)HstPtrBase; 348 UpdateRef = true; // subsequently update ref count of pointee 349 } 350 351 void *TgtPtrBegin = Device.getOrAllocTgtPtr( 352 HstPtrBegin, HstPtrBase, data_size, IsNew, IsHostPtr, IsImplicit, 353 UpdateRef, HasCloseModifier, HasPresentModifier); 354 // If data_size==0, then the argument could be a zero-length pointer to 355 // NULL, so getOrAlloc() returning NULL is not an error. 356 if (!TgtPtrBegin && (data_size || HasPresentModifier)) { 357 DP("Call to getOrAllocTgtPtr returned null pointer (%s).\n", 358 HasPresentModifier ? "'present' map type modifier" 359 : "device failure or illegal mapping"); 360 return OFFLOAD_FAIL; 361 } 362 DP("There are %" PRId64 " bytes allocated at target address " DPxMOD 363 " - is%s new\n", data_size, DPxPTR(TgtPtrBegin), 364 (IsNew ? "" : " not")); 365 366 if (arg_types[i] & OMP_TGT_MAPTYPE_RETURN_PARAM) { 367 uintptr_t Delta = (uintptr_t)HstPtrBegin - (uintptr_t)HstPtrBase; 368 void *TgtPtrBase = (void *)((uintptr_t)TgtPtrBegin - Delta); 369 DP("Returning device pointer " DPxMOD "\n", DPxPTR(TgtPtrBase)); 370 args_base[i] = TgtPtrBase; 371 } 372 373 if (arg_types[i] & OMP_TGT_MAPTYPE_TO) { 374 bool copy = false; 375 if (!(RTLs->RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) || 376 HasCloseModifier) { 377 if (IsNew || (arg_types[i] & OMP_TGT_MAPTYPE_ALWAYS)) { 378 copy = true; 379 } else if (arg_types[i] & OMP_TGT_MAPTYPE_MEMBER_OF) { 380 // Copy data only if the "parent" struct has RefCount==1. 381 int32_t parent_idx = getParentIndex(arg_types[i]); 382 uint64_t parent_rc = Device.getMapEntryRefCnt(args[parent_idx]); 383 assert(parent_rc > 0 && "parent struct not found"); 384 if (parent_rc == 1) { 385 copy = true; 386 } 387 } 388 } 389 390 if (copy && !IsHostPtr) { 391 DP("Moving %" PRId64 " bytes (hst:" DPxMOD ") -> (tgt:" DPxMOD ")\n", 392 data_size, DPxPTR(HstPtrBegin), DPxPTR(TgtPtrBegin)); 393 int rt = Device.submitData(TgtPtrBegin, HstPtrBegin, data_size, 394 async_info_ptr); 395 if (rt != OFFLOAD_SUCCESS) { 396 DP("Copying data to device failed.\n"); 397 return OFFLOAD_FAIL; 398 } 399 } 400 } 401 402 if (arg_types[i] & OMP_TGT_MAPTYPE_PTR_AND_OBJ && !IsHostPtr) { 403 DP("Update pointer (" DPxMOD ") -> [" DPxMOD "]\n", 404 DPxPTR(PointerTgtPtrBegin), DPxPTR(TgtPtrBegin)); 405 uint64_t Delta = (uint64_t)HstPtrBegin - (uint64_t)HstPtrBase; 406 void *TgtPtrBase = (void *)((uint64_t)TgtPtrBegin - Delta); 407 int rt = Device.submitData(PointerTgtPtrBegin, &TgtPtrBase, 408 sizeof(void *), async_info_ptr); 409 if (rt != OFFLOAD_SUCCESS) { 410 DP("Copying data to device failed.\n"); 411 return OFFLOAD_FAIL; 412 } 413 // create shadow pointers for this entry 414 Device.ShadowMtx.lock(); 415 Device.ShadowPtrMap[Pointer_HstPtrBegin] = { 416 HstPtrBase, PointerTgtPtrBegin, TgtPtrBase}; 417 Device.ShadowMtx.unlock(); 418 } 419 } 420 421 return OFFLOAD_SUCCESS; 422 } 423 424 namespace { 425 /// This structure contains information to deallocate a target pointer, aka. 426 /// used to call the function \p DeviceTy::deallocTgtPtr. 427 struct DeallocTgtPtrInfo { 428 /// Host pointer used to look up into the map table 429 void *HstPtrBegin; 430 /// Size of the data 431 int64_t DataSize; 432 /// Whether it is forced to be removed from the map table 433 bool ForceDelete; 434 /// Whether it has \p close modifier 435 bool HasCloseModifier; 436 437 DeallocTgtPtrInfo(void *HstPtr, int64_t Size, bool ForceDelete, 438 bool HasCloseModifier) 439 : HstPtrBegin(HstPtr), DataSize(Size), ForceDelete(ForceDelete), 440 HasCloseModifier(HasCloseModifier) {} 441 }; 442 } // namespace 443 444 /// Internal function to undo the mapping and retrieve the data from the device. 445 int targetDataEnd(DeviceTy &Device, int32_t ArgNum, void **ArgBases, 446 void **Args, int64_t *ArgSizes, int64_t *ArgTypes, 447 void **ArgMappers, __tgt_async_info *AsyncInfo) { 448 int Ret; 449 std::vector<DeallocTgtPtrInfo> DeallocTgtPtrs; 450 // process each input. 451 for (int32_t I = ArgNum - 1; I >= 0; --I) { 452 // Ignore private variables and arrays - there is no mapping for them. 453 // Also, ignore the use_device_ptr directive, it has no effect here. 454 if ((ArgTypes[I] & OMP_TGT_MAPTYPE_LITERAL) || 455 (ArgTypes[I] & OMP_TGT_MAPTYPE_PRIVATE)) 456 continue; 457 458 if (ArgMappers && ArgMappers[I]) { 459 // Instead of executing the regular path of targetDataEnd, call the 460 // targetDataMapper variant which will call targetDataEnd again 461 // with new arguments. 462 DP("Calling targetDataMapper for the %dth argument\n", I); 463 464 Ret = targetDataMapper(Device, ArgBases[I], Args[I], ArgSizes[I], 465 ArgTypes[I], ArgMappers[I], targetDataEnd); 466 467 if (Ret != OFFLOAD_SUCCESS) { 468 DP("Call to targetDataEnd via targetDataMapper for custom mapper" 469 " failed.\n"); 470 return OFFLOAD_FAIL; 471 } 472 473 // Skip the rest of this function, continue to the next argument. 474 continue; 475 } 476 477 void *HstPtrBegin = Args[I]; 478 int64_t DataSize = ArgSizes[I]; 479 // Adjust for proper alignment if this is a combined entry (for structs). 480 // Look at the next argument - if that is MEMBER_OF this one, then this one 481 // is a combined entry. 482 const int NextI = I + 1; 483 if (getParentIndex(ArgTypes[I]) < 0 && NextI < ArgNum && 484 getParentIndex(ArgTypes[NextI]) == I) { 485 int64_t Padding = (int64_t)HstPtrBegin % Alignment; 486 if (Padding) { 487 DP("Using a Padding of %" PRId64 " bytes for begin address " DPxMOD 488 "\n", 489 Padding, DPxPTR(HstPtrBegin)); 490 HstPtrBegin = (char *)HstPtrBegin - Padding; 491 DataSize += Padding; 492 } 493 } 494 495 bool IsLast, IsHostPtr; 496 bool UpdateRef = !(ArgTypes[I] & OMP_TGT_MAPTYPE_MEMBER_OF) || 497 (ArgTypes[I] & OMP_TGT_MAPTYPE_PTR_AND_OBJ); 498 bool ForceDelete = ArgTypes[I] & OMP_TGT_MAPTYPE_DELETE; 499 bool HasCloseModifier = ArgTypes[I] & OMP_TGT_MAPTYPE_CLOSE; 500 bool HasPresentModifier = ArgTypes[I] & OMP_TGT_MAPTYPE_PRESENT; 501 502 // If PTR_AND_OBJ, HstPtrBegin is address of pointee 503 void *TgtPtrBegin = Device.getTgtPtrBegin(HstPtrBegin, DataSize, IsLast, 504 UpdateRef, IsHostPtr); 505 if (!TgtPtrBegin && (DataSize || HasPresentModifier)) { 506 DP("Mapping does not exist (%s)\n", 507 (HasPresentModifier ? "'present' map type modifier" : "ignored")); 508 if (HasPresentModifier) { 509 // FIXME: This should not be an error on exit from "omp target data", 510 // but it should be an error upon entering an "omp target exit data". 511 MESSAGE("device mapping required by 'present' map type modifier does " 512 "not exist for host address " DPxMOD " (%ld bytes)", 513 DPxPTR(HstPtrBegin), DataSize); 514 return OFFLOAD_FAIL; 515 } 516 } else { 517 DP("There are %" PRId64 " bytes allocated at target address " DPxMOD 518 " - is%s last\n", 519 DataSize, DPxPTR(TgtPtrBegin), (IsLast ? "" : " not")); 520 } 521 522 bool DelEntry = IsLast || ForceDelete; 523 524 if ((ArgTypes[I] & OMP_TGT_MAPTYPE_MEMBER_OF) && 525 !(ArgTypes[I] & OMP_TGT_MAPTYPE_PTR_AND_OBJ)) { 526 DelEntry = false; // protect parent struct from being deallocated 527 } 528 529 if ((ArgTypes[I] & OMP_TGT_MAPTYPE_FROM) || DelEntry) { 530 // Move data back to the host 531 if (ArgTypes[I] & OMP_TGT_MAPTYPE_FROM) { 532 bool Always = ArgTypes[I] & OMP_TGT_MAPTYPE_ALWAYS; 533 bool CopyMember = false; 534 if (!(RTLs->RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) || 535 HasCloseModifier) { 536 if ((ArgTypes[I] & OMP_TGT_MAPTYPE_MEMBER_OF) && 537 !(ArgTypes[I] & OMP_TGT_MAPTYPE_PTR_AND_OBJ)) { 538 // Copy data only if the "parent" struct has RefCount==1. 539 int32_t ParentIdx = getParentIndex(ArgTypes[I]); 540 uint64_t ParentRC = Device.getMapEntryRefCnt(Args[ParentIdx]); 541 assert(ParentRC > 0 && "parent struct not found"); 542 if (ParentRC == 1) 543 CopyMember = true; 544 } 545 } 546 547 if ((DelEntry || Always || CopyMember) && 548 !(RTLs->RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY && 549 TgtPtrBegin == HstPtrBegin)) { 550 DP("Moving %" PRId64 " bytes (tgt:" DPxMOD ") -> (hst:" DPxMOD ")\n", 551 DataSize, DPxPTR(TgtPtrBegin), DPxPTR(HstPtrBegin)); 552 Ret = Device.retrieveData(HstPtrBegin, TgtPtrBegin, DataSize, 553 AsyncInfo); 554 if (Ret != OFFLOAD_SUCCESS) { 555 DP("Copying data from device failed.\n"); 556 return OFFLOAD_FAIL; 557 } 558 } 559 } 560 561 // If we copied back to the host a struct/array containing pointers, we 562 // need to restore the original host pointer values from their shadow 563 // copies. If the struct is going to be deallocated, remove any remaining 564 // shadow pointer entries for this struct. 565 uintptr_t LB = (uintptr_t)HstPtrBegin; 566 uintptr_t UB = (uintptr_t)HstPtrBegin + DataSize; 567 Device.ShadowMtx.lock(); 568 for (ShadowPtrListTy::iterator Itr = Device.ShadowPtrMap.begin(); 569 Itr != Device.ShadowPtrMap.end();) { 570 void **ShadowHstPtrAddr = (void **)Itr->first; 571 572 // An STL map is sorted on its keys; use this property 573 // to quickly determine when to break out of the loop. 574 if ((uintptr_t)ShadowHstPtrAddr < LB) { 575 ++Itr; 576 continue; 577 } 578 if ((uintptr_t)ShadowHstPtrAddr >= UB) 579 break; 580 581 // If we copied the struct to the host, we need to restore the pointer. 582 if (ArgTypes[I] & OMP_TGT_MAPTYPE_FROM) { 583 DP("Restoring original host pointer value " DPxMOD " for host " 584 "pointer " DPxMOD "\n", 585 DPxPTR(Itr->second.HstPtrVal), DPxPTR(ShadowHstPtrAddr)); 586 *ShadowHstPtrAddr = Itr->second.HstPtrVal; 587 } 588 // If the struct is to be deallocated, remove the shadow entry. 589 if (DelEntry) { 590 DP("Removing shadow pointer " DPxMOD "\n", DPxPTR(ShadowHstPtrAddr)); 591 Itr = Device.ShadowPtrMap.erase(Itr); 592 } else { 593 ++Itr; 594 } 595 } 596 Device.ShadowMtx.unlock(); 597 598 // Add pointer to the buffer for later deallocation 599 if (DelEntry) 600 DeallocTgtPtrs.emplace_back(HstPtrBegin, DataSize, ForceDelete, 601 HasCloseModifier); 602 } 603 } 604 605 // We need to synchronize before deallocating data. 606 // If AsyncInfo is nullptr, the previous data transfer (if has) will be 607 // synchronous, so we don't need to synchronize again. If AsyncInfo->Queue is 608 // nullptr, there is no data transfer happened because once there is, 609 // AsyncInfo->Queue will not be nullptr, so again, we don't need to 610 // synchronize. 611 if (AsyncInfo && AsyncInfo->Queue) { 612 Ret = Device.synchronize(AsyncInfo); 613 if (Ret != OFFLOAD_SUCCESS) { 614 DP("Failed to synchronize device.\n"); 615 return OFFLOAD_FAIL; 616 } 617 } 618 619 // Deallocate target pointer 620 for (DeallocTgtPtrInfo &Info : DeallocTgtPtrs) { 621 Ret = Device.deallocTgtPtr(Info.HstPtrBegin, Info.DataSize, 622 Info.ForceDelete, Info.HasCloseModifier); 623 if (Ret != OFFLOAD_SUCCESS) { 624 DP("Deallocating data from device failed.\n"); 625 return OFFLOAD_FAIL; 626 } 627 } 628 629 return OFFLOAD_SUCCESS; 630 } 631 632 /// Internal function to pass data to/from the target. 633 // async_info_ptr is currently unused, added here so target_data_update has the 634 // same signature as targetDataBegin and targetDataEnd. 635 int target_data_update(DeviceTy &Device, int32_t arg_num, 636 void **args_base, void **args, int64_t *arg_sizes, int64_t *arg_types, 637 void **arg_mappers, __tgt_async_info *async_info_ptr) { 638 // process each input. 639 for (int32_t i = 0; i < arg_num; ++i) { 640 if ((arg_types[i] & OMP_TGT_MAPTYPE_LITERAL) || 641 (arg_types[i] & OMP_TGT_MAPTYPE_PRIVATE)) 642 continue; 643 644 if (arg_mappers && arg_mappers[i]) { 645 // Instead of executing the regular path of target_data_update, call the 646 // targetDataMapper variant which will call target_data_update again 647 // with new arguments. 648 DP("Calling targetDataMapper for the %dth argument\n", i); 649 650 int rc = 651 targetDataMapper(Device, args_base[i], args[i], arg_sizes[i], 652 arg_types[i], arg_mappers[i], target_data_update); 653 654 if (rc != OFFLOAD_SUCCESS) { 655 DP("Call to target_data_update via targetDataMapper for custom mapper" 656 " failed.\n"); 657 return OFFLOAD_FAIL; 658 } 659 660 // Skip the rest of this function, continue to the next argument. 661 continue; 662 } 663 664 void *HstPtrBegin = args[i]; 665 int64_t MapSize = arg_sizes[i]; 666 bool IsLast, IsHostPtr; 667 void *TgtPtrBegin = Device.getTgtPtrBegin(HstPtrBegin, MapSize, IsLast, 668 false, IsHostPtr); 669 if (!TgtPtrBegin) { 670 DP("hst data:" DPxMOD " not found, becomes a noop\n", DPxPTR(HstPtrBegin)); 671 if (arg_types[i] & OMP_TGT_MAPTYPE_PRESENT) { 672 MESSAGE("device mapping required by 'present' motion modifier does not " 673 "exist for host address " DPxMOD " (%ld bytes)", 674 DPxPTR(HstPtrBegin), MapSize); 675 return OFFLOAD_FAIL; 676 } 677 continue; 678 } 679 680 if (RTLs->RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY && 681 TgtPtrBegin == HstPtrBegin) { 682 DP("hst data:" DPxMOD " unified and shared, becomes a noop\n", 683 DPxPTR(HstPtrBegin)); 684 continue; 685 } 686 687 if (arg_types[i] & OMP_TGT_MAPTYPE_FROM) { 688 DP("Moving %" PRId64 " bytes (tgt:" DPxMOD ") -> (hst:" DPxMOD ")\n", 689 arg_sizes[i], DPxPTR(TgtPtrBegin), DPxPTR(HstPtrBegin)); 690 int rt = Device.retrieveData(HstPtrBegin, TgtPtrBegin, MapSize, nullptr); 691 if (rt != OFFLOAD_SUCCESS) { 692 DP("Copying data from device failed.\n"); 693 return OFFLOAD_FAIL; 694 } 695 696 uintptr_t lb = (uintptr_t) HstPtrBegin; 697 uintptr_t ub = (uintptr_t) HstPtrBegin + MapSize; 698 Device.ShadowMtx.lock(); 699 for (ShadowPtrListTy::iterator it = Device.ShadowPtrMap.begin(); 700 it != Device.ShadowPtrMap.end(); ++it) { 701 void **ShadowHstPtrAddr = (void**) it->first; 702 if ((uintptr_t) ShadowHstPtrAddr < lb) 703 continue; 704 if ((uintptr_t) ShadowHstPtrAddr >= ub) 705 break; 706 DP("Restoring original host pointer value " DPxMOD " for host pointer " 707 DPxMOD "\n", DPxPTR(it->second.HstPtrVal), 708 DPxPTR(ShadowHstPtrAddr)); 709 *ShadowHstPtrAddr = it->second.HstPtrVal; 710 } 711 Device.ShadowMtx.unlock(); 712 } 713 714 if (arg_types[i] & OMP_TGT_MAPTYPE_TO) { 715 DP("Moving %" PRId64 " bytes (hst:" DPxMOD ") -> (tgt:" DPxMOD ")\n", 716 arg_sizes[i], DPxPTR(HstPtrBegin), DPxPTR(TgtPtrBegin)); 717 int rt = Device.submitData(TgtPtrBegin, HstPtrBegin, MapSize, nullptr); 718 if (rt != OFFLOAD_SUCCESS) { 719 DP("Copying data to device failed.\n"); 720 return OFFLOAD_FAIL; 721 } 722 723 uintptr_t lb = (uintptr_t) HstPtrBegin; 724 uintptr_t ub = (uintptr_t) HstPtrBegin + MapSize; 725 Device.ShadowMtx.lock(); 726 for (ShadowPtrListTy::iterator it = Device.ShadowPtrMap.begin(); 727 it != Device.ShadowPtrMap.end(); ++it) { 728 void **ShadowHstPtrAddr = (void **)it->first; 729 if ((uintptr_t)ShadowHstPtrAddr < lb) 730 continue; 731 if ((uintptr_t)ShadowHstPtrAddr >= ub) 732 break; 733 DP("Restoring original target pointer value " DPxMOD " for target " 734 "pointer " DPxMOD "\n", 735 DPxPTR(it->second.TgtPtrVal), DPxPTR(it->second.TgtPtrAddr)); 736 rt = Device.submitData(it->second.TgtPtrAddr, &it->second.TgtPtrVal, 737 sizeof(void *), nullptr); 738 if (rt != OFFLOAD_SUCCESS) { 739 DP("Copying data to device failed.\n"); 740 Device.ShadowMtx.unlock(); 741 return OFFLOAD_FAIL; 742 } 743 } 744 Device.ShadowMtx.unlock(); 745 } 746 } 747 return OFFLOAD_SUCCESS; 748 } 749 750 static const unsigned LambdaMapping = OMP_TGT_MAPTYPE_PTR_AND_OBJ | 751 OMP_TGT_MAPTYPE_LITERAL | 752 OMP_TGT_MAPTYPE_IMPLICIT; 753 static bool isLambdaMapping(int64_t Mapping) { 754 return (Mapping & LambdaMapping) == LambdaMapping; 755 } 756 757 namespace { 758 /// Find the table information in the map or look it up in the translation 759 /// tables. 760 TableMap *getTableMap(void *HostPtr) { 761 std::lock_guard<std::mutex> TblMapLock(*TblMapMtx); 762 HostPtrToTableMapTy::iterator TableMapIt = HostPtrToTableMap->find(HostPtr); 763 764 if (TableMapIt != HostPtrToTableMap->end()) 765 return &TableMapIt->second; 766 767 // We don't have a map. So search all the registered libraries. 768 TableMap *TM = nullptr; 769 std::lock_guard<std::mutex> TrlTblLock(*TrlTblMtx); 770 for (HostEntriesBeginToTransTableTy::iterator Itr = 771 HostEntriesBeginToTransTable->begin(); 772 Itr != HostEntriesBeginToTransTable->end(); ++Itr) { 773 // get the translation table (which contains all the good info). 774 TranslationTable *TransTable = &Itr->second; 775 // iterate over all the host table entries to see if we can locate the 776 // host_ptr. 777 __tgt_offload_entry *Cur = TransTable->HostTable.EntriesBegin; 778 for (uint32_t I = 0; Cur < TransTable->HostTable.EntriesEnd; ++Cur, ++I) { 779 if (Cur->addr != HostPtr) 780 continue; 781 // we got a match, now fill the HostPtrToTableMap so that we 782 // may avoid this search next time. 783 TM = &(*HostPtrToTableMap)[HostPtr]; 784 TM->Table = TransTable; 785 TM->Index = I; 786 return TM; 787 } 788 } 789 790 return nullptr; 791 } 792 793 /// Get loop trip count 794 /// FIXME: This function will not work right if calling 795 /// __kmpc_push_target_tripcount in one thread but doing offloading in another 796 /// thread, which might occur when we call task yield. 797 uint64_t getLoopTripCount(int64_t DeviceId) { 798 DeviceTy &Device = Devices[DeviceId]; 799 uint64_t LoopTripCount = 0; 800 801 { 802 std::lock_guard<std::mutex> TblMapLock(*TblMapMtx); 803 auto I = Device.LoopTripCnt.find(__kmpc_global_thread_num(NULL)); 804 if (I != Device.LoopTripCnt.end()) { 805 LoopTripCount = I->second; 806 Device.LoopTripCnt.erase(I); 807 DP("loop trip count is %lu.\n", LoopTripCount); 808 } 809 } 810 811 return LoopTripCount; 812 } 813 814 /// Process data before launching the kernel, including calling targetDataBegin 815 /// to map and transfer data to target device, transferring (first-)private 816 /// variables. 817 int processDataBefore(int64_t DeviceId, void *HostPtr, int32_t ArgNum, 818 void **ArgBases, void **Args, int64_t *ArgSizes, 819 int64_t *ArgTypes, void **ArgMappers, 820 std::vector<void *> &TgtArgs, 821 std::vector<ptrdiff_t> &TgtOffsets, 822 std::vector<void *> &FPArrays, 823 __tgt_async_info *AsyncInfo) { 824 DeviceTy &Device = Devices[DeviceId]; 825 int Ret = targetDataBegin(Device, ArgNum, ArgBases, Args, ArgSizes, ArgTypes, 826 ArgMappers, AsyncInfo); 827 if (Ret != OFFLOAD_SUCCESS) { 828 DP("Call to targetDataBegin failed, abort target.\n"); 829 return OFFLOAD_FAIL; 830 } 831 832 // List of (first-)private arrays allocated for this target region 833 std::vector<int> TgtArgsPositions(ArgNum, -1); 834 835 for (int32_t I = 0; I < ArgNum; ++I) { 836 if (!(ArgTypes[I] & OMP_TGT_MAPTYPE_TARGET_PARAM)) { 837 // This is not a target parameter, do not push it into TgtArgs. 838 // Check for lambda mapping. 839 if (isLambdaMapping(ArgTypes[I])) { 840 assert((ArgTypes[I] & OMP_TGT_MAPTYPE_MEMBER_OF) && 841 "PTR_AND_OBJ must be also MEMBER_OF."); 842 unsigned Idx = getParentIndex(ArgTypes[I]); 843 int TgtIdx = TgtArgsPositions[Idx]; 844 assert(TgtIdx != -1 && "Base address must be translated already."); 845 // The parent lambda must be processed already and it must be the last 846 // in TgtArgs and TgtOffsets arrays. 847 void *HstPtrVal = Args[I]; 848 void *HstPtrBegin = ArgBases[I]; 849 void *HstPtrBase = Args[Idx]; 850 bool IsLast, IsHostPtr; // unused. 851 void *TgtPtrBase = 852 (void *)((intptr_t)TgtArgs[TgtIdx] + TgtOffsets[TgtIdx]); 853 DP("Parent lambda base " DPxMOD "\n", DPxPTR(TgtPtrBase)); 854 uint64_t Delta = (uint64_t)HstPtrBegin - (uint64_t)HstPtrBase; 855 void *TgtPtrBegin = (void *)((uintptr_t)TgtPtrBase + Delta); 856 void *PointerTgtPtrBegin = Device.getTgtPtrBegin( 857 HstPtrVal, ArgSizes[I], IsLast, false, IsHostPtr); 858 if (!PointerTgtPtrBegin) { 859 DP("No lambda captured variable mapped (" DPxMOD ") - ignored\n", 860 DPxPTR(HstPtrVal)); 861 continue; 862 } 863 if (RTLs->RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY && 864 TgtPtrBegin == HstPtrBegin) { 865 DP("Unified memory is active, no need to map lambda captured" 866 "variable (" DPxMOD ")\n", 867 DPxPTR(HstPtrVal)); 868 continue; 869 } 870 DP("Update lambda reference (" DPxMOD ") -> [" DPxMOD "]\n", 871 DPxPTR(PointerTgtPtrBegin), DPxPTR(TgtPtrBegin)); 872 Ret = Device.submitData(TgtPtrBegin, &PointerTgtPtrBegin, 873 sizeof(void *), AsyncInfo); 874 if (Ret != OFFLOAD_SUCCESS) { 875 DP("Copying data to device failed.\n"); 876 return OFFLOAD_FAIL; 877 } 878 } 879 continue; 880 } 881 void *HstPtrBegin = Args[I]; 882 void *HstPtrBase = ArgBases[I]; 883 void *TgtPtrBegin; 884 ptrdiff_t TgtBaseOffset; 885 bool IsLast, IsHostPtr; // unused. 886 if (ArgTypes[I] & OMP_TGT_MAPTYPE_LITERAL) { 887 DP("Forwarding first-private value " DPxMOD " to the target construct\n", 888 DPxPTR(HstPtrBase)); 889 TgtPtrBegin = HstPtrBase; 890 TgtBaseOffset = 0; 891 } else if (ArgTypes[I] & OMP_TGT_MAPTYPE_PRIVATE) { 892 // Allocate memory for (first-)private array 893 TgtPtrBegin = Device.allocData(ArgSizes[I], HstPtrBegin); 894 if (!TgtPtrBegin) { 895 DP("Data allocation for %sprivate array " DPxMOD " failed, " 896 "abort target.\n", 897 (ArgTypes[I] & OMP_TGT_MAPTYPE_TO ? "first-" : ""), 898 DPxPTR(HstPtrBegin)); 899 return OFFLOAD_FAIL; 900 } 901 FPArrays.push_back(TgtPtrBegin); 902 TgtBaseOffset = (intptr_t)HstPtrBase - (intptr_t)HstPtrBegin; 903 #ifdef OMPTARGET_DEBUG 904 void *TgtPtrBase = (void *)((intptr_t)TgtPtrBegin + TgtBaseOffset); 905 DP("Allocated %" PRId64 " bytes of target memory at " DPxMOD " for " 906 "%sprivate array " DPxMOD " - pushing target argument " DPxMOD "\n", 907 ArgSizes[I], DPxPTR(TgtPtrBegin), 908 (ArgTypes[I] & OMP_TGT_MAPTYPE_TO ? "first-" : ""), 909 DPxPTR(HstPtrBegin), DPxPTR(TgtPtrBase)); 910 #endif 911 // If first-private, copy data from host 912 if (ArgTypes[I] & OMP_TGT_MAPTYPE_TO) { 913 Ret = 914 Device.submitData(TgtPtrBegin, HstPtrBegin, ArgSizes[I], AsyncInfo); 915 if (Ret != OFFLOAD_SUCCESS) { 916 DP("Copying data to device failed, failed.\n"); 917 return OFFLOAD_FAIL; 918 } 919 } 920 } else { 921 if (ArgTypes[I] & OMP_TGT_MAPTYPE_PTR_AND_OBJ) 922 HstPtrBase = *reinterpret_cast<void **>(HstPtrBase); 923 TgtPtrBegin = Device.getTgtPtrBegin(HstPtrBegin, ArgSizes[I], IsLast, 924 false, IsHostPtr); 925 TgtBaseOffset = (intptr_t)HstPtrBase - (intptr_t)HstPtrBegin; 926 #ifdef OMPTARGET_DEBUG 927 void *TgtPtrBase = (void *)((intptr_t)TgtPtrBegin + TgtBaseOffset); 928 DP("Obtained target argument " DPxMOD " from host pointer " DPxMOD "\n", 929 DPxPTR(TgtPtrBase), DPxPTR(HstPtrBegin)); 930 #endif 931 } 932 TgtArgsPositions[I] = TgtArgs.size(); 933 TgtArgs.push_back(TgtPtrBegin); 934 TgtOffsets.push_back(TgtBaseOffset); 935 } 936 937 assert(TgtArgs.size() == TgtOffsets.size() && 938 "Size mismatch in arguments and offsets"); 939 940 return OFFLOAD_SUCCESS; 941 } 942 943 /// Process data after launching the kernel, including transferring data back to 944 /// host if needed and deallocating target memory of (first-)private variables. 945 /// FIXME: This function has correctness issue that target memory might be 946 /// deallocated when they're being used. 947 int processDataAfter(int64_t DeviceId, void *HostPtr, int32_t ArgNum, 948 void **ArgBases, void **Args, int64_t *ArgSizes, 949 int64_t *ArgTypes, void **ArgMappers, 950 std::vector<void *> &FPArrays, 951 __tgt_async_info *AsyncInfo) { 952 DeviceTy &Device = Devices[DeviceId]; 953 954 // Move data from device. 955 int Ret = targetDataEnd(Device, ArgNum, ArgBases, Args, ArgSizes, ArgTypes, 956 ArgMappers, AsyncInfo); 957 if (Ret != OFFLOAD_SUCCESS) { 958 DP("Call to targetDataEnd failed, abort targe.\n"); 959 return OFFLOAD_FAIL; 960 } 961 962 // Deallocate (first-)private arrays 963 for (void *P : FPArrays) { 964 Ret = Device.deleteData(P); 965 if (Ret != OFFLOAD_SUCCESS) { 966 DP("Deallocation of (first-)private arrays failed.\n"); 967 return OFFLOAD_FAIL; 968 } 969 } 970 971 return OFFLOAD_SUCCESS; 972 } 973 } // namespace 974 975 /// performs the same actions as data_begin in case arg_num is 976 /// non-zero and initiates run of the offloaded region on the target platform; 977 /// if arg_num is non-zero after the region execution is done it also 978 /// performs the same action as data_update and data_end above. This function 979 /// returns 0 if it was able to transfer the execution to a target and an 980 /// integer different from zero otherwise. 981 int target(int64_t DeviceId, void *HostPtr, int32_t ArgNum, void **ArgBases, 982 void **Args, int64_t *ArgSizes, int64_t *ArgTypes, void **ArgMappers, 983 int32_t TeamNum, int32_t ThreadLimit, int IsTeamConstruct) { 984 DeviceTy &Device = Devices[DeviceId]; 985 986 TableMap *TM = getTableMap(HostPtr); 987 // No map for this host pointer found! 988 if (!TM) { 989 DP("Host ptr " DPxMOD " does not have a matching target pointer.\n", 990 DPxPTR(HostPtr)); 991 return OFFLOAD_FAIL; 992 } 993 994 // get target table. 995 __tgt_target_table *TargetTable = nullptr; 996 { 997 std::lock_guard<std::mutex> TrlTblLock(*TrlTblMtx); 998 assert(TM->Table->TargetsTable.size() > (size_t)DeviceId && 999 "Not expecting a device ID outside the table's bounds!"); 1000 TargetTable = TM->Table->TargetsTable[DeviceId]; 1001 } 1002 assert(TargetTable && "Global data has not been mapped\n"); 1003 1004 __tgt_async_info AsyncInfo; 1005 1006 std::vector<void *> TgtArgs; 1007 std::vector<ptrdiff_t> TgtOffsets; 1008 std::vector<void *> FPArrays; 1009 1010 // Process data, such as data mapping, before launching the kernel 1011 int Ret = processDataBefore(DeviceId, HostPtr, ArgNum, ArgBases, Args, 1012 ArgSizes, ArgTypes, ArgMappers, TgtArgs, 1013 TgtOffsets, FPArrays, &AsyncInfo); 1014 if (Ret != OFFLOAD_SUCCESS) { 1015 DP("Failed to process data before launching the kernel.\n"); 1016 return OFFLOAD_FAIL; 1017 } 1018 1019 // Get loop trip count 1020 uint64_t LoopTripCount = getLoopTripCount(DeviceId); 1021 1022 // Launch device execution. 1023 void *TgtEntryPtr = TargetTable->EntriesBegin[TM->Index].addr; 1024 DP("Launching target execution %s with pointer " DPxMOD " (index=%d).\n", 1025 TargetTable->EntriesBegin[TM->Index].name, DPxPTR(TgtEntryPtr), TM->Index); 1026 1027 if (IsTeamConstruct) 1028 Ret = Device.runTeamRegion(TgtEntryPtr, &TgtArgs[0], &TgtOffsets[0], 1029 TgtArgs.size(), TeamNum, ThreadLimit, 1030 LoopTripCount, &AsyncInfo); 1031 else 1032 Ret = Device.runRegion(TgtEntryPtr, &TgtArgs[0], &TgtOffsets[0], 1033 TgtArgs.size(), &AsyncInfo); 1034 1035 if (Ret != OFFLOAD_SUCCESS) { 1036 DP("Executing target region abort target.\n"); 1037 return OFFLOAD_FAIL; 1038 } 1039 1040 // Transfer data back and deallocate target memory for (first-)private 1041 // variables 1042 Ret = processDataAfter(DeviceId, HostPtr, ArgNum, ArgBases, Args, ArgSizes, 1043 ArgTypes, ArgMappers, FPArrays, &AsyncInfo); 1044 if (Ret != OFFLOAD_SUCCESS) { 1045 DP("Failed to process data after launching the kernel.\n"); 1046 return OFFLOAD_FAIL; 1047 } 1048 1049 return OFFLOAD_SUCCESS; 1050 } 1051