Skip to content

implement extended const expr #4318

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 5 commits into
base: dev/extended_const
Choose a base branch
from

Conversation

kylo5aby
Copy link
Contributor

@kylo5aby kylo5aby commented Jun 3, 2025

  • extended const for interp
  • extended const for aot
  • tests
  • CI

@kylo5aby kylo5aby changed the title implement extended const expr for interp wip: implement extended const expr Jun 3, 2025
@kylo5aby kylo5aby mentioned this pull request Jun 3, 2025
@kylo5aby kylo5aby force-pushed the extended-const-expr branch 2 times, most recently from 09367e2 to 019a93f Compare June 3, 2025 07:25
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
case INIT_EXPR_TYPE_I32_ADD:
case INIT_EXPR_TYPE_I32_SUB:
case INIT_EXPR_TYPE_I32_MUL:
Copy link
Collaborator

Choose a reason for hiding this comment

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

i guess you can share the logic between i32 and i64.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i guess you can share the logic between i32 and i64.

Resolved. thanks

#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
struct InitializerExpression *l_expr;
struct InitializerExpression *r_expr;
#endif
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 cases where you need both of value (u) and exprs (l_expr/r_expr)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there any cases where you need both of value (u) and exprs (l_expr/r_expr)?

iiuc, for unary init expr, only value (u) is needed, for binary init expr, exprs (l_expr/r_expr) are needed to store sub-exprs in order to compute the expr value at runtime.

Copy link
Collaborator

@yamt yamt Jun 4, 2025

Choose a reason for hiding this comment

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

can they be a union then?

eg.

union {
   struct {
      WASMValue value;
   } unary;
   struct {
      struct InitializerExpression *l_expr;
      struct InitializerExpression *r_expr;
   } binary;
} u;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can they be a union then?

eg.

union {
   struct {
      WASMValue value;
   } unary;
   struct {
      struct InitializerExpression *l_expr;
      struct InitializerExpression *r_expr;
   } binary;
} u;

I think this is ok, and will save more memories after enable extended const expr, I will update this after finish the aot part, thanks the suggestion.

@yamt
Copy link
Collaborator

yamt commented Jun 3, 2025

i guess it's safer to have a small limit for the stack height in the loader.

@kylo5aby kylo5aby force-pushed the extended-const-expr branch 12 times, most recently from 63c429b to 669c1b8 Compare June 9, 2025 03:51
@kylo5aby
Copy link
Contributor Author

kylo5aby commented Jun 9, 2025

i guess it's safer to have a small limit for the stack height in the loader.

I didn't see a limit value specified in spec. and in this discussion, there is a suggestion to limit global initializer go with the function body limit of 7654321 bytes WebAssembly/extended-const#15 (comment), do you have any suggestions?

@kylo5aby kylo5aby force-pushed the extended-const-expr branch from 669c1b8 to 2233ff4 Compare June 9, 2025 04:04
@kylo5aby kylo5aby changed the title wip: implement extended const expr implement extended const expr Jun 9, 2025
@kylo5aby kylo5aby force-pushed the extended-const-expr branch 2 times, most recently from e8e5ba2 to 36a8996 Compare June 9, 2025 09:59
@kylo5aby kylo5aby force-pushed the extended-const-expr branch from 36a8996 to 0f475a5 Compare June 9, 2025 10:27
@@ -695,11 +709,11 @@ 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"
" \"Extended Constant Expressions\" via WAMR_BUILD_EXTENDED_CONST_EXPR: ${WAMR_BUILD_EXTENDED_CONST_EXPR}\n"
Copy link
Collaborator

Choose a reason for hiding this comment

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

keep alphabeta order

data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
data_list[i]->offset.u.i64 = (int64)init_expr_value;
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
&offset_expr, sizeof(InitializerExpression));
Copy link
Collaborator

Choose a reason for hiding this comment

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

better be something like: xxx->offset = offset_expr. Compiler will check the type.

else()
add_definitions(-DWASM_ENABLE_EXTENDED_CONST_EXPR=0)
endif()

Copy link
Collaborator

Choose a reason for hiding this comment

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

we might tend to enable it by default in wamrc. Then, wamrc can use a command line option to switch features.

value_type = VALUE_TYPE_I64;
}

if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please provide a quick introduction to the binary format of a constant expression. It will be helpful.

#endif
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
if (is_expr_binary_op(data_list[i]->offset.init_expr_type)) {
destroy_sub_init_expr(&data_list[i]->offset);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it's a good idea to refactor the destroy_init_expr and merge the code logic of destroy_sub_init_expr into it.

|| offset_flag == INIT_EXPR_TYPE_FUNCREF_CONST
|| offset_flag == INIT_EXPR_TYPE_REFNULL_CONST
|| (tbl_inst->is_table64
? (offset_flag == INIT_EXPR_TYPE_I64_CONST
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should add a function/macro for this check since it appears several times, and use macro to control it, for it is only valid when extended const proposal is on.

uint8 offset_flag = data_seg->base_offset.init_expr_type;
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
|| (memory->is_memory64
? (offset_flag == INIT_EXPR_TYPE_I64_CONST
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as aot_runtime.c Maybe we should add a function/macro for this check since it appears several times, and use macro to control it, for it is only valid when extendedd const proposal is on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as aot_runtime.c Maybe we should add a function/macro for this check since it appears several times, and use macro to control it, for it is only valid when extendedd const proposal is on.

resolved.


#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
void
destroy_init_expr_recursive(InitializerExpression *expr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it's a good idea that we extract the destroy_init_expr in wasm_loader.c and unify them here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it's a good idea that we extract the destroy_init_expr in wasm_loader.c and unify them here

resolved.


#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
void
destroy_init_expr_recursive(InitializerExpression *expr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since those two APIs are only used in the loader, better to put them in wasm_loader_common.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since those two APIs are only used in the loader, better to put them in wasm_loader_common.h

resolved. thanks

@kylo5aby kylo5aby force-pushed the extended-const-expr branch 2 times, most recently from 45c71ed to 1f3d64e Compare June 11, 2025 02:47
@kylo5aby kylo5aby force-pushed the extended-const-expr branch from 1f3d64e to 759d61d Compare June 11, 2025 03:07
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.

4 participants