@@ -43,18 +43,19 @@ using namespace error;
43
43
// interface.
44
44
struct ol_device_impl_t {
45
45
ol_device_impl_t (int DeviceNum, GenericDeviceTy *Device,
46
- ol_platform_handle_t Platform)
47
- : DeviceNum(DeviceNum), Device(Device), Platform(Platform) {}
46
+ ol_platform_handle_t Platform, InfoTreeNode &&DevInfo)
47
+ : DeviceNum(DeviceNum), Device(Device), Platform(Platform),
48
+ Info (std::forward<InfoTreeNode>(DevInfo)) {}
48
49
int DeviceNum;
49
50
GenericDeviceTy *Device;
50
51
ol_platform_handle_t Platform;
52
+ InfoTreeNode Info;
51
53
};
52
54
53
55
struct ol_platform_impl_t {
54
56
ol_platform_impl_t (std::unique_ptr<GenericPluginTy> Plugin,
55
- std::vector<ol_device_impl_t > Devices,
56
57
ol_platform_backend_t BackendType)
57
- : Plugin(std::move(Plugin)), Devices(Devices), BackendType(BackendType) {}
58
+ : Plugin(std::move(Plugin)), BackendType(BackendType) {}
58
59
std::unique_ptr<GenericPluginTy> Plugin;
59
60
std::vector<ol_device_impl_t > Devices;
60
61
ol_platform_backend_t BackendType;
@@ -144,15 +145,14 @@ constexpr ol_platform_backend_t pluginNameToBackend(StringRef Name) {
144
145
#define PLUGIN_TARGET (Name ) extern " C" GenericPluginTy *createPlugin_##Name();
145
146
#include " Shared/Targets.def"
146
147
147
- void initPlugins () {
148
+ Error initPlugins () {
148
149
auto *Context = new OffloadContext{};
149
150
150
151
// Attempt to create an instance of each supported plugin.
151
152
#define PLUGIN_TARGET (Name ) \
152
153
do { \
153
154
Context->Platforms .emplace_back (ol_platform_impl_t { \
154
155
std::unique_ptr<GenericPluginTy>(createPlugin_##Name ()), \
155
- {}, \
156
156
pluginNameToBackend (#Name)}); \
157
157
} while (false );
158
158
#include " Shared/Targets.def"
@@ -167,31 +167,39 @@ void initPlugins() {
167
167
for (auto DevNum = 0 ; DevNum < Platform.Plugin ->number_of_devices ();
168
168
DevNum++) {
169
169
if (Platform.Plugin ->init_device (DevNum) == OFFLOAD_SUCCESS) {
170
- Platform.Devices .emplace_back (ol_device_impl_t {
171
- DevNum, &Platform.Plugin ->getDevice (DevNum), &Platform});
170
+ auto Device = &Platform.Plugin ->getDevice (DevNum);
171
+ auto Info = Device->obtainInfoImpl ();
172
+ if (auto Err = Info.takeError ())
173
+ return Err;
174
+ Platform.Devices .emplace_back (DevNum, Device, &Platform,
175
+ std::move (*Info));
172
176
}
173
177
}
174
178
}
175
179
176
180
// Add the special host device
177
181
auto &HostPlatform = Context->Platforms .emplace_back (
178
- ol_platform_impl_t {nullptr ,
179
- {ol_device_impl_t {-1 , nullptr , nullptr }},
180
- OL_PLATFORM_BACKEND_HOST});
182
+ ol_platform_impl_t {nullptr , OL_PLATFORM_BACKEND_HOST});
183
+ HostPlatform.Devices .emplace_back (-1 , nullptr , nullptr , InfoTreeNode{});
181
184
Context->HostDevice ()->Platform = &HostPlatform;
182
185
183
186
Context->TracingEnabled = std::getenv (" OFFLOAD_TRACE" );
184
187
Context->ValidationEnabled = !std::getenv (" OFFLOAD_DISABLE_VALIDATION" );
185
188
186
189
OffloadContextVal = Context;
190
+
191
+ return Plugin::success ();
187
192
}
188
193
189
194
// TODO: We can properly reference count here and manage the resources in a more
190
195
// clever way
191
196
Error olInit_impl () {
192
197
static std::once_flag InitFlag;
193
- std::call_once (InitFlag, initPlugins);
198
+ std::optional<Error> InitResult{};
199
+ std::call_once (InitFlag, [&] { InitResult = initPlugins (); });
194
200
201
+ if (InitResult)
202
+ return std::move (*InitResult);
195
203
return Error::success ();
196
204
}
197
205
Error olShutDown_impl () { return Error::success (); }
@@ -250,15 +258,8 @@ Error olGetDeviceInfoImplDetail(ol_device_handle_t Device,
250
258
if (Device == OffloadContext::get ().HostDevice ())
251
259
return " Host" ;
252
260
253
- if (!Device->Device )
254
- return " " ;
255
-
256
- auto Info = Device->Device ->obtainInfoImpl ();
257
- if (auto Err = Info.takeError ())
258
- return " " ;
259
-
260
261
for (auto Name : Names) {
261
- if (auto Entry = Info-> get (Name))
262
+ if (auto Entry = Device-> Info . get (Name))
262
263
return std::get<std::string>((*Entry)->Value ).c_str ();
263
264
}
264
265
0 commit comments