20
20
21
21
#include " llvm/ADT/DenseSet.h"
22
22
#include " llvm/ADT/SmallVector.h"
23
+ #include " llvm/ADT/iterator_range.h"
23
24
#include " llvm/Support/DynamicLibrary.h"
24
25
25
26
#include < list>
26
27
#include < mutex>
28
+ #include < string>
27
29
28
30
struct 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);
34
32
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 ;
36
42
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;
40
51
41
52
#define PLUGIN_API_HANDLE (NAME, MANDATORY ) \
42
53
using NAME##_ty = decltype(__tgt_rtl_##NAME); \
@@ -45,9 +56,6 @@ struct PluginAdaptorTy {
45
56
#include " Shared/PluginAPI.inc"
46
57
#undef PLUGIN_API_HANDLE
47
58
48
- // Are there images associated with this RTL.
49
- bool IsUsed = false ;
50
-
51
59
llvm::DenseSet<const __tgt_device_image *> UsedImages;
52
60
53
61
// Mutex for thread-safety when calling RTL interface functions.
@@ -58,41 +66,26 @@ struct PluginAdaptorTy {
58
66
59
67
// / RTLs identified in the system.
60
68
struct 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
-
68
69
int64_t RequiresFlags = OMP_REQ_UNDEFINED;
69
70
70
71
explicit PluginAdaptorManagerTy () = default;
71
72
72
73
// Register the clauses of the requires directive.
73
74
void registerRequires (int64_t Flags);
74
75
75
- // Initialize RTL if it has not been initialized
76
- void initRTLonce (PluginAdaptorTy &RTL);
77
-
78
- // Initialize all RTLs
79
- void initAllRTLs ();
80
-
81
76
// Register a shared library with all (compatible) RTLs.
82
77
void registerLib (__tgt_bin_desc *Desc);
83
78
84
79
// Unregister a shared library from all RTLs.
85
80
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);
92
81
};
93
82
94
83
// / Struct for the data required to handle plugins
95
84
struct PluginManager {
85
+ PluginManager () {}
86
+
87
+ void init ();
88
+
96
89
// / RTLs identified on the host
97
90
PluginAdaptorManagerTy RTLs;
98
91
@@ -141,9 +134,30 @@ struct PluginManager {
141
134
return Devices.size ();
142
135
}
143
136
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
+
144
155
private:
145
156
bool RTLsLoaded = false ;
146
157
llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;
158
+
159
+ // List of all plugin adaptors, in use or not.
160
+ std::list<PluginAdaptorTy> PluginAdaptors;
147
161
};
148
162
149
163
extern PluginManager *PM;
0 commit comments