Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable running mode control for runtime and module instance #1923

Merged
merged 5 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enable running mode control for various compiler/cli options (#1890)
  • Loading branch information
wenyongh authored Jan 25, 2023
commit e1595c95c0583f2e352d5734fa7ac4971889374c
23 changes: 1 addition & 22 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1256,28 +1256,7 @@ wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
WASMModuleInstance *module_inst_interp =
(WASMModuleInstance *)module_inst;

if (running_mode == Mode_Default) {
#if WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT == 0
running_mode = Mode_Interp;
#elif WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT == 0
running_mode = Mode_Fast_JIT;
#elif WASM_ENABLE_FAST_JIT == 0 && WASM_ENABLE_JIT != 0
running_mode = Mode_LLVM_JIT;
#else /* WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 */
#if WASM_ENABLE_LAZY_JIT == 0
running_mode = Mode_LLVM_JIT;
#else
running_mode = Mode_Multi_Tier_JIT;
#endif
#endif
}
module_inst_interp->e->running_mode = running_mode;

/* TODO: And here we should handle the function pointers, and call this
* API when instantiating the wasm module in wasm_runtime.c, so we don't
* need to modify the function pointers in wasm_interp_classic.c, let's
* do it later */
return true;
return wasm_set_running_mode(module_inst_interp, running_mode);
}
#endif

Expand Down
30 changes: 26 additions & 4 deletions core/iwasm/fast-jit/jit_codecache.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,31 @@ jit_code_cache_free(void *ptr)
bool
jit_pass_register_jitted_code(JitCompContext *cc)
{
uint32 jit_func_idx =
cc->cur_wasm_func_idx - cc->cur_wasm_module->import_function_count;
cc->cur_wasm_module->fast_jit_func_ptrs[jit_func_idx] =
cc->cur_wasm_func->fast_jit_jitted_code = cc->jitted_addr_begin;
WASMModuleInstance *instance;
WASMModule *module = cc->cur_wasm_module;
WASMFunction *func = cc->cur_wasm_func;
uint32 jit_func_idx = cc->cur_wasm_func_idx - module->import_function_count;

#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
&& WASM_ENABLE_LAZY_JIT != 0
os_mutex_lock(&module->instance_list_lock);
#endif

module->fast_jit_func_ptrs[jit_func_idx] = func->fast_jit_jitted_code =
cc->jitted_addr_begin;

#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
&& WASM_ENABLE_LAZY_JIT != 0
instance = module->instance_list;
while (instance) {
if (instance->e->running_mode == Mode_Fast_JIT)
instance->fast_jit_func_ptrs[jit_func_idx] = cc->jitted_addr_begin;
instance = instance->e->next;
}

os_mutex_unlock(&module->instance_list_lock);
#else
(void)instance;
#endif
return true;
}
10 changes: 7 additions & 3 deletions core/iwasm/fast-jit/jit_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ jit_compiler_set_call_to_fast_jit(WASMModule *module, uint32 func_idx)

func_ptr = jit_codegen_compile_call_to_fast_jit(module, func_idx);
if (func_ptr) {
uint32 i = func_idx - module->import_function_count;
module->functions[i]->call_to_fast_jit_from_llvm_jit = func_ptr;
jit_compiler_set_llvm_jit_func_ptr(module, func_idx, func_ptr);
}

