Skip to content

Commit e8e6651

Browse files
committed
[Offload] Follow other LLVM error code handling
1 parent 5850cb9 commit e8e6651

File tree

4 files changed

+133
-36
lines changed

4 files changed

+133
-36
lines changed

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)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 llvm {
18+
namespace omp {
19+
namespace target {
20+
namespace plugin {
21+
22+
enum class ErrorCode {
23+
#define OFFLOAD_ERRC(Name, _, Value) Name = Value,
24+
#include "OffloadErrcodes.inc"
25+
#undef OFFLOAD_ERRC
26+
};
27+
28+
class OffloadErrorCategory : public std::error_category {
29+
const char *name() const noexcept override { return "Offload Error"; }
30+
std::string message(int ev) const override {
31+
switch (static_cast<ErrorCode>(ev)) {
32+
#define OFFLOAD_ERRC(Name, Desc, Value) \
33+
case ErrorCode::Name: \
34+
return #Desc;
35+
#include "OffloadErrcodes.inc"
36+
#undef OFFLOAD_ERRC
37+
}
38+
}
39+
};
40+
} // namespace plugin
41+
} // namespace target
42+
} // namespace omp
43+
} // namespace llvm
44+
45+
namespace std {
46+
template <>
47+
struct is_error_code_enum<llvm::omp::target::plugin::ErrorCode>
48+
: std::true_type {};
49+
} // namespace std
50+
51+
namespace llvm {
52+
namespace omp {
53+
namespace target {
54+
namespace plugin {
55+
56+
const std::error_category &OffloadErrCategory();
57+
58+
inline std::error_code make_error_code(ErrorCode E) {
59+
return std::error_code(static_cast<int>(E), OffloadErrCategory());
60+
}
61+
62+
/// Base class for errors originating in DIA SDK, e.g. COM calls
63+
class OffloadError : public ErrorInfo<OffloadError, StringError> {
64+
public:
65+
using ErrorInfo<OffloadError, StringError>::ErrorInfo;
66+
67+
OffloadError(const Twine &S) : ErrorInfo(S, ErrorCode::UNKNOWN) {}
68+
69+
static char ID;
70+
};
71+
} // namespace plugin
72+
} // namespace target
73+
} // namespace omp
74+
} // namespace llvm
75+
76+
#endif

offload/plugins-nextgen/common/include/PluginInterface.h

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "GlobalHandler.h"
3131
#include "JIT.h"
3232
#include "MemoryManager.h"
33+
#include "OffloadError.h"
3334
#include "RPC.h"
3435
#include "omptarget.h"
3536

@@ -58,30 +59,6 @@ struct GenericKernelTy;
5859
struct GenericDeviceTy;
5960
struct RecordReplayTy;
6061

61-
enum class ErrorCode {
62-
#define OFFLOAD_ERRC(Name, _, Value) Name = Value,
63-
#include "OffloadErrcodes.inc"
64-
#undef OFFLOAD_ERRC
65-
};
66-
67-
class OffloadErrorCategory : public std::error_category {
68-
const char *name() const noexcept override { return "Offload Error"; }
69-
std::string message(int ev) const override {
70-
switch (static_cast<ErrorCode>(ev)) {
71-
#define OFFLOAD_ERRC(Name, Desc, Value) \
72-
case ErrorCode::Name: \
73-
return #Desc;
74-
#include "OffloadErrcodes.inc"
75-
#undef OFFLOAD_ERRC
76-
}
77-
}
78-
};
79-
80-
inline std::error_code make_error_code(ErrorCode EC) {
81-
static OffloadErrorCategory Cat{};
82-
return {static_cast<int>(EC), Cat};
83-
}
84-
8562
/// Class that wraps the __tgt_async_info to simply its usage. In case the
8663
/// object is constructed without a valid __tgt_async_info, the object will use
8764
/// an internal one and will synchronize the current thread with the pending
@@ -1406,16 +1383,25 @@ namespace Plugin {
14061383
/// Plugin::check().
14071384
static inline Error success() { return Error::success(); }
14081385

1409-
/// Create a string error.
1386+
/// Create an Offload error.
14101387
template <typename... ArgsTy>
1411-
static Error error(const char *ErrFmt, ArgsTy... Args) {
1412-
return createStringError(ErrorCode::UNKNOWN, ErrFmt, Args...);
1388+
static Error error(ErrorCode Code, const char *ErrFmt, ArgsTy... Args) {
1389+
std::string Buffer;
1390+
raw_string_ostream(Buffer) << format(ErrFmt, Args...);
1391+
return make_error<OffloadError>(Code, Buffer);
14131392
}
14141393

1415-
/// Create a string error.
14161394
template <typename... ArgsTy>
1417-
static Error error(ErrorCode Code, const char *ErrFmt, ArgsTy... Args) {
1418-
return createStringError(Code, ErrFmt, Args...);
1395+
static Error error(const char *ErrFmt, ArgsTy... Args) {
1396+
return error(ErrorCode::UNKNOWN, ErrFmt, Args...);
1397+
}
1398+
1399+
inline Error error(ErrorCode Code, const char *S) {
1400+
return make_error<OffloadError>(Code, S);
1401+
}
1402+
1403+
inline Error error(const char *S) {
1404+
return make_error<OffloadError>(ErrorCode::UNKNOWN, S);
14191405
}
14201406

14211407
/// Check the plugin-specific error code and return an error or success
@@ -1626,10 +1612,4 @@ template <typename ResourceRef> class GenericDeviceResourceManagerTy {
16261612
} // namespace omp
16271613
} // namespace llvm
16281614

1629-
namespace std {
1630-
template <>
1631-
struct is_error_code_enum<llvm::omp::target::plugin::ErrorCode>
1632-
: std::true_type {};
1633-
} // namespace std
1634-
16351615
#endif // OPENMP_LIBOMPTARGET_PLUGINS_COMMON_PLUGININTERFACE_H
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- OffloadError.cpp - Error extensions for offload --------------------===//
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+
#include "OffloadError.h"
10+
#include "llvm/Support/ErrorHandling.h"
11+
12+
using namespace llvm;
13+
using namespace llvm::omp::target::plugin;
14+
15+
namespace {
16+
// FIXME: This class is only here to support the transition to llvm::Error. It
17+
// will be removed once this transition is complete. Clients should prefer to
18+
// deal with the Error value directly, rather than converting to error_code.
19+
class OffloadErrorCategory : public std::error_category {
20+
public:
21+
const char *name() const noexcept override { return "llvm.offload"; }
22+
std::string message(int Condition) const override {
23+
switch (static_cast<ErrorCode>(Condition)) {
24+
#define OFFLOAD_ERRC(Name, Desc, Value) \
25+
case ErrorCode::Name: \
26+
return #Desc;
27+
#include "OffloadErrcodes.inc"
28+
#undef OFFLOAD_ERRC
29+
}
30+
llvm_unreachable("Unrecognized offload ErrorCode");
31+
}
32+
};
33+
} // namespace
34+
35+
const std::error_category &llvm::omp::target::plugin::OffloadErrCategory() {
36+
static OffloadErrorCategory MSFCategory;
37+
return MSFCategory;
38+
}
39+
40+
char OffloadError::ID;

0 commit comments

Comments
 (0)