Skip to content

Commit

Permalink
Merge pull request #3925 from bytecodealliance/main
Browse files Browse the repository at this point in the history
Merge branch main into dev/simd_for_interp
  • Loading branch information
wenyongh authored Nov 23, 2024
2 parents c3601cc + 62aca17 commit 7b704e4
Show file tree
Hide file tree
Showing 27 changed files with 340 additions and 157 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3.26.13
uses: github/codeql-action/init@v3.27.4
with:
languages: ${{ matrix.language }}

Expand All @@ -70,7 +70,7 @@ jobs:
- run: |
./.github/scripts/codeql_buildscript.sh
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3.26.13
uses: github/codeql-action/analyze@v3.27.4
with:
category: "/language:${{matrix.language}}"
upload: false
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif

- name: Upload CodeQL results to code scanning
uses: github/codeql-action/upload-sarif@v3.26.13
uses: github/codeql-action/upload-sarif@v3.27.4
with:
sarif_file: ${{ steps.step1.outputs.sarif-output }}
category: "/language:${{matrix.language}}"
Expand Down
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 @@ -828,7 +828,7 @@ jobs:
run: |
mkdir build
cd build
cmake .. -DWAMR_BUILD_DEBUG_INTERP=1
cmake .. -DWAMR_BUILD_DEBUG_INTERP=1 -DWAMR_BUILD_REF_TYPES=1
make
working-directory: product-mini/platforms/linux

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/supply_chain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ jobs:

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@af56b044b5d41c317aef5d19920b3183cb4fbbec # v2.2.4
uses: github/codeql-action/upload-sarif@a1695c562bbfa68dc5ab58c9b5e9f616b52bf5be # v2.2.4
with:
sarif_file: results.sarif
20 changes: 18 additions & 2 deletions build-scripts/build_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,27 @@ def build_llvm(llvm_dir, platform, backends, projects, use_clang=False, extra_fl
"default": [],
}

experimental_backends = ["ARC", "Xtensa"]
normal_backends = [s for s in backends if s not in experimental_backends]

LLVM_TARGETS_TO_BUILD = [
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(backends) + '"'
if backends
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(normal_backends) + '"'
if normal_backends
else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"'
]

# if not on ARC platform, but want to add expeirmental backend ARC as target
if platform != "ARC" and "ARC" in backends:
LLVM_TARGETS_TO_BUILD.extend(
LLVM_EXTRA_COMPILE_OPTIONS["arc"]
)

if platform != "Xtensa" and "Xtensa" in backends:
print(
"Currently it's not supported to build Xtensa backend on non-Xtensa platform"
)
return None

LLVM_PROJECTS_TO_BUILD = [
'-DLLVM_ENABLE_PROJECTS:STRING="' + ";".join(projects) + '"' if projects else ""
]
Expand Down Expand Up @@ -240,6 +255,7 @@ def main():
"X86",
"Xtensa",
],
default=[],
help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'",
)
parser.add_argument(
Expand Down
2 changes: 1 addition & 1 deletion core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
#endif

#define AOT_MAGIC_NUMBER 0x746f6100
#define AOT_CURRENT_VERSION 3
#define AOT_CURRENT_VERSION 4

#ifndef WASM_ENABLE_JIT
#define WASM_ENABLE_JIT 0
Expand Down
15 changes: 13 additions & 2 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,13 @@ loader_mmap(uint32 size, bool prot_exec, char *error_buf, uint32 error_buf_size)
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_RISCV64_LP64D) \
|| defined(BUILD_TARGET_RISCV64_LP64)
#ifndef __APPLE__
#if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX)
/* The mmapped AOT data and code in 64-bit targets had better be in
range 0 to 2G, or aot loader may fail to apply some relocations,
e.g., R_X86_64_32/R_X86_64_32S/R_X86_64_PC32/R_RISCV_32.
We try to mmap with MMAP_MAP_32BIT flag first, and if fails, mmap
again without the flag. */
/* sgx_tprotect_rsrv_mem() and sgx_alloc_rsrv_mem() will ignore flags */
map_flags = MMAP_MAP_32BIT;
if ((mem = os_mmap(NULL, size, map_prot, map_flags,
os_get_invalid_handle()))) {
Expand Down Expand Up @@ -4235,6 +4236,16 @@ create_sections(AOTModule *module, const uint8 *buf, uint32 size,
return false;
}

static bool
aot_compatible_version(uint32 version)
{
/*
* refer to "AoT-compiled module compatibility among WAMR versions" in
* ./doc/biuld_wasm_app.md
*/
return version == 4 || version == 3;
}

static bool
load(const uint8 *buf, uint32 size, AOTModule *module,
bool wasm_binary_freeable, bool no_resolve, char *error_buf,
Expand All @@ -4253,7 +4264,7 @@ load(const uint8 *buf, uint32 size, AOTModule *module,
}

read_uint32(p, p_end, version);
if (version != AOT_CURRENT_VERSION) {
if (!aot_compatible_version(version)) {
set_error_buf(error_buf, error_buf_size, "unknown binary version");
return false;
}
Expand Down
86 changes: 75 additions & 11 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,11 @@ aot_get_default_memory(AOTModuleInstance *module_inst)
}

AOTMemoryInstance *
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index)
aot_get_memory_with_idx(AOTModuleInstance *module_inst, uint32 mem_idx)
{
if ((index >= module_inst->memory_count) || !module_inst->memories)
if ((mem_idx >= module_inst->memory_count) || !module_inst->memories)
return NULL;
return module_inst->memories[index];
return module_inst->memories[mem_idx];
}

static bool
Expand Down Expand Up @@ -1282,21 +1282,78 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
return true;
}

