Skip to content

Commit

Permalink
Move param/local ref flags to AOT module instead of commiting in GC A…
Browse files Browse the repository at this point in the history
…OT code
  • Loading branch information
TianlongLiang committed Jan 9, 2024
1 parent a210b97 commit 01c266c
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 40 deletions.
49 changes: 49 additions & 0 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,41 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
#endif
}

#if WASM_ENABLE_GC != 0
size = sizeof(LocalRefFlag)
* (uint64)(module->import_func_count + module->func_count);
if (size > 0) {
if (!(module->func_local_ref_flags =
loader_malloc(size, error_buf, error_buf_size))) {
return false;
}

for (i = 0; i < module->import_func_count + module->func_count; i++) {
uint32 j, local_ref_flag_cell_num;

buf = (uint8 *)align_ptr(buf, sizeof(uint32));
read_uint32(
p, p_end,
module->func_local_ref_flags[i].local_ref_flag_cell_num);

local_ref_flag_cell_num =
module->func_local_ref_flags[i].local_ref_flag_cell_num;
size = sizeof(uint8) * (uint64)local_ref_flag_cell_num;
if (size > 0) {
if (!(module->func_local_ref_flags[i].local_ref_flags =
loader_malloc(size, error_buf, error_buf_size))) {
return false;
}
for (j = 0; j < local_ref_flag_cell_num; j++) {
read_uint8(
p, p_end,
module->func_local_ref_flags[i].local_ref_flags[j]);
}
}
}
}
#endif /* end of WASM_ENABLE_GC != 0 */

if (p != buf_end) {
set_error_buf(error_buf, error_buf_size,
"invalid function section size");
Expand Down Expand Up @@ -4131,6 +4166,20 @@ aot_unload(AOTModule *module)
wasm_runtime_free(module->max_stack_cell_nums);
#endif

#if WASM_ENABLE_GC != 0
if (module->func_local_ref_flags) {
uint32 i;
for (i = 0; i < module->import_func_count + module->func_count; i++) {
if (module->func_local_ref_flags[i].local_ref_flags) {
wasm_runtime_free(
module->func_local_ref_flags[i].local_ref_flags);
}
}

wasm_runtime_free(module->func_local_ref_flags);
}
#endif

if (module->func_ptrs)
wasm_runtime_free(module->func_ptrs);

Expand Down
54 changes: 25 additions & 29 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3514,33 +3514,6 @@ aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)

#if WASM_ENABLE_GC != 0
frame->frame_ref = (uint8 *)(frame->sp + max_stack_cell_num);

/* Initialize frame ref flags for import function */
if (func_index < module->import_func_count) {
AOTFuncType *func_type = module->import_funcs[func_index].func_type;
uint8 *frame_ref = frame->frame_ref;
uint32 i, j, k, value_type_cell_num;

for (i = 0, j = 0; i < func_type->param_count; i++) {
if (wasm_is_type_reftype(func_type->types[i])
&& !wasm_is_reftype_i31ref(func_type->types[i])) {
frame_ref[j++] = 1;
#if UINTPTR_MAX == UINT64_MAX
frame_ref[j++] = 1;
#endif
}
else {
value_type_cell_num =
wasm_value_type_cell_num(func_type->types[i]);
for (k = 0; k < value_type_cell_num; k++)
frame_ref[j++] = 0;
}
}

for (j = func_type->param_cell_num; j < 2; j++) {
frame_ref[j] = 0;
}
}
#endif

