[llvm] [Offload] Add Error Codes to PluginInterface (PR #138258)

Ross Brunton via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 03:48:05 PDT 2025


================
@@ -58,6 +58,30 @@ struct GenericKernelTy;
 struct GenericDeviceTy;
 struct RecordReplayTy;
 
+enum class ErrorCode {
+#define OFFLOAD_ERRC(Name, _, Value) Name = Value,
+#include "OffloadErrcodes.inc"
+#undef OFFLOAD_ERRC
+};
+
+class OffloadErrorCategory : public std::error_category {
+  const char *name() const noexcept override { return "Offload Error"; }
+  std::string message(int ev) const override {
+    switch (static_cast<ErrorCode>(ev)) {
+#define OFFLOAD_ERRC(Name, Desc, Value)                                        \
+  case ErrorCode::Name:                                                        \
+    return #Desc;
+#include "OffloadErrcodes.inc"
+#undef OFFLOAD_ERRC
+    }
+  }
+};
+
+inline std::error_code make_error_code(ErrorCode EC) {
+  static OffloadErrorCategory Cat{};
+  return {static_cast<int>(EC), Cat};
+}
----------------
RossBrunton wrote:

So to give a concrete example, as part of parsing elf files, we call `ELFObjectFileBase::createELFObjectFile` which can fail (e.g. if the elf file is malformed). When it fails, it returns some llvm::Error. Since this is not an offload error, we could/should swallow it and convert it to an appropriate Offload error (like INVALID_BINARY or whatever). If we forget to do that, we end up with the "Elf Error code" being returned from the C API, which is going to be nonsensical. That's why I think it's important to be able to tell whether an error is "offload" or not, so we can convert the nonsensical ELF errors into `UNKNOWN` or similar.

Although, now I've looked into it more, it looks like the preferred method is to use error RTTI for this. Would you be okay with it if, instead, I made an OffloadError type that extends StringError like some other components do? https://llvm.org/doxygen/classllvm_1_1StringError.html

https://github.com/llvm/llvm-project/pull/138258


More information about the llvm-commits mailing list