13
13
14
14
#include " OffloadImpl.hpp"
15
15
#include " Helpers.hpp"
16
+ #include " OffloadPrint.hpp"
16
17
#include " PluginManager.h"
17
18
#include " llvm/Support/FormatVariadic.h"
18
19
#include < OffloadAPI.h>
@@ -234,23 +235,22 @@ Error olShutDown_impl() {
234
235
Error olGetPlatformInfoImplDetail (ol_platform_handle_t Platform,
235
236
ol_platform_info_t PropName, size_t PropSize,
236
237
void *PropValue, size_t *PropSizeRet) {
237
- ReturnHelper ReturnValue (PropSize, PropValue, PropSizeRet);
238
+ InfoWriter Info (PropSize, PropValue, PropSizeRet);
238
239
bool IsHost = Platform->BackendType == OL_PLATFORM_BACKEND_HOST;
239
240
240
241
switch (PropName) {
241
242
case OL_PLATFORM_INFO_NAME:
242
- return ReturnValue (IsHost ? " Host" : Platform->Plugin ->getName ());
243
+ return Info. writeString (IsHost ? " Host" : Platform->Plugin ->getName ());
243
244
case OL_PLATFORM_INFO_VENDOR_NAME:
244
245
// TODO: Implement this
245
- return ReturnValue (" Unknown platform vendor" );
246
+ return Info. writeString (" Unknown platform vendor" );
246
247
case OL_PLATFORM_INFO_VERSION: {
247
- return ReturnValue (formatv (" v{0}.{1}.{2}" , OL_VERSION_MAJOR,
248
- OL_VERSION_MINOR, OL_VERSION_PATCH)
249
- .str ()
250
- .c_str ());
248
+ return Info.writeString (formatv (" v{0}.{1}.{2}" , OL_VERSION_MAJOR,
249
+ OL_VERSION_MINOR, OL_VERSION_PATCH)
250
+ .str ());
251
251
}
252
252
case OL_PLATFORM_INFO_BACKEND: {
253
- return ReturnValue (Platform->BackendType );
253
+ return Info. write < ol_platform_backend_t > (Platform->BackendType );
254
254
}
255
255
default :
256
256
return createOffloadError (ErrorCode::INVALID_ENUMERATION,
@@ -277,36 +277,68 @@ Error olGetPlatformInfoSize_impl(ol_platform_handle_t Platform,
277
277
Error olGetDeviceInfoImplDetail (ol_device_handle_t Device,
278
278
ol_device_info_t PropName, size_t PropSize,
279
279
void *PropValue, size_t *PropSizeRet) {
280
+ assert (Device != OffloadContext::get ().HostDevice ());
281
+ InfoWriter Info (PropSize, PropValue, PropSizeRet);
280
282
281
- ReturnHelper ReturnValue (PropSize, PropValue, PropSizeRet);
283
+ auto makeError = [&](ErrorCode Code, StringRef Err) {
284
+ std::string ErrBuffer;
285
+ llvm::raw_string_ostream (ErrBuffer) << PropName << " : " << Err;
286
+ return Plugin::error (ErrorCode::UNIMPLEMENTED, ErrBuffer.c_str ());
287
+ };
282
288
283
289
// Find the info if it exists under any of the given names
284
- auto GetInfoString = [&](std::vector<std::string> Names) {
285
- if (Device == OffloadContext::get ().HostDevice ())
286
- return " Host" ;
287
-
288
- for (auto Name : Names) {
289
- if (auto Entry = Device->Info .get (Name))
290
+ auto getInfoString =
291
+ [&](std::vector<std::string> Names) -> llvm::Expected<const char *> {
292
+ for (auto &Name : Names) {
293
+ if (auto Entry = Device->Info .get (Name)) {
294
+ if (!std::holds_alternative<std::string>((*Entry)->Value ))
295
+ return makeError (ErrorCode::BACKEND_FAILURE,
296
+ " plugin returned incorrect type" );
290
297
return std::get<std::string>((*Entry)->Value ).c_str ();
298
+ }
291
299
}
292
300
293
- return " " ;
301
+ return makeError (ErrorCode::UNIMPLEMENTED,
302
+ " plugin did not provide a response for this information" );
294
303
};
295
304
296
305
switch (PropName) {
297
306
case OL_DEVICE_INFO_PLATFORM:
298
- return ReturnValue (Device->Platform );
307
+ return Info.write <void *>(Device->Platform );
308
+ case OL_DEVICE_INFO_TYPE:
309
+ return Info.write <ol_device_type_t >(OL_DEVICE_TYPE_GPU);
310
+ case OL_DEVICE_INFO_NAME:
311
+ return Info.writeString (getInfoString ({" Device Name" }));
312
+ case OL_DEVICE_INFO_VENDOR:
313
+ return Info.writeString (getInfoString ({" Vendor Name" }));
314
+ case OL_DEVICE_INFO_DRIVER_VERSION:
315
+ return Info.writeString (
316
+ getInfoString ({" CUDA Driver Version" , " HSA Runtime Version" }));
317
+ default :
318
+ return createOffloadError (ErrorCode::INVALID_ENUMERATION,
319
+ " getDeviceInfo enum '%i' is invalid" , PropName);
320
+ }
321
+
322
+ return Error::success ();
323
+ }
324
+
325
+ Error olGetDeviceInfoImplDetailHost (ol_device_handle_t Device,
326
+ ol_device_info_t PropName, size_t PropSize,
327
+ void *PropValue, size_t *PropSizeRet) {
328
+ assert (Device == OffloadContext::get ().HostDevice ());
329
+ InfoWriter Info (PropSize, PropValue, PropSizeRet);
330
+
331
+ switch (PropName) {
332
+ case OL_DEVICE_INFO_PLATFORM:
333
+ return Info.write <void *>(Device->Platform );
299
334
case OL_DEVICE_INFO_TYPE:
300
- return Device == OffloadContext::get ().HostDevice ()
301
- ? ReturnValue (OL_DEVICE_TYPE_HOST)
302
- : ReturnValue (OL_DEVICE_TYPE_GPU);
335
+ return Info.write <ol_device_type_t >(OL_DEVICE_TYPE_HOST);
303
336
case OL_DEVICE_INFO_NAME:
304
- return ReturnValue ( GetInfoString ({ " Device Name " }) );
337
+ return Info. writeString ( " Virtual Host Device " );
305
338
case OL_DEVICE_INFO_VENDOR:
306
- return ReturnValue ( GetInfoString ({ " Vendor Name " }) );
339
+ return Info. writeString ( " Liboffload " );
307
340
case OL_DEVICE_INFO_DRIVER_VERSION:
308
- return ReturnValue (
309
- GetInfoString ({" CUDA Driver Version" , " HSA Runtime Version" }));
341
+ return Info.writeString (LLVM_VERSION_STRING);
310
342
default :
311
343
return createOffloadError (ErrorCode::INVALID_ENUMERATION,
312
344
" getDeviceInfo enum '%i' is invalid" , PropName);
@@ -317,12 +349,18 @@ Error olGetDeviceInfoImplDetail(ol_device_handle_t Device,
317
349
318
350
Error olGetDeviceInfo_impl (ol_device_handle_t Device, ol_device_info_t PropName,
319
351
size_t PropSize, void *PropValue) {
352
+ if (Device == OffloadContext::get ().HostDevice ())
353
+ return olGetDeviceInfoImplDetailHost (Device, PropName, PropSize, PropValue,
354
+ nullptr );
320
355
return olGetDeviceInfoImplDetail (Device, PropName, PropSize, PropValue,
321
356
nullptr );
322
357
}
323
358
324
359
Error olGetDeviceInfoSize_impl (ol_device_handle_t Device,
325
360
ol_device_info_t PropName, size_t *PropSizeRet) {
361
+ if (Device == OffloadContext::get ().HostDevice ())
362
+ return olGetDeviceInfoImplDetailHost (Device, PropName, 0 , nullptr ,
363
+ PropSizeRet);
326
364
return olGetDeviceInfoImplDetail (Device, PropName, 0 , nullptr , PropSizeRet);
327
365
}
328
366
0 commit comments