From 62fc486c20b128459119318d2d5162d0c67d9e95 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 7 Apr 2023 06:47:24 +0800 Subject: [PATCH] Refine aot compiler check suspend_flags and fix issue of multi-tier jit (#2111) In LLVM AOT/JIT compiler, only need to check the suspend_flags when memory is a shared memory since the shared memory must be enabled for multi-threading, so as not to impact the performance in non-multi-threading memory mode. Also refine the LLVM IRs to check the suspend_flags. And fix an issue of multi-tier jit for multi-threading, the instance of the child thread should be removed from the instance list before it is de-instantiated. --- core/iwasm/compilation/aot_emit_control.c | 36 +++++++++++------------ core/iwasm/interpreter/wasm_runtime.c | 2 +- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/core/iwasm/compilation/aot_emit_control.c b/core/iwasm/compilation/aot_emit_control.c index 68c286f4..8c15d4d1 100644 --- a/core/iwasm/compilation/aot_emit_control.c +++ b/core/iwasm/compilation/aot_emit_control.c @@ -671,9 +671,16 @@ bool check_suspend_flags(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx) { LLVMValueRef terminate_addr, terminate_flags, flag, offset, res; - LLVMBasicBlockRef terminate_check_block, non_terminate_block; + LLVMBasicBlockRef terminate_block, non_terminate_block; AOTFuncType *aot_func_type = func_ctx->aot_func->func_type; - LLVMBasicBlockRef terminate_block; + bool is_shared_memory = + comp_ctx->comp_data->memories[0].memory_flags & 0x02 ? true : false; + + /* Only need to check the suspend flags when memory is shared since + shared memory must be enabled for multi-threading */ + if (!is_shared_memory) { + return true; + } /* Offset of suspend_flags */ offset = I32_FIVE; @@ -701,29 +708,20 @@ check_suspend_flags(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx) will always be loaded from memory rather than register */ LLVMSetVolatile(terminate_flags, true); - CREATE_BLOCK(terminate_check_block, "terminate_check"); - MOVE_BLOCK_AFTER_CURR(terminate_check_block); - - CREATE_BLOCK(non_terminate_block, "non_terminate"); - MOVE_BLOCK_AFTER_CURR(non_terminate_block); - - BUILD_ICMP(LLVMIntSGT, terminate_flags, I32_ZERO, res, "need_terminate"); - BUILD_COND_BR(res, terminate_check_block, non_terminate_block); - - /* Move builder to terminate check block */ - SET_BUILDER_POS(terminate_check_block); - - CREATE_BLOCK(terminate_block, "terminate"); - MOVE_BLOCK_AFTER_CURR(terminate_block); - if (!(flag = LLVMBuildAnd(comp_ctx->builder, terminate_flags, I32_ONE, "termination_flag"))) { aot_set_last_error("llvm build AND failed"); return false; } - BUILD_ICMP(LLVMIntSGT, flag, I32_ZERO, res, "need_terminate"); - BUILD_COND_BR(res, terminate_block, non_terminate_block); + CREATE_BLOCK(non_terminate_block, "non_terminate"); + MOVE_BLOCK_AFTER_CURR(non_terminate_block); + + CREATE_BLOCK(terminate_block, "terminate"); + MOVE_BLOCK_AFTER_CURR(terminate_block); + + BUILD_ICMP(LLVMIntEQ, flag, I32_ZERO, res, "flag_terminate"); + BUILD_COND_BR(res, non_terminate_block, terminate_block); /* Move builder to terminate block */ SET_BUILDER_POS(terminate_block); diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 8130d90a..29365024 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2178,7 +2178,7 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) func_ptrs and fast_jit_func_ptrs of the instance, to avoid accessing the freed memory in the jit backend compilation threads */ - if (!is_sub_inst) { + { WASMModule *module = module_inst->module; WASMModuleInstance *instance_prev = NULL, *instance; os_mutex_lock(&module->instance_list_lock);