Skip to content

Commit

Permalink
feat(wasm-optimizer): extract wasm optimizer from wasm builder (#4127)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored Aug 15, 2024
1 parent 5b6bb0d commit 59f8dc2
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
- name: "Test: Wasm-builder recommended toolchain matches rust-toolchain.toml"
run: |
TOOLCHAIN=$(grep 'channel' rust-toolchain.toml | cut -d '"' -f 2)
CARGO_TOOLCHAIN="utils/wasm-builder/src/cargo_toolchain.rs"
CARGO_TOOLCHAIN="utils/wasm-optimizer/src/cargo_toolchain.rs"
if ! grep -q "$TOOLCHAIN" $CARGO_TOOLCHAIN; then
echo "Please update PINNED_NIGHTLY_TOOLCHAIN constant in $CARGO_TOOLCHAIN to match rust-toolchain.toml."
exit 1
Expand Down
24 changes: 19 additions & 5 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ gear-stack-buffer = { path = "stack-buffer" }
gear-ss58 = { path = "utils/ss58", default-features = false }
gear-utils = { path = "utils/utils" }
gear-wasm-builder = { path = "utils/wasm-builder", default-features = false }
gear-wasm-optimizer = { path = "utils/wasm-optimizer", default-features = false }
gear-wasm-gen = { path = "utils/wasm-gen" }
gear-wasm-instrument = { path = "utils/wasm-instrument", default-features = false }
junit-common = { path = "utils/junit-common" }
Expand Down
2 changes: 1 addition & 1 deletion utils/cargo-gbuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde_json.workspace = true
toml.workspace = true
tracing.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter"] }
gear-wasm-builder.workspace = true
gear-wasm-optimizer.workspace = true
glob.workspace = true

[dev-dependencies]
Expand Down
7 changes: 2 additions & 5 deletions utils/cargo-gbuild/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ use crate::metadata::Metadata;
use anyhow::{anyhow, Result};
use cargo_toml::Manifest;
use colored::Colorize;
use gear_wasm_builder::{
optimize::{self, OptType, Optimizer},
CargoCommand,
};
use gear_wasm_optimizer::{self as optimize, CargoCommand, OptType, Optimizer};
use std::{
env, fs,
path::{Path, PathBuf},
Expand Down Expand Up @@ -159,7 +156,7 @@ impl Artifact {
if !self.opt.is_meta() {
optimizer
.insert_stack_end_export()
.map_err(|e| anyhow!(e))?;
.map_err(|e| anyhow!("{e}"));
optimizer.strip_custom_sections();
}

Expand Down
2 changes: 1 addition & 1 deletion utils/cargo-gbuild/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use anyhow::{anyhow, Result};
use cargo_toml::Manifest;
use clap::Parser;
use colored::Colorize;
use gear_wasm_builder::CargoCommand;
use gear_wasm_optimizer::CargoCommand;
use std::{
env, fs,
path::{Path, PathBuf},
Expand Down
2 changes: 1 addition & 1 deletion utils/cargo-gbuild/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::Artifact;
use anyhow::{anyhow, Result};
use cargo_metadata::{CargoOpt, Message, MetadataCommand};
use cargo_toml::Manifest;
use gear_wasm_builder::optimize::OptType;
use gear_wasm_optimizer::OptType;
use serde::{Deserialize, Serialize};
use std::{
io::BufReader,
Expand Down
3 changes: 2 additions & 1 deletion utils/crates-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ pub const STACKED_DEPENDENCIES: [&str; 15] = [
/// NOTE: Each package in this array could possibly depend
/// on the previous one, please be cautious about changing
/// the order.
pub const PACKAGES: [&str; 10] = [
pub const PACKAGES: [&str; 11] = [
"gring",
"gear-wasm-optimizer",
"gear-wasm-builder",
"gear-node-wrapper",
"gtest",
Expand Down
10 changes: 4 additions & 6 deletions utils/wasm-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ readme = "README.md"
anyhow.workspace = true
cargo_metadata.workspace = true
chrono = "0.4"
thiserror.workspace = true
regex.workspace = true
pwasm-utils = { workspace = true, features = ["sign_ext"] }
toml.workspace = true
thiserror.workspace = true
log.workspace = true
pathdiff.workspace = true
which.workspace = true
colored.workspace = true
dirs.workspace = true
gmeta.workspace = true
gear-core.workspace = true
gear-wasm-instrument.workspace = true
wasm-opt = { workspace = true, optional = true }
wasmparser.workspace = true
regex.workspace = true
gear-wasm-optimizer.workspace = true
rustc_version.workspace = true

[dev-dependencies]
Expand All @@ -37,3 +34,4 @@ parity-wasm.workspace = true

[features]
metawasm = ["gmeta/codegen"]
wasm-opt = ["gear-wasm-optimizer/wasm-opt"]
13 changes: 0 additions & 13 deletions utils/wasm-builder/src/builder_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ pub enum BuilderError {
#[error("please add \"rlib\" to [lib.crate-type]")]
CrateTypeInvalid,

#[error("cargo command run failed: {0}")]
CargoRunFailed(String),

#[error("unable to find the root package in cargo metadata")]
RootPackageNotFound,

#[error("cargo toolchain is invalid `{0}`")]
CargoToolchainInvalid(String),

#[error(
"recommended toolchain `{0}` not found, install it using the command:\n\
rustup toolchain install {0} --component llvm-tools --target wasm32-unknown-unknown\n\n\
after installation, do not forget to set `channel = \"{0}\"` in `rust-toolchain.toml` file"
)]
RecommendedToolchainNotFound(String),
}
6 changes: 1 addition & 5 deletions utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#![doc(html_logo_url = "https://docs.gear.rs/logo.svg")]
#![doc(html_favicon_url = "https://gear-tech.io/favicons/favicon.ico")]

pub use cargo_command::CargoCommand;
pub use gear_wasm_optimizer::{self as optimize, CargoCommand};
pub use wasm_project::{PreProcessor, PreProcessorResult, PreProcessorTarget};

use crate::wasm_project::WasmProject;
Expand All @@ -30,13 +30,9 @@ use std::{env, path::PathBuf, process};
use wasm_project::ProjectType;

mod builder_error;
mod cargo_command;
mod cargo_toolchain;
pub mod code_validator;
mod crate_info;
pub mod optimize;
mod smart_fs;
mod stack_end;
mod wasm_project;

pub const TARGET: &str = env!("TARGET");
Expand Down
8 changes: 2 additions & 6 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
code_validator::CodeValidator,
crate_info::CrateInfo,
optimize::{self, OptType, Optimizer},
smart_fs,
};
use crate::{code_validator::CodeValidator, crate_info::CrateInfo, smart_fs};
use anyhow::{Context, Result};
use chrono::offset::Local as ChronoLocal;
use gear_wasm_optimizer::{self as optimize, OptType, Optimizer};
use gmeta::MetadataRepr;
use pwasm_utils::parity_wasm::{self, elements::Internal};
use std::{
Expand Down
26 changes: 26 additions & 0 deletions utils/wasm-optimizer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "gear-wasm-optimizer"
description = "Utility for optimizing Gear programs"
documentation = "https://docs.rs/gear-wasm-optimizer"
keywords = ["gear", "wasm"]
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
anyhow.workspace = true
gear-wasm-instrument.workspace = true
pwasm-utils = { workspace = true, features = ["sign_ext"] }
wasm-opt = { workspace = true, optional = true }
regex.workspace = true
log.workspace = true
wasmparser.workspace = true
which.workspace = true
colored.workspace = true

[dev-dependencies]
wasmi = { workspace = true, features = ["std"] }
wabt.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::cargo_toolchain::Toolchain;
use anyhow::{ensure, Context, Result};
use anyhow::{anyhow, ensure, Context, Result};
use std::{env, path::PathBuf, process::Command};

use crate::builder_error::BuilderError;

/// Helper to deal with the `cargo` command.
#[derive(Clone)]
pub struct CargoCommand {
Expand Down Expand Up @@ -85,12 +83,12 @@ impl CargoCommand {
}

/// Sets whether to check the version of the recommended toolchain.
pub(crate) fn set_check_recommended_toolchain(&mut self, check_recommended_toolchain: bool) {
pub fn set_check_recommended_toolchain(&mut self, check_recommended_toolchain: bool) {
self.check_recommended_toolchain = check_recommended_toolchain;
}

/// Sets whether to force the version of the recommended toolchain.
pub(crate) fn set_force_recommended_toolchain(&mut self, force_recommended_toolchain: bool) {
pub fn set_force_recommended_toolchain(&mut self, force_recommended_toolchain: bool) {
self.force_recommended_toolchain = force_recommended_toolchain;
}

Expand Down Expand Up @@ -160,7 +158,7 @@ impl CargoCommand {
let status = cargo.status().context("unable to execute cargo command")?;
ensure!(
status.success(),
BuilderError::CargoRunFailed(status.to_string())
anyhow!("cargo command run failed: {status}")
);

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::builder_error::BuilderError;
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use regex::Regex;
use std::{borrow::Cow, process::Command, sync::LazyLock};

Expand Down Expand Up @@ -67,7 +66,7 @@ impl Toolchain {

let toolchain = TOOLCHAIN_CHANNEL_RE
.captures(toolchain_desc)
.ok_or_else(|| BuilderError::CargoToolchainInvalid(toolchain_desc.into()))?
.ok_or_else(|| anyhow!("cargo toolchain is invalid {toolchain_desc}"))?
.get(0)
.unwrap() // It is safe to use unwrap here because we know the regex matches
.as_str()
Expand All @@ -93,7 +92,12 @@ impl Toolchain {
let toolchain = Self::PINNED_NIGHTLY_TOOLCHAIN;
anyhow::ensure!(
self.raw_toolchain_str() == toolchain,
BuilderError::RecommendedToolchainNotFound(toolchain.into()),
anyhow!(
"recommended toolchain `{x}` not found, install it using the command:\n\
rustup toolchain install {x} --component llvm-tools --target wasm32-unknown-unknown\n\n\
after installation, do not forget to set `channel = \"{x}\"` in `rust-toolchain.toml` file",
x = toolchain
)
);
Ok(())
}
Expand Down
25 changes: 25 additions & 0 deletions utils/wasm-optimizer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This file is part of Gear.

// Copyright (C) 2022-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

mod cargo_command;
mod cargo_toolchain;
mod optimize;
mod stack_end;

pub use cargo_command::CargoCommand;
pub use optimize::*;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::stack_end;
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
#[cfg(not(feature = "wasm-opt"))]
use colored::Colorize;
use gear_wasm_instrument::STACK_END_EXPORT_NAME;
Expand Down Expand Up @@ -62,10 +62,6 @@ impl OptType {
}
}

#[derive(Debug, thiserror::Error)]
#[error("Optimizer failed: {0:?}")]
pub struct OptimizerError(pwasm_utils::OptimizerError);

pub struct Optimizer {
module: Module,
file: PathBuf,
Expand Down Expand Up @@ -129,7 +125,7 @@ impl Optimizer {
};

pwasm_utils::optimize(&mut module, exports)
.map_err(OptimizerError)
.map_err(|e| anyhow!("{e:?}"))
.with_context(|| {
format!(
"unable to optimize the WASM file `{0}`",
Expand Down
File renamed without changes.

0 comments on commit 59f8dc2

Please sign in to comment.