1 //===----------- rtl.cpp - Target independent OpenMP target RTL -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.txt for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Functionality for handling RTL plugins.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "device.h"
15 #include "private.h"
16 #include "rtl.h"
17 
18 #include <cassert>
19 #include <cstring>
20 #include <dlfcn.h>
21 #include <mutex>
22 
23 // List of all plugins that can support offloading.
24 static const char *RTLNames[] = {
25     /* PowerPC target */ "libomptarget.rtl.ppc64.so",
26     /* x86_64 target  */ "libomptarget.rtl.x86_64.so",
27     /* CUDA target    */ "libomptarget.rtl.cuda.so",
28     /* AArch64 target */ "libomptarget.rtl.aarch64.so"};
29 
30 RTLsTy RTLs;
31 std::mutex RTLsMtx;
32 
33 HostEntriesBeginToTransTableTy HostEntriesBeginToTransTable;
34 std::mutex TrlTblMtx;
35 
36 HostPtrToTableMapTy HostPtrToTableMap;
37 std::mutex TblMapMtx;
38 
39 void RTLsTy::LoadRTLs() {
40 #ifdef OMPTARGET_DEBUG
41   if (char *envStr = getenv("LIBOMPTARGET_DEBUG")) {
42     DebugLevel = std::stoi(envStr);
43   }
44 #endif // OMPTARGET_DEBUG
45 
46   // Parse environment variable OMP_TARGET_OFFLOAD (if set)
47   char *envStr = getenv("OMP_TARGET_OFFLOAD");
48   if (envStr && !strcmp(envStr, "DISABLED")) {
49     DP("Target offloading disabled by environment\n");
50     return;
51   }
52 
53   DP("Loading RTLs...\n");
54 
55   // Attempt to open all the plugins and, if they exist, check if the interface
56   // is correct and if they are supporting any devices.
57   for (auto *Name : RTLNames) {
58     DP("Loading library '%s'...\n", Name);
59     void *dynlib_handle = dlopen(Name, RTLD_NOW);
60 
61     if (!dynlib_handle) {
62       // Library does not exist or cannot be found.
63       DP("Unable to load library '%s': %s!\n", Name, dlerror());
64       continue;
65     }
66 
67     DP("Successfully loaded library '%s'!\n", Name);
68 
69     // Retrieve the RTL information from the runtime library.
70     RTLInfoTy R;
71 
72     R.LibraryHandler = dynlib_handle;
73     R.isUsed = false;
74 
75 #ifdef OMPTARGET_DEBUG
76     R.RTLName = Name;
77 #endif
78 
79     if (!(*((void**) &R.is_valid_binary) = dlsym(
80               dynlib_handle, "__tgt_rtl_is_valid_binary")))
81       continue;
82     if (!(*((void**) &R.number_of_devices) = dlsym(
83               dynlib_handle, "__tgt_rtl_number_of_devices")))
84       continue;
85     if (!(*((void**) &R.init_device) = dlsym(
86               dynlib_handle, "__tgt_rtl_init_device")))
87       continue;
88     if (!(*((void**) &R.load_binary) = dlsym(
89               dynlib_handle, "__tgt_rtl_load_binary")))
90       continue;
91     if (!(*((void**) &R.data_alloc) = dlsym(
92               dynlib_handle, "__tgt_rtl_data_alloc")))
93       continue;
94     if (!(*((void**) &R.data_submit) = dlsym(
95               dynlib_handle, "__tgt_rtl_data_submit")))
96       continue;
97     if (!(*((void**) &R.data_retrieve) = dlsym(
98               dynlib_handle, "__tgt_rtl_data_retrieve")))
99       continue;
100     if (!(*((void**) &R.data_delete) = dlsym(
101               dynlib_handle, "__tgt_rtl_data_delete")))
102       continue;
103     if (!(*((void**) &R.run_region) = dlsym(
104               dynlib_handle, "__tgt_rtl_run_target_region")))
105       continue;
106     if (!(*((void**) &R.run_team_region) = dlsym(
107               dynlib_handle, "__tgt_rtl_run_target_team_region")))
108       continue;
109 
110     // No devices are supported by this RTL?
111     if (!(R.NumberOfDevices = R.number_of_devices())) {
112       DP("No devices supported in this RTL\n");
113       continue;
114     }
115 
116     DP("Registering RTL %s supporting %d devices!\n",
117         R.RTLName.c_str(), R.NumberOfDevices);
118 
119     // The RTL is valid! Will save the information in the RTLs list.
120     AllRTLs.push_back(R);
121   }
122 
123   DP("RTLs loaded!\n");
124 
125   return;
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 // Functionality for registering libs
130 
131 static void RegisterImageIntoTranslationTable(TranslationTable &TT,
132     RTLInfoTy &RTL, __tgt_device_image *image) {
133 
134   // same size, as when we increase one, we also increase the other.
135   assert(TT.TargetsTable.size() == TT.TargetsImages.size() &&
136          "We should have as many images as we have tables!");
137 
138   // Resize the Targets Table and Images to accommodate the new targets if
139   // required
140   unsigned TargetsTableMinimumSize = RTL.Idx + RTL.NumberOfDevices;
141 
142   if (TT.TargetsTable.size() < TargetsTableMinimumSize) {
143     TT.TargetsImages.resize(TargetsTableMinimumSize, 0);
144     TT.TargetsTable.resize(TargetsTableMinimumSize, 0);
145   }
146 
147   // Register the image in all devices for this target type.
148   for (int32_t i = 0; i < RTL.NumberOfDevices; ++i) {
149     // If we are changing the image we are also invalidating the target table.
150     if (TT.TargetsImages[RTL.Idx + i] != image) {
151       TT.TargetsImages[RTL.Idx + i] = image;
152       TT.TargetsTable[RTL.Idx + i] = 0; // lazy initialization of target table.
153     }
154   }
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 // Functionality for registering Ctors/Dtors
159 
160 static void RegisterGlobalCtorsDtorsForImage(__tgt_bin_desc *desc,
161     __tgt_device_image *img, RTLInfoTy *RTL) {
162 
163   for (int32_t i = 0; i < RTL->NumberOfDevices; ++i) {
164     DeviceTy &Device = Devices[RTL->Idx + i];
165     Device.PendingGlobalsMtx.lock();
166     Device.HasPendingGlobals = true;
167     for (__tgt_offload_entry *entry = img->EntriesBegin;
168         entry != img->EntriesEnd; ++entry) {
169       if (entry->flags & OMP_DECLARE_TARGET_CTOR) {
170         DP("Adding ctor " DPxMOD " to the pending list.\n",
171             DPxPTR(entry->addr));
172         Device.PendingCtorsDtors[desc].PendingCtors.push_back(entry->addr);
173       } else if (entry->flags & OMP_DECLARE_TARGET_DTOR) {
174         // Dtors are pushed in reverse order so they are executed from end
175         // to beginning when unregistering the library!
176         DP("Adding dtor " DPxMOD " to the pending list.\n",
177             DPxPTR(entry->addr));
178         Device.PendingCtorsDtors[desc].PendingDtors.push_front(entry->addr);
179       }
180 
181       if (entry->flags & OMP_DECLARE_TARGET_LINK) {
182         DP("The \"link\" attribute is not yet supported!\n");
183       }
184     }
185     Device.PendingGlobalsMtx.unlock();
186   }
187 }
188 
189 void RTLsTy::RegisterLib(__tgt_bin_desc *desc) {
190   // Attempt to load all plugins available in the system.
191   std::call_once(initFlag, &RTLsTy::LoadRTLs, this);
192 
193   RTLsMtx.lock();
194   // Register the images with the RTLs that understand them, if any.
195   for (int32_t i = 0; i < desc->NumDeviceImages; ++i) {
196     // Obtain the image.
197     __tgt_device_image *img = &desc->DeviceImages[i];
198 
199     RTLInfoTy *FoundRTL = NULL;
200 
201     // Scan the RTLs that have associated images until we find one that supports
202     // the current image.
203     for (auto &R : RTLs.AllRTLs) {
204       if (!R.is_valid_binary(img)) {
205         DP("Image " DPxMOD " is NOT compatible with RTL %s!\n",
206             DPxPTR(img->ImageStart), R.RTLName.c_str());
207         continue;
208       }
209 
210       DP("Image " DPxMOD " is compatible with RTL %s!\n",
211           DPxPTR(img->ImageStart), R.RTLName.c_str());
212 
213       // If this RTL is not already in use, initialize it.
214       if (!R.isUsed) {
215         // Initialize the device information for the RTL we are about to use.
216         DeviceTy device(&R);
217 
218         size_t start = Devices.size();
219         Devices.resize(start + R.NumberOfDevices, device);
220         for (int32_t device_id = 0; device_id < R.NumberOfDevices;
221             device_id++) {
222           // global device ID
223           Devices[start + device_id].DeviceID = start + device_id;
224           // RTL local device ID
225           Devices[start + device_id].RTLDeviceID = device_id;
226 
227           // Save pointer to device in RTL in case we want to unregister the RTL
228           R.Devices.push_back(&Devices[start + device_id]);
229         }
230 
231         // Initialize the index of this RTL and save it in the used RTLs.
232         R.Idx = (RTLs.UsedRTLs.empty())
233                     ? 0
234                     : RTLs.UsedRTLs.back()->Idx +
235                           RTLs.UsedRTLs.back()->NumberOfDevices;
236         assert((size_t) R.Idx == start &&
237             "RTL index should equal the number of devices used so far.");
238         R.isUsed = true;
239         RTLs.UsedRTLs.push_back(&R);
240 
241         DP("RTL " DPxMOD " has index %d!\n", DPxPTR(R.LibraryHandler), R.Idx);
242       }
243 
244       // Initialize (if necessary) translation table for this library.
245       TrlTblMtx.lock();
246       if(!HostEntriesBeginToTransTable.count(desc->HostEntriesBegin)){
247         TranslationTable &tt =
248             HostEntriesBeginToTransTable[desc->HostEntriesBegin];
249         tt.HostTable.EntriesBegin = desc->HostEntriesBegin;
250         tt.HostTable.EntriesEnd = desc->HostEntriesEnd;
251       }
252 
253       // Retrieve translation table for this library.
254       TranslationTable &TransTable =
255           HostEntriesBeginToTransTable[desc->HostEntriesBegin];
256 
257       DP("Registering image " DPxMOD " with RTL %s!\n",
258           DPxPTR(img->ImageStart), R.RTLName.c_str());
259       RegisterImageIntoTranslationTable(TransTable, R, img);
260       TrlTblMtx.unlock();
261       FoundRTL = &R;
262 
263       // Load ctors/dtors for static objects
264       RegisterGlobalCtorsDtorsForImage(desc, img, FoundRTL);
265 
266       // if an RTL was found we are done - proceed to register the next image
267       break;
268     }
269 
270     if (!FoundRTL) {
271       DP("No RTL found for image " DPxMOD "!\n", DPxPTR(img->ImageStart));
272     }
273   }
274   RTLsMtx.unlock();
275 
276 
277   DP("Done registering entries!\n");
278 }
279 
280 void RTLsTy::UnregisterLib(__tgt_bin_desc *desc) {
281   DP("Unloading target library!\n");
282 
283   RTLsMtx.lock();
284   // Find which RTL understands each image, if any.
285   for (int32_t i = 0; i < desc->NumDeviceImages; ++i) {
286     // Obtain the image.
287     __tgt_device_image *img = &desc->DeviceImages[i];
288 
289     RTLInfoTy *FoundRTL = NULL;
290 
291     // Scan the RTLs that have associated images until we find one that supports
292     // the current image. We only need to scan RTLs that are already being used.
293     for (auto *R : RTLs.UsedRTLs) {
294 
295       assert(R->isUsed && "Expecting used RTLs.");
296 
297       if (!R->is_valid_binary(img)) {
298         DP("Image " DPxMOD " is NOT compatible with RTL " DPxMOD "!\n",
299             DPxPTR(img->ImageStart), DPxPTR(R->LibraryHandler));
300         continue;
301       }
302 
303       DP("Image " DPxMOD " is compatible with RTL " DPxMOD "!\n",
304           DPxPTR(img->ImageStart), DPxPTR(R->LibraryHandler));
305 
306       FoundRTL = R;
307 
308       // Execute dtors for static objects if the device has been used, i.e.
309       // if its PendingCtors list has been emptied.
310       for (int32_t i = 0; i < FoundRTL->NumberOfDevices; ++i) {
311         DeviceTy &Device = Devices[FoundRTL->Idx + i];
312         Device.PendingGlobalsMtx.lock();
313         if (Device.PendingCtorsDtors[desc].PendingCtors.empty()) {
314           for (auto &dtor : Device.PendingCtorsDtors[desc].PendingDtors) {
315             int rc = target(Device.DeviceID, dtor, 0, NULL, NULL, NULL, NULL, 1,
316                 1, true /*team*/);
317             if (rc != OFFLOAD_SUCCESS) {
318               DP("Running destructor " DPxMOD " failed.\n", DPxPTR(dtor));
319             }
320           }
321           // Remove this library's entry from PendingCtorsDtors
322           Device.PendingCtorsDtors.erase(desc);
323         }
324         Device.PendingGlobalsMtx.unlock();
325       }
326 
327       DP("Unregistered image " DPxMOD " from RTL " DPxMOD "!\n",
328           DPxPTR(img->ImageStart), DPxPTR(R->LibraryHandler));
329 
330       break;
331     }
332 
333     // if no RTL was found proceed to unregister the next image
334     if (!FoundRTL){
335       DP("No RTLs in use support the image " DPxMOD "!\n",
336           DPxPTR(img->ImageStart));
337     }
338   }
339   RTLsMtx.unlock();
340   DP("Done unregistering images!\n");
341 
342   // Remove entries from HostPtrToTableMap
343   TblMapMtx.lock();
344   for (__tgt_offload_entry *cur = desc->HostEntriesBegin;
345       cur < desc->HostEntriesEnd; ++cur) {
346     HostPtrToTableMap.erase(cur->addr);
347   }
348 
349   // Remove translation table for this descriptor.
350   auto tt = HostEntriesBeginToTransTable.find(desc->HostEntriesBegin);
351   if (tt != HostEntriesBeginToTransTable.end()) {
352     DP("Removing translation table for descriptor " DPxMOD "\n",
353         DPxPTR(desc->HostEntriesBegin));
354     HostEntriesBeginToTransTable.erase(tt);
355   } else {
356     DP("Translation table for descriptor " DPxMOD " cannot be found, probably "
357         "it has been already removed.\n", DPxPTR(desc->HostEntriesBegin));
358   }
359 
360   TblMapMtx.unlock();
361 
362   // TODO: Remove RTL and the devices it manages if it's not used anymore?
363   // TODO: Write some RTL->unload_image(...) function?
364 
365   DP("Done unregistering library!\n");
366 }
367