-
Notifications
You must be signed in to change notification settings - Fork 576
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
Enable AOT usage on M1 mac #2618
Conversation
There was a problem hiding this 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
it seems inefficient to call pthread_jit_write_protect_np before every calls. also, the doc you mentioned seems suggesting to invalidate icache. isn't it necessary? |
72ccf91
to
98ad518
Compare
Thanks for starting the investigation, you input was was very useful!
Moved that to call it only before and after module loading
Yes, I put it before and after
Added that now |
core/iwasm/aot/aot_loader.c
Outdated
#if defined(__APPLE__) && defined(__arm64__) | ||
pthread_jit_write_protect_np(true); // Make memory executable | ||
sys_icache_invalidate(module->code, module->code_size); | ||
#endif |
There was a problem hiding this comment.
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:
and call it here.
and how about wrappring sys_icache_invalidate to os_icache_flush? like os_dcache_flush:
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
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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.
98ad518
to
4abe771
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
After reading the docs, I managed to use AOT on my M1 mac.
Issue: #1365