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

Compile to WASM/WASI? #4

Open
turbolent opened this issue Feb 2, 2023 · 10 comments
Open

Compile to WASM/WASI? #4

turbolent opened this issue Feb 2, 2023 · 10 comments

Comments

@turbolent
Copy link

A couple years ago @binji modified clang/LLVM and was able to compile it to WASM/WASI: https://github.com/binji/wasm-clang. However, it is now multiple years old and the changes weren't upstreamed.

Maybe I'm mistaken, but could the llvmbox effort help with getting clang compiled to WASM/WASI?

@binji
Copy link

binji commented Feb 6, 2023

I don't know much about llvmbox, but maybe it would help? I ended up making things a bit harder for myself in general, since I ended up writing my own WASI Filesystem layer that would work well on the web. It's likely that there are better ways to do that now (and probably even then).

The biggest hack is the same as always with the web platform -- handling blocking calls, and dealing with threading. I think if you're focused on WASI then this maybe isn't much of an issue, and you could get pretty far with a simple LLVM cross-compile using the WASI SDK?

@turbolent
Copy link
Author

Thank you for the details @binji!

@rsms
Copy link
Owner

rsms commented Mar 1, 2023

Hi! I'd love to add both plain WASM and WASI support. Simply haven't gotten around to it.

Contributions and collaboration welcome :-)

@turbolent
Copy link
Author

@binji I've started with making changes in wasi-sdk itself (the hope is to port the patch here at some point). Looking at the changes to llvm at the time (https://github.com/llvm/llvm-project/compare/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9..5dc09c94393510bc8d042a9f07382b53e845c0f2), I noticed only support for signals is disabled, but mutex operations weren't changed. How did this build, given there's no support for mutex operations in wasi-sdk? Did you have stubs for the mutex operations?

@binji
Copy link

binji commented Mar 7, 2023

@turbolent yeah, some folks were asking me about this on the Wasm discord too. I forgot that I ended up modifying wasi-sdk and llvm to get this to compile. See https://github.com/binji/wasi-sdk-bin and my notes from the time here: https://gist.github.com/binji/b7541f9740c21d7c6dac95cbc9ea6fca

My apologies about this, I really should have left the project in a better state at the time!

@turbolent
Copy link
Author

@binji No worries at all! This is great info, thank you

@rsms
Copy link
Owner

rsms commented Mar 12, 2023

I've added WASI as a target (wasm32-wasi) in Compis, which is based on llvmbox (but bundles all tools into one executable, like zig.) Here's a simple example: https://github.com/rsms/compis/tree/main/examples/hello-wasi

I realize you're discussing compiling llvmbox itself for WASI, but maybe with some more work on Compis (ie adding libc++) it can be used to compile LLVM for WASI.

@turbolent
Copy link
Author

turbolent commented Mar 12, 2023

@rsms Awesome work on Compis, it's looking great! Love seeing linear types being more widely adopted (shameless plug, I'm working on https://github.com/onflow/cadence, which also has linear types; maybe some day we can replace Go and rewrite Cadence in Compis, it looks like a great fit, haha).

I'm making good progress on re-applying @binji's changes on top of LLVM 15. For now I'm using wasi-sdk to build LLVM, which already builds LLVM targeting just WebAssembly like llvmbox, as its build system is a bit simpler (single stage LLVM build, just a single Makefile). Unlike llvmbox, I think it doesn't build a static / single-binary LLVM/Clang, but I'm confident the LLVM changes will be useful for llvmbox too.

I'll try to push the changes up soon. The approach I'm taking for now is to not have a modified wasi-sdk/libc/libc++, and using what is shipped in wasi-sdk as-is. That means there is no C++ mutex/future/etc., so much more most be changed in LLVM.
With the release of https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-20%2Bthreads and future development of bringing threads to WASI, I hope that those threading related changes to LLVM will only be temporary and eventually wasi-sdk will ship with a C++ stdlib that has mutex/future/etc.

@rsms
Copy link
Owner

rsms commented Mar 12, 2023

@turbolent: I'm working on github.com/onflow/cadence, which also has linear types

Very nice! I see we share many ideas about syntax and semantics :–)

@turbolent: For now I'm using wasi-sdk to build LLVM, which already builds LLVM targeting just WebAssembly like llvmbox, as its build system is a bit simpler

Nice. Simpler is almost always better. The downside is your Linux builds will be specific to your distro, but that's of course fine unless you intend to distribute the binaries widely.

@turbolent
Copy link
Author

I finally managed to compile LLVM 15 to WebAssembly/WASI and opened a draft PR in the WASI SDK project: WebAssembly/wasi-sdk#304

I think it should be possible to integrate the changes to LLVM into llvmbox.

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

No branches or pull requests

3 participants