Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/compilation_on_android_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ jobs:
$CLASSIC_INTERP_BUILD_OPTIONS,
$FAST_INTERP_BUILD_OPTIONS,
$LLVM_EAGER_JIT_BUILD_OPTIONS,
#$LLVM_LAZY_JIT_BUILD_OPTIONS,
$LLVM_LAZY_JIT_BUILD_OPTIONS,
]
make_options_feature: [
# Features
Expand Down
13 changes: 13 additions & 0 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2898,14 +2898,22 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
}

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

snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
#if WASM_ENABLE_LAZY_JIT != 0
error =
LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
#else
error = LLVMOrcLLJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
#endif
if (error != LLVMErrorSuccess) {
char *err_msg = LLVMGetErrorMessage(error);
set_error_buf_v(error_buf, error_buf_size,
Expand Down Expand Up @@ -2944,8 +2952,13 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
comp_data->start_func_index
- module->import_func_count);
#if WASM_ENABLE_LAZY_JIT != 0
if ((error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit,
&func_addr, func_name))) {
#else
if ((error = LLVMOrcLLJITLookup(comp_ctx->orc_jit, &func_addr,
func_name))) {
#endif
char *err_msg = LLVMGetErrorMessage(error);
set_error_buf_v(error_buf, error_buf_size,
"failed to compile orc jit function: %s",
Expand Down
43 changes: 27 additions & 16 deletions core/iwasm/compilation/aot_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2674,28 +2674,38 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
return false;
}

/* Run IR optimization before feeding in ORCJIT and AOT codegen */
if (comp_ctx->optimize) {
if (!comp_ctx->is_jit_mode) {
/* Run passes for AOT indirect mode */
if (comp_ctx->is_indirect_mode) {
bh_print_time("Begin to run optimization passes "
"for indirect mode");
if (!apply_passes_for_indirect_mode(comp_ctx)) {
return false;
}
/* Run specific passes for AOT indirect mode */
if (!comp_ctx->is_jit_mode && comp_ctx->is_indirect_mode) {
bh_print_time("Begin to run optimization passes "
"for indirect mode");
if (!apply_passes_for_indirect_mode(comp_ctx)) {
return false;
}

/* Run passes for both indirect mode and normal mode */
bh_print_time("Begin to run llvm optimization passes");
aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
}

/* Run passes for all JIT/AOT mode */
bh_print_time("Begin to run llvm optimization passes");
aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
bh_print_time("Finish llvm optimization passes");
}

#ifdef DUMP_MODULE
LLVMDumpModule(comp_ctx->module);
os_printf("\n");
#endif

if (comp_ctx->is_jit_mode) {
LLVMErrorRef err;
LLVMOrcJITDylibRef orc_main_dylib;
LLVMOrcThreadSafeModuleRef orc_thread_safe_module;

#if WASM_ENABLE_LAZY_JIT != 0
orc_main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->orc_jit);
#else
orc_main_dylib = LLVMOrcLLJITGetMainJITDylib(comp_ctx->orc_jit);
#endif
if (!orc_main_dylib) {
aot_set_last_error(
"failed to get orc orc_jit main dynmaic library");
Expand All @@ -2709,8 +2719,13 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
return false;
}

#if WASM_ENABLE_LAZY_JIT != 0
if ((err = LLVMOrcLLLazyJITAddLLVMIRModule(
comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
#else
if ((err = LLVMOrcLLJITAddLLVMIRModule(
comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
#endif
/* If adding the ThreadSafeModule fails then we need to clean it up
by ourselves, otherwise the orc orc_jit will manage the memory.
*/
Expand All @@ -2720,10 +2735,6 @@ aot_compile_wasm(AOTCompContext *comp_ctx)
}
}

#if 0
LLVMDumpModule(comp_ctx->module);
os_printf("\n");
#endif
return true;
}

Expand Down
158 changes: 0 additions & 158 deletions core/iwasm/compilation/aot_emit_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,130 +267,6 @@ call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return true;
}

/*TODO: maybe no need to do that*/
static bool
lookup_orcjit_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef func_idx, LLVMValueRef *p_func)
{
LLVMBasicBlockRef block_curr, block_resolve_func, block_func_resolved;
LLVMValueRef param_values[3], func, value, func_ptr, cmp, phi;
LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;

block_curr = LLVMGetInsertBlock(comp_ctx->builder);

if (!(block_resolve_func = LLVMAppendBasicBlockInContext(
comp_ctx->context, func_ctx->func, "resolve_func"))) {
aot_set_last_error("llvm add basic block failed.");
return false;
}
if (!(block_func_resolved = LLVMAppendBasicBlockInContext(
comp_ctx->context, func_ctx->func, "func_resolved"))) {
aot_set_last_error("llvm add basic block failed.");
return false;
}
LLVMMoveBasicBlockAfter(block_resolve_func, block_curr);
LLVMMoveBasicBlockAfter(block_func_resolved, block_resolve_func);

LLVMPositionBuilderAtEnd(comp_ctx->builder, block_func_resolved);
if (!(phi = LLVMBuildPhi(comp_ctx->builder, INT8_PTR_TYPE, "phi"))) {
aot_set_last_error("llvm build phi failed.");
return false;
}

LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);

/* Load function pointer */
if (!(func_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, OPQ_PTR_TYPE,
func_ctx->func_ptrs, &func_idx, 1,
"func_ptr_tmp"))) {
aot_set_last_error("llvm build inbounds gep failed.");
return false;
}

if (!(func = LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, func_ptr,
"func_ptr"))) {
aot_set_last_error("llvm build load failed.");
return false;
}

/* If func ptr is NULL, call aot_lookup_orcjit_func to resolve it */
if (!(cmp = LLVMBuildIsNull(comp_ctx->builder, func, "cmp"))) {
aot_set_last_error("llvm build is null failed");
return false;
}

/* Create condition br */
if (!LLVMBuildCondBr(comp_ctx->builder, cmp, block_resolve_func,
block_func_resolved)) {
aot_set_last_error("llvm build cond br failed.");
return false;
}
LLVMAddIncoming(phi, &func, &block_curr, 1);

LLVMPositionBuilderAtEnd(comp_ctx->builder, block_resolve_func);

param_types[0] = INT8_PTR_TYPE;
param_types[1] = comp_ctx->aot_inst_type;
param_types[2] = I32_TYPE;
ret_type = INT8_PTR_TYPE;

if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))
|| !(func_ptr_type = LLVMPointerType(func_type, 0))) {
aot_set_last_error("llvm add function type failed.");
return false;
}

