Skip to content

Commit 14288f5

Browse files
authored
Implement Multi-tier JIT (bytecodealliance#1774)
Implement 2-level Multi-tier JIT engine: tier-up from Fast JIT to LLVM JIT to get quick cold startup by Fast JIT and better performance by gradually switching to LLVM JIT when the LLVM JIT functions are compiled by the backend threads. Refer to: bytecodealliance#1302
1 parent fb8727b commit 14288f5

21 files changed

+2156
-314
lines changed

build-scripts/config_common.cmake

+17-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ if (NOT WAMR_BUILD_AOT EQUAL 1)
8585
endif ()
8686
endif ()
8787

88+
if (WAMR_BUILD_FAST_JIT EQUAL 1)
89+
if (NOT WAMR_BUILD_LAZY_JIT EQUAL 0)
90+
# Enable Lazy JIT by default
91+
set (WAMR_BUILD_LAZY_JIT 1)
92+
endif ()
93+
endif ()
94+
8895
if (WAMR_BUILD_JIT EQUAL 1)
8996
if (NOT WAMR_BUILD_LAZY_JIT EQUAL 0)
9097
# Enable Lazy JIT by default
@@ -136,7 +143,12 @@ else ()
136143
message (" WAMR AOT disabled")
137144
endif ()
138145
if (WAMR_BUILD_FAST_JIT EQUAL 1)
139-
message (" WAMR Fast JIT enabled")
146+
if (WAMR_BUILD_LAZY_JIT EQUAL 1)
147+
add_definitions("-DWASM_ENABLE_LAZY_JIT=1")
148+
message (" WAMR Fast JIT enabled with Lazy Compilation")
149+
else ()
150+
message (" WAMR Fast JIT enabled with Eager Compilation")
151+
endif ()
140152
else ()
141153
message (" WAMR Fast JIT disabled")
142154
endif ()
@@ -151,6 +163,10 @@ if (WAMR_BUILD_JIT EQUAL 1)
151163
else ()
152164
message (" WAMR LLVM ORC JIT disabled")
153165
endif ()
166+
if (WAMR_BUILD_FAST_JIT EQUAL 1 AND WAMR_BUILD_JIT EQUAL 1
167+
AND WAMR_BUILD_LAZY_JIT EQUAL 1)
168+
message (" Multi-tier JIT enabled")
169+
endif ()
154170
if (WAMR_BUILD_LIBC_BUILTIN EQUAL 1)
155171
message (" Libc builtin enabled")
156172
else ()

core/iwasm/common/wasm_runtime_common.c

+3
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,9 @@ static const char *exception_msgs[] = {
21762176
"wasm auxiliary stack underflow", /* EXCE_AUX_STACK_UNDERFLOW */
21772177
"out of bounds table access", /* EXCE_OUT_OF_BOUNDS_TABLE_ACCESS */
21782178
"wasm operand stack overflow", /* EXCE_OPERAND_STACK_OVERFLOW */
2179+
#if WASM_ENABLE_FAST_JIT != 0
2180+
"failed to compile fast jit function", /* EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC */
2181+
#endif
21792182
"", /* EXCE_ALREADY_THROWN */
21802183
};
21812184
/* clang-format on */

core/iwasm/compilation/aot.h

+4
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ aot_get_imp_tbl_data_slots(const AOTImportTable *tbl, bool is_jit_mode)
309309
#if WASM_ENABLE_MULTI_MODULE != 0
310310
if (is_jit_mode)
311311
return tbl->table_max_size;
312+
#else
313+
(void)is_jit_mode;
312314
#endif
313315
return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
314316
}
@@ -319,6 +321,8 @@ aot_get_tbl_data_slots(const AOTTable *tbl, bool is_jit_mode)
319321
#if WASM_ENABLE_MULTI_MODULE != 0
320322
if (is_jit_mode)
321323
return tbl->table_max_size;
324+
#else
325+
(void)is_jit_mode;
322326
#endif
323327
return tbl->possible_grow ? tbl->table_max_size : tbl->table_init_size;
324328
}

core/iwasm/compilation/aot_emit_function.c

+47-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,53 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
798798
func = func_ctx->func;
799799
}
800800
else {
801-
func = func_ctxes[func_idx - import_func_count]->func;
801+
if (!comp_ctx->is_jit_mode) {
802+
func = func_ctxes[func_idx - import_func_count]->func;
803+
}
804+
else {
805+
#if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
806+
func = func_ctxes[func_idx - import_func_count]->func;
807+
#else
808+
/* JIT tier-up, load func ptr from func_ptrs[func_idx] */
809+
LLVMValueRef func_ptr, func_idx_const;
810+
LLVMTypeRef func_ptr_type;
811+
812+
if (!(func_idx_const = I32_CONST(func_idx))) {
813+
aot_set_last_error("llvm build const failed.");
814+
goto fail;
815+
}
816+
817+
if (!(func_ptr = LLVMBuildInBoundsGEP2(
818+
comp_ctx->builder, OPQ_PTR_TYPE,
819+
func_ctx->func_ptrs, &func_idx_const, 1,
820+
"func_ptr_tmp"))) {
821+
aot_set_last_error("llvm build inbounds gep failed.");
822+
goto fail;
823+
}
824+
825+
if (!(func_ptr =
826+
LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE,
827+
func_ptr, "func_ptr"))) {
828+
aot_set_last_error("llvm build load failed.");
829+
goto fail;
830+
}
831+
832+
if (!(func_ptr_type = LLVMPointerType(
833+
func_ctxes[func_idx - import_func_count]
834+
->func_type,
835+
0))) {
836+
aot_set_last_error("construct func ptr type failed.");
837+
goto fail;
838+
}
839+
840+
if (!(func = LLVMBuildBitCast(comp_ctx->builder, func_ptr,
841+
func_ptr_type,
842+
"indirect_func"))) {
843+
aot_set_last_error("llvm build bit cast failed.");
844+
goto fail;
845+
}
846+
#endif /* end of !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0) */
847+
}
802848
}
803849
}
804850

0 commit comments

Comments
 (0)