Skip to content

Commit 2257e3d

Browse files
[openmp] Workaround for HSA in issue 60119
Move plugin initialization to libomptarget initialization. Removes the call_once control, probably fractionally faster overall. Fixes issue 60119 because the plugin initialization, which might try to dlopen unrelated shared libraries, is no longer nested within a call from application code. Fixes #60119 Reviewed By: Maetveis, jhuber6 Differential Revision: https://reviews.llvm.org/D142249
1 parent 5a4e9aa commit 2257e3d

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

openmp/libomptarget/include/device.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff 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

535560
extern PluginManager *PM;

openmp/libomptarget/include/rtl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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

179177
private:
180178
static bool attemptLoadRTL(const std::string &RTLName, RTLInfoTy &RTL);

openmp/libomptarget/plugins/remote/server/Server.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ Status RemoteOffloadImpl::IsValidBinary(ServerContext *Context,
9090
Status 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)

openmp/libomptarget/src/interface.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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
3636
EXTERN 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) {

openmp/libomptarget/src/rtl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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() {

0 commit comments

Comments
 (0)