Skip to content

Commit

Permalink
Add multi-module support and related feature flag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lum1n0us committed Nov 11, 2024
1 parent a9e7954 commit 79ce3f6
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 19 deletions.
10 changes: 9 additions & 1 deletion core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,14 @@ check_feature_flags(char *error_buf, uint32 error_buf_size,
}
#endif

#if WASM_ENABLE_MULTI_MODULE == 0
if (feature_flags & WASM_FEATURE_MULTI_MODULE) {
set_error_buf(error_buf, error_buf_size,
"multi-module is not enabled in this build");
return false;
}
#endif

return true;
}

Expand All @@ -529,7 +537,7 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end,
AOTModule *module, char *error_buf,
uint32 error_buf_size)
{
AOTTargetInfo target_info;
AOTTargetInfo target_info = { 0 };
const uint8 *p = buf, *p_end = buf_end;
bool is_target_little_endian, is_target_64_bit;

Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,8 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
== INIT_EXPR_TYPE_REFNULL_CONST);
#else
bh_assert(table_seg->offset.init_expr_type
== (tbl_inst->is_table64 ? INIT_EXPR_TYPE_I64_CONST
: INIT_EXPR_TYPE_I32_CONST)
== (table->is_table64 ? INIT_EXPR_TYPE_I64_CONST
: INIT_EXPR_TYPE_I32_CONST)
|| table_seg->offset.init_expr_type
== INIT_EXPR_TYPE_GET_GLOBAL);
#endif
Expand Down
1 change: 1 addition & 0 deletions core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern "C" {
* and not at the beginning of each function call */
#define WASM_FEATURE_FRAME_PER_FUNCTION (1 << 12)
#define WASM_FEATURE_FRAME_NO_FUNC_IDX (1 << 13)
#define WASM_FEATURE_MULTI_MODULE (1 << 14)

typedef enum AOTSectionType {
AOT_SECTION_TYPE_TARGET_INFO = 0,
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/compilation/aot_emit_aot_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -4604,6 +4604,9 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
if (!comp_ctx->call_stack_features.func_idx) {
obj_data->target_info.feature_flags |= WASM_FEATURE_FRAME_NO_FUNC_IDX;
}
if (comp_ctx->enable_multi_module) {
obj_data->target_info.feature_flags |= WASM_FEATURE_MULTI_MODULE;
}

bh_print_time("Begin to resolve object file info");

Expand Down
32 changes: 17 additions & 15 deletions core/iwasm/compilation/aot_emit_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ get_tbl_inst_offset(const AOTCompContext *comp_ctx,
: comp_ctx->comp_data->global_data_size_32bit);

uint64 i = 0;
#if WASM_ENABLE_MULTI_MODULE != 0
AOTImportTable *imp_tbls = comp_ctx->comp_data->import_tables;
#endif
while (i < tbl_idx && i < comp_ctx->comp_data->import_table_count) {
offset += offsetof(AOTTableInstance, elems);
/* avoid loading from current AOTTableInstance */
#if WASM_ENABLE_MULTI_MODULE != 0
offset += (uint64)comp_ctx->pointer_size
* aot_get_tbl_data_slots(&(imp_tbls + i)->table_type,
comp_ctx->is_jit_mode);
#else
/* there is only one pointer for imported elem */
offset += (uint64)comp_ctx->pointer_size;
#endif
if (comp_ctx->enable_multi_module) {
AOTImportTable *imp_tbls = comp_ctx->comp_data->import_tables;
/* avoid loading from current AOTTableInstance */
offset += (uint64)comp_ctx->pointer_size
* aot_get_tbl_data_slots(&(imp_tbls + i)->table_type,
comp_ctx->is_jit_mode);
}
else {
/* there is only one pointer for imported elem */
offset += (uint64)comp_ctx->pointer_size;
}

++i;
}

Expand Down Expand Up @@ -161,7 +161,7 @@ aot_compile_get_table_elem_base(AOTCompContext *comp_ctx,
* for local table,
* table_elem_base = (*(uint8*)WASMModuleInstance) + offset
*
* if for import table, if WASM_ENABLE_MULTI_MODULE == 0
* if for import table, if enable_multi_module == true
* table_elem_base = (*(uint8*)WASMModuleInstance) + offset
* table_elem_base = *(uintptr_t*)(table_elem_base)
*/
Expand All @@ -173,7 +173,10 @@ aot_compile_get_table_elem_base(AOTCompContext *comp_ctx,
goto fail;
}

#if WASM_ENABLE_MULTI_MODULE == 0
if (comp_ctx->enable_multi_module) {
return table_elem_base;
}

if (table_index < comp_ctx->comp_data->import_table_count) {
table_elem_base =
LLVMBuildIntToPtr(comp_ctx->builder, table_elem_base,
Expand All @@ -199,7 +202,6 @@ aot_compile_get_table_elem_base(AOTCompContext *comp_ctx,
goto fail;
}
}
#endif

return table_elem_base;
fail:
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/compilation/aot_llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2695,6 +2695,9 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
if (option->enable_shared_heap)
comp_ctx->enable_shared_heap = true;

if (option->enable_multi_module)
comp_ctx->enable_multi_module = true;

comp_ctx->opt_level = option->opt_level;
comp_ctx->size_level = option->size_level;

Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/compilation/aot_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ typedef struct AOTCompContext {

bool enable_shared_heap;

bool enable_multi_module;

uint32 opt_level;
uint32 size_level;

Expand Down
1 change: 1 addition & 0 deletions core/iwasm/include/aot_comp_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef struct AOTCompOption {
bool enable_stack_estimation;
bool quick_invoke_c_api_import;
bool enable_shared_heap;
bool enable_multi_module;
char *use_prof_file;
uint32_t opt_level;
uint32_t size_level;
Expand Down
3 changes: 3 additions & 0 deletions tests/wamr-test-suites/spec-test-script/runtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,9 @@ def compile_wasm_to_aot(wasm_tempfile, aot_tempfile, runner, opts, r, output = '
cmd.append("--format=object")
elif output == 'ir':
cmd.append("--format=llvmir-opt")

if opts.multi_module:
cmd.append("--enable-multi-module")

# disable llvm link time optimization as it might convert
# code of tail call into code of dead loop, and stack overflow
Expand Down
10 changes: 9 additions & 1 deletion tests/wamr-test-suites/test_wamr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,19 @@ function build_wamrc()
fi

echo "Build wamrc for spec test under aot compile type"

local EXTRA_COMPILE_FLAGS=""
EXTRA_COMPILE_FLAGS+=" -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE}"

if [[ ${ENABLE_DEBUG_VERSION} == 1 ]]; then
EXTRA_COMPILE_FLAGS+=" -DCMAKE_BUILD_TYPE=Debug"
fi

cd ${WAMR_DIR}/wamr-compiler \
&& ./${BUILD_LLVM_SH} \
&& if [ -d build ]; then rm -r build/*; else mkdir build; fi \
&& cd build \
&& cmake .. -DCOLLECT_CODE_COVERAGE=${COLLECT_CODE_COVERAGE} \
&& cmake ${EXTRA_COMPILE_FLAGS} .. \
&& make -j 4
}

Expand Down
5 changes: 5 additions & 0 deletions wamr-compiler/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ print_help()
#endif
printf(" --mllvm=<option> Add the LLVM command line option\n");
printf(" --enable-shared-heap Enable shared heap feature\n");
printf(" --enable-multi-module Enable multi-module(loader linking) feature. By default, it is \n");
printf(" instantiation linking\n");
printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
printf(" --version Show version information\n");
printf("Examples: wamrc -o test.aot test.wasm\n");
Expand Down Expand Up @@ -651,6 +653,9 @@ main(int argc, char *argv[])
else if (!strcmp(argv[0], "--enable-shared-heap")) {
option.enable_shared_heap = true;
}
else if (!strcmp(argv[0], "--enable-multi-module")) {
option.enable_multi_module = true;
}
else if (!strcmp(argv[0], "--version")) {
uint32 major, minor, patch;
wasm_runtime_get_version(&major, &minor, &patch);
Expand Down

0 comments on commit 79ce3f6

Please sign in to comment.