Skip to content

Commit 410bfa0

Browse files
JonChesterfieldtstellar
authored andcommitted
[openmp] Introduce optional plugin init/deinit functions
Will allow plugins to migrate away from using global variables to manage lifetime, which will fix a segfault discovered in relation to D127432 Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D130712 (cherry picked from commit 1f9d397)
1 parent e4ec381 commit 410bfa0

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

openmp/libomptarget/include/omptargetplugin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
extern "C" {
2121
#endif
2222

23+
// First method called on the plugin
24+
int32_t __tgt_rtl_init_plugin();
25+
26+
// Last method called on the plugin
27+
int32_t __tgt_rtl_deinit_plugin();
28+
2329
// Return the number of available devices of the type supported by the
2430
// target RTL.
2531
int32_t __tgt_rtl_number_of_devices(void);

openmp/libomptarget/include/rtl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct DeviceTy;
2525
struct __tgt_bin_desc;
2626

2727
struct RTLInfoTy {
28+
typedef int32_t(init_plugin_ty)();
29+
typedef int32_t(deinit_plugin_ty)();
2830
typedef int32_t(is_valid_binary_ty)(void *);
2931
typedef int32_t(is_valid_binary_info_ty)(void *, void *);
3032
typedef int32_t(is_data_exchangable_ty)(int32_t, int32_t);
@@ -82,6 +84,8 @@ struct RTLInfoTy {
8284
#endif
8385

8486
// Functions implemented in the RTL.
87+
init_plugin_ty *init_plugin = nullptr;
88+
deinit_plugin_ty *deinit_plugin = nullptr;
8589
is_valid_binary_ty *is_valid_binary = nullptr;
8690
is_valid_binary_info_ty *is_valid_binary_info = nullptr;
8791
is_data_exchangable_ty *is_data_exchangable = nullptr;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,9 @@ int32_t __tgt_rtl_is_valid_binary_info(__tgt_device_image *image,
20482048
return true;
20492049
}
20502050

2051+
int32_t __tgt_rtl_init_plugin() { return OFFLOAD_SUCCESS; }
2052+
int32_t __tgt_rtl_deinit_plugin() { return OFFLOAD_SUCCESS; }
2053+
20512054
int __tgt_rtl_number_of_devices() {
20522055
// If the construction failed, no methods are safe to call
20532056
if (DeviceInfo.ConstructionSucceeded) {

openmp/libomptarget/plugins/exports

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
VERS1.0 {
22
global:
3+
__tgt_rtl_init_plugin;
4+
__tgt_rtl_deinit_plugin;
35
__tgt_rtl_is_valid_binary;
46
__tgt_rtl_is_valid_binary_info;
57
__tgt_rtl_is_data_exchangable;

openmp/libomptarget/src/rtl.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ void RTLsTy::loadRTLs() {
109109
// Retrieve the RTL information from the runtime library.
110110
RTLInfoTy &R = AllRTLs.back();
111111

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+
112123
bool ValidPlugin = true;
113124

114125
if (!(*((void **)&R.is_valid_binary) =
@@ -167,6 +178,8 @@ void RTLsTy::loadRTLs() {
167178
R.NumberOfDevices);
168179

169180
// Optional functions
181+
*((void **)&R.deinit_plugin) =
182+
dlsym(DynlibHandle, "__tgt_rtl_deinit_plugin");
170183
*((void **)&R.is_valid_binary_info) =
171184
dlsym(DynlibHandle, "__tgt_rtl_is_valid_binary_info");
172185
*((void **)&R.deinit_device) =
@@ -553,8 +566,14 @@ void RTLsTy::unregisterLib(__tgt_bin_desc *Desc) {
553566

554567
PM->TblMapMtx.unlock();
555568

556-
// TODO: Remove RTL and the devices it manages if it's not used anymore?
557569
// 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+
}
558577

559578
DP("Done unregistering library!\n");
560579
}

0 commit comments

Comments
 (0)