Skip to content

[Offload] Use new error code handling mechanism and lower-case messages #139275

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 20, 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
48 changes: 31 additions & 17 deletions offload/include/Shared/OffloadErrcodes.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,38 @@
// To add new error codes, add them to offload/liboffload/API/Common.td and run
// the GenerateOffload target.

OFFLOAD_ERRC(SUCCESS, "Success", 0)
OFFLOAD_ERRC(UNKNOWN, "Unknown or internal error", 1)
OFFLOAD_ERRC(SUCCESS, "success", 0)
OFFLOAD_ERRC(UNKNOWN, "unknown or internal error", 1)
OFFLOAD_ERRC(HOST_IO, "I/O error on host", 2)
OFFLOAD_ERRC(INVALID_BINARY, "a provided binary image is malformed", 3)
OFFLOAD_ERRC(INVALID_NULL_POINTER,
"A pointer argument is null when it should not be", 2)
OFFLOAD_ERRC(INVALID_ARGUMENT, "An argument is invalid", 3)
OFFLOAD_ERRC(OUT_OF_RESOURCES, "Out of resources", 4)
OFFLOAD_ERRC(UNSUPPORTED,
"generic error code for unsupported features and enums", 5)
"a pointer argument is null when it should not be", 4)
OFFLOAD_ERRC(INVALID_ARGUMENT, "an argument is invalid", 5)
OFFLOAD_ERRC(NOT_FOUND, "requested object was not found in the binary image", 6)
OFFLOAD_ERRC(OUT_OF_RESOURCES, "out of resources", 7)
OFFLOAD_ERRC(
INVALID_SIZE,
"invalid size or dimensions (e.g., must not be zero, or is out of bounds)",
6)
OFFLOAD_ERRC(INVALID_ENUMERATION, "enumerator argument is not valid", 7)
OFFLOAD_ERRC(INVALID_KERNEL_NAME,
"Named kernel not found in the program binary", 8)
OFFLOAD_ERRC(INVALID_VALUE, "Invalid Value", 9)
OFFLOAD_ERRC(INVALID_PLATFORM, "Invalid platform", 10)
OFFLOAD_ERRC(INVALID_DEVICE, "Invalid device", 11)
OFFLOAD_ERRC(INVALID_QUEUE, "Invalid queue", 12)
OFFLOAD_ERRC(INVALID_EVENT, "Invalid event", 13)
OFFLOAD_ERRC(INVALID_NULL_HANDLE, "handle argument is not valid", 14)
8)
OFFLOAD_ERRC(INVALID_ENUMERATION, "enumerator argument is not valid", 9)
OFFLOAD_ERRC(HOST_TOOL_NOT_FOUND,
"a required binary (linker, etc.) was not found on the host", 10)
OFFLOAD_ERRC(INVALID_VALUE, "invalid value", 11)
OFFLOAD_ERRC(UNIMPLEMENTED,
"generic error code for features currently unimplemented by the "
"device/backend",
12)
OFFLOAD_ERRC(
UNSUPPORTED,
"generic error code for features unsupported by the device/backend", 13)
OFFLOAD_ERRC(ASSEMBLE_FAILURE,
"assembler failure while processing binary image", 14)
OFFLOAD_ERRC(LINK_FAILURE, "linker failure while processing binary image", 15)
OFFLOAD_ERRC(BACKEND_FAILURE,
"the plugin backend is in an invalid or unsupported state", 16)
OFFLOAD_ERRC(INVALID_NULL_HANDLE,
"a handle argument is null when it should not be", 17)
OFFLOAD_ERRC(INVALID_PLATFORM, "invalid platform", 18)
OFFLOAD_ERRC(INVALID_DEVICE, "invalid device", 19)
OFFLOAD_ERRC(INVALID_QUEUE, "invalid queue", 20)
OFFLOAD_ERRC(INVALID_EVENT, "invalid event", 21)
36 changes: 36 additions & 0 deletions offload/include/Shared/OffloadError.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@ class OffloadError : public llvm::ErrorInfo<OffloadError, llvm::StringError> {
// The definition for this resides in the plugin static library
static char ID;
};

/// Create an Offload error.
template <typename... ArgsTy>
static llvm::Error createOffloadError(error::ErrorCode Code, const char *ErrFmt,
ArgsTy... Args) {
std::string Buffer;
llvm::raw_string_ostream(Buffer) << llvm::format(ErrFmt, Args...);
return llvm::make_error<error::OffloadError>(Code, Buffer);
}

