File tree Expand file tree Collapse file tree 5 files changed +33
-7
lines changed
Expand file tree Collapse file tree 5 files changed +33
-7
lines changed Original file line number Diff line number Diff line change @@ -530,6 +530,31 @@ struct PluginManager {
530530 // / Flag to indicate if we use events to ensure the atomicity of
531531 // / map clauses or not. Can be modified with an environment variable.
532532 const bool UseEventsForAtomicTransfers;
533+
534+ // Work around for plugins that call dlopen on shared libraries that call
535+ // tgt_register_lib during their initialisation. Stash the pointers in a
536+ // vector until the plugins are all initialised and then register them.
537+ bool maybeDelayRegisterLib (__tgt_bin_desc *Desc) {
538+ if (!RTLsLoaded) {
539+ // Only reachable from libomptarget constructor
540+ DelayedBinDesc.push_back (Desc);
541+ return true ;
542+ } else {
543+ return false ;
544+ }
545+ }
546+
547+ void registerDelayedLibraries () {
548+ // Only called by libomptarget constructor
549+ RTLsLoaded = true ;
550+ for (auto *Desc : DelayedBinDesc)
551+ __tgt_register_lib (Desc);
552+ DelayedBinDesc.clear ();
553+ }
554+
555+ private:
556+ bool RTLsLoaded = false ;
557+ llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;
533558};
534559
535560extern PluginManager *PM;
Original file line number Diff line number Diff line change @@ -171,10 +171,8 @@ struct RTLsTy {
171171 // Unregister a shared library from all RTLs.
172172 void unregisterLib (__tgt_bin_desc *Desc);
173173
174- // Mutex-like object to guarantee thread-safety and unique initialization
175- // (i.e. the library attempts to load the RTLs (plugins) only once).
176- std::once_flag InitFlag;
177- void loadRTLs (); // not thread-safe
174+ // not thread-safe, called from global constructor (i.e. once)
175+ void loadRTLs ();
178176
179177private:
180178 static bool attemptLoadRTL (const std::string &RTLName, RTLInfoTy &RTL);
Original file line number Diff line number Diff line change @@ -90,8 +90,6 @@ Status RemoteOffloadImpl::IsValidBinary(ServerContext *Context,
9090Status RemoteOffloadImpl::GetNumberOfDevices (ServerContext *Context,
9191 const Null *Null,
9292 I32 *NumberOfDevices) {
93- std::call_once (PM->RTLs .initFlag , &RTLsTy::LoadRTLs, &PM->RTLs );
94-
9593 int32_t Devices = 0 ;
9694 PM->RTLsMtx .lock ();
9795 for (auto &RTL : PM->RTLs .AllRTLs )
Original file line number Diff line number Diff line change @@ -35,7 +35,9 @@ EXTERN void __tgt_register_requires(int64_t Flags) {
3535// / adds a target shared library to the target execution image
3636EXTERN void __tgt_register_lib (__tgt_bin_desc *Desc) {
3737 TIMESCOPE ();
38- std::call_once (PM->RTLs .InitFlag , &RTLsTy::loadRTLs, &PM->RTLs );
38+ if (PM->maybeDelayRegisterLib (Desc))
39+ return ;
40+
3941 for (auto &RTL : PM->RTLs .AllRTLs ) {
4042 if (RTL.register_lib ) {
4143 if ((*RTL.register_lib )(Desc) != OFFLOAD_SUCCESS) {
Original file line number Diff line number Diff line change @@ -64,6 +64,9 @@ __attribute__((constructor(101))) void init() {
6464 // TODO: add a configuration option for time granularity
6565 if (ProfileTraceFile)
6666 timeTraceProfilerInitialize (500 /* us */ , " libomptarget" );
67+
68+ PM->RTLs .loadRTLs ();
69+ PM->registerDelayedLibraries ();
6770}
6871
6972__attribute__ ((destructor(101 ))) void deinit() {
You can’t perform that action at this time.
0 commit comments