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

Switch to wasm-opt-rs crate #604

Merged
merged 1 commit into from
Feb 5, 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
157 changes: 133 additions & 24 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ experimental_event_loop = []
wizer = { workspace = true }
structopt = "0.3"
anyhow = { workspace = true }
binaryen = { git = "https://github.com/pepyakin/binaryen-rs", rev = "00c98174843f957681ba0bc5cdcc9d15f5d0cb23" }
brotli = "3.4.0"
wasmprinter = { version = "0.2.78", optional = true }
wasmtime = { workspace = true }
Expand All @@ -28,6 +27,8 @@ walrus = "0.20.3"
swc_core = { version = "0.89.7", features = ["common_sourcemap", "ecma_ast", "ecma_parser"] }
wit-parser = "0.13.1"
convert_case = "0.6.0"
wasm-opt = "0.116.0"
tempfile = "3.9.0"

[dev-dependencies]
serde_json = "1.0"
Expand All @@ -36,7 +37,6 @@ lazy_static = "1.4"
serde = { version = "1.0", default-features = false, features = ["derive"] }
criterion = "0.5"
num-format = "0.4.4"
tempfile = "3.9.0"
wasmparser = "0.121.0"

[build-dependencies]
Expand Down
32 changes: 17 additions & 15 deletions crates/cli/src/wasm_generator/static.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{collections::HashMap, rc::Rc, sync::OnceLock};
use std::{collections::HashMap, fs, rc::Rc, sync::OnceLock};

use anyhow::{anyhow, Result};
use binaryen::{CodegenConfig, Module};
use walrus::{DataKind, ExportItem, FunctionBuilder, FunctionId, MemoryId, ValType};
use wasi_common::{pipe::ReadPipe, WasiCtx};
use wasm_opt::{OptimizationOptions, ShrinkLevel};
use wasmtime::Linker;
use wasmtime_wasi::WasiCtxBuilder;
use wizer::Wizer;
Expand Down Expand Up @@ -82,19 +82,7 @@ pub fn generate(js: &JS, exports: Vec<Export>, no_source_compression: bool) -> R

let wasm = module.emit_wasm();

let codegen_cfg = CodegenConfig {
optimization_level: 3, // Aggressively optimize for speed.
shrink_level: 0, // Don't optimize for size at the expense of performance.
debug_info: false,
};

let mut module = Module::read(&wasm)
.map_err(|_| anyhow!("Unable to read wasm binary for wasm-opt optimizations"))?;
module.optimize(&codegen_cfg);
module
.run_optimization_passes(vec!["strip"], &codegen_cfg)
.map_err(|_| anyhow!("Running wasm-opt optimization passes failed"))?;
let wasm = module.write();
let wasm = optimize_wasm(&wasm)?;

let mut module = transform::module_config().parse(&wasm)?;
if no_source_compression {
Expand Down Expand Up @@ -140,3 +128,17 @@ fn export_exported_js_functions(
module.exports.add(&export.wit, export_fn);
}
}

fn optimize_wasm(wasm: &[u8]) -> Result<Vec<u8>> {
let tempdir = tempfile::tempdir()?;
let tempfile_path = tempdir.path().join("temp.wasm");

fs::write(&tempfile_path, wasm)?;

OptimizationOptions::new_opt_level_3() // Aggressively optimize for speed.
.shrink_level(ShrinkLevel::Level0) // Don't optimize for size at the expense of performance.
.debug_info(false)
.run(&tempfile_path, &tempfile_path)?;

Ok(fs::read(&tempfile_path)?)
}
30 changes: 30 additions & 0 deletions supply-chain/audits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ user-id = 189 # Andrew Gallant (BurntSushi)
start = "2019-06-26"
end = "2024-10-03"

[[trusted.cxx]]
criteria = "safe-to-deploy"
user-id = 3618 # David Tolnay (dtolnay)
start = "2019-12-28"
end = "2025-02-05"

[[trusted.cxx-build]]
criteria = "safe-to-deploy"
user-id = 3618 # David Tolnay (dtolnay)
start = "2020-04-30"
end = "2025-02-05"

[[trusted.cxxbridge-flags]]
criteria = "safe-to-deploy"
user-id = 3618 # David Tolnay (dtolnay)
start = "2020-08-30"
end = "2025-02-05"

[[trusted.cxxbridge-macro]]
criteria = "safe-to-deploy"
user-id = 3618 # David Tolnay (dtolnay)
start = "2020-01-08"
end = "2025-02-05"

[[trusted.env_logger]]
criteria = "safe-to-deploy"
user-id = 6743 # Ed Page (epage)
Expand Down Expand Up @@ -351,6 +375,12 @@ user-id = 2915 # Amanieu d'Antras (Amanieu)
start = "2020-02-16"
end = "2024-07-12"

[[trusted.scratch]]
criteria = "safe-to-deploy"
user-id = 3618 # David Tolnay (dtolnay)
start = "2020-09-17"
end = "2025-02-05"

[[trusted.serde]]
criteria = "safe-to-deploy"
user-id = 3618 # David Tolnay (dtolnay)
Expand Down
Loading
Loading