Repository: llvm/llvm-project
File: libsycl/src/detail/offload/offload_utils.hpp (C++)

Code before:
#include <sycl/__impl/info/device_type.hpp>

#include <OffloadAPI.h>

_LIBSYCL_BEGIN_NAMESPACE_SYCL

namespace detail {

/// Converts liboffload error code to C-string.
///
/// \param Error liboffload error code.
///
/// \returns C-string representing the name of Error as specified in enum.
const char *stringifyErrorCode(ol_errc_t Error);

/// Contructs C++-string with information about liboffload error.
///
/// \param Error liboffload result of calling API.
///
/// \returns C++-string containing all available data of failure.
inline std::string formatCodeString(ol_result_t Result) {
  return std::to_string(Result->Code) + " (" +
         std::string(stringifyErrorCode(Result->Code)) + ") " + Result->Details;
}

inline bool isSuccess(const ol_result_t &Result) {
  return Result == OL_SUCCESS;
}

/// Checks liboffload API call result.
///
/// Used after calling the API without check.
/// To be called when specific handling is needed and explicitly done by
/// developer before throwing exception.
///
/// \param Error liboffload result of calling API.
///
/// \throw sycl::runtime_exception if the call was not successful.
template <sycl::errc errc = sycl::errc::runtime>
void checkAndThrow(ol_result_t Result) {
  if (!isSuccess(Result)) {
    throw sycl::exception(sycl::make_error_code(errc),
                          detail::formatCodeString(Result));
  }
}

/// Calls the API, doesn't check result.
/// To be called when specific handling is needed and explicitly done by
/// developer after.
///
/// \param Function liboffload API function to be called.

Review comment (bug):
```suggestion
inline bool isFailed(const ol_result_t &Result) {
```
`if (isFailed(...` is easier to read than `if (!isSuccess(...`

Code after:
#include <sycl/__impl/info/device_type.hpp>

#include <OffloadAPI.h>

_LIBSYCL_BEGIN_NAMESPACE_SYCL

namespace detail {

/// Converts liboffload error code to C-string.
///
/// \param Error liboffload error code.
///
/// \returns C-string representing the name of Error as specified in enum.
const char *stringifyErrorCode(ol_errc_t Error);

/// Contructs C++-string with information about liboffload error.
///
/// \param Error liboffload result of calling API.
///
/// \returns C++-string containing all available data of failure.
inline std::string formatCodeString(ol_result_t Result) {
  return std::to_string(Result->Code) + " (" +
         std::string(stringifyErrorCode(Result->Code)) + ") " + Result->Details;
}

inline bool isFailed(const ol_result_t &Result) { return Result != OL_SUCCESS; }

/// Checks liboffload API call result.
///
/// Used after calling the API without check.
/// To be called when specific handling is needed and explicitly done by
/// developer before throwing exception.
///
/// \param Error liboffload result of calling API.
///
/// \throw sycl::runtime_exception if the call was not successful.
template <sycl::errc errc = sycl::errc::runtime>
void checkAndThrow(ol_result_t Result) {
  if (isFailed(Result)) {
    throw sycl::exception(sycl::make_error_code(errc),
                          detail::formatCodeString(Result));
  }
}

/// Calls the API, doesn't check result.
/// To be called when specific handling is needed and explicitly done by
/// developer after.
///
/// \param Function liboffload API function to be called.
/// \param Args arguments to be passed to the liboffload API function.
///