Skip to content

Commit 7b704e4

Browse files
authored
Merge pull request #3925 from bytecodealliance/main
Merge branch main into dev/simd_for_interp
2 parents c3601cc + 62aca17 commit 7b704e4

27 files changed

Lines changed: 340 additions & 157 deletions

File tree

.github/workflows/codeql.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353

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

@@ -70,7 +70,7 @@ jobs:
7070
- run: |
7171
./.github/scripts/codeql_buildscript.sh
7272
- name: Perform CodeQL Analysis
73-
uses: github/codeql-action/analyze@v3.26.13
73+
uses: github/codeql-action/analyze@v3.27.4
7474
with:
7575
category: "/language:${{matrix.language}}"
7676
upload: false
@@ -99,7 +99,7 @@ jobs:
9999
output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif
100100

101101
- name: Upload CodeQL results to code scanning
102-
uses: github/codeql-action/upload-sarif@v3.26.13
102+
uses: github/codeql-action/upload-sarif@v3.27.4
103103
with:
104104
sarif_file: ${{ steps.step1.outputs.sarif-output }}
105105
category: "/language:${{matrix.language}}"

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ jobs:
828828
run: |
829829
mkdir build
830830
cd build
831-
cmake .. -DWAMR_BUILD_DEBUG_INTERP=1
831+
cmake .. -DWAMR_BUILD_DEBUG_INTERP=1 -DWAMR_BUILD_REF_TYPES=1
832832
make
833833
working-directory: product-mini/platforms/linux
834834

.github/workflows/supply_chain.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ jobs:
6060

6161
# Upload the results to GitHub's code scanning dashboard.
6262
- name: "Upload to code-scanning"
63-
uses: github/codeql-action/upload-sarif@af56b044b5d41c317aef5d19920b3183cb4fbbec # v2.2.4
63+
uses: github/codeql-action/upload-sarif@a1695c562bbfa68dc5ab58c9b5e9f616b52bf5be # v2.2.4
6464
with:
6565
sarif_file: results.sarif

build-scripts/build_llvm.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,27 @@ def build_llvm(llvm_dir, platform, backends, projects, use_clang=False, extra_fl
102102
"default": [],
103103
}
104104

105+
experimental_backends = ["ARC", "Xtensa"]
106+
normal_backends = [s for s in backends if s not in experimental_backends]
107+
105108
LLVM_TARGETS_TO_BUILD = [
106-
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(backends) + '"'
107-
if backends
109+
'-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(normal_backends) + '"'
110+
if normal_backends
108111
else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"'
109112
]
110113

