1212#endif
1313#include "../aot/aot_runtime.h"
1414#include "../interpreter/wasm_loader.h"
15+ #include "../common/wasm_loader_common.h"
1516
1617#if WASM_ENABLE_DEBUG_AOT != 0
1718#include "debug/dwarf_extractor.h"
@@ -87,11 +88,10 @@ format_block_name(char *name, uint32 name_size, uint32 block_index,
8788 } \
8889 } while (0)
8990
90- #define BUILD_COND_BR_V (value_if , block_then , block_else , instr ) \
91- do { \
92- if (instr = LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
93- block_else), \
94- !instr) { \
91+ #define BUILD_COND_BR_V (value_if , block_then , block_else , instr ) \
92+ do { \
93+ if (!(instr = LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
94+ block_else))) { \
9595 aot_set_last_error("llvm build cond br failed."); \
9696 goto fail; \
9797 } \
@@ -267,34 +267,30 @@ restore_frame_sp_for_op_end(AOTBlock *block, AOTCompFrame *aot_frame)
267267
268268#if WASM_ENABLE_BRANCH_HINTS != 0
269269static void
270- aot_emit_branch_hint (LLVMContextRef ctxt , AOTFuncContext * func_ctx ,
270+ aot_emit_branch_hint (AOTCompContext * comp_ctx , AOTFuncContext * func_ctx ,
271271 uint32 offset , LLVMValueRef br_if_instr )
272272{
273- struct WASMCompilationHintBranchHint * hint =
274- (struct WASMCompilationHintBranchHint * )func_ctx -> function_hints ;
273+ struct WASMCompilationHint * hint = func_ctx -> function_hints ;
275274 while (hint != NULL ) {
276275 if (hint -> type == WASM_COMPILATION_BRANCH_HINT
277- && hint -> offset == offset ) {
276+ && ((struct WASMCompilationHintBranchHint * )hint )-> offset
277+ == offset ) {
278278 break ;
279279 }
280280 hint = hint -> next ;
281281 }
282282 if (hint != NULL ) {
283- LLVMMetadataRef header = LLVMMDStringInContext2 (
284- ctxt , "branch_weights" , 14 /* strlen("branch_hint") */ );
285283 // same weight llvm MDBuilder::createLikelyBranchWeights assigns
286284 const uint32_t likely_weight = (1U << 20 ) - 1 ;
287285 const uint32_t unlikely_weight = 1 ;
288- LLVMMetadataRef true_w = LLVMValueAsMetadata (LLVMConstInt (
289- LLVMInt32TypeInContext (ctxt ),
290- hint -> is_likely ? likely_weight : unlikely_weight , false));
291- LLVMMetadataRef false_w = LLVMValueAsMetadata (LLVMConstInt (
292- LLVMInt32TypeInContext (ctxt ),
293- hint -> is_likely ? unlikely_weight : likely_weight , false));
294- LLVMMetadataRef mds [] = { header , true_w , false_w };
295- LLVMMetadataRef md = LLVMMDNodeInContext2 (ctxt , mds , 3 );
296- LLVMValueRef md_val = LLVMMetadataAsValue (ctxt , md );
297- LLVMSetMetadata (br_if_instr , LLVMGetMDKindID ("prof" , 4 ), md_val );
286+ aot_set_cond_br_weights (
287+ comp_ctx , br_if_instr ,
288+ ((struct WASMCompilationHintBranchHint * )hint )-> is_likely
289+ ? likely_weight
290+ : unlikely_weight ,
291+ ((struct WASMCompilationHintBranchHint * )hint )-> is_likely
292+ ? unlikely_weight
293+ : likely_weight );
298294 }
299295}
300296#endif
@@ -723,8 +719,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
723719 block -> llvm_else_block , br_if_val );
724720 const uint32 off =
725721 * p_frame_ip - func_ctx -> aot_func -> code_body_begin ;
726- aot_emit_branch_hint (comp_ctx -> context , func_ctx , off ,
727- br_if_val );
722+ aot_emit_branch_hint (comp_ctx , func_ctx , off , br_if_val );
728723#else
729724 BUILD_COND_BR (value , block -> llvm_entry_block ,
730725 block -> llvm_else_block );
@@ -738,8 +733,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
738733 block -> llvm_end_block , br_if_val );
739734 const uint32 off =
740735 * p_frame_ip - func_ctx -> aot_func -> code_body_begin ;
741- aot_emit_branch_hint (comp_ctx -> context , func_ctx , off ,
742- br_if_val );
736+ aot_emit_branch_hint (comp_ctx , func_ctx , off , br_if_val );
743737#else
744738 BUILD_COND_BR (value , block -> llvm_entry_block ,
745739 block -> llvm_end_block );
@@ -1090,8 +1084,7 @@ aot_compile_op_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
10901084
10911085static bool
10921086aot_compile_conditional_br (AOTCompContext * comp_ctx , AOTFuncContext * func_ctx ,
1093- uint32 br_depth , LLVMValueRef value_cmp ,
1094- uint8 * * p_frame_ip , uint32 off )
1087+ LLVMValueRef value_cmp , uint8 * * p_frame_ip )
10951088{
10961089 AOTBlock * block_dst ;
10971090 LLVMValueRef value , * values = NULL ;
@@ -1100,6 +1093,17 @@ aot_compile_conditional_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
11001093 uint32 i , param_index , result_index ;
11011094 uint64 size ;
11021095
1096+ // ip is advanced by one byte for the opcode
1097+ #if WASM_ENABLE_BRANCH_HINTS != 0
1098+ uint32 instr_offset =
1099+ (* p_frame_ip - 0x1 ) - (func_ctx -> aot_func -> code_body_begin );
1100+ #else
1101+ uint32 instr_offset = 0 ;
1102+ #endif
1103+ uint64 br_depth ;
1104+ if (!read_leb (p_frame_ip , * p_frame_ip + 5 , 32 , false, & br_depth , NULL , 0 ))
1105+ return false;
1106+
11031107 if (!(block_dst = get_target_block (func_ctx , br_depth ))) {
11041108 return false;
11051109 }
@@ -1176,7 +1180,7 @@ aot_compile_conditional_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
11761180 LLVMValueRef br_if_val = NULL ;
11771181 BUILD_COND_BR_V (value_cmp , block_dst -> llvm_entry_block ,
11781182 llvm_else_block , br_if_val );
1179- aot_emit_branch_hint (comp_ctx -> context , func_ctx , off , br_if_val );
1183+ aot_emit_branch_hint (comp_ctx , func_ctx , instr_offset , br_if_val );
11801184#else
11811185 BUILD_COND_BR (value_cmp , block_dst -> llvm_entry_block ,
11821186 llvm_else_block );
@@ -1227,7 +1231,7 @@ aot_compile_conditional_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
12271231 LLVMValueRef br_if_val = NULL ;
12281232 BUILD_COND_BR_V (value_cmp , block_dst -> llvm_end_block ,
12291233 llvm_else_block , br_if_val );
1230- aot_emit_branch_hint (comp_ctx -> context , func_ctx , off , br_if_val );
1234+ aot_emit_branch_hint (comp_ctx , func_ctx , instr_offset , br_if_val );
12311235#else
12321236 BUILD_COND_BR (value_cmp , block_dst -> llvm_end_block ,
12331237 llvm_else_block );
@@ -1255,14 +1259,14 @@ aot_compile_conditional_br(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
12551259
12561260bool
12571261aot_compile_op_br_if (AOTCompContext * comp_ctx , AOTFuncContext * func_ctx ,
1258- uint32 br_depth , uint8 * * p_frame_ip , uint32 off )
1262+ uint8 * * p_frame_ip )
12591263{
12601264 LLVMValueRef value_cmp ;
12611265
12621266 POP_COND (value_cmp );
12631267
1264- return aot_compile_conditional_br (comp_ctx , func_ctx , br_depth , value_cmp ,
1265- p_frame_ip , off );
1268+ return aot_compile_conditional_br (comp_ctx , func_ctx , value_cmp ,
1269+ p_frame_ip );
12661270fail :
12671271 return false;
12681272}
0 commit comments