pepyakin / binaryen-rs

Binaryen bindings for Rust.
Apache License 2.0
74 stars 16 forks source link

Are there still bindings somewhere to the IR construction #65

Open bcpeinhardt opened 1 year ago

bcpeinhardt commented 1 year ago

Hey there! I want to experiment with extending the gleam language to support compiling to webassembly. The compiler is written in rust, so I was hoping to find rust bindings to the IR construction part of binaryen. Any advice on an approach do explore would be appreciated :)

coffee-cup commented 1 year ago

I am interested in this as well. I am currently building a small language that compiles directly to webassembly. At the moment I am just generating WAT code and using wasmtime to compile into wasm and run. I've just discovered binaryen and would love to take advantage of the optimizations. Translating to binaryen IR instead of WAT would simplify things quite a bit.

pepyakin commented 1 year ago

This project actually contained bindings for building IR. IIRC, it was not trivial to map to safe Rust and there were no interest in it so that's why it had to go.

I would not be opposed to merging a PR that provides a good mapping for it.

samestep commented 8 months ago

@pepyakin I'm interested in reviving this. Do you happen to remember any more details about why it was not trivial to map to safe Rust?

pepyakin commented 8 months ago

It was years and years ago so I don't remember exactly.

If I were to guess, then I would assume that it's nothing out of ordinary when it comes dealing with C++ code. E.g. some method has some precondition and if it doesn't hold then the binaryen either aborts (as in abort) or does something memory unsafe. To prevent those situations from happening you would need to do those checks in the bindings code, maybe track some additional state.

I would expect from the bindings to provide a Rust-idiomatic interface. That means, that as long as the user deals with the safe Rust it cannot trigger memory unsafety. It also means the code should panic and not abort directly.

UPD: completely coincedentally I bumped on this discussion, which may indicate kinds of problems I encountered.

clacladev commented 1 month ago

I see that v0.6 is the last version which was wrapping the C api of Binaryen. But it's 6 years old. So a bit old to use.

As far as you know, did anybody continue to maintain that? Or do you know of anybody working on creating a Rust wrapper for Binaryen?

clacladev commented 1 month ago

@pepyakin I am trying to create a crate with the binaryen rust bindings. Here a wip https://github.com/clacladev/binaryen-rust

The problem as soon as I try to use the binded types, as when uncommenting the test in https://github.com/clacladev/binaryen-rust/blob/dev/src/lib.rs it crashes with:

Compiling binaryen-rust v0.1.0 (/Users/claudio/Dev/webby/binaryen-rust)
error: linking with `cc` failed: exit status: 1
  |
  = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET -u XROS_DEPLOYMENT_TARGET LC_ALL="C" PATH="/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/claudio/.wasmtime/bin:/Users/claudio/.wasmer/bin:/Users/claudio/.bun/bin:/Users/claudio/.rbenv/shims:/Users/claudio/.nvm/versions/node/v20.16.0/bin:/Users/claudio/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/MacGPG2/bin:/Users/claudio/.wasmtime/bin:/Users/claudio/.wasmer/bin:/Users/claudio/.bun/bin:/Users/claudio/.rbenv/shims:/Users/claudio/.nvm/versions/node/v20.16.0/bin:/Users/claudio/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/claudio/Library/pnpm:/Users/claudio/.miniconda3/bin:/Users/claudio/.miniconda3/condabin:/Users/claudio/.starkli/bin:/Users/claudio/.cargo/bin:/Users/claudio/.foundry/bin:/Users/claudio/Library/Android/sdk/emulator:/Users/claudio/Library/Android/sdk/platform-tools:/Users/claudio/.local/bin:/Users/claudio/.foundry/bin:/Users/claudio/Library/Android/sdk/emulator:/Users/claudio/Library/Android/sdk/platform-tools:/Users/claudio/.local/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-arch" "arm64" "/var/folders/k3/2cm7mqwd3nqffkdmf0_36vdh0000gn/T/rustcKMN7Fl/symbols.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.189pok23frc9ep7qqv8r35d8k.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.1fpjbs9x6py06b78pelseik5e.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.1mk3px3y3vrq0hjnjxe6dl04c.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.1pe7waywdnz3aeaf45jqw6sj3.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.3xg6k53az00rd4ywft37o6lce.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.4m6uszeshiemas8fwtfphdqhk.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5248wdh08bf14edqzkafei1cn.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5pteqznd4cw9d1y0myi0dwwhf.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5xoq1v7ptdeacva5lo21rwpm9.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.6djx01iq8a43638bch0jcyijd.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.6jcbski7u9toefgf42c85msbo.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.6wlt211ei5jp3u9e53iv49axf.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.761get07q680x978f1f9sm4qg.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.8rd5mmdnag21rwiwii2vc7e51.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.8v5gokn527pynlc2cdb1ybkzz.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.anjzalk10yfkbb11qsqf2jo6b.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.b3el27uaovlhyyig0ff63bmcs.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.bk2e9gpsh8xwkw8z12b0ll19o.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.cbcn0w4i6shqf5foslxg638ii.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.cfccsu9jjdosinb08qi47qeyd.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.d8cwjb41z73a3wai2oqonzl4j.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.dy194cy38y65ac5sd8qg8jhfp.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.e37wenafym10yvbllvtiu8x25.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.f32n0019ti2eeccb9bt4kupte.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5b6w8ognp6zf2r66ubipa0uv5.rcgu.o" "-L" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps" "-L" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libtest-edf45beacd3678bf.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgetopts-71c4eb6d673d56ea.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunicode_width-e28435f78d418cf7.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_std-f3e6f7c04a993e06.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd-2cd4f83c0684cf3b.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libpanic_unwind-ae5c5b32e17a4253.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libobject-0a06236df3debd3d.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libmemchr-eb79452a16ef20ff.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libaddr2line-e5400cafb1ff9160.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgimli-cd7b8be743512bf0.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_demangle-13002c379b6a4f94.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd_detect-66b97aed86b38916.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libhashbrown-e526da30a7a44502.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_alloc-8a46d2981213a851.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libminiz_oxide-2f857721294fc67d.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libadler-7bb2d22a2a2c450a.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunwind-96650a40faca9ad5.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcfg_if-10cbd942774a0e4f.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liblibc-542fd8c92681fb67.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liballoc-5487ab1633e6869d.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_core-7be3f18be52763ef.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcore-e0586f91b4987ecb.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcompiler_builtins-3f821062253d71e0.rlib" "-lSystem" "-lc" "-lm" "-L" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "-o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: Undefined symbols for architecture arm64:
            "_BinaryenAddFunction", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenAddInt32", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenBinary", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenLocalGet", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModuleCreate", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModuleDispose", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModulePrint", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModulePrintStackIR", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenTypeCreate", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenTypeInt32", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: could not compile `binaryen-rust` (lib test) due to 1 previous error

I am not expert in this FFI Rust stuff. Can you help me on this?