diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 7ba1999d2..9077b9f3e 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -1642,9 +1642,11 @@ wasm_trap_new_internal(wasm_store_t *store, const char *error_info) { wasm_trap_t *trap; +#if WASM_ENABLE_DUMP_CALL_STACK != 0 wasm_instance_vec_t *instances; wasm_instance_t *frame_instance = NULL; uint32 i; +#endif if (!singleton_engine || !singleton_engine->stores || !singleton_engine->stores->num_elems) { diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 2896fac8c..220aa311a 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -30,6 +30,9 @@ #if WASM_ENABLE_FAST_JIT != 0 #include "../fast-jit/jit_compiler.h" #endif +#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 +#include "../compilation/aot_llvm.h" +#endif #include "../common/wasm_c_api_internal.h" #include "../../version.h" @@ -347,8 +350,20 @@ wasm_runtime_env_init() } #endif +#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 + if (!aot_compiler_init()) { + goto fail10; + } +#endif + return true; +#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 +fail10: +#if WASM_ENABLE_FAST_JIT != 0 + jit_compiler_destroy(); +#endif +#endif #if WASM_ENABLE_FAST_JIT != 0 fail9: #if WASM_ENABLE_REF_TYPES != 0 @@ -438,6 +453,17 @@ wasm_runtime_destroy() os_mutex_destroy(®istered_module_list_lock); #endif +#if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 + /* Destroy LLVM-JIT compiler after destroying the modules + * loaded by multi-module feature, since these modules may + * create backend threads to compile the wasm functions, + * which may access the LLVM resources. We wait until they + * finish the compilation to avoid accessing the destroyed + * resources in the compilation threads. + */ + aot_compiler_destroy(); +#endif + #if WASM_ENABLE_FAST_JIT != 0 /* Destroy Fast-JIT compiler after destroying the modules * loaded by multi-module feature, since the Fast JIT's diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index e60e421b2..fa4aa681e 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -1400,6 +1400,34 @@ fail: return ret; } +bool +aot_compiler_init(void) +{ + /* Initialize LLVM environment */ + + LLVMInitializeCore(LLVMGetGlobalPassRegistry()); +#if WASM_ENABLE_WAMR_COMPILER != 0 + /* Init environment of all targets for AOT compiler */ + LLVMInitializeAllTargetInfos(); + LLVMInitializeAllTargets(); + LLVMInitializeAllTargetMCs(); + LLVMInitializeAllAsmPrinters(); +#else + /* Init environment of native for JIT compiler */ + LLVMInitializeNativeTarget(); + LLVMInitializeNativeTarget(); + LLVMInitializeNativeAsmPrinter(); +#endif + + return true; +} + +void +aot_compiler_destroy(void) +{ + LLVMShutdown(); +} + AOTCompContext * aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option) { @@ -1415,15 +1443,6 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option) LLVMCodeModel code_model; LLVMTargetDataRef target_data_ref; - /* Initialize LLVM environment */ - LLVMInitializeCore(LLVMGetGlobalPassRegistry()); - /* To all available target */ - LLVMInitializeAllTargetInfos(); - LLVMInitializeAllTargets(); - LLVMInitializeAllTargetMCs(); - LLVMInitializeAllAsmPrinters(); - LLVMInitializeAllAsmParsers(); - /* Allocate memory */ if (!(comp_ctx = wasm_runtime_malloc(sizeof(AOTCompContext)))) { aot_set_last_error("allocate memory failed."); @@ -2031,8 +2050,6 @@ aot_destroy_comp_context(AOTCompContext *comp_ctx) if (comp_ctx->orc_jit) LLVMOrcDisposeLLLazyJIT(comp_ctx->orc_jit); - LLVMShutdown(); - if (comp_ctx->func_ctxes) aot_destroy_func_contexts(comp_ctx->func_ctxes, comp_ctx->func_ctx_count); diff --git a/core/iwasm/compilation/aot_llvm.h b/core/iwasm/compilation/aot_llvm.h index e55b564c6..5cb84d582 100644 --- a/core/iwasm/compilation/aot_llvm.h +++ b/core/iwasm/compilation/aot_llvm.h @@ -408,6 +408,12 @@ typedef struct AOTCompOption { uint32 custom_sections_count; } AOTCompOption, *aot_comp_option_t; +bool +aot_compiler_init(void); + +void +aot_compiler_destroy(void); + AOTCompContext * aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option); diff --git a/core/iwasm/include/aot_export.h b/core/iwasm/include/aot_export.h index b60454398..9dc1441fe 100644 --- a/core/iwasm/include/aot_export.h +++ b/core/iwasm/include/aot_export.h @@ -63,6 +63,12 @@ typedef struct AOTCompOption { uint32_t custom_sections_count; } AOTCompOption, *aot_comp_option_t; +bool +aot_compiler_init(void); + +void +aot_compiler_destroy(void); + aot_comp_context_t aot_create_comp_context(aot_comp_data_t comp_data, aot_comp_option_t option);