[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
This commit is contained in:
Jon Chesterfield 2022-07-28 16:21:36 +01:00
parent 59ea2c64d5
commit 1f9d3974e4
5 changed files with 35 additions and 1 deletions

View File

@ -20,6 +20,12 @@
extern "C" {
#endif
// First method called on the plugin
int32_t __tgt_rtl_init_plugin();
// Last method called on the plugin
int32_t __tgt_rtl_deinit_plugin();
// Return the number of available devices of the type supported by the
// target RTL.
int32_t __tgt_rtl_number_of_devices(void);

View File

@ -25,6 +25,8 @@ struct DeviceTy;
struct __tgt_bin_desc;
struct RTLInfoTy {
typedef int32_t(init_plugin_ty)();
typedef int32_t(deinit_plugin_ty)();
typedef int32_t(is_valid_binary_ty)(void *);
typedef int32_t(is_valid_binary_info_ty)(void *, void *);
typedef int32_t(is_data_exchangable_ty)(int32_t, int32_t);
@ -82,6 +84,8 @@ struct RTLInfoTy {
#endif
// Functions implemented in the RTL.
init_plugin_ty *init_plugin = nullptr;
deinit_plugin_ty *deinit_plugin = nullptr;
is_valid_binary_ty *is_valid_binary = nullptr;
is_valid_binary_info_ty *is_valid_binary_info = nullptr;
is_data_exchangable_ty *is_data_exchangable = nullptr;

View File

@ -2048,6 +2048,9 @@ int32_t __tgt_rtl_is_valid_binary_info(__tgt_device_image *image,
return true;
}
int32_t __tgt_rtl_init_plugin() { return OFFLOAD_SUCCESS; }
int32_t __tgt_rtl_deinit_plugin() { return OFFLOAD_SUCCESS; }
int __tgt_rtl_number_of_devices() {
// If the construction failed, no methods are safe to call
if (DeviceInfo.ConstructionSucceeded) {

View File

@ -1,5 +1,7 @@
VERS1.0 {
global:
__tgt_rtl_init_plugin;
__tgt_rtl_deinit_plugin;
__tgt_rtl_is_valid_binary;
__tgt_rtl_is_valid_binary_info;
__tgt_rtl_is_data_exchangable;

View File

@ -109,6 +109,17 @@ void RTLsTy::loadRTLs() {
// Retrieve the RTL information from the runtime library.
RTLInfoTy &R = AllRTLs.back();
// Remove plugin on failure to call optional init_plugin
*((void **)&R.init_plugin) = dlsym(DynlibHandle, "__tgt_rtl_init_plugin");
if (R.init_plugin) {
int32_t Rc = R.init_plugin();
if (Rc != OFFLOAD_SUCCESS) {
DP("Unable to initialize library '%s': %u!\n", Name, Rc);
AllRTLs.pop_back();
continue;
}
}
bool ValidPlugin = true;
if (!(*((void **)&R.is_valid_binary) =
@ -167,6 +178,8 @@ void RTLsTy::loadRTLs() {
R.NumberOfDevices);
// Optional functions
*((void **)&R.deinit_plugin) =
dlsym(DynlibHandle, "__tgt_rtl_deinit_plugin");
*((void **)&R.is_valid_binary_info) =
dlsym(DynlibHandle, "__tgt_rtl_is_valid_binary_info");
*((void **)&R.deinit_device) =
@ -553,8 +566,14 @@ void RTLsTy::unregisterLib(__tgt_bin_desc *Desc) {
PM->TblMapMtx.unlock();
// TODO: Remove RTL and the devices it manages if it's not used anymore?
// TODO: Write some RTL->unload_image(...) function?
for (auto *R : UsedRTLs) {
if (R->deinit_plugin) {
if (R->deinit_plugin() != OFFLOAD_SUCCESS) {
DP("Failure deinitializing RTL %s!\n", R->RTLName.c_str());
}
}
}
DP("Done unregistering library!\n");
}