Skip to content

Commit 050892d

Browse files
authored
[Offload] Use new error code handling mechanism and lower-case messages (#139275)
[Offload] Use new error code handling mechanism This removes the old ErrorCode-less error method and requires every user to provide a concrete error code. All calls have been updated. In addition, for consistency with error messages elsewhere in LLVM, all messages have been made to start lower case.
1 parent ba52b56 commit 050892d

File tree

19 files changed

+498
-287
lines changed

19 files changed

+498
-287
lines changed

offload/include/Shared/OffloadErrcodes.inc

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,38 @@
1414
// To add new error codes, add them to offload/liboffload/API/Common.td and run
1515
// the GenerateOffload target.
1616

17-
OFFLOAD_ERRC(SUCCESS, "Success", 0)
18-
OFFLOAD_ERRC(UNKNOWN, "Unknown or internal error", 1)
17+
OFFLOAD_ERRC(SUCCESS, "success", 0)
18+
OFFLOAD_ERRC(UNKNOWN, "unknown or internal error", 1)
19+
OFFLOAD_ERRC(HOST_IO, "I/O error on host", 2)
20+
OFFLOAD_ERRC(INVALID_BINARY, "a provided binary image is malformed", 3)
1921
OFFLOAD_ERRC(INVALID_NULL_POINTER,
20-
"A pointer argument is null when it should not be", 2)
21-
OFFLOAD_ERRC(INVALID_ARGUMENT, "An argument is invalid", 3)
22-
OFFLOAD_ERRC(OUT_OF_RESOURCES, "Out of resources", 4)
23-
OFFLOAD_ERRC(UNSUPPORTED,
24-
"generic error code for unsupported features and enums", 5)
22+
"a pointer argument is null when it should not be", 4)
23+
OFFLOAD_ERRC(INVALID_ARGUMENT, "an argument is invalid", 5)
24+
OFFLOAD_ERRC(NOT_FOUND, "requested object was not found in the binary image", 6)
25+
OFFLOAD_ERRC(OUT_OF_RESOURCES, "out of resources", 7)
2526
OFFLOAD_ERRC(
2627
INVALID_SIZE,
2728
"invalid size or dimensions (e.g., must not be zero, or is out of bounds)",
28-
6)
29-
OFFLOAD_ERRC(INVALID_ENUMERATION, "enumerator argument is not valid", 7)
30-
OFFLOAD_ERRC(INVALID_KERNEL_NAME,
31-
"Named kernel not found in the program binary", 8)
32-
OFFLOAD_ERRC(INVALID_VALUE, "Invalid Value", 9)
33-
OFFLOAD_ERRC(INVALID_PLATFORM, "Invalid platform", 10)
34-
OFFLOAD_ERRC(INVALID_DEVICE, "Invalid device", 11)
35-
OFFLOAD_ERRC(INVALID_QUEUE, "Invalid queue", 12)
36-
OFFLOAD_ERRC(INVALID_EVENT, "Invalid event", 13)
37-
OFFLOAD_ERRC(INVALID_NULL_HANDLE, "handle argument is not valid", 14)
29+
8)
30+
OFFLOAD_ERRC(INVALID_ENUMERATION, "enumerator argument is not valid", 9)
31+
OFFLOAD_ERRC(HOST_TOOL_NOT_FOUND,
32+
"a required binary (linker, etc.) was not found on the host", 10)
33+
OFFLOAD_ERRC(INVALID_VALUE, "invalid value", 11)
34+
OFFLOAD_ERRC(UNIMPLEMENTED,
35+
"generic error code for features currently unimplemented by the "
36+
"device/backend",
37+
12)
38+
OFFLOAD_ERRC(
39+
UNSUPPORTED,
40+
"generic error code for features unsupported by the device/backend", 13)
41+
OFFLOAD_ERRC(ASSEMBLE_FAILURE,
42+
"assembler failure while processing binary image", 14)
43+
OFFLOAD_ERRC(LINK_FAILURE, "linker failure while processing binary image", 15)
44+
OFFLOAD_ERRC(BACKEND_FAILURE,
45+
"the plugin backend is in an invalid or unsupported state", 16)
46+
OFFLOAD_ERRC(INVALID_NULL_HANDLE,
47+
"a handle argument is null when it should not be", 17)
48+
OFFLOAD_ERRC(INVALID_PLATFORM, "invalid platform", 18)
49+
OFFLOAD_ERRC(INVALID_DEVICE, "invalid device", 19)
50+
OFFLOAD_ERRC(INVALID_QUEUE, "invalid queue", 20)
51+
OFFLOAD_ERRC(INVALID_EVENT, "invalid event", 21)

