Enable lazy Orc JIT feature (#732)
The feature is disabled by default, to enable it, please use `cmake -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1` to build iwasm.
This commit is contained in:
@ -2535,6 +2535,13 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
|
||||
char func_name[32];
|
||||
AOTModule *module;
|
||||
|
||||
#if WASM_ENABLE_LAZY_JIT != 0
|
||||
LLVMOrcThreadSafeModuleRef ts_module;
|
||||
LLVMOrcJITDylibRef main_dylib;
|
||||
LLVMErrorRef error;
|
||||
LLVMOrcJITTargetAddress func_addr = 0;
|
||||
#endif
|
||||
|
||||
/* Allocate memory for module */
|
||||
if (!(module =
|
||||
loader_malloc(sizeof(AOTModule), error_buf, error_buf_size))) {
|
||||
@ -2597,6 +2604,47 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LAZY_JIT != 0
|
||||
bh_assert(comp_ctx->lazy_orcjit);
|
||||
|
||||
main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->lazy_orcjit);
|
||||
if (!main_dylib) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"failed to get dynmaic library reference");
|
||||
goto fail3;
|
||||
}
|
||||
|
||||
ts_module = LLVMOrcCreateNewThreadSafeModule(comp_ctx->module,
|
||||
comp_ctx->ts_context);
|
||||
if (!ts_module) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"failed to create thread safe module");
|
||||
goto fail3;
|
||||
}
|
||||
|
||||
if ((error = LLVMOrcLLLazyJITAddLLVMIRModule(comp_ctx->lazy_orcjit,
|
||||
main_dylib, ts_module))) {
|
||||
/*
|
||||
* If adding the ThreadSafeModule fails then we need to clean it up
|
||||
* ourselves. If adding it succeeds the JIT will manage the memory.
|
||||
*/
|
||||
aot_handle_llvm_errmsg(error_buf, error_buf_size,
|
||||
"failed to addIRModule: ", error);
|
||||
goto fail4;
|
||||
}
|
||||
|
||||
for (i = 0; i < comp_data->func_count; i++) {
|
||||
snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
|
||||
if ((error = LLVMOrcLLLazyJITLookup(comp_ctx->lazy_orcjit,
|
||||
&func_addr, func_name))) {
|
||||
aot_handle_llvm_errmsg(error_buf, error_buf_size,
|
||||
"cannot lookup: ", error);
|
||||
goto fail3;
|
||||
}
|
||||
module->func_ptrs[i] = (void *)func_addr;
|
||||
func_addr = 0;
|
||||
}
|
||||
#else
|
||||
/* Resolve function addresses */
|
||||
bh_assert(comp_ctx->exec_engine);
|
||||
for (i = 0; i < comp_data->func_count; i++) {
|
||||
@ -2609,6 +2657,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
|
||||
goto fail3;
|
||||
}
|
||||
}
|
||||
#endif /* WASM_ENABLE_LAZY_JIT != 0 */
|
||||
|
||||
/* Allocation memory for function type indexes */
|
||||
size = (uint64)module->func_count * sizeof(uint32);
|
||||
@ -2662,6 +2711,11 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
|
||||
|
||||
return module;
|
||||
|
||||
#if WASM_ENABLE_LAZY_JIT != 0
|
||||
fail4:
|
||||
LLVMOrcDisposeThreadSafeModule(ts_module);
|
||||
#endif
|
||||
|
||||
fail3:
|
||||
if (module->func_ptrs)
|
||||
wasm_runtime_free(module->func_ptrs);
|
||||
|
||||
Reference in New Issue
Block a user