-
Notifications
You must be signed in to change notification settings - Fork 213
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
expose math symbols on wasm32-unknown-unknown #248
Conversation
latest commit adds more math functions. Only four are missing from the list in rustwasm/team#84 (comment), but people are already working on them. |
Once those four are in we'll also have all the functions required to put math support in core so we should think about the possible ways to do that. (a) One way is to merge src/libstd/{f32,f64}.rs into src/libcore/num/{f32,f64}.rs (e.g. we move the current f32.sin implementation into core) and add all math symbols to compiler-bulitins. In that case the src/math.rs file in this repo should be available on all targets, and not just on wasm. (b) The other way is to source import libm into core and implement the math methods by directly calling the libm functions, instead of using llvm intrinsics (e.g. The downside to (a) is that downstream users could run into linker errors ("undefined reference") if we forget to include some math symbol. (a) may also cause linker errors ("duplicate symbol") when linking statically, which is how the thumb targets do linking, if the user also links to e.g. glibc's libm. The advantages to (b) are (i) llvm would be able to inline code (*) and remove unreachable branches in some cases producing smaller binaries, and (ii) it would be possible to turn the math methods into const functions in the future (we can't mark functions that call llvm intrinsics as const fn -- unless we use compiler magic, maybe). (*) it might be possible to achieve inlining using approach (a) if we get link-time / cross-language LTO working. I think doing either (a) or (b) would have the effect that all users would use our libm implementation of math functions (instead of e.g. glibc's libm implementation) and would close the possibility of providing your own implementation of math functions -- unless we do (a) and make the symbols weak. So I would vote for approach (b) but we should implement it right after the next beta cutoff to maximize testing time. |
Hm I agree that the first strategy probably isn't workable, even for normal Linux distributions the linker command would look like:
If Note that this strategy I think is workable if we compile each function in For strategy (b) I'm a little hesitant to do that as well. I agree that it should solve the symbol visibility issue, but it has the downside of forcing users to use our own implementation which may not be as fast as the native implementations. Additionally I'm not sure if we want to commit long-term to making these Now all of this is somewhat moot if it's just for wasm specifically which has no other competing library here (other than shelling out to |
My main motivation for this is making (pure Rust) math support available to targets that don't have std support, like the In the case of the In conclusion, I think that right now it may be best to provide only these math symbols for wasm and see how the use of the |
Sounds reasonable to me! I think it'd also be fine to include this on |
All the required functions by wasm are in now.
I think that might cause linker errors if the program is already being statically linked to libm but I can't check that right now / this week so I would hold off from doing that in this PR. |
Sounds reasonable to me! I think the wasm CI is still failing though? |
https://travis-ci.org/rust-lang-nursery/compiler-builtins/jobs/405440231 wasm build is now fixed |
Make sure they respect the `mangled-names` feature as well as have the `"C"` ABI.
I pushed up another commit to use Thanks so much for pushing on this @japaric, this is some amazing work! |
This commit updates the compiler-builtins submodule, primarily pulling in rust-lang/compiler-builtins#248 which should resolve a number of undefined symbols on the wasm target by default (instead of leaving them in the final binary).
@japaric I would like to use this for my own custom target, too. Any thoughts on best approach? Right now I am using edit: Reached out to you on Discord, too. |
@parasyte You mean you are compiling If you are writing |
r? @alexcrichton
I think this should do the trick. I wasn't sure how to write a test that checks that the symbols make it to a wasm file -- it seems that the wasm linker is lenient and doesn't throw errors when undefined symbols make it to the final artifact.