Skip to content

Commit

Permalink
bytecode_module.cc -> bytecode_module.c
Browse files Browse the repository at this point in the history
  • Loading branch information
benvanik committed Aug 17, 2020
1 parent bce528f commit 3bf301e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
53 changes: 37 additions & 16 deletions iree/base/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,23 @@ typedef struct {
iree_host_size_t data_length;
} iree_byte_span_t;

#define iree_make_byte_span(data, data_length) \
{ (uint8_t*)(data), (data_length) }
static inline iree_byte_span_t iree_make_byte_span(
void* data, iree_host_size_t data_length) {
iree_byte_span_t v = {(uint8_t*)data, data_length};
return v;
}

// A span of constant bytes (ala std::span of const uint8_t).
typedef struct {
const uint8_t* data;
iree_host_size_t data_length;
} iree_const_byte_span_t;

#define iree_make_const_byte_span(data, data_length) \
{ (const uint8_t*)(data), (data_length) }
static inline iree_const_byte_span_t iree_make_const_byte_span(
const void* data, iree_host_size_t data_length) {
iree_const_byte_span_t v = {(const uint8_t*)data, data_length};
return v;
}

//===----------------------------------------------------------------------===//
// iree_string_view_t (like std::string_view/absl::string_view)
Expand All @@ -254,6 +260,12 @@ static inline iree_string_view_t iree_string_view_empty() {
// Returns true if the given string view is the empty string.
#define iree_string_view_is_empty(sv) (((sv).data == NULL) || ((sv).size == 0))

static inline iree_string_view_t iree_make_string_view(
const char* str, iree_host_size_t str_length) {
iree_string_view_t v = {str, str_length};
return v;
}

// Returns a string view initialized with a reference to the given
// NUL-terminated string literal.
static inline iree_string_view_t iree_make_cstring_view(const char* str) {
Expand Down Expand Up @@ -661,18 +673,6 @@ typedef struct {
void(IREE_API_PTR* free)(void* self, void* ptr);
} iree_allocator_t;

// Allocates using the iree_allocator_malloc and iree_allocator_free methods.
// These will usually be backed by malloc and free.
#define IREE_ALLOCATOR_SYSTEM \
iree_allocator_t { \
0, iree_allocator_system_allocate, iree_allocator_system_free \
}

// Does not perform any allocation or deallocation; used to wrap objects that
// are owned by external code/live in read-only memory/etc.
#define IREE_ALLOCATOR_NULL \
iree_allocator_t { 0, 0, 0 }

#ifndef IREE_API_NO_PROTOTYPES

// Allocates a block of |byte_length| bytes from the given allocator.
Expand Down Expand Up @@ -700,6 +700,27 @@ IREE_API_EXPORT void IREE_API_CALL iree_allocator_system_free(void* self,

#endif // IREE_API_NO_PROTOTYPES

// Allocates using the iree_allocator_malloc and iree_allocator_free methods.
// These will usually be backed by malloc and free.
#define IREE_ALLOCATOR_SYSTEM \
iree_allocator_t { \
0, iree_allocator_system_allocate, iree_allocator_system_free \
}
static inline iree_allocator_t iree_allocator_system() {
iree_allocator_t v = {0, iree_allocator_system_allocate,
iree_allocator_system_free};
return v;
}

// Does not perform any allocation or deallocation; used to wrap objects that
// are owned by external code/live in read-only memory/etc.
#define IREE_ALLOCATOR_NULL \
iree_allocator_t { 0, 0, 0 }
static inline iree_allocator_t iree_allocator_null() {
iree_allocator_t v = {0, 0, 0};
return v;
}

//===----------------------------------------------------------------------===//
// iree::FileMapping
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion iree/vm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ cc_library(
srcs = [
"bytecode_dispatch.c",
"bytecode_dispatch_util.h",
"bytecode_module.cc",
"bytecode_module.c",
"bytecode_module_impl.h",
"bytecode_op_table.h",
],
Expand Down
2 changes: 1 addition & 1 deletion iree/vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ iree_cc_library(
SRCS
"bytecode_dispatch.c"
"bytecode_dispatch_util.h"
"bytecode_module.cc"
"bytecode_module.c"
"bytecode_module_impl.h"
"bytecode_op_table.h"
DEPS
Expand Down
19 changes: 9 additions & 10 deletions iree/vm/bytecode_module.cc → iree/vm/bytecode_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "iree/base/alignment.h"
#include "iree/base/api.h"
#include "iree/base/logging.h"
#include "iree/vm/bytecode_module_impl.h"
#include "iree/vm/ref.h"
#include "iree/vm/stack.h"
Expand Down Expand Up @@ -58,8 +57,8 @@ static iree_vm_type_def_t iree_vm_bytecode_module_resolve_type(
result.value_type = IREE_VM_VALUE_TYPE_I64;
} else if (full_name[0] == '!') {
// Note that we drop the ! prefix:
iree_string_view_t type_name = iree_string_view_t{
full_name + 1, flatbuffers_string_len(full_name) - 1};
iree_string_view_t type_name = {full_name + 1,
flatbuffers_string_len(full_name) - 1};
if (strncmp(type_name.data, "vm.list<", strlen("vm.list<")) == 0) {
// This is a !vm.list<...> type. We don't actually care about the type as
// we allow list types to be widened. Rewrite to just vm.list as that's
Expand Down Expand Up @@ -254,16 +253,16 @@ static void iree_vm_bytecode_module_destroy(void* self) {

iree_allocator_free(module->flatbuffer_allocator,
(void*)module->flatbuffer_data.data);
module->flatbuffer_data = {NULL, 0};
module->flatbuffer_allocator = IREE_ALLOCATOR_NULL;
module->flatbuffer_data = iree_make_const_byte_span(NULL, 0);
module->flatbuffer_allocator = iree_allocator_null();

iree_allocator_free(module->allocator, module);
}

static iree_string_view_t iree_vm_bytecode_module_name(void* self) {
iree_vm_bytecode_module_t* module = (iree_vm_bytecode_module_t*)self;
flatbuffers_string_t name = iree_vm_BytecodeModuleDef_name(module->def);
return iree_string_view_t{name, flatbuffers_string_len(name)};
return iree_make_string_view(name, flatbuffers_string_len(name));
}

static iree_vm_module_signature_t iree_vm_bytecode_module_signature(
Expand Down Expand Up @@ -534,8 +533,8 @@ static iree_host_size_t iree_vm_bytecode_module_layout_state(
iree_align(sizeof(iree_vm_bytecode_module_state_t), 16);

if (state) {
state->rwdata_storage = {(base_ptr + offset),
(iree_host_size_t)rwdata_storage_capacity};
state->rwdata_storage =
iree_make_byte_span(base_ptr + offset, rwdata_storage_capacity);
}
offset += iree_align(rwdata_storage_capacity, 16);

Expand Down Expand Up @@ -709,8 +708,8 @@ IREE_API_EXPORT iree_status_t IREE_API_CALL iree_vm_bytecode_module_create(

flatbuffers_uint8_vec_t bytecode_data =
iree_vm_BytecodeModuleDef_bytecode_data(module_def);
module->bytecode_data = iree_const_byte_span_t{
bytecode_data, flatbuffers_uint8_vec_len(bytecode_data)};
module->bytecode_data = iree_make_const_byte_span(
bytecode_data, flatbuffers_uint8_vec_len(bytecode_data));

module->flatbuffer_data = flatbuffer_data;
module->flatbuffer_allocator = flatbuffer_allocator;
Expand Down

0 comments on commit 3bf301e

Please sign in to comment.