Skip to content

Commit 40a14b5

Browse files
authored
Enable running mode control for runtime and module instance (#1923)
Enable setting running mode when executing a wasm bytecode file - Four running modes are supported: interpreter, fast-jit, llvm-jit and multi-tier-jit - Add APIs to set/get the default running mode of the runtime - Add APIs to set/get the running mode of a wasm module instance - Add running mode options for iwasm command line tool And add size/opt level options for LLVM JIT
1 parent 7bb78dc commit 40a14b5

File tree

13 files changed

+696
-88
lines changed

13 files changed

+696
-88
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
128128
static JitCompOptions jit_options = { 0 };
129129
#endif
130130

131+
#if WASM_ENABLE_JIT != 0
132+
static LLVMJITOptions llvm_jit_options = { 3, 3 };
133+
#endif
134+
135+
static RunningMode runtime_running_mode = Mode_Default;
136+
131137
#ifdef OS_ENABLE_HW_BOUND_CHECK
132138
/* The exec_env of thread local storage, set before calling function
133139
and used in signal handler, as we cannot get it from the argument
@@ -514,17 +520,41 @@ wasm_runtime_destroy()
514520
wasm_runtime_memory_destroy();
515521
}
516522

523+
RunningMode
524+
wasm_runtime_get_default_running_mode(void)
525+
{
526+
return runtime_running_mode;
527+
}
528+
529+
#if WASM_ENABLE_JIT != 0
530+
LLVMJITOptions
531+
wasm_runtime_get_llvm_jit_options(void)
532+
{
533+
return llvm_jit_options;
534+
}
535+
#endif
536+
517537
bool
518538
wasm_runtime_full_init(RuntimeInitArgs *init_args)
519539
{
520540
if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
521541
&init_args->mem_alloc_option))
522542
return false;
523543

544+
if (!wasm_runtime_set_default_running_mode(init_args->running_mode)) {
545+
wasm_runtime_memory_destroy();
546+
return false;
547+
}
548+
524549
#if WASM_ENABLE_FAST_JIT != 0
525550
jit_options.code_cache_size = init_args->fast_jit_code_cache_size;
526551
#endif
527552

553+
#if WASM_ENABLE_JIT != 0
554+
llvm_jit_options.size_level = init_args->llvm_jit_size_level;
555+
llvm_jit_options.opt_level = init_args->llvm_jit_opt_level;
556+
#endif
557+
528558
if (!wasm_runtime_env_init()) {
529559
wasm_runtime_memory_destroy();
530560
return false;
@@ -554,6 +584,47 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
554584
return true;
555585
}
556586

587+
bool
588+
wasm_runtime_is_running_mode_supported(RunningMode running_mode)
589+
{
590+
if (running_mode == Mode_Default) {
591+
return true;
592+
}
593+
else if (running_mode == Mode_Interp) {
594+
#if WASM_ENABLE_INTERP != 0
595+
return true;
596+
#endif
597+
}
598+
else if (running_mode == Mode_Fast_JIT) {
599+
#if WASM_ENABLE_FAST_JIT != 0
600+
return true;
601+
#endif
602+
}
603+
else if (running_mode == Mode_LLVM_JIT) {
604+
#if WASM_ENABLE_JIT != 0
605+
return true;
606+
#endif
607+
}
608+
else if (running_mode == Mode_Multi_Tier_JIT) {
609+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
610+
&& WASM_ENABLE_LAZY_JIT != 0
611+
return true;
612+
#endif
613+
}
614+
615+
return false;
616+
}
617+
618+
bool
619+
wasm_runtime_set_default_running_mode(RunningMode running_mode)
620+
{
621+
if (wasm_runtime_is_running_mode_supported(running_mode)) {
622+
runtime_running_mode = running_mode;
623+
return true;
624+
}
625+
return false;
626+
}
627+
557628
PackageType
558629
get_package_type(const uint8 *buf, uint32 size)
559630
{
@@ -1171,6 +1242,41 @@ wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
11711242
#endif
11721243
}
11731244

1245+
bool
1246+
wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
1247+
RunningMode running_mode)
1248+
{
1249+
#if WASM_ENABLE_AOT != 0
1250+
if (module_inst->module_type == Wasm_Module_AoT)
1251+
return true;
1252+
#endif
1253+
1254+
#if WASM_ENABLE_INTERP != 0
1255+
if (module_inst->module_type == Wasm_Module_Bytecode) {
1256+
WASMModuleInstance *module_inst_interp =
1257+
(WASMModuleInstance *)module_inst;
1258+
1259+
return wasm_set_running_mode(module_inst_interp, running_mode);
1260+
}
1261+
#endif
1262+
1263+
return false;
1264+
}
1265+
1266+
RunningMode
1267+
wasm_runtime_get_running_mode(wasm_module_inst_t module_inst)
1268+
{
1269+
#if WASM_ENABLE_INTERP != 0
1270+
if (module_inst->module_type == Wasm_Module_Bytecode) {
1271+
WASMModuleInstance *module_inst_interp =
1272+
(WASMModuleInstance *)module_inst;
1273+
return module_inst_interp->e->running_mode;
1274+
}
1275+
#endif
1276+
1277+
return Mode_Default;
1278+
}
1279+
11741280
void
11751281
wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
11761282
{

core/iwasm/common/wasm_runtime_common.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
extern "C" {
2626
#endif
2727

28+
/* Internal use for setting default running mode */
29+
#define Mode_Default 0
30+
2831
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
2932

3033
#define PUT_I64_TO_ADDR(addr, value) \
@@ -413,6 +416,13 @@ typedef struct wasm_frame_t {
413416
const char *func_name_wp;
414417
} WASMCApiFrame;
415418

419+
#ifdef WASM_ENABLE_JIT
420+
typedef struct LLVMJITOptions {
421+
uint32 opt_level;
422+
uint32 size_level;
423+
} LLVMJITOptions;
424+
#endif
425+
416426
#ifdef OS_ENABLE_HW_BOUND_CHECK
417427
/* Signal info passing to interp/aot signal handler */
418428
typedef struct WASMSignalInfo {
@@ -437,10 +447,28 @@ wasm_runtime_get_exec_env_tls(void);
437447
WASM_RUNTIME_API_EXTERN bool
438448
wasm_runtime_init(void);
439449

450+
/* Internal API */
451+
RunningMode
452+
wasm_runtime_get_default_running_mode(void);
453+
454+
#if WASM_ENABLE_JIT != 0
455+
/* Internal API */
456+
LLVMJITOptions
457+
wasm_runtime_get_llvm_jit_options(void);
458+
#endif
459+
440460
/* See wasm_export.h for description */
441461
WASM_RUNTIME_API_EXTERN bool
442462
wasm_runtime_full_init(RuntimeInitArgs *init_args);
443463

464+
/* See wasm_export.h for description */
465+
WASM_RUNTIME_API_EXTERN bool
466+
wasm_runtime_is_running_mode_supported(RunningMode running_mode);
467+
468+
/* See wasm_export.h for description */
469+
WASM_RUNTIME_API_EXTERN bool
470+
wasm_runtime_set_default_running_mode(RunningMode running_mode);
471+
444472
/* See wasm_export.h for description */
445473
WASM_RUNTIME_API_EXTERN void
446474
wasm_runtime_destroy(void);
@@ -484,6 +512,15 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
484512
uint32 heap_size, char *error_buf,
485513
uint32 error_buf_size);
486514

515+
/* See wasm_export.h for description */
516+
WASM_RUNTIME_API_EXTERN bool
517+
wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
518+
RunningMode running_mode);
519+
520+
/* See wasm_export.h for description */
521+
WASM_RUNTIME_API_EXTERN RunningMode
522+
wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
523+
487524
/* See wasm_export.h for description */
488525
WASM_RUNTIME_API_EXTERN void
489526
wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);

