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 WASMFunctionInstance #3947

Open
wants to merge 22 commits into
base: dev/instantiate_linking
Choose a base branch
from

Conversation

lum1n0us
Copy link
Collaborator

@lum1n0us lum1n0us commented Dec 5, 2024

No description provided.

@lum1n0us lum1n0us force-pushed the feat/inst_linking_function branch from 68f35cb to 261f2a8 Compare December 7, 2024 11:28
@lum1n0us lum1n0us marked this pull request as ready for review December 7, 2024 12:10
…pawned' for clarity in deinstantiation functions
…conv_wasm_c_api and call_conv_raw in WASM function structures
…structure and improving function import handling
@lum1n0us lum1n0us force-pushed the feat/inst_linking_function branch from 1c78639 to cbb33b4 Compare December 22, 2024 02:59
@lum1n0us
Copy link
Collaborator Author

lum1n0us commented Jan 9, 2025

@wenyongh Reviewing this extensive document may be time-consuming, but I still require your input to begin the subsequent optimization process. ☕ 🍺

set_error_buf_v(error_buf, error_buf_size,
"unknown import module \"%s\"",
import->module_name);
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should destroy functions before return, same as the below return NULL lines.

import_func->module_name, import_func->func_name);

*func_ptrs = import_func->func_ptr_linked;
bh_assert(*func_ptrs);
Copy link
Contributor

Choose a reason for hiding this comment

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

The import func ptr is not "must be prepared", the original code in aot loader reports "warning: failed to link import function", and when execution, runtime throws "failed to call unlinked import function". We had better not change the behavior?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If my memory serves me right, the initial design was due to AOT not fully supporting the multi-module feature until it was contributed by the community. However, it wasn't a complete implementation and had some gaps, such as the one we're discussing.

In this round, I'm inclined to align with the specification requirements and maintain the same expected behavior as the wasm_runtime. Like: all functions, both imported and local ones should be set up prior to execution..

uint64 total_size = sizeof(AOTFunctionInstance) * (uint64)function_count;

AOTFunctionInstance *functions =
runtime_malloc(total_size, error_buf, error_buf_size);
Copy link
Contributor

Choose a reason for hiding this comment

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

In the original implementation, we first only create function instances of export functions instead all the instances. The latter are only created in special and rare situation, e.g., called from wasm_table_get_func_inst. It may increase the memory consumption a lot, e.g., there are lots of wasm functions but only several functions are exported, I think we had better not change the optimization, or only create import/export func instances at first, or discuss with others?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

only create import/export func instances

It seems feasible. Let me start working on it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

put it in #4018

/* write it from wasm_c_api */
bool call_conv_wasm_c_api;
/* AOTModuleInstance collects it to form c_api_func_imports */
CApiFuncImport import_func_c_api;
Copy link
Contributor

Choose a reason for hiding this comment

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

These fields are only required by import functions, adding them here will increase memory consumption when non-import function instances are created, could we get them or most of them from the AOTImportFunc *func_import?

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's quite challenging to do that.

AOTModuleInstance *import_module_inst and struct AOTFunctionInstance *import_func_inst are used for instantiation linking with other modules, so we need to retain both here.

bool call_conv_raw is utilized for registered native symbols and can be taken from AOTImportFunc.

bool call_conv_wasm_c_api and CApiFuncImport import_func_c_api are created by the wasm_c_api APIs. Both should be capable of instantiation linking.

Therefore, only one bool field is duplicated from AOTImportFunc. The other fields need to remain in AOTFunctionInstance to meet the requirements of instantiation linking.

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