2020
2121#include " llvm/ADT/DenseSet.h"
2222#include " llvm/ADT/SmallVector.h"
23+ #include " llvm/ADT/iterator_range.h"
2324#include " llvm/Support/DynamicLibrary.h"
2425
2526#include < list>
2627#include < mutex>
28+ #include < string>
2729
2830struct PluginAdaptorTy {
29- int32_t Idx = -1 ; // RTL index, index is the number of devices
30- // of other RTLs that were registered before,
31- // i.e. the OpenMP index of the first device
32- // to be registered with this RTL.
33- int32_t NumberOfDevices = -1 ; // Number of devices this RTL deals with.
31+ PluginAdaptorTy (const std::string &Name);
3432
35- std::unique_ptr<llvm::sys::DynamicLibrary> LibraryHandler;
33+ bool isUsed () const { return DeviceOffset >= 0 ; }
34+
35+ // / Return the number of devices available to this plugin.
36+ int32_t getNumDevices () const { return NumberOfDevices; }
37+
38+ // / RTL index, index is the number of devices of other RTLs that were
39+ // / registered before, i.e. the OpenMP index of the first device to be
40+ // / registered with this RTL.
41+ int32_t DeviceOffset = -1 ;
3642
37- #ifdef OMPTARGET_DEBUG
38- std::string RTLName;
39- #endif
43+ // / Number of devices this RTL deals with.
44+ int32_t NumberOfDevices = -1 ;
45+
46+ // / Name of the shared object file representing the plugin.
47+ std::string Name;
48+
49+ // / Access to the shared object file representing the plugin.
50+ std::unique_ptr<llvm::sys::DynamicLibrary> LibraryHandler;
4051
4152#define PLUGIN_API_HANDLE (NAME, MANDATORY ) \
4253 using NAME##_ty = decltype (__tgt_rtl_##NAME); \
@@ -45,9 +56,6 @@ struct PluginAdaptorTy {
4556#include " Shared/PluginAPI.inc"
4657#undef PLUGIN_API_HANDLE
4758
48- // Are there images associated with this RTL.
49- bool IsUsed = false ;
50-
5159 llvm::DenseSet<const __tgt_device_image *> UsedImages;
5260
5361 // Mutex for thread-safety when calling RTL interface functions.
@@ -58,41 +66,26 @@ struct PluginAdaptorTy {
5866
5967// / RTLs identified in the system.
6068struct PluginAdaptorManagerTy {
61- // List of the detected runtime libraries.
62- std::list<PluginAdaptorTy> AllRTLs;
63-
64- // Array of pointers to the detected runtime libraries that have compatible
65- // binaries.
66- llvm::SmallVector<PluginAdaptorTy *> UsedRTLs;
67-
6869 int64_t RequiresFlags = OMP_REQ_UNDEFINED;
6970
7071 explicit PluginAdaptorManagerTy () = default;
7172
7273 // Register the clauses of the requires directive.
7374 void registerRequires (int64_t Flags);
7475
75- // Initialize RTL if it has not been initialized
76- void initRTLonce (PluginAdaptorTy &RTL);
77-
78- // Initialize all RTLs
79- void initAllRTLs ();
80-
8176 // Register a shared library with all (compatible) RTLs.
8277 void registerLib (__tgt_bin_desc *Desc);
8378
8479 // Unregister a shared library from all RTLs.
8580 void unregisterLib (__tgt_bin_desc *Desc);
86-
87- // not thread-safe, called from global constructor (i.e. once)
88- void loadRTLs ();
89-
90- private:
91- static bool attemptLoadRTL (const std::string &RTLName, PluginAdaptorTy &RTL);
9281};
9382
9483// / Struct for the data required to handle plugins
9584struct PluginManager {
85+ PluginManager () {}
86+
87+ void init ();
88+
9689 // / RTLs identified on the host
9790 PluginAdaptorManagerTy RTLs;
9891
@@ -141,9 +134,30 @@ struct PluginManager {
141134 return Devices.size ();
142135 }
143136
137+ int getNumUsedPlugins () const {
138+ int NCI = 0 ;
139+ for (auto &P : PluginAdaptors)
140+ NCI += P.isUsed ();
141+ return NCI;
142+ }
143+
144+ // Initialize \p Plugin if it has not been initialized.
145+ void initPlugin (PluginAdaptorTy &Plugin);
146+
147+ // Initialize all plugins.
148+ void initAllPlugins ();
149+
150+ // / Iterator range for all plugin adaptors (in use or not, but always valid).
151+ auto pluginAdaptors () {
152+ return llvm::make_range (PluginAdaptors.begin (), PluginAdaptors.end ());
153+ }
154+
144155private:
145156 bool RTLsLoaded = false ;
146157 llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;
158+
159+ // List of all plugin adaptors, in use or not.
160+ std::list<PluginAdaptorTy> PluginAdaptors;
147161};
148162
149163extern PluginManager *PM;
0 commit comments