From c1b029a6b196a1c9e98c04652cd5fe5337ed95b2 Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Sat, 20 Feb 2021 23:44:57 -0800 Subject: [PATCH 01/16] ink::test attributes in new template --- templates/new/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/new/lib.rs b/templates/new/lib.rs index c81744f88..4cb22b451 100644 --- a/templates/new/lib.rs +++ b/templates/new/lib.rs @@ -53,14 +53,14 @@ mod {{name}} { use super::*; /// We test if the default constructor does its job. - #[test] + #[ink::test] fn default_works() { let {{name}} = {{camel_name}}::default(); assert_eq!({{name}}.get(), false); } /// We test a simple use case of our contract. - #[test] + #[ink::test] fn it_works() { let mut {{name}} = {{camel_name}}::new(false); assert_eq!({{name}}.get(), false); From 5461e780a6909f438d02cb36743402ddb507755f Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Sun, 21 Feb 2021 20:08:56 -0800 Subject: [PATCH 02/16] add use ink --- templates/new/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/templates/new/lib.rs b/templates/new/lib.rs index 4cb22b451..dd4a0331e 100644 --- a/templates/new/lib.rs +++ b/templates/new/lib.rs @@ -52,6 +52,8 @@ mod {{name}} { /// Imports all the definitions from the outer scope so we can use them here. use super::*; + use ink_lang as ink; + /// We test if the default constructor does its job. #[ink::test] fn default_works() { From 52980c2ad5431db9770d16aefaf0291a59a6bcad Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Sun, 7 Mar 2021 14:07:25 -0800 Subject: [PATCH 03/16] adding optimization-passes option --- src/cmd/build.rs | 24 ++++++++++++++++-------- src/cmd/metadata.rs | 4 ++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 85ec744d0..533561043 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -66,6 +66,8 @@ pub struct BuildCommand { verbosity: VerbosityFlags, #[structopt(flatten)] unstable_options: UnstableOptions, + #[structopt(long = "optimization-passes", default_value = "3")] + optimization_passes: u32, } impl BuildCommand { @@ -80,6 +82,7 @@ impl BuildCommand { true, self.build_artifact, unstable_flags, + self.optimization_passes, ) } } @@ -102,12 +105,14 @@ impl CheckCommand { let unstable_flags: UnstableFlags = TryFrom::<&UnstableOptions>::try_from(&self.unstable_options)?; let verbosity: Verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?; + let optimization_passes = 0; execute( &manifest_path, verbosity, false, BuildArtifacts::CheckOnly, unstable_flags, + optimization_passes, ) } } @@ -132,6 +137,7 @@ fn build_cargo_project( build_artifact: BuildArtifacts, verbosity: Verbosity, unstable_flags: UnstableFlags, + _optimization_passes: u32, ) -> Result<()> { util::assert_channel()?; @@ -268,14 +274,13 @@ fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> { /// /// The intention is to reduce the size of bloated wasm binaries as a result of missing /// optimizations (or bugs?) between Rust and Wasm. -fn optimize_wasm(crate_metadata: &CrateMetadata) -> Result { +fn optimize_wasm(crate_metadata: &CrateMetadata, optimization_passes: u32) -> Result { let mut dest_optimized = crate_metadata.dest_wasm.clone(); dest_optimized.set_file_name(format!("{}-opt.wasm", crate_metadata.package_name)); - let _ = do_optimization( crate_metadata.dest_wasm.as_os_str(), &dest_optimized.as_os_str(), - 3, + optimization_passes, )?; let original_size = metadata(&crate_metadata.dest_wasm)?.len() as f64 / 1000.0; @@ -334,7 +339,7 @@ fn do_optimization( fn do_optimization( dest_wasm: &OsStr, dest_optimized: &OsStr, - optimization_level: u32, + optimization_passes: u32, ) -> Result<()> { // check `wasm-opt` is installed if which::which("wasm-opt").is_err() { @@ -349,7 +354,7 @@ fn do_optimization( let output = Command::new("wasm-opt") .arg(dest_wasm) - .arg(format!("-O{}", optimization_level)) + .arg(format!("-O{}", optimization_passes)) .arg("-o") .arg(dest_optimized) // the memory in our module is imported, `wasm-opt` needs to be told that @@ -381,6 +386,7 @@ fn execute( optimize_contract: bool, build_artifact: BuildArtifacts, unstable_flags: UnstableFlags, + optimization_passes: u32, ) -> Result { if build_artifact == BuildArtifacts::CodeOnly || build_artifact == BuildArtifacts::CheckOnly { let crate_metadata = CrateMetadata::collect(manifest_path)?; @@ -390,6 +396,7 @@ fn execute( optimize_contract, build_artifact, unstable_flags, + optimization_passes, )?; let res = BuildResult { dest_wasm: maybe_dest_wasm, @@ -403,7 +410,7 @@ fn execute( return Ok(res); } - let res = super::metadata::execute(&manifest_path, verbosity, build_artifact, unstable_flags)?; + let res = super::metadata::execute(&manifest_path, verbosity, build_artifact, unstable_flags, optimization_passes)?; Ok(res) } @@ -422,6 +429,7 @@ pub(crate) fn execute_with_crate_metadata( optimize_contract: bool, build_artifact: BuildArtifacts, unstable_flags: UnstableFlags, + optimization_passes: u32, ) -> Result<(Option, Option)> { maybe_println!( verbosity, @@ -429,7 +437,7 @@ pub(crate) fn execute_with_crate_metadata( format!("[1/{}]", build_artifact.steps()).bold(), "Building cargo project".bright_green().bold() ); - build_cargo_project(&crate_metadata, build_artifact, verbosity, unstable_flags)?; + build_cargo_project(&crate_metadata, build_artifact, verbosity, unstable_flags, optimization_passes)?; maybe_println!( verbosity, " {} {}", @@ -446,7 +454,7 @@ pub(crate) fn execute_with_crate_metadata( format!("[3/{}]", build_artifact.steps()).bold(), "Optimizing wasm file".bright_green().bold() ); - let optimization_result = optimize_wasm(&crate_metadata)?; + let optimization_result = optimize_wasm(&crate_metadata, optimization_passes)?; Ok(( Some(crate_metadata.dest_wasm.clone()), Some(optimization_result), diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 387e16bc6..a447b6936 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -40,6 +40,7 @@ struct GenerateMetadataCommand { verbosity: Verbosity, build_artifact: BuildArtifacts, unstable_options: UnstableFlags, + optimization_passes: u32, } /// Result of generating the extended contract project metadata @@ -239,6 +240,7 @@ impl GenerateMetadataCommand { true, // for the hash we always use the optimized version of the contract self.build_artifact, self.unstable_options.clone(), + self.optimization_passes, )?; let wasm = fs::read(&self.crate_metadata.dest_wasm)?; @@ -265,6 +267,7 @@ pub(crate) fn execute( verbosity: Verbosity, build_artifact: BuildArtifacts, unstable_options: UnstableFlags, + optimization_passes: u32, ) -> Result { let crate_metadata = CrateMetadata::collect(manifest_path)?; let res = GenerateMetadataCommand { @@ -272,6 +275,7 @@ pub(crate) fn execute( verbosity, build_artifact, unstable_options, + optimization_passes, } .exec()?; Ok(res) From c01a44149f782641e56d98b33f26c002fec2a3da Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Sun, 7 Mar 2021 14:11:23 -0800 Subject: [PATCH 04/16] undoing unnecessary variable name change --- src/cmd/build.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 533561043..6fd9d0037 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -339,7 +339,7 @@ fn do_optimization( fn do_optimization( dest_wasm: &OsStr, dest_optimized: &OsStr, - optimization_passes: u32, + optimization_level: u32, ) -> Result<()> { // check `wasm-opt` is installed if which::which("wasm-opt").is_err() { @@ -354,7 +354,7 @@ fn do_optimization( let output = Command::new("wasm-opt") .arg(dest_wasm) - .arg(format!("-O{}", optimization_passes)) + .arg(format!("-O{}", optimization_level)) .arg("-o") .arg(dest_optimized) // the memory in our module is imported, `wasm-opt` needs to be told that From 9546ec0c82069addcfcc68e3f0c854c8861537b3 Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Sun, 7 Mar 2021 14:13:42 -0800 Subject: [PATCH 05/16] remove fork changes --- templates/new/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/templates/new/lib.rs b/templates/new/lib.rs index dd4a0331e..c81744f88 100644 --- a/templates/new/lib.rs +++ b/templates/new/lib.rs @@ -52,17 +52,15 @@ mod {{name}} { /// Imports all the definitions from the outer scope so we can use them here. use super::*; - use ink_lang as ink; - /// We test if the default constructor does its job. - #[ink::test] + #[test] fn default_works() { let {{name}} = {{camel_name}}::default(); assert_eq!({{name}}.get(), false); } /// We test a simple use case of our contract. - #[ink::test] + #[test] fn it_works() { let mut {{name}} = {{camel_name}}::new(false); assert_eq!({{name}}.get(), false); From d14d313dd4d039d38841c21dcc5383df9b18e89c Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Sun, 7 Mar 2021 14:23:42 -0800 Subject: [PATCH 06/16] adding optimization_passes to tests --- src/cmd/build.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 6fd9d0037..8567e7356 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -472,12 +472,14 @@ mod tests_ci_only { cmd::new::execute("new_project", Some(path)).expect("new project creation failed"); let manifest_path = ManifestPath::new(&path.join("new_project").join("Cargo.toml")).unwrap(); + let optimization_passes = 3; let res = super::execute( &manifest_path, None, true, BuildArtifacts::All, UnstableFlags::default(), + optimization_passes ) .expect("build failed"); @@ -503,6 +505,7 @@ mod tests_ci_only { cmd::new::execute("new_project", Some(path)).expect("new project creation failed"); let project_dir = path.join("new_project"); let manifest_path = ManifestPath::new(&project_dir.join("Cargo.toml")).unwrap(); + let optimization_passes = 3; // when super::execute( @@ -511,6 +514,7 @@ mod tests_ci_only { true, BuildArtifacts::CheckOnly, UnstableFlags::default(), + optimization_passes, ) .expect("build failed"); From 9a73bb0ea42b8650a559161149043aed265d1fad Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Sun, 7 Mar 2021 14:45:06 -0800 Subject: [PATCH 07/16] cargo fmting --- src/cmd/build.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 8567e7356..f91b012c3 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -274,7 +274,10 @@ fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> { /// /// The intention is to reduce the size of bloated wasm binaries as a result of missing /// optimizations (or bugs?) between Rust and Wasm. -fn optimize_wasm(crate_metadata: &CrateMetadata, optimization_passes: u32) -> Result { +fn optimize_wasm( + crate_metadata: &CrateMetadata, + optimization_passes: u32, +) -> Result { let mut dest_optimized = crate_metadata.dest_wasm.clone(); dest_optimized.set_file_name(format!("{}-opt.wasm", crate_metadata.package_name)); let _ = do_optimization( @@ -410,7 +413,13 @@ fn execute( return Ok(res); } - let res = super::metadata::execute(&manifest_path, verbosity, build_artifact, unstable_flags, optimization_passes)?; + let res = super::metadata::execute( + &manifest_path, + verbosity, + build_artifact, + unstable_flags, + optimization_passes, + )?; Ok(res) } @@ -437,7 +446,13 @@ pub(crate) fn execute_with_crate_metadata( format!("[1/{}]", build_artifact.steps()).bold(), "Building cargo project".bright_green().bold() ); - build_cargo_project(&crate_metadata, build_artifact, verbosity, unstable_flags, optimization_passes)?; + build_cargo_project( + &crate_metadata, + build_artifact, + verbosity, + unstable_flags, + optimization_passes, + )?; maybe_println!( verbosity, " {} {}", @@ -479,7 +494,7 @@ mod tests_ci_only { true, BuildArtifacts::All, UnstableFlags::default(), - optimization_passes + optimization_passes, ) .expect("build failed"); From a7340ea4faad5356f5adfb7bd4bf05419c7fdfab Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Tue, 9 Mar 2021 16:26:18 -0800 Subject: [PATCH 08/16] switching to enum type --- src/cmd/build.rs | 39 ++++++++++------------- src/cmd/metadata.rs | 6 ++-- src/main.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 25 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index f91b012c3..76ec04b36 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -29,8 +29,8 @@ use crate::{ crate_metadata::CrateMetadata, maybe_println, util, validate_wasm, workspace::{ManifestPath, Profile, Workspace}, - BuildArtifacts, BuildResult, OptimizationResult, UnstableFlags, UnstableOptions, Verbosity, - VerbosityFlags, + BuildArtifacts, BuildResult, OptimizationFlags, OptimizationPasses, OptimizationResult, + UnstableFlags, UnstableOptions, Verbosity, VerbosityFlags, }; use anyhow::{Context, Result}; use colored::Colorize; @@ -66,8 +66,8 @@ pub struct BuildCommand { verbosity: VerbosityFlags, #[structopt(flatten)] unstable_options: UnstableOptions, - #[structopt(long = "optimization-passes", default_value = "3")] - optimization_passes: u32, + #[structopt(flatten)] + optimization_passes: OptimizationFlags, } impl BuildCommand { @@ -76,13 +76,15 @@ impl BuildCommand { let unstable_flags: UnstableFlags = TryFrom::<&UnstableOptions>::try_from(&self.unstable_options)?; let verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?; + let optimization_passes = + TryFrom::<&OptimizationFlags>::try_from(&self.optimization_passes)?; execute( &manifest_path, verbosity, true, self.build_artifact, unstable_flags, - self.optimization_passes, + optimization_passes, ) } } @@ -105,7 +107,7 @@ impl CheckCommand { let unstable_flags: UnstableFlags = TryFrom::<&UnstableOptions>::try_from(&self.unstable_options)?; let verbosity: Verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?; - let optimization_passes = 0; + let optimization_passes = OptimizationPasses::Zero; execute( &manifest_path, verbosity, @@ -137,7 +139,6 @@ fn build_cargo_project( build_artifact: BuildArtifacts, verbosity: Verbosity, unstable_flags: UnstableFlags, - _optimization_passes: u32, ) -> Result<()> { util::assert_channel()?; @@ -276,7 +277,7 @@ fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> { /// optimizations (or bugs?) between Rust and Wasm. fn optimize_wasm( crate_metadata: &CrateMetadata, - optimization_passes: u32, + optimization_passes: OptimizationPasses, ) -> Result { let mut dest_optimized = crate_metadata.dest_wasm.clone(); dest_optimized.set_file_name(format!("{}-opt.wasm", crate_metadata.package_name)); @@ -307,7 +308,7 @@ fn optimize_wasm( fn do_optimization( dest_wasm: &OsStr, dest_optimized: &OsStr, - optimization_level: u32, + optimization_level: OptimizationPasses, ) -> Result<()> { let mut dest_wasm_file = File::open(dest_wasm)?; let mut dest_wasm_file_content = Vec::new(); @@ -315,9 +316,9 @@ fn do_optimization( let codegen_config = binaryen::CodegenConfig { // number of optimization passes (spends potentially a lot of time optimizing) - optimization_level, + optimization_level: optimization_level.to_passes(), // the default - shrink_level: 1, + shrink_level: optimization_level.to_shrink(), // the default debug_info: false, }; @@ -342,7 +343,7 @@ fn do_optimization( fn do_optimization( dest_wasm: &OsStr, dest_optimized: &OsStr, - optimization_level: u32, + optimization_level: OptimizationPasses, ) -> Result<()> { // check `wasm-opt` is installed if which::which("wasm-opt").is_err() { @@ -357,7 +358,7 @@ fn do_optimization( let output = Command::new("wasm-opt") .arg(dest_wasm) - .arg(format!("-O{}", optimization_level)) + .arg(format!("-O{}", optimization_level.to_str())) .arg("-o") .arg(dest_optimized) // the memory in our module is imported, `wasm-opt` needs to be told that @@ -389,7 +390,7 @@ fn execute( optimize_contract: bool, build_artifact: BuildArtifacts, unstable_flags: UnstableFlags, - optimization_passes: u32, + optimization_passes: OptimizationPasses, ) -> Result { if build_artifact == BuildArtifacts::CodeOnly || build_artifact == BuildArtifacts::CheckOnly { let crate_metadata = CrateMetadata::collect(manifest_path)?; @@ -438,7 +439,7 @@ pub(crate) fn execute_with_crate_metadata( optimize_contract: bool, build_artifact: BuildArtifacts, unstable_flags: UnstableFlags, - optimization_passes: u32, + optimization_passes: OptimizationPasses, ) -> Result<(Option, Option)> { maybe_println!( verbosity, @@ -446,13 +447,7 @@ pub(crate) fn execute_with_crate_metadata( format!("[1/{}]", build_artifact.steps()).bold(), "Building cargo project".bright_green().bold() ); - build_cargo_project( - &crate_metadata, - build_artifact, - verbosity, - unstable_flags, - optimization_passes, - )?; + build_cargo_project(&crate_metadata, build_artifact, verbosity, unstable_flags)?; maybe_println!( verbosity, " {} {}", diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index a447b6936..bb1e73a1a 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -18,7 +18,7 @@ use crate::{ crate_metadata::CrateMetadata, maybe_println, util, workspace::{ManifestPath, Workspace}, - BuildArtifacts, BuildResult, OptimizationResult, UnstableFlags, Verbosity, + BuildArtifacts, BuildResult, OptimizationPasses, OptimizationResult, UnstableFlags, Verbosity, }; use anyhow::Result; @@ -40,7 +40,7 @@ struct GenerateMetadataCommand { verbosity: Verbosity, build_artifact: BuildArtifacts, unstable_options: UnstableFlags, - optimization_passes: u32, + optimization_passes: OptimizationPasses, } /// Result of generating the extended contract project metadata @@ -267,7 +267,7 @@ pub(crate) fn execute( verbosity: Verbosity, build_artifact: BuildArtifacts, unstable_options: UnstableFlags, - optimization_passes: u32, + optimization_passes: OptimizationPasses, ) -> Result { let crate_metadata = CrateMetadata::collect(manifest_path)?; let res = GenerateMetadataCommand { diff --git a/src/main.rs b/src/main.rs index 445d48864..5c1138fa8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,6 +93,83 @@ impl ExtrinsicOpts { } } +#[derive(Clone, Debug, StructOpt)] +pub struct OptimizationFlags { + // number of optimization passes + #[structopt(long = "optimization-passes", default_value = "3")] + optimization_passes: String, +} + +/// Denotes if output should be printed to stdout. +#[derive(Clone, Copy)] +pub enum OptimizationPasses { + /// + Zero, + One, + Two, + Three, + Four, + S, + Z, +} + +impl TryFrom<&OptimizationFlags> for OptimizationPasses { + type Error = Error; + + fn try_from(value: &OptimizationFlags) -> Result { + match value.optimization_passes.to_lowercase().as_str() { + "0" => Ok(OptimizationPasses::Zero), + "1" => Ok(OptimizationPasses::One), + "2" => Ok(OptimizationPasses::Two), + "3" => Ok(OptimizationPasses::Three), + "4" => Ok(OptimizationPasses::Four), + "s" => Ok(OptimizationPasses::S), + "z" => Ok(OptimizationPasses::Z), + _ => anyhow::bail!( + "Unknown optimization passes option {}", + value.optimization_passes + ), + } + } +} + +impl OptimizationPasses { + /// Returns string representation of OptimizationPasses + pub(crate) fn to_str(&self) -> &str { + match self { + OptimizationPasses::Zero => "0", + OptimizationPasses::One => "1", + OptimizationPasses::Two => "2", + OptimizationPasses::Three => "3", + OptimizationPasses::Four => "4", + OptimizationPasses::S => "s", + OptimizationPasses::Z => "z", + } + } + + /// to_passes returns number of optimization passes to do + pub(crate) fn to_passes(&self) -> u32 { + match self { + OptimizationPasses::Zero => 0, + OptimizationPasses::One => 1, + OptimizationPasses::Two => 2, + OptimizationPasses::Three => 3, + OptimizationPasses::Four => 4, + _ => 3, // Default to three for shrink settings + } + } + + /// to_shrink returns amount of shrinkage to do + pub(crate) fn to_shrink(&self) -> u32 { + match self { + OptimizationPasses::Zero => 0, + OptimizationPasses::S => 1, + OptimizationPasses::Z => 2, + _ => 1, + } + } +} + #[derive(Clone, Debug, StructOpt)] pub struct VerbosityFlags { /// No output printed to stdout From b8d195ca44bee4f2b26d1f8037d4ae1dcb5c4775 Mon Sep 17 00:00:00 2001 From: trace-andreason Date: Thu, 11 Mar 2021 09:33:56 -0800 Subject: [PATCH 09/16] Update src/main.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 5c1138fa8..94738cf54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,7 @@ impl OptimizationPasses { } } - /// to_passes returns number of optimization passes to do + /// Returns the number of optimization passes to do pub(crate) fn to_passes(&self) -> u32 { match self { OptimizationPasses::Zero => 0, From 0ec57710838131055d074ee1d9f2727606a24ffd Mon Sep 17 00:00:00 2001 From: trace-andreason Date: Thu, 11 Mar 2021 09:34:02 -0800 Subject: [PATCH 10/16] Update src/main.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 94738cf54..d6e4fde43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,7 +159,7 @@ impl OptimizationPasses { } } - /// to_shrink returns amount of shrinkage to do + /// Returns amount of shrinkage to do pub(crate) fn to_shrink(&self) -> u32 { match self { OptimizationPasses::Zero => 0, From 23832f28b6bff874fbb80ad4287e97ebf7477761 Mon Sep 17 00:00:00 2001 From: trace-andreason Date: Thu, 11 Mar 2021 09:34:12 -0800 Subject: [PATCH 11/16] Update src/main.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index d6e4fde43..265b7d8ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,7 +134,7 @@ impl TryFrom<&OptimizationFlags> for OptimizationPasses { } impl OptimizationPasses { - /// Returns string representation of OptimizationPasses + /// Returns the string representation of `OptimizationPasses` pub(crate) fn to_str(&self) -> &str { match self { OptimizationPasses::Zero => "0", From 8ddd27770d7078d7745c2597f637c81ff4de5358 Mon Sep 17 00:00:00 2001 From: trace-andreason Date: Thu, 11 Mar 2021 09:34:20 -0800 Subject: [PATCH 12/16] Update src/main.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 265b7d8ef..ad0601acd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,6 +148,7 @@ impl OptimizationPasses { } /// Returns the number of optimization passes to do + #[cfg(feature = "binaryen-as-dependency")] pub(crate) fn to_passes(&self) -> u32 { match self { OptimizationPasses::Zero => 0, From 16abf4a8a8c7578cf87aeb787da84937ff79af70 Mon Sep 17 00:00:00 2001 From: trace-andreason Date: Thu, 11 Mar 2021 09:34:26 -0800 Subject: [PATCH 13/16] Update src/main.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index ad0601acd..fae9033d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,6 +161,7 @@ impl OptimizationPasses { } /// Returns amount of shrinkage to do + #[cfg(feature = "binaryen-as-dependency")] pub(crate) fn to_shrink(&self) -> u32 { match self { OptimizationPasses::Zero => 0, From c29217c3a5f278dbf2ccbdfd49e95cb231bd10ec Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Thu, 11 Mar 2021 21:45:09 -0800 Subject: [PATCH 14/16] adding cli help comment --- src/main.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index fae9033d9..55b0b0d18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,15 +95,28 @@ impl ExtrinsicOpts { #[derive(Clone, Debug, StructOpt)] pub struct OptimizationFlags { - // number of optimization passes + /// number of optimization passes, passed as an argument to wasm-opt + /// + /// - `0`: execute no optimization passes + /// + /// - `1`: execute 1 optimization pass (quick & useful opts, useful for iteration builds) + /// + /// - `2`, execute 2 optimization passes (most opts, generally gets most perf) + /// + /// - `3`, execute 3 optimization passes (spends potentially a lot of time optimizing) + /// + /// - `4`, execute 4 optimization passes (also flatten the IR, which can take a lot more time and memory + /// but is useful on more nested / complex / less-optimized input) + /// + /// - `s`, execute default optimization passes, focusing on code size + /// + /// - `z`, execute default optimization passes, super-focusing on code size #[structopt(long = "optimization-passes", default_value = "3")] optimization_passes: String, } -/// Denotes if output should be printed to stdout. #[derive(Clone, Copy)] pub enum OptimizationPasses { - /// Zero, One, Two, From ce256771f42d4ef920e47de1cce9f040736587b8 Mon Sep 17 00:00:00 2001 From: Trace Andreason Date: Thu, 11 Mar 2021 21:50:40 -0800 Subject: [PATCH 15/16] add line so default option is more visible --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.rs b/src/main.rs index 55b0b0d18..dccdb0b75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,6 +111,8 @@ pub struct OptimizationFlags { /// - `s`, execute default optimization passes, focusing on code size /// /// - `z`, execute default optimization passes, super-focusing on code size + /// + /// - #[structopt(long = "optimization-passes", default_value = "3")] optimization_passes: String, } From 28c8ed8637d6a196536212e6c7b1424de56dbbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=BCller?= Date: Thu, 18 Mar 2021 13:14:04 +0100 Subject: [PATCH 16/16] Update src/cmd/build.rs --- src/cmd/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 76ec04b36..6ec536f72 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -315,7 +315,7 @@ fn do_optimization( dest_wasm_file.read_to_end(&mut dest_wasm_file_content)?; let codegen_config = binaryen::CodegenConfig { - // number of optimization passes (spends potentially a lot of time optimizing) + // Number of optimization passes (spends potentially a lot of time optimizing) optimization_level: optimization_level.to_passes(), // the default shrink_level: optimization_level.to_shrink(),