Skip to content

Commit 7ddc5f8

Browse files
committed
try func call to replace shared heap chain traverse
1 parent 5702a61 commit 7ddc5f8

7 files changed

Lines changed: 217 additions & 282 deletions

File tree

core/iwasm/aot/aot_reloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ typedef struct {
185185
#define REG_STRINGREF_SYM()
186186
#endif
187187

188+
#if WASM_ENABLE_SHARED_HEAP != 0
189+
#define REG_SHARED_HEAP_SYM() \
190+
REG_SYM(wasm_runtime_update_last_used_shared_heap),
191+
#else
192+
#define REG_SHARED_HEAP_SYM()
193+
#endif
194+
188195
#define REG_COMMON_SYMBOLS \
189196
REG_SYM(aot_set_exception_with_id), \
190197
REG_SYM(aot_invoke_native), \
@@ -218,6 +225,7 @@ typedef struct {
218225
REG_LLVM_PGO_SYM() \
219226
REG_GC_SYM() \
220227
REG_STRINGREF_SYM() \
228+
REG_SHARED_HEAP_SYM() \
221229

222230
#define CHECK_RELOC_OFFSET(data_size) do { \
223231
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
@@ -7883,3 +7883,39 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)
78837883

78847884
return true;
78857885
}
7886+
7887+
#if WASM_ENABLE_SHARED_HEAP != 0
7888+
bool
7889+
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
7890+
uintptr_t app_offset, size_t bytes,
7891+
uintptr_t *shared_heap_start_off_p,
7892+
uintptr_t *shared_heap_end_off_p,
7893+
uint8 **shared_heap_base_addr_adj_p,
7894+
bool is_memory64)
7895+
{
7896+
WASMSharedHeap *heap = wasm_runtime_get_shared_heap(module_inst), *cur;
7897+
uint64 shared_heap_start, shared_heap_end;
7898+
7899+
if (bytes == 0) {
7900+
bytes = 1;
7901+
}
7902+
7903+
/* Find the exact shared heap that app addr is in, and update last used
7904+
* shared heap info in func context */
7905+
for (cur = heap; cur; cur = cur->chain_next) {
7906+
shared_heap_start =
7907+
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
7908+
shared_heap_end = shared_heap_start - 1 + cur->size;
7909+
if (app_offset >= shared_heap_start
7910+
&& app_offset <= shared_heap_end - bytes + 1) {
7911+
*shared_heap_start_off_p = (uintptr_t)shared_heap_start;
7912+
*shared_heap_end_off_p = (uintptr_t)shared_heap_end;
7913+
*shared_heap_base_addr_adj_p =
7914+
cur->base_addr - (uintptr_t)shared_heap_start;
7915+
return true;
7916+
}
7917+
}
7918+
7919+
return false;
7920+
}
7921+
#endif

core/iwasm/common/wasm_runtime_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,15 @@ void
13361336
wasm_runtime_set_linux_perf(bool flag);
13371337
#endif
13381338

1339+
#if WASM_ENABLE_SHARED_HEAP != 0
1340+
bool
1341+
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
1342+
uintptr_t app_offset, size_t bytes,
1343+
uintptr_t *shared_heap_start_off_p,
1344+
uintptr_t *shared_heap_end_off_p,
1345+
uint8 **shared_heap_base_addr_adj_p, bool is_memory64);
1346+
#endif
1347+
13391348
#ifdef __cplusplus
13401349
}
13411350
#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
@@ -1526,60 +1526,87 @@ create_memory_info(const AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
15261526
func_ctx->aot_inst, &offset, 1, \
15271527
#field "_p"))) { \
15281528
aot_set_last_error("llvm build inbounds gep failed"); \
1529-
return false; \
1529+
goto fail; \
15301530
} \
15311531
if (!(load_val = \
15321532
LLVMBuildLoad2(comp_ctx->builder, type, field_p, #field))) { \
15331533
aot_set_last_error("llvm build load failed"); \
1534-
return false; \
1534+
goto fail; \
15351535
} \
15361536
if (!(func_ctx->field = \
15371537
LLVMBuildAlloca(comp_ctx->builder, type, #field))) { \
15381538
aot_set_last_error("llvm build alloca failed"); \
1539-
return false; \
1539+
goto fail; \
15401540
} \
15411541
if (!LLVMBuildStore(comp_ctx->builder, load_val, func_ctx->field)) { \
15421542
aot_set_last_error("llvm build store failed"); \
1543-
return false; \
1544-
} \
1545-
} while (0)
1546-
1547-
#define LOAD_MODULE_EXTRA_FIELD(field, type) \
1548-
do { \
1549-
get_module_extra_field_offset(field); \
1550-
offset = I32_CONST(offset_u32); \
1551-
CHECK_LLVM_CONST(offset); \
1552-
if (!(field_p = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, \
1553-
func_ctx->aot_inst, &offset, 1, \
1554-
#field "_p"))) { \
1555-
aot_set_last_error("llvm build inbounds gep failed"); \
1556-
return false; \
1557-
} \
1558-
if (!(func_ctx->field = \
1559-
LLVMBuildLoad2(comp_ctx->builder, type, field_p, #field))) { \
1560-
aot_set_last_error("llvm build load failed"); \
1561-
return false; \
1543+
goto fail; \
15621544
} \
15631545
} while (0)
15641546

15651547
static bool
15661548
create_shared_heap_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
15671549
{
1568-
LLVMValueRef offset, field_p, load_val;
1550+
LLVMValueRef offset, field_p, load_val, shared_heap_head_p,
1551+
shared_heap_head;
1552+
LLVMTypeRef shared_heap_offset_type;
15691553
uint32 offset_u32;
1554+
#if WASM_ENABLE_MEMORY64 == 0
1555+
bool is_memory64 = false;
1556+
#else
1557+
bool is_memory64 = IS_MEMORY64;
1558+
#endif
15701559

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

15841611
return true;
15851612
fail:
@@ -2438,7 +2465,7 @@ jit_stack_size_callback(void *user_data, const char *name, size_t namelen,
24382465
stack_consumption_to_call_wrapped_func =
24392466
musttail ? 0
24402467
: aot_estimate_stack_usage_for_function_call(
2441-
comp_ctx, func_ctx->aot_func->func_type);
2468+
comp_ctx, func_ctx->aot_func->func_type);
24422469
LOG_VERBOSE("func %.*s stack %u + %zu + %u", (int)namelen, name,
24432470
stack_consumption_to_call_wrapped_func, stack_size, call_size);
24442471

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}/interpreter/iwasm_interp.cmake)
284284
include (${IWASM_DIR}/aot/iwasm_aot.cmake)
285285
include (${IWASM_DIR}/compilation/iwasm_compl.cmake)
286286
include (${PROJECT_SOURCE_DIR}/../build-scripts/version.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)
@@ -366,6 +367,7 @@ add_library (vmlib
366367
${LIBC_WASI_SOURCE}
367368
${LIB_PTHREAD_SOURCE}
368369
${LIB_WASI_THREADS_SOURCE}
370+
${LIB_SHARED_HEAP_SOURCE}
369371
${IWASM_COMMON_SOURCE}
370372
${IWASM_INTERP_SOURCE}
371373
${IWASM_AOT_SOURCE}

0 commit comments

Comments
 (0)