Skip to content

Commit 4d829a8

Browse files
chore: reduce instantiation ovherhead & refactor optimize
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 19038bf commit 4d829a8

20 files changed

Lines changed: 1258 additions & 475 deletions

File tree

‎Cargo.lock‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/parser/src/lib.rs‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,22 @@ pub use tinywasm_types::Module;
4747
pub struct ParserOptions {
4848
/// Whether to optimize local memory allocation by skipping allocation of unused local memories.
4949
pub optimize_local_memory_allocation: bool,
50+
/// Whether to run the peephole rewrite optimizer.
51+
pub optimize_rewrite: bool,
52+
/// Whether to remove `Nop` and `MergeBarrier` instructions after rewriting.
53+
pub optimize_remove_nop: bool,
54+
/// Whether to invert conditional branches over an unconditional jump.
55+
pub optimize_branch_inversion: bool,
5056
}
5157

5258
impl Default for ParserOptions {
5359
fn default() -> Self {
54-
Self { optimize_local_memory_allocation: true }
60+
Self {
61+
optimize_local_memory_allocation: true,
62+
optimize_rewrite: true,
63+
optimize_remove_nop: true,
64+
optimize_branch_inversion: false,
65+
}
5566
}
5667
}
5768

@@ -66,6 +77,39 @@ impl ParserOptions {
6677
pub const fn optimize_local_memory_allocation(&self) -> bool {
6778
self.optimize_local_memory_allocation
6879
}
80+
81+
/// Enable or disable the peephole rewrite optimizer.
82+
pub const fn with_rewrite_optimization(mut self, enabled: bool) -> Self {
83+
self.optimize_rewrite = enabled;
84+
self
85+
}
86+
87+
/// Returns whether the peephole rewrite optimizer is enabled.
88+
pub const fn optimize_rewrite(&self) -> bool {
89+
self.optimize_rewrite
90+
}
91+
92+
/// Enable or disable `Nop`/`MergeBarrier` removal after rewriting.
93+
pub const fn with_nop_removal_optimization(mut self, enabled: bool) -> Self {
94+
self.optimize_remove_nop = enabled;
95+
self
96+
}
97+
98+
/// Returns whether `Nop`/`MergeBarrier` removal is enabled.
99+
pub const fn optimize_remove_nop(&self) -> bool {
100+
self.optimize_remove_nop
101+
}
102+
103+
/// Enable or disable the optimization that inverts conditional branches over an unconditional jump.
104+
pub const fn with_branch_inversion_optimization(mut self, enabled: bool) -> Self {
105+
self.optimize_branch_inversion = enabled;
106+
self
107+
}
108+
109+
/// Returns whether conditional branch inversion optimization is enabled.
110+
pub const fn optimize_branch_inversion(&self) -> bool {
111+
self.optimize_branch_inversion
112+
}
69113
}
70114

71115
/// A WebAssembly parser

‎crates/parser/src/macros.rs‎

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,120 @@ pub(crate) mod optimize {
216216
};
217217
}
218218

