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

Conversation

RossBrunton
Copy link
Contributor

This removes the ol_impl_result_t helper class, replacing it with
llvm::Error. In addition, some internal functions that returned
ol_errc_t now return llvm::Error (with a fancy message).

@llvmbot
Copy link
Member

llvmbot commented May 21, 2025

@llvm/pr-subscribers-offload

Author: Ross Brunton (RossBrunton)

Changes

This removes the ol_impl_result_t helper class, replacing it with
llvm::Error. In addition, some internal functions that returned
ol_errc_t now return llvm::Error (with a fancy message).


Patch is 55.67 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/140879.diff

8 Files Affected:

  • (modified) offload/liboffload/include/OffloadImpl.hpp (+22-39)
  • (modified) offload/liboffload/include/generated/OffloadEntryPoints.inc (+142-99)
  • (modified) offload/liboffload/include/generated/OffloadImplFuncDecls.inc (+37-38)
  • (modified) offload/liboffload/src/Helpers.hpp (+16-12)
  • (modified) offload/liboffload/src/OffloadImpl.cpp (+100-118)
  • (modified) offload/tools/offload-tblgen/EntryPointGen.cpp (+7-4)
  • (modified) offload/tools/offload-tblgen/MiscGen.cpp (+1-1)
  • (modified) offload/tools/offload-tblgen/RecordTypes.hpp (+6)
diff --git a/offload/liboffload/include/OffloadImpl.hpp b/offload/liboffload/include/OffloadImpl.hpp
index 7d5c20bc48670..df7615a657c07 100644
--- a/offload/liboffload/include/OffloadImpl.hpp
+++ b/offload/liboffload/include/OffloadImpl.hpp
@@ -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());
   }
+  return OL_ERRC_UNKNOWN;
+}
+} // namespace
 
