@@ -122,3 +122,194 @@ void PluginManager::initAllPlugins() {
122
122
for (auto &R : PluginAdaptors)
123
123
initPlugin (R);
124
124
}
125
+
126
+ static void registerImageIntoTranslationTable (TranslationTable &TT,
127
+ PluginAdaptorTy &RTL,
128
+ __tgt_device_image *Image) {
129
+
130
+ // same size, as when we increase one, we also increase the other.
131
+ assert (TT.TargetsTable .size () == TT.TargetsImages .size () &&
132
+ " We should have as many images as we have tables!" );
133
+
134
+ // Resize the Targets Table and Images to accommodate the new targets if
135
+ // required
136
+ unsigned TargetsTableMinimumSize = RTL.DeviceOffset + RTL.NumberOfDevices ;
137
+
138
+ if (TT.TargetsTable .size () < TargetsTableMinimumSize) {
139
+ TT.TargetsImages .resize (TargetsTableMinimumSize, 0 );
140
+ TT.TargetsTable .resize (TargetsTableMinimumSize, 0 );
141
+ }
142
+
143
+ // Register the image in all devices for this target type.
144
+ for (int32_t I = 0 ; I < RTL.NumberOfDevices ; ++I) {
145
+ // If we are changing the image we are also invalidating the target table.
146
+ if (TT.TargetsImages [RTL.DeviceOffset + I] != Image) {
147
+ TT.TargetsImages [RTL.DeviceOffset + I] = Image;
148
+ TT.TargetsTable [RTL.DeviceOffset + I] =
149
+ 0 ; // lazy initialization of target table.
150
+ }
151
+ }
152
+ }
153
+
154
+ void PluginManager::registerLib (__tgt_bin_desc *Desc) {
155
+ PM->RTLsMtx .lock ();
156
+
157
+ // Extract the exectuable image and extra information if availible.
158
+ for (int32_t i = 0 ; i < Desc->NumDeviceImages ; ++i)
159
+ PM->addDeviceImage (*Desc, Desc->DeviceImages [i]);
160
+
161
+ // Register the images with the RTLs that understand them, if any.
162
+ for (DeviceImageTy &DI : PM->deviceImages ()) {
163
+ // Obtain the image and information that was previously extracted.
164
+ __tgt_device_image *Img = &DI.getExecutableImage ();
165
+ __tgt_image_info *Info = &DI.getImageInfo ();
166
+
167
+ PluginAdaptorTy *FoundRTL = nullptr ;
168
+
169
+ // Scan the RTLs that have associated images until we find one that supports
170
+ // the current image.
171
+ for (auto &R : PM->pluginAdaptors ()) {
172
+ if (R.is_valid_binary_info ) {
173
+ if (!R.is_valid_binary_info (Img, Info)) {
174
+ DP (" Image " DPxMOD " is NOT compatible with RTL %s!\n " ,
175
+ DPxPTR (Img->ImageStart ), R.Name .c_str ());
176
+ continue ;
177
+ }
178
+ } else if (!R.is_valid_binary (Img)) {
179
+ DP (" Image " DPxMOD " is NOT compatible with RTL %s!\n " ,
180
+ DPxPTR (Img->ImageStart ), R.Name .c_str ());
181
+ continue ;
182
+ }
183
+
184
+ DP (" Image " DPxMOD " is compatible with RTL %s!\n " ,
185
+ DPxPTR (Img->ImageStart ), R.Name .c_str ());
186
+
187
+ PM->initPlugin (R);
188
+
189
+ // Initialize (if necessary) translation table for this library.
190
+ PM->TrlTblMtx .lock ();
191
+ if (!PM->HostEntriesBeginToTransTable .count (Desc->HostEntriesBegin )) {
192
+ PM->HostEntriesBeginRegistrationOrder .push_back (Desc->HostEntriesBegin );
193
+ TranslationTable &TransTable =
194
+ (PM->HostEntriesBeginToTransTable )[Desc->HostEntriesBegin ];
195
+ TransTable.HostTable .EntriesBegin = Desc->HostEntriesBegin ;
196
+ TransTable.HostTable .EntriesEnd = Desc->HostEntriesEnd ;
197
+ }
198
+
199
+ // Retrieve translation table for this library.
200
+ TranslationTable &TransTable =
201
+ (PM->HostEntriesBeginToTransTable )[Desc->HostEntriesBegin ];
202
+
203
+ DP (" Registering image " DPxMOD " with RTL %s!\n " , DPxPTR (Img->ImageStart ),
204
+ R.Name .c_str ());
205
+ registerImageIntoTranslationTable (TransTable, R, Img);
206
+ R.UsedImages .insert (Img);
207
+
208
+ PM->TrlTblMtx .unlock ();
209
+ FoundRTL = &R;
210
+
211
+ // Register all offload entries with the devices handled by the plugin.
212
+ R.addOffloadEntries (DI);
213
+
214
+ // if an RTL was found we are done - proceed to register the next image
215
+ break ;
216
+ }
217
+
218
+ if (!FoundRTL) {
219
+ DP (" No RTL found for image " DPxMOD " !\n " , DPxPTR (Img->ImageStart ));
220
+ }
221
+ }
222
+ PM->RTLsMtx .unlock ();
223
+
224
+ DP (" Done registering entries!\n " );
225
+ }
226
+
227
+ // Temporary forward declaration, old style CTor/DTor handling is going away.
228
+ int target (ident_t *Loc, DeviceTy &Device, void *HostPtr,
229
+ KernelArgsTy &KernelArgs, AsyncInfoTy &AsyncInfo);
230
+
231
+ void PluginManager::unregisterLib (__tgt_bin_desc *Desc) {
232
+ DP (" Unloading target library!\n " );
233
+
234
+ PM->RTLsMtx .lock ();
235
+ // Find which RTL understands each image, if any.
236
+ for (DeviceImageTy &DI : PM->deviceImages ()) {
237
+ // Obtain the image and information that was previously extracted.
238
+ __tgt_device_image *Img = &DI.getExecutableImage ();
239
+
240
+ PluginAdaptorTy *FoundRTL = NULL ;
241
+
242
+ // Scan the RTLs that have associated images until we find one that supports
243
+ // the current image. We only need to scan RTLs that are already being used.
244
+ for (auto &R : PM->pluginAdaptors ()) {
245
+ if (!R.isUsed ())
246
+ continue ;
247
+
248
+ // Ensure that we do not use any unused images associated with this RTL.
249
+ if (!R.UsedImages .contains (Img))
250
+ continue ;
251
+
252
+ FoundRTL = &R;
253
+
254
+ // Execute dtors for static objects if the device has been used, i.e.
255
+ // if its PendingCtors list has been emptied.
256
+ for (int32_t I = 0 ; I < FoundRTL->NumberOfDevices ; ++I) {
257
+ DeviceTy &Device = *PM->Devices [FoundRTL->DeviceOffset + I];
258
+ Device.PendingGlobalsMtx .lock ();
259
+ if (Device.PendingCtorsDtors [Desc].PendingCtors .empty ()) {
260
+ AsyncInfoTy AsyncInfo (Device);
261
+ for (auto &Dtor : Device.PendingCtorsDtors [Desc].PendingDtors ) {
262
+ int Rc =
263
+ target (nullptr , Device, Dtor, CTorDTorKernelArgs, AsyncInfo);
264
+ if (Rc != OFFLOAD_SUCCESS) {
265
+ DP (" Running destructor " DPxMOD " failed.\n " , DPxPTR (Dtor));
266
+ }
267
+ }
268
+ // Remove this library's entry from PendingCtorsDtors
269
+ Device.PendingCtorsDtors .erase (Desc);
270
+ // All constructors have been issued, wait for them now.
271
+ if (AsyncInfo.synchronize () != OFFLOAD_SUCCESS)
272
+ DP (" Failed synchronizing destructors kernels.\n " );
273
+ }
274
+ Device.PendingGlobalsMtx .unlock ();
275
+ }
276
+
277
+ DP (" Unregistered image " DPxMOD " from RTL " DPxMOD " !\n " ,
278
+ DPxPTR (Img->ImageStart ), DPxPTR (R.LibraryHandler .get ()));
279
+
280
+ break ;
281
+ }
282
+
283
+ // if no RTL was found proceed to unregister the next image
284
+ if (!FoundRTL) {
285
+ DP (" No RTLs in use support the image " DPxMOD " !\n " ,
286
+ DPxPTR (Img->ImageStart ));
287
+ }
288
+ }
289
+ PM->RTLsMtx .unlock ();
290
+ DP (" Done unregistering images!\n " );
291
+
292
+ // Remove entries from PM->HostPtrToTableMap
293
+ PM->TblMapMtx .lock ();
294
+ for (__tgt_offload_entry *Cur = Desc->HostEntriesBegin ;
295
+ Cur < Desc->HostEntriesEnd ; ++Cur) {
296
+ PM->HostPtrToTableMap .erase (Cur->addr );
297
+ }
298
+
299
+ // Remove translation table for this descriptor.
300
+ auto TransTable =
301
+ PM->HostEntriesBeginToTransTable .find (Desc->HostEntriesBegin );
302
+ if (TransTable != PM->HostEntriesBeginToTransTable .end ()) {
303
+ DP (" Removing translation table for descriptor " DPxMOD " \n " ,
304
+ DPxPTR (Desc->HostEntriesBegin ));
305
+ PM->HostEntriesBeginToTransTable .erase (TransTable);
306
+ } else {
307
+ DP (" Translation table for descriptor " DPxMOD " cannot be found, probably "
308
+ " it has been already removed.\n " ,
309
+ DPxPTR (Desc->HostEntriesBegin ));
310
+ }
311
+
312
+ PM->TblMapMtx .unlock ();
313
+
314
+ DP (" Done unregistering library!\n " );
315
+ }
0 commit comments