core/iwasm/fast-jit/jit_codecache.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,31 @@ jit_code_cache_free(void *ptr)
5656
bool
5757
jit_pass_register_jitted_code(JitCompContext *cc)
5858
{
59-
uint32 jit_func_idx =
60-
cc->cur_wasm_func_idx - cc->cur_wasm_module->import_function_count;
61-
cc->cur_wasm_module->fast_jit_func_ptrs[jit_func_idx] =
62-
cc->cur_wasm_func->fast_jit_jitted_code = cc->jitted_addr_begin;
59+
WASMModuleInstance *instance;
60+
WASMModule *module = cc->cur_wasm_module;
61+
WASMFunction *func = cc->cur_wasm_func;
62+
uint32 jit_func_idx = cc->cur_wasm_func_idx - module->import_function_count;
63+
64+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
65+
&& WASM_ENABLE_LAZY_JIT != 0
66+
os_mutex_lock(&module->instance_list_lock);
67+
#endif
68+
69+
module->fast_jit_func_ptrs[jit_func_idx] = func->fast_jit_jitted_code =
70+
cc->jitted_addr_begin;
71+
72+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
73+
&& WASM_ENABLE_LAZY_JIT != 0
74+
instance = module->instance_list;
75+
while (instance) {
76+
if (instance->e->running_mode == Mode_Fast_JIT)
77+
instance->fast_jit_func_ptrs[jit_func_idx] = cc->jitted_addr_begin;
78+
instance = instance->e->next;
79+
}
80+
81+
os_mutex_unlock(&module->instance_list_lock);
82+
#else
83+
(void)instance;
84+
#endif
6385
return true;
6486
}

core/iwasm/fast-jit/jit_compiler.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ jit_compiler_set_call_to_fast_jit(WASMModule *module, uint32 func_idx)
254254

255255
func_ptr = jit_codegen_compile_call_to_fast_jit(module, func_idx);
256256
if (func_ptr) {
257+
uint32 i = func_idx - module->import_function_count;
258+
module->functions[i]->call_to_fast_jit_from_llvm_jit = func_ptr;
257259
jit_compiler_set_llvm_jit_func_ptr(module, func_idx, func_ptr);
258260
}
259261

