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

Wasm-opt fails on Rust 1.87.0+, LLVM 20+ #2601

Open
MolotovCherry opened this issue Mar 8, 2025 · 18 comments
Open

Wasm-opt fails on Rust 1.87.0+, LLVM 20+ #2601

MolotovCherry opened this issue Mar 8, 2025 · 18 comments
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc bug Something isn't working

Comments

@MolotovCherry
Copy link

MolotovCherry commented Mar 8, 2025

Describe the bug

An update to Rust with LLVM 20 requires new wasm features in the resulting binary. The new wasm features caused downstream breakage for a lot of people.

Affected: Rust 1.87.0+, LLVM 20+

Log file of failing build:
out.txt

Adding these flags to wasm-opt fixes the compilation
--enable-bulk-memory --enable-threads --enable-nontrapping-float-to-int

Note: I am unsure how exactly the compilation takes place. Perhaps who's at fault here is wasm-pack? In either case, this bridge plugin is still affected though

Please see
rust-lang/rust#137315
rust-lang/rust#137322
https://doc.rust-lang.org/nightly/rustc/platform-support/wasm32-unknown-unknown.html#enabled-webassembly-features

Steps to reproduce

flutter_rust_bridge_codegen build-web --release
flutter build web --wasm --release

Logs

n/a (log is in first post)

Expected behavior

Compiles properly

Generated binding code

n/a

OS

Wasm

Version of flutter_rust_bridge_codegen

2.8.0

Flutter info

[✓] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Version 10.0.26100.3323], locale en-US) [310ms]
    • Flutter version 3.29.0 on channel stable at C:\Users\cherry\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 35c388afb5 (4 weeks ago), 2025-02-10 12:48:41 -0800
    • Engine revision f73bfc4522
    • Dart version 3.7.0
    • DevTools version 2.42.2

Version of clang++

n/a

Additional context

No response

@MolotovCherry MolotovCherry added the bug Something isn't working label Mar 8, 2025
Copy link

welcome bot commented Mar 8, 2025

Hi! Thanks for opening your first issue here! 😄

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 8, 2025

Hmm I see. Btw where is --enable-threads from? From the links it seems they mention nontrapping-fptoint and bulk-memory EDIT: See it from logs

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 8, 2025

Btw, I wonder whether if we enable these flags, will the resulting binary require more modern browser etc?

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Mar 8, 2025
@MolotovCherry
Copy link
Author

MolotovCherry commented Mar 8, 2025

I have no idea. It was mentioned in the failed build as something that was needed, but as to why it wanted it, I don't know. I can say though that my project setup is vanilla (I just made it fresh from the tool, and nothing core was altered)

Edit: It's from the atomics

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 8, 2025

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 8, 2025

Hmm I cannot reproduce it:

cargo +nightly --version
cargo 1.87.0-nightly (2622e844b 2025-02-28)

flutter_rust_bridge_codegen build-web --release