offload/include/Shared/OffloadError.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ class OffloadError : public llvm::ErrorInfo<OffloadError, llvm::StringError> {
4646
// The definition for this resides in the plugin static library
4747
static char ID;
4848
};
49+
50+
/// Create an Offload error.
51+
template <typename... ArgsTy>
52+
static llvm::Error createOffloadError(error::ErrorCode Code, const char *ErrFmt,
53+
ArgsTy... Args) {
54+
std::string Buffer;
55+
llvm::raw_string_ostream(Buffer) << llvm::format(ErrFmt, Args...);
56+
return llvm::make_error<error::OffloadError>(Code, Buffer);
57+
}
58+
59+
inline llvm::Error createOffloadError(error::ErrorCode Code, const char *S) {
60+
return llvm::make_error<error::OffloadError>(Code, S);
61+
}
62+
63+
// The OffloadError will have a message of either:
64+
// * "{Context}: {Message}" if the other error is a StringError
65+
// * "{Context}" otherwise
66+
inline llvm::Error createOffloadError(error::ErrorCode Code,
67+
llvm::Error &&OtherError,
68+
const char *Context) {
69+
std::string Buffer{Context};
70+
llvm::raw_string_ostream buffer(Buffer);
71+
72+
handleAllErrors(
73+
std::move(OtherError),
74+
[&](llvm::StringError &Err) {
75+
buffer << ": ";
76+
buffer << Err.getMessage();
77+
},
78+
[&](llvm::ErrorInfoBase &Err) {
79+
// Non-string error message don't add anything to the offload error's
80+
// error message
81+
});
82+
83+
return llvm::make_error<error::OffloadError>(Code, Buffer);
84+
}
4985
} // namespace error
5086

5187
#endif

offload/liboffload/API/Common.td

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,32 @@ def ErrorCode : Enum {
8787
let name = "ol_errc_t";
8888
let desc = "Defines Return/Error codes";
8989
let etors =[
90-
Etor<"SUCCESS", "Success">,
90+
Etor<"SUCCESS", "success">,
9191

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

102110
// Handle related errors - only makes sense for liboffload
103-
Etor<"INVALID_VALUE", "Invalid Value">,
104-
Etor<"INVALID_PLATFORM", "Invalid platform">,
105-
Etor<"INVALID_DEVICE", "Invalid device">,
106-
Etor<"INVALID_QUEUE", "Invalid queue">,
107-
Etor<"INVALID_EVENT", "Invalid event">,
108-
Etor<"INVALID_NULL_HANDLE", "handle argument is not valid">
111+
Etor<"INVALID_NULL_HANDLE", "a handle argument is null when it should not be">,
112+
Etor<"INVALID_PLATFORM", "invalid platform">,
113+
Etor<"INVALID_DEVICE", "invalid device">,
114+
Etor<"INVALID_QUEUE", "invalid queue">,
115+
Etor<"INVALID_EVENT", "invalid event">,
109116
];
110117
}
111118

