Skip to content

Commit

Permalink
add aot
Browse files Browse the repository at this point in the history
  • Loading branch information
victoryang00 committed Aug 17, 2023
1 parent 6f95951 commit 8dd28b8
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 98 deletions.
5 changes: 3 additions & 2 deletions include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#ifndef MVVM_WAMR_H
#define MVVM_WAMR_H

#include "aot_runtime.h"
#include "bh_read_file.h"
#include "logging.h"
#include "wamr_exec_env.h"
#include "wamr_export.h"
#include "wamr_read_write.h"
#include "wasm_runtime.h"
#include "wamr_export.h"

class WAMRInstance {
WASMExecEnv *exec_env{};
Expand Down Expand Up @@ -49,7 +50,7 @@ class WAMRInstance {
void set_func(WASMFunction *);
WASMExecEnv *get_exec_env();
WASMModuleInstance *get_module_instance();
WASMModule *get_module();
AOTModule *get_module();
void set_wasi_args(WAMRWASIContext &addrs);
void set_wasi_args(const std::vector<std::string> &dir_list, const std::vector<std::string> &map_dir_list,
const std::vector<std::string> &env_list, const std::vector<std::string> &arg_list,
Expand Down
9 changes: 5 additions & 4 deletions include/wamr_exec_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct WAMRExecEnv { // multiple
/* The native thread handle of current thread */
// korp_tid handle;

#if WASM_ENABLE_INTERP != 0
#if WASM_ENABLE_AOT == 0
WAMRBlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
#endif

Expand Down Expand Up @@ -146,18 +146,18 @@ struct WAMRExecEnv { // multiple
flags = env->suspend_flags.flags;
aux_boundary = env->aux_stack_boundary.boundary;
aux_bottom = env->aux_stack_bottom.bottom;
#if WASM_ENABLE_AOT == 0
for (int i = 0; i < BLOCK_ADDR_CACHE_SIZE; i++) {
for (int j = 0; j < 2; j++) {
dump(&(block_addr_cache[i][j]), &(env->block_addr_cache[i][j]));
}
}
#endif
auto cur_frame = env->cur_frame;
while (cur_frame) {
auto dumped_frame = new WAMRInterpFrame();
dump(dumped_frame, cur_frame);
this->frames.emplace_back(dumped_frame);
// this->lp.emplace_back(((uint8*)cur_frame->lp)- env->wasm_stack.s.bottom);
// LOGV(DEBUG)<<"lp:"<<this->lp.back() << " " << ((uint8*)cur_frame)-env->wasm_stack.s.bottom;
cur_frame = cur_frame->prev_frame;
}
wasm_stack = std::vector(env->wasm_stack.s.bottom, env->wasm_stack.s.top);
Expand Down Expand Up @@ -185,12 +185,13 @@ struct WAMRExecEnv { // multiple
cur_frame = cur_frame->prev_frame;
}
}

#if WASM_ENABLE_AOT == 0
for (int i = 0; i < BLOCK_ADDR_CACHE_SIZE; i++) {
for (int j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
restore(&(block_addr_cache[i][j]), &(env->block_addr_cache[i][j]));
}
}
#endif
};
};

Expand Down
42 changes: 6 additions & 36 deletions include/wamr_function_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,16 @@ template <CheckerTrait<WASMFunctionImport *> T> void dump(T t, WASMFunctionImpor
template <CheckerTrait<WASMFunctionImport *> T> bool equal(T t, WASMFunctionImport *env) { return t->equal_impl(env); }

struct WAMRFunctionInstance {
/* AOT required func index */
uint16 func_index{};
/* whether it is import function or WASM function */
bool is_import_func;
bool is_import_func{};
/* parameter count */
uint16 param_count;
uint16 param_count{};
/* local variable count, 0 for import function */
uint16 local_count;
/* cell num of parameters */
uint16 param_cell_num;
/* cell num of return type */
uint16 ret_cell_num;
/* cell num of local variables, 0 for import function */
uint16 local_cell_num;
// #if WASM_ENABLE_FAST_INTERP != 0
// /* cell num of consts */
// uint16 const_cell_num;
// #endif
std::vector<uint16> local_offsets;
/* parameter types */
std::vector<uint8> param_types;
/* local types, NULL for import function */
std::vector<uint8> local_types;
// union {
// WASMFunctionImport *func_import;
WAMRFunction func;
uint16 local_count{};
WAMRFunction func{};
WAMRFunctionImport func_import;
// } u;
void dump_impl(WASMFunctionInstance *env) {
is_import_func = env->is_import_func;
LOGV(DEBUG) << "is_import_func:" << is_import_func;
Expand All @@ -140,20 +124,6 @@ struct WAMRFunctionInstance {
/* local variable count, 0 for import function */
local_count = env->local_count;
LOGV(DEBUG) << "local_count:" << local_count;
/* cell num of parameters */
param_cell_num = env->param_cell_num;
LOGV(DEBUG) << "param_cell_num:" << param_cell_num;
/* cell num of return type */
ret_cell_num = env->ret_cell_num;
LOGV(DEBUG) << "ret_cell_num:" << ret_cell_num;
/* cell num of local variables, 0 for import function */
local_cell_num = env->local_cell_num;
LOGV(DEBUG) << "local_cell_num:" << local_cell_num;
local_offsets = std::vector(env->local_offsets, env->local_offsets + (param_count + local_count));
/* parameter types */
param_types = std::vector(env->param_types, env->param_types + param_count);
/* local types, NULL for import function */
local_types = std::vector(env->local_types, env->local_types + local_count);
};
void restore_impl(WASMFunctionInstance *env);
};
Expand Down
33 changes: 15 additions & 18 deletions src/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ std::vector<std::unique_ptr<WAMRExecEnv>> as;
std::mutex as_mtx;
/**fopen, fseek*/
void insert_fd(int fd, const char *path, int flags) {
printf("\n #insert_fd(fd,filename,flags) %d %s %d \n\n",fd, path,flags);
printf("\n #insert_fd(fd,filename,flags) %d %s %d \n\n", fd, path, flags);

if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) {
LOGV(ERROR) << "fd already exist" << fd;
wamr->fd_map_[fd] = std::make_pair(std::string(path), flags);
Expand All @@ -33,25 +33,25 @@ void remove_fd(int fd) {
if (wamr->fd_map_.find(fd) != wamr->fd_map_.end())
wamr->fd_map_.erase(fd);
else
LOGV(ERROR)<< "fd not found" << fd;
}
void insert_socket(int fd){

LOGV(ERROR) << "fd not found" << fd;
}
void insert_socket(int fd) {}
void serialize_to_file(WASMExecEnv *instance) {
/** Sounds like AoT/JIT is in this?*/
// Note: insert fd
std::ifstream stdoutput; stdoutput.open("output.txt");
std:string current_str;
std::ifstream stdoutput;
stdoutput.open("output.txt");
std:
string current_str;
std::string fd_output;
std::string filename_output;
std::string flags_output;
if(stdoutput.is_open()) {
while(stdoutput.good()) {

if (stdoutput.is_open()) {
while (stdoutput.good()) {
stdoutput >> current_str;
if(current_str == "fopen_test(fd,filename,flags)") {
stdoutput >> fd_output;
if (current_str == "fopen_test(fd,filename,flags)") {
stdoutput >> fd_output;
stdoutput >> filename_output;
stdoutput >> flags_output;
insert_fd(std::stoi(fd_output), filename_output.c_str(), std::stoi(flags_output));
Expand All @@ -60,8 +60,6 @@ void serialize_to_file(WASMExecEnv *instance) {
}
stdoutput.close();

//
std::cout<<"dasfasdfasf"<< re.str()<<"dasfasdfasf\n";
auto cluster = wasm_exec_env_get_cluster(instance);
if (bh_list_length(&cluster->exec_env_list) > 1) {
auto elem = (WASMExecEnv *)bh_list_first_elem(&cluster->exec_env_list);
Expand Down Expand Up @@ -136,8 +134,7 @@ int main(int argc, char *argv[]) {
options.add_options()("t,target", "The webassembly file to execute",
cxxopts::value<std::string>()->default_value("./test/counter.wasm"))(
"j,jit", "Whether the jit mode or interp mode", cxxopts::value<bool>()->default_value("false"))(
"d,dir", "The directory list exposed to WAMR",
cxxopts::value<std::vector<std::string>>()->default_value("./"))(
"d,dir", "The directory list exposed to WAMR", cxxopts::value<std::vector<std::string>>()->default_value("./"))(
"m,map_dir", "The mapped directory list exposed to WAMRe",
cxxopts::value<std::vector<std::string>>()->default_value(""))(
"e,env", "The environment list exposed to WAMR",
Expand All @@ -164,7 +161,7 @@ int main(int argc, char *argv[]) {
wamr = new WAMRInstance(target.c_str(), is_jit);
wamr->set_wasi_args(dir, map_dir, env, arg, addr, ns_pool);
wamr->instantiate();
freopen("output.txt","w",stdout);
freopen("output.txt", "w", stdout);

#ifndef MVVM_DEBUG
// Define the sigaction structure
Expand Down
4 changes: 2 additions & 2 deletions src/wamr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ WASMModuleInstance *WAMRInstance::get_module_instance() {
return reinterpret_cast<WASMModuleInstance *>(exec_env->module_inst);
}

WASMModule *WAMRInstance::get_module() {
return reinterpret_cast<WASMModule *>(reinterpret_cast<WASMModuleInstance *>(exec_env->module_inst)->module);
AOTModule *WAMRInstance::get_module() {
return reinterpret_cast<AOTModule *>(reinterpret_cast<WASMModuleInstance *>(exec_env->module_inst)->module);
}
void WAMRInstance::recover(
std::vector<std::unique_ptr<WAMRExecEnv>> *execEnv) { // will call pthread create wrapper if needed?
Expand Down
1 change: 0 additions & 1 deletion src/wamr_block_addr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ void WAMRBlockAddr::dump_impl(BlockAddr *env) {
this->start_addr =
env->start_addr -
wamr->get_exec_env()->cur_frame->function->u.func->code; // here we need to get the offset from the code start.
// LOGV(DEBUG) << "start_addr " << this->start_addr;
if (env->else_addr)
this->else_addr = env->else_addr - wamr->get_exec_env()->cur_frame->function->u.func->code;
if (env->end_addr)
Expand Down
28 changes: 0 additions & 28 deletions src/wamr_function_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,6 @@ void WAMRFunctionInstance::restore_impl(WASMFunctionInstance *env) {
/* local variable count, 0 for import function */
if (local_count != env->local_count) {
continue;
} /* cell num of parameters */
if (param_cell_num != env->param_cell_num) {
continue;
}
/* cell num of return type */
if (ret_cell_num != env->ret_cell_num) {
continue;
}
/* cell num of local variables, 0 for import function */
if (local_cell_num != env->local_cell_num) {
continue;
}

for (i = 0; i < param_count + local_count; i++) {
if (local_offsets[i] != env->local_offsets[i]) {
continue;
}
}
for (i = 0; i < param_count; i++) {
if (param_types[i] != env->param_types[i]) {
continue;
}
}
/* local types, NULL for import function */
for (i = 0; i < local_count; i++) {
if (local_types[i] != env->local_types[i]) {
continue;
}
}
#endif
if (equal(&func, cur_func.u.func)) {
Expand Down
26 changes: 20 additions & 6 deletions src/wamr_interp_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
extern WAMRInstance *wamr;
void WAMRInterpFrame::dump_impl(WASMInterpFrame *env) {
if (env->function) {
// what's the counterpart for the aot?
#if WASM_ENABLE_AOT == 0
wamr->set_func(env->function->u.func);

#endif
if (env->ip)
ip = env->ip - env->function->u.func->code; // here we need to get the offset from the code start.

Expand All @@ -24,8 +26,21 @@ void WAMRInterpFrame::dump_impl(WASMInterpFrame *env) {
dump(local_csp, env->csp - i);
csp.emplace_back(local_csp);
}
if (env->function)
dump(&function, env->function);
#if WASM_ENABLE_AOT == 0
dump(&function, env->function);
#else
// get type + offset for the function
auto module_ = wamr->get_module();
uint32 *func_type_indexes = module_->func_type_indexes;
auto func_type_idx = module_->func_type_indexes[module_->start_func_index - module_->import_func_count];
auto func_type = module_->func_types[func_type_idx];
auto func = new WASMFunctionInstance{.is_import_func = false,
.param_count = func_type->param_count,
.local_count = func_type->result_count,
.u = {.func = new WASMFunction{.func_type = func_type}}
};
dump(&function, func);
#endif
}
}
void WAMRInterpFrame::restore_impl(WASMInterpFrame *env) {
Expand Down Expand Up @@ -58,8 +73,7 @@ void WAMRInterpFrame::restore_impl(WASMInterpFrame *env) {
}
env->csp = env->csp_bottom + csp.size() - 1;
env->csp_boundary = env->csp_bottom + env->function->u.func->max_block_num;
LOGV(DEBUG) << " csp_bottom" << env->csp_bottom << " sp_bottom"
<< env->sp_bottom << " sp" << sp << ((uint8 *)env->sp) - wamr->get_exec_env()->wasm_stack.s.bottom
<< " lp" << lp;
LOGV(DEBUG) << " csp_bottom" << env->csp_bottom << " sp_bottom" << env->sp_bottom << " sp" << sp
<< ((uint8 *)env->sp) - wamr->get_exec_env()->wasm_stack.s.bottom << " lp" << lp;
}
}

0 comments on commit 8dd28b8

Please sign in to comment.