114+
# if not on ARC platform, but want to add expeirmental backend ARC as target
115+
if platform != "ARC" and "ARC" in backends:
116+
LLVM_TARGETS_TO_BUILD.extend(
117+
LLVM_EXTRA_COMPILE_OPTIONS["arc"]
118+
)
119+
120+
if platform != "Xtensa" and "Xtensa" in backends:
121+
print(
122+
"Currently it's not supported to build Xtensa backend on non-Xtensa platform"
123+
)
124+
return None
125+
111126
LLVM_PROJECTS_TO_BUILD = [
112127
'-DLLVM_ENABLE_PROJECTS:STRING="' + ";".join(projects) + '"' if projects else ""
113128
]
@@ -240,6 +255,7 @@ def main():
240255
"X86",
241256
"Xtensa",
242257
],
258+
default=[],
243259
help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'",
244260
)
245261
parser.add_argument(

core/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
#endif
8585

8686
#define AOT_MAGIC_NUMBER 0x746f6100
87-
#define AOT_CURRENT_VERSION 3
87+
#define AOT_CURRENT_VERSION 4
8888

8989
#ifndef WASM_ENABLE_JIT
9090
#define WASM_ENABLE_JIT 0

core/iwasm/aot/aot_loader.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,13 @@ loader_mmap(uint32 size, bool prot_exec, char *error_buf, uint32 error_buf_size)
304304
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
305305
|| defined(BUILD_TARGET_RISCV64_LP64D) \
306306
|| defined(BUILD_TARGET_RISCV64_LP64)
307-
#ifndef __APPLE__
307+
#if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX)
308308
/* The mmapped AOT data and code in 64-bit targets had better be in
309309
range 0 to 2G, or aot loader may fail to apply some relocations,
310310
e.g., R_X86_64_32/R_X86_64_32S/R_X86_64_PC32/R_RISCV_32.
311311
We try to mmap with MMAP_MAP_32BIT flag first, and if fails, mmap
312312
again without the flag. */
313+
/* sgx_tprotect_rsrv_mem() and sgx_alloc_rsrv_mem() will ignore flags */
313314
map_flags = MMAP_MAP_32BIT;
314315
if ((mem = os_mmap(NULL, size, map_prot, map_flags,
315316
os_get_invalid_handle()))) {
@@ -4235,6 +4236,16 @@ create_sections(AOTModule *module, const uint8 *buf, uint32 size,
42354236
return false;
42364237
}
42374238

4239+
static bool
4240+
aot_compatible_version(uint32 version)
4241+
{
4242+
/*
4243+
* refer to "AoT-compiled module compatibility among WAMR versions" in
4244+
* ./doc/biuld_wasm_app.md
4245+
*/
4246+
return version == 4 || version == 3;
4247+
}
4248+
42384249
static bool
42394250
load(const uint8 *buf, uint32 size, AOTModule *module,
42404251
bool wasm_binary_freeable, bool no_resolve, char *error_buf,
@@ -4253,7 +4264,7 @@ load(const uint8 *buf, uint32 size, AOTModule *module,
42534264
}
42544265

42554266
read_uint32(p, p_end, version);
4256-
if (version != AOT_CURRENT_VERSION) {
4267+
if (!aot_compatible_version(version)) {
42574268
set_error_buf(error_buf, error_buf_size, "unknown binary version");
42584269
return false;
42594270
}

core/iwasm/aot/aot_runtime.c

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,11 +1096,11 @@ aot_get_default_memory(AOTModuleInstance *module_inst)
10961096
}
10971097

10981098
AOTMemoryInstance *
1099-
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index)
1099+
aot_get_memory_with_idx(AOTModuleInstance *module_inst, uint32 mem_idx)
11001100
{
1101-
if ((index >= module_inst->memory_count) || !module_inst->memories)
1101+
if ((mem_idx >= module_inst->memory_count) || !module_inst->memories)
11021102
return NULL;
1103-
return module_inst->memories[index];
1103+
return module_inst->memories[mem_idx];
11041104
}
11051105

11061106
static bool
@@ -1282,21 +1282,78 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
12821282
return true;
12831283
}
12841284

1285+
static int
1286+
cmp_export_func_map(const void *a, const void *b)
1287+
{
1288+
uint32 func_idx1 = ((const ExportFuncMap *)a)->func_idx;
1289+
uint32 func_idx2 = ((const ExportFuncMap *)b)->func_idx;
1290+
return func_idx1 < func_idx2 ? -1 : (func_idx1 > func_idx2 ? 1 : 0);
1291+
}
1292+
12851293
AOTFunctionInstance *
1286-
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
1294+
aot_lookup_function_with_idx(AOTModuleInstance *module_inst, uint32 func_idx)
12871295
{
1288-
AOTModule *module = (AOTModule *)module_inst->module;
12891296
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
12901297
AOTFunctionInstance *export_funcs =
12911298
(AOTFunctionInstance *)module_inst->export_functions;
1299+
AOTFunctionInstance *func_inst = NULL;
1300+
ExportFuncMap *export_func_maps, *export_func_map, key;
1301+
uint64 size;
12921302
uint32 i;
12931303

1294-
/* export functions are pre-instantiated */
1295-
for (i = 0; i < module_inst->export_func_count; i++) {
1296-
if (export_funcs[i].func_index == func_idx)
1297-
return &export_funcs[i];
1304+
if (module_inst->export_func_count == 0)
1305+
return NULL;
1306+
1307+
exception_lock(module_inst);
1308+
1309+
/* create the func_idx to export_idx maps if it hasn't been created */
1310+
if (!extra->export_func_maps) {
1311+
size = sizeof(ExportFuncMap) * (uint64)module_inst->export_func_count;
1312+
if (!(export_func_maps = extra->export_func_maps =
1313+
runtime_malloc(size, NULL, 0))) {
1314+
/* allocate memory failed, lookup the export function one by one */
1315+
for (i = 0; i < module_inst->export_func_count; i++) {
1316+
if (export_funcs[i].func_index == func_idx) {
1317+
func_inst = &export_funcs[i];
1318+
break;
1319+
}
1320+
}
1321+
goto unlock_and_return;
1322+
}
1323+
1324+
for (i = 0; i < module_inst->export_func_count; i++) {
1325+
export_func_maps[i].func_idx = export_funcs[i].func_index;
1326+
export_func_maps[i].export_idx = i;
1327+
}
1328+
1329+
qsort(export_func_maps, module_inst->export_func_count,
1330+
sizeof(ExportFuncMap), cmp_export_func_map);
12981331
}
12991332

1333+
/* lookup the map to get the export_idx of the func_idx */
1334+
key.func_idx = func_idx;
1335+
export_func_map =
1336+
bsearch(&key, extra->export_func_maps, module_inst->export_func_count,
1337+
sizeof(ExportFuncMap), cmp_export_func_map);
1338+
if (export_func_map)
1339+
func_inst = &export_funcs[export_func_map->export_idx];
1340+
1341+
unlock_and_return:
1342+
exception_unlock(module_inst);
1343+
return func_inst;
1344+
}
1345+
1346+
AOTFunctionInstance *
1347+
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx)
1348+
{
1349+
AOTModule *module = (AOTModule *)module_inst->module;
1350+
AOTModuleInstanceExtra *extra = (AOTModuleInstanceExtra *)module_inst->e;
1351+
AOTFunctionInstance *func_inst;
1352+
1353+
/* lookup from export functions first */
1354+
if ((func_inst = aot_lookup_function_with_idx(module_inst, func_idx)))
1355+
return func_inst;
1356+
13001357
exception_lock(module_inst);
13011358

13021359
/* allocate functions array if needed */
@@ -1728,7 +1785,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
17281785
bool ret = false;
17291786
#endif
17301787

1731-
/* Check heap size */
1788+
/* Align and validate heap size */
17321789
heap_size = align_uint(heap_size, 8);
17331790
if (heap_size > APP_HEAP_SIZE_MAX)
17341791
heap_size = APP_HEAP_SIZE_MAX;
@@ -1944,7 +2001,11 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
19442001
AOTTableInstance *table_inst;
19452002
table_elem_type_t *table_data;
19462003

1947-
table = &module->tables[i];
2004+
/* bypass imported table since AOTImportTable doesn't have init_expr */
2005+
if (i < module->import_table_count)
2006+
continue;
2007+
2008+
table = &module->tables[i - module->import_table_count];
19482009
bh_assert(table);
19492010

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

2232+
if (extra->export_func_maps)
2233+
wasm_runtime_free(extra->export_func_maps);
2234+
21712235
#if WASM_ENABLE_MULTI_MEMORY != 0
21722236
if (module_inst->export_memories)
21732237
wasm_runtime_free(module_inst->export_memories);

core/iwasm/aot/aot_runtime.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ typedef struct AOTFunctionInstance {
109109
} u;
110110
} AOTFunctionInstance;
111111

112+
/* Map of a function index to the element ith in
113+
the export functions array */
114+
typedef struct ExportFuncMap {
115+
uint32 func_idx;
116+
uint32 export_idx;
117+
} ExportFuncMap;
118+
112119
typedef struct AOTModuleInstanceExtra {
113120
DefPointer(const uint32 *, stack_sizes);
114121
/*
@@ -120,6 +127,13 @@ typedef struct AOTModuleInstanceExtra {
120127
MemBound shared_heap_start_off;
121128

122129
WASMModuleInstanceExtraCommon common;
130+
131+
/**
132+
* maps of func indexes to export func indexes, which
133+
* is sorted by func index for a quick lookup and is
134+
* created only when first time used.
135+
*/
136+
ExportFuncMap *export_func_maps;
123137
AOTFunctionInstance **functions;
124138
uint32 function_count;
125139
#if WASM_ENABLE_MULTI_MODULE != 0
@@ -556,14 +570,21 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst);
556570
AOTFunctionInstance *
557571
aot_lookup_function(const AOTModuleInstance *module_inst, const char *name);
558572

573+
/**
574+
* Lookup an exported function in the AOT module instance with
575+
* the function index.
576+
*/
577+
AOTFunctionInstance *
578+
aot_lookup_function_with_idx(AOTModuleInstance *module_inst, uint32 func_idx);
579+
559580
AOTMemoryInstance *
560581
aot_lookup_memory(AOTModuleInstance *module_inst, char const *name);
561582

562583
AOTMemoryInstance *
563584
aot_get_default_memory(AOTModuleInstance *module_inst);
564585

565586
AOTMemoryInstance *
566-
aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index);
587+
aot_get_memory_with_idx(AOTModuleInstance *module_inst, uint32 mem_idx);
567588

