Skip to content

Commit

Permalink
alloc/free aot frame in IR
Browse files Browse the repository at this point in the history
  • Loading branch information
sakria9 committed Mar 22, 2024
1 parent 55a7db0 commit bec7667
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class WAMRInstance {
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,
const std::vector<std::string> &addr_list, const std::vector<std::string> &ns_lookup_pool);
void spawn_child(WASMExecEnv *main_env, bool);
void spawn_child(WASMExecEnv *cur_env, bool main);
void find_func(const char *name);
int invoke_main();
void invoke_init_c();
Expand Down
32 changes: 14 additions & 18 deletions src/wamr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,12 @@ void WAMRInstance::recover(std::vector<std::unique_ptr<WAMRExecEnv>> *e_) {
}

auto main_env = cur_env;
auto main_saved_call_chain = main_env->restore_call_chain;
cur_thread = ((uint64_t)main_env->handle);

fprintf(stderr, "main_env created %p %p\n\n", main_env, main_saved_call_chain);

main_env->is_restore = true;

main_env->restore_call_chain = nullptr;

// invoke_init_c();
#if WASM_ENABLE_LIB_PTHREAD != 0
SPDLOG_ERROR("no impl");
exit(-1);
spawn_child(main_env, true);
#endif
// restart main thread execution
Expand All @@ -542,10 +537,10 @@ void WAMRInstance::recover(std::vector<std::unique_ptr<WAMRExecEnv>> *e_) {
fprintf(stderr, "wakeup.release\n");
wakeup.release(100);

cur_env->is_restore = true;
cur_env->restore_call_chain = main_saved_call_chain;
#if WASM_ENABLE_LIB_PTHREAD != 0
fprintf(stderr, "invoke main %p %p\n", cur_env, cur_env->restore_call_chain);
SPDLOG_ERROR("no impl");
exit(-1);
fprintf(stderr, "invoke main %p %u\n", cur_env, cur_env->call_chain_size);
// replay sync ops to get OS state matching
wamr_handle_map(execEnv.front()->cur_count, ((uint64_t)main_env->handle));

Expand Down Expand Up @@ -604,15 +599,19 @@ void WAMRInstance::spawn_child(WASMExecEnv *cur_env, bool main) {
SPDLOG_DEBUG("pthread_create_wrapper, func {}\n", child_env->cur_count);
// module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf));
if (tid_start_arg_map.find(child_env->cur_count) != tid_start_arg_map.end()) {
SPDLOG_ERROR("no impl");
exit(-1);
// find the parent env
auto *saved_env = cur_env->restore_call_chain;
cur_env->restore_call_chain = NULL;
auto saved_call_chain_size = cur_env->call_chain_size;
auto saved_stack_size = cur_env->wasm_stack.top_boundary - cur_env->wasm_stack.bottom;
auto saved_stack = (char *)malloc(saved_stack_size);
memcpy(saved_stack, cur_env->wasm_stack.bottom, saved_stack_size);
exec_env->is_restore = true;
// main thread
thread_spawn_wrapper(cur_env, tid_start_arg_map[child_env->cur_count].first);
cur_env->restore_call_chain = saved_env;
exec_env->is_restore = false;

cur_env->call_chain_size = saved_call_chain_size;
memcpy(cur_env->wasm_stack.bottom, saved_stack, saved_stack_size);
free(saved_stack);
} else {
exec_env->is_restore = true;
pthread_create_wrapper(cur_env, nullptr, nullptr, id, id); // tid_map
Expand Down Expand Up @@ -682,11 +681,8 @@ WASMExecEnv *restore_env(WASMModuleInstanceCommon *module_inst) {
auto exec_env = wasm_exec_env_create_internal(module_inst, wamr->stack_size);
restore(child_env, exec_env);

auto s = exec_env->restore_call_chain;

wamr->cur_thread = ((uint64_t)exec_env->handle);
exec_env->is_restore = true;
fprintf(stderr, "restore_env: %p %p\n", exec_env, s);

return exec_env;
}
Expand Down
4 changes: 2 additions & 2 deletions src/wamr_branch_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void WAMRBranchBlock::dump_impl(WASMBranchBlock *env) {

if (env->frame_sp) {
frame_sp = reinterpret_cast<uint8 *>(env->frame_sp) -
wamr->get_exec_env()->wasm_stack.s.bottom; // offset to the wasm_stack_top
wamr->get_exec_env()->wasm_stack.bottom; // offset to the wasm_stack_top
}

cell_num = env->cell_num;
Expand All @@ -36,7 +36,7 @@ void WAMRBranchBlock::restore_impl(WASMBranchBlock *env) const {
env->target_addr = wamr->get_func()->code + target_addr;

if (frame_sp) {
uint8 *local_sp = wamr->get_exec_env()->wasm_stack.s.bottom + frame_sp;
uint8 *local_sp = wamr->get_exec_env()->wasm_stack.bottom + frame_sp;
env->frame_sp = reinterpret_cast<uint32 *>(local_sp);
}

Expand Down
12 changes: 5 additions & 7 deletions src/wamr_exec_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ void WAMRExecEnv::restore_impl(WASMExecEnv *env) {

if (wamr->is_aot) {
std::reverse(frames.begin(), frames.end());
std::vector<AOTFrame *> replay_frames;

auto save_top = env->wasm_stack.top;
env->call_chain_size = 0;
for (const auto &dump_frame : frames) {
if (!aot_alloc_frame(env, dump_frame->function_index)) {
SPDLOG_DEBUG("restore error: aot_alloc_frame failed");
Expand All @@ -53,14 +55,10 @@ void WAMRExecEnv::restore_impl(WASMExecEnv *env) {
fprintf(stderr, "restore function_index %d\n", dump_frame->function_index);
cur_frame->func_index = dump_frame->function_index;
memcpy(cur_frame->lp, dump_frame->stack_frame.data(), dump_frame->stack_frame.size() * sizeof(uint32));
replay_frames.emplace_back(cur_frame);
}
std::reverse(replay_frames.begin(), replay_frames.end());
env->call_chain_size = replay_frames.size();
env->restore_call_chain = (AOTFrame **)malloc(sizeof(void *) * replay_frames.size());
memcpy(env->restore_call_chain, replay_frames.data(), sizeof(void *) * replay_frames.size());

env->cur_frame = nullptr;
env->wasm_stack.top = save_top;
env->call_chain_size = frames.size();
} else {
auto module_inst = (WASMModuleInstance *)env->module_inst;
auto get_function = [&](size_t function_index) {
Expand Down
8 changes: 4 additions & 4 deletions src/wamr_interp_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ void WAMRInterpFrame::restore_impl(WASMInterpFrame *env) {
int i = 0;
for (auto &&csp_item : csp) {
restore(csp_item.get(), env->csp_bottom + i);
SPDLOG_ERROR("csp_bottom {}", ((uint8 *)env->csp_bottom + i) - wamr->get_exec_env()->wasm_stack.s.bottom);
SPDLOG_ERROR("csp_bottom {}", ((uint8 *)env->csp_bottom + i) - wamr->get_exec_env()->wasm_stack.bottom);
i++;
}

env->csp = env->csp_bottom + csp.size();
env->csp_boundary = env->csp_bottom + env->function->u.func->max_block_num;
}
SPDLOG_INFO("func_idx {} ip {} sp {} stack bottom {}", function_index, (void *)env->ip, (void *)env->sp,
(void *)wamr->get_exec_env()->wasm_stack.s.bottom);
(void *)wamr->get_exec_env()->wasm_stack.bottom);
}

#if WASM_ENABLE_AOT != 0
Expand Down Expand Up @@ -243,7 +243,7 @@ std::vector<std::unique_ptr<WAMRBranchBlock>> wasm_replay_csp_bytecode(WASMExecE
e->cell_num = cell_num; \
e->begin_addr = frame_ip - cur_func->u.func->code; \
e->target_addr = (_target_addr)-cur_func->u.func->code; \
e->frame_sp = reinterpret_cast<uint8 *>(frame_sp - (param_cell_num)) - exec_env->wasm_stack.s.bottom; \
e->frame_sp = reinterpret_cast<uint8 *>(frame_sp - (param_cell_num)) - exec_env->wasm_stack.bottom; \
csp.emplace_back(std::move(e)); \
}

Expand All @@ -266,7 +266,7 @@ std::vector<std::unique_ptr<WAMRBranchBlock>> wasm_replay_csp_bytecode(WASMExecE
csp.resize(csp.size() - (n)); \
frame_ip = cur_func->u.func->code + csp.back()->target_addr; \
/* copy arity values of block */ \
frame_sp = reinterpret_cast<uint32 *>(exec_env->wasm_stack.s.bottom + csp.back()->frame_sp); \
frame_sp = reinterpret_cast<uint32 *>(exec_env->wasm_stack.bottom + csp.back()->frame_sp); \
cell_num_to_copy = csp.back()->cell_num; \
frame_sp += cell_num_to_copy; \
} while (0)
Expand Down

0 comments on commit bec7667

Please sign in to comment.