Building package executable... 
Built flutter_rust_bridge:flutter_rust_bridge.
> which wasm-pack (pwd: null, env: null)
/Users/tom/.cargo/bin/wasm-pack
> cargo read-manifest (pwd: rust, env: null)
> wasm-pack build -t no-modules -d /Volumes/MyExternal/ExternalRefCode/flutter_rust_bridge/frb_example/flutter_via_create/web/pkg --no-typescript --out-name rust_lib_flutter_via_create rust -- -Z build-std=std,panic_abort (pwd: null, env: {RUSTUP_TOOLCHAIN: nightly, RUSTFLAGS: -C target-feature=+atomics,+bulk-memory,+mutable-globals, CARGO_TERM_COLOR: always})
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
 Downloading crates ...
  Downloaded compiler_builtins v0.1.151
   Compiling compiler_builtins v0.1.151
   Compiling core v0.0.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core)
   Compiling libc v0.2.170
   Compiling object v0.36.7
   Compiling std v0.0.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/std)
   Compiling proc-macro2 v1.0.70
   Compiling unicode-ident v1.0.12
   Compiling wasm-bindgen-shared v0.2.92
   Compiling log v0.4.20
   Compiling bumpalo v3.14.0
   Compiling version_check v0.9.4
   Compiling typenum v1.17.0
   Compiling once_cell v1.18.0
   Compiling futures-core v0.3.29
   Compiling wasm-bindgen v0.2.92
   Compiling generic-array v0.14.7
   Compiling autocfg v1.1.0
   Compiling slab v0.4.9
   Compiling futures-task v0.3.29
   Compiling futures-channel v0.3.29
   Compiling futures-util v0.3.29
   Compiling portable-atomic v1.11.0
   Compiling cfg-if v1.0.0
   Compiling anyhow v1.0.75
   Compiling build-target v0.4.0
   Compiling hex v0.4.3
   Compiling flutter_rust_bridge v2.8.0 (/Volumes/MyExternal/ExternalRefCode/flutter_rust_bridge/frb_rust)
   Compiling quote v1.0.33
   Compiling syn v2.0.39
   Compiling crypto-common v0.1.6
   Compiling block-buffer v0.10.4
   Compiling digest v0.10.7
   Compiling md-5 v0.10.6
   Compiling wasm-bindgen-backend v0.2.92
   Compiling futures-macro v0.3.29
   Compiling delegate-attr v0.3.0
   Compiling flutter_rust_bridge_macros v2.8.0 (/Volumes/MyExternal/ExternalRefCode/flutter_rust_bridge/frb_macros)
   Compiling wasm-bindgen-macro-support v0.2.92
   Compiling wasm-bindgen-macro v0.2.92
   Compiling rustc-std-workspace-core v1.99.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/rustc-std-workspace-core)
   Compiling alloc v0.0.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc)
   Compiling memchr v2.7.4
   Compiling adler2 v2.0.0
   Compiling rustc-demangle v0.1.24
   Compiling unwind v0.0.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/unwind)
   Compiling dlmalloc v0.2.7
   Compiling rustc-std-workspace-alloc v1.99.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/rustc-std-workspace-alloc)
   Compiling panic_unwind v0.0.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/panic_unwind)
   Compiling panic_abort v0.0.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/panic_abort)
   Compiling gimli v0.31.1
   Compiling miniz_oxide v0.8.3
   Compiling std_detect v0.1.5 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/stdarch/crates/std_detect)
   Compiling hashbrown v0.15.2
   Compiling addr2line v0.24.2
   Compiling proc_macro v0.0.0 (/Users/tom/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/proc_macro)
   Compiling pin-project-lite v0.2.13
   Compiling futures-sink v0.3.29
   Compiling futures-io v0.3.29
   Compiling memchr v2.6.4
   Compiling pin-utils v0.1.0
   Compiling lazy_static v1.4.0
   Compiling tokio v1.34.0
   Compiling bytemuck v1.14.0
   Compiling byteorder v1.5.0
   Compiling js-sys v0.3.69
   Compiling console_error_panic_hook v0.1.7
   Compiling futures-executor v0.3.29
   Compiling web-sys v0.3.66
   Compiling futures v0.3.29
   Compiling wasm-bindgen-futures v0.4.42
warning: unstable feature specified for `-Ctarget-feature`: `atomics`
  |
  = note: this feature is not stably supported; its behavior can change in the future

warning: `flutter_rust_bridge` (lib) generated 1 warning
   Compiling rust_lib_flutter_via_create v0.1.0 (/Volumes/MyExternal/ExternalRefCode/flutter_rust_bridge/frb_example/flutter_via_create/rust)
warning: `rust_lib_flutter_via_create` (lib) generated 1 warning (1 duplicate)
    Finished `release` profile [optimized] target(s) in 35.15s
[INFO]: ⬇️  Installing wasm-bindgen...
[INFO]: found wasm-opt at "/usr/local/bin/wasm-opt"
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
[INFO]: ✨   Done in 35.86s
[INFO]: 📦   Your wasm pkg is ready to publish at /Volumes/MyExternal/ExternalRefCode/flutter_rust_bridge/frb_example/flutter_via_create/web/pkg.

lutter build web --wasm --release

Downloading Web SDK...                                              5.7s

┌─ New feature ────────────────────────────────────────────────────────────────────────────┐
│   WebAssembly compilation is new. Understand the details before deploying to production. │
│   See https://flutter.dev/to/wasm for more information.                                  │
└──────────────────────────────────────────────────────────────────────────────────────────┘

Font asset "CupertinoIcons.ttf" was tree-shaken, reducing it from 257628 to 1472 bytes (99.4% reduction). Tree-shaking can be disabled
by providing the --no-tree-shake-icons flag when building your app.
Font asset "MaterialIcons-Regular.otf" was tree-shaken, reducing it from 1645184 to 7736 bytes (99.5% reduction). Tree-shaking can be
disabled by providing the --no-tree-shake-icons flag when building your app.
Compiling lib/main.dart for the Web...                             40.5s
✓ Built build/web

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 8, 2025