Expand All @@ -259,12 +261,14 @@ jit_compiler_set_llvm_jit_func_ptr(WASMModule *module, uint32 func_idx,
WASMModuleInstance *instance;
uint32 i = func_idx - module->import_function_count;

module->functions[i]->llvm_jit_func_ptr = module->func_ptrs[i] = func_ptr;

os_mutex_lock(&module->instance_list_lock);

module->func_ptrs[i] = func_ptr;

instance = module->instance_list;
while (instance) {
instance->func_ptrs[func_idx] = func_ptr;
if (instance->e->running_mode == Mode_Multi_Tier_JIT)
instance->func_ptrs[func_idx] = func_ptr;
instance = instance->e->next;
}
os_mutex_unlock(&module->instance_list_lock);
Expand Down
7 changes: 5 additions & 2 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ wasm_runtime_instantiate(const wasm_module_t module,

/**
* Set the running mode of a WASM module instance, override the
* default running mode of the runtime
* default running mode of the runtime. Note that it only makes sense when
* the input is a wasm bytecode file: for the AOT file, runtime always runs
* it with AOT engine, and this function always returns true.
*
* @param module_inst the WASM module instance to set running mode
* @param running_mode the running mode to set
Expand All @@ -503,7 +505,8 @@ wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
/**
* Get the running mode of a WASM module instance, if no running mode
* is explicitly set the default running mode of runtime will
* be used and returned
* be used and returned. Note that it only makes sense when the input is a
* wasm bytecode file: for the AOT file, this function always returns 0.
*
* @param module_inst the WASM module instance to query for running mode
*
Expand Down
42 changes: 38 additions & 4 deletions core/iwasm/interpreter/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,14 @@ struct WASMFunction {
#endif

#if WASM_ENABLE_FAST_JIT != 0
/* The compiled fast jit jitted code block of this function */
void *fast_jit_jitted_code;
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
/* The compiled llvm jit func ptr of this function */
void *llvm_jit_func_ptr;
/* Code block to call fast jit jitted code of this function
from the llvm jit jitted code */
void *call_to_fast_jit_from_llvm_jit;
#endif
#endif
};
Expand Down Expand Up @@ -512,8 +517,7 @@ struct WASMModule {
* List of instances referred to this module. When source debugging
* feature is enabled, the debugger may modify the code section of
* the module, so we need to report a warning if user create several
* instances based on the same module. Sub instances created by
* lib-pthread or spawn API won't be added into the list.
* instances based on the same module.
*
* Also add the instance to the list for Fast JIT to LLVM JIT
* tier-up, since we need to lazily update the LLVM func pointers
Expand All @@ -533,7 +537,22 @@ struct WASMModule {
#endif

#if WASM_ENABLE_FAST_JIT != 0
/* func pointers of Fast JITed (un-imported) functions */
/**
* func pointers of Fast JITed (un-imported) functions
* for non Multi-Tier JIT mode:
* (1) when lazy jit is disabled, each pointer is set to the compiled
* fast jit jitted code
* (2) when lazy jit is enabled, each pointer is firstly inited as
* jit_global->compile_fast_jit_and_then_call, and then set to the
* compiled fast jit jitted code when it is called (the stub will
* compile the jit function and then update itself)
* for Multi-Tier JIT mode:
* each pointer is firstly inited as compile_fast_jit_and_then_call,
* and then set to the compiled fast jit jitted code when it is called,
* and when the llvm jit func ptr of the same function is compiled, it
* will be set to call_to_llvm_jit_from_fast_jit of this function type
* (tier-up from fast-jit to llvm-jit)
*/
void **fast_jit_func_ptrs;
/* locks for Fast JIT lazy compilation */
korp_mutex fast_jit_thread_locks[WASM_ORC_JIT_BACKEND_THREAD_NUM];
Expand All @@ -543,7 +562,16 @@ struct WASMModule {
#if WASM_ENABLE_JIT != 0
struct AOTCompData *comp_data;
struct AOTCompContext *comp_ctx;
/* func pointers of LLVM JITed (un-imported) functions */
/**
* func pointers of LLVM JITed (un-imported) functions
* for non Multi-Tier JIT mode:
* each pointer is set to the lookuped llvm jit func ptr, note that it
* is a stub and will trigger the actual compilation when it is called
* for Multi-Tier JIT mode:
* each pointer is inited as call_to_fast_jit code block, when the llvm
* jit func ptr is actually compiled, it is set to the compiled llvm jit
* func ptr
*/
void **func_ptrs;
/* whether the func pointers are compiled */
bool *func_ptrs_compiled;
Expand All @@ -568,6 +596,12 @@ struct WASMModule {
korp_tid llvm_jit_init_thread;
/* whether the llvm jit is initialized */
bool llvm_jit_inited;
/* Whether to enable llvm jit compilation:
it is set to true only when there is a module instance starts to
run with running mode Mode_LLVM_JIT or Mode_Multi_Tier_JIT,
since no need to enable llvm jit compilation for Mode_Interp and
Mode_Fast_JIT, so as to improve performance for them */
bool enable_llvm_jit_compilation;
#endif
};

Expand Down
12 changes: 7 additions & 5 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2989,7 +2989,7 @@ static bool
init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
uint32 error_buf_size)
{
LLVMJITOptions llvm_jit_options;
LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
AOTCompOption option = { 0 };
char *aot_last_error;
uint64 size;
Expand Down Expand Up @@ -3113,6 +3113,8 @@ init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
module->func_ptrs[i] = (void *)func_addr;

#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;

if (module->orcjit_stop_compiling)
return false;
#endif
Expand Down Expand Up @@ -3203,9 +3205,9 @@ orcjit_thread_callback(void *arg)

/* Wait until init_llvm_jit_functions_stage2 finishes */
os_mutex_lock(&module->tierup_wait_lock);
while (!module->llvm_jit_inited) {
while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation)) {
os_cond_reltimedwait(&module->tierup_wait_cond,
&module->tierup_wait_lock, 10);
&module->tierup_wait_lock, 10000);
if (module->orcjit_stop_compiling) {
/* init_llvm_jit_functions_stage2 failed */
os_mutex_unlock(&module->tierup_wait_lock);
Expand Down Expand Up @@ -4303,9 +4305,9 @@ wasm_loader_unload(WASMModule *module)
module->functions[i]->fast_jit_jitted_code);
}
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
if (module->functions[i]->llvm_jit_func_ptr) {
if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
jit_code_cache_free(
module->functions[i]->llvm_jit_func_ptr);
module->functions[i]->call_to_fast_jit_from_llvm_jit);
}
#endif
#endif
Expand Down
16 changes: 10 additions & 6 deletions core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,7 @@ static bool
init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
uint32 error_buf_size)
{
LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
AOTCompOption option = { 0 };
char *aot_last_error;
uint64 size;
Expand Down Expand Up @@ -1873,8 +1874,9 @@ init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
}

