Skip to content

[Offload] Add Error Codes to PluginInterface #138258

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions offload/include/Shared/OffloadErrcodes.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===- Auto-generated file, part of the LLVM/Offload project --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef OFFLOAD_ERRC
#error Please define the macro OFFLOAD_ERRCODE(Name, Desc, Value)
#endif

// Error codes are shared between PluginInterface and liboffload.
// 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(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)
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)
10 changes: 7 additions & 3 deletions offload/liboffload/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ if (CLANG_FORMAT)
tablegen(OFFLOAD OffloadFuncs.inc -gen-func-names)
tablegen(OFFLOAD OffloadImplFuncDecls.inc -gen-impl-func-decls)
tablegen(OFFLOAD OffloadPrint.hpp -gen-print-header)
tablegen(OFFLOAD OffloadErrcodes.inc -gen-errcodes)

set(OFFLOAD_GENERATED_FILES ${TABLEGEN_OUTPUT})
set(FILES_TO_COPY "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp")
set(GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated)
add_public_tablegen_target(OffloadGenerate)
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CLANG_FORMAT}
-i ${OFFLOAD_GENERATED_FILES})
-i ${TABLEGEN_OUTPUT})
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different ${OFFLOAD_GENERATED_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/../include/generated")
-E copy_if_different ${FILES_TO_COPY} ${GEN_DIR})
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc")
else()
message(WARNING "clang-format was not found, so the OffloadGenerate target\
will not be available. Offload will still build, but you will not be\
Expand Down
25 changes: 14 additions & 11 deletions offload/liboffload/API/Common.td
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,29 @@ def : Typedef {
let value = "void *";
}

def : Enum {
def ErrorCode : Enum {
let name = "ol_errc_t";
let desc = "Defines Return/Error codes";
let etors =[
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<"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">,

// 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_KERNEL_NAME", "Named kernel not found in the program binary">,
Etor<"OUT_OF_RESOURCES", "Out of resources">,
Etor<"UNSUPPORTED_FEATURE", "generic error code for unsupported features">,
Etor<"INVALID_ARGUMENT", "generic error code for invalid arguments">,
Etor<"INVALID_NULL_HANDLE", "handle argument is not valid">,
Etor<"INVALID_NULL_POINTER", "pointer argument may not be nullptr">,
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<"UNSUPPORTED_ENUMERATION", "enumerator argument is not supported by the device">,
Etor<"UNKNOWN", "Unknown or internal error">
Etor<"INVALID_NULL_HANDLE", "handle argument is not valid">
];
}

Expand Down
2 changes: 2 additions & 0 deletions offload/liboffload/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set(LIBOFFLOAD_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")

add_subdirectory(API)

add_llvm_library(
Expand Down
14 changes: 11 additions & 3 deletions offload/liboffload/include/OffloadImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#pragma once

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

Expand All @@ -105,5 +104,14 @@ struct ol_impl_result_t {
operator ol_result_t() { return Result; }

private:
static ol_errc_t GetErrorCode(std::error_code Code) {
if (Code.category() == llvm::omp::target::plugin::make_error_code(
llvm::omp::target::plugin::ErrorCode::SUCCESS)
.category()) {
return static_cast<ol_errc_t>(Code.value());
}
return OL_ERRC_UNKNOWN;
}

ol_result_t Result;
};
83 changes: 41 additions & 42 deletions offload/liboffload/include/generated/OffloadAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,45 @@
extern "C" {
#endif

///////////////////////////////////////////////////////////////////////////////
/// @brief Defines Return/Error codes
typedef enum ol_errc_t {
/// Success
OL_ERRC_SUCCESS = 0,
/// 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,
/// invalid size or dimensions (e.g., must not be zero, or is out of bounds)
OL_ERRC_INVALID_SIZE = 6,
/// 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,
/// @cond
OL_ERRC_FORCE_UINT32 = 0x7fffffff
/// @endcond

} ol_errc_t;

///////////////////////////////////////////////////////////////////////////////
#ifndef OL_VERSION_MAJOR
/// @brief Major version of the Offload API
Expand Down Expand Up @@ -101,47 +140,6 @@ typedef struct ol_program_impl_t *ol_program_handle_t;
/// @brief Handle of kernel object
typedef void *ol_kernel_handle_t;

///////////////////////////////////////////////////////////////////////////////
/// @brief Defines Return/Error codes
typedef enum ol_errc_t {
/// Success
OL_ERRC_SUCCESS = 0,
/// Invalid Value
OL_ERRC_INVALID_VALUE = 1,
/// Invalid platform
OL_ERRC_INVALID_PLATFORM = 2,
/// Invalid device
OL_ERRC_INVALID_DEVICE = 3,
/// Invalid queue
OL_ERRC_INVALID_QUEUE = 4,
/// Invalid event
OL_ERRC_INVALID_EVENT = 5,
/// Named kernel not found in the program binary
OL_ERRC_INVALID_KERNEL_NAME = 6,
/// Out of resources
OL_ERRC_OUT_OF_RESOURCES = 7,
/// generic error code for unsupported features
OL_ERRC_UNSUPPORTED_FEATURE = 8,
/// generic error code for invalid arguments
OL_ERRC_INVALID_ARGUMENT = 9,
/// handle argument is not valid
OL_ERRC_INVALID_NULL_HANDLE = 10,
/// pointer argument may not be nullptr
OL_ERRC_INVALID_NULL_POINTER = 11,
/// invalid size or dimensions (e.g., must not be zero, or is out of bounds)
OL_ERRC_INVALID_SIZE = 12,
/// enumerator argument is not valid
OL_ERRC_INVALID_ENUMERATION = 13,
/// enumerator argument is not supported by the device
OL_ERRC_UNSUPPORTED_ENUMERATION = 14,
/// Unknown or internal error
OL_ERRC_UNKNOWN = 15,
/// @cond
OL_ERRC_FORCE_UINT32 = 0x7fffffff
/// @endcond

} ol_errc_t;

///////////////////////////////////////////////////////////////////////////////
/// @brief Details of the error condition returned by an API call
typedef struct ol_error_struct_t {
Expand Down Expand Up @@ -477,7 +475,8 @@ OL_APIEXPORT ol_result_t OL_APICALL olMemFree(
/// @brief Enqueue a memcpy operation.
///
/// @details
/// - For host pointers, use the device returned by olGetHostDevice
/// - For host pointers, use the host device belonging to the
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't part of this MR, but I assume a previous update to the comments didn't get regen-ed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's my bad. If we continue to check in the generated files it would be useful to have CI catch this by checking there's no diff after regenerating.

/// OL_PLATFORM_BACKEND_HOST platform.
/// - If a queue is specified, at least one device must be a non-host device
/// - If a queue is not specified, the memcpy happens synchronously
///
Expand Down
51 changes: 24 additions & 27 deletions offload/liboffload/include/generated/OffloadPrint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
case OL_ERRC_SUCCESS:
os << "OL_ERRC_SUCCESS";
break;
case OL_ERRC_UNKNOWN:
os << "OL_ERRC_UNKNOWN";
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_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";
break;
case OL_ERRC_INVALID_VALUE:
os << "OL_ERRC_INVALID_VALUE";
break;
Expand All @@ -64,36 +88,9 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
case OL_ERRC_INVALID_EVENT:
os << "OL_ERRC_INVALID_EVENT";
break;
case OL_ERRC_INVALID_KERNEL_NAME:
os << "OL_ERRC_INVALID_KERNEL_NAME";
break;
case OL_ERRC_OUT_OF_RESOURCES:
os << "OL_ERRC_OUT_OF_RESOURCES";
break;
case OL_ERRC_UNSUPPORTED_FEATURE:
os << "OL_ERRC_UNSUPPORTED_FEATURE";
break;
case OL_ERRC_INVALID_ARGUMENT:
os << "OL_ERRC_INVALID_ARGUMENT";
break;
case OL_ERRC_INVALID_NULL_HANDLE:
os << "OL_ERRC_INVALID_NULL_HANDLE";
break;
case OL_ERRC_INVALID_NULL_POINTER:
os << "OL_ERRC_INVALID_NULL_POINTER";
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_UNSUPPORTED_ENUMERATION:
os << "OL_ERRC_UNSUPPORTED_ENUMERATION";
break;
case OL_ERRC_UNKNOWN:
os << "OL_ERRC_UNKNOWN";
break;
default:
os << "unknown enumerator";
break;
Expand Down
1 change: 1 addition & 0 deletions offload/plugins-nextgen/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(PluginCommon OBJECT
src/GlobalHandler.cpp
src/JIT.cpp
src/RPC.cpp
src/OffloadError.cpp
src/Utils/ELF.cpp
)
add_dependencies(PluginCommon intrinsics_gen)
Expand Down
64 changes: 64 additions & 0 deletions offload/plugins-nextgen/common/include/OffloadError.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//===- OffloadError.h - Definition of error class -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//

#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_OFFLOAD_ERROR_H
#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_OFFLOAD_ERROR_H

#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"

namespace llvm {
namespace omp {
namespace target {
namespace plugin {

enum class ErrorCode {
#define OFFLOAD_ERRC(Name, _, Value) Name = Value,
#include "Shared/OffloadErrcodes.inc"
#undef OFFLOAD_ERRC
};

} // namespace plugin
} // namespace target
} // namespace omp
} // namespace llvm

namespace std {
template <>
struct is_error_code_enum<llvm::omp::target::plugin::ErrorCode>
: std::true_type {};
} // namespace std

namespace llvm {
namespace omp {
namespace target {
namespace plugin {

const std::error_category &OffloadErrCategory();

inline std::error_code make_error_code(ErrorCode E) {
return std::error_code(static_cast<int>(E), OffloadErrCategory());
}

/// Base class for errors originating in DIA SDK, e.g. COM calls
class OffloadError : public ErrorInfo<OffloadError, StringError> {
public:
using ErrorInfo<OffloadError, StringError>::ErrorInfo;

OffloadError(const Twine &S) : ErrorInfo(S, ErrorCode::UNKNOWN) {}

static char ID;
};
} // namespace plugin
} // namespace target
} // namespace omp
} // namespace llvm

#endif
Loading
Loading