Skip to content

Commit 2f2575d

Browse files
committed
Add support for metadata.code.branch_hint section
1 parent 7d05dbc commit 2f2575d

File tree

18 files changed

+282
-7
lines changed

18 files changed

+282
-7
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ if (NOT DEFINED WAMR_BUILD_REF_TYPES)
119119
set (WAMR_BUILD_REF_TYPES 1)
120120
endif ()
121121

122+
if (NOT DEFINED WAMR_BUILD_BRANCH_HINTS)
123+
# Enable branch hints by default
124+
set (WAMR_BUILD_BRANCH_HINTS 1)
125+
endif ()
126+
122127
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
123128

124129
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)

build-scripts/SConscript_config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ if GetDepend(['WAMR_BUILD_REF_TYPES']):
128128
CPPDEFINES += ['WASM_ENABLE_REF_TYPES=1']
129129
print('[WAMR] enable ref types')
130130

131+
if GetDepend(['WAMR_BUILD_BRANCH_HINTS']):
132+
CPPDEFINES += ['WASM_ENABLE_BRANCH_HINTS=1']
133+
print('[WAMR] enable branch hints')
134+
131135
CPPDEFINES += ['BH_MALLOC=wasm_runtime_malloc']
132136
CPPDEFINES += ['BH_FREE=wasm_runtime_free']
133137

build-scripts/config_common.cmake

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ endif ()
213213

214214
if (NOT DEFINED WAMR_BUILD_EXTENDED_CONST_EXPR)
215215
set (WAMR_BUILD_EXTENDED_CONST_EXPR 0)
216+
217+
if (NOT DEFINED WAMR_BUILD_BRANCH_HINTS)
218+
set (WAMR_BUILD_BRANCH_HINTS 1)
216219
endif ()
217220

218221
########################################
@@ -419,6 +422,12 @@ endif ()
419422
if (WAMR_BUILD_REF_TYPES EQUAL 1)
420423
add_definitions (-DWASM_ENABLE_REF_TYPES=1)
421424
endif ()
425+
if (WAMR_BUILD_BRANCH_HINTS EQUAL 1)
426+
add_definitions (-DWASM_ENABLE_BRANCH_HINTS=1)
427+
message (" branch hints enabled")
428+
else ()
429+
message (" branch hints disabled")
430+
endif ()
422431
if (WAMR_BUILD_GC EQUAL 1)
423432
if (WAMR_TEST_GC EQUAL 1)
424433
message(" GC testing enabled")
@@ -708,8 +717,8 @@ message (
708717
" \"Tail call\" via WAMR_BUILD_TAIL_CALL: ${WAMR_BUILD_TAIL_CALL}\n"
709718
" \"Threads\" via WAMR_BUILD_SHARED_MEMORY: ${WAMR_BUILD_SHARED_MEMORY}\n"
710719
" \"Typed Function References\" via WAMR_BUILD_GC: ${WAMR_BUILD_GC}\n"
720+
" \"Branch Hinting\" via WAMR_BUILD_BRANCH_HINTS: ${WAMR_BUILD_BRANCH_HINTS}\n"
711721
" Unsupported (>= Phase4):\n"
712-
" \"Branch Hinting\"\n"
713722
" \"Custom Annotation Syntax in the Text Format\"\n"
714723
" \"Exception handling\"\n"
715724
" \"Import/Export of Mutable Globals\"\n"

core/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ unless used elsewhere */
579579
#define WASM_ENABLE_REF_TYPES 0
580580
#endif
581581

582+
#ifndef WASM_ENABLE_BRANCH_HINTS
583+
#define WASM_ENABLE_BRANCH_HINTS 0
584+
#endif
585+
582586
#ifndef WASM_ENABLE_GC
583587
#define WASM_ENABLE_GC 0
584588
#endif

core/iwasm/aot/aot_loader.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,13 @@ check_feature_flags(char *error_buf, uint32 error_buf_size,
505505
}
506506
#endif
507507

508+
#if WASM_ENABLE_BRANCH_HINTS == 0
509+
if (feature_flags & WASM_ENABLE_BRANCH_HINTS) {
510+
LOG_WARNING(
511+
"branch hints not enabled, but wasm file contains branch hints");
512+
}
513+
#endif
514+
508515
#if WASM_ENABLE_GC == 0
509516
if (feature_flags & WASM_FEATURE_GARBAGE_COLLECTION) {
510517
set_error_buf(error_buf, error_buf_size,

core/iwasm/aot/aot_runtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern "C" {
3535
* and not at the beginning of each function call */
3636
#define WASM_FEATURE_FRAME_PER_FUNCTION (1 << 12)
3737
#define WASM_FEATURE_FRAME_NO_FUNC_IDX (1 << 13)
38+
#define WASM_FEATURE_BRANCH_HINTS (1 << 14)
3839

3940
typedef enum AOTSectionType {
4041
AOT_SECTION_TYPE_TARGET_INFO = 0,
@@ -57,6 +58,7 @@ typedef enum AOTCustomSectionType {
5758
AOT_CUSTOM_SECTION_ACCESS_CONTROL = 2,
5859
AOT_CUSTOM_SECTION_NAME = 3,
5960
AOT_CUSTOM_SECTION_STRING_LITERAL = 4,
61+
AOT_CUSTOM_SECTION_CODE_METADATA = 5,
6062
} AOTCustomSectionType;
6163

6264
typedef struct AOTObjectDataSection {

core/iwasm/compilation/aot.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ aot_create_funcs(const WASMModule *module, uint32 pointer_size)
392392
memset(aot_func, 0, sizeof(AOTFunc));
393393

394394
func_type = aot_func->func_type = func->func_type;
395+
aot_func->func_index = i + module->import_function_count;
395396

396397
/* Resolve function type index */
397398
for (j = 0; j < module->type_count; j++) {
@@ -416,6 +417,9 @@ aot_create_funcs(const WASMModule *module, uint32 pointer_size)
416417
aot_func->local_types_wp = func->local_types;
417418
aot_func->code = func->code;
418419
aot_func->code_size = func->code_size;
420+
#if WASM_ENABLE_BRANCH_HINTS != 0
421+
aot_func->code_body_begin = func->code_body_begin;
422+
#endif
419423

420424
/* Resolve local offsets */
421425
for (j = 0; j < func_type->param_count; j++) {
@@ -872,6 +876,10 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
872876
comp_data->name_section_buf_end = module->name_section_buf_end;
873877
#endif
874878

879+
#if WASM_ENABLE_BRANCH_HINTS != 0
880+
comp_data->function_hints = module->function_hints;
881+
#endif
882+
875883
aot_init_aux_data(comp_data, module);
876884

877885
comp_data->wasm_module = module;

core/iwasm/compilation/aot.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ typedef struct AOTImportFunc {
207207
typedef struct AOTFunc {
208208
AOTFuncType *func_type;
209209
uint32 func_type_index;
210+
uint32 func_index;
210211
uint32 local_count;
211212
uint8 *local_types_wp;
212213
uint16 param_cell_num;
@@ -217,6 +218,9 @@ typedef struct AOTFunc {
217218
/* offset of each local, including function parameters
218219
and local variables */
219220
uint16 *local_offsets;
221+
#if WASM_ENABLE_BRANCH_HINTS != 0
222+
uint8 *code_body_begin;
223+
#endif
220224
} AOTFunc;
221225

222226
typedef struct AOTCompData {
@@ -296,6 +300,10 @@ typedef struct AOTCompData {
296300
#if WASM_ENABLE_DEBUG_AOT != 0
297301
dwarf_extractor_handle_t extractor;
298302
#endif
303+
304+
#if WASM_ENABLE_BRANCH_HINTS != 0
305+
struct WASMCompilationHint **function_hints;
306+
#endif
299307
} AOTCompData;
300308

301309
typedef struct AOTNativeSymbol {

core/iwasm/compilation/aot_compiler.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,9 +1158,16 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
11581158

11591159
case WASM_OP_BR_IF:
11601160
{
1161+
// ip is advanced by one byte for the opcode
1162+
#if WASM_ENABLE_BRANCH_HINTS != 0
1163+
uint32 instr_offset =
1164+
(frame_ip - 0x1) - (func_ctx->aot_func->code_body_begin);
1165+
#else
1166+
uint32 instr_offset = 0;
1167+
#endif
11611168
read_leb_uint32(frame_ip, frame_ip_end, br_depth);
11621169
if (!aot_compile_op_br_if(comp_ctx, func_ctx, br_depth,
1163-
&frame_ip))
1170+
&frame_ip, instr_offset))
11641171
return false;
11651172
break;
11661173
}

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,6 +4501,9 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
45014501
if (comp_ctx->enable_ref_types) {
45024502
obj_data->target_info.feature_flags |= WASM_FEATURE_REF_TYPES;
45034503
}
4504+
if (comp_ctx->enable_branch_hints) {
4505+
obj_data->target_info.feature_flags |= WASM_FEATURE_BRANCH_HINTS;
4506+
}
45044507
if (comp_ctx->enable_gc) {
45054508
obj_data->target_info.feature_flags |= WASM_FEATURE_GARBAGE_COLLECTION;
45064509
}

0 commit comments

Comments
 (0)