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 c956c4c commit 19cdf2d
Show file tree
Hide file tree
Showing 123 changed files with 1,039 additions and 1,368 deletions.
2 changes: 1 addition & 1 deletion bindings/python/pyiree/status_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ 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);
assert(!iree_status_is_ok(status));
auto full_message = absl::StrCat(message, ": ", static_cast<int>(status));
PyErr_SetString(ApiStatusToPyExcClass(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
59 changes: 50 additions & 9 deletions iree/base/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
// iree_status_t status = \
// ((PFN_iree_api_version_check)dlsym(library, "iree_api_version_check"))(
// IREE_API_VERSION_LATEST, &actual_version);
// if (status != IREE_STATUS_OK) {
// if (!iree_status_is_ok(status)) {
// LOG(FATAL) << "Unsupported runtime API version " << actual_version;
// }
// dlclose(library);
Expand Down Expand Up @@ -230,23 +230,70 @@ typedef struct iree_file_mapping iree_file_mapping_t;
// Error handling macros
//===----------------------------------------------------------------------===//

// TODO(GH-265): string formatting constructors.
// TODO(GH-265): payload support (custom stack traces, etc).

// TODO(GH-265): replace status with a composite struct.
#define iree_status_code(value) (value)

// Macros to check the value of a status code.
#define iree_status_is_ok(value) (iree_status_code(value) == IREE_STATUS_OK)
#define iree_status_is_cancelled(value) \
(iree_status_code(value) == IREE_STATUS_CANCELLED)
#define iree_status_is_unknown(value) \
(iree_status_code(value) == IREE_STATUS_UNKNOWN)
#define iree_status_is_invalid_argument(value) \
(iree_status_code(value) == IREE_STATUS_INVALID_ARGUMENT)
#define iree_status_is_deadline_exceeded(value) \
(iree_status_code(value) == IREE_STATUS_DEADLINE_EXCEEDED)
#define iree_status_is_not_found(value) \
(iree_status_code(value) == IREE_STATUS_NOT_FOUND)
#define iree_status_is_already_exists(value) \
(iree_status_code(value) == IREE_STATUS_ALREADY_EXISTS)
#define iree_status_is_permission_denied(value) \
(iree_status_code(value) == IREE_STATUS_PERMISSION_DENIED)
#define iree_status_is_resource_exhausted(value) \
(iree_status_code(value) == IREE_STATUS_RESOURCE_EXHAUSTED)
#define iree_status_is_failed_precondition(value) \
(iree_status_code(value) == IREE_STATUS_FAILED_PRECONDITION)
#define iree_status_is_aborted(value) \
(iree_status_code(value) == IREE_STATUS_ABORTED)
#define iree_status_is_out_of_range(value) \
(iree_status_code(value) == IREE_STATUS_OUT_OF_RANGE)
#define iree_status_is_unimplemented(value) \
(iree_status_code(value) == IREE_STATUS_UNIMPLEMENTED)
#define iree_status_is_internal(value) \
(iree_status_code(value) == IREE_STATUS_INTERNAL)
#define iree_status_is_unavailable(value) \
(iree_status_code(value) == IREE_STATUS_UNAVAILABLE)
#define iree_status_is_data_loss(value) \
(iree_status_code(value) == IREE_STATUS_DATA_LOSS)
#define iree_status_is_unauthenticated(value) \
(iree_status_code(value) == IREE_STATUS_UNAUTHENTICATED)

// TODO(GH-265): better logging of status checks.
#define IREE_CHECK_OK(expr) CHECK_EQ(IREE_STATUS_OK, iree_status_code(expr))
#define IREE_ASSERT_OK(expr) ASSERT_EQ(IREE_STATUS_OK, iree_status_code(expr))
#define IREE_EXPECT_OK(expr) EXPECT_EQ(IREE_STATUS_OK, iree_status_code(expr))

#define IREE_API_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
#define IREE_API_STATUS_MACROS_IMPL_CONCAT_(x, y) \
IREE_API_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y)
#define IREE_API_STATUS_MACROS_IMPL_RETURN_IF_API_ERROR_(var, expr) \
iree_status_t var = (expr); \
if (var) return var;

// TODO(GH-265): variadic arguments to support appending strings.
// Propagates the error returned by (expr) by returning from the current
// function on non-OK status.
//
// Example:
// iree_status_t OtherFunc(...);
// iree_status_t MyFunc(...) {
// IREE_API_RETURN_IF_API_ERROR(OtherFunc(...));
// IREE_RETURN_IF_ERROR(OtherFunc(...));
// return IREE_STATUS_OK;
// }
#define IREE_API_RETURN_IF_API_ERROR(expr) \
#define IREE_RETURN_IF_ERROR(expr) \
IREE_API_STATUS_MACROS_IMPL_RETURN_IF_API_ERROR_( \
IREE_API_STATUS_MACROS_IMPL_CONCAT_(__status_, __COUNTER__), (expr))

Expand Down Expand Up @@ -333,12 +380,6 @@ IREE_API_EXPORT int IREE_API_CALL iree_string_view_split(

#endif // IREE_API_NO_PROTOTYPES

//===----------------------------------------------------------------------===//
// iree::Shape
//===----------------------------------------------------------------------===//

// TODO(benvanik): shape functions.

//===----------------------------------------------------------------------===//
// iree::FileMapping
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 2 additions & 2 deletions iree/base/api_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ absl::InlinedVector<T, 4> QueryListValues(
absl::InlinedVector<T, 4> values(4);
iree_host_size_t count = 0;
iree_status_t status = fn(args..., values.size(), values.data(), &count);
if (status == IREE_STATUS_OUT_OF_RANGE) {
if (iree_status_is_out_of_range(status)) {
values.resize(count);
status = fn(args..., values.size(), values.data(), &count);
} else if (status != IREE_STATUS_OK) {
} else if (!iree_status_is_ok(status)) {
return {};
}
values.resize(count);
Expand Down
3 changes: 0 additions & 3 deletions iree/compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
# limitations under the License.

add_subdirectory(Dialect)
add_subdirectory(IR)
add_subdirectory(Serialization)
add_subdirectory(Transforms)
add_subdirectory(Translation)
add_subdirectory(Utils)
1 change: 0 additions & 1 deletion iree/compiler/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
add_subdirectory(Flow)
add_subdirectory(HAL)
add_subdirectory(IREE)
#add_subdirectory(Interpreter)
add_subdirectory(VM)
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ func @multipleDispatches(%arg0: tensor<128xf32>) -> tensor<128xf32> {
%cst = constant dense<[128, 1, 1]> : vector<3xi32>
// CHECK: [[RET_BUF:%.+]] = hal.allocator.allocate.shaped {{.+}}, "HostVisible|DeviceVisible|DeviceLocal", "Constant|Transfer|Mapping|Dispatch", shape=[
// CHECK-SAME: [[C128]]
// CHECK-SAME: ], element_size=4 : !ireex.ref<!hal.buffer>
// CHECK-SAME: ], element_size=4 : !iree.ref<!hal.buffer>
// CHECK-NEXT: hal.ex.defer_release [[RET_BUF]]
// CHECK-NEXT: [[TMP_BUF:%.+]] = hal.allocator.allocate.shaped {{.+}}, "DeviceVisible|DeviceLocal", "Transfer|Dispatch", shape=[
// CHECK-SAME: [[C128]]
// CHECK-SAME: ], element_size=4 : !ireex.ref<!hal.buffer>
// CHECK-SAME: ], element_size=4 : !iree.ref<!hal.buffer>
// CHECK-NEXT: hal.ex.defer_release [[TMP_BUF]]
// CHECK-NEXT: [[CMD:%.+]] = hal.command_buffer.create {{.+}}, "OneShot", "Transfer|Dispatch"
// CHECK-NEXT: hal.command_buffer.begin [[CMD]]
%0 = flow.ex.stream.fragment(%arg1 = %cst : vector<3xi32>, %arg2 = %arg0 : tensor<128xf32>) -> tensor<128xf32> {
// CHECK: [[EXE:%.+]] = hal.ex.cache_executable {{.+}}, @ex0 : !ireex.ref<!hal.executable>
// CHECK: [[EXE:%.+]] = hal.ex.cache_executable {{.+}}, @ex0 : !iree.ref<!hal.executable>
// CHECK-NEXT: hal.ex.push_binding [[CMD]], 0, %arg0, shape=[
// CHECK-SAME: [[C128]]
// CHECK-SAME: ], element_size=4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// RUN: iree-opt -split-input-file -iree-convert-flow-to-hal %s | IreeFileCheck %s

// CHECK-LABEL: func @tensorIO(%arg0: !ireex.ref<!hal.buffer>) -> !ireex.ref<!hal.buffer>
// CHECK-LABEL: func @tensorIO(%arg0: !iree.ref<!hal.buffer>) -> !iree.ref<!hal.buffer>
func @tensorIO(%arg0 : tensor<1x1xi32>) -> tensor<1x1xi32> {
// CHECK-NEXT: br ^bb1(%arg0 : !ireex.ref<!hal.buffer>)
// CHECK-NEXT: br ^bb1(%arg0 : !iree.ref<!hal.buffer>)
br ^bb1(%arg0 : tensor<1x1xi32>)
// CHECK-NEXT: ^bb1([[BB0:%.+]]: !ireex.ref<!hal.buffer>)
// CHECK-NEXT: ^bb1([[BB0:%.+]]: !iree.ref<!hal.buffer>)
^bb1(%0 : tensor<1x1xi32>):
// CHECK-NEXT: return [[BB0]] : !ireex.ref<!hal.buffer>
// CHECK-NEXT: return [[BB0]] : !iree.ref<!hal.buffer>
return %0 : tensor<1x1xi32>
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
// RUN: iree-opt -split-input-file -iree-convert-flow-to-hal -verify-diagnostics %s | IreeFileCheck %s

// CHECK-LABEL: hal.variable @var_i32 mutable : !ireex.ref<!hal.buffer>
// CHECK-LABEL: hal.variable @var_i32 mutable : !iree.ref<!hal.buffer>
flow.variable @var_i32 mutable : tensor<i32>
func @fn() {
// CHECK: [[V:%.+]] = hal.variable.load @var_i32 : !ireex.ref<!hal.buffer>
// CHECK: [[V:%.+]] = hal.variable.load @var_i32 : !iree.ref<!hal.buffer>
%0 = flow.variable.load @var_i32 : tensor<i32>
// CHECK-NEXT: hal.variable.store [[V]], @var_i32 : !ireex.ref<!hal.buffer>
// CHECK-NEXT: hal.variable.store [[V]], @var_i32 : !iree.ref<!hal.buffer>
flow.variable.store %0, @var_i32 : tensor<i32>
return
}

// -----

// CHECK-LABEL: hal.variable @var_i1 mutable : !ireex.ref<!hal.buffer>
// CHECK-LABEL: hal.variable @var_i1 mutable : !iree.ref<!hal.buffer>
flow.variable @var_i1 mutable : tensor<i1>
func @fn() {
// CHECK: [[V:%.+]] = hal.variable.load @var_i1 : !ireex.ref<!hal.buffer>
// CHECK: [[V:%.+]] = hal.variable.load @var_i1 : !iree.ref<!hal.buffer>
%0 = flow.variable.load @var_i1 : tensor<i1>
// CHECK-NEXT: hal.variable.store [[V]], @var_i1 : !ireex.ref<!hal.buffer>
// CHECK-NEXT: hal.variable.store [[V]], @var_i1 : !iree.ref<!hal.buffer>
flow.variable.store %0, @var_i1 : tensor<i1>
return
}

// -----
// Checks that an initializer function is generated, used and operates on
// a hal.buffer (versus tensor).
// CHECK-LABEL: func @__var_with_tensor_initializer_initializer() -> !ireex.ref<!hal.buffer>
// CHECK-LABEL: func @__var_with_tensor_initializer_initializer() -> !iree.ref<!hal.buffer>
// CHECK: hal.variable @var_with_tensor_initializer
// CHECK-SAME: init(@__var_with_tensor_initializer_initializer)
// CHECK-SAME: : !ireex.ref<!hal.buffer>
// CHECK-SAME: : !iree.ref<!hal.buffer>
flow.variable @var_with_tensor_initializer mutable dense<0.000000e+00> : tensor<f32>
func @fn() {
%0 = flow.variable.load @var_with_tensor_initializer : tensor<f32>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
// RUN: iree-opt -split-input-file -iree-convert-hal-to-vm %s | IreeFileCheck %s

// CHECK-LABEL: @allocatorComputeSize
func @allocatorComputeSize(%arg0 : !ireex.ref<!hal.allocator>) -> i32 {
func @allocatorComputeSize(%arg0 : !iree.ref<!hal.allocator>) -> i32 {
%c1024_i32 = constant 1024 : i32
// CHECK: %0 = vm.call.variadic @hal.allocator.compute_size(%arg0, %c6, %c15, [%c1024, %c1024], %c4) : (!ireex.ref<!hal.allocator>, i32, i32, i32..., i32) -> i32
// CHECK: %0 = vm.call.variadic @hal.allocator.compute_size(%arg0, %c6, %c15, [%c1024, %c1024], %c4) : (!iree.ref<!hal.allocator>, i32, i32, i32..., i32) -> i32
%0 = hal.allocator.compute_size %arg0, "HostLocal", "All", shape=[%c1024_i32, %c1024_i32], element_size=4
return %0 : i32
}

// -----

// CHECK-LABEL: @allocatorAllocate
func @allocatorAllocate(%arg0 : !ireex.ref<!hal.allocator>) -> !ireex.ref<!hal.buffer> {
func @allocatorAllocate(%arg0 : !iree.ref<!hal.allocator>) -> !iree.ref<!hal.buffer> {
%c1024_i32 = constant 1024 : i32
// CHECK: %ref = vm.call @hal.allocator.allocate(%arg0, %c6, %c15, %c1024) : (!ireex.ref<!hal.allocator>, i32, i32, i32) -> !ireex.ref<!hal.buffer>
%0 = hal.allocator.allocate %arg0, "HostLocal", "All", %c1024_i32 : !ireex.ref<!hal.buffer>
return %0 : !ireex.ref<!hal.buffer>
// CHECK: %ref = vm.call @hal.allocator.allocate(%arg0, %c6, %c15, %c1024) : (!iree.ref<!hal.allocator>, i32, i32, i32) -> !iree.ref<!hal.buffer>
%0 = hal.allocator.allocate %arg0, "HostLocal", "All", %c1024_i32 : !iree.ref<!hal.buffer>
return %0 : !iree.ref<!hal.buffer>
}

// -----

// CHECK: vm.rodata @allocatorAllocateConst_const_0 dense<123> : tensor<4x4xi32>
// CHECK-LABEL: func @allocatorAllocateConst
func @allocatorAllocateConst() -> !ireex.ref<!hal.buffer> {
%allocator = "test_hal.allocator"() : () -> !ireex.ref<!hal.allocator>
// CHECK: %allocatorAllocateConst_const_0 = vm.const.ref.rodata @allocatorAllocateConst_const_0 : !ireex.byte_buffer_ref
// CHECK: %ref = vm.call.variadic @hal.allocator.allocate.const(%0, %c6, %c2, [%c4, %c4_0], %c4_1, %allocatorAllocateConst_const_0) : (!ireex.ref<!hal.allocator>, i32, i32, i32..., i32, !ireex.byte_buffer_ref) -> !ireex.ref<!hal.buffer>
%buffer = hal.allocator.allocate.const %allocator, "HostVisible|HostCoherent", "Transfer" : !ireex.ref<!hal.buffer> = dense<123> : tensor<4x4xi32>
return %buffer : !ireex.ref<!hal.buffer>
func @allocatorAllocateConst() -> !iree.ref<!hal.buffer> {
%allocator = "test_hal.allocator"() : () -> !iree.ref<!hal.allocator>
// CHECK: %allocatorAllocateConst_const_0 = vm.const.ref.rodata @allocatorAllocateConst_const_0 : !iree.byte_buffer_ref
// CHECK: %ref = vm.call.variadic @hal.allocator.allocate.const(%0, %c6, %c2, [%c4, %c4_0], %c4_1, %allocatorAllocateConst_const_0) : (!iree.ref<!hal.allocator>, i32, i32, i32..., i32, !iree.byte_buffer_ref) -> !iree.ref<!hal.buffer>
%buffer = hal.allocator.allocate.const %allocator, "HostVisible|HostCoherent", "Transfer" : !iree.ref<!hal.buffer> = dense<123> : tensor<4x4xi32>
return %buffer : !iree.ref<!hal.buffer>
}

// -----

// CHECK-LABEL: @allocatorAllocateShaped
func @allocatorAllocateShaped(%arg0 : !ireex.ref<!hal.allocator>) -> !ireex.ref<!hal.buffer> {
func @allocatorAllocateShaped(%arg0 : !iree.ref<!hal.allocator>) -> !iree.ref<!hal.buffer> {
%c1024_i32 = constant 1024 : i32
// CHECK: %ref = vm.call.variadic @hal.allocator.allocate.shaped(%arg0, %c6, %c15, [%c1024, %c1024], %c4) : (!ireex.ref<!hal.allocator>, i32, i32, i32..., i32) -> !ireex.ref<!hal.buffer>
%0 = hal.allocator.allocate.shaped %arg0, "HostLocal", "All", shape=[%c1024_i32, %c1024_i32], element_size=4 : !ireex.ref<!hal.buffer>
return %0 : !ireex.ref<!hal.buffer>
// CHECK: %ref = vm.call.variadic @hal.allocator.allocate.shaped(%arg0, %c6, %c15, [%c1024, %c1024], %c4) : (!iree.ref<!hal.allocator>, i32, i32, i32..., i32) -> !iree.ref<!hal.buffer>
%0 = hal.allocator.allocate.shaped %arg0, "HostLocal", "All", shape=[%c1024_i32, %c1024_i32], element_size=4 : !iree.ref<!hal.buffer>
return %0 : !iree.ref<!hal.buffer>
}
Loading

0 comments on commit 19cdf2d

Please sign in to comment.