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
// Forward declarations.
29
31
struct DeviceTy ;
30
32
struct __tgt_bin_desc ;
31
33
32
34
struct PluginAdaptorTy {
33
- int32_t Idx = -1 ; // RTL index, index is the number of devices
34
- // of other RTLs that were registered before,
35
- // i.e. the OpenMP index of the first device
36
- // to be registered with this RTL.
37
- int32_t NumberOfDevices = -1 ; // Number of devices this RTL deals with.
35
+ PluginAdaptorTy (const std::string &Name);
38
36
39
- std::unique_ptr<llvm::sys::DynamicLibrary> LibraryHandler;
37
+ bool isUsed () const { return DeviceOffset >= 0 ; }
38
+
39
+ // / Return the number of devices available to this plugin.
40
+ int32_t getNumDevices () const { return NumberOfDevices; }
41
+
42
+ // / RTL index, index is the number of devices of other RTLs that were
43
+ // / registered before, i.e. the OpenMP index of the first device to be
44
+ // / registered with this RTL.
45
+ int32_t DeviceOffset = -1 ;
40
46
41
- #ifdef OMPTARGET_DEBUG
42
- std::string RTLName;
43
- #endif
47
+ // / Number of devices this RTL deals with.
48
+ int32_t NumberOfDevices = -1 ;
49
+
50
+ // / Name of the shared object file representing the plugin.
51
+ std::string Name;
52
+
53
+ // / Access to the shared object file representing the plugin.
54
+ std::unique_ptr<llvm::sys::DynamicLibrary> LibraryHandler;
44
55
45
56
#define PLUGIN_API_HANDLE (NAME, MANDATORY ) \
46
57
using NAME##_ty = decltype(__tgt_rtl_##NAME); \
@@ -49,9 +60,6 @@ struct PluginAdaptorTy {
49
60
#include " Shared/PluginAPI.inc"
50
61
#undef PLUGIN_API_HANDLE
51
62
52
- // Are there images associated with this RTL.
53
- bool IsUsed = false ;
54
-
55
63
llvm::DenseSet<const __tgt_device_image *> UsedImages;
56
64
57
65
// Mutex for thread-safety when calling RTL interface functions.
@@ -62,41 +70,26 @@ struct PluginAdaptorTy {
62
70
63
71
// / RTLs identified in the system.
64
72
struct PluginAdaptorManagerTy {
65
- // List of the detected runtime libraries.
66
- std::list<PluginAdaptorTy> AllRTLs;
67
-
68
- // Array of pointers to the detected runtime libraries that have compatible
69
- // binaries.
70
- llvm::SmallVector<PluginAdaptorTy *> UsedRTLs;
71
-
72
73
int64_t RequiresFlags = OMP_REQ_UNDEFINED;
73
74
74
75
explicit PluginAdaptorManagerTy () = default;
75
76
76
77
// Register the clauses of the requires directive.
77
78
void registerRequires (int64_t Flags);
78
79
79
- // Initialize RTL if it has not been initialized
80
- void initRTLonce (PluginAdaptorTy &RTL);
81
-
82
- // Initialize all RTLs
83
- void initAllRTLs ();
84
-
85
80
// Register a shared library with all (compatible) RTLs.
86
81
void registerLib (__tgt_bin_desc *Desc);
87
82
88
83
// Unregister a shared library from all RTLs.
89
84
void unregisterLib (__tgt_bin_desc *Desc);
90
-
91
- // not thread-safe, called from global constructor (i.e. once)
92
- void loadRTLs ();
93
-
94
- private:
95
- static bool attemptLoadRTL (const std::string &RTLName, PluginAdaptorTy &RTL);
96
85
};
97
86
98
87
// / Struct for the data required to handle plugins
99
88
struct PluginManager {
89
+ PluginManager () {}
90
+
91
+ void init ();
92
+
100
93
// / RTLs identified on the host
101
94
PluginAdaptorManagerTy RTLs;
102
95
@@ -145,9 +138,30 @@ struct PluginManager {
145
138
return Devices.size ();
146
139
}
147
140
141
+ int getNumUsedPlugins () const {
142
+ int NCI = 0 ;
143
+ for (auto &P : PluginAdaptors)
144
+ NCI += P.isUsed ();
145
+ return NCI;
146
+ }
147
+
148
+ // Initialize \p Plugin if it has not been initialized.
149
+ void initPlugin (PluginAdaptorTy &Plugin);
150
+
151
+ // Initialize all plugins.
152
+ void initAllPlugins ();
153
+
154
+ // / Iterator range for all plugin adaptors (in use or not, but always valid).
155
+ auto pluginAdaptors () {
156
+ return llvm::make_range (PluginAdaptors.begin (), PluginAdaptors.end ());
157
+ }
158
+
148
159
private:
149
160
bool RTLsLoaded = false ;
150
161
llvm::SmallVector<__tgt_bin_desc *> DelayedBinDesc;
162
+
163
+ // List of all plugin adaptors, in use or not.
164
+ std::list<PluginAdaptorTy> PluginAdaptors;
151
165
};
152
166
153
167
extern PluginManager *PM;
0 commit comments