Skip to content

Commit 8a55a1e

Browse files
Shared heap enhancements for Interpreter and AOT (#4400)
Propose two enhancements: - Shared heap created from preallocated memory buffer: The user can create a shared heap from a pre-allocated buffer and see that memory region as one large chunk; there's no need to dynamically manage it(malloc/free). The user needs to make sure the native address and size of that memory region are valid. - Introduce shared heap chain: The user can create a shared heap chain, from the wasm app point of view, it's still a continuous memory region in wasm app's point of view while in the native it can consist of multiple shared heaps (each of which is a continuous memory region). For example, one 500MB shared heap 1 and one 500 MB shared heap 2 form a chain, in Wasm's point of view, it's one 1GB shared heap. After these enhancements, the data sharing between wasm apps, and between hosts can be more efficient and flexible. Admittedly shared heap management can be more complex for users, but it's similar to the zero-overhead principle. No overhead will be imposed for the users who don't use the shared heap enhancement or don't use the shared heap at all.
1 parent ee056d8 commit 8a55a1e

35 files changed

+2786
-651
lines changed

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_check_and_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/aot/aot_runtime.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ bh_static_assert(offsetof(AOTModuleInstanceExtra, stack_sizes) == 0);
6060
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_base_addr_adj)
6161
== 8);
6262
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_start_off) == 16);
63+
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_end_off) == 24);
64+
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap) == 32);
65+
66+
bh_static_assert(offsetof(WASMSharedHeap, next) == 0);
67+
bh_static_assert(offsetof(WASMSharedHeap, chain_next) == 8);
68+
bh_static_assert(offsetof(WASMSharedHeap, heap_handle) == 16);
69+
bh_static_assert(offsetof(WASMSharedHeap, base_addr) == 24);
70+
bh_static_assert(offsetof(WASMSharedHeap, size) == 32);
71+
bh_static_assert(offsetof(WASMSharedHeap, start_off_mem64) == 40);
72+
bh_static_assert(offsetof(WASMSharedHeap, start_off_mem32) == 48);
6373

6474
bh_static_assert(sizeof(CApiFuncImport) == sizeof(uintptr_t) * 3);
6575

@@ -1989,6 +1999,8 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
19891999
#else
19902000
extra->shared_heap_start_off.u32[0] = UINT32_MAX;
19912001
#endif
2002+
/* After shared heap chain, will early stop if shared heap is NULL */
2003+
extra->shared_heap = NULL;
19922004

19932005
#if WASM_ENABLE_PERF_PROFILING != 0
19942006
total_size = sizeof(AOTFuncPerfProfInfo)

core/iwasm/aot/aot_runtime.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ typedef struct AOTModuleInstanceExtra {
125125
*/
126126
DefPointer(uint8 *, shared_heap_base_addr_adj);
127127
MemBound shared_heap_start_off;
128+
MemBound shared_heap_end_off;
129+
DefPointer(WASMSharedHeap *, shared_heap);
128130

129131
WASMModuleInstanceExtraCommon common;
130132

@@ -142,9 +144,6 @@ typedef struct AOTModuleInstanceExtra {
142144
WASMModuleInstanceCommon **import_func_module_insts;
143145
#endif
144146

145-
#if WASM_ENABLE_SHARED_HEAP != 0
146-
WASMSharedHeap *shared_heap;
147-
#endif
148147
} AOTModuleInstanceExtra;
149148

150149
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)

0 commit comments

Comments
 (0)