Skip to content

[Offload] Use llvm::Error throughout liboffload internals #140879

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 4 commits into from
May 27, 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
61 changes: 22 additions & 39 deletions offload/liboffload/include/OffloadImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,48 +69,31 @@ struct ErrPtrHash {
using ErrSetT = std::unordered_set<ErrPtrT, ErrPtrHash, ErrPtrEqual>;
ErrSetT &errors();

struct ol_impl_result_t {
ol_impl_result_t(std::nullptr_t) : Result(OL_SUCCESS) {}
ol_impl_result_t(ol_errc_t Code) {
if (Code == OL_ERRC_SUCCESS) {
Result = nullptr;
} else {
auto Err = std::unique_ptr<ol_error_struct_t>(
new ol_error_struct_t{Code, nullptr});
Result = errors().emplace(std::move(Err)).first->get();
}
}

ol_impl_result_t(ol_errc_t Code, llvm::StringRef Details) {
assert(Code != OL_ERRC_SUCCESS);
Result = nullptr;
auto DetailsStr = errorStrs().insert(Details).first->getKeyData();
auto Err = std::unique_ptr<ol_error_struct_t>(
new ol_error_struct_t{Code, DetailsStr});
Result = errors().emplace(std::move(Err)).first->get();
}
namespace {
ol_errc_t GetErrorCode(std::error_code Code) {
if (Code.category() ==
error::make_error_code(error::ErrorCode::SUCCESS).category())
return static_cast<ol_errc_t>(Code.value());

static ol_impl_result_t fromError(llvm::Error &&Error) {
ol_errc_t ErrCode;
llvm::StringRef Details;
llvm::handleAllErrors(std::move(Error), [&](llvm::StringError &Err) {
ErrCode = GetErrorCode(Err.convertToErrorCode());
Details = errorStrs().insert(Err.getMessage()).first->getKeyData();
});
return OL_ERRC_UNKNOWN;
}
} // namespace

return ol_impl_result_t{ErrCode, Details};
inline ol_result_t llvmErrorToOffloadError(llvm::Error &&Err) {
if (!Err) {
// No error
return nullptr;
}
Comment on lines +83 to 86
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (!Err) {
// No error
return nullptr;
}
if (!Err)
return nullptr;

Shouldn't we have a success type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't we have a success type?

Liboffload uses nullptr to mean success. We could change it to be a constant {OL_ERRC_SUCCESS, "success"} somewhere in memory, but I think that's out of scope for this mr and worth discussing separately.


operator ol_result_t() { return Result; }
ol_errc_t ErrCode;
llvm::StringRef Details;

private:
static ol_errc_t GetErrorCode(std::error_code Code) {
if (Code.category() ==
error::make_error_code(error::ErrorCode::SUCCESS).category()) {
return static_cast<ol_errc_t>(Code.value());
}
return OL_ERRC_UNKNOWN;
}
llvm::handleAllErrors(std::move(Err), [&](llvm::StringError &Err) {
ErrCode = GetErrorCode(Err.convertToErrorCode());
Details = errorStrs().insert(Err.getMessage()).first->getKeyData();
});

ol_result_t Result;
};
auto NewErr = std::unique_ptr<ol_error_struct_t>(
new ol_error_struct_t{ErrCode, Details.data()});
Comment on lines +96 to +97
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be make_unique intead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, since ol_error_struct_t doesn't provide a constructor for this and make_unique can't accept an initialiser list (until C++20, apparently).

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we make a constructor for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ol_error_struct_t is a C type, not a C++ type. I guess we could make a constructor and guard it with defined(__cplusplus), but that seems messy to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right, forgot about the C API part. Nah, this is fine.

return errors().emplace(std::move(NewErr)).first->get();
}
Loading
Loading