Skip to content

Commit 1c8b7c3

Browse files
committed
[Offload] Ensure all llvm::Errors are handled
`llvm::Error`s containing errors must be explicitly handled or an assert will be raised. With this change, `ol_impl_result_t` can accept and consume an `llvm::Error` for errors raised by PluginInterface that have multiple causes and other places now call `llvm::consumeError`. Note that there is currently no facility for PluginInterface to communicate exact error codes, but the constructor is designed in such a way that it can be easily added later. This MR is to convert a crash into an error code. A new test was added, however due to the aforementioned issue with error codes, it does not pass and instead is marked as a skip.
1 parent 9f94e36 commit 1c8b7c3

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

offload/liboffload/include/OffloadImpl.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/DenseSet.h"
2020
#include "llvm/ADT/StringRef.h"
2121
#include "llvm/ADT/StringSet.h"
22+
#include "llvm/Support/Error.h"
2223

2324
struct OffloadConfig {
2425
bool TracingEnabled = false;
@@ -88,6 +89,17 @@ struct ol_impl_result_t {
8889
Result = errors().emplace(std::move(Err)).first->get();
8990
}
9091

92+
ol_impl_result_t(llvm::Error &&Error, llvm::StringRef Details) {
93+
ol_errc_t ErrCode;
94+
llvm::handleAllErrors(std::move(Error), [&](llvm::StringError &Err) {
95+
// TODO: PluginInterface doesn't yet have a way to communicate offload
96+
// error codes
97+
ErrCode = OL_ERRC_UNKNOWN;
98+
});
99+
100+
ol_impl_result_t(ErrCode, Details);
101+
}
102+
91103
operator ol_result_t() { return Result; }
92104

93105
private:

offload/liboffload/src/OffloadImpl.cpp

+26-13
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,11 @@ ol_impl_result_t olMemAlloc_impl(ol_device_handle_t Device,
308308
void **AllocationOut) {
309309
auto Alloc =
310310
Device->Device->dataAlloc(Size, nullptr, convertOlToPluginAllocTy(Type));
311-
if (!Alloc)
311+
if (!Alloc) {
312+
llvm::consumeError(Alloc.takeError());
312313
return {OL_ERRC_OUT_OF_RESOURCES,
313314
formatv("Could not create allocation on device {0}", Device).str()};
315+
}
314316

315317
*AllocationOut = *Alloc;
316318
allocInfoMap().insert_or_assign(*Alloc, AllocInfo{Device, Type});
@@ -327,8 +329,10 @@ ol_impl_result_t olMemFree_impl(void *Address) {
327329

328330
auto Res =
329331
Device->Device->dataDelete(Address, convertOlToPluginAllocTy(Type));
330-
if (Res)
332+
if (Res) {
333+
llvm::consumeError(std::move(Res));
331334
return {OL_ERRC_OUT_OF_RESOURCES, "Could not free allocation"};
335+
}
332336

333337
allocInfoMap().erase(Address);
334338

@@ -340,7 +344,7 @@ ol_impl_result_t olCreateQueue_impl(ol_device_handle_t Device,
340344
auto CreatedQueue = std::make_unique<ol_queue_impl_t>(nullptr, Device);
341345
auto Err = Device->Device->initAsyncInfo(&(CreatedQueue->AsyncInfo));
342346
if (Err)
343-
return {OL_ERRC_UNKNOWN, "Could not initialize stream resource"};
347+
return {std::move(Err), "Could not initialize stream resource"};
344348

345349
*Queue = CreatedQueue.release();
346350
return OL_SUCCESS;
@@ -355,24 +359,28 @@ ol_impl_result_t olWaitQueue_impl(ol_queue_handle_t Queue) {
355359
// on it, but we have nothing to synchronize in that situation anyway.
356360
if (Queue->AsyncInfo->Queue) {
357361
auto Err = Queue->Device->Device->synchronize(Queue->AsyncInfo);
358-
if (Err)
362+
if (Err) {
363+
llvm::consumeError(std::move(Err));
359364
return {OL_ERRC_INVALID_QUEUE, "The queue failed to synchronize"};
365+
}
360366
}
361367

362368
// Recreate the stream resource so the queue can be reused
363369
// TODO: Would be easier for the synchronization to (optionally) not release
364370
// it to begin with.
365371
auto Res = Queue->Device->Device->initAsyncInfo(&Queue->AsyncInfo);
366372
if (Res)
367-
return {OL_ERRC_UNKNOWN, "Could not reinitialize the stream resource"};
373+
return {std::move(Res), "Could not reinitialize the stream resource"};
368374

369375
return OL_SUCCESS;
370376
}
371377

372378
ol_impl_result_t olWaitEvent_impl(ol_event_handle_t Event) {
373379
auto Res = Event->Queue->Device->Device->syncEvent(Event->EventInfo);
374-
if (Res)
380+
if (Res) {
381+
llvm::consumeError(std::move(Res));
375382
return {OL_ERRC_INVALID_EVENT, "The event failed to synchronize"};
383+
}
376384

377385
return OL_SUCCESS;
378386
}
@@ -384,13 +392,17 @@ ol_impl_result_t olDestroyEvent_impl(ol_event_handle_t Event) {
384392
ol_event_handle_t makeEvent(ol_queue_handle_t Queue) {
385393
auto EventImpl = std::make_unique<ol_event_impl_t>(nullptr, Queue);
386394
auto Res = Queue->Device->Device->createEvent(&EventImpl->EventInfo);
387-
if (Res)
395+
if (Res) {
396+
llvm::consumeError(std::move(Res));
388397
return nullptr;
398+
}
389399

390400
Res = Queue->Device->Device->recordEvent(EventImpl->EventInfo,
391401
Queue->AsyncInfo);
392-
if (Res)
402+
if (Res) {
403+
llvm::consumeError(std::move(Res));
393404
return nullptr;
405+
}
394406

395407
return EventImpl.release();
396408
}
@@ -416,16 +428,16 @@ ol_impl_result_t olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
416428
if (DstDevice == HostDevice()) {
417429
auto Res = SrcDevice->Device->dataRetrieve(DstPtr, SrcPtr, Size, QueueImpl);
418430
if (Res)
419-
return {OL_ERRC_UNKNOWN, "The data retrieve operation failed"};
431+
return {std::move(Res), "The data retrieve operation failed"};
420432
} else if (SrcDevice == HostDevice()) {
421433
auto Res = DstDevice->Device->dataSubmit(DstPtr, SrcPtr, Size, QueueImpl);
422434
if (Res)
423-
return {OL_ERRC_UNKNOWN, "The data submit operation failed"};
435+
return {std::move(Res), "The data submit operation failed"};
424436
} else {
425437
auto Res = SrcDevice->Device->dataExchange(SrcPtr, *DstDevice->Device,
426438
DstPtr, Size, QueueImpl);
427439
if (Res)
428-
return {OL_ERRC_UNKNOWN, "The data exchange operation failed"};
440+
return {std::move(Res), "The data exchange operation failed"};
429441
}
430442

431443
if (EventOut)
@@ -452,6 +464,7 @@ ol_impl_result_t olCreateProgram_impl(ol_device_handle_t Device,
452464
auto Res =
453465
Device->Device->loadBinary(Device->Device->Plugin, &Prog->DeviceImage);
454466
if (!Res) {
467+
llvm::consumeError(Res.takeError());
455468
delete Prog;
456469
return OL_ERRC_INVALID_VALUE;
457470
}
@@ -477,7 +490,7 @@ ol_impl_result_t olGetKernel_impl(ol_program_handle_t Program,
477490

478491
auto Err = KernelImpl->init(Device, *Program->Image);
479492
if (Err)
480-
return {OL_ERRC_UNKNOWN, "Could not initialize the kernel"};
493+
return {std::move(Err), "Could not initialize the kernel"};
481494

482495
*Kernel = &*KernelImpl;
483496

@@ -520,7 +533,7 @@ olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,
520533

521534
AsyncInfoWrapper.finalize(Err);
522535
if (Err)
523-
return {OL_ERRC_UNKNOWN, "Could not finalize the AsyncInfoWrapper"};
536+
return {std::move(Err), "Could not finalize the AsyncInfoWrapper"};
524537

525538
if (EventOut)
526539
*EventOut = makeEvent(Queue);

offload/unittests/OffloadAPI/kernel/olGetKernel.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ TEST_F(olGetKernelTest, InvalidNullKernelPointer) {
2828
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER,
2929
olGetKernel(Program, "foo", nullptr));
3030
}
31+
32+
TEST_F(olGetKernelTest, InvalidKernelName) {
33+
GTEST_SKIP()
34+
<< "Error code returning from plugin interface not yet supported";
35+
ol_kernel_handle_t Kernel = nullptr;
36+
ASSERT_ERROR(OL_ERRC_INVALID_KERNEL_NAME,
37+
olGetKernel(Program, "invalid_kernel_name", &Kernel));
38+
}

0 commit comments

Comments
 (0)