-  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};
+inline ol_result_t llvmErrorToOffloadError(llvm::Error &&Err) {
+  if (!Err) {
+    // No error
+    return nullptr;
   }
 
-  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()});
+  return errors().emplace(std::move(NewErr)).first->get();
+}
diff --git a/offload/liboffload/include/generated/OffloadEntryPoints.inc b/offload/liboffload/include/generated/OffloadEntryPoints.inc
index d70ebed934dce..9feebeea09ec3 100644
--- a/offload/liboffload/include/generated/OffloadEntryPoints.inc
+++ b/offload/liboffload/include/generated/OffloadEntryPoints.inc
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olInit_val() {
+llvm::Error olInit_val() {
   if (offloadConfig().ValidationEnabled) {
   }
 
@@ -18,7 +18,7 @@ OL_APIEXPORT ol_result_t OL_APICALL olInit() {
     llvm::errs() << "---> olInit";
   }
 
-  ol_result_t Result = olInit_val();
+  ol_result_t Result = llvmErrorToOffloadError(olInit_val());
 
   if (offloadConfig().TracingEnabled) {
     llvm::errs() << "()";
@@ -38,7 +38,7 @@ ol_result_t olInitWithCodeLoc(ol_code_location_t *CodeLocation) {
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olShutDown_val() {
+llvm::Error olShutDown_val() {
   if (offloadConfig().ValidationEnabled) {
   }
 
@@ -49,7 +49,7 @@ OL_APIEXPORT ol_result_t OL_APICALL olShutDown() {
     llvm::errs() << "---> olShutDown";
   }
 
-  ol_result_t Result = olShutDown_val();
+  ol_result_t Result = llvmErrorToOffloadError(olShutDown_val());
 
   if (offloadConfig().TracingEnabled) {
     llvm::errs() << "()";
@@ -69,20 +69,23 @@ ol_result_t olShutDownWithCodeLoc(ol_code_location_t *CodeLocation) {
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olGetPlatformInfo_val(ol_platform_handle_t Platform,
-                                       ol_platform_info_t PropName,
-                                       size_t PropSize, void *PropValue) {
+llvm::Error olGetPlatformInfo_val(ol_platform_handle_t Platform,
+                                  ol_platform_info_t PropName, size_t PropSize,
+                                  void *PropValue) {
   if (offloadConfig().ValidationEnabled) {
     if (PropSize == 0) {
-      return OL_ERRC_INVALID_SIZE;
+      return createOffloadError(error::ErrorCode::INVALID_SIZE,
+                                "validation failure: PropSize == 0");
     }
 
     if (NULL == Platform) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Platform");
     }
 
     if (NULL == PropValue) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == PropValue");
     }
   }
 
@@ -96,8 +99,8 @@ olGetPlatformInfo(ol_platform_handle_t Platform, ol_platform_info_t PropName,
     llvm::errs() << "---> olGetPlatformInfo";
   }
 
-  ol_result_t Result =
-      olGetPlatformInfo_val(Platform, PropName, PropSize, PropValue);
+  ol_result_t Result = llvmErrorToOffloadError(
+      olGetPlatformInfo_val(Platform, PropName, PropSize, PropValue));
 
   if (offloadConfig().TracingEnabled) {
     ol_get_platform_info_params_t Params = {&Platform, &PropName, &PropSize,
@@ -123,16 +126,18 @@ ol_result_t olGetPlatformInfoWithCodeLoc(ol_platform_handle_t Platform,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olGetPlatformInfoSize_val(ol_platform_handle_t Platform,
-                                           ol_platform_info_t PropName,
-                                           size_t *PropSizeRet) {
+llvm::Error olGetPlatformInfoSize_val(ol_platform_handle_t Platform,
+                                      ol_platform_info_t PropName,
+                                      size_t *PropSizeRet) {
   if (offloadConfig().ValidationEnabled) {
     if (NULL == Platform) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Platform");
     }
 
     if (NULL == PropSizeRet) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == PropSizeRet");
     }
   }
 
@@ -146,8 +151,8 @@ olGetPlatformInfoSize(ol_platform_handle_t Platform,
     llvm::errs() << "---> olGetPlatformInfoSize";
   }
 
-  ol_result_t Result =
-      olGetPlatformInfoSize_val(Platform, PropName, PropSizeRet);
+  ol_result_t Result = llvmErrorToOffloadError(
+      olGetPlatformInfoSize_val(Platform, PropName, PropSizeRet));
 
   if (offloadConfig().TracingEnabled) {
     ol_get_platform_info_size_params_t Params = {&Platform, &PropName,
@@ -172,8 +177,8 @@ ol_result_t olGetPlatformInfoSizeWithCodeLoc(ol_platform_handle_t Platform,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olIterateDevices_val(ol_device_iterate_cb_t Callback,
-                                      void *UserData) {
+llvm::Error olIterateDevices_val(ol_device_iterate_cb_t Callback,
+                                 void *UserData) {
   if (offloadConfig().ValidationEnabled) {
   }
 
@@ -185,7 +190,8 @@ olIterateDevices(ol_device_iterate_cb_t Callback, void *UserData) {
     llvm::errs() << "---> olIterateDevices";
   }
 
-  ol_result_t Result = olIterateDevices_val(Callback, UserData);
+  ol_result_t Result =
+      llvmErrorToOffloadError(olIterateDevices_val(Callback, UserData));
 
   if (offloadConfig().TracingEnabled) {
     ol_iterate_devices_params_t Params = {&Callback, &UserData};
@@ -208,20 +214,23 @@ ol_result_t olIterateDevicesWithCodeLoc(ol_device_iterate_cb_t Callback,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olGetDeviceInfo_val(ol_device_handle_t Device,
-                                     ol_device_info_t PropName, size_t PropSize,
-                                     void *PropValue) {
+llvm::Error olGetDeviceInfo_val(ol_device_handle_t Device,
+                                ol_device_info_t PropName, size_t PropSize,
+                                void *PropValue) {
   if (offloadConfig().ValidationEnabled) {
     if (PropSize == 0) {
-      return OL_ERRC_INVALID_SIZE;
+      return createOffloadError(error::ErrorCode::INVALID_SIZE,
+                                "validation failure: PropSize == 0");
     }
 
     if (NULL == Device) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Device");
     }
 
     if (NULL == PropValue) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == PropValue");
     }
   }
 
@@ -236,8 +245,8 @@ OL_APIEXPORT ol_result_t OL_APICALL olGetDeviceInfo(ol_device_handle_t Device,
     llvm::errs() << "---> olGetDeviceInfo";
   }
 
-  ol_result_t Result =
-      olGetDeviceInfo_val(Device, PropName, PropSize, PropValue);
+  ol_result_t Result = llvmErrorToOffloadError(
+      olGetDeviceInfo_val(Device, PropName, PropSize, PropValue));
 
   if (offloadConfig().TracingEnabled) {
     ol_get_device_info_params_t Params = {&Device, &PropName, &PropSize,
@@ -262,16 +271,18 @@ ol_result_t olGetDeviceInfoWithCodeLoc(ol_device_handle_t Device,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olGetDeviceInfoSize_val(ol_device_handle_t Device,
-                                         ol_device_info_t PropName,
-                                         size_t *PropSizeRet) {
+llvm::Error olGetDeviceInfoSize_val(ol_device_handle_t Device,
+                                    ol_device_info_t PropName,
+                                    size_t *PropSizeRet) {
   if (offloadConfig().ValidationEnabled) {
     if (NULL == Device) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Device");
     }
 
     if (NULL == PropSizeRet) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == PropSizeRet");
     }
   }
 
@@ -283,7 +294,8 @@ OL_APIEXPORT ol_result_t OL_APICALL olGetDeviceInfoSize(
     llvm::errs() << "---> olGetDeviceInfoSize";
   }
 
-  ol_result_t Result = olGetDeviceInfoSize_val(Device, PropName, PropSizeRet);
+  ol_result_t Result = llvmErrorToOffloadError(
+      olGetDeviceInfoSize_val(Device, PropName, PropSizeRet));
 
   if (offloadConfig().TracingEnabled) {
     ol_get_device_info_size_params_t Params = {&Device, &PropName,
@@ -308,19 +320,22 @@ ol_result_t olGetDeviceInfoSizeWithCodeLoc(ol_device_handle_t Device,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olMemAlloc_val(ol_device_handle_t Device, ol_alloc_type_t Type,
-                                size_t Size, void **AllocationOut) {
+llvm::Error olMemAlloc_val(ol_device_handle_t Device, ol_alloc_type_t Type,
+                           size_t Size, void **AllocationOut) {
   if (offloadConfig().ValidationEnabled) {
     if (Size == 0) {
-      return OL_ERRC_INVALID_SIZE;
+      return createOffloadError(error::ErrorCode::INVALID_SIZE,
+                                "validation failure: Size == 0");
     }
 
     if (NULL == Device) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Device");
     }
 
     if (NULL == AllocationOut) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == AllocationOut");
     }
   }
 
@@ -334,7 +349,8 @@ OL_APIEXPORT ol_result_t OL_APICALL olMemAlloc(ol_device_handle_t Device,
     llvm::errs() << "---> olMemAlloc";
   }
 
-  ol_result_t Result = olMemAlloc_val(Device, Type, Size, AllocationOut);
+  ol_result_t Result = llvmErrorToOffloadError(
+      olMemAlloc_val(Device, Type, Size, AllocationOut));
 
   if (offloadConfig().TracingEnabled) {
     ol_mem_alloc_params_t Params = {&Device, &Type, &Size, &AllocationOut};
@@ -358,10 +374,11 @@ ol_result_t olMemAllocWithCodeLoc(ol_device_handle_t Device,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olMemFree_val(void *Address) {
+llvm::Error olMemFree_val(void *Address) {
   if (offloadConfig().ValidationEnabled) {
     if (NULL == Address) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == Address");
     }
   }
 
@@ -372,7 +389,7 @@ OL_APIEXPORT ol_result_t OL_APICALL olMemFree(void *Address) {
     llvm::errs() << "---> olMemFree";
   }
 
-  ol_result_t Result = olMemFree_val(Address);
+  ol_result_t Result = llvmErrorToOffloadError(olMemFree_val(Address));
 
   if (offloadConfig().TracingEnabled) {
     ol_mem_free_params_t Params = {&Address};
@@ -394,29 +411,35 @@ ol_result_t olMemFreeWithCodeLoc(void *Address,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olMemcpy_val(ol_queue_handle_t Queue, void *DstPtr,
-                              ol_device_handle_t DstDevice, void *SrcPtr,
-                              ol_device_handle_t SrcDevice, size_t Size,
-                              ol_event_handle_t *EventOut) {
+llvm::Error olMemcpy_val(ol_queue_handle_t Queue, void *DstPtr,
+                         ol_device_handle_t DstDevice, void *SrcPtr,
+                         ol_device_handle_t SrcDevice, size_t Size,
+                         ol_event_handle_t *EventOut) {
   if (offloadConfig().ValidationEnabled) {
     if (Queue == NULL && EventOut != NULL) {
-      return OL_ERRC_INVALID_ARGUMENT;
+      return createOffloadError(
+          error::ErrorCode::INVALID_ARGUMENT,
+          "validation failure: Queue == NULL && EventOut != NULL");
     }
 
     if (NULL == DstDevice) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == DstDevice");
     }
 
     if (NULL == SrcDevice) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == SrcDevice");
     }
 
     if (NULL == DstPtr) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == DstPtr");
     }
 
     if (NULL == SrcPtr) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == SrcPtr");
     }
   }
 
@@ -431,8 +454,8 @@ olMemcpy(ol_queue_handle_t Queue, void *DstPtr, ol_device_handle_t DstDevice,
     llvm::errs() << "---> olMemcpy";
   }
 
-  ol_result_t Result =
-      olMemcpy_val(Queue, DstPtr, DstDevice, SrcPtr, SrcDevice, Size, EventOut);
+  ol_result_t Result = llvmErrorToOffloadError(olMemcpy_val(
+      Queue, DstPtr, DstDevice, SrcPtr, SrcDevice, Size, EventOut));
 
   if (offloadConfig().TracingEnabled) {
     ol_memcpy_params_t Params = {&Queue,     &DstPtr, &DstDevice, &SrcPtr,
@@ -459,15 +482,17 @@ ol_result_t olMemcpyWithCodeLoc(ol_queue_handle_t Queue, void *DstPtr,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olCreateQueue_val(ol_device_handle_t Device,
-                                   ol_queue_handle_t *Queue) {
+llvm::Error olCreateQueue_val(ol_device_handle_t Device,
+                              ol_queue_handle_t *Queue) {
   if (offloadConfig().ValidationEnabled) {
     if (NULL == Device) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Device");
     }
 
     if (NULL == Queue) {
-      return OL_ERRC_INVALID_NULL_POINTER;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
+                                "validation failure: NULL == Queue");
     }
   }
 
@@ -479,7 +504,8 @@ OL_APIEXPORT ol_result_t OL_APICALL olCreateQueue(ol_device_handle_t Device,
     llvm::errs() << "---> olCreateQueue";
   }
 
-  ol_result_t Result = olCreateQueue_val(Device, Queue);
+  ol_result_t Result =
+      llvmErrorToOffloadError(olCreateQueue_val(Device, Queue));
 
   if (offloadConfig().TracingEnabled) {
     ol_create_queue_params_t Params = {&Device, &Queue};
@@ -502,10 +528,11 @@ ol_result_t olCreateQueueWithCodeLoc(ol_device_handle_t Device,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olDestroyQueue_val(ol_queue_handle_t Queue) {
+llvm::Error olDestroyQueue_val(ol_queue_handle_t Queue) {
   if (offloadConfig().ValidationEnabled) {
     if (NULL == Queue) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Queue");
     }
   }
 
@@ -516,7 +543,7 @@ OL_APIEXPORT ol_result_t OL_APICALL olDestroyQueue(ol_queue_handle_t Queue) {
     llvm::errs() << "---> olDestroyQueue";
   }
 
-  ol_result_t Result = olDestroyQueue_val(Queue);
+  ol_result_t Result = llvmErrorToOffloadError(olDestroyQueue_val(Queue));
 
   if (offloadConfig().TracingEnabled) {
     ol_destroy_queue_params_t Params = {&Queue};
@@ -538,10 +565,11 @@ ol_result_t olDestroyQueueWithCodeLoc(ol_queue_handle_t Queue,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olWaitQueue_val(ol_queue_handle_t Queue) {
+llvm::Error olWaitQueue_val(ol_queue_handle_t Queue) {
   if (offloadConfig().ValidationEnabled) {
     if (NULL == Queue) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Queue");
     }
   }
 
@@ -552,7 +580,7 @@ OL_APIEXPORT ol_result_t OL_APICALL olWaitQueue(ol_queue_handle_t Queue) {
     llvm::errs() << "---> olWaitQueue";
   }
 
-  ol_result_t Result = olWaitQueue_val(Queue);
+  ol_result_t Result = llvmErrorToOffloadError(olWaitQueue_val(Queue));
 
   if (offloadConfig().TracingEnabled) {
     ol_wait_queue_params_t Params = {&Queue};
@@ -574,10 +602,11 @@ ol_result_t olWaitQueueWithCodeLoc(ol_queue_handle_t Queue,
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-ol_impl_result_t olDestroyEvent_val(ol_event_handle_t Event) {
+llvm::Error olDestroyEvent_val(ol_event_handle_t Event) {
   if (offloadConfig().ValidationEnabled) {
     if (NULL == Event) {
-      return OL_ERRC_INVALID_NULL_HANDLE;
+      return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE,
+                                "validation failure: NULL == Event");
     }
   }
 
@@ -588,7 +617,7 @@ OL_APIEXPORT ol_result_t OL_APICALL olDestroyEvent(ol_event_handle_t Event) {
     llvm::errs() << "---> olDestroyEvent";
   }
 
-  ol_result_t Result = olDestroyEvent_val(Event);
+  ol_result_t Result = l...
[truncated]

Copy link

github-actions bot commented May 21, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

if (!ParamValue && !ParamValueSizeRet) {
return OL_ERRC_INVALID_NULL_POINTER;
return error::createOffloadError(error::ErrorCode::INVALID_NULL_POINTER,
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 have an overload of createOffloadError that takes a ol_errc_t? That way we could use OL_ERRC_INVALID_NULL_POINTER here instead of the longer error::ErrorCode::INVALID_NULL_POINTER. The same is true of the autogenerated entry point code. It's purely subjective but that would seem tidier to me.

Copy link
Contributor Author

@RossBrunton RossBrunton May 21, 2025

Choose a reason for hiding this comment

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

There are already three overloads of createOffloadError, so this would double it to six, which feels like a lot (unless we do some fancy template shenanigans). I also think that it makes sense for C++ code to only use the C++ enums.

Of course, my views are subjective as well, and I'll add it if I'm outvoted.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair, I'm ok with leaving it then, especially if we consistently use the C++ enum

Comment on lines +83 to 86
if (!Err) {
// No error
return nullptr;
}
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.

@RossBrunton
Copy link
Contributor Author

@jhuber6 @callumfare Any changes you want me to make with this?

Comment on lines +94 to +97
auto NewErr = std::unique_ptr<ol_error_struct_t>(
new ol_error_struct_t{ErrCode, Details.data()});
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.

This removes the `ol_impl_result_t` helper class, replacing it with
`llvm::Error`. In addition, some internal functions that returned
`ol_errc_t` now return `llvm::Error` (with a fancy message).
@jhuber6 jhuber6 merged commit 7e9d708 into llvm:main May 27, 2025
9 checks passed
@mgorny
Copy link
Member

mgorny commented May 28, 2025

This change seems to have introduced test failures for me:

FAIL: libomptarget :: x86_64-unknown-linux-gnu :: tools/offload-tblgen/default_returns.td (314 of 716)
******************** TEST 'libomptarget :: x86_64-unknown-linux-gnu :: tools/offload-tblgen/default_returns.td' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload_build/offload-tblgen -gen-api -I /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/../../../liboffload/API /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td | /usr/lib/llvm/21/bin/FileCheck /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td --check-prefix=CHECK-API
# executed command: /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload_build/offload-tblgen -gen-api -I /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/../../../liboffload/API /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td
# executed command: /usr/lib/llvm/21/bin/FileCheck /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td --check-prefix=CHECK-API
# RUN: at line 2
/var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload_build/offload-tblgen -gen-entry-points -I /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/../../../liboffload/API /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td | /usr/lib/llvm/21/bin/FileCheck /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td --check-prefix=CHECK-VALIDATION
# executed command: /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload_build/offload-tblgen -gen-entry-points -I /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/../../../liboffload/API /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td
# executed command: /usr/lib/llvm/21/bin/FileCheck /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td --check-prefix=CHECK-VALIDATION
# .---command stderr------------
# | /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td:37:27: error: CHECK-VALIDATION-NEXT: expected string not found in input
# | // CHECK-VALIDATION-NEXT: return OL_ERRC_INVALID_NULL_HANDLE;
# |                           ^
# | <stdin>:13:26: note: scanning from here
# |  if (NULL == ParamHandle) {
# |                          ^
# | <stdin>:14:31: note: possible intended match here
# |  return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE, "validation failure: NULL == ParamHandle");
# |                               ^
# | 
# | Input file: <stdin>
# | Check file: /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/default_returns.td
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |            .
# |            .
# |            .
# |            8:  
# |            9: /////////////////////////////////////////////////////////////////////////////// 
# |           10: llvm::Error FunctionA_val( 
# |           11:  uint32_t ParamValue, ol_foo_handle_t ParamHandle, uint32_t* ParamPointer, uint32_t* ParamPointerOpt) { 
# |           12:  if (offloadConfig().ValidationEnabled) { 
# |           13:  if (NULL == ParamHandle) { 
# | next:37'0                              X~~ error: no match found
# |           14:  return createOffloadError(error::ErrorCode::INVALID_NULL_HANDLE, "validation failure: NULL == ParamHandle"); 
# | next:37'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | next:37'1                                   ?                                                                                possible intended match
# |           15:  } 
# | next:37'0     ~~~
# |           16:  
# | next:37'0     ~
# |           17:  if (NULL == ParamPointer) { 
# | next:37'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |           18:  return createOffloadError(error::ErrorCode::INVALID_NULL_POINTER, "validation failure: NULL == ParamPointer"); 
# | next:37'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |           19:  } 
# | next:37'0     ~~~
# |            .
# |            .
# |            .
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

********************
FAIL: libomptarget :: x86_64-unknown-linux-gnu :: tools/offload-tblgen/entry_points.td (315 of 716)
******************** TEST 'libomptarget :: x86_64-unknown-linux-gnu :: tools/offload-tblgen/entry_points.td' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload_build/offload-tblgen -gen-entry-points -I /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/../../../liboffload/API /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/entry_points.td | /usr/lib/llvm/21/bin/FileCheck /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/entry_points.td
# executed command: /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload_build/offload-tblgen -gen-entry-points -I /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/../../../liboffload/API /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/entry_points.td
# executed command: /usr/lib/llvm/21/bin/FileCheck /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/entry_points.td
# .---command stderr------------
# | /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/entry_points.td:31:11: error: CHECK: expected string not found in input
# | // CHECK: Result = FunctionA_val(ParamA, ParamB);
# |           ^
# | <stdin>:25:35: note: scanning from here
# |  llvm::errs() << "---> FunctionA";
# |                                   ^
# | <stdin>:43:14: note: possible intended match here
# |  ol_result_t Result = ::FunctionA(ParamA, ParamB);
# |              ^
# | 
# | Input file: <stdin>
# | Check file: /var/tmp/portage/llvm-runtimes/offload-21.0.0.9999/work/offload/test/tools/offload-tblgen/entry_points.td
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |             .
# |             .
# |             .
# |            20:  
# |            21: } 
# |            22: OL_APIEXPORT ol_result_t OL_APICALL FunctionA( 
# |            23:  uint32_t ParamA, uint32_t* ParamB) { 
# |            24:  if (offloadConfig().TracingEnabled) { 
# |            25:  llvm::errs() << "---> FunctionA"; 
# | check:31'0                                       X error: no match found
# |            26:  } 
# | check:31'0     ~~~
# |            27:  
# | check:31'0     ~
# |            28:  ol_result_t Result = llvmErrorToOffloadError(FunctionA_val(ParamA, ParamB)); 
# | check:31'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            29:  
# | check:31'0     ~
# |            30:  if (offloadConfig().TracingEnabled) { 
# | check:31'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             .
# |             .
# |             .
# |            38:  return Result; 
# | check:31'0     ~~~~~~~~~~~~~~~~
# |            39: } 
# | check:31'0     ~~
# |            40: ol_result_t FunctionAWithCodeLoc( 
# | check:31'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            41:  uint32_t ParamA, uint32_t* ParamB, ol_code_location_t *CodeLocation) { 
# | check:31'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            42:  currentCodeLocation() = CodeLocation; 
# | check:31'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            43:  ol_result_t Result = ::FunctionA(ParamA, ParamB); 
# | check:31'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | check:31'1                  ?                                      possible intended match
# |            44:  
# | check:31'0     ~
# |            45:  currentCodeLocation() = nullptr; 
# | check:31'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            46:  return Result; 
# | check:31'0     ~~~~~~~~~~~~~~~~
# |            47: } 
# | check:31'0     ~~
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

********************

(and same in the "LTO" test variants)

RossBrunton added a commit to RossBrunton/llvm-project that referenced this pull request May 28, 2025
@RossBrunton
Copy link
Contributor Author

@mgorny Whoops, didn't realise those tests existed. Should be fixed by #141796

sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
This removes the `ol_impl_result_t` helper class, replacing it with
`llvm::Error`. In addition, some internal functions that returned
`ol_errc_t` now return `llvm::Error` (with a fancy message).
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants