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] Optimize aot func inst list #4018

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

Conversation

lum1n0us
Copy link
Collaborator

…pawned' for clarity in deinstantiation functions
…conv_wasm_c_api and call_conv_raw in WASM function structures
…structure and improving function import handling
WASM_IMPORT_EXPORT_KIND_FUNC, i);
if (!extern_inst) {
LOG_DEBUG("no import function(%s, %s) from imports list, might "
"provied by wasm_native",
Copy link
Contributor

Choose a reason for hiding this comment

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

provided

const char *module_name = module->import_funcs[i].module_name;
const char *field_name = module->import_funcs[i].func_name;
LOG_WARNING("warning: failed to link import function (%s, %s)",
module_name, field_name);
Copy link
Contributor

@wenyongh wenyongh Jan 14, 2025

Choose a reason for hiding this comment

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

This is an intentional warning, it may be important to developer, could we keep it in the new code if link failed?

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.

Per my understanding, the import function doesn't have to be linked and runtime can print warning here, and during execution, the aot code or runtime throws exception "failed to call unlinked import function". The original behavior is like that, we had better not change 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.

Regarding that matter, I am inclined to align with the specification requirements, specifically Step #3 and Step #4. If I understand correctly, the linking error should be reported during the installation phase rather than at execution time.

Our current implementation deviates from this, and I intend to correct it.

In this PR, the wasm_runtime implementation is also incorrect and needs to be addressed. I plan to fix both issues in subsequent pull requests.

Copy link
Contributor

Choose a reason for hiding this comment

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

That brings convenience to developers and is a common handle in many VMs like jvm. In many cases the unlinked import function isn't called and developer can still call other functions. No developer reports it a problem, I strongly suggest to keep this. If you want to fix this, had better open another PR to let others to review/discuss and not fix it in this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In many cases the unlinked import function isn't called and developer can still call other functions.

However, this does not align with the specification definition. I believed the purpose of creating the "instantiation linking" series of pull requests was to align with the specification requirements. Otherwise, our main branch code is functioning quite well.

extra->function_count = module->import_func_count + module->func_count;
if (extra->function_count > 0) {
extra->import_functions =
functions_instantiate(module_inst, module, imports, import_count,
Copy link
Contributor

Choose a reason for hiding this comment

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

how about renaming it to import_functions_instantiate?

Copy link
Contributor

Choose a reason for hiding this comment

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

If this file isn't needed, had better remove it?

Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

}

*imports_dst =
malloc_internal(sizeof(WASMExternInstance) * imports_src->num_elems);
Copy link
Contributor

Choose a reason for hiding this comment

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

how about (uint64)sizeof(WASMExternInstance)?

@@ -225,7 +238,7 @@ typedef struct AOTModule {

/* function info */
uint32 func_count;
/* func pointers of AOTed (un-imported) functions */
/* func pointers of AOTed (imported + local) functions */
Copy link
Contributor

Choose a reason for hiding this comment

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

how about also changing comment of L243?

Copy link
Collaborator Author

@lum1n0us lum1n0us Jan 15, 2025

Choose a reason for hiding this comment

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

func_type_indexes still only stores local(un-imported) functions.

{
AOTFunctionInstance *function = runtime_malloc(
sizeof(AOTFunctionInstance) + sizeof(AOTImportFunc), NULL, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

A little confused why need to plus sizeof(AOTImportFunc)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am considering that this would enable host APIs (defined in wasm_export.h) to create an AOTFunctionInstance from scratch and pass it to the runtime for execution. The runtime would then use the u.func_import field to retrieve some information. However, there might be room for optimization in subsequent pull requests.

wasm_create_function_empty(const WASMModule *module)
{
WASMFunctionInstance *function = runtime_malloc(
sizeof(WASMFunctionInstance) + sizeof(WASMFunctionImport), NULL, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to allocate extra memory (+ sizeof(WASMFunctionImport))?

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.

That brings convenience to developers and is a common handle in many VMs like jvm. In many cases the unlinked import function isn't called and developer can still call other functions. No developer reports it a problem, I strongly suggest to keep this. If you want to fix this, had better open another PR to let others to review/discuss and not fix it in this PR.

@lum1n0us
Copy link
Collaborator Author

In many cases the unlinked import function isn't called and developer can still call other functions.

However, this does not align with the specification definition. I believed the purpose of creating the "instantiation linking" series of pull requests was to align with the specification requirements. Otherwise, our main branch code is functioning quite well.

@wenyongh
Copy link
Contributor

In many cases the unlinked import function isn't called and developer can still call other functions.

However, this does not align with the specification definition. I believed the purpose of creating the "instantiation linking" series of pull requests was to align with the specification requirements. Otherwise, our main branch code is functioning quite well.

It reports the error during the execution time but not instantiation time, it doesn't strictly obey the spec. But sometimes the user experience is more important, in fact, we also do some special handlings in other places. I still think it is good to keep it, it also reports error and it is more friendly to developers.

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