wasm_opt/
builder.rs

1//! A builder API for `OptimizationOptions`.
2
3use crate::api::*;
4
5/// Builder methods.
6impl OptimizationOptions {
7    /// Sets [`ReaderOptions::file_type`].
8    pub fn reader_file_type(&mut self, value: FileType) -> &mut Self {
9        self.reader.file_type = value;
10        self
11    }
12
13    /// Sets [`WriterOptions::file_type`].
14    pub fn writer_file_type(&mut self, value: FileType) -> &mut Self {
15        self.writer.file_type = value;
16        self
17    }
18
19    /// Sets [`OptimizationOptions::converge`].
20    pub fn set_converge(&mut self) -> &mut Self {
21        self.converge = true;
22        self
23    }
24
25    /// Sets [`InliningOptions::always_inline_max_size`].
26    pub fn always_inline_max_size(&mut self, value: u32) -> &mut Self {
27        self.inlining.always_inline_max_size = value;
28        self
29    }
30
31    /// Sets [`InliningOptions::one_caller_inline_max_size`].
32    pub fn one_caller_inline_max_size(&mut self, value: u32) -> &mut Self {
33        self.inlining.one_caller_inline_max_size = value;
34        self
35    }
36
37    /// Sets [`InliningOptions::flexible_inline_max_size`].
38    pub fn flexible_inline_max_size(&mut self, value: u32) -> &mut Self {
39        self.inlining.flexible_inline_max_size = value;
40        self
41    }
42
43    /// Sets [`InliningOptions::allow_functions_with_loops`].
44    pub fn allow_functions_with_loops(&mut self, value: bool) -> &mut Self {
45        self.inlining.allow_functions_with_loops = value;
46        self
47    }
48
49    /// Sets [`InliningOptions::partial_inlining_ifs`].
50    pub fn partial_inlining_ifs(&mut self, value: u32) -> &mut Self {
51        self.inlining.partial_inlining_ifs = value;
52        self
53    }
54
55    /// Sets [`PassOptions::validate`].
56    pub fn validate(&mut self, value: bool) -> &mut Self {
57        self.passopts.validate = value;
58        self
59    }
60
61    /// Sets [`PassOptions::validate_globally`].
62    pub fn validate_globally(&mut self, value: bool) -> &mut Self {
63        self.passopts.validate_globally = value;
64        self
65    }
66
67    /// Sets [`PassOptions::optimize_level`].
68    pub fn optimize_level(&mut self, value: OptimizeLevel) -> &mut Self {
69        self.passopts.optimize_level = value;
70        self
71    }
72
73    /// Sets [`PassOptions::shrink_level`].
74    pub fn shrink_level(&mut self, value: ShrinkLevel) -> &mut Self {
75        self.passopts.shrink_level = value;
76        self
77    }
78
79    /// Sets [`PassOptions::traps_never_happen`].
80    pub fn traps_never_happen(&mut self, value: bool) -> &mut Self {
81        self.passopts.traps_never_happen = value;
82        self
83    }
84
85    /// Sets [`PassOptions::low_memory_unused`].
86    pub fn low_memory_unused(&mut self, value: bool) -> &mut Self {
87        self.passopts.low_memory_unused = value;
88        self
89    }
90
91    /// Sets [`PassOptions::fast_math`].
92    pub fn fast_math(&mut self, value: bool) -> &mut Self {
93        self.passopts.fast_math = value;
94        self
95    }
96
97    /// Sets [`PassOptions::zero_filled_memory`].
98    pub fn zero_filled_memory(&mut self, value: bool) -> &mut Self {
99        self.passopts.zero_filled_memory = value;
100        self
101    }
102
103    /// Sets [`PassOptions::debug_info`].
104    pub fn debug_info(&mut self, value: bool) -> &mut Self {
105        self.passopts.debug_info = value;
106        self
107    }
108
109    /// Adds a pass argument to [`PassOptions::arguments`].
110    pub fn set_pass_arg(&mut self, key: &str, value: &str) -> &mut Self {
111        self.passopts
112            .arguments
113            .insert(key.to_string(), value.to_string());
114        self
115    }
116
117    /// Sets [`Passes::add_default_passes`].
118    pub fn add_default_passes(&mut self, value: bool) -> &mut Self {
119        self.passes.add_default_passes = value;
120        self
121    }
122
123    /// Adds a pass to [`Passes::more_passes`].
124    pub fn add_pass(&mut self, value: Pass) -> &mut Self {
125        self.passes.more_passes.push(value);
126        self
127    }
128
129    /// Sets the baseline feature set to [`FeatureBaseline::MvpOnly`].
130    pub fn mvp_features_only(&mut self) -> &mut Self {
131        self.features.baseline = FeatureBaseline::MvpOnly;
132        self
133    }
134
135    /// Sets the baseline feature set to [`FeatureBaseline::All`].
136    pub fn all_features(&mut self) -> &mut Self {
137        self.features.baseline = FeatureBaseline::All;
138        self
139    }
140
141    /// Enables a feature.
142    ///
143    /// This adds the feature to [`Features::enabled`], and is equivalent to the
144    /// `--enable-{feature}` command line arguments.
145    pub fn enable_feature(&mut self, feature: Feature) -> &mut Self {
146        self.features.enabled.insert(feature);
147        self
148    }
149
150    /// Disables a feature.
151    ///
152    /// This adds the feature to [`Features::disabled`], and is equivalent to
153    /// the `--disable-{feature}` command line arguments.
154    pub fn disable_feature(&mut self, feature: Feature) -> &mut Self {
155        self.features.disabled.insert(feature);
156        self
157    }
158}