frame->prev_frame = (AOTFrame *)exec_env->cur_frame;
Expand Down Expand Up @@ -3601,6 +3574,7 @@ aot_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame)
bool
aot_create_call_stack(struct WASMExecEnv *exec_env)
{
/* TODO: write back ref flags in AOT module */
AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame,
*first_frame = cur_frame;
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
Expand Down Expand Up @@ -4471,13 +4445,35 @@ static bool
aot_frame_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
{
AOTFrame *frame;
AOTModule *module;
LocalRefFlag frame_local_flags;
WASMObjectRef gc_obj;
int i;
uint32 i, local_ref_flag_cell_num;

module = (AOTModule *)wasm_exec_env_get_module(exec_env);
frame = (AOTFrame *)wasm_exec_env_get_cur_frame(exec_env);
for (; frame; frame = frame->prev_frame) {
/* local ref flags */
frame_local_flags = module->func_local_ref_flags[frame->func_index];
local_ref_flag_cell_num = frame_local_flags.local_ref_flag_cell_num;
for (i = 0; i < local_ref_flag_cell_num; i++) {
if (frame_local_flags.local_ref_flags[i]) {
gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
if (wasm_obj_is_created_from_heap(gc_obj)) {
if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
return false;
}
}
#if UINTPTR_MAX == UINT64_MAX
bh_assert(frame_local_flags.local_ref_flags[i + 1]);
i++;
#endif
}
}

/* stack ref flags */
uint8 *frame_ref = frame->frame_ref;
for (i = 0; i < frame->sp - frame->lp; i++) {
for (i = local_ref_flag_cell_num; i < frame->sp - frame->lp; i++) {
if (frame_ref[i]) {
gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
if (wasm_obj_is_created_from_heap(gc_obj)) {
Expand Down
15 changes: 14 additions & 1 deletion core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ typedef struct GOTItem {
} GOTItem, *GOTItemList;
#endif

#if WASM_ENABLE_GC != 0
typedef struct LocalRefFlag {
uint32 local_ref_flag_cell_num;
uint8 *local_ref_flags;
} LocalRefFlag;
#endif

typedef struct AOTModule {
uint32 module_type;

Expand Down Expand Up @@ -183,6 +190,11 @@ typedef struct AOTModule {
uint32 *max_stack_cell_nums;
#endif

#if WASM_ENABLE_GC != 0
/* params + locals ref flags of (both import and AOTed) functions */
struct LocalRefFlag *func_local_ref_flags;
#endif

/* export info */
uint32 export_count;
AOTExport *exports;
Expand Down Expand Up @@ -362,7 +374,8 @@ typedef struct AOTFrame {
* local area: parameters and local variables
* stack area: wasm operand stack
* frame ref flags (GC only):
* whether each cell in local and stack area is a GC obj
* whether each cell in local(LLVM JIT) and stack area is a GC obj
* for AOT, local is only paddings, actual ref flags for local area are stored in AOT module
*/
uint32 lp[1];
} AOTFrame;
Expand Down
21 changes: 18 additions & 3 deletions core/iwasm/compilation/aot_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ aot_gen_commit_values(AOTCompFrame *frame)
LLVMValueRef value;
uint32 n;

for (p = frame->lp; p < frame->sp; p++) {
/* First, commit reference flags, ignore local(params + locals) ref flags */
for (p = frame->lp + frame->max_local_cell_num; p < frame->sp; p++) {
if (!p->dirty)
continue;

p->dirty = 0;
n = p - frame->lp;

/* Commit reference flag */
Expand All @@ -349,6 +349,7 @@ aot_gen_commit_values(AOTCompFrame *frame)
return false;
p->committed_ref = (p + 1)->committed_ref = p->ref + 1;
}
p++;
break;

case VALUE_TYPE_V128:
Expand All @@ -365,6 +366,9 @@ aot_gen_commit_values(AOTCompFrame *frame)
(p + 2)->committed_ref = (p + 3)->committed_ref =
p->ref + 1;
}
p++;
p++;
p++;
break;

case REF_TYPE_NULLFUNCREF:
Expand Down Expand Up @@ -397,6 +401,7 @@ aot_gen_commit_values(AOTCompFrame *frame)
p->committed_ref = (p + 1)->committed_ref =
p->ref + 1;
}
p++;
}
else {
if (p->ref != p->committed_ref - 1) {
Expand All @@ -414,7 +419,17 @@ aot_gen_commit_values(AOTCompFrame *frame)
break;
}
}
}

/* Second, commit all values */
for (p = frame->lp; p < frame->sp; p++) {
if (!p->dirty)
continue;

p->dirty = 0;
n = p - frame->lp;

/* Commit values */
switch (p->type) {
case VALUE_TYPE_I32:
if (!store_value(comp_ctx, p->value, VALUE_TYPE_I32,
Expand Down Expand Up @@ -900,7 +915,7 @@ init_comp_frame(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,

/* No need to initialize aot_frame all cells' committed_ref flags
and all stack cells' ref flags since they have been initialized
as 0 (uncommited and not-reference) by the memset above */
as 0 (uncommitted and not-reference) by the memset above */

return true;
}
Expand Down
Loading

0 comments on commit 01c266c

Please sign in to comment.