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

GC runtime export APIs implement #2154

Merged
merged 13 commits into from
May 5, 2023
Merged

GC runtime export APIs implement #2154

merged 13 commits into from
May 5, 2023

Conversation

kylo5aby
Copy link

No description provided.

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.

Use clang-format-12 -i --style=file <path/to/source/file> to format the source files.

@@ -4,6 +4,8 @@
*/

#include "../wasm_runtime_common.h"
#include "gc_object.h"
#include <stdlib.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't include stdblib.h, it should have been included in wasm_runtime_common.h

Copy link
Author

Choose a reason for hiding this comment

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

Don't include stdblib.h, it should have been included in wasm_runtime_common.h

get it, thanks

@@ -53,6 +78,33 @@ wasm_func_type_get_param_count(WASMFuncType *const func_type)
return 0;
}

wasm_ref_type_t *
wasm_ref_type_normalize(wasm_ref_type_t *ref_type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

put { to the next line

@@ -53,6 +78,33 @@ wasm_func_type_get_param_count(WASMFuncType *const func_type)
return 0;
}

wasm_ref_type_t *
wasm_ref_type_normalize(wasm_ref_type_t *ref_type) {
if (ref_type->value_type != REF_TYPE_HT_NULLABLE) {
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 set ref_type->nullable = false also.

return ref_type;
}
int32_t heap_type = ref_type->heap_type;
switch (heap_type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can convert it directly:

    if (heap_type >= HEAP_TYPE_NONE && heap_type <= HEAP_TYPE_FUNC) {
        ref_type->value_type = (uint8)(REF_TYPE_NULLREF + heap_type - HEAP_TYPE_NONE);
        ref_type->nullable = false;
        ref_type->heap_type = 0;
    }
    else {
        ref_type->nullable = true;
    }

XX(HEAP_TYPE_NONE, REF_TYPE_NULLREF);
#undef XX
}
ref_type->heap_type = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this line

@@ -153,15 +262,41 @@ wasm_ref_type_is_subtype_of(const wasm_ref_type_t *ref_type1,
WASMStructObjectRef
wasm_struct_obj_new_with_typeidx(WASMExecEnv *exec_env, uint32 type_idx)
{
/* TODO */
return NULL;
wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use WASMModuleInstanceCommon * in internal code

Comment on lines 290 to 295
WASMRttTypeRef rtt_type = (WASMRttTypeRef)malloc(sizeof(WASMRttTypeRef));

rtt_type->type_flag = WASM_TYPE_STRUCT;
rtt_type->inherit_depth = type->inherit_depth;
rtt_type->defined_type = (WASMType*)type;
rtt_type->root_type = type->root_type;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use wasm_rtt_type_new to create/get the rtt_type, refer to
https://github.com/bytecodealliance/wasm-micro-runtime/blob/dev/gc_refactor/core/iwasm/interpreter/wasm_interp_classic.c#L1998-L2002

Note that we should check the return value

Copy link
Author

Choose a reason for hiding this comment

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

Use wasm_rtt_type_new to create/get the rtt_type, refer to https://github.com/bytecodealliance/wasm-micro-runtime/blob/dev/gc_refactor/core/iwasm/interpreter/wasm_interp_classic.c#L1998-L2002

Note that we should check the return value

get it, thanks

rtt_type->root_type = type->root_type;

struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
free(rtt_type);
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the line

#endif

for ( ; type_idx < type_count; type_idx++) {
if (wasm_defined_type_equal(types[type_idx], defined_type, module)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use if (types[type_idx] == defined_type)
Or add a common field type_idx for WASMTYPE, WASMFuncType, WASMStructType, WASMArrayType, init it in the loader, and here we get it directly.

@@ -7,6 +7,7 @@
#define _GC_EXPORT_H

#include "wasm_export.h"
#include <stdbool.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems no need to change the return type of wasm_ref_type_set_type_idx, and no need to include stdbool.h?

@@ -14,15 +15,39 @@
uint32
wasm_get_defined_type_count(WASMModuleCommon *const module)
{
/* TODO */
return 0;
WASMModule *wasm_module;
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
WASMModule *wasm_module;

WASMModule is only used when #if WASM_ENABLE_INTERP != 0, move this after line 21


#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
wasm_module = (WASMModule *)module;
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
wasm_module = (WASMModule *)module;
WASMModule *wasm_module = (WASMModule *)module;

core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
if (ref_type->value_type != REF_TYPE_HT_NULLABLE) {
return ref_type;
}
int32_t heap_type = ref_type->heap_type;
Copy link
Collaborator

@xujuntwt95329 xujuntwt95329 Apr 27, 2023

Choose a reason for hiding this comment

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

use int32 in our internal file, and put the variable declaration to the front of the scope

core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
// TODO
#endif

for (; type_idx < type_count; type_idx++) {
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
for (; type_idx < type_count; type_idx++) {
for (type_idx = 0; type_idx < type_count; type_idx++) {

core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

@zhenweijin Can you update the comments for the APIs you implemented?
You can refer to wasm_export.h

Copy link
Author

@kylo5aby kylo5aby Apr 27, 2023

Choose a reason for hiding this comment

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

@zhenweijin Can you update the comments for the APIs you implemented? You can refer to wasm_export.h

Do you mean gc_export.h? some APIs are not declared in gc_common.c, but there have been implemented in gc_object.c, for example wasm_obj_is_anyref_obj/wasm_obj_is_i31_obj

here are the APIs in gc_export.h I have implemented:

  • wasm_get_defined_type_count(const wasm_module_t module);

  • wasm_get_defined_type(const wasm_module_t module, uint32_t index);

  • wasm_defined_type_is_func_type(const wasm_defined_type_t def_type);

  • wasm_defined_type_is_struct_type(const wasm_defined_type_t def_type);

  • wasm_defined_type_is_array_type(const wasm_defined_type_t def_type);

  • wasm_func_type_get_param_count(const wasm_func_type_t func_type);

  • wasm_ref_type_normalize(wasm_ref_type_t *ref_type);

  • wasm_func_type_get_param_type(const wasm_func_type_t func_type,
    uint32_t param_idx);

  • wasm_func_type_get_result_count(const wasm_func_type_t func_type);

  • wasm_func_type_get_result_type

  • wasm_struct_type_get_field_count(const wasm_struct_type_t struct_type);

  • wasm_struct_type_get_field_type

  • wasm_array_type_get_elem_type

  • wasm_defined_type_equal

  • wasm_defined_type_is_subtype_of

  • wasm_ref_type_set_type_idx

  • wasm_ref_type_set_heap_type

  • wasm_ref_type_equal

  • wasm_ref_type_is_subtype_of

  • wasm_struct_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);

  • wasm_struct_obj_new_with_type

  • wasm_struct_obj_set_field (gc_object.c)

  • wasm_struct_obj_get_field (gc_object.c)

  • wasm_array_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);

  • wasm_array_obj_new_with_type

  • wasm_array_obj_set_elem

  • wasm_array_obj_get_elem

  • wasm_array_obj_copy

  • wasm_array_obj_length(const wasm_array_obj_t array_obj);

  • wasm_array_obj_first_elem_addr(const wasm_array_obj_t array_obj);

  • wasm_array_obj_elem_addr(const wasm_array_obj_t array_obj, uint32_t elem_idx);

  • wasm_func_obj_new_with_typeidx(wasm_exec_env_t exec_env, uint32_t type_idx);

  • wasm_func_obj_new_with_type(wasm_exec_env_t exec_env, wasm_func_type_t type);

  • wasm_func_obj_get_func_idx_bound(const wasm_func_obj_t func_obj);

  • wasm_func_obj_get_func_type(const wasm_func_obj_t func_obj);

  • wasm_externref_obj_new(wasm_exec_env_t exec_env, const void *host_obj);

  • wasm_externref_obj_get_value(const wasm_externref_obj_t externref_obj);

  • wasm_anyref_obj_new(wasm_exec_env_t exec_env, const void *host_obj); (gc_object.c)

  • wasm_anyref_obj_get_value(const wasm_anyref_obj_t anyref_obj); (gc_object.c)

  • wasm_externref_obj_to_internal_obj(const wasm_externref_obj_t externref_obj);

  • wasm_internal_obj_to_externref_obj

  • wasm_i31_obj_new(uint32_t i31_value); (gc_object.c)

  • wasm_i31_obj_get_value(wasm_i31_obj_t i31_obj, bool sign_extend); (gc_object.c)

  • wasm_runtime_pin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);

  • wasm_runtime_unpin_object(wasm_exec_env_t exec_env, wasm_obj_t obj);

  • wasm_obj_is_struct_obj(const wasm_obj_t obj); (gc_object.c)

  • wasm_obj_is_array_obj(const wasm_obj_t obj);

  • wasm_obj_is_func_obj(const wasm_obj_t obj);

  • wasm_obj_is_i31_obj(const wasm_obj_t obj); (gc_object.c)

  • wasm_obj_is_externref_obj(const wasm_obj_t obj);

  • wasm_obj_is_anyref_obj(const wasm_obj_t obj); (gc_object.c)

  • wasm_obj_is_internal_obj(const wasm_obj_t obj);

  • wasm_obj_is_eq_obj(const wasm_obj_t obj);

  • wasm_obj_is_instance_of_defined_type

  • wasm_obj_is_instance_of_type_idx

  • wasm_obj_is_instance_of_ref_type

  • wasm_runtime_push_local_object_ref

  • wasm_runtime_pop_local_object_ref(wasm_exec_env_t exec_env);

  • wasm_runtime_pop_local_object_refs(wasm_exec_env_t exec_env, uint32_t n);

@@ -4,6 +4,11 @@
*/

#include "../wasm_runtime_common.h"
#include "bh_assert.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't include header files under core/shared/utils, include "bh_platform.h" instead.

#include "gc_export.h"
#include "gc_object.h"
#include "wasm.h"
#include <stdbool.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to include stdbool.h

}
#endif
#if WASM_ENABLE_AOT != 0
// TODO
Copy link
Contributor

Choose a reason for hiding this comment

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

Use /* .. */ to add comments in C file

}
#endif
#if WASM_ENABLE_AOT != 0
// TODO
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

wasm_value_type_t value_type = ref_type->value_type;
int32 heap_type = ref_type->heap_type;

if (value_type < VALUE_TYPE_NULLREF || value_type > VALUE_TYPE_I32) {
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

if ((value_type >= VALUE_TYPE_V128 && value_type <= VALUE_TYPE_I32)
    || (value_type >= VALUE_TYPE_NULLREF && value_type <= VALUE_TYPE_FUNCREF))

Since there are no value types between VALUE_TYPE_FUNCREF (0x70) and VALUE_TYPE_V128 (0x7B).

Copy link
Author

Choose a reason for hiding this comment

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

Had better

if ((value_type >= VALUE_TYPE_V128 && value_type <= VALUE_TYPE_I32)
    || (value_type >= VALUE_TYPE_NULLREF && value_type <= VALUE_TYPE_FUNCREF))

Since there are no value types between VALUE_TYPE_FUNCREF (0x70) and VALUE_TYPE_V128 (0x7B).

Get it, thanks

core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
// TODO
#endif

bh_assert(rtt_type);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't bh_assert here, rtt_type may be NULL, just check the return value.

Copy link
Author

Choose a reason for hiding this comment

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

Don't bh_assert here, rtt_type may be NULL, just check the return value.

get it.

WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
uint32 type_count = module->type_count;

for (uint32 i = 0; i < type_count; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Declare uint32 i; at the beginning of function.

Comment on lines 383 to 393
for (uint32 i = 0; i < type_count; i++) {
if (module->types[i]->type_flag != WASM_TYPE_STRUCT) {
continue;
}
if (module->types[i] == (WASMType *)type) {
rtt_type = wasm_rtt_type_new(
(WASMType *)type, i, module->rtt_types, module->type_count,
&module->rtt_type_lock);
break;
}
}
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 get i firstly:

    for (i = 0; i < i < type_count; i++) {
        if (module->types[i] == (WASMType *)type) {
            break;
        }
    }
    bh_assert(i < type_count);
    bh_assert(type->type_flag == WASM_TYPE_STRUCT);

And at the end of function:

    rtt_type = wasm_rtt_type_new(
                    (WASMType *)type, i, module->rtt_types, module->type_count,
                    &module->rtt_type_lock);
    if (!rtt_type)
        return NULL;
    struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
    return struct_obj;

// TODO
#endif

bh_assert(rtt_type);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should check the return value but not bh_assert it.

bh_memcpy_s(&ref_type2_norm, (uint32)sizeof(wasm_ref_type_t), ref_type2,
(uint32)sizeof(wasm_ref_type_t));
if (!wasm_ref_type_normalize(&ref_type1_norm)) {
bh_assert(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't bh_assert(0), return false instead

bh_assert(0);
}
if (!wasm_ref_type_normalize(&ref_type2_norm)) {
bh_assert(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't bh_assert(0), return false instead

bh_memcpy_s(&ref_type2_norm, (uint32)sizeof(wasm_ref_type_t), ref_type2,
(uint32)sizeof(wasm_ref_type_t));
if (!wasm_ref_type_normalize(&ref_type1_norm)) {
bh_assert(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't bh_assert(0), return false instead

bh_assert(0);
}
if (!wasm_ref_type_normalize(&ref_type2_norm)) {
bh_assert(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't bh_assert(0), return false instead

core/iwasm/common/gc/gc_common.c Outdated Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Outdated Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Outdated Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c Outdated Show resolved Hide resolved
core/iwasm/common/gc/gc_common.c 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

Copy link
Collaborator

@xujuntwt95329 xujuntwt95329 left a comment

Choose a reason for hiding this comment

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

LGTM

@wenyongh wenyongh merged commit 2e5ba11 into bytecodealliance:dev/gc_refactor May 5, 2023
493 checks passed
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.

None yet

3 participants