Skip to content

Commit

Permalink
Run wasm-opt first, remove sign_ext feature (#1416)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascjones authored Dec 1, 2023
1 parent bb6f0ce commit 981a439
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include = ["Cargo.toml", "*.rs", "LICENSE"]

[dependencies]
contract-metadata = { version = "4.0.0-rc", path = "../metadata" }
parity-wasm = { version = "0.45.0", features = ["sign_ext"] }
parity-wasm = { version = "0.45.0" }
anyhow = "1.0.75"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rustc_version = "0.4.0"
scale = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
toml = "0.8.8"
tracing = "0.1.40"
parity-wasm = { version = "0.45.0", features = ["sign_ext"] }
parity-wasm = { version = "0.45.0"}
semver = { version = "1.0.20", features = ["serde"] }
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1.0.108"
Expand Down
17 changes: 7 additions & 10 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,14 @@ fn load_module<P: AsRef<Path>>(path: P) -> Result<Module> {

/// Performs required post-processing steps on the Wasm artifact.
fn post_process_wasm(
crate_metadata: &CrateMetadata,
optimized_code: &PathBuf,
skip_wasm_validation: bool,
verbosity: &Verbosity,
max_memory_pages: u32,
) -> Result<()> {
// Deserialize Wasm module from a file.
let mut module = load_module(&crate_metadata.original_code)
.context("Loading of original wasm failed")?;
let mut module =
load_module(optimized_code).context("Loading of optimized wasm failed")?;

strip_exports(&mut module);
ensure_maximum_memory_pages(&mut module, max_memory_pages)?;
Expand All @@ -761,7 +761,7 @@ fn post_process_wasm(
"resulting wasm size of post processing must be > 0"
);

parity_wasm::serialize_to_file(&crate_metadata.dest_code, module)?;
parity_wasm::serialize_to_file(optimized_code, module)?;
Ok(())
}

Expand Down Expand Up @@ -1007,17 +1007,14 @@ fn local_build(

match target {
Target::Wasm => {
let handler = WasmOptHandler::new(*optimization_passes, *keep_debug_symbols)?;
handler.optimize(&crate_metadata.original_code, &crate_metadata.dest_code)?;
post_process_wasm(
crate_metadata,
&crate_metadata.dest_code,
*skip_wasm_validation,
verbosity,
*max_memory_pages,
)?;
let handler = WasmOptHandler::new(*optimization_passes, *keep_debug_symbols)?;
handler.optimize(
&crate_metadata.dest_code,
&crate_metadata.contract_artifact_name,
)?;
}
Target::RiscV => {
fs::copy(&crate_metadata.original_code, &crate_metadata.dest_code)?;
Expand Down
19 changes: 4 additions & 15 deletions crates/build/src/wasm_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,7 @@ impl WasmOptHandler {
/// Attempts to perform optional Wasm optimization using Binaryen's `wasm-opt` tool.
///
/// If successful, the optimized Wasm binary is written to `dest_wasm`.
pub fn optimize(
&self,
dest_wasm: &PathBuf,
contract_artifact_name: &String,
) -> Result<()> {
// We'll create a temporary file for our optimized Wasm binary. Note that we'll
// later overwrite this with the original path of the Wasm binary.
let mut dest_optimized = dest_wasm.clone();
dest_optimized.set_file_name(format!("{contract_artifact_name}-opt.wasm"));

pub fn optimize(&self, original_wasm: &PathBuf, dest_wasm: &PathBuf) -> Result<()> {
tracing::debug!(
"Optimization level passed to wasm-opt: {}",
self.optimization_level
Expand All @@ -82,17 +73,15 @@ impl WasmOptHandler {
// memory-packing pre-pass.
.zero_filled_memory(true)
.debug_info(self.keep_debug_symbols)
.run(dest_wasm, &dest_optimized)?;
.run(original_wasm, dest_wasm)?;

if !dest_optimized.exists() {
if !dest_wasm.exists() {
return Err(anyhow::anyhow!(
"Optimization failed, optimized wasm output file `{}` not found.",
dest_optimized.display()
dest_wasm.display()
))
}

// Overwrite existing destination wasm file with the optimised version
std::fs::rename(&dest_optimized, dest_wasm)?;
Ok(())
}
}
Expand Down

0 comments on commit 981a439

Please sign in to comment.