diff --git a/runtime/core/error.h b/runtime/core/error.h index 0450476ea93..b75f107314d 100644 --- a/runtime/core/error.h +++ b/runtime/core/error.h @@ -205,42 +205,37 @@ using ::executorch::runtime::error_code_t; * @param[in] ... Optional format string for the log error message and its * arguments. */ -#define ET_CHECK_OK_OR_RETURN_ERROR(error__, ...) \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR(error__, ##__VA_ARGS__) - -// Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead. -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR(...) \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT( \ - __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) \ - (__VA_ARGS__) +#define ET_CHECK_OK_OR_RETURN_ERROR(...) \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR(__VA_ARGS__) /** * Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead. * This macro selects the correct version of * ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR based on the number of arguments passed. - * It uses a trick with the preprocessor to count the number of arguments and - * then selects the appropriate macro. - * - * The macro expansion uses __VA_ARGS__ to accept any number of arguments and - * then appends them to ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_, followed by the - * count of arguments. The count is determined by the macro - * ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT which takes the arguments and - * passes them along with a sequence of numbers (2, 1). The preprocessor then - * matches this sequence to the correct number of arguments provided. - * - * If two arguments are passed, ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 is - * selected, suitable for cases where an error code and a custom message are - * provided. If only one argument is passed, - * ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_1 is selected, which is used for cases - * with just an error code. - * - * Usage: - * ET_CHECK_OK_OR_RETURN_ERROR(error_code); // Calls v1 - * ET_CHECK_OK_OR_RETURN_ERROR(error_code, "Error message", ...); // Calls v2 + * It uses a helper that reliably picks the 1-arg or 2+-arg form on + * MSVC/Clang/GCC. */ -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT( \ - _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_##N +#define ET_INTERNAL_EXPAND(x) x +#define ET_INTERNAL_GET_MACRO( \ + _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) \ + NAME + +// Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead. +// Picks _2 for 2..10 args, _1 for exactly 1 arg. +#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR(...) \ + ET_INTERNAL_EXPAND(ET_INTERNAL_GET_MACRO( \ + __VA_ARGS__, \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 10 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 9 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 8 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 7 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 6 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 5 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 4 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 3 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2, /* 2 */ \ + ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_1 /* 1 */ \ + )(__VA_ARGS__)) // Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead. #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_1(error__) \ @@ -260,21 +255,3 @@ using ::executorch::runtime::error_code_t; return et_error__; \ } \ } while (0) - -// Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead. -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_3 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_4 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_5 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_6 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_7 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_8 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_9 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 -#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_10 \ - ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 diff --git a/runtime/executor/method.cpp b/runtime/executor/method.cpp index 7d35ebe5054..7a15fd7f5ee 100644 --- a/runtime/executor/method.cpp +++ b/runtime/executor/method.cpp @@ -1076,26 +1076,22 @@ Method::set_input(const EValue& input_evalue, size_t input_idx) { executorch::runtime::toString(t_src.scalar_type())); // Reset the shape for the Method's input as the size of forwarded input // tensor for shape dynamism. Also is a safety check if need memcpy. - Error err = resize_tensor(t_dst, t_src.sizes()); - ET_CHECK_OR_RETURN_ERROR( - err == Error::Ok, - InvalidArgument, - "Error setting input %" ET_PRIsize_t ": 0x%" PRIx32, - input_idx, - static_cast(err)); - Error error; + ET_CHECK_OK_OR_RETURN_ERROR( + resize_tensor(t_dst, t_src.sizes()), + "Error resizing tensor at input %" ET_PRIsize_t, + input_idx); auto tensor_meta = this->method_meta().input_tensor_meta(input_idx); if (tensor_meta->is_memory_planned()) { - error = internal::copy_tensor_data(t_dst, t_src); + ET_CHECK_OK_OR_RETURN_ERROR( + internal::copy_tensor_data(t_dst, t_src), + "Error copying tensor data at input %" ET_PRIsize_t, + input_idx); } else { - error = internal::share_tensor_data(t_dst, t_src); + ET_CHECK_OK_OR_RETURN_ERROR( + internal::share_tensor_data(t_dst, t_src), + "Error sharing tensor data at input %" ET_PRIsize_t, + input_idx); } - ET_CHECK_OR_RETURN_ERROR( - error == Error::Ok, - InvalidArgument, - "Error setting data_ptr %" ET_PRIsize_t ": 0x%" PRIx32, - input_idx, - static_cast(error)); // Prims have to be the same as what was traced } else if (e.isInt()) { ET_CHECK_OR_RETURN_ERROR( @@ -1188,10 +1184,7 @@ Method::set_inputs(const executorch::aten::ArrayRef& input_evalues) { input_size); for (size_t i = 0; i < input_size; i++) { - Error status = set_input(input_evalues[i], i); - if (status != Error::Ok) { - return status; - } + ET_CHECK_OK_OR_RETURN_ERROR(set_input(input_evalues[i], i)); } return Error::Ok; }