offload/liboffload/include/generated/OffloadAPI.h

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,51 @@ extern "C" {
2020
///////////////////////////////////////////////////////////////////////////////
2121
/// @brief Defines Return/Error codes
2222
typedef enum ol_errc_t {
23-
/// Success
23+
/// success
2424
OL_ERRC_SUCCESS = 0,
25-
/// Unknown or internal error
25+
/// unknown or internal error
2626
OL_ERRC_UNKNOWN = 1,
27-
/// A pointer argument is null when it should not be
28-
OL_ERRC_INVALID_NULL_POINTER = 2,
29-
/// An argument is invalid
30-
OL_ERRC_INVALID_ARGUMENT = 3,
31-
/// Out of resources
32-
OL_ERRC_OUT_OF_RESOURCES = 4,
33-
/// generic error code for unsupported features and enums
34-
OL_ERRC_UNSUPPORTED = 5,
27+
/// I/O error on host
28+
OL_ERRC_HOST_IO = 2,
29+
/// a provided binary image is malformed
30+
OL_ERRC_INVALID_BINARY = 3,
31+
/// a pointer argument is null when it should not be
32+
OL_ERRC_INVALID_NULL_POINTER = 4,
33+
/// an argument is invalid
34+
OL_ERRC_INVALID_ARGUMENT = 5,
35+
/// requested object was not found in the binary image
36+
OL_ERRC_NOT_FOUND = 6,
37+
/// out of resources
38+
OL_ERRC_OUT_OF_RESOURCES = 7,
3539
/// invalid size or dimensions (e.g., must not be zero, or is out of bounds)
36-
OL_ERRC_INVALID_SIZE = 6,
40+
OL_ERRC_INVALID_SIZE = 8,
3741
/// enumerator argument is not valid
38-
OL_ERRC_INVALID_ENUMERATION = 7,
39-
/// Named kernel not found in the program binary
40-
OL_ERRC_INVALID_KERNEL_NAME = 8,
41-
/// Invalid Value
42-
OL_ERRC_INVALID_VALUE = 9,
43-
/// Invalid platform
44-
OL_ERRC_INVALID_PLATFORM = 10,
45-
/// Invalid device
46-
OL_ERRC_INVALID_DEVICE = 11,
47-
/// Invalid queue
48-
OL_ERRC_INVALID_QUEUE = 12,
49-
/// Invalid event
50-
OL_ERRC_INVALID_EVENT = 13,
51-
/// handle argument is not valid
52-
OL_ERRC_INVALID_NULL_HANDLE = 14,
42+
OL_ERRC_INVALID_ENUMERATION = 9,
43+
/// a required binary (linker, etc.) was not found on the host
44+
OL_ERRC_HOST_TOOL_NOT_FOUND = 10,
45+
/// invalid value
46+
OL_ERRC_INVALID_VALUE = 11,
47+
/// generic error code for features currently unimplemented by the
48+
/// device/backend
49+
OL_ERRC_UNIMPLEMENTED = 12,
50+
/// generic error code for features unsupported by the device/backend
51+
OL_ERRC_UNSUPPORTED = 13,
52+
/// assembler failure while processing binary image
53+
OL_ERRC_ASSEMBLE_FAILURE = 14,
54+
/// linker failure while processing binary image
55+
OL_ERRC_LINK_FAILURE = 15,
56+
/// the plugin backend is in an invalid or unsupported state
57+
OL_ERRC_BACKEND_FAILURE = 16,
58+
/// a handle argument is null when it should not be
59+
OL_ERRC_INVALID_NULL_HANDLE = 17,
60+
/// invalid platform
61+
OL_ERRC_INVALID_PLATFORM = 18,
62+
/// invalid device
63+
OL_ERRC_INVALID_DEVICE = 19,
64+
/// invalid queue
65+
OL_ERRC_INVALID_QUEUE = 20,
66+
/// invalid event
67+
OL_ERRC_INVALID_EVENT = 21,
5368
/// @cond
5469
OL_ERRC_FORCE_UINT32 = 0x7fffffff
5570
/// @endcond

offload/liboffload/include/generated/OffloadPrint.hpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,54 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
5252
case OL_ERRC_UNKNOWN:
5353
os << "OL_ERRC_UNKNOWN";
5454
break;
55+
case OL_ERRC_HOST_IO:
56+
os << "OL_ERRC_HOST_IO";
57+
break;
58+
case OL_ERRC_INVALID_BINARY:
59+
os << "OL_ERRC_INVALID_BINARY";
60+
break;
5561
case OL_ERRC_INVALID_NULL_POINTER:
5662
os << "OL_ERRC_INVALID_NULL_POINTER";
5763
break;
5864
case OL_ERRC_INVALID_ARGUMENT:
5965
os << "OL_ERRC_INVALID_ARGUMENT";
6066
break;
67+
case OL_ERRC_NOT_FOUND:
68+
os << "OL_ERRC_NOT_FOUND";
69+
break;
6170
case OL_ERRC_OUT_OF_RESOURCES:
6271
os << "OL_ERRC_OUT_OF_RESOURCES";
6372
break;
64-
case OL_ERRC_UNSUPPORTED:
65-
os << "OL_ERRC_UNSUPPORTED";
66-
break;
6773
case OL_ERRC_INVALID_SIZE:
6874
os << "OL_ERRC_INVALID_SIZE";
6975
break;
7076
case OL_ERRC_INVALID_ENUMERATION:
7177
os << "OL_ERRC_INVALID_ENUMERATION";
7278
break;
73-
case OL_ERRC_INVALID_KERNEL_NAME:
74-
os << "OL_ERRC_INVALID_KERNEL_NAME";
79+
case OL_ERRC_HOST_TOOL_NOT_FOUND:
80+
os << "OL_ERRC_HOST_TOOL_NOT_FOUND";
7581
break;
7682
case OL_ERRC_INVALID_VALUE:
7783
os << "OL_ERRC_INVALID_VALUE";
7884
break;
85+
case OL_ERRC_UNIMPLEMENTED:
86+
os << "OL_ERRC_UNIMPLEMENTED";
87+
break;
88+
case OL_ERRC_UNSUPPORTED:
89+
os << "OL_ERRC_UNSUPPORTED";
90+
break;
91+
case OL_ERRC_ASSEMBLE_FAILURE:
92+
os << "OL_ERRC_ASSEMBLE_FAILURE";
93+
break;
94+
case OL_ERRC_LINK_FAILURE:
95+
os << "OL_ERRC_LINK_FAILURE";
96+
break;
97+
case OL_ERRC_BACKEND_FAILURE:
98+
os << "OL_ERRC_BACKEND_FAILURE";
99+
break;
100+
case OL_ERRC_INVALID_NULL_HANDLE:
101+
os << "OL_ERRC_INVALID_NULL_HANDLE";
102+
break;
79103
case OL_ERRC_INVALID_PLATFORM:
80104
os << "OL_ERRC_INVALID_PLATFORM";
81105
break;
@@ -88,9 +112,6 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
88112
case OL_ERRC_INVALID_EVENT:
89113
os << "OL_ERRC_INVALID_EVENT";
90114
break;
91-
case OL_ERRC_INVALID_NULL_HANDLE:
92-
os << "OL_ERRC_INVALID_NULL_HANDLE";
93-
break;
94115
default:
95116
os << "unknown enumerator";
96117
break;

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ ol_impl_result_t olGetKernel_impl(ol_program_handle_t Program,
482482
auto &Device = Program->Image->getDevice();
483483
auto KernelImpl = Device.constructKernel(KernelName);
484484
if (!KernelImpl)
485-
return OL_ERRC_INVALID_KERNEL_NAME;
485+
return ol_impl_result_t::fromError(KernelImpl.takeError());
486486

487487
auto Err = KernelImpl->init(Device, *Program->Image);
488488
if (Err)

offload/libomptarget/PluginManager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,9 @@ Expected<DeviceTy &> PluginManager::getDevice(uint32_t DeviceNo) {
538538
{
539539
auto ExclusiveDevicesAccessor = getExclusiveDevicesAccessor();
540540
if (DeviceNo >= ExclusiveDevicesAccessor->size())
541-
return createStringError(
542-
inconvertibleErrorCode(),
543-
"Device number '%i' out of range, only %i devices available",
541+
return error::createOffloadError(
542+
error::ErrorCode::INVALID_VALUE,
543+
"device number '%i' out of range, only %i devices available",
544544
DeviceNo, ExclusiveDevicesAccessor->size());
545545

546546
DevicePtr = &*(*ExclusiveDevicesAccessor)[DeviceNo];
@@ -549,8 +549,8 @@ Expected<DeviceTy &> PluginManager::getDevice(uint32_t DeviceNo) {
549549
// Check whether global data has been mapped for this device
550550
if (DevicePtr->hasPendingImages())
551551
if (loadImagesOntoDevice(*DevicePtr) != OFFLOAD_SUCCESS)
552-
return createStringError(inconvertibleErrorCode(),
553-
"Failed to load images on device '%i'",
554-
DeviceNo);
552+
return error::createOffloadError(error::ErrorCode::BACKEND_FAILURE,
553+
"failed to load images on device '%i'",
554+
DeviceNo);
555555
return *DevicePtr;
556556
}

offload/libomptarget/device.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ DeviceTy::~DeviceTy() {
7979
llvm::Error DeviceTy::init() {
8080
int32_t Ret = RTL->init_device(RTLDeviceID);
8181
if (Ret != OFFLOAD_SUCCESS)
82-
return llvm::createStringError(llvm::inconvertibleErrorCode(),
83-
"Failed to initialize device %d\n",
84-
DeviceID);
82+
return error::createOffloadError(error::ErrorCode::BACKEND_FAILURE,
83+
"failed to initialize device %d\n",
84+
DeviceID);
8585

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

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

offload/plugins-nextgen/amdgpu/dynamic_hsa/hsa.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef enum {
3030
HSA_STATUS_INFO_BREAK = 0x1,
3131
HSA_STATUS_ERROR = 0x1000,
3232
HSA_STATUS_ERROR_INVALID_CODE_OBJECT = 0x1010,
33+
HSA_STATUS_ERROR_INVALID_SYMBOL_NAME = 0x1013,
3334
HSA_STATUS_ERROR_NOT_INITIALIZED = 0x100B,
3435
HSA_STATUS_ERROR_EXCEPTION = 0x1016,
3536
} hsa_status_t;

0 commit comments

Comments
 (0)