Skip to content

Commit

Permalink
WIP list conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-camp committed Apr 8, 2021
1 parent 59ad168 commit 6e94e8e
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
15 changes: 15 additions & 0 deletions iree/compiler/Dialect/VM/Conversion/VMToEmitC/ConvertVMToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ class GlobalStoreOpConversion : public OpConversionPattern<StoreOpTy> {
StringRef funcName;
};

class ListAllocOpConversion
: public OpConversionPattern<IREE::VM::ListAllocOp> {
using OpConversionPattern<IREE::VM::ListAllocOp>::OpConversionPattern;

private:
LogicalResult matchAndRewrite(
IREE::VM::ListAllocOp allocOp, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const override {
return failure();
}
};

} // namespace

void populateVMToCPatterns(MLIRContext *context,
Expand All @@ -226,6 +238,9 @@ void populateVMToCPatterns(MLIRContext *context,
patterns.insert<ConstZeroOpConversion<IREE::VM::ConstI32ZeroOp>>(context);
patterns.insert<ConstRefZeroOpConversion>(context);

// Lists
patterns.insert<ListAllocOpConversion>(context);

// Conditional assignment ops
patterns.insert<CallOpConversion<IREE::VM::SelectI32Op>>(context,
"vm_select_i32");
Expand Down
10 changes: 10 additions & 0 deletions iree/vm/test/emitc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ iree_cc_test(
::conversion_ops
::conversion_ops_i64
::global_ops
::list_ops
::shift_ops
::shift_ops_i64
)
Expand Down Expand Up @@ -134,6 +135,15 @@ iree_c_module(
"global_ops.h"
)

iree_c_module(
NAME
list_ops
SRC
"../list_ops.mlir"
H_FILE_OUTPUT
"list_ops.h"
)

iree_c_module(
NAME
shift_ops
Expand Down
121 changes: 121 additions & 0 deletions iree/vm/test/emitc/list_ops_ref.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "iree/testing/status_matchers.h"
#include "iree/vm/api.h"
#include "iree/vm/ops.h"
#include "iree/vm/shims.h"

//=============================================================================
// module "list_ops_ref"
//=============================================================================

struct list_ops_ref_s {
iree_allocator_t allocator;
};
struct list_ops_ref_state_s {
iree_allocator_t allocator;
uint8_t rwdata[0];
iree_vm_ref_t refs[0];
};
typedef struct list_ops_ref_s list_ops_ref_t;
typedef struct list_ops_ref_state_s list_ops_ref_state_t;

iree_status_t list_ops_ref_test_i8_impl(list_ops_ref_state_t* state) {
// %c42 = vm.const.i32 42 : i32
int32_t c42 = 42;

// %list = vm.list.alloc %c42 : (i32) -> !vm.list<i8>
iree_vm_type_def_t element_type =
iree_vm_type_def_make_value_type(IREE_VM_VALUE_TYPE_I8);
iree_vm_type_def_t* element_type_ptr = &element_type;
iree_vm_list_t* list = nullptr;
iree_vm_list_t** list_ptr = &list;
iree_vm_list_create(element_type_ptr, c42, state->allocator, list_ptr);

// %sz = vm.list.size %list : (!vm.list<i8>) -> i32
int32_t sz = iree_vm_list_size(list);

// %sz_dno = iree.do_not_optimize(%sz) : i32

// vm.return
return iree_ok_status();
}

//=============================================================================
// The code below setups functions and lookup tables to implement the vm
// interface
//=============================================================================
//=============================================================================
// module "list_ops_ref"
//=============================================================================

static iree_status_t list_ops_ref_test_i8(iree_vm_stack_t* stack,
list_ops_ref_t* module,
list_ops_ref_state_t* state) {
return list_ops_ref_test_i8_impl(state);
}
static const iree_vm_native_export_descriptor_t list_ops_ref_exports_[] = {
{iree_make_cstring_view("test_i8"), iree_make_cstring_view("0v_v"), 0,
NULL},
};

static const iree_vm_native_import_descriptor_t list_ops_ref_imports_[] = {};

static const iree_vm_native_function_ptr_t list_ops_ref_funcs_[] = {
{(iree_vm_native_function_shim_t)call_0v_v_shim,
(iree_vm_native_function_target_t)list_ops_ref_test_i8},
};

static const iree_vm_native_module_descriptor_t list_ops_ref_descriptor_ = {
iree_make_cstring_view("list_ops_ref"),
IREE_ARRAYSIZE(list_ops_ref_imports_),
list_ops_ref_imports_,
IREE_ARRAYSIZE(list_ops_ref_exports_),
list_ops_ref_exports_,
IREE_ARRAYSIZE(list_ops_ref_funcs_),
list_ops_ref_funcs_,
0,
NULL,
};
static iree_status_t list_ops_ref_alloc_state(
void* self, iree_allocator_t allocator,
iree_vm_module_state_t** out_module_state) {
list_ops_ref_state_t* state = NULL;
IREE_RETURN_IF_ERROR(
iree_allocator_malloc(allocator, sizeof(*state), (void**)&state));
memset(state, 0, sizeof(*state));
state->allocator = allocator;
state->allocator = iree_allocator_system();
*out_module_state = (iree_vm_module_state_t*)state;
return iree_ok_status();
}
static void list_ops_ref_free_state(void* self,
iree_vm_module_state_t* module_state) {
list_ops_ref_state_t* state = (list_ops_ref_state_t*)module_state;
iree_allocator_free(state->allocator, state);
}

static void list_ops_ref_destroy(void* self) {
list_ops_ref_t* module = (list_ops_ref_t*)self;
iree_allocator_free(module->allocator, module);
}

static iree_status_t list_ops_ref_create(iree_allocator_t allocator,
iree_vm_module_t** out_module) {
// Allocate shared module state.
list_ops_ref_t* module = NULL;
IREE_RETURN_IF_ERROR(
iree_allocator_malloc(allocator, sizeof(*module), (void**)&module));
memset(module, 0, sizeof(*module));
module->allocator = allocator;

iree_vm_module_t interface;
iree_status_t status = iree_vm_module_initialize(&interface, module);
if (!iree_status_is_ok(status)) {
iree_allocator_free(allocator, module);
return status;
}
interface.destroy = list_ops_ref_destroy;
interface.alloc_state = list_ops_ref_alloc_state;
interface.free_state = list_ops_ref_free_state;
return iree_vm_native_module_create(&interface, &list_ops_ref_descriptor_,
allocator, out_module);
}
2 changes: 2 additions & 0 deletions iree/vm/test/emitc/module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "iree/vm/test/emitc/conversion_ops.h"
#include "iree/vm/test/emitc/conversion_ops_i64.h"
#include "iree/vm/test/emitc/global_ops.h"
#include "iree/vm/test/emitc/list_ops_ref.h"
#include "iree/vm/test/emitc/shift_ops.h"
#include "iree/vm/test/emitc/shift_ops_i64.h"

Expand Down Expand Up @@ -66,6 +67,7 @@ std::vector<TestParams> GetModuleTestParams() {
{conversion_ops_descriptor_, conversion_ops_create},
{conversion_ops_i64_descriptor_, conversion_ops_i64_create},
{global_ops_descriptor_, global_ops_create},
{list_ops_ref_descriptor_, list_ops_ref_create},
{shift_ops_descriptor_, shift_ops_create},
{shift_ops_i64_descriptor_, shift_ops_i64_create}};

Expand Down

0 comments on commit 6e94e8e

Please sign in to comment.