Skip to content

Commit 5931aaa

Browse files
authored
aot compiler: Place precheck wrapper before the corresponding wrapped function (#3141)
This increases the chance to use "short" calls. Assumptions: - LLVM preserves the order of functions in a module - The wrapper function are smaller than the wrapped functions - The target CPU has "short" PC-relative variation of call/jmp instructions and they are preferrable over the "long" ones. A motivation: - To avoid some relocations for XIP, I want to use xtensa PC-relative call instructions, which can only reach ~512KB.
1 parent 6e547ba commit 5931aaa

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

core/iwasm/compilation/aot_llvm.c

+15-17
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,18 @@ get_inst_extra_offset(AOTCompContext *comp_ctx)
263263
* - update native_stack_top_min if necessary
264264
* - stack overflow check (if it does, trap)
265265
*/
266-
static LLVMValueRef
267-
aot_add_precheck_function(AOTCompContext *comp_ctx, LLVMModuleRef module,
268-
uint32 func_index, uint32 orig_param_count,
269-
LLVMTypeRef func_type, LLVMValueRef wrapped_func)
266+
static bool
267+
aot_build_precheck_function(AOTCompContext *comp_ctx, LLVMModuleRef module,
268+
LLVMValueRef precheck_func, uint32 func_index,
269+
LLVMTypeRef func_type, LLVMValueRef wrapped_func)
270270
{
271-
LLVMValueRef precheck_func;
272271
LLVMBasicBlockRef begin = NULL;
273272
LLVMBasicBlockRef check_top_block = NULL;
274273
LLVMBasicBlockRef update_top_block = NULL;
275274
LLVMBasicBlockRef stack_bound_check_block = NULL;
276275
LLVMBasicBlockRef call_wrapped_func_block = NULL;
277276
LLVMValueRef *params = NULL;
278277

279-
precheck_func =
280-
aot_add_llvm_func1(comp_ctx, module, func_index, orig_param_count,
281-
func_type, AOT_FUNC_PREFIX);
282-
if (!precheck_func) {
283-
goto fail;
284-
}
285278
begin = LLVMAppendBasicBlockInContext(comp_ctx->context, precheck_func,
286279
"begin");
287280
check_top_block = LLVMAppendBasicBlockInContext(
@@ -550,13 +543,13 @@ aot_add_precheck_function(AOTCompContext *comp_ctx, LLVMModuleRef module,
550543
}
551544
}
552545

553-
return precheck_func;
546+
return true;
554547
fail:
555548
if (params != NULL) {
556549
wasm_runtime_free(params);
557550
}
558551
aot_set_last_error("failed to build precheck wrapper function.");
559-
return NULL;
552+
return false;
560553
}
561554

562555
/**
@@ -626,7 +619,14 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, LLVMModuleRef module,
626619
const char *prefix = AOT_FUNC_PREFIX;
627620
const bool need_precheck =
628621
comp_ctx->enable_stack_bound_check || comp_ctx->enable_stack_estimation;
622+
LLVMValueRef precheck_func;
629623
if (need_precheck) {
624+
precheck_func = aot_add_llvm_func1(comp_ctx, module, func_index,
625+
aot_func_type->param_count,
626+
func_type, AOT_FUNC_PREFIX);
627+
if (!precheck_func) {
628+
goto fail;
629+
}
630630
/*
631631
* REVISIT: probably this breaks windows hw bound check
632632
* (the RtlAddFunctionTable stuff)
@@ -671,10 +671,8 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, LLVMModuleRef module,
671671
LLVMAddAttributeAtIndex(func, LLVMAttributeFunctionIndex,
672672
attr_noinline);
673673

674-
LLVMValueRef precheck_func = aot_add_precheck_function(
675-
comp_ctx, module, func_index, aot_func_type->param_count, func_type,
676-
func);
677-
if (!precheck_func)
674+
if (!aot_build_precheck_function(comp_ctx, module, precheck_func,
675+
func_index, func_type, func))
678676
goto fail;
679677
LLVMAddAttributeAtIndex(precheck_func, LLVMAttributeFunctionIndex,
680678
attr_noinline);

0 commit comments

Comments
 (0)