219-
pub(crate) use {replace, rewrite};
219+
macro_rules! define_local_source_resolver {
220+
(
221+
$name:ident,
222+
get = $get:ident,
223+
tee = $tee:ident,
224+
set = $set:ident,
225+
copy = $copy:ident,
226+
binop_local_local_tee = $lltee:ident,
227+
binop_local_local_set = $llset:ident,
228+
binop_local_const_tee = $lctee:ident,
229+
binop_local_const_set = $lcset:ident
230+
$(, load_local_tee = $loadtee:ident, load_local_set = $loadset:ident)?
231+
) => {
232+
fn $name(instrs: &mut [Instruction], read: usize, instr: Instruction) -> Option<(Instruction, u16)> {
233+
Some(match instr {
234+
Instruction::$get(local) => (Instruction::Nop, local),
235+
Instruction::$tee(local) => {
236+
let replacement = if let Some([(prev_idx, Instruction::$get(src))]) = previous_non_nop::<1>(instrs, read) {
237+
instrs[prev_idx] = Instruction::Nop;
238+
if src == local { Instruction::Nop } else { Instruction::$copy(src, local) }
239+
} else {
240+
Instruction::$set(local)
241+
};
242+
(replacement, local)
243+
}
244+
Instruction::$lltee(op, a, b, local) => (Instruction::$llset(op, a, b, local), local),
245+
Instruction::$lctee(op, src, c, local) => (Instruction::$lcset(op, src, c, local), local),
246+
$(Instruction::$loadtee(memarg, addr, local) => (Instruction::$loadset(memarg, addr, local), local.into()),)?
247+
_ => return None,
248+
})
249+
}
250+
};
251+
}
252+
253+
macro_rules! fold_local_binop {
254+
(
255+
$instrs:ident, $read:expr, $dst:expr,
256+
source = $source:ident,
257+
op = $op:ident,
258+
const = $const:ident,
259+
local_local = $local_local:ident,
260+
local_const = $local_const:expr
261+
) => {{
262+
if let Some([(lhs_idx, lhs_src), (rhs_idx, rhs_src), (op_idx, raw_op)]) =
263+
previous_non_nop::<3>($instrs, $read)
264+
&& let Some((lhs_instr, lhs)) = $source($instrs, lhs_idx, lhs_src)
265+
&& let Some(op) = $op(raw_op)
266+
{
267+
if let Some((rhs_instr, rhs)) = $source($instrs, rhs_idx, rhs_src) {
268+
$instrs[lhs_idx] = lhs_instr;
269+
$instrs[rhs_idx] = rhs_instr;
270+
$instrs[op_idx] = Instruction::Nop;
271+
$instrs[$read] = Instruction::$local_local(op, lhs, rhs, $dst);
272+
} else if let Some(imm) = $const(rhs_src, raw_op) {
273+
$instrs[lhs_idx] = lhs_instr;
274+
$instrs[rhs_idx] = Instruction::Nop;
275+
$instrs[op_idx] = Instruction::Nop;
276+
$instrs[$read] = $local_const($dst, lhs, op, imm);
277+
}
278+
}
279+
}};
280+
}
281+
282+
macro_rules! rewrite_local_set_direct {
283+
(
284+
$instrs:ident, $read:ident, $dst:expr,
285+
get = $get:ident,
286+
copy = $copy:ident,
287+
binop_local_local = $ll:ident,
288+
binop_local_local_set = $llset:ident,
289+
binop_local_const = $lc:ident,
290+
binop_local_const_set = $lcset:expr
291+
$(, const_instr = $const_instr:ident, set_local_const = $set_local_const:ident)?
292+
) => {{
293+
rewrite!($instrs, $read, [$get(src)] => if src == $dst { Instruction::Nop } else { Instruction::$copy(src, $dst) });
294+
$(rewrite!($instrs, $read, [$const_instr(c)] => Instruction::$set_local_const($dst, c));)?
295+
rewrite!($instrs, $read, [$ll(op, a, b)] => Instruction::$llset(op, a, b, $dst));
296+
rewrite!($instrs, $read, [$lc(op, src, c)] => { replace!($instrs, $read, 1 => $lcset($dst, src, op, c)); });
297+
}};
298+
}
299+
300+
macro_rules! rewrite_local_tee_direct {
301+
(
302+
$instrs:ident, $read:ident, $dst:expr,
303+
get = $get:ident,
304+
binop_local_local = $ll:ident,
305+
binop_local_local_tee = $lltee:ident,
306+
binop_local_const = $lc:ident,
307+
binop_local_const_tee = $lctee:ident
308+
) => {{
309+
rewrite!($instrs, $read, [$get(src)] if (src == $dst) => [Instruction::$get(src), Instruction::Nop]);
310+
rewrite!($instrs, $read, [$ll(op, a, b)] => Instruction::$lltee(op, a, b, $dst));
311+
rewrite!($instrs, $read, [$lc(op, src, c)] => Instruction::$lctee(op, src, c, $dst));
312+
}};
313+
}
314+
315+
macro_rules! rewrite_drop_tee_direct {
316+
(
317+
$instrs:ident, $read:ident,
318+
tee = $tee:ident,
319+
set = $set:ident,
320+
binop_local_local_tee = $lltee:ident,
321+
binop_local_local_set = $llset:ident,
322+
binop_local_const_tee = $lctee:ident,
323+
binop_local_const_set = $lcset:ident
324+
) => {{
325+
rewrite!($instrs, $read, [$tee(local)] => [Instruction::$set(local), Instruction::Nop]);
326+
rewrite!($instrs, $read, [$lltee(op, a, b, dst)] => Instruction::$llset(op, a, b, dst));
327+
rewrite!($instrs, $read, [$lctee(op, src, c, dst)] => Instruction::$lcset(op, src, c, dst));
328+
}};
329+
}
330+
331+
pub(crate) use {
332+
define_local_source_resolver, fold_local_binop, replace, rewrite, rewrite_drop_tee_direct,
333+
rewrite_local_set_direct, rewrite_local_tee_direct,
334+
};
220335
}

‎crates/parser/src/module.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,26 @@ impl ModuleReader {
200200
{
201201
let ty = self.func_types.get(ty_idx as usize).expect("No func type for func, this is a bug").clone();
202202
let params = ValueCounts::from_iter(ty.params());
203+
let results = ValueCounts::from_iter(ty.results());
203204
let self_func = (imported_func_count + func_idx) as u32;
204205
let local_mem_alloc =
205206
optimize_local_memory_allocation && local_memory_allocation != LocalMemoryAllocation::Eager;
206-
let optimized =
207-
optimize::optimize_instructions(instructions, &mut data, self_func, import_mem_count, local_mem_alloc);
207+
let optimized = optimize::optimize_instructions(
208+
instructions,
209+
&mut data,
210+
options,
211+
self_func,
212+
import_mem_count,
213+
local_mem_alloc,
214+
);
208215

209216
if optimized.uses_local_memory {
210217
local_memory_allocation = LocalMemoryAllocation::Eager;
211218
}
212219

213-
funcs.push(WasmFunction { instructions: optimized.instructions.into(), data, locals, params, ty }.into());
220+
funcs.push(
221+
WasmFunction { instructions: optimized.instructions.into(), data, locals, params, results, ty }.into(),
222+
);
214223
}
215224

216225
Ok(ModuleInner {

0 commit comments

Comments
 (0)