Skip to content

Commit 9a71956

Browse files
committed
try func call to replace shared heap chain traverse
1 parent 623bcb7 commit 9a71956

File tree

7 files changed

+217
-282
lines changed

7 files changed

+217
-282
lines changed

core/iwasm/aot/aot_reloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ typedef struct {
181181
#define REG_STRINGREF_SYM()
182182
#endif
183183

184+
#if WASM_ENABLE_SHARED_HEAP != 0
185+
#define REG_SHARED_HEAP_SYM() \
186+
REG_SYM(wasm_runtime_update_last_used_shared_heap),
187+
#else
188+
#define REG_SHARED_HEAP_SYM()
189+
#endif
190+
184191
#define REG_COMMON_SYMBOLS \
185192
REG_SYM(aot_set_exception_with_id), \
186193
REG_SYM(aot_invoke_native), \
@@ -214,6 +221,7 @@ typedef struct {
214221
REG_LLVM_PGO_SYM() \
215222
REG_GC_SYM() \
216223
REG_STRINGREF_SYM() \
224+
REG_SHARED_HEAP_SYM() \
217225

218226
#define CHECK_RELOC_OFFSET(data_size) do { \
219227
if (!check_reloc_offset(target_section_size, \

core/iwasm/common/wasm_runtime_common.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7827,3 +7827,39 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)
78277827

78287828
return true;
78297829
}
7830+
7831+
#if WASM_ENABLE_SHARED_HEAP != 0
7832+
bool
7833+
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
7834+
uintptr_t app_offset, size_t bytes,
7835+
uintptr_t *shared_heap_start_off_p,
7836+
uintptr_t *shared_heap_end_off_p,
7837+
uint8 **shared_heap_base_addr_adj_p,
7838+
bool is_memory64)
7839+
{
7840+
WASMSharedHeap *heap = wasm_runtime_get_shared_heap(module_inst), *cur;
7841+
uint64 shared_heap_start, shared_heap_end;
7842+
7843+
if (bytes == 0) {
7844+
bytes = 1;
7845+
}
7846+
7847+
/* Find the exact shared heap that app addr is in, and update last used
7848+
* shared heap info in func context */
7849+
for (cur = heap; cur; cur = cur->chain_next) {
7850+
shared_heap_start =
7851+
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
7852+
shared_heap_end = shared_heap_start - 1 + cur->size;
7853+
if (app_offset >= shared_heap_start
7854+
&& app_offset <= shared_heap_end - bytes + 1) {
7855+
*shared_heap_start_off_p = (uintptr_t)shared_heap_start;
7856+
*shared_heap_end_off_p = (uintptr_t)shared_heap_end;
7857+
*shared_heap_base_addr_adj_p =
7858+
cur->base_addr - (uintptr_t)shared_heap_start;
7859+
return true;
7860+
}
7861+
}
7862+
7863+
return false;
7864+
}
7865+
#endif

core/iwasm/common/wasm_runtime_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,15 @@ void
12151215
wasm_runtime_set_linux_perf(bool flag);
12161216
#endif
12171217

1218+
#if WASM_ENABLE_SHARED_HEAP != 0
1219+
bool
1220+
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
1221+
uintptr_t app_offset, size_t bytes,
1222+
uintptr_t *shared_heap_start_off_p,
1223+
uintptr_t *shared_heap_end_off_p,
1224+
uint8 **shared_heap_base_addr_adj_p, bool is_memory64);
1225+
#endif
1226+
12181227
#ifdef __cplusplus
12191228
}
12201229
#endif

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 99 additions & 246 deletions
Large diffs are not rendered by default.

core/iwasm/compilation/aot_llvm.c

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,60 +1527,87 @@ create_memory_info(const AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
15271527
func_ctx->aot_inst, &offset, 1, \
15281528
#field "_p"))) { \
15291529
aot_set_last_error("llvm build inbounds gep failed"); \
1530-
return false; \
1530+
goto fail; \
15311531
} \
15321532
if (!(load_val = \
15331533
LLVMBuildLoad2(comp_ctx->builder, type, field_p, #field))) { \
15341534
aot_set_last_error("llvm build load failed"); \
1535-
return false; \
1535+
goto fail; \
15361536
} \
15371537
if (!(func_ctx->field = \
15381538
LLVMBuildAlloca(comp_ctx->builder, type, #field))) { \
15391539
aot_set_last_error("llvm build alloca failed"); \
1540-
return false; \
1540+
goto fail; \
15411541
} \
15421542
if (!LLVMBuildStore(comp_ctx->builder, load_val, func_ctx->field)) { \
15431543
aot_set_last_error("llvm build store failed"); \
1544-
return false; \
1545-
} \
1546-
} while (0)
1547-
1548-
#define LOAD_MODULE_EXTRA_FIELD(field, type) \
1549-
do { \
1550-
get_module_extra_field_offset(field); \
1551-
offset = I32_CONST(offset_u32); \
1552-
CHECK_LLVM_CONST(offset); \
1553-
if (!(field_p = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, \
1554-
func_ctx->aot_inst, &offset, 1, \
1555-
#field "_p"))) { \
1556-
aot_set_last_error("llvm build inbounds gep failed"); \
1557-
return false; \
1558-
} \
1559-
if (!(func_ctx->field = \
1560-
LLVMBuildLoad2(comp_ctx->builder, type, field_p, #field))) { \
1561-
aot_set_last_error("llvm build load failed"); \
1562-
return false; \
1544+
goto fail; \
15631545
} \
15641546
} while (0)
15651547

15661548
static bool
15671549
create_shared_heap_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
15681550
{
1569-
LLVMValueRef offset, field_p, load_val;
1551+
LLVMValueRef offset, field_p, load_val, shared_heap_head_p,
1552+
shared_heap_head;
1553+
LLVMTypeRef shared_heap_offset_type;
15701554
uint32 offset_u32;
1555+
#if WASM_ENABLE_MEMORY64 == 0
1556+
bool is_memory64 = false;
1557+
#else
1558+
bool is_memory64 = IS_MEMORY64;
1559+
#endif
15711560

1572-
/* shared_heap_base_addr_adj, shared_heap_start_off, and shared_heap_end_off
1573-
* can be updated later, use local variable to represent them */
1561+
shared_heap_offset_type =
1562+
comp_ctx->pointer_size == sizeof(uint64) ? I64_TYPE : I32_TYPE;
1563+
1564+
/* shared_heap_base_addr_adj, shared_heap_start_off, and
1565+
* shared_heap_end_off can be updated later, use local variable to
1566+
* represent them */
15741567
LOAD_MODULE_EXTRA_FIELD_AND_ALLOCA(shared_heap_base_addr_adj,
15751568
INT8_PTR_TYPE);
1576-
LOAD_MODULE_EXTRA_FIELD_AND_ALLOCA(
1577-
shared_heap_start_off,
1578-
comp_ctx->pointer_size == sizeof(uint64) ? I64_TYPE : I32_TYPE);
1579-
LOAD_MODULE_EXTRA_FIELD_AND_ALLOCA(
1580-
shared_heap_end_off,
1581-
comp_ctx->pointer_size == sizeof(uint64) ? I64_TYPE : I32_TYPE);
1582-
/* Shared Heap won't be updated, no need to alloca */
1583-
LOAD_MODULE_EXTRA_FIELD(shared_heap, INT8_PTR_TYPE);
1569+
LOAD_MODULE_EXTRA_FIELD_AND_ALLOCA(shared_heap_start_off,
1570+
shared_heap_offset_type);
1571+
LOAD_MODULE_EXTRA_FIELD_AND_ALLOCA(shared_heap_end_off,
1572+
shared_heap_offset_type);
1573+
1574+
/* Shared Heap head start off won't be updated, no need to alloca */
1575+
get_module_extra_field_offset(shared_heap);
1576+
offset = I32_CONST(offset_u32);
1577+
CHECK_LLVM_CONST(offset);
1578+
if (!(shared_heap_head_p = LLVMBuildInBoundsGEP2(
1579+
comp_ctx->builder, INT8_TYPE, func_ctx->aot_inst, &offset, 1,
1580+
"shared_heap_head_p"))) {
1581+
aot_set_last_error("llvm build inbounds gep failed");
1582+
goto fail;
1583+
}
1584+
if (!(shared_heap_head =
1585+
LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
1586+
shared_heap_head_p, "shared_heap_head"))) {
1587+
aot_set_last_error("llvm build load failed");
1588+
goto fail;
1589+
}
1590+
1591+
if (is_memory64) {
1592+
offset_u32 = offsetof(WASMSharedHeap, start_off_mem64);
1593+
}
1594+
else {
1595+
offset_u32 = offsetof(WASMSharedHeap, start_off_mem32);
1596+
}
1597+
offset = I32_CONST(offset_u32);
1598+
CHECK_LLVM_CONST(offset);
1599+
if (!(field_p = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
1600+
shared_heap_head, &offset, 1,
1601+
"head_start_off_p"))) {
1602+
aot_set_last_error("llvm build inbounds gep failed");
1603+
goto fail;
1604+
}
1605+
if (!(func_ctx->shared_heap_head_start_off =
1606+
LLVMBuildLoad2(comp_ctx->builder, shared_heap_offset_type,
1607+
field_p, "shared_heap_head_start_off"))) {
1608+
aot_set_last_error("llvm build load failed");
1609+
goto fail;
1610+
}
15841611

15851612
return true;
15861613
fail:
@@ -2439,7 +2466,7 @@ jit_stack_size_callback(void *user_data, const char *name, size_t namelen,
24392466
stack_consumption_to_call_wrapped_func =
24402467
musttail ? 0
24412468
: aot_estimate_stack_usage_for_function_call(
2442-
comp_ctx, func_ctx->aot_func->func_type);
2469+
comp_ctx, func_ctx->aot_func->func_type);
24432470
LOG_VERBOSE("func %.*s stack %u + %zu + %u", (int)namelen, name,
24442471
stack_consumption_to_call_wrapped_func, stack_size, call_size);
24452472

core/iwasm/compilation/aot_llvm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ typedef struct AOTFuncContext {
275275
LLVMValueRef shared_heap_base_addr_adj;
276276
LLVMValueRef shared_heap_start_off;
277277
LLVMValueRef shared_heap_end_off;
278-
/* The head of shared heap chain, and its start offset */
279-
LLVMValueRef shared_heap;
278+
/* The start offset of the head of shared heap chain */
279+
LLVMValueRef shared_heap_head_start_off;
280280

281281
LLVMBasicBlockRef got_exception_block;
282282
LLVMBasicBlockRef func_return_block;

wamr-compiler/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ include (${IWASM_DIR}/common/gc/iwasm_gc.cmake)
284284
include (${IWASM_DIR}/interpreter/iwasm_interp.cmake)
285285
include (${IWASM_DIR}/aot/iwasm_aot.cmake)
286286
include (${IWASM_DIR}/compilation/iwasm_compl.cmake)
287+
include (${IWASM_DIR}/libraries/shared-heap/shared_heap.cmake)
287288

288289
if (WAMR_BUILD_LIBC_BUILTIN EQUAL 1)
289290
include (${IWASM_DIR}/libraries/libc-builtin/libc_builtin.cmake)
@@ -365,6 +366,7 @@ add_library (vmlib
365366
${LIBC_WASI_SOURCE}
366367
${LIB_PTHREAD_SOURCE}
367368
${LIB_WASI_THREADS_SOURCE}
369+
${LIB_SHARED_HEAP_SOURCE}
368370
${IWASM_COMMON_SOURCE}
369371
${IWASM_INTERP_SOURCE}
370372
${IWASM_AOT_SOURCE}

0 commit comments

Comments
 (0)