inline llvm::Error createOffloadError(error::ErrorCode Code, const char *S) {
return llvm::make_error<error::OffloadError>(Code, S);
}

// The OffloadError will have a message of either:
// * "{Context}: {Message}" if the other error is a StringError
// * "{Context}" otherwise
inline llvm::Error createOffloadError(error::ErrorCode Code,
llvm::Error &&OtherError,
const char *Context) {
std::string Buffer{Context};
llvm::raw_string_ostream buffer(Buffer);

handleAllErrors(
std::move(OtherError),
[&](llvm::StringError &Err) {
buffer << ": ";
buffer << Err.getMessage();
},
[&](llvm::ErrorInfoBase &Err) {
// Non-string error message don't add anything to the offload error's
// error message
});

return llvm::make_error<error::OffloadError>(Code, Buffer);
}
} // namespace error

#endif
33 changes: 20 additions & 13 deletions offload/liboffload/API/Common.td
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,32 @@ def ErrorCode : Enum {
let name = "ol_errc_t";
let desc = "Defines Return/Error codes";
let etors =[
Etor<"SUCCESS", "Success">,
Etor<"SUCCESS", "success">,

// Universal errors
Etor<"UNKNOWN", "Unknown or internal error">,
Etor<"INVALID_NULL_POINTER", "A pointer argument is null when it should not be">,
Etor<"INVALID_ARGUMENT", "An argument is invalid">,
Etor<"OUT_OF_RESOURCES", "Out of resources">,
Etor<"UNSUPPORTED", "generic error code for unsupported features and enums">,
Etor<"UNKNOWN", "unknown or internal error">,
Etor<"HOST_IO", "I/O error on host">,
Etor<"INVALID_BINARY", "a provided binary image is malformed">,
Etor<"INVALID_NULL_POINTER", "a pointer argument is null when it should not be">,
Etor<"INVALID_ARGUMENT", "an argument is invalid">,
Etor<"NOT_FOUND", "requested object was not found in the binary image">,
Etor<"OUT_OF_RESOURCES", "out of resources">,
Etor<"INVALID_SIZE", "invalid size or dimensions (e.g., must not be zero, or is out of bounds)">,
Etor<"INVALID_ENUMERATION", "enumerator argument is not valid">,
Etor<"INVALID_KERNEL_NAME", "Named kernel not found in the program binary">,
Etor<"HOST_TOOL_NOT_FOUND", "a required binary (linker, etc.) was not found on the host">,
Etor<"INVALID_VALUE", "invalid value">,
Etor<"UNIMPLEMENTED", "generic error code for features currently unimplemented by the device/backend">,
Etor<"UNSUPPORTED", "generic error code for features unsupported by the device/backend">,
Etor<"ASSEMBLE_FAILURE", "assembler failure while processing binary image">,
Etor<"LINK_FAILURE", "linker failure while processing binary image">,
Etor<"BACKEND_FAILURE", "the plugin backend is in an invalid or unsupported state">,

// Handle related errors - only makes sense for liboffload
Etor<"INVALID_VALUE", "Invalid Value">,
Etor<"INVALID_PLATFORM", "Invalid platform">,
Etor<"INVALID_DEVICE", "Invalid device">,
Etor<"INVALID_QUEUE", "Invalid queue">,
Etor<"INVALID_EVENT", "Invalid event">,
Etor<"INVALID_NULL_HANDLE", "handle argument is not valid">
Etor<"INVALID_NULL_HANDLE", "a handle argument is null when it should not be">,
Etor<"INVALID_PLATFORM", "invalid platform">,
Etor<"INVALID_DEVICE", "invalid device">,
Etor<"INVALID_QUEUE", "invalid queue">,
Etor<"INVALID_EVENT", "invalid event">,
];
}