option.is_jit_mode = true;
option.opt_level = 3;
option.size_level = 3;
option.opt_level = llvm_jit_options.opt_level;
option.size_level = llvm_jit_options.size_level;

#if WASM_ENABLE_BULK_MEMORY != 0
option.enable_bulk_memory = true;
#endif
Expand Down Expand Up @@ -1957,6 +1959,8 @@ init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
module->func_ptrs[i] = (void *)func_addr;

#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;

if (module->orcjit_stop_compiling)
return false;
#endif
Expand Down Expand Up @@ -2047,9 +2051,9 @@ orcjit_thread_callback(void *arg)

/* Wait until init_llvm_jit_functions_stage2 finishes */
os_mutex_lock(&module->tierup_wait_lock);
while (!module->llvm_jit_inited) {
while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation)) {
os_cond_reltimedwait(&module->tierup_wait_cond,
&module->tierup_wait_lock, 10);
&module->tierup_wait_lock, 10000);
if (module->orcjit_stop_compiling) {
/* init_llvm_jit_functions_stage2 failed */
os_mutex_unlock(&module->tierup_wait_lock);
Expand Down Expand Up @@ -2995,9 +2999,9 @@ wasm_loader_unload(WASMModule *module)
module->functions[i]->fast_jit_jitted_code);
}
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
if (module->functions[i]->llvm_jit_func_ptr) {
if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
jit_code_cache_free(
module->functions[i]->llvm_jit_func_ptr);
module->functions[i]->call_to_fast_jit_from_llvm_jit);
}
#endif
#endif
Expand Down
Loading