[llvm] [Offload] Use llvm::Error throughout liboffload internals (PR #140879)
Ross Brunton via llvm-commits
llvm-commits at lists.llvm.org
Tue May 27 06:15:51 PDT 2025
================
@@ -69,48 +69,29 @@ 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();
- }
+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());
}
+ return OL_ERRC_UNKNOWN;
+}
+} // namespace
- 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();
- }
+inline ol_result_t llvmErrorToOffloadError(llvm::Error &&Err) {
+ if (!Err)
+ return nullptr;
- 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_impl_result_t{ErrCode, Details};
- }
+ ol_errc_t ErrCode;
+ llvm::StringRef Details;
- operator ol_result_t() { return Result; }
+ llvm::handleAllErrors(std::move(Err), [&](llvm::StringError &Err) {
+ ErrCode = GetErrorCode(Err.convertToErrorCode());
+ Details = errorStrs().insert(Err.getMessage()).first->getKeyData();
+ });
-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;
- }
-
- ol_result_t Result;
-};
+ auto NewErr = std::unique_ptr<ol_error_struct_t>(
+ new ol_error_struct_t{ErrCode, Details.data()});
----------------
RossBrunton wrote:
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).
https://github.com/llvm/llvm-project/pull/140879
More information about the llvm-commits
mailing list