Skip to content

Commit 0320ec2

Browse files
committed
Upgrade to ORCv2 APIs
Use LLJIT to do eager compilation Use LLLazyJIT to do lazy compilation
1 parent 5db3412 commit 0320ec2

File tree

11 files changed

+541
-428
lines changed

11 files changed

+541
-428
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ jobs:
185185
$CLASSIC_INTERP_BUILD_OPTIONS,
186186
$FAST_INTERP_BUILD_OPTIONS,
187187
$LLVM_EAGER_JIT_BUILD_OPTIONS,
188-
#$LLVM_LAZY_JIT_BUILD_OPTIONS,
188+
$LLVM_LAZY_JIT_BUILD_OPTIONS,
189189
]
190190
make_options_feature: [
191191
# Features

core/iwasm/aot/aot_loader.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,14 +2898,22 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
28982898
}
28992899

29002900
/* fill in func_ptrs[] */
2901+
LOG_VERBOSE("to fill in func_ptrs ...");
2902+
#if WASM_ENABLE_LAZY_JIT == 0
29012903
bh_print_time("Begin to do eager compilation...");
2904+
#endif
29022905
for (i = 0; i < module->func_count; i++) {
29032906
LLVMErrorRef error;
29042907
LLVMOrcJITTargetAddress func_addr;
29052908
char func_name[32] = { 0 };
29062909

29072910
snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
2911+
#if WASM_ENABLE_LAZY_JIT != 0
2912+
error =
2913+
LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
2914+
#else
29082915
error = LLVMOrcLLJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
2916+
#endif
29092917
if (error != LLVMErrorSuccess) {
29102918
char *err_msg = LLVMGetErrorMessage(error);
29112919
set_error_buf_v(error_buf, error_buf_size,
@@ -2944,8 +2952,13 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
29442952
snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
29452953
comp_data->start_func_index
29462954
- module->import_func_count);
2955+
#if WASM_ENABLE_LAZY_JIT != 0
2956+
if ((error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit,
2957+
&func_addr, func_name))) {
2958+
#else
29472959
if ((error = LLVMOrcLLJITLookup(comp_ctx->orc_jit, &func_addr,
29482960
func_name))) {
2961+
#endif
29492962
char *err_msg = LLVMGetErrorMessage(error);
29502963
set_error_buf_v(error_buf, error_buf_size,
29512964
"failed to compile orc jit function: %s",

core/iwasm/compilation/aot_compiler.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,28 +2674,38 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
26742674
return false;
26752675
}
26762676

2677+
/* Run IR optimization before feeding in ORCJIT and AOT codegen */
26772678
if (comp_ctx->optimize) {
2678-
if (!comp_ctx->is_jit_mode) {
2679-
/* Run passes for AOT indirect mode */
2680-
if (comp_ctx->is_indirect_mode) {
2681-
bh_print_time("Begin to run optimization passes "
2682-
"for indirect mode");
2683-
if (!apply_passes_for_indirect_mode(comp_ctx)) {
2684-
return false;
2685-
}
2679+
/* Run specific passes for AOT indirect mode */
2680+
if (!comp_ctx->is_jit_mode && comp_ctx->is_indirect_mode) {
2681+
bh_print_time("Begin to run optimization passes "
2682+
"for indirect mode");
2683+
if (!apply_passes_for_indirect_mode(comp_ctx)) {
2684+
return false;
26862685
}
2687-
2688-
/* Run passes for both indirect mode and normal mode */
2689-
bh_print_time("Begin to run llvm optimization passes");
2690-
aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
26912686
}
2687+
2688+
/* Run passes for all JIT/AOT mode */
2689+
bh_print_time("Begin to run llvm optimization passes");
2690+
aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
2691+
bh_print_time("Finish llvm optimization passes");
26922692
}
26932693

2694+
#ifdef DUMP_MODULE
2695+
LLVMDumpModule(comp_ctx->module);
2696+
os_printf("\n");
2697+
#endif
2698+
26942699
if (comp_ctx->is_jit_mode) {
26952700
LLVMErrorRef err;
26962701
LLVMOrcJITDylibRef orc_main_dylib;
26972702
LLVMOrcThreadSafeModuleRef orc_thread_safe_module;
2703+
2704+
#if WASM_ENABLE_LAZY_JIT != 0
2705+
orc_main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->orc_jit);
2706+
#else
26982707
orc_main_dylib = LLVMOrcLLJITGetMainJITDylib(comp_ctx->orc_jit);
2708+
#endif
26992709
if (!orc_main_dylib) {
27002710
aot_set_last_error(
27012711
"failed to get orc orc_jit main dynmaic library");
@@ -2709,8 +2719,13 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
27092719
return false;
27102720
}
27112721

2722+
#if WASM_ENABLE_LAZY_JIT != 0
2723+
if ((err = LLVMOrcLLLazyJITAddLLVMIRModule(
2724+
comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
2725+
#else
27122726
if ((err = LLVMOrcLLJITAddLLVMIRModule(
27132727
comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
2728+
#endif
27142729
/* If adding the ThreadSafeModule fails then we need to clean it up
27152730
by ourselves, otherwise the orc orc_jit will manage the memory.
27162731
*/
@@ -2720,10 +2735,6 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
27202735
}
27212736
}
27222737

2723-
#if 0
2724-
LLVMDumpModule(comp_ctx->module);
2725-
os_printf("\n");
2726-
#endif
27272738
return true;
27282739
}
27292740

core/iwasm/compilation/aot_emit_function.c

Lines changed: 0 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -267,130 +267,6 @@ call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
267267
return true;
268268
}
269269

270-
/*TODO: maybe no need to do that*/
271-
static bool
272-
lookup_orcjit_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
273-
LLVMValueRef func_idx, LLVMValueRef *p_func)
274-
{
275-
LLVMBasicBlockRef block_curr, block_resolve_func, block_func_resolved;
276-
LLVMValueRef param_values[3], func, value, func_ptr, cmp, phi;
277-
LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
278-
279-
block_curr = LLVMGetInsertBlock(comp_ctx->builder);
280-
281-
if (!(block_resolve_func = LLVMAppendBasicBlockInContext(
282-
comp_ctx->context, func_ctx->func, "resolve_func"))) {
283-
aot_set_last_error("llvm add basic block failed.");
284-
return false;
285-
}
286-
if (!(block_func_resolved = LLVMAppendBasicBlockInContext(
287-
comp_ctx->context, func_ctx->func, "func_resolved"))) {
288-
aot_set_last_error("llvm add basic block failed.");
289-
return false;
290-
}
291-
LLVMMoveBasicBlockAfter(block_resolve_func, block_curr);
292-
LLVMMoveBasicBlockAfter(block_func_resolved, block_resolve_func);
293-
294-
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_func_resolved);
295-
if (!(phi = LLVMBuildPhi(comp_ctx->builder, INT8_PTR_TYPE, "phi"))) {
296-
aot_set_last_error("llvm build phi failed.");
297-
return false;
298-
}
299-
300-
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
301-
302-
/* Load function pointer */
303-
if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
304-
func_ctx->func_ptrs, &func_idx, 1,
305-
"func_ptr_tmp"))) {
306-
aot_set_last_error("llvm build inbounds gep failed.");
307-
return false;
308-
}
309-
310-
if (!(func = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
311-
"func_ptr"))) {
312-
aot_set_last_error("llvm build load failed.");
313-
return false;
314-
}
315-
316-
/* If func ptr is NULL, call aot_lookup_orcjit_func to resolve it */
317-
if (!(cmp = LLVMBuildIsNull(comp_ctx->builder, func, "cmp"))) {
318-
aot_set_last_error("llvm build is null failed");
319-
return false;
320-
}
321-
322-
/* Create condition br */
323-
if (!LLVMBuildCondBr(comp_ctx->builder, cmp, block_resolve_func,
324-
block_func_resolved)) {
325-
aot_set_last_error("llvm build cond br failed.");
326-
return false;
327-
}
328-
LLVMAddIncoming(phi, &func, &block_curr, 1);
329-
330-
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_resolve_func);
331-
332-
param_types[0] = INT8_PTR_TYPE;
333-
param_types[1] = comp_ctx->aot_inst_type;
334-
param_types[2] = I32_TYPE;
335-
ret_type = INT8_PTR_TYPE;
336-
337-
if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))
338-
|| !(func_ptr_type = LLVMPointerType(func_type, 0))) {
339-
aot_set_last_error("llvm add function type failed.");
340-
return false;
341-
}
342-
343-
if (!(value = I64_CONST((uint64)(uintptr_t)aot_lookup_orcjit_func))
344-
|| !(func = LLVMConstIntToPtr(value, func_ptr_type))) {
345-
aot_set_last_error("create LLVM value failed.");
346-
return false;
347-
}
348-
349-
param_values[0] = I64_CONST((uintptr_t)comp_ctx->orc_jit);
350-
if (!param_values[0]) {
351-
aot_set_last_error("llvm build const failed.");
352-
return false;
353-
}
354-
if (!(param_values[0] =
355-
LLVMConstIntToPtr(param_values[0], INT8_PTR_TYPE))) {
356-
aot_set_last_error("llvm build bit cast failed.");
357-
return false;
358-
}
359-
360-
param_values[1] = func_ctx->aot_inst;
361-
362-
param_values[2] = func_idx;
363-
if (!param_values[2]) {
364-
aot_set_last_error("llvm build const failed.");
365-
return false;
366-
}
367-
368-
/* Call the function */
369-
if (!(func = LLVMBuildCall2(comp_ctx->builder, func_type, func,
370-
param_values, 3, "call_orcjit_lookup"))) {
371-
aot_set_last_error("LLVM build call failed.");
372-
return false;
373-
}
374-
375-
/* Check whether exception was thrown when looking up func */
376-
if (!check_exception_thrown(comp_ctx, func_ctx)) {
377-
return false;
378-
}
379-
380-
block_curr = LLVMGetInsertBlock(comp_ctx->builder);
381-
LLVMAddIncoming(phi, &func, &block_curr, 1);
382-
383-
if (!LLVMBuildBr(comp_ctx->builder, block_func_resolved)) {
384-
aot_set_last_error("llvm build br failed.");
385-
return false;
386-
}
387-
388-
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_func_resolved);
389-
390-
*p_func = phi;
391-
return true;
392-
}
393-
394270
#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
395271
static bool
396272
call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
@@ -909,35 +785,6 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
909785
else {
910786
func = func_ctxes[func_idx - import_func_count]->func;
911787
}
912-
// else {
913-
// LLVMTypeRef func_ptr_type;
914-
// LLVMValueRef func_idx_const = I32_CONST(func_idx);
915-
916-
// if (!func_idx_const) {
917-
// aot_set_last_error("llvm build const failed.");
918-
// goto fail;
919-
// }
920-
921-
// /* For LAZY JIT, each function belongs to its own module,
922-
// we call aot_lookup_orcjit_func to get the func pointer */
923-
// if (!lookup_orcjit_func(comp_ctx, func_ctx, func_idx_const,
924-
// &func)) {
925-
// goto fail;
926-
// }
927-
928-
// if (!(func_ptr_type = LLVMPointerType(
929-
// func_ctxes[func_idx -
930-
// import_func_count]->func_type, 0))) {
931-
// aot_set_last_error("construct func ptr type failed.");
932-
// goto fail;
933-
// }
934-
935-
// if (!(func = LLVMBuildBitCast(comp_ctx->builder, func,
936-
// func_ptr_type, "aot_func"))) {
937-
// aot_set_last_error("llvm bit cast failed.");
938-
// goto fail;
939-
// }
940-
// }
941788
}
942789

943790
aot_func = func_ctxes[func_idx - import_func_count]->aot_func;
@@ -1580,11 +1427,6 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
15801427
goto fail;
15811428
}
15821429

1583-
// /* For LAZY JIT, each function belongs to its own module,
1584-
// we call aot_lookup_orcjit_func to get the func pointer */
1585-
// if (!lookup_orcjit_func(comp_ctx, func_ctx, func_idx, &func_ptr))
1586-
// goto fail;
1587-
15881430
if (!(llvm_func_type =
15891431
LLVMFunctionType(ret_type, param_types, total_param_count, false))
15901432
|| !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {

0 commit comments

Comments
 (0)