Skip to content

Commit 542d9c2

Browse files
manoromSimon Moll
authored andcommitted
[libomptarget] Load images in order of registration
This makes sure that images are loaded in the order in which they are registered with libomptarget. If a target can load multiple images and these images depend on each other (for example if one image contains the programs target regions and one image contains library code), then the order in which images are loaded can be important for symbol resolution (for example, in the VE plugin). In this case: because the same code exist in the host binaries, the order in which the host linker loads them (which is also the order in which images are registered with libomptarget) is the order in which the images have to be loaded onto the device. Reviewed By: JonChesterfield Differential Revision: https://reviews.llvm.org/D95530
1 parent 43a569f commit 542d9c2

File tree

7 files changed

+23
-5
lines changed

7 files changed

+23
-5
lines changed

openmp/libomptarget/include/omptargetplugin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *Image);
3636
// function to move data from source device to destination device directly.
3737
int32_t __tgt_rtl_is_data_exchangable(int32_t SrcDevId, int32_t DstDevId);
3838

39+
// Return an integer other than zero if the plugin can handle images which do
40+
// not contain target regions and global variables (but can contain other
41+
// functions)
42+
int32_t __tgt_rtl_supports_empty_images();
43+
3944
// Initialize the requires flags for the device.
4045
int64_t __tgt_rtl_init_requires(int64_t RequiresFlags);
4146

openmp/libomptarget/plugins/exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ VERS1.0 {
2121
__tgt_rtl_synchronize;
2222
__tgt_rtl_register_lib;
2323
__tgt_rtl_unregister_lib;
24+
__tgt_rtl_supports_empty_images;
2425
local:
2526
*;
2627
};

openmp/libomptarget/plugins/ve/src/rtl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,5 @@ int32_t __tgt_rtl_run_target_region(int32_t ID, void *Entry, void **Args,
444444
return __tgt_rtl_run_target_team_region(ID, Entry, Args, Offsets, NumArgs, 1,
445445
1, 0);
446446
}
447+
448+
int32_t __tgt_rtl_supports_empty_images() { return 1; }

openmp/libomptarget/src/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ struct PluginManager {
241241
/// Translation table retreived from the binary
242242
HostEntriesBeginToTransTableTy HostEntriesBeginToTransTable;
243243
std::mutex TrlTblMtx; ///< For Translation Table
244+
/// Host offload entries in order of image registration
245+
std::vector<__tgt_offload_entry *> HostEntriesBeginRegistrationOrder;
244246

245247
/// Map from ptrs on the host to an entry in the Translation Table
246248
HostPtrToTableMapTy HostPtrToTableMap;

openmp/libomptarget/src/omptarget.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,21 @@ static int InitLibrary(DeviceTy &Device) {
7575
*/
7676
int32_t device_id = Device.DeviceID;
7777
int rc = OFFLOAD_SUCCESS;
78+
bool supportsEmptyImages = Device.RTL->supports_empty_images &&
79+
Device.RTL->supports_empty_images() > 0;
7880

7981
Device.PendingGlobalsMtx.lock();
8082
PM->TrlTblMtx.lock();
81-
for (HostEntriesBeginToTransTableTy::iterator entry_it =
82-
PM->HostEntriesBeginToTransTable.begin();
83-
entry_it != PM->HostEntriesBeginToTransTable.end(); ++entry_it) {
84-
TranslationTable *TransTable = &entry_it->second;
83+
for (auto *HostEntriesBegin : PM->HostEntriesBeginRegistrationOrder) {
84+
TranslationTable *TransTable =
85+
&PM->HostEntriesBeginToTransTable[HostEntriesBegin];
8586
if (TransTable->HostTable.EntriesBegin ==
86-
TransTable->HostTable.EntriesEnd) {
87+
TransTable->HostTable.EntriesEnd &&
88+
!supportsEmptyImages) {
8789
// No host entry so no need to proceed
8890
continue;
8991
}
92+
9093
if (TransTable->TargetsTable[device_id] != 0) {
9194
// Library entries have already been processed
9295
continue;

openmp/libomptarget/src/rtl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ void RTLsTy::LoadRTLs() {
173173
dlsym(dynlib_handle, "__tgt_rtl_register_lib");
174174
*((void **)&R.unregister_lib) =
175175
dlsym(dynlib_handle, "__tgt_rtl_unregister_lib");
176+
*((void **)&R.supports_empty_images) =
177+
dlsym(dynlib_handle, "__tgt_rtl_supports_empty_images");
176178
}
177179

178180
DP("RTLs loaded!\n");
@@ -334,6 +336,7 @@ void RTLsTy::RegisterLib(__tgt_bin_desc *desc) {
334336
// Initialize (if necessary) translation table for this library.
335337
PM->TrlTblMtx.lock();
336338
if (!PM->HostEntriesBeginToTransTable.count(desc->HostEntriesBegin)) {
339+
PM->HostEntriesBeginRegistrationOrder.push_back(desc->HostEntriesBegin);
337340
TranslationTable &TransTable =
338341
(PM->HostEntriesBeginToTransTable)[desc->HostEntriesBegin];
339342
TransTable.HostTable.EntriesBegin = desc->HostEntriesBegin;

openmp/libomptarget/src/rtl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct RTLInfoTy {
5454
typedef int64_t(init_requires_ty)(int64_t);
5555
typedef int64_t(synchronize_ty)(int32_t, __tgt_async_info *);
5656
typedef int32_t (*register_lib_ty)(__tgt_bin_desc *);
57+
typedef int32_t(supports_empty_images_ty)();
5758

5859
int32_t Idx = -1; // RTL index, index is the number of devices
5960
// of other RTLs that were registered before,
@@ -89,6 +90,7 @@ struct RTLInfoTy {
8990
synchronize_ty *synchronize = nullptr;
9091
register_lib_ty register_lib = nullptr;
9192
register_lib_ty unregister_lib = nullptr;
93+
supports_empty_images_ty *supports_empty_images = nullptr;
9294

9395
// Are there images associated with this RTL.
9496
bool isUsed = false;

0 commit comments

Comments
 (0)