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

Enable AOT usage on M1 mac #2618

Merged

Conversation

eloparco
Copy link
Contributor

@eloparco eloparco commented Oct 5, 2023

After reading the docs, I managed to use AOT on my M1 mac.

Issue: #1365

Copy link
Contributor

@cosmo0920 cosmo0920 left a comment

Choose a reason for hiding this comment

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

Thanks for your work. I confirmed that it works!!

% sw_vers
ProductName:		macOS
ProductVersion:		14.0
BuildVersion:		23A344
% git fetch upstream pull/2618/head:pr-2618
% git checkout pr-2618

Build wamrc and basic samples with the PR patch.

% ../../wamr-compiler/wamrc --size-level=3 -o ./out/wasm-apps/testapp.aot ./out/wasm-apps/testapp.wasm
Create AoT compiler with:
  target:        arm64
  target cpu:    apple-m1
  target triple: arm64-pc-linux-gnu
  cpu features:  
  opt level:     3
  size level:    3
  output format: AoT file
Compile success, file ./out/wasm-apps/testapp.aot was generated.
% file ./out/wasm-apps/testapp.aot
./out/wasm-apps/testapp.aot: data
% ./out/basic -f out/wasm-apps/testapp.aot
calling into WASM function: generate_float
Native finished calling wasm function generate_float(), returned a float value: 102009.921875f
calling into WASM function: float_to_string
calling into native function: intToStr
calling into native function: get_pow
calling into native function: intToStr
Native finished calling wasm function: float_to_string, returned a formatted string: 102009.921
calling into WASM function: calculate
calling into native function: calculate_native, n=3, func1=1, func2=2
calling into WASM function: mul5,    mul5 return 15 
call func1 and return n1=15
calling into WASM function: mul7,    mul7 return 105 
call func2 and return n2=105
Native finished calling wasm function: calculate, return: 120

@yamt
Copy link
Collaborator

yamt commented Oct 6, 2023

After reading the docs, I managed to use AOT on my M1 mac.

Issue: #1365

it seems inefficient to call pthread_jit_write_protect_np before every calls.
isn't it enough to enable write temporary, only for text relocation?

also, the doc you mentioned seems suggesting to invalidate icache. isn't it necessary?

@eloparco eloparco force-pushed the eloparco/enable-aot-on-m1-mac branch 2 times, most recently from 72ccf91 to 98ad518 Compare October 6, 2023 09:23
@eloparco
Copy link
Contributor Author

eloparco commented Oct 6, 2023

Thanks for your work.

Thanks for starting the investigation, you input was was very useful!

it seems inefficient to call pthread_jit_write_protect_np before every calls.

Moved that to call it only before and after module loading

isn't it enough to enable write temporary, only for text relocation?

Yes, I put it before and after load so that we don't need to call it in multiple places inside load

also, the doc you mentioned seems suggesting to invalidate icache. isn't it necessary?

Added that now

#if defined(__APPLE__) && defined(__arm64__)
pthread_jit_write_protect_np(true); // Make memory executable
sys_icache_invalidate(module->code, module->code_size);
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

We usually don't directly call the libc functions in core/iwasm, instead, we encapsulated them in the platform layer and call them. How about adding API in core/shared/platform, e.g. void os_thread_jit_write_protect_np(bool enabled), in:

https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/shared/platform/include/platform_api_vmcore.h#L83

and:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/shared/platform/common/posix/posix_thread.c#L420

and call it here.

and how about wrappring sys_icache_invalidate to os_icache_flush? like os_dcache_flush:

https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/shared/platform/include/platform_api_vmcore.h#L144

https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/shared/platform/common/posix/posix_memmap.c#L253C1-L253C1

And had better set memory executable after ret is true. Reference code:

#if defined(__APPLE__) && defined(__arm64__)
    os_thread_jit_write_protect_np(false); /* Make memory writable */
#endif

    if (!load(buf, size, module, error_buf, error_buf_size)) {
        aot_unload(module);
        return NULL;
    }

#if defined(__APPLE__) && defined(__arm64__)
    if (module->code) {
        os_thread_jit_write_protect_np(true); /* Make memory executable */
        os_icache_flush(module->code, module->code_size);
    }
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I should have known.
The ifdef should be inside the platform-specific code I think, and no need for if (module->code) since load would have returned false if that was NULL.

os_thread_jit_write_protect_np(false); /* Make memory writable */
if (!load(buf, size, module, error_buf, error_buf_size)) {
    aot_unload(module);
    return NULL;
}
os_thread_jit_write_protect_np(true); /* Make memory executable */
os_icache_flush(module->code, module->code_size);

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, module->code may be NULL if the module hasn't function bytecodes, but I think the pthread_jit_write_protect_np should have handle that case.

@eloparco eloparco force-pushed the eloparco/enable-aot-on-m1-mac branch from 98ad518 to 4abe771 Compare October 6, 2023 14:12
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

@wenyongh wenyongh merged commit 3668093 into bytecodealliance:main Oct 7, 2023
368 checks passed
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
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

4 participants