1 //===----------- rtl.cpp - Target independent OpenMP target RTL -----------===//
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 // Functionality for handling RTL plugins.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "rtl.h"
14 #include "device.h"
15 #include "private.h"
16 
17 #include "llvm/Object/OffloadBinary.h"
18 
19 #include <cassert>
20 #include <cstdlib>
21 #include <cstring>
22 #include <dlfcn.h>
23 #include <mutex>
24 #include <string>
25 
26 // List of all plugins that can support offloading.
27 static const char *RTLNames[] = {
28     /* PowerPC target       */ "libomptarget.rtl.ppc64.so",
29     /* x86_64 target        */ "libomptarget.rtl.x86_64.so",
30     /* CUDA target          */ "libomptarget.rtl.cuda.so",
31     /* AArch64 target       */ "libomptarget.rtl.aarch64.so",
32     /* SX-Aurora VE target  */ "libomptarget.rtl.ve.so",
33     /* AMDGPU target        */ "libomptarget.rtl.amdgpu.so",
34     /* Remote target        */ "libomptarget.rtl.rpc.so",
35 };
36 
37 PluginManager *PM;
38 
39 #if OMPTARGET_PROFILE_ENABLED
40 static char *ProfileTraceFile = nullptr;
41 #endif
42 
init()43 __attribute__((constructor(101))) void init() {
44   DP("Init target library!\n");
45 
46   bool UseEventsForAtomicTransfers = true;
47   if (const char *ForceAtomicMap = getenv("LIBOMPTARGET_MAP_FORCE_ATOMIC")) {
48     std::string ForceAtomicMapStr(ForceAtomicMap);
49     if (ForceAtomicMapStr == "false" || ForceAtomicMapStr == "FALSE")
50       UseEventsForAtomicTransfers = false;
51     else if (ForceAtomicMapStr != "true" && ForceAtomicMapStr != "TRUE")
52       fprintf(stderr,
53               "Warning: 'LIBOMPTARGET_MAP_FORCE_ATOMIC' accepts only "
54               "'true'/'TRUE' or 'false'/'FALSE' as options, '%s' ignored\n",
55               ForceAtomicMap);
56   }
57 
58   PM = new PluginManager(UseEventsForAtomicTransfers);
59 
60 #ifdef OMPTARGET_PROFILE_ENABLED
61   ProfileTraceFile = getenv("LIBOMPTARGET_PROFILE");
62   // TODO: add a configuration option for time granularity
63   if (ProfileTraceFile)
64     llvm::timeTraceProfilerInitialize(500 /* us */, "libomptarget");
65 #endif
66 }
67 
deinit()68 __attribute__((destructor(101))) void deinit() {
69   DP("Deinit target library!\n");
70   delete PM;
71 
72 #ifdef OMPTARGET_PROFILE_ENABLED
73   if (ProfileTraceFile) {
74     // TODO: add env var for file output
75     if (auto E = llvm::timeTraceProfilerWrite(ProfileTraceFile, "-"))
76       fprintf(stderr, "Error writing out the time trace\n");
77 
78     llvm::timeTraceProfilerCleanup();
79   }
80 #endif
81 }
82 
loadRTLs()83 void RTLsTy::loadRTLs() {
84   // Parse environment variable OMP_TARGET_OFFLOAD (if set)
85   PM->TargetOffloadPolicy =
86       (kmp_target_offload_kind_t)__kmpc_get_target_offload();
87   if (PM->TargetOffloadPolicy == tgt_disabled) {
88     return;
89   }
90 
91   DP("Loading RTLs...\n");
92 
93   // Attempt to open all the plugins and, if they exist, check if the interface
94   // is correct and if they are supporting any devices.
95   for (auto *Name : RTLNames) {
96     DP("Loading library '%s'...\n", Name);
97     void *DynlibHandle = dlopen(Name, RTLD_NOW);
98 
99     if (!DynlibHandle) {
100       // Library does not exist or cannot be found.
101       DP("Unable to load library '%s': %s!\n", Name, dlerror());
102       continue;
103     }
104 
105     DP("Successfully loaded library '%s'!\n", Name);
106 
107     AllRTLs.emplace_back();
108 
109     // Retrieve the RTL information from the runtime library.
110     RTLInfoTy &R = AllRTLs.back();
111 
112     // Remove plugin on failure to call optional init_plugin
113     *((void **)&R.init_plugin) = dlsym(DynlibHandle, "__tgt_rtl_init_plugin");
114     if (R.init_plugin) {
115       int32_t Rc = R.init_plugin();
116       if (Rc != OFFLOAD_SUCCESS) {
117         DP("Unable to initialize library '%s': %u!\n", Name, Rc);
118         AllRTLs.pop_back();
119         continue;
120       }
121     }
122 
123     bool ValidPlugin = true;
124 
125     if (!(*((void **)&R.is_valid_binary) =
126               dlsym(DynlibHandle, "__tgt_rtl_is_valid_binary")))
127       ValidPlugin = false;
128     if (!(*((void **)&R.number_of_devices) =
129               dlsym(DynlibHandle, "__tgt_rtl_number_of_devices")))
130       ValidPlugin = false;
131     if (!(*((void **)&R.init_device) =
132               dlsym(DynlibHandle, "__tgt_rtl_init_device")))
133       ValidPlugin = false;
134     if (!(*((void **)&R.load_binary) =
135               dlsym(DynlibHandle, "__tgt_rtl_load_binary")))
136       ValidPlugin = false;
137     if (!(*((void **)&R.data_alloc) =
138               dlsym(DynlibHandle, "__tgt_rtl_data_alloc")))
139       ValidPlugin = false;
140     if (!(*((void **)&R.data_submit) =
141               dlsym(DynlibHandle, "__tgt_rtl_data_submit")))
142       ValidPlugin = false;
143     if (!(*((void **)&R.data_retrieve) =
144               dlsym(DynlibHandle, "__tgt_rtl_data_retrieve")))
145       ValidPlugin = false;
146     if (!(*((void **)&R.data_delete) =
147               dlsym(DynlibHandle, "__tgt_rtl_data_delete")))
148       ValidPlugin = false;
149     if (!(*((void **)&R.run_region) =
150               dlsym(DynlibHandle, "__tgt_rtl_run_target_region")))
151       ValidPlugin = false;
152     if (!(*((void **)&R.run_team_region) =
153               dlsym(DynlibHandle, "__tgt_rtl_run_target_team_region")))
154       ValidPlugin = false;
155 
156     // Invalid plugin
157     if (!ValidPlugin) {
158       DP("Invalid plugin as necessary interface is not found.\n");
159       AllRTLs.pop_back();
160       continue;
161     }
162 
163     // No devices are supported by this RTL?
164     if (!(R.NumberOfDevices = R.number_of_devices())) {
165       // The RTL is invalid! Will pop the object from the RTLs list.
166       DP("No devices supported in this RTL\n");
167       AllRTLs.pop_back();
168       continue;
169     }
170 
171     R.LibraryHandler = DynlibHandle;
172 
173 #ifdef OMPTARGET_DEBUG
174     R.RTLName = Name;
175 #endif
176 
177     DP("Registering RTL %s supporting %d devices!\n", R.RTLName.c_str(),
178        R.NumberOfDevices);
179 
180     // Optional functions
181     *((void **)&R.deinit_plugin) =
182         dlsym(DynlibHandle, "__tgt_rtl_deinit_plugin");
183     *((void **)&R.is_valid_binary_info) =
184         dlsym(DynlibHandle, "__tgt_rtl_is_valid_binary_info");
185     *((void **)&R.deinit_device) =
186         dlsym(DynlibHandle, "__tgt_rtl_deinit_device");
187     *((void **)&R.init_requires) =
188         dlsym(DynlibHandle, "__tgt_rtl_init_requires");
189     *((void **)&R.data_submit_async) =
190         dlsym(DynlibHandle, "__tgt_rtl_data_submit_async");
191     *((void **)&R.data_retrieve_async) =
192         dlsym(DynlibHandle, "__tgt_rtl_data_retrieve_async");
193     *((void **)&R.run_region_async) =
194         dlsym(DynlibHandle, "__tgt_rtl_run_target_region_async");
195     *((void **)&R.run_team_region_async) =
196         dlsym(DynlibHandle, "__tgt_rtl_run_target_team_region_async");
197     *((void **)&R.synchronize) = dlsym(DynlibHandle, "__tgt_rtl_synchronize");
198     *((void **)&R.data_exchange) =
199         dlsym(DynlibHandle, "__tgt_rtl_data_exchange");
200     *((void **)&R.data_exchange_async) =
201         dlsym(DynlibHandle, "__tgt_rtl_data_exchange_async");
202     *((void **)&R.is_data_exchangable) =
203         dlsym(DynlibHandle, "__tgt_rtl_is_data_exchangable");
204     *((void **)&R.register_lib) = dlsym(DynlibHandle, "__tgt_rtl_register_lib");
205     *((void **)&R.unregister_lib) =
206         dlsym(DynlibHandle, "__tgt_rtl_unregister_lib");
207     *((void **)&R.supports_empty_images) =
208         dlsym(DynlibHandle, "__tgt_rtl_supports_empty_images");
209     *((void **)&R.set_info_flag) =
210         dlsym(DynlibHandle, "__tgt_rtl_set_info_flag");
211     *((void **)&R.print_device_info) =
212         dlsym(DynlibHandle, "__tgt_rtl_print_device_info");
213     *((void **)&R.create_event) = dlsym(DynlibHandle, "__tgt_rtl_create_event");
214     *((void **)&R.record_event) = dlsym(DynlibHandle, "__tgt_rtl_record_event");
215     *((void **)&R.wait_event) = dlsym(DynlibHandle, "__tgt_rtl_wait_event");
216     *((void **)&R.sync_event) = dlsym(DynlibHandle, "__tgt_rtl_sync_event");
217     *((void **)&R.destroy_event) =
218         dlsym(DynlibHandle, "__tgt_rtl_destroy_event");
219     *((void **)&R.release_async_info) =
220         dlsym(DynlibHandle, "__tgt_rtl_release_async_info");
221     *((void **)&R.init_async_info) =
222         dlsym(DynlibHandle, "__tgt_rtl_init_async_info");
223     *((void **)&R.init_device_info) =
224         dlsym(DynlibHandle, "__tgt_rtl_init_device_info");
225   }
226 
227   DP("RTLs loaded!\n");
228 
229   return;
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 // Functionality for registering libs
234 
registerImageIntoTranslationTable(TranslationTable & TT,RTLInfoTy & RTL,__tgt_device_image * Image)235 static void registerImageIntoTranslationTable(TranslationTable &TT,
236                                               RTLInfoTy &RTL,
237                                               __tgt_device_image *Image) {
238 
239   // same size, as when we increase one, we also increase the other.
240   assert(TT.TargetsTable.size() == TT.TargetsImages.size() &&
241          "We should have as many images as we have tables!");
242 
243   // Resize the Targets Table and Images to accommodate the new targets if
244   // required
245   unsigned TargetsTableMinimumSize = RTL.Idx + RTL.NumberOfDevices;
246 
247   if (TT.TargetsTable.size() < TargetsTableMinimumSize) {
248     TT.TargetsImages.resize(TargetsTableMinimumSize, 0);
249     TT.TargetsTable.resize(TargetsTableMinimumSize, 0);
250   }
251 
252   // Register the image in all devices for this target type.
253   for (int32_t I = 0; I < RTL.NumberOfDevices; ++I) {
254     // If we are changing the image we are also invalidating the target table.
255     if (TT.TargetsImages[RTL.Idx + I] != Image) {
256       TT.TargetsImages[RTL.Idx + I] = Image;
257       TT.TargetsTable[RTL.Idx + I] = 0; // lazy initialization of target table.
258     }
259   }
260 }
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 // Functionality for registering Ctors/Dtors
264 
registerGlobalCtorsDtorsForImage(__tgt_bin_desc * Desc,__tgt_device_image * Img,RTLInfoTy * RTL)265 static void registerGlobalCtorsDtorsForImage(__tgt_bin_desc *Desc,
266                                              __tgt_device_image *Img,
267                                              RTLInfoTy *RTL) {
268 
269   for (int32_t I = 0; I < RTL->NumberOfDevices; ++I) {
270     DeviceTy &Device = *PM->Devices[RTL->Idx + I];
271     Device.PendingGlobalsMtx.lock();
272     Device.HasPendingGlobals = true;
273     for (__tgt_offload_entry *Entry = Img->EntriesBegin;
274          Entry != Img->EntriesEnd; ++Entry) {
275       if (Entry->flags & OMP_DECLARE_TARGET_CTOR) {
276         DP("Adding ctor " DPxMOD " to the pending list.\n",
277            DPxPTR(Entry->addr));
278         Device.PendingCtorsDtors[Desc].PendingCtors.push_back(Entry->addr);
279       } else if (Entry->flags & OMP_DECLARE_TARGET_DTOR) {
280         // Dtors are pushed in reverse order so they are executed from end
281         // to beginning when unregistering the library!
282         DP("Adding dtor " DPxMOD " to the pending list.\n",
283            DPxPTR(Entry->addr));
284         Device.PendingCtorsDtors[Desc].PendingDtors.push_front(Entry->addr);
285       }
286 
287       if (Entry->flags & OMP_DECLARE_TARGET_LINK) {
288         DP("The \"link\" attribute is not yet supported!\n");
289       }
290     }
291     Device.PendingGlobalsMtx.unlock();
292   }
293 }
294 
getExecutableImage(__tgt_device_image * Image)295 static __tgt_device_image getExecutableImage(__tgt_device_image *Image) {
296   llvm::StringRef ImageStr(static_cast<char *>(Image->ImageStart),
297                            static_cast<char *>(Image->ImageEnd) -
298                                static_cast<char *>(Image->ImageStart));
299   auto BinaryOrErr =
300       llvm::object::OffloadBinary::create(llvm::MemoryBufferRef(ImageStr, ""));
301   if (!BinaryOrErr) {
302     llvm::consumeError(BinaryOrErr.takeError());
303     return *Image;
304   }
305 
306   void *Begin = const_cast<void *>(
307       static_cast<const void *>((*BinaryOrErr)->getImage().bytes_begin()));
308   void *End = const_cast<void *>(
309       static_cast<const void *>((*BinaryOrErr)->getImage().bytes_end()));
310 
311   return {Begin, End, Image->EntriesBegin, Image->EntriesEnd};
312 }
313 
getImageInfo(__tgt_device_image * Image)314 static __tgt_image_info getImageInfo(__tgt_device_image *Image) {
315   llvm::StringRef ImageStr(static_cast<char *>(Image->ImageStart),
316                            static_cast<char *>(Image->ImageEnd) -
317                                static_cast<char *>(Image->ImageStart));
318   auto BinaryOrErr =
319       llvm::object::OffloadBinary::create(llvm::MemoryBufferRef(ImageStr, ""));
320   if (!BinaryOrErr) {
321     llvm::consumeError(BinaryOrErr.takeError());
322     return __tgt_image_info{};
323   }
324 
325   return __tgt_image_info{(*BinaryOrErr)->getArch().data()};
326 }
327 
registerRequires(int64_t Flags)328 void RTLsTy::registerRequires(int64_t Flags) {
329   // TODO: add more elaborate check.
330   // Minimal check: only set requires flags if previous value
331   // is undefined. This ensures that only the first call to this
332   // function will set the requires flags. All subsequent calls
333   // will be checked for compatibility.
334   assert(Flags != OMP_REQ_UNDEFINED &&
335          "illegal undefined flag for requires directive!");
336   if (RequiresFlags == OMP_REQ_UNDEFINED) {
337     RequiresFlags = Flags;
338     return;
339   }
340 
341   // If multiple compilation units are present enforce
342   // consistency across all of them for require clauses:
343   //  - reverse_offload
344   //  - unified_address
345   //  - unified_shared_memory
346   if ((RequiresFlags & OMP_REQ_REVERSE_OFFLOAD) !=
347       (Flags & OMP_REQ_REVERSE_OFFLOAD)) {
348     FATAL_MESSAGE0(
349         1, "'#pragma omp requires reverse_offload' not used consistently!");
350   }
351   if ((RequiresFlags & OMP_REQ_UNIFIED_ADDRESS) !=
352       (Flags & OMP_REQ_UNIFIED_ADDRESS)) {
353     FATAL_MESSAGE0(
354         1, "'#pragma omp requires unified_address' not used consistently!");
355   }
356   if ((RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) !=
357       (Flags & OMP_REQ_UNIFIED_SHARED_MEMORY)) {
358     FATAL_MESSAGE0(
359         1,
360         "'#pragma omp requires unified_shared_memory' not used consistently!");
361   }
362 
363   // TODO: insert any other missing checks
364 
365   DP("New requires flags %" PRId64 " compatible with existing %" PRId64 "!\n",
366      Flags, RequiresFlags);
367 }
368 
initRTLonce(RTLInfoTy & R)369 void RTLsTy::initRTLonce(RTLInfoTy &R) {
370   // If this RTL is not already in use, initialize it.
371   if (!R.IsUsed && R.NumberOfDevices != 0) {
372     // Initialize the device information for the RTL we are about to use.
373     const size_t Start = PM->Devices.size();
374     PM->Devices.reserve(Start + R.NumberOfDevices);
375     for (int32_t DeviceId = 0; DeviceId < R.NumberOfDevices; DeviceId++) {
376       PM->Devices.push_back(std::make_unique<DeviceTy>(&R));
377       // global device ID
378       PM->Devices[Start + DeviceId]->DeviceID = Start + DeviceId;
379       // RTL local device ID
380       PM->Devices[Start + DeviceId]->RTLDeviceID = DeviceId;
381     }
382 
383     // Initialize the index of this RTL and save it in the used RTLs.
384     R.Idx = (UsedRTLs.empty())
385                 ? 0
386                 : UsedRTLs.back()->Idx + UsedRTLs.back()->NumberOfDevices;
387     assert((size_t)R.Idx == Start &&
388            "RTL index should equal the number of devices used so far.");
389     R.IsUsed = true;
390     UsedRTLs.push_back(&R);
391 
392     DP("RTL " DPxMOD " has index %d!\n", DPxPTR(R.LibraryHandler), R.Idx);
393   }
394 }
395 
initAllRTLs()396 void RTLsTy::initAllRTLs() {
397   for (auto &R : AllRTLs)
398     initRTLonce(R);
399 }
400 
registerLib(__tgt_bin_desc * Desc)401 void RTLsTy::registerLib(__tgt_bin_desc *Desc) {
402   PM->RTLsMtx.lock();
403 
404   // Extract the exectuable image and extra information if availible.
405   for (int32_t i = 0; i < Desc->NumDeviceImages; ++i)
406     PM->Images.emplace_back(getExecutableImage(&Desc->DeviceImages[i]),
407                             getImageInfo(&Desc->DeviceImages[i]));
408 
409   // Register the images with the RTLs that understand them, if any.
410   for (auto &ImageAndInfo : PM->Images) {
411     // Obtain the image and information that was previously extracted.
412     __tgt_device_image *Img = &ImageAndInfo.first;
413     __tgt_image_info *Info = &ImageAndInfo.second;
414 
415     RTLInfoTy *FoundRTL = nullptr;
416 
417     // Scan the RTLs that have associated images until we find one that supports
418     // the current image.
419     for (auto &R : AllRTLs) {
420       if (R.is_valid_binary_info) {
421         if (!R.is_valid_binary_info(Img, Info)) {
422           DP("Image " DPxMOD " is NOT compatible with RTL %s!\n",
423              DPxPTR(Img->ImageStart), R.RTLName.c_str());
424           continue;
425         }
426       } else if (!R.is_valid_binary(Img)) {
427         DP("Image " DPxMOD " is NOT compatible with RTL %s!\n",
428            DPxPTR(Img->ImageStart), R.RTLName.c_str());
429         continue;
430       }
431 
432       DP("Image " DPxMOD " is compatible with RTL %s!\n",
433          DPxPTR(Img->ImageStart), R.RTLName.c_str());
434 
435       initRTLonce(R);
436 
437       // Initialize (if necessary) translation table for this library.
438       PM->TrlTblMtx.lock();
439       if (!PM->HostEntriesBeginToTransTable.count(Desc->HostEntriesBegin)) {
440         PM->HostEntriesBeginRegistrationOrder.push_back(Desc->HostEntriesBegin);
441         TranslationTable &TransTable =
442             (PM->HostEntriesBeginToTransTable)[Desc->HostEntriesBegin];
443         TransTable.HostTable.EntriesBegin = Desc->HostEntriesBegin;
444         TransTable.HostTable.EntriesEnd = Desc->HostEntriesEnd;
445       }
446 
447       // Retrieve translation table for this library.
448       TranslationTable &TransTable =
449           (PM->HostEntriesBeginToTransTable)[Desc->HostEntriesBegin];
450 
451       DP("Registering image " DPxMOD " with RTL %s!\n", DPxPTR(Img->ImageStart),
452          R.RTLName.c_str());
453       registerImageIntoTranslationTable(TransTable, R, Img);
454       PM->TrlTblMtx.unlock();
455       FoundRTL = &R;
456 
457       // Load ctors/dtors for static objects
458       registerGlobalCtorsDtorsForImage(Desc, Img, FoundRTL);
459 
460       // if an RTL was found we are done - proceed to register the next image
461       break;
462     }
463 
464     if (!FoundRTL) {
465       DP("No RTL found for image " DPxMOD "!\n", DPxPTR(Img->ImageStart));
466     }
467   }
468   PM->RTLsMtx.unlock();
469 
470   DP("Done registering entries!\n");
471 }
472 
unregisterLib(__tgt_bin_desc * Desc)473 void RTLsTy::unregisterLib(__tgt_bin_desc *Desc) {
474   DP("Unloading target library!\n");
475 
476   PM->RTLsMtx.lock();
477   // Find which RTL understands each image, if any.
478   for (auto &ImageAndInfo : PM->Images) {
479     // Obtain the image and information that was previously extracted.
480     __tgt_device_image *Img = &ImageAndInfo.first;
481     __tgt_image_info *Info = &ImageAndInfo.second;
482 
483     RTLInfoTy *FoundRTL = NULL;
484 
485     // Scan the RTLs that have associated images until we find one that supports
486     // the current image. We only need to scan RTLs that are already being used.
487     for (auto *R : UsedRTLs) {
488 
489       assert(R->IsUsed && "Expecting used RTLs.");
490 
491       if (R->is_valid_binary_info) {
492         if (!R->is_valid_binary_info(Img, Info)) {
493           DP("Image " DPxMOD " is NOT compatible with RTL %s!\n",
494              DPxPTR(Img->ImageStart), R->RTLName.c_str());
495           continue;
496         }
497       } else if (!R->is_valid_binary(Img)) {
498         DP("Image " DPxMOD " is NOT compatible with RTL %s!\n",
499            DPxPTR(Img->ImageStart), R->RTLName.c_str());
500         continue;
501       }
502 
503       DP("Image " DPxMOD " is compatible with RTL " DPxMOD "!\n",
504          DPxPTR(Img->ImageStart), DPxPTR(R->LibraryHandler));
505 
506       FoundRTL = R;
507 
508       // Execute dtors for static objects if the device has been used, i.e.
509       // if its PendingCtors list has been emptied.
510       for (int32_t I = 0; I < FoundRTL->NumberOfDevices; ++I) {
511         DeviceTy &Device = *PM->Devices[FoundRTL->Idx + I];
512         Device.PendingGlobalsMtx.lock();
513         if (Device.PendingCtorsDtors[Desc].PendingCtors.empty()) {
514           AsyncInfoTy AsyncInfo(Device);
515           for (auto &Dtor : Device.PendingCtorsDtors[Desc].PendingDtors) {
516             int Rc = target(nullptr, Device, Dtor, 0, nullptr, nullptr, nullptr,
517                             nullptr, nullptr, nullptr, 1, 1, 0, true /*team*/,
518                             AsyncInfo);
519             if (Rc != OFFLOAD_SUCCESS) {
520               DP("Running destructor " DPxMOD " failed.\n", DPxPTR(Dtor));
521             }
522           }
523           // Remove this library's entry from PendingCtorsDtors
524           Device.PendingCtorsDtors.erase(Desc);
525           // All constructors have been issued, wait for them now.
526           if (AsyncInfo.synchronize() != OFFLOAD_SUCCESS)
527             DP("Failed synchronizing destructors kernels.\n");
528         }
529         Device.PendingGlobalsMtx.unlock();
530       }
531 
532       DP("Unregistered image " DPxMOD " from RTL " DPxMOD "!\n",
533          DPxPTR(Img->ImageStart), DPxPTR(R->LibraryHandler));
534 
535       break;
536     }
537 
538     // if no RTL was found proceed to unregister the next image
539     if (!FoundRTL) {
540       DP("No RTLs in use support the image " DPxMOD "!\n",
541          DPxPTR(Img->ImageStart));
542     }
543   }
544   PM->RTLsMtx.unlock();
545   DP("Done unregistering images!\n");
546 
547   // Remove entries from PM->HostPtrToTableMap
548   PM->TblMapMtx.lock();
549   for (__tgt_offload_entry *Cur = Desc->HostEntriesBegin;
550        Cur < Desc->HostEntriesEnd; ++Cur) {
551     PM->HostPtrToTableMap.erase(Cur->addr);
552   }
553 
554   // Remove translation table for this descriptor.
555   auto TransTable =
556       PM->HostEntriesBeginToTransTable.find(Desc->HostEntriesBegin);
557   if (TransTable != PM->HostEntriesBeginToTransTable.end()) {
558     DP("Removing translation table for descriptor " DPxMOD "\n",
559        DPxPTR(Desc->HostEntriesBegin));
560     PM->HostEntriesBeginToTransTable.erase(TransTable);
561   } else {
562     DP("Translation table for descriptor " DPxMOD " cannot be found, probably "
563        "it has been already removed.\n",
564        DPxPTR(Desc->HostEntriesBegin));
565   }
566 
567   PM->TblMapMtx.unlock();
568 
569   // TODO: Write some RTL->unload_image(...) function?
570   for (auto *R : UsedRTLs) {
571     if (R->deinit_plugin) {
572       if (R->deinit_plugin() != OFFLOAD_SUCCESS) {
573         DP("Failure deinitializing RTL %s!\n", R->RTLName.c_str());
574       }
575     }
576   }
577 
578   DP("Done unregistering library!\n");
579 }
580