Skip to content
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

[instantiation linking] Create and import WASMGlobalInstance #3914

Conversation

lum1n0us
Copy link
Collaborator

No description provided.

@lum1n0us lum1n0us force-pushed the feat/inst_linking_global branch from 92a6310 to eb6a1ef Compare November 24, 2024 23:18
@lum1n0us lum1n0us force-pushed the feat/inst_linking_global branch 2 times, most recently from bcb32d9 to f4b1f30 Compare December 7, 2024 05:16
@lum1n0us lum1n0us marked this pull request as ready for review December 7, 2024 11:28
@wenyongh
Copy link
Contributor

@lum1n0us there are some conflict files in this PR, could you help resolve them so that I can read the code better? Thanks.

…d improve export retrieval logic

Remove me when meeting rebase conflication
@lum1n0us lum1n0us force-pushed the feat/inst_linking_global branch from a84546f to 36b8555 Compare December 12, 2024 08:01
aot_set_last_error("llvm build in bounds gep failed.");
return false;
}
global_ptr = get_global_value_addr(comp_ctx, func_ctx, global_idx);
Copy link
Contributor

Choose a reason for hiding this comment

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

The original code directly gets the global offset at the compilation time and loads global one time in execution. Per my understanding, get_global_value_addr will get the global address by several loads, it will reduce the performance? It seems that it is only needed for import global and only needed when multi-module is enabled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It seems that it is only needed for import global and only needed when multi-module is enabled?

In this case, multi-module means loading linking. By default, it is instantiation linking. Therefore, It always requires to prepare extra loading for an import global.

Yes. There are opportunities for optimization. Like:

  • to a local global.
  • to a built-in global.

I believe we should complete the functionality in this PR first. We can focus on optimization in a separate one.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, since performance and code size are important for the developers, hope the new feature impacts them as less as we can.

#endif
#endif /* WASM_ENABLE_MULTI_MODULE != 0 */
Copy link
Contributor

Choose a reason for hiding this comment

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

When multi-module isn't enabled and libc-builtin is enabled, the global isn't linked by default, is it expected? Or will the iwasm's main.c link the global when instantiating?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

YES. only apply wasm_native_lookup_libc_builtin_global() when executing loading linking. Otherwise, core/iwasm/libraries/libc-builtin/spec_test_builtin_wrapper.c will take over

core/iwasm/aot/aot_runtime.h Show resolved Hide resolved
uint32 global_idx)
{
/* WASMModuleInstance->e */
uint32 e_offset_val = offsetof(WASMModuleInstance, e);
Copy link
Contributor

Choose a reason for hiding this comment

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

The WASMModuleInstanceExtra structure is put at the end of WASMModuleInstance, we can use a fixed offset to get it and its fields, like offset_u32 = get_module_inst_extra_offset(comp_ctx) and then offset = offset_u32 + offsetof(AOTModuleInstanceExtra, globals) for AOT or offset = offset_u32 + offsetof(WASMModuleInstanceExtra, globals) for JIT. By this way, we can save one load operation, refer to create_shared_heap_info:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/compilation/aot_llvm.c#L1528-L1545

LLVMValueRef e_ptr =
LLVMBuildBitCast(comp_ctx->builder, e_ptr_u8, OPQ_PTR_TYPE, "e_ptr");
LLVMValueRef e =
LLVMBuildLoad2(comp_ctx->builder, OPQ_PTR_TYPE, e_ptr, "e");
Copy link
Contributor

Choose a reason for hiding this comment

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

had better check return value of e_offset, e_ptr_u8, e_ptr, e

core/iwasm/compilation/aot_emit_variable.c Show resolved Hide resolved
core/iwasm/compilation/aot_emit_variable.c Show resolved Hide resolved
core/iwasm/compilation/aot_emit_variable.c Show resolved Hide resolved
core/iwasm/interpreter/wasm_runtime.h Show resolved Hide resolved
Copy link
Contributor

@wenyongh wenyongh left a comment

Choose a reason for hiding this comment

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

LGTM

@lum1n0us lum1n0us merged commit b18cb3b into bytecodealliance:dev/instantiate_linking Dec 19, 2024
383 checks passed
@lum1n0us lum1n0us deleted the feat/inst_linking_global branch December 19, 2024 08:52
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.

2 participants