Skip to content

Commit

Permalink
Wrapping iree_status_t access with macros.
Browse files Browse the repository at this point in the history
This makes it possible to change the type of iree_status_t with most code not caring about what its bits are.

Progress on issue #265.

PiperOrigin-RevId: 287847316
  • Loading branch information
benvanik authored and copybara-github committed Jan 2, 2020
1 parent b293e6a commit dab2a0d
Show file tree
Hide file tree
Showing 27 changed files with 483 additions and 381 deletions.
12 changes: 7 additions & 5 deletions bindings/python/pyiree/status_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ PyObject* StatusToPyExcClass(const Status& status) {
}
}

PyObject* ApiStatusToPyExcClass(iree_status_t status) {
switch (status) {
PyObject* ApiStatusToPyExcClass(iree_status_code_t status_code) {
switch (status_code) {
case IREE_STATUS_INVALID_ARGUMENT:
return PyExc_ValueError;
case IREE_STATUS_OUT_OF_RANGE:
Expand All @@ -57,9 +57,11 @@ pybind11::error_already_set StatusToPyExc(const Status& status) {

pybind11::error_already_set ApiStatusToPyExc(iree_status_t status,
const char* message) {
assert(status != IREE_STATUS_OK);
auto full_message = absl::StrCat(message, ": ", static_cast<int>(status));
PyErr_SetString(ApiStatusToPyExcClass(status), full_message.c_str());
assert(!iree_status_is_ok(status));
auto full_message = absl::StrCat(
message, ": ", iree_status_code_string(iree_status_code(status)));
PyErr_SetString(ApiStatusToPyExcClass(iree_status_code(status)),
full_message.c_str());
return pybind11::error_already_set();
}

Expand Down
2 changes: 1 addition & 1 deletion bindings/python/pyiree/status_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pybind11::error_already_set ApiStatusToPyExc(iree_status_t status,
const char* message);

inline void CheckApiStatus(iree_status_t status, const char* message) {
if (status == IREE_STATUS_OK) {
if (iree_status_is_ok(status)) {
return;
}
throw ApiStatusToPyExc(status, message);
Expand Down
10 changes: 5 additions & 5 deletions bindings/python/pyiree/vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ std::unique_ptr<FunctionAbi> VmContext::CreateFunctionAbi(
attrs.push_back({});
auto status = iree_vm_get_function_reflection_attr(
f, i, &attrs.back().first, &attrs.back().second);
if (status == IREE_STATUS_NOT_FOUND) {
if (iree_status_is_not_found(status)) {
attrs.pop_back();
break;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ VmModule VmModule::FromFlatbufferBlob(
{static_cast<const uint8_t*>(flatbuffer_blob->data()),
flatbuffer_blob->size()},
deallocator, IREE_ALLOCATOR_SYSTEM, &module);
if (status != IREE_STATUS_OK) {
if (!iree_status_is_ok(status)) {
deallocator.free(deallocator.self, nullptr);
}

Expand All @@ -150,7 +150,7 @@ absl::optional<iree_vm_function_t> VmModule::LookupFunction(
iree_vm_function_t f;
auto status = iree_vm_module_lookup_function_by_name(
raw_ptr(), linkage, {name.data(), name.size()}, &f);
if (status == IREE_STATUS_NOT_FOUND) {
if (iree_status_is_not_found(status)) {
return absl::nullopt;
}
CheckApiStatus(status, "Error looking up function");
Expand Down Expand Up @@ -194,8 +194,8 @@ std::string VmVariantList::DebugString() const {
}

void SetupVmBindings(pybind11::module m) {
CHECK_EQ(IREE_STATUS_OK, iree_vm_register_builtin_types());
CHECK_EQ(IREE_STATUS_OK, iree_hal_module_register_types());
IREE_CHECK_OK(iree_vm_register_builtin_types());
IREE_CHECK_OK(iree_hal_module_register_types());

// Built-in module creation.
m.def("create_hal_module", &CreateHalModule);
Expand Down
54 changes: 52 additions & 2 deletions iree/base/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,53 @@
namespace iree {

//===----------------------------------------------------------------------===//
// iree Core API
// iree_status_t
//===----------------------------------------------------------------------===//

IREE_API_EXPORT const char* IREE_API_CALL
iree_status_code_string(iree_status_code_t code) {
switch (code) {
case IREE_STATUS_OK:
return "OK";
case IREE_STATUS_CANCELLED:
return "CANCELLED";
case IREE_STATUS_UNKNOWN:
return "UNKNOWN";
case IREE_STATUS_INVALID_ARGUMENT:
return "INVALID_ARGUMENT";
case IREE_STATUS_DEADLINE_EXCEEDED:
return "DEADLINE_EXCEEDED";
case IREE_STATUS_NOT_FOUND:
return "NOT_FOUND";
case IREE_STATUS_ALREADY_EXISTS:
return "ALREADY_EXISTS";
case IREE_STATUS_PERMISSION_DENIED:
return "PERMISSION_DENIED";
case IREE_STATUS_UNAUTHENTICATED:
return "UNAUTHENTICATED";
case IREE_STATUS_RESOURCE_EXHAUSTED:
return "RESOURCE_EXHAUSTED";
case IREE_STATUS_FAILED_PRECONDITION:
return "FAILED_PRECONDITION";
case IREE_STATUS_ABORTED:
return "ABORTED";
case IREE_STATUS_OUT_OF_RANGE:
return "OUT_OF_RANGE";
case IREE_STATUS_UNIMPLEMENTED:
return "UNIMPLEMENTED";
case IREE_STATUS_INTERNAL:
return "INTERNAL";
case IREE_STATUS_UNAVAILABLE:
return "UNAVAILABLE";
case IREE_STATUS_DATA_LOSS:
return "DATA_LOSS";
default:
return "";
}
}

//===----------------------------------------------------------------------===//
// IREE Core API
//===----------------------------------------------------------------------===//

IREE_API_EXPORT iree_status_t IREE_API_CALL
Expand All @@ -44,6 +90,10 @@ IREE_API_EXPORT iree_status_t IREE_API_CALL iree_api_init(int* argc,
return IREE_STATUS_OK;
}

//===----------------------------------------------------------------------===//
// iree_allocator_t
//===----------------------------------------------------------------------===//

IREE_API_EXPORT iree_status_t IREE_API_CALL iree_allocator_malloc(
iree_allocator_t allocator, iree_host_size_t byte_length, void** out_ptr) {
if (!allocator.alloc) return IREE_STATUS_INVALID_ARGUMENT;
Expand Down Expand Up @@ -97,7 +147,7 @@ iree_allocator_system_free(void* self, void* ptr) {
}

//===----------------------------------------------------------------------===//
// Utilities for working with API types
// iree_string_view_t
//===----------------------------------------------------------------------===//

IREE_API_EXPORT iree_string_view_t IREE_API_CALL
Expand Down
Loading

0 comments on commit dab2a0d

Please sign in to comment.