568589
/**
569590
* Get a function in the AOT module instance.
@@ -574,7 +595,7 @@ aot_get_memory_with_index(AOTModuleInstance *module_inst, uint32 index);
574595
* @return the function instance found
575596
*/
576597
AOTFunctionInstance *
577-
aot_get_function_instance(AOTModuleInstance *module_inst, uint32_t func_idx);
598+
aot_get_function_instance(AOTModuleInstance *module_inst, uint32 func_idx);
578599

579600
/**
580601
* Call the given AOT function of a AOT module instance with

core/iwasm/common/wasm_application.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
105105
bool ret, is_import_func = true, is_memory64 = false;
106106
#if WASM_ENABLE_MEMORY64 != 0
107107
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
108-
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
108+
if (wasm_module_inst->memory_count > 0)
109+
is_memory64 = wasm_module_inst->memories[0]->is_memory64;
109110
#endif
110111

111112
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);

core/iwasm/common/wasm_c_api.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,21 +3383,8 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
33833383
if (!(func_comm_rt = func->func_comm_rt)) {
33843384
AOTModuleInstance *inst_aot =
33853385
(AOTModuleInstance *)func->inst_comm_rt;
3386-
AOTModule *module_aot = (AOTModule *)inst_aot->module;
3387-
uint32 export_i = 0, export_func_j = 0;
3388-
3389-
for (; export_i < module_aot->export_count; ++export_i) {
3390-
AOTExport *export = module_aot->exports + export_i;
3391-
if (export->kind == EXPORT_KIND_FUNC) {
3392-
if (export->index == func->func_idx_rt) {
3393-
func_comm_rt =
3394-
aot_lookup_function(inst_aot, export->name);
3395-
((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
3396-
break;
3397-
}
3398-
export_func_j++;
3399-
}
3400-
}
3386+
func_comm_rt = ((wasm_func_t *)func)->func_comm_rt =
3387+
aot_lookup_function_with_idx(inst_aot, func->func_idx_rt);
34013388
}
34023389
#endif
34033390
}

0 commit comments

Comments
 (0)