Btw, frb's build-web already support:

    --wasm-bindgen-args             Arguments passed to wasm-bindgen
    --wasm-pack-rustup-toolchain    Override RUSTUP_TOOLCHAIN environment variable when running wasm-pack
    --wasm-pack-rustflags           Override RUSTFLAGS environment variable when running wasm-pack

Does passing these help?

Especially, the default rustflags are:

RUSTFLAGS: -C target-feature=+atomics,+bulk-memory,+mutable-globals

So I wonder whether adding those to it will help

@MolotovCherry
Copy link
Author

MolotovCherry commented Mar 8, 2025

This actually didn't happen to me before either, but abruptly started.* I'm trying to see if I can pinpoint the cause

I know I did change the features to remove wasm-start, but I don't think that's the cause(nevertheless, here's the config)

flutter_rust_bridge = { version = "=2.8.0", features = [
    "anyhow",
    "dart-opaque",
    "log",
    "portable-atomic",
    "rust-async",
    "thread-pool",
    "user-utils",
] }

Edit for clarity: Abruptly maybe isn't the right word. Debug builds work fine for me; this only happened to me on release

I'm currently also running
wasm-pack v0.13.1
wasm-opt-cdcb9e877b68d02e / 117 (in .wasm-pack folder; updating to 122 didn't help)

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 8, 2025

I see. Feel free to try the flags I mentioned above and see whether it works

@MolotovCherry
Copy link
Author

MolotovCherry commented Mar 8, 2025

I see. Feel free to try the flags I mentioned above and see whether it works

May I ask, what is the full arg command you'd like me to try? So far I've tried every combination of flags and args I can think of, including downgrading the compiler to your version (prob downgrading it even further would "fix it"). I still can't pinpoint the cause/solution that doesn't involve adding those wasm-opt flags :/ (well, I suppose removing all the rustflags might "work", but I also tried that in the flag, perhaps I didn't get the syntax right)

(I wonder if it's an OS difference, since I'm on Windows)

(I've made sure wasm-pack, wasm-bindgen, etc all is up to date too)

@MolotovCherry
Copy link
Author

MolotovCherry commented Mar 8, 2025

I tried a new project like yours and it worked perfectly. I could not get it to fail no matter what I tried. At that point I realized it may be some config difference between new projects and my custom project structure.

So I made a new empty project with my same project structure, and it reliably triggered the first time. I uploaded the failing repo for you.

Though I do find it quite odd that one works but not the other.

This was made by first making a cargo workspace + empty crates + empty crates/wasm dir, then using integrate with the flags

❯ cat Cargo.toml
[workspace]
resolver = "3"
members = [
    "crates/wasm",
]

[profile.release]
lto = true
codegen-units = 1
strip = true

[profile.dev]
opt-level = 1

❯ mkdir crates
❯ cd crates
❯ mkdir wasm
❯ cd ..
❯ flutter create my_name
❯ cd my_name
❯ flutter_rust_bridge_codegen integrate --rust-crate-dir ../crates/wasm --rust-crate-name wasm --template app
❯ flutter_rust_bridge_codegen build-web -c ../crates/wasm -o ../../my_name/build/web --release

Hopefully this repo will be able to reproduce it for you
https://github.com/MolotovCherry/flutter_rust_bridge_2601

On a somewhat aside note, the below command crashes. The culprit that triggers it is using a hyphen in the project name (wasm-foo) with the flag --rust-crate-name

❯ flutter_rust_bridge_codegen integrate --rust-crate-dir ../crates/wasm --rust-crate-name wasm-foo --template app
# ... Snipped output
[2025-03-08T22:49:13.427Z WARN C:\Users\cherry\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\flutter_rust_bridge_codegen-2.8.0\src\library\commands\command_runner.rs:161] command="powershell" "-noprofile" "-command" "& \"flutter\" \"pub\" \"add\" \"wasm-foo\" \"--path=rust_builder\"" stdout= stderr=Not a valid package name: "null"

Usage: dart pub add [options] [<section>:]<package>[:descriptor]
       [<section>:]<package2>[:descriptor] ...]
-h, --help               Print this usage information.
    --[no-]offline       Use cached packages instead of accessing the network.
-n, --dry-run            Report what dependencies would change but don't change
                         any.
    --[no-]precompile    Build executables in immediate dependencies.
-C, --directory=<dir>    Run this in the directory <dir>.

Run "dart help" to see global options.
See https://dart.dev/tools/pub/cmd/pub-add for detailed documentation.
Failed to update packages.

Error: Command execution failed: Not a valid package name: "null"

Usage: dart pub add [options] [<section>:]<package>[:descriptor]
       [<section>:]<package2>[:descriptor] ...]
-h, --help               Print this usage information.
    --[no-]offline       Use cached packages instead of accessing the network.
-n, --dry-run            Report what dependencies would change but don't change
                         any.
    --[no-]precompile    Build executables in immediate dependencies.
-C, --directory=<dir>    Run this in the directory <dir>.

Run "dart help" to see global options.
See https://dart.dev/tools/pub/cmd/pub-add for detailed documentation.
Failed to update packages.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 8, 2025

May I ask, what is the full arg command you'd like me to try?

flutter_rust_bridge_codegen build-web --wasm-pack-rustflags blahblah

etc

i.e. try to add those required flags into the wasm-pack-rustflags or things like that

@MolotovCherry
Copy link
Author

MolotovCherry commented Mar 9, 2025

May I ask, what is the full arg command you'd like me to try?

flutter_rust_bridge_codegen build-web --wasm-pack-rustflags blahblah

etc

i.e. try to add those required flags into the wasm-pack-rustflags or things like that

It's telling me it doesn't recognize them (I tried various combinations, from a single flag, to many, commas, no commas, etc, all leading to it not working). Seems rustc doesn't like it

❯ flutter_rust_bridge_codegen build-web --release --wasm-pack-rustflags "--enable-bulk-memory --enable-threads --enable-nontrapping-float-to-int"
Error: `cargo metadata` exited with an error: error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `C:\Users\cherry\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\rustc.exe - --crate-name ___ --print=file-names --enable-bulk-memory --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit code: 1)
  --- stderr
  error: Unrecognized option: 'enable-bulk-memory'


Caused by: `cargo metadata` exited with an error: error: failed to run `rustc` to learn about target-specific information

Caused by:
  process didn't exit successfully: `C:\Users\cherry\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\rustc.exe - --crate-name ___ --print=file-names --enable-bulk-memory --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit code: 1)
  --- stderr
  error: Unrecognized option: 'enable-bulk-memory'

(I also tried --wasm-bindgen-args and --cargo-build-args much to the same effect)

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 9, 2025

Well, in #2601 (comment) I mentioned it is originally -C target-feature=+atomics,+bulk-memory,+mutable-globals. Maybe try to follow that format and see whether it works

@MolotovCherry
Copy link
Author

MolotovCherry commented Mar 9, 2025

Well, in #2601 (comment) I mentioned it is originally -C target-feature=+atomics,+bulk-memory,+mutable-globals. Maybe try to follow that format and see whether it works

Thanks for your suggestion. I get the same log output as usual with this command 😧 (the usual you need to --enable-threads etc)

flutter_rust_bridge_codegen build-web -c ../crates/my_crate -o ../../flutter/build/web --release --wasm-pack-rustflags "-C target-feature=+atomics,+bulk-memory,+mutable-globals"

(Thanks for taking the time to go through this by the way 😊 . I appreciate all the work you've put into this library)

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 9, 2025

You are welcome!

Well I mean tweaking the flags...

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 9, 2025

Adding these flags to wasm-opt fixes the compilation

Btw how do you achieve this, and does that already fix you scenario?

@MolotovCherry
Copy link
Author

MolotovCherry commented Mar 9, 2025

Adding these flags to wasm-opt fixes the compilation

Btw how do you achieve this, and does that already fix you scenario?

I achieved it by copying it to my repo, disabling and manually calling it in my command runner (just). It does fix it, but it's not a very good solution (then again, there may not be any real better way to solve it, short of disabling the wasm features through the compiler flags. I suppose restructuring the project would fix it too, though I much rather like my current structure)

[package.metadata.wasm-pack.profile.release]
wasm-opt = false

Edit: Removing my release profile in the workspace fixes it, specifically strip = true. What a strange bug

[profile.release]
lto = true
codegen-units = 1
strip = true

Since that seems to be the issue, maybe it's a rust bug, I'll report it to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting Waiting for responses, PR, further discussions, upstream release, etc bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants