Skip to content

Commit fccb29c

Browse files
committed
add option for wamrc to enable single shared heap/multi shared heap, and update shared heap unit tests and sample
1 parent 5e0d3c9 commit fccb29c

File tree

10 files changed

+243
-71
lines changed

10 files changed

+243
-71
lines changed

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -278,36 +278,47 @@ aot_check_shared_heap_memory_overflow_common(
278278
/* On 64/32-bit target, the offset is 64/32-bit */
279279
offset_type = is_target_64bit ? I64_TYPE : I32_TYPE;
280280

281-
ADD_BASIC_BLOCK(app_addr_in_shared_heap_chain,
282-
"app_addr_in_shared_heap_chain");
283281
ADD_BASIC_BLOCK(app_addr_in_cache_shared_heap,
284282
"app_addr_in_cache_shared_heap");
285-
ADD_BASIC_BLOCK(check_shared_heap_chain, "check_shared_heap_chain");
286283
ADD_BASIC_BLOCK(app_addr_in_linear_mem, "app_addr_in_linear_mem");
287284

288-
LLVMMoveBasicBlockAfter(app_addr_in_shared_heap_chain, block_curr);
289-
LLVMMoveBasicBlockAfter(app_addr_in_cache_shared_heap,
290-
app_addr_in_shared_heap_chain);
291-
LLVMMoveBasicBlockAfter(check_shared_heap_chain,
292-
app_addr_in_cache_shared_heap);
293-
LLVMMoveBasicBlockAfter(app_addr_in_linear_mem,
294-
app_addr_in_cache_shared_heap);
285+
if (comp_ctx->enable_shared_heap) {
286+
LLVMMoveBasicBlockAfter(app_addr_in_cache_shared_heap, block_curr);
287+
LLVMMoveBasicBlockAfter(app_addr_in_linear_mem,
288+
app_addr_in_cache_shared_heap);
289+
}
290+
else if (comp_ctx->enable_shared_chain) {
291+
ADD_BASIC_BLOCK(app_addr_in_shared_heap_chain,
292+
"app_addr_in_shared_heap_chain");
293+
ADD_BASIC_BLOCK(check_shared_heap_chain, "check_shared_heap_chain");
294+
LLVMMoveBasicBlockAfter(app_addr_in_shared_heap_chain, block_curr);
295+
LLVMMoveBasicBlockAfter(app_addr_in_cache_shared_heap,
296+
app_addr_in_shared_heap_chain);
297+
LLVMMoveBasicBlockAfter(check_shared_heap_chain,
298+
app_addr_in_cache_shared_heap);
299+
LLVMMoveBasicBlockAfter(app_addr_in_linear_mem,
300+
app_addr_in_cache_shared_heap);
301+
}
295302

296303
if (!bulk_memory)
297304
LLVMMoveBasicBlockAfter(block_maddr_phi, app_addr_in_linear_mem);
298305
else
299306
LLVMMoveBasicBlockAfter(block_maddr_phi, check_succ);
300307

301-
/* Use >= here for func_ctx->shared_heap_head_start_off =
302-
* shared_heap_head->start - 1 and use UINT32_MAX/UINT64_MAX value to
303-
* indicate invalid value. The shared heap chain oob will be detected in
304-
* app_addr_in_shared_heap block or aot_check_shared_heap_chain function */
305-
BUILD_ICMP(LLVMIntUGT, start_offset, func_ctx->shared_heap_head_start_off,
306-
is_in_shared_heap, "shared_heap_lb_cmp");
307-
BUILD_COND_BR(is_in_shared_heap, app_addr_in_shared_heap_chain,
308-
app_addr_in_linear_mem);
308+
if (comp_ctx->enable_shared_chain) {
309+
/* Use >= here for func_ctx->shared_heap_head_start_off =
310+
* shared_heap_head->start - 1 and use UINT32_MAX/UINT64_MAX value to
311+
* indicate invalid value. The shared heap chain oob will be detected in
312+
* app_addr_in_shared_heap block or aot_check_shared_heap_chain function
313+
*/
314+
BUILD_ICMP(LLVMIntUGT, start_offset,
315+
func_ctx->shared_heap_head_start_off, is_in_shared_heap,
316+
"shared_heap_lb_cmp");
317+
BUILD_COND_BR(is_in_shared_heap, app_addr_in_shared_heap_chain,
318+
app_addr_in_linear_mem);
309319

310-
SET_BUILD_POS(app_addr_in_shared_heap_chain);
320+
SET_BUILD_POS(app_addr_in_shared_heap_chain);
321+
}
311322
/* Load the local variable of the function */
312323
BUILD_LOAD_PTR(func_ctx->shared_heap_start_off, offset_type,
313324
shared_heap_start_off);
@@ -334,17 +345,24 @@ aot_check_shared_heap_memory_overflow_common(
334345
"cmp_cache_shared_heap_end");
335346
}
336347
BUILD_OP(And, cmp, cmp1, cmp2, "is_in_cache_shared_heap");
337-
BUILD_COND_BR(cmp2, app_addr_in_cache_shared_heap, check_shared_heap_chain);
338-
339-
SET_BUILD_POS(check_shared_heap_chain);
340-
if (!bulk_memory) {
341-
bytes = is_target_64bit ? I64_CONST(bytes_u32) : I32_CONST(bytes_u32);
342-
}
343-
if (!aot_check_shared_heap_chain(comp_ctx, func_ctx,
344-
app_addr_in_cache_shared_heap,
345-
start_offset, bytes, is_memory64)) {
346-
aot_set_last_error("llvm build aot_check_shared_heap_chain failed");
347-
goto fail;
348+
if (comp_ctx->enable_shared_heap) {
349+
BUILD_COND_BR(cmp2, app_addr_in_cache_shared_heap,
350+
app_addr_in_linear_mem);
351+
}
352+
else if (comp_ctx->enable_shared_chain) {
353+
BUILD_COND_BR(cmp2, app_addr_in_cache_shared_heap,
354+
check_shared_heap_chain);
355+
SET_BUILD_POS(check_shared_heap_chain);
356+
if (!bulk_memory) {
357+
bytes =
358+
is_target_64bit ? I64_CONST(bytes_u32) : I32_CONST(bytes_u32);
359+
}
360+
if (!aot_check_shared_heap_chain(comp_ctx, func_ctx,
361+
app_addr_in_cache_shared_heap,
362+
start_offset, bytes, is_memory64)) {
363+
aot_set_last_error("llvm build aot_check_shared_heap_chain failed");
364+
goto fail;
365+
}
348366
}
349367

350368
SET_BUILD_POS(app_addr_in_cache_shared_heap);
@@ -617,7 +635,8 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
617635
}
618636