static int
cmp_export_func_map(const void *a, const void *b)
{
uint32 func_idx1 = ((const ExportFuncMap *)a)->func_idx;
uint32 func_idx2 = ((const ExportFuncMap *)b)->func_idx;
return func_idx1 < func_idx2 ? -1 : (func_idx1 > func_idx2 ? 1 : 0);
}

AOTFunctionInstance *
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
aot_lookup_function_with_idx(AOTModuleInstance *module_inst, uint32 func_idx)
{
AOTModule *module = (AOTModule *)module_inst->module;
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
AOTFunctionInstance *export_funcs =
(AOTFunctionInstance *)module_inst->export_functions;
AOTFunctionInstance *func_inst = NULL;
ExportFuncMap *export_func_maps, *export_func_map, key;
uint64 size;
uint32 i;

/* export functions are pre-instantiated */
for (i = 0; i < module_inst->export_func_count; i++) {
if (export_funcs[i].func_index == func_idx)
return &export_funcs[i];
if (module_inst->export_func_count == 0)
return NULL;

exception_lock(module_inst);

/* create the func_idx to export_idx maps if it hasn't been created */
if (!extra->export_func_maps) {
size = sizeof(ExportFuncMap) * (uint64)module_inst->export_func_count;
if (!(export_func_maps = extra->export_func_maps =
runtime_malloc(size, NULL, 0))) {
/* allocate memory failed, lookup the export function one by one */
for (i = 0; i < module_inst->export_func_count; i++) {
if (export_funcs[i].func_index == func_idx) {
func_inst = &export_funcs[i];
break;
}
}
goto unlock_and_return;
}

for (i = 0; i < module_inst->export_func_count; i++) {
export_func_maps[i].func_idx = export_funcs[i].func_index;
export_func_maps[i].export_idx = i;
}

qsort(export_func_maps, module_inst->export_func_count,
sizeof(ExportFuncMap), cmp_export_func_map);
}

/* lookup the map to get the export_idx of the func_idx */
key.func_idx = func_idx;
export_func_map =
bsearch(&key, extra->export_func_maps, module_inst->export_func_count,
sizeof(ExportFuncMap), cmp_export_func_map);
if (export_func_map)
func_inst = &export_funcs[export_func_map->export_idx];

unlock_and_return:
exception_unlock(module_inst);
return func_inst;
}

AOTFunctionInstance *
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
{
AOTModule *module = (AOTModule *)module_inst->module;
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
AOTFunctionInstance *func_inst;

/* lookup from export functions first */
if ((func_inst = aot_lookup_function_with_idx(module_inst, func_idx)))
return func_inst;

exception_lock(module_inst);

/* allocate functions array if needed */
Expand Down Expand Up @@ -1728,7 +1785,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
bool ret = false;
#endif

/* Check heap size */
/* Align and validate heap size */
heap_size = align_uint(heap_size, 8);
if (heap_size > APP_HEAP_SIZE_MAX)
heap_size = APP_HEAP_SIZE_MAX;
Expand Down Expand Up @@ -1944,7 +2001,11 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
AOTTableInstance *table_inst;
table_elem_type_t *table_data;

table = &module->tables[i];
/* bypass imported table since AOTImportTable doesn't have init_expr */
if (i < module->import_table_count)
continue;

table = &module->tables[i - module->import_table_count];
bh_assert(table);

if (table->init_expr.init_expr_type == INIT_EXPR_NONE) {
Expand Down Expand Up @@ -2168,6 +2229,9 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
if (module_inst->export_functions)
wasm_runtime_free(module_inst->export_functions);

if (extra->export_func_maps)
wasm_runtime_free(extra->export_func_maps);

#if WASM_ENABLE_MULTI_MEMORY != 0
if (module_inst->export_memories)
wasm_runtime_free(module_inst->export_memories);
Expand Down
25 changes: 23 additions & 2 deletions core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ typedef struct AOTFunctionInstance {
} u;
} AOTFunctionInstance;

/* Map of a function index to the element ith in
the export functions array */
typedef struct ExportFuncMap {
uint32 func_idx;
uint32 export_idx;
} ExportFuncMap;

typedef struct AOTModuleInstanceExtra {
DefPointer(const uint32 *, stack_sizes);
/*
Expand All @@ -120,6 +127,13 @@ typedef struct AOTModuleInstanceExtra {
MemBound shared_heap_start_off;

WASMModuleInstanceExtraCommon common;

/**
* maps of func indexes to export func indexes, which
* is sorted by func index for a quick lookup and is
* created only when first time used.
*/
ExportFuncMap *export_func_maps;
AOTFunctionInstance **functions;
uint32 function_count;
#if WASM_ENABLE_MULTI_MODULE != 0
Expand Down Expand Up @@ -556,14 +570,21 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
AOTFunctionInstance *
aot_lookup_function(const AOTModuleInstance *module_inst, const char *name);

/**
* Lookup an exported function in the AOT module instance with
* the function index.
*/
AOTFunctionInstance *
aot_lookup_function_with_idx(AOTModuleInstance *module_inst, uint32 func_idx);

AOTMemoryInstance *
aot_lookup_memory(AOTModuleInstance *module_inst, char const *name);

AOTMemoryInstance *
aot_get_default_memory(AOTModuleInstance *module_inst);

AOTMemoryInstance *
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index);
aot_get_memory_with_idx(AOTModuleInstance *module_inst, uint32 mem_idx);

/**
* Get a function in the AOT module instance.
Expand All @@ -574,7 +595,7 @@ aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index);
* @return the function instance found
*/
AOTFunctionInstance *
aot_get_function_instance(AOTModuleInstance *module_inst, uint32_t func_idx);
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx);

/**
* Call the given AOT function of a AOT module instance with
Expand Down
3 changes: 2 additions & 1 deletion core/iwasm/common/wasm_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
bool ret, is_import_func = true, is_memory64 = false;
#if WASM_ENABLE_MEMORY64 != 0
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
if (wasm_module_inst->memory_count > 0)
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
#endif

exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
Expand Down
17 changes: 2 additions & 15 deletions core/iwasm/common/wasm_c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -3383,21 +3383,8 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
if (!(func_comm_rt = func->func_comm_rt)) {
AOTModuleInstance *inst_aot =
(AOTModuleInstance *)func->inst_comm_rt;
AOTModule *module_aot = (AOTModule *)inst_aot->module;
uint32 export_i = 0, export_func_j = 0;

for (; export_i < module_aot->export_count; ++export_i) {
AOTExport *export = module_aot->exports + export_i;
if (export->kind == EXPORT_KIND_FUNC) {
if (export->index == func->func_idx_rt) {
func_comm_rt =
aot_lookup_function(inst_aot, export->name);
((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
break;
}
export_func_j++;
}
}
func_comm_rt = ((wasm_func_t *)func)->func_comm_rt =
aot_lookup_function_with_idx(inst_aot, func->func_idx_rt);
}
#endif
}
Expand Down
Loading

0 comments on commit 7b704e4

Please sign in to comment.