Skip to content

Commit

Permalink
feat(wasm-optimizer): build WASMs with -Ctarget-cpu=mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on committed Sep 23, 2024
1 parent 5536e04 commit 29681bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
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")
}
}

0 comments on commit 29681bf

Please sign in to comment.