@@ -267,12 +269,14 @@ jit_compiler_set_llvm_jit_func_ptr(WASMModule *module, uint32 func_idx,
267269
WASMModuleInstance *instance;
268270
uint32 i = func_idx - module->import_function_count;
269271

270-
module->functions[i]->llvm_jit_func_ptr = module->func_ptrs[i] = func_ptr;
271-
272272
os_mutex_lock(&module->instance_list_lock);
273+
274+
module->func_ptrs[i] = func_ptr;
275+
273276
instance = module->instance_list;
274277
while (instance) {
275-
instance->func_ptrs[func_idx] = func_ptr;
278+
if (instance->e->running_mode == Mode_Multi_Tier_JIT)
279+
instance->func_ptrs[func_idx] = func_ptr;
276280
instance = instance->e->next;
277281
}
278282
os_mutex_unlock(&module->instance_list_lock);

core/iwasm/include/wasm_export.h

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ typedef struct mem_alloc_info_t {
131131
uint32_t highmark_size;
132132
} mem_alloc_info_t;
133133

134+
/* Running mode of runtime and module instance*/
135+
typedef enum RunningMode {
136+
Mode_Interp = 1,
137+
Mode_Fast_JIT,
138+
Mode_LLVM_JIT,
139+
Mode_Multi_Tier_JIT,
140+
} RunningMode;
141+
134142
/* WASM runtime initialize arguments */
135143
typedef struct RuntimeInitArgs {
136144
mem_alloc_type_t mem_alloc_type;
@@ -152,6 +160,13 @@ typedef struct RuntimeInitArgs {
152160

153161
/* Fast JIT code cache size */
154162
uint32_t fast_jit_code_cache_size;
163+
164+
/* Default running mode of the runtime */
165+
RunningMode running_mode;
166+
167+
/* LLVM JIT opt and size level */
168+
uint32_t llvm_jit_opt_level;
169+
uint32_t llvm_jit_size_level;
155170
} RuntimeInitArgs;
156171

157172
#ifndef WASM_VALKIND_T_DEFINED
@@ -195,9 +210,9 @@ WASM_RUNTIME_API_EXTERN bool
195210
wasm_runtime_init(void);
196211

197212
/**
198-
* Initialize the WASM runtime environment, and also initialize
199-
* the memory allocator and register native symbols, which are specified
200-
* with init arguments
213+
* Initialize the WASM runtime environment, WASM running mode,
214+
* and also initialize the memory allocator and register native symbols,
215+
* which are specified with init arguments
201216
*
202217
* @param init_args specifies the init arguments
203218
*
@@ -206,6 +221,28 @@ wasm_runtime_init(void);
206221
WASM_RUNTIME_API_EXTERN bool
207222
wasm_runtime_full_init(RuntimeInitArgs *init_args);
208223

224+
/**
225+
* Query whether a certain running mode is supported for the runtime
226+
*
227+
* @param running_mode the running mode to query
228+
*
229+
* @return true if this running mode is supported, false otherwise
230+
*/
231+
WASM_RUNTIME_API_EXTERN bool
232+
wasm_runtime_is_running_mode_supported(RunningMode running_mode);
233+
234+
/**
235+
* Set the default running mode for the runtime. It is inherited
236+
* to set the running mode of a module instance when it is instantiated,
237+
* and can be changed by calling wasm_runtime_set_running_mode
238+
*
239+
* @param running_mode the running mode to set
240+
*
241+
* @return true if success, false otherwise
242+
*/
243+
WASM_RUNTIME_API_EXTERN bool
244+
wasm_runtime_set_default_running_mode(RunningMode running_mode);
245+
209246
/**
210247
* Destroy the WASM runtime environment.
211248
*/
@@ -450,6 +487,34 @@ wasm_runtime_instantiate(const wasm_module_t module,
450487
uint32_t stack_size, uint32_t heap_size,
451488
char *error_buf, uint32_t error_buf_size);
452489

490+
/**
491+
* Set the running mode of a WASM module instance, override the
492+
* default running mode of the runtime. Note that it only makes sense when
493+
* the input is a wasm bytecode file: for the AOT file, runtime always runs
494+
* it with AOT engine, and this function always returns true.
495+
*
496+
* @param module_inst the WASM module instance to set running mode
497+
* @param running_mode the running mode to set
498+
*
499+
* @return true if success, false otherwise
500+
*/
501+
WASM_RUNTIME_API_EXTERN bool
502+
wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
503+
RunningMode running_mode);
504+
505+
/**
506+
* Get the running mode of a WASM module instance, if no running mode
507+
* is explicitly set the default running mode of runtime will
508+
* be used and returned. Note that it only makes sense when the input is a
509+
* wasm bytecode file: for the AOT file, this function always returns 0.
510+
*
511+
* @param module_inst the WASM module instance to query for running mode
512+
*
513+
* @return the running mode this module instance currently use
514+
*/
515+
WASM_RUNTIME_API_EXTERN RunningMode
516+
wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
517+
453518
/**
454519
* Deinstantiate a WASM module instance, destroy the resources.
455520
*

0 commit comments

Comments
 (0)