619637
#if WASM_ENABLE_SHARED_HEAP != 0
620-
if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
638+
if (comp_ctx->enable_shared_heap
639+
|| comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
621640
ADD_BASIC_BLOCK(block_maddr_phi, "maddr_phi");
622641
SET_BUILD_POS(block_maddr_phi);
623642
if (!(maddr_phi =
@@ -716,7 +735,8 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
716735
}
717736

718737
#if WASM_ENABLE_SHARED_HEAP != 0
719-
if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
738+
if (comp_ctx->enable_shared_heap
739+
|| comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
720740
block_curr = LLVMGetInsertBlock(comp_ctx->builder);
721741
LLVMAddIncoming(maddr_phi, &maddr, &block_curr, 1);
722742
if (!LLVMBuildBr(comp_ctx->builder, block_maddr_phi)) {
@@ -1465,7 +1485,8 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
14651485
}
14661486

14671487
#if WASM_ENABLE_SHARED_HEAP != 0
1468-
if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
1488+
if (comp_ctx->enable_shared_heap
1489+
|| comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
14691490
ADD_BASIC_BLOCK(block_maddr_phi, "maddr_phi");
14701491
SET_BUILD_POS(block_maddr_phi);
14711492
if (!(maddr_phi = LLVMBuildPhi(comp_ctx->builder, INT8_PTR_TYPE,
@@ -1507,7 +1528,8 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
15071528
}
15081529

15091530
#if WASM_ENABLE_SHARED_HEAP != 0
1510-
if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) {
1531+
if (comp_ctx->enable_shared_heap
1532+
|| comp_ctx->enable_shared_chain /* TODO: && mem_idx == 0 */) {
15111533
block_curr = LLVMGetInsertBlock(comp_ctx->builder);
15121534
LLVMAddIncoming(maddr_phi, &maddr, &block_curr, 1);
15131535
if (!LLVMBuildBr(comp_ctx->builder, block_maddr_phi)) {

core/iwasm/compilation/aot_llvm.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,8 +1614,8 @@ create_shared_heap_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
16141614
goto fail;
16151615
}
16161616

1617-
/* Select shared heap head ptr or safe alloca
1618-
* shared_heap_start_off(UINT32_MAX/UINT64_MAX) based on the condition */
1617+
/* Select a valid shared heap head ptr or safe alloca ptr stores
1618+
* shared_heap_start_off(UINT32_MAX/UINT64_MAX) */
16191619
if (!(field_p_or_default = LLVMBuildSelect(comp_ctx->builder, cmp, field_p,
16201620
func_ctx->shared_heap_start_off,
16211621
"ptr_or_default"))) {
@@ -1638,6 +1638,8 @@ create_shared_heap_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
16381638
goto fail;
16391639
}
16401640

1641+
/* if there is attached shared heap(s), the value will be valid start_off-1,
1642+
* otherwise it will be UINT32_MAX/UINT64_MAX */
16411643
if (!(func_ctx->shared_heap_head_start_off = LLVMBuildSelect(
16421644
comp_ctx->builder, cmp, shared_heap_head_start_off_minus_one,
16431645
shared_heap_head_start_off, "head_start_off"))) {
@@ -1943,7 +1945,7 @@ aot_create_func_context(const AOTCompData *comp_data, AOTCompContext *comp_ctx,
19431945
}
19441946

19451947
/* Load shared heap, shared heap start off mem32 or mem64 */
1946-
if (comp_ctx->enable_shared_heap
1948+
if ((comp_ctx->enable_shared_heap || comp_ctx->enable_shared_chain)
19471949
&& !create_shared_heap_info(comp_ctx, func_ctx)) {
19481950
goto fail;
19491951
}
@@ -2762,6 +2764,9 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
27622764
if (option->enable_shared_heap)
27632765
comp_ctx->enable_shared_heap = true;
27642766

2767+
if (option->enable_shared_chain)
2768+
comp_ctx->enable_shared_chain = true;
2769+
27652770
comp_ctx->opt_level = option->opt_level;
27662771
comp_ctx->size_level = option->size_level;
27672772

core/iwasm/compilation/aot_llvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ typedef struct AOTCompContext {
504504
bool enable_gc;
505505

506506
bool enable_shared_heap;
507+
bool enable_shared_chain;
507508

508509
uint32 opt_level;
509510
uint32 size_level;

core/iwasm/include/aot_comp_option.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ typedef struct AOTCompOption {
7474
bool enable_stack_estimation;
7575
bool quick_invoke_c_api_import;
7676
bool enable_shared_heap;
77+
bool enable_shared_chain;
7778
char *use_prof_file;
7879
uint32_t opt_level;
7980
uint32_t size_level;

samples/shared-heap/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ if (WAMR_BUILD_AOT EQUAL 1)
122122

123123
if (WAMR_BUILD_TARGET STREQUAL "X86_32")
124124
set (WAMR_COMPILER_FLAGS --enable-shared-heap --target=i386)
125+
set (WAMR_COMPILER_CHAIN_FLAGS --enable-shared-chain --target=i386)
125126
else ()
126127
set (WAMR_COMPILER_FLAGS --enable-shared-heap)
128+
set (WAMR_COMPILER_CHAIN_FLAGS --enable-shared-chain)
127129
endif ()
128130

129131
add_custom_target(
@@ -132,6 +134,8 @@ if (WAMR_BUILD_AOT EQUAL 1)
132134
DEPENDS wasm-apps/test1.wasm wasm-apps/test2.wasm ${WAMR_COMPILER}
133135
COMMAND ${WAMR_COMPILER} ${WAMR_COMPILER_FLAGS} -o wasm-apps/test1.aot wasm-apps/test1.wasm
134136
COMMAND ${WAMR_COMPILER} ${WAMR_COMPILER_FLAGS} -o wasm-apps/test2.aot wasm-apps/test2.wasm
137+
COMMAND ${WAMR_COMPILER} ${WAMR_COMPILER_CHAIN_FLAGS} -o wasm-apps/test1_chain.aot wasm-apps/test1.wasm
138+
COMMAND ${WAMR_COMPILER} ${WAMR_COMPILER_CHAIN_FLAGS} -o wasm-apps/test2_chain.aot wasm-apps/test2.wasm
135139
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
136140
)
137141
endif()

samples/shared-heap/src/shared_heap_chain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ main(int argc, char **argv)
198198
if (!aot_mode)
199199
wasm_file1 = "./wasm-apps/test1.wasm";
200200
else
201-
wasm_file1 = "./wasm-apps/test1.aot";
201+
wasm_file1 = "./wasm-apps/test1_chain.aot";
202202
if (!(wasm_file1_buf =
203203
bh_read_file_to_buffer(wasm_file1, &wasm_file1_size))) {
204204
printf("Open wasm file %s failed.\n", wasm_file1);
@@ -225,7 +225,7 @@ main(int argc, char **argv)
225225
if (!aot_mode)
226226
wasm_file2 = "./wasm-apps/test2.wasm";
227227
else
228-
wasm_file2 = "./wasm-apps/test2.aot";
228+
wasm_file2 = "./wasm-apps/test2_chain.aot";
229229
if (!(wasm_file2_buf =
230230
bh_read_file_to_buffer(wasm_file2, &wasm_file2_size))) {
231231
printf("Open wasm file %s failed.\n", wasm_file1);

0 commit comments

Comments
 (0)