Expand Down
67 changes: 41 additions & 26 deletions offload/liboffload/include/generated/OffloadAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,51 @@ extern "C" {
///////////////////////////////////////////////////////////////////////////////
/// @brief Defines Return/Error codes
typedef enum ol_errc_t {
/// Success
/// success
OL_ERRC_SUCCESS = 0,
/// Unknown or internal error
/// unknown or internal error
OL_ERRC_UNKNOWN = 1,
/// A pointer argument is null when it should not be
OL_ERRC_INVALID_NULL_POINTER = 2,
/// An argument is invalid
OL_ERRC_INVALID_ARGUMENT = 3,
/// Out of resources
OL_ERRC_OUT_OF_RESOURCES = 4,
/// generic error code for unsupported features and enums
OL_ERRC_UNSUPPORTED = 5,
/// I/O error on host
OL_ERRC_HOST_IO = 2,
/// a provided binary image is malformed
OL_ERRC_INVALID_BINARY = 3,
/// a pointer argument is null when it should not be
OL_ERRC_INVALID_NULL_POINTER = 4,
/// an argument is invalid
OL_ERRC_INVALID_ARGUMENT = 5,
/// requested object was not found in the binary image
OL_ERRC_NOT_FOUND = 6,
/// out of resources
OL_ERRC_OUT_OF_RESOURCES = 7,
/// invalid size or dimensions (e.g., must not be zero, or is out of bounds)
OL_ERRC_INVALID_SIZE = 6,
OL_ERRC_INVALID_SIZE = 8,
/// enumerator argument is not valid
OL_ERRC_INVALID_ENUMERATION = 7,
/// Named kernel not found in the program binary
OL_ERRC_INVALID_KERNEL_NAME = 8,
/// Invalid Value
OL_ERRC_INVALID_VALUE = 9,
/// Invalid platform
OL_ERRC_INVALID_PLATFORM = 10,
/// Invalid device
OL_ERRC_INVALID_DEVICE = 11,
/// Invalid queue
OL_ERRC_INVALID_QUEUE = 12,
/// Invalid event
OL_ERRC_INVALID_EVENT = 13,
/// handle argument is not valid
OL_ERRC_INVALID_NULL_HANDLE = 14,
OL_ERRC_INVALID_ENUMERATION = 9,
/// a required binary (linker, etc.) was not found on the host
OL_ERRC_HOST_TOOL_NOT_FOUND = 10,
/// invalid value
OL_ERRC_INVALID_VALUE = 11,
/// generic error code for features currently unimplemented by the
/// device/backend
OL_ERRC_UNIMPLEMENTED = 12,
/// generic error code for features unsupported by the device/backend
OL_ERRC_UNSUPPORTED = 13,
/// assembler failure while processing binary image
OL_ERRC_ASSEMBLE_FAILURE = 14,
/// linker failure while processing binary image
OL_ERRC_LINK_FAILURE = 15,
/// the plugin backend is in an invalid or unsupported state
OL_ERRC_BACKEND_FAILURE = 16,
/// a handle argument is null when it should not be
OL_ERRC_INVALID_NULL_HANDLE = 17,
/// invalid platform
OL_ERRC_INVALID_PLATFORM = 18,
/// invalid device
OL_ERRC_INVALID_DEVICE = 19,
/// invalid queue
OL_ERRC_INVALID_QUEUE = 20,
/// invalid event
OL_ERRC_INVALID_EVENT = 21,
/// @cond
OL_ERRC_FORCE_UINT32 = 0x7fffffff
/// @endcond
Expand Down
37 changes: 29 additions & 8 deletions offload/liboffload/include/generated/OffloadPrint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,54 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
case OL_ERRC_UNKNOWN:
os << "OL_ERRC_UNKNOWN";
break;
case OL_ERRC_HOST_IO:
os << "OL_ERRC_HOST_IO";
break;
case OL_ERRC_INVALID_BINARY:
os << "OL_ERRC_INVALID_BINARY";
break;
case OL_ERRC_INVALID_NULL_POINTER:
os << "OL_ERRC_INVALID_NULL_POINTER";
break;
case OL_ERRC_INVALID_ARGUMENT:
os << "OL_ERRC_INVALID_ARGUMENT";
break;
case OL_ERRC_NOT_FOUND:
os << "OL_ERRC_NOT_FOUND";
break;
case OL_ERRC_OUT_OF_RESOURCES:
os << "OL_ERRC_OUT_OF_RESOURCES";
break;
case OL_ERRC_UNSUPPORTED:
os << "OL_ERRC_UNSUPPORTED";
break;
case OL_ERRC_INVALID_SIZE:
os << "OL_ERRC_INVALID_SIZE";
break;
case OL_ERRC_INVALID_ENUMERATION:
os << "OL_ERRC_INVALID_ENUMERATION";
break;
case OL_ERRC_INVALID_KERNEL_NAME:
os << "OL_ERRC_INVALID_KERNEL_NAME";
case OL_ERRC_HOST_TOOL_NOT_FOUND:
os << "OL_ERRC_HOST_TOOL_NOT_FOUND";
break;
case OL_ERRC_INVALID_VALUE:
os << "OL_ERRC_INVALID_VALUE";
break;
case OL_ERRC_UNIMPLEMENTED:
os << "OL_ERRC_UNIMPLEMENTED";
break;
case OL_ERRC_UNSUPPORTED:
os << "OL_ERRC_UNSUPPORTED";
break;
case OL_ERRC_ASSEMBLE_FAILURE:
os << "OL_ERRC_ASSEMBLE_FAILURE";
break;
case OL_ERRC_LINK_FAILURE:
os << "OL_ERRC_LINK_FAILURE";
break;
case OL_ERRC_BACKEND_FAILURE:
os << "OL_ERRC_BACKEND_FAILURE";
break;
case OL_ERRC_INVALID_NULL_HANDLE:
os << "OL_ERRC_INVALID_NULL_HANDLE";
break;
case OL_ERRC_INVALID_PLATFORM:
os << "OL_ERRC_INVALID_PLATFORM";
break;
Expand All @@ -88,9 +112,6 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
case OL_ERRC_INVALID_EVENT:
os << "OL_ERRC_INVALID_EVENT";
break;
case OL_ERRC_INVALID_NULL_HANDLE:
os << "OL_ERRC_INVALID_NULL_HANDLE";
break;
default:
os << "unknown enumerator";
break;
Expand Down
2 changes: 1 addition & 1 deletion offload/liboffload/src/OffloadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ ol_impl_result_t olGetKernel_impl(ol_program_handle_t Program,
auto &Device = Program->Image->getDevice();
auto KernelImpl = Device.constructKernel(KernelName);
if (!KernelImpl)
return OL_ERRC_INVALID_KERNEL_NAME;
return ol_impl_result_t::fromError(KernelImpl.takeError());

auto Err = KernelImpl->init(Device, *Program->Image);
if (Err)
Expand Down
12 changes: 6 additions & 6 deletions offload/libomptarget/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,9 @@ Expected<DeviceTy &> PluginManager::getDevice(uint32_t DeviceNo) {
{
auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor();
if (DeviceNo >= ExclusiveDevicesAccessor->size())
return createStringError(
inconvertibleErrorCode(),
"Device number '%i' out of range, only %i devices available",
return error::createOffloadError(
error::ErrorCode::INVALID_VALUE,
"device number '%i' out of range, only %i devices available",
DeviceNo, ExclusiveDevicesAccessor->size());

DevicePtr = &*(*ExclusiveDevicesAccessor)[DeviceNo];
Expand All @@ -549,8 +549,8 @@ Expected<DeviceTy &> PluginManager::getDevice(uint32_t DeviceNo) {
// Check whether global data has been mapped for this device
if (DevicePtr->hasPendingImages())
if (loadImagesOntoDevice(*DevicePtr) != OFFLOAD_SUCCESS)
return createStringError(inconvertibleErrorCode(),
"Failed to load images on device '%i'",
DeviceNo);
return error::createOffloadError(error::ErrorCode::BACKEND_FAILURE,
"failed to load images on device '%i'",
DeviceNo);
return *DevicePtr;
}
10 changes: 5 additions & 5 deletions offload/libomptarget/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ DeviceTy::~DeviceTy() {
llvm::Error DeviceTy::init() {
int32_t Ret = RTL->init_device(RTLDeviceID);
if (Ret != OFFLOAD_SUCCESS)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Failed to initialize device %d\n",
DeviceID);
return error::createOffloadError(error::ErrorCode::BACKEND_FAILURE,
"failed to initialize device %d\n",
DeviceID);

// Enables recording kernels if set.
BoolEnvar OMPX_RecordKernel("LIBOMPTARGET_RECORD", false);
Expand All @@ -103,8 +103,8 @@ DeviceTy::loadBinary(__tgt_device_image *Img) {
__tgt_device_binary Binary;

if (RTL->load_binary(RTLDeviceID, Img, &Binary) != OFFLOAD_SUCCESS)
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Failed to load binary %p", Img);
return error::createOffloadError(error::ErrorCode::INVALID_BINARY,
"failed to load binary %p", Img);
return Binary;
}

Expand Down
1 change: 1 addition & 0 deletions offload/plugins-nextgen/amdgpu/dynamic_hsa/hsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef enum {
HSA_STATUS_INFO_BREAK = 0x1,
HSA_STATUS_ERROR = 0x1000,
HSA_STATUS_ERROR_INVALID_CODE_OBJECT = 0x1010,
HSA_STATUS_ERROR_INVALID_SYMBOL_NAME = 0x1013,
HSA_STATUS_ERROR_NOT_INITIALIZED = 0x100B,
HSA_STATUS_ERROR_EXCEPTION = 0x1016,
} hsa_status_t;
Expand Down
Loading
Loading