summaryrefslogtreecommitdiff
path: root/crates/parser/src/macros.rs
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2026-08-17 20:07:51 +0200
committerGitHub <noreply@github.com>2026-08-17 20:07:51 +0200
commit358a183e5c12c32b8836acf9c71799f56e00b036 (patch)
treeffe63de847cebf5935951454eb8d76d9c76c222a /crates/parser/src/macros.rs
parenta42cb3434bd79accb6a49298f498e016e9c93b9e (diff)
chore: reduce instruction enum size (#52)
this is a fairly big commit since I've been just playing around with this for a while now. the main goal is to reduce instruction size by moving some operands into two separate vecs. memory operations still seem like the biggest issue right now, so as a follow op I'll probably remove the public linear memory trait again but let's see how this goes first. performance wise this is about the same as before, but serialization/deserialization is about 20% faster now. Signed-off-by: Henry <mail@henrygressmann.de> --------- Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser/src/macros.rs')
-rw-r--r--crates/parser/src/macros.rs155
1 files changed, 25 insertions, 130 deletions
diff --git a/crates/parser/src/macros.rs b/crates/parser/src/macros.rs
index 9fa7a01..5e675e6 100644
--- a/crates/parser/src/macros.rs
+++ b/crates/parser/src/macros.rs
@@ -72,10 +72,17 @@ pub(crate) mod visit {
) => {
fn $visit(&mut self, memarg: wasmparser::MemArg $(, $lane: $ty)?) -> Self::Output {
let address = self.metadata.memory_size(memarg.memory)?;
+ let memory_arg_idx = self.push_operand(MemoryArg::new(memarg.offset, memarg.memory))?;
lowering_ops!(@emit self address(address) [$($input),*] => [$($output),*]
- Instruction::$instr(MemoryArg::new(memarg.offset, memarg.memory) $(, $lane)?).into())
+ lowering_ops!(@memory_instruction $instr memory_arg_idx $(, $lane)?))
}
};
+ (@memory_instruction $instr:ident $memory_arg_idx:ident) => {
+ Instruction::$instr($memory_arg_idx)
+ };
+ (@memory_instruction $instr:ident $memory_arg_idx:ident, $lane:ident) => {
+ Instruction::$instr(tinywasm_types::MemoryLaneArg { memory_arg_idx: $memory_arg_idx, lane: $lane })
+ };
(@global $inputs:tt => $outputs:tt $($operator:tt)*) => {
lowering_ops!(@resolved global_size $inputs => $outputs $($operator)*);
};
@@ -191,6 +198,20 @@ pub(crate) mod optimize {
($instructions:ident, $read:ident, $consumed:expr => $out:expr) => {
replace!($instructions, $read, $consumed => [$out]);
};
+ ($instructions:ident, *$read:ident, $consumed:expr => [$($out:expr),+ $(,)?]) => {{
+ const {
+ assert!($consumed >= 1 && $consumed <= 3);
+ assert!([$(stringify!($out)),+].len() <= $consumed + 1);
+ }
+ let replacements = [$($out),+];
+ let start = *$read - $consumed;
+ $instructions[start..start + replacements.len()].copy_from_slice(&replacements);
+ $instructions.truncate(start + replacements.len());
+ *$read = $instructions.len() - 1;
+ }};
+ ($instructions:ident, *$read:ident, $consumed:expr => $out:expr) => {
+ replace!($instructions, *$read, $consumed => [$out])
+ };
}
macro_rules! rewrite {
@@ -201,16 +222,13 @@ pub(crate) mod optimize {
};
($instructions:ident, $read:ident, [$($pattern:pat),+] $(if ($($guard:tt)+))? => $body:block $(,)?) => {{
const CONSUMED: usize = [$(stringify!($pattern)),+].len();
- if !$instructions.tail_rewritten
- && $read < $instructions.len()
- && $read >= $instructions.block_start + CONSUMED
- {
+ if $read >= $instructions.block_start + CONSUMED {
let previous: [Instruction; CONSUMED] = $instructions[$read - CONSUMED..$read].try_into().unwrap();
if let [$($pattern),+] = previous $(
&& $($guard)+
)? {
- $instructions.tail_rewritten = true;
$body
+ continue;
}
}
}};
@@ -221,128 +239,5 @@ pub(crate) mod optimize {
};
}
- macro_rules! define_local_source_resolver {
- (
- $name:ident,
- get = $get:ident,
- tee = $tee:ident,
- set = $set:ident,
- binop_local_local_tee = $lltee:ident,
- binop_local_local_set = $llset:ident,
- binop_local_const_tee = $lctee:ident,
- binop_local_const_set = $lcset:ident
- $(, load_local_tee = $loadtee:ident, load_local_set = $loadset:ident)?
- ) => {
- fn $name(instr: Instruction) -> Option<(Option<Instruction>, u16)> {
- Some(match instr {
- Instruction::$get(local) => (None, local),
- Instruction::$tee(local) => (Some(Instruction::$set(local)), local),
- Instruction::$lltee(op, a, b, local) => (Some(Instruction::$llset(op, a, b, local)), local),
- Instruction::$lctee(op, src, c, local) => (Some(Instruction::$lcset(op, src, c, local)), local),
- $(Instruction::$loadtee(memarg, addr, local) => (Some(Instruction::$loadset(memarg, addr, local)), local.into()),)?
- _ => return None,
- })
- }
- };
- }
-
- macro_rules! fold_local_binop {
- (
- $instrs:ident, $read:expr, $dst:expr,
- source = $source:ident,
- op = $op:ident,
- const = $const:ident,
- local_local = $local_local:ident,
- local_const = $local_const:expr
- ) => {{
- if !$instrs.tail_rewritten
- && $read < $instrs.len()
- && $read >= $instrs.block_start + 3
- && let [lhs_src, rhs_src, raw_op] = [$instrs[$read - 3], $instrs[$read - 2], $instrs[$read - 1]]
- && let Some((lhs_instr, lhs)) = $source(lhs_src)
- && let Some(op) = $op(raw_op)
- {
- if let Some((rhs_instr, rhs)) = $source(rhs_src) {
- if rhs_instr.is_none() || rhs != lhs {
- $instrs.tail_rewritten = true;
- $instrs.truncate($read - 3);
- $instrs.extend(lhs_instr);
- $instrs.extend(rhs_instr);
- $instrs.push(Instruction::$local_local(op, lhs, rhs, $dst));
- $read = $instrs.len() - 1;
- }
- } else if let Some(imm) = $const(rhs_src, raw_op) {
- $instrs.tail_rewritten = true;
- $instrs.truncate($read - 3);
- $instrs.extend(lhs_instr);
- $instrs.push($local_const($dst, lhs, op, imm));
- $read = $instrs.len() - 1;
- }
- }
- }};
- }
-
- macro_rules! rewrite_local_set_direct {
- (
- $instrs:ident, $read:ident, $dst:expr,
- get = $get:ident,
- copy = $copy:ident,
- binop_local_local = $ll:ident,
- binop_local_local_set = $llset:ident,
- binop_local_const = $lc:ident,
- binop_local_const_set = $lcset:expr
- $(, const_instr = $const_instr:ident, set_local_const = $set_local_const:ident)?
- ) => {{
- rewrite!($instrs, $read, [$get(src)] if (src != $dst) => Instruction::$copy(src, $dst));
- if !$instrs.tail_rewritten
- && $read < $instrs.len()
- && $read > $instrs.block_start
- && let Instruction::$get(src) = $instrs[$read - 1]
- && src == $dst
- {
- $instrs.tail_rewritten = true;
- $instrs.truncate($read - 1);
- $read = $instrs.len();
- }
- $(rewrite!($instrs, $read, [$const_instr(c)] => Instruction::$set_local_const($dst, c));)?
- rewrite!($instrs, $read, [$ll(op, a, b)] => Instruction::$llset(op, a, b, $dst));
- rewrite!($instrs, $read, [$lc(op, src, c)] => { replace!($instrs, $read, 1 => $lcset($dst, src, op, c)); });
- }};
- }
-
- macro_rules! rewrite_local_tee_direct {
- (
- $instrs:ident, $read:ident, $dst:expr,
- get = $get:ident,
- binop_local_local = $ll:ident,
- binop_local_local_tee = $lltee:ident,
- binop_local_const = $lc:ident,
- binop_local_const_tee = $lctee:ident
- ) => {{
- rewrite!($instrs, $read, [$get(src)] if (src == $dst) => Instruction::$get(src));
- rewrite!($instrs, $read, [$ll(op, a, b)] => Instruction::$lltee(op, a, b, $dst));
- rewrite!($instrs, $read, [$lc(op, src, c)] => Instruction::$lctee(op, src, c, $dst));
- }};
- }
-
- macro_rules! rewrite_drop_tee_direct {
- (
- $instrs:ident, $read:ident,
- tee = $tee:ident,
- set = $set:ident,
- binop_local_local_tee = $lltee:ident,
- binop_local_local_set = $llset:ident,
- binop_local_const_tee = $lctee:ident,
- binop_local_const_set = $lcset:ident
- ) => {{
- rewrite!($instrs, $read, [$tee(local)] => Instruction::$set(local));
- rewrite!($instrs, $read, [$lltee(op, a, b, dst)] => Instruction::$llset(op, a, b, dst));
- rewrite!($instrs, $read, [$lctee(op, src, c, dst)] => Instruction::$lcset(op, src, c, dst));
- }};
- }
-
- pub(crate) use {
- define_local_source_resolver, fold_local_binop, replace, rewrite, rewrite_drop_tee_direct,
- rewrite_local_set_direct, rewrite_local_tee_direct,
- };
+ pub(crate) use {replace, rewrite};
}