if (!(value = I64_CONST((uint64)(uintptr_t)aot_lookup_orcjit_func))
|| !(func = LLVMConstIntToPtr(value, func_ptr_type))) {
aot_set_last_error("create LLVM value failed.");
return false;
}

param_values[0] = I64_CONST((uintptr_t)comp_ctx->orc_jit);
if (!param_values[0]) {
aot_set_last_error("llvm build const failed.");
return false;
}
if (!(param_values[0] =
LLVMConstIntToPtr(param_values[0], INT8_PTR_TYPE))) {
aot_set_last_error("llvm build bit cast failed.");
return false;
}

param_values[1] = func_ctx->aot_inst;

param_values[2] = func_idx;
if (!param_values[2]) {
aot_set_last_error("llvm build const failed.");
return false;
}

/* Call the function */
if (!(func = LLVMBuildCall2(comp_ctx->builder, func_type, func,
param_values, 3, "call_orcjit_lookup"))) {
aot_set_last_error("LLVM build call failed.");
return false;
}

/* Check whether exception was thrown when looking up func */
if (!check_exception_thrown(comp_ctx, func_ctx)) {
return false;
}

block_curr = LLVMGetInsertBlock(comp_ctx->builder);
LLVMAddIncoming(phi, &func, &block_curr, 1);

if (!LLVMBuildBr(comp_ctx->builder, block_func_resolved)) {
aot_set_last_error("llvm build br failed.");
return false;
}

LLVMPositionBuilderAtEnd(comp_ctx->builder, block_func_resolved);

*p_func = phi;
return true;
}

#if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0)
static bool
call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
Expand Down Expand Up @@ -909,35 +785,6 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
else {
func = func_ctxes[func_idx - import_func_count]->func;
}
// else {
// LLVMTypeRef func_ptr_type;
// LLVMValueRef func_idx_const = I32_CONST(func_idx);

// if (!func_idx_const) {
// aot_set_last_error("llvm build const failed.");
// goto fail;
// }

// /* For LAZY JIT, each function belongs to its own module,
// we call aot_lookup_orcjit_func to get the func pointer */
// if (!lookup_orcjit_func(comp_ctx, func_ctx, func_idx_const,
// &func)) {
// goto fail;
// }

// if (!(func_ptr_type = LLVMPointerType(
// func_ctxes[func_idx -
// import_func_count]->func_type, 0))) {
// aot_set_last_error("construct func ptr type failed.");
// goto fail;
// }

// if (!(func = LLVMBuildBitCast(comp_ctx->builder, func,
// func_ptr_type, "aot_func"))) {
// aot_set_last_error("llvm bit cast failed.");
// goto fail;
// }
// }
}

aot_func = func_ctxes[func_idx - import_func_count]->aot_func;
Expand Down Expand Up @@ -1580,11 +1427,6 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
goto fail;
}

// /* For LAZY JIT, each function belongs to its own module,
// we call aot_lookup_orcjit_func to get the func pointer */
// if (!lookup_orcjit_func(comp_ctx, func_ctx, func_idx, &func_ptr))
// goto fail;

if (!(llvm_func_type =
LLVMFunctionType(ret_type, param_types, total_param_count, false))
|| !(llvm_func_ptr_type = LLVMPointerType(llvm_func_type, 0))) {
Expand Down
Loading