Skip to content

Commit 47e6586

Browse files
RossBruntonJaddyen
authored andcommitted
[Offload] Add Error Codes to PluginInterface (llvm#138258)
A new ErrorCode enumeration is present in PluginInterface which can be used when returning an llvm::Error from offload and PluginInterface functions. This enum must be kept up to sync with liboffload's ol_errc_t enum, so both are automatically generated from liboffload's enum definition. Some error codes have also been shuffled around to allow for future work. Note that this patch only adds the machinery; actual error codes will be added in a future patch. ~~Depends on llvm#137339 , please ignore first commit of this MR.~~ This has been merged.
1 parent 35e0830 commit 47e6586

File tree

14 files changed

+275
-92
lines changed

14 files changed

+275
-92
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===- Auto-generated file, part of the LLVM/Offload project --------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef OFFLOAD_ERRC
10+
#error Please define the macro OFFLOAD_ERRCODE(Name, Desc, Value)
11+
#endif
12+
13+
// Error codes are shared between PluginInterface and liboffload.
14+
// To add new error codes, add them to offload/liboffload/API/Common.td and run
15+
// the GenerateOffload target.
16+
17+
OFFLOAD_ERRC(SUCCESS, "Success", 0)
18+
OFFLOAD_ERRC(UNKNOWN, "Unknown or internal error", 1)
19+
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)
25+
OFFLOAD_ERRC(
26+
INVALID_SIZE,
27+
"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)

offload/include/Shared/OffloadError.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===- OffloadError.h - Definition of error class -------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_OFFLOAD_ERROR_H
12+
#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_OFFLOAD_ERROR_H
13+
14+
#include "llvm/Support/Error.h"
15+
#include "llvm/Support/ErrorHandling.h"
16+
17+
namespace error {
18+
19+
enum class ErrorCode {
20+
#define OFFLOAD_ERRC(Name, _, Value) Name = Value,
21+
#include "Shared/OffloadErrcodes.inc"
22+
#undef OFFLOAD_ERRC
23+
};
24+
25+
} // namespace error
26+
27+
namespace std {
28+
template <> struct is_error_code_enum<error::ErrorCode> : std::true_type {};
29+
} // namespace std
30+
31+
namespace error {
32+
33+
const std::error_category &OffloadErrCategory();
34+
35+
inline std::error_code make_error_code(ErrorCode E) {
36+
return std::error_code(static_cast<int>(E), OffloadErrCategory());
37+
}
38+
39+
/// Base class for errors originating in DIA SDK, e.g. COM calls
40+
class OffloadError : public llvm::ErrorInfo<OffloadError, llvm::StringError> {
41+
public:
42+
using ErrorInfo<OffloadError, StringError>::ErrorInfo;
43+
44+
OffloadError(const llvm::Twine &S) : ErrorInfo(S, ErrorCode::UNKNOWN) {}
45+
46+
// The definition for this resides in the plugin static library
47+
static char ID;
48+
};
49+
} // namespace error
50+
51+
#endif

offload/liboffload/API/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ if (CLANG_FORMAT)
1111
tablegen(OFFLOAD OffloadFuncs.inc -gen-func-names)
1212
tablegen(OFFLOAD OffloadImplFuncDecls.inc -gen-impl-func-decls)
1313
tablegen(OFFLOAD OffloadPrint.hpp -gen-print-header)
14+
tablegen(OFFLOAD OffloadErrcodes.inc -gen-errcodes)
1415

15-
set(OFFLOAD_GENERATED_FILES ${TABLEGEN_OUTPUT})
16+
set(FILES_TO_COPY "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp")
17+
set(GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated)
1618
add_public_tablegen_target(OffloadGenerate)
1719
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CLANG_FORMAT}
18-
-i ${OFFLOAD_GENERATED_FILES})
20+
-i ${TABLEGEN_OUTPUT})
1921
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
20-
-E copy_if_different ${OFFLOAD_GENERATED_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/../include/generated")
22+
-E copy_if_different ${FILES_TO_COPY} ${GEN_DIR})
23+
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
24+
-E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc")
2125
else()
2226
message(WARNING "clang-format was not found, so the OffloadGenerate target\
2327
will not be available. Offload will still build, but you will not be\

offload/liboffload/API/Common.td

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,29 @@ def : Typedef {
8383
let value = "void *";
8484
}
8585

86-
def : Enum {
86+
def ErrorCode : Enum {
8787
let name = "ol_errc_t";
8888
let desc = "Defines Return/Error codes";
8989
let etors =[
9090
Etor<"SUCCESS", "Success">,
91+
92+
// 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">,
98+
Etor<"INVALID_SIZE", "invalid size or dimensions (e.g., must not be zero, or is out of bounds)">,
99+
Etor<"INVALID_ENUMERATION", "enumerator argument is not valid">,
100+
Etor<"INVALID_KERNEL_NAME", "Named kernel not found in the program binary">,
101+
102+
// Handle related errors - only makes sense for liboffload
91103
Etor<"INVALID_VALUE", "Invalid Value">,
92104
Etor<"INVALID_PLATFORM", "Invalid platform">,
93105
Etor<"INVALID_DEVICE", "Invalid device">,
94106
Etor<"INVALID_QUEUE", "Invalid queue">,
95107
Etor<"INVALID_EVENT", "Invalid event">,
96-
Etor<"INVALID_KERNEL_NAME", "Named kernel not found in the program binary">,
97-
Etor<"OUT_OF_RESOURCES", "Out of resources">,
98-
Etor<"UNSUPPORTED_FEATURE", "generic error code for unsupported features">,
99-
Etor<"INVALID_ARGUMENT", "generic error code for invalid arguments">,
100-
Etor<"INVALID_NULL_HANDLE", "handle argument is not valid">,
101-
Etor<"INVALID_NULL_POINTER", "pointer argument may not be nullptr">,
102-
Etor<"INVALID_SIZE", "invalid size or dimensions (e.g., must not be zero, or is out of bounds)">,
103-
Etor<"INVALID_ENUMERATION", "enumerator argument is not valid">,
104-
Etor<"UNSUPPORTED_ENUMERATION", "enumerator argument is not supported by the device">,
105-
Etor<"UNKNOWN", "Unknown or internal error">
108+
Etor<"INVALID_NULL_HANDLE", "handle argument is not valid">
106109
];
107110
}
108111

offload/liboffload/include/OffloadImpl.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88
#pragma once
99

10+
#include "PluginInterface.h"
1011
#include <OffloadAPI.h>
1112
#include <iostream>
1213
#include <memory>
@@ -93,9 +94,7 @@ struct ol_impl_result_t {
9394
ol_errc_t ErrCode;
9495
llvm::StringRef Details;
9596
llvm::handleAllErrors(std::move(Error), [&](llvm::StringError &Err) {
96-
// TODO: PluginInterface doesn't yet have a way to communicate offload
97-
// error codes
98-
ErrCode = OL_ERRC_UNKNOWN;
97+
ErrCode = GetErrorCode(Err.convertToErrorCode());
9998
Details = errorStrs().insert(Err.getMessage()).first->getKeyData();
10099
});
101100

@@ -105,5 +104,13 @@ struct ol_impl_result_t {
105104
operator ol_result_t() { return Result; }
106105

107106
private:
107+
static ol_errc_t GetErrorCode(std::error_code Code) {
108+
if (Code.category() ==
109+
error::make_error_code(error::ErrorCode::SUCCESS).category()) {
110+
return static_cast<ol_errc_t>(Code.value());
111+
}
112+
return OL_ERRC_UNKNOWN;
113+
}
114+
108115
ol_result_t Result;
109116
};

offload/liboffload/include/generated/OffloadAPI.h

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,45 @@
1717
extern "C" {
1818
#endif
1919

20+
///////////////////////////////////////////////////////////////////////////////
21+
/// @brief Defines Return/Error codes
22+
typedef enum ol_errc_t {
23+
/// Success
24+
OL_ERRC_SUCCESS = 0,
25+
/// Unknown or internal error
26+
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,
35+
/// invalid size or dimensions (e.g., must not be zero, or is out of bounds)
36+
OL_ERRC_INVALID_SIZE = 6,
37+
/// 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,
53+
/// @cond
54+
OL_ERRC_FORCE_UINT32 = 0x7fffffff
55+
/// @endcond
56+
57+
} ol_errc_t;
58+
2059
///////////////////////////////////////////////////////////////////////////////
2160
#ifndef OL_VERSION_MAJOR
2261
/// @brief Major version of the Offload API
@@ -101,47 +140,6 @@ typedef struct ol_program_impl_t *ol_program_handle_t;
101140
/// @brief Handle of kernel object
102141
typedef void *ol_kernel_handle_t;
103142

104-
///////////////////////////////////////////////////////////////////////////////
105-
/// @brief Defines Return/Error codes
106-
typedef enum ol_errc_t {
107-
/// Success
108-
OL_ERRC_SUCCESS = 0,
109-
/// Invalid Value
110-
OL_ERRC_INVALID_VALUE = 1,
111-
/// Invalid platform
112-
OL_ERRC_INVALID_PLATFORM = 2,
113-
/// Invalid device
114-
OL_ERRC_INVALID_DEVICE = 3,
115-
/// Invalid queue
116-
OL_ERRC_INVALID_QUEUE = 4,
117-
/// Invalid event
118-
OL_ERRC_INVALID_EVENT = 5,
119-
/// Named kernel not found in the program binary
120-
OL_ERRC_INVALID_KERNEL_NAME = 6,
121-
/// Out of resources
122-
OL_ERRC_OUT_OF_RESOURCES = 7,
123-
/// generic error code for unsupported features
124-
OL_ERRC_UNSUPPORTED_FEATURE = 8,
125-
/// generic error code for invalid arguments
126-
OL_ERRC_INVALID_ARGUMENT = 9,
127-
/// handle argument is not valid
128-
OL_ERRC_INVALID_NULL_HANDLE = 10,
129-
/// pointer argument may not be nullptr
130-
OL_ERRC_INVALID_NULL_POINTER = 11,
131-
/// invalid size or dimensions (e.g., must not be zero, or is out of bounds)
132-
OL_ERRC_INVALID_SIZE = 12,
133-
/// enumerator argument is not valid
134-
OL_ERRC_INVALID_ENUMERATION = 13,
135-
/// enumerator argument is not supported by the device
136-
OL_ERRC_UNSUPPORTED_ENUMERATION = 14,
137-
/// Unknown or internal error
138-
OL_ERRC_UNKNOWN = 15,
139-
/// @cond
140-
OL_ERRC_FORCE_UINT32 = 0x7fffffff
141-
/// @endcond
142-
143-
} ol_errc_t;
144-
145143
///////////////////////////////////////////////////////////////////////////////
146144
/// @brief Details of the error condition returned by an API call
147145
typedef struct ol_error_struct_t {
@@ -477,7 +475,8 @@ OL_APIEXPORT ol_result_t OL_APICALL olMemFree(
477475
/// @brief Enqueue a memcpy operation.
478476
///
479477
/// @details
480-
/// - For host pointers, use the device returned by olGetHostDevice
478+
/// - For host pointers, use the host device belonging to the
479+
/// OL_PLATFORM_BACKEND_HOST platform.
481480
/// - If a queue is specified, at least one device must be a non-host device
482481
/// - If a queue is not specified, the memcpy happens synchronously
483482
///

offload/liboffload/include/generated/OffloadPrint.hpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
4949
case OL_ERRC_SUCCESS:
5050
os << "OL_ERRC_SUCCESS";
5151
break;
52+
case OL_ERRC_UNKNOWN:
53+
os << "OL_ERRC_UNKNOWN";
54+
break;
55+
case OL_ERRC_INVALID_NULL_POINTER:
56+
os << "OL_ERRC_INVALID_NULL_POINTER";
57+
break;
58+
case OL_ERRC_INVALID_ARGUMENT:
59+
os << "OL_ERRC_INVALID_ARGUMENT";
60+
break;
61+
case OL_ERRC_OUT_OF_RESOURCES:
62+
os << "OL_ERRC_OUT_OF_RESOURCES";
63+
break;
64+
case OL_ERRC_UNSUPPORTED:
65+
os << "OL_ERRC_UNSUPPORTED";
66+
break;
67+
case OL_ERRC_INVALID_SIZE:
68+
os << "OL_ERRC_INVALID_SIZE";
69+
break;
70+
case OL_ERRC_INVALID_ENUMERATION:
71+
os << "OL_ERRC_INVALID_ENUMERATION";
72+
break;
73+
case OL_ERRC_INVALID_KERNEL_NAME:
74+
os << "OL_ERRC_INVALID_KERNEL_NAME";
75+
break;
5276
case OL_ERRC_INVALID_VALUE:
5377
os << "OL_ERRC_INVALID_VALUE";
5478
break;
@@ -64,36 +88,9 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
6488
case OL_ERRC_INVALID_EVENT:
6589
os << "OL_ERRC_INVALID_EVENT";
6690
break;
67-
case OL_ERRC_INVALID_KERNEL_NAME:
68-
os << "OL_ERRC_INVALID_KERNEL_NAME";
69-
break;
70-
case OL_ERRC_OUT_OF_RESOURCES:
71-
os << "OL_ERRC_OUT_OF_RESOURCES";
72-
break;
73-
case OL_ERRC_UNSUPPORTED_FEATURE:
74-
os << "OL_ERRC_UNSUPPORTED_FEATURE";
75-
break;
76-
case OL_ERRC_INVALID_ARGUMENT:
77-
os << "OL_ERRC_INVALID_ARGUMENT";
78-
break;
7991
case OL_ERRC_INVALID_NULL_HANDLE:
8092
os << "OL_ERRC_INVALID_NULL_HANDLE";
8193
break;
82-
case OL_ERRC_INVALID_NULL_POINTER:
83-
os << "OL_ERRC_INVALID_NULL_POINTER";
84-
break;
85-
case OL_ERRC_INVALID_SIZE:
86-
os << "OL_ERRC_INVALID_SIZE";
87-
break;
88-
case OL_ERRC_INVALID_ENUMERATION:
89-
os << "OL_ERRC_INVALID_ENUMERATION";
90-
break;
91-
case OL_ERRC_UNSUPPORTED_ENUMERATION:
92-
os << "OL_ERRC_UNSUPPORTED_ENUMERATION";
93-
break;
94-
case OL_ERRC_UNKNOWN:
95-
os << "OL_ERRC_UNKNOWN";
96-
break;
9794
default:
9895
os << "unknown enumerator";
9996
break;

offload/plugins-nextgen/common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_library(PluginCommon OBJECT
55
src/GlobalHandler.cpp
66
src/JIT.cpp
77
src/RPC.cpp
8+
src/OffloadError.cpp
89
src/Utils/ELF.cpp
910
)
1011
add_dependencies(PluginCommon intrinsics_gen)

0 commit comments

Comments
 (0)