Skip to content

[Offload] Ensure all llvm::Errors are handled #137339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions offload/liboffload/include/OffloadImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Error.h"

struct OffloadConfig {
bool TracingEnabled = false;
Expand Down Expand Up @@ -88,6 +89,19 @@ struct ol_impl_result_t {
Result = errors().emplace(std::move(Err)).first->get();
}

static ol_impl_result_t fromError(llvm::Error &&Error) {
ol_errc_t ErrCode;
llvm::StringRef Details;
llvm::handleAllErrors(std::move(Error), [&](llvm::StringError &Err) {
// TODO: PluginInterface doesn't yet have a way to communicate offload
// error codes
ErrCode = OL_ERRC_UNKNOWN;
Details = errorStrs().insert(Err.getMessage()).first->getKeyData();
});

return ol_impl_result_t{ErrCode, Details};
}

operator ol_result_t() { return Result; }

private:
Expand Down
33 changes: 18 additions & 15 deletions offload/liboffload/src/OffloadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ ol_impl_result_t olMemAlloc_impl(ol_device_handle_t Device,
auto Alloc =
Device->Device->dataAlloc(Size, nullptr, convertOlToPluginAllocTy(Type));
if (!Alloc)
return {OL_ERRC_OUT_OF_RESOURCES,
formatv("Could not create allocation on device {0}", Device).str()};
return ol_impl_result_t::fromError(Alloc.takeError());

*AllocationOut = *Alloc;
allocInfoMap().insert_or_assign(*Alloc, AllocInfo{Device, Type});
Expand All @@ -330,7 +329,7 @@ ol_impl_result_t olMemFree_impl(void *Address) {
auto Res =
Device->Device->dataDelete(Address, convertOlToPluginAllocTy(Type));
if (Res)
return {OL_ERRC_OUT_OF_RESOURCES, "Could not free allocation"};
return ol_impl_result_t::fromError(std::move(Res));

allocInfoMap().erase(Address);

Expand All @@ -342,7 +341,7 @@ ol_impl_result_t olCreateQueue_impl(ol_device_handle_t Device,
auto CreatedQueue = std::make_unique<ol_queue_impl_t>(nullptr, Device);
auto Err = Device->Device->initAsyncInfo(&(CreatedQueue->AsyncInfo));
if (Err)
return {OL_ERRC_UNKNOWN, "Could not initialize stream resource"};
return ol_impl_result_t::fromError(std::move(Err));

*Queue = CreatedQueue.release();
return OL_SUCCESS;
Expand All @@ -358,23 +357,23 @@ ol_impl_result_t olWaitQueue_impl(ol_queue_handle_t Queue) {
if (Queue->AsyncInfo->Queue) {
auto Err = Queue->Device->Device->synchronize(Queue->AsyncInfo);
if (Err)
return {OL_ERRC_INVALID_QUEUE, "The queue failed to synchronize"};
return ol_impl_result_t::fromError(std::move(Err));
}

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

return OL_SUCCESS;
}

ol_impl_result_t olWaitEvent_impl(ol_event_handle_t Event) {
auto Res = Event->Queue->Device->Device->syncEvent(Event->EventInfo);
if (Res)
return {OL_ERRC_INVALID_EVENT, "The event failed to synchronize"};
return ol_impl_result_t::fromError(std::move(Res));

return OL_SUCCESS;
}
Expand All @@ -390,13 +389,17 @@ ol_impl_result_t olDestroyEvent_impl(ol_event_handle_t Event) {
ol_event_handle_t makeEvent(ol_queue_handle_t Queue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need this to return an error, but unrelated.

auto EventImpl = std::make_unique<ol_event_impl_t>(nullptr, Queue);
auto Res = Queue->Device->Device->createEvent(&EventImpl->EventInfo);
if (Res)
if (Res) {
llvm::consumeError(std::move(Res));
return nullptr;
}

Res = Queue->Device->Device->recordEvent(EventImpl->EventInfo,
Queue->AsyncInfo);
if (Res)
if (Res) {
llvm::consumeError(std::move(Res));
return nullptr;
}

return EventImpl.release();
}
Expand All @@ -422,16 +425,16 @@ ol_impl_result_t olMemcpy_impl(ol_queue_handle_t Queue, void *DstPtr,
if (DstDevice == HostDevice()) {
auto Res = SrcDevice->Device->dataRetrieve(DstPtr, SrcPtr, Size, QueueImpl);
if (Res)
return {OL_ERRC_UNKNOWN, "The data retrieve operation failed"};
return ol_impl_result_t::fromError(std::move(Res));
} else if (SrcDevice == HostDevice()) {
auto Res = DstDevice->Device->dataSubmit(DstPtr, SrcPtr, Size, QueueImpl);
if (Res)
return {OL_ERRC_UNKNOWN, "The data submit operation failed"};
return ol_impl_result_t::fromError(std::move(Res));
} else {
auto Res = SrcDevice->Device->dataExchange(SrcPtr, *DstDevice->Device,
DstPtr, Size, QueueImpl);
if (Res)
return {OL_ERRC_UNKNOWN, "The data exchange operation failed"};
return ol_impl_result_t::fromError(std::move(Res));
}

if (EventOut)
Expand Down Expand Up @@ -459,7 +462,7 @@ ol_impl_result_t olCreateProgram_impl(ol_device_handle_t Device,
Device->Device->loadBinary(Device->Device->Plugin, &Prog->DeviceImage);
if (!Res) {
delete Prog;
return OL_ERRC_INVALID_VALUE;
return ol_impl_result_t::fromError(Res.takeError());
}

Prog->Image = *Res;
Expand All @@ -483,7 +486,7 @@ ol_impl_result_t olGetKernel_impl(ol_program_handle_t Program,

auto Err = KernelImpl->init(Device, *Program->Image);
if (Err)
return {OL_ERRC_UNKNOWN, "Could not initialize the kernel"};
return ol_impl_result_t::fromError(std::move(Err));

*Kernel = &*KernelImpl;

Expand Down Expand Up @@ -526,7 +529,7 @@ olLaunchKernel_impl(ol_queue_handle_t Queue, ol_device_handle_t Device,

AsyncInfoWrapper.finalize(Err);
if (Err)
return {OL_ERRC_UNKNOWN, "Could not finalize the AsyncInfoWrapper"};
return ol_impl_result_t::fromError(std::move(Err));

if (EventOut)
*EventOut = makeEvent(Queue);
Expand Down
7 changes: 7 additions & 0 deletions offload/unittests/OffloadAPI/kernel/olGetKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ TEST_P(olGetKernelTest, InvalidNullKernelPointer) {
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER,
olGetKernel(Program, "foo", nullptr));
}

// Error code returning from plugin interface not yet supported
TEST_F(olGetKernelTest, DISABLED_InvalidKernelName) {
ol_kernel_handle_t Kernel = nullptr;
ASSERT_ERROR(OL_ERRC_INVALID_KERNEL_NAME,
olGetKernel(Program, "invalid_kernel_name", &Kernel));
}
Loading