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

feat(wasm-optimizer): build WASMs with -Ctarget-cpu=mvp #4247

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/wasm-optimizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gear-wasm-instrument.workspace = true
pwasm-utils = { workspace = true, features = ["sign_ext"] }
wasm-opt = { workspace = true, optional = true }
regex.workspace = true
rustc_version.workspace = true
log.workspace = true
wasmparser.workspace = true
which.workspace = true
Expand Down
28 changes: 26 additions & 2 deletions utils/wasm-optimizer/src/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,34 @@ pub struct CargoCommand {
toolchain: Toolchain,
check_recommended_toolchain: bool,
force_recommended_toolchain: bool,
force_wasm_mvp: bool,
}

impl CargoCommand {
/// Initialize new cargo command.
pub fn new() -> CargoCommand {
let toolchain = Toolchain::try_from_rustup().expect("Failed to get toolchain from rustup");
let rustc_version = rustc_version::version().expect("Failed to get rustc version");
let force_wasm_mvp = toolchain != Toolchain::recommended_nightly()
&& rustc_version.major == 1
&& rustc_version.minor >= 82;

CargoCommand {
path: "rustup".to_string(),
manifest_path: "Cargo.toml".into(),
profile: "dev".to_string(),
rustc_flags: vec!["-C", "link-arg=--import-memory", "-C", "linker-plugin-lto"],
rustc_flags: if force_wasm_mvp {
// -C linker-plugin-lto causes conflict: https://github.com/rust-lang/rust/issues/130604
vec!["-C", "link-arg=--import-memory"]
} else {
vec!["-C", "link-arg=--import-memory", "-C", "linker-plugin-lto"]
},
target_dir: "target".into(),
features: vec![],
toolchain: Toolchain::try_from_rustup().expect("Failed to get toolchain from rustup"),
toolchain,
check_recommended_toolchain: false,
force_recommended_toolchain: false,
force_wasm_mvp,
}
}
}
Expand Down Expand Up @@ -117,6 +130,10 @@ impl CargoCommand {
.arg("--profile")
.arg(&self.profile);

if self.force_wasm_mvp {
cargo.arg("-Zbuild-std=core,alloc,panic_abort");
}

if !self.features.is_empty() {
cargo.arg("--features");
cargo.arg(self.features.join(","));
Expand All @@ -130,6 +147,13 @@ impl CargoCommand {

self.remove_cargo_encoded_rustflags(&mut cargo);

if self.force_wasm_mvp {
cargo.env("CARGO_ENCODED_RUSTFLAGS", "-Ctarget-cpu=mvp");
if !self.toolchain.is_nightly() {
cargo.env("RUSTC_BOOTSTRAP", "1");
}
}

let status = cargo.status().context("unable to execute cargo command")?;
ensure!(
status.success(),
Expand Down
5 changes: 5 additions & 0 deletions utils/wasm-optimizer/src/cargo_toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ impl Toolchain {
);
Ok(())
}

/// Returns bool representing nightly toolchain.
pub fn is_nightly(&self) -> bool {
self.0.starts_with("nightly")
}
}
Loading