Skip to content

Add support for metadata.code.branch_hint section #4460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lukasdoe
Copy link

@Lukasdoe Lukasdoe commented Jul 7, 2025

Since we're currently in the progress of upstreaming the generation of the code metadata branch hint section in llvm (PR), I also implemented support for this feature in wamr.

One important consideration for the implementation was to keep the encoding of hints generic, so that more compilation hints could be added in the future.

@Lukasdoe
Copy link
Author

Lukasdoe commented Jul 7, 2025

(rebased on main)

}
if (hint != NULL) {
LLVMMetadataRef header = LLVMMDStringInContext2(
ctxt, "branch_weights", 14 /* strlen("branch_hint") */);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stale comment?

do { \
if (instr = LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
block_else), \
!instr) { \
Copy link
Collaborator

@yamt yamt Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we usually use a style like:

if (!(instr = ...)) {

or

instr = ....;
if (!instr) {

@@ -57,6 +58,7 @@ typedef enum AOTCustomSectionType {
AOT_CUSTOM_SECTION_ACCESS_CONTROL = 2,
AOT_CUSTOM_SECTION_NAME = 3,
AOT_CUSTOM_SECTION_STRING_LITERAL = 4,
AOT_CUSTOM_SECTION_CODE_METADATA = 5,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for?

@@ -35,6 +35,7 @@ extern "C" {
* and not at the beginning of each function call */
#define WASM_FEATURE_FRAME_PER_FUNCTION (1 << 12)
#define WASM_FEATURE_FRAME_NO_FUNC_IDX (1 << 13)
#define WASM_FEATURE_BRANCH_HINTS (1 << 14)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this flag?
branch hinting doesn't produce any incompatibility in the aot file, does it?

@@ -215,6 +215,10 @@ if (NOT DEFINED WAMR_BUILD_EXTENDED_CONST_EXPR)
set (WAMR_BUILD_EXTENDED_CONST_EXPR 0)
endif ()

if (NOT DEFINED WAMR_BUILD_BRANCH_HINTS)
set (WAMR_BUILD_BRANCH_HINTS 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any benefit to enable this besides wamr-compiler?

Copy link
Collaborator

@lum1n0us lum1n0us left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any possibility that the interpreter can also benefit from this feature?

@@ -708,8 +718,8 @@ message (
" \"Tail call\" via WAMR_BUILD_TAIL_CALL: ${WAMR_BUILD_TAIL_CALL}\n"
" \"Threads\" via WAMR_BUILD_SHARED_MEMORY: ${WAMR_BUILD_SHARED_MEMORY}\n"
" \"Typed Function References\" via WAMR_BUILD_GC: ${WAMR_BUILD_GC}\n"
" \"Branch Hinting\" via WAMR_BUILD_BRANCH_HINTS: ${WAMR_BUILD_BRANCH_HINTS}\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please maintain alphabetical order.

@@ -579,6 +579,10 @@ unless used elsewhere */
#define WASM_ENABLE_REF_TYPES 0
#endif

#ifndef WASM_ENABLE_BRANCH_HINTS
#define WASM_ENABLE_BRANCH_HINTS 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consistent with the content of config_common.cmake.

Suggested change
#define WASM_ENABLE_BRANCH_HINTS 0
#define WASM_ENABLE_BRANCH_HINTS 1

add_definitions (-DWASM_ENABLE_BRANCH_HINTS=1)
message (" branch hints enabled")
else ()
message (" branch hints disabled")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
message (" branch hints disabled")
add_definitions (-DWASM_ENABLE_BRANCH_HINTS=0)
message (" branch hints disabled")

Make sure WASM_ENABLE_BRANCH_HINTS is defined.

@@ -217,6 +218,9 @@ typedef struct AOTFunc {
/* offset of each local, including function parameters
and local variables */
uint16 *local_offsets;
#if WASM_ENABLE_BRANCH_HINTS != 0
uint8 *code_body_begin;
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest using existed filed code at line 217 instead of creating a new field.

@@ -207,6 +207,7 @@ typedef struct AOTImportFunc {
typedef struct AOTFunc {
AOTFuncType *func_type;
uint32 func_type_index;
uint32 func_index;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unused.

enum WASMCompilationHintType type;
};
struct WASMCompilationHintBranchHint {
void *next;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void *next;
struct WASMCompilationHintBranchHint *next;

};
struct WASMCompilationHint {
struct WASMCompilationHint *next;
enum WASMCompilationHintType type;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a new field like struct WASMCompilationHintBranchHint *branch_hints?

uint32 error_buf_size)
{
if (module->function_hints == NULL) {
module->function_hints = loader_malloc(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somewhere should release allocations

uint32 num_hints;
read_leb_uint32(buf, buf_end, num_hints);
for (uint32 j = 0; j < num_hints; ++j) {
struct WASMCompilationHintBranchHint *new_hint =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somewhere should release allocations

struct WASMCompilationHintBranchHint *new_hint =
loader_malloc(sizeof(struct WASMCompilationHintBranchHint),
error_buf, error_buf_size);
new_hint->next = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since L5555 has already learned num_hints, it is better to malloc() once outside of the loop to reduce memory fragments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants