Просмотр исходного кода

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.
Wenyong Huang 2 лет назад
Родитель
Сommit
62fc486c20
2 измененных файлов с 17 добавлено и 19 удалено
  1. 16 18
      core/iwasm/compilation/aot_emit_control.c
  2. 1 1
      core/iwasm/interpreter/wasm_runtime.c

+ 16 - 18
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);
+    if (!(flag = LLVMBuildAnd(comp_ctx->builder, terminate_flags, I32_ONE,
+                              "termination_flag"))) {
+        aot_set_last_error("llvm build AND failed");
+        return false;
+    }
 
     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);
+    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);

+ 1 - 1
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);