Refine the wasm-c-api native func call process of aot mode (#640)

This commit is contained in:
Wenyong Huang
2021-05-26 20:57:30 +08:00
committed by GitHub
parent 1b34606940
commit ba922b0d0d
14 changed files with 459 additions and 67 deletions

View File

@ -290,6 +290,7 @@ aot_create_import_funcs(const WASMModule *module)
import_funcs[i].signature = import_func->signature;
import_funcs[i].attachment = import_func->attachment;
import_funcs[i].call_conv_raw = import_func->call_conv_raw;
import_funcs[i].call_conv_wasm_c_api = false;
/* Resolve function type index */
for (j = 0; j < module->type_count; j++)
if (import_func->func_type == module->types[j]) {

View File

@ -148,6 +148,8 @@ typedef struct AOTImportFunc {
/* attachment */
void *attachment;
bool call_conv_raw;
bool call_conv_wasm_c_api;
bool wasm_c_api_with_env;
} AOTImportFunc;
/**

View File

@ -2130,3 +2130,103 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
return true;
}
#if WASM_ENABLE_REF_TYPES != 0
extern void
wasm_set_ref_types_flag(bool enable);
#endif
uint8*
aot_compile_wasm_file(const uint8 *wasm_file_buf, uint32 wasm_file_size,
uint32 opt_level, uint32 size_level,
uint32 *p_aot_file_size)
{
WASMModuleCommon *wasm_module = NULL;
AOTCompData *comp_data = NULL;
AOTCompContext *comp_ctx = NULL;
RuntimeInitArgs init_args;
AOTCompOption option = { 0 };
uint8 *aot_file_buf = NULL;
uint32 aot_file_size;
char error_buf[128];
option.is_jit_mode = false;
option.opt_level = opt_level;
option.size_level = size_level;
option.output_format = AOT_FORMAT_FILE;
/* default value, enable or disable depends on the platform */
option.bounds_checks = 2;
option.enable_simd = true;
option.enable_aux_stack_check = true;
#if WASM_ENABLE_BULK_MEMORY != 0
option.enable_bulk_memory = true;
#endif
#if WASM_ENABLE_THREAD_MGR != 0
option.enable_thread_mgr = true;
#endif
#if WASM_ENABLE_TAIL_CALL != 0
option.enable_tail_call = true;
#endif
#if WASM_ENABLE_SIMD != 0
option.enable_simd = true;
#endif
#if WASM_ENABLE_REF_TYPES != 0
option.enable_ref_types = true;
#endif
#if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
option.enable_aux_stack_frame = true;
#endif
#if WASM_ENABLE_REF_TYPES != 0
wasm_set_ref_types_flag(option.enable_ref_types);
#endif
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = malloc;
init_args.mem_alloc_option.allocator.realloc_func = realloc;
init_args.mem_alloc_option.allocator.free_func = free;
/* load WASM module */
if (!(wasm_module = (WASMModuleCommon*)
wasm_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) {
os_printf("%s\n", error_buf);
aot_set_last_error(error_buf);
return NULL;
}
if (!(comp_data = aot_create_comp_data((WASMModule*)wasm_module))) {
os_printf("%s\n", aot_get_last_error());
goto fail1;
}
if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
os_printf("%s\n", aot_get_last_error());
goto fail2;
}
if (!aot_compile_wasm(comp_ctx)) {
os_printf("%s\n", aot_get_last_error());
goto fail3;
}
if (!(aot_file_buf = aot_emit_aot_file_buf(comp_ctx, comp_data,
&aot_file_size))) {
os_printf("%s\n", aot_get_last_error());
goto fail3;
}
*p_aot_file_size = aot_file_size;
fail3:
/* Destroy compiler context */
aot_destroy_comp_context(comp_ctx);
fail2:
/* Destroy compile data */
aot_destroy_comp_data(comp_data);
fail1:
wasm_runtime_unload(wasm_module);
return aot_file_buf;
}

View File

@ -368,9 +368,19 @@ aot_emit_aot_file(AOTCompContext *comp_ctx,
AOTCompData *comp_data,
const char *file_name);
uint8_t*
aot_emit_aot_file_buf(AOTCompContext *comp_ctx,
AOTCompData *comp_data,
uint32_t *p_aot_file_size);
bool
aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name);
uint8_t*
aot_compile_wasm_file(const uint8_t *wasm_file_buf, uint32_t wasm_file_size,
uint32_t opt_level, uint32_t size_level,
uint32_t *p_aot_file_size);
#ifdef __cplusplus
} /* end of extern "C" */
#endif

View File

@ -2208,20 +2208,17 @@ fail:
return NULL;
}
bool
aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
const char *file_name)
uint8*
aot_emit_aot_file_buf(AOTCompContext *comp_ctx,
AOTCompData *comp_data,
uint32 *p_aot_file_size)
{
AOTObjectData *obj_data = aot_obj_data_create(comp_ctx);
uint8 *aot_file_buf, *buf, *buf_end;
uint32 aot_file_size, offset = 0;
bool ret = false;
FILE *file;
if (!obj_data)
return false;
bh_print_time("Begin to emit AOT file");
return NULL;
aot_file_size = get_aot_file_size(comp_ctx, comp_data, obj_data);
@ -2251,25 +2248,52 @@ aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
goto fail2;
}
/* write buffer to file */
if (!(file = fopen(file_name, "wb"))) {
aot_set_last_error("open or create aot file failed.");
goto fail2;
}
if (!fwrite(aot_file_buf, aot_file_size, 1, file)) {
aot_set_last_error("write to aot file failed.");
goto fail3;
}
*p_aot_file_size = aot_file_size;
ret = true;
fail3:
fclose(file);
aot_obj_data_destroy(obj_data);
return aot_file_buf;
fail2:
wasm_runtime_free(aot_file_buf);
fail1:
aot_obj_data_destroy(obj_data);
return NULL;
}
bool
aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
const char *file_name)
{
uint8 *aot_file_buf;
uint32 aot_file_size;
bool ret = false;
FILE *file;
bh_print_time("Begin to emit AOT file");
if (!(aot_file_buf = aot_emit_aot_file_buf(comp_ctx, comp_data,
&aot_file_size))) {
return false;
}
/* write buffer to file */
if (!(file = fopen(file_name, "wb"))) {
aot_set_last_error("open or create aot file failed.");
goto fail1;
}
if (!fwrite(aot_file_buf, aot_file_size, 1, file)) {
aot_set_last_error("write to aot file failed.");
goto fail2;
}
ret = true;
fail2:
fclose(file);
fail1:
wasm_runtime_free(aot_file_buf);
return ret;
}