Skip to content

Commit 843e0ff

Browse files
chore: simplify operands, extract more exec_ methods
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent a8f42e4 commit 843e0ff

27 files changed

Lines changed: 1942 additions & 1436 deletions

‎benches/fibonacci.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn criterion_benchmark(c: &mut Criterion) {
4444
group.bench_function("fibonacci_from_twasm", |b| b.iter(|| fibonacci_from_twasm(&twasm)));
4545

4646
group.measurement_time(std::time::Duration::from_secs(10));
47-
group.bench_function("fibonacci_iterative_60", |b| b.iter(|| fibonacci_run(module.clone(), false, 60)));
47+
group.bench_function("fibonacci_iterative_100", |b| b.iter(|| fibonacci_run(module.clone(), false, 100)));
4848
group.bench_function("fibonacci_recursive_26", |b| b.iter(|| fibonacci_run(module.clone(), true, 26)));
4949
}
5050

‎crates/parser/src/conversion.rs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ pub(crate) fn convert_module_code(
186186
}
187187

188188
#[cfg(feature = "validate")]
189-
let (body, data, validator_allocs, reader_allocs) = match validator {
189+
let (body, data, uses_local_memory, validator_allocs, reader_allocs) = match validator {
190190
Some(validator) => {
191-
let (body, data, validator_allocs, reader_allocs) = process_operators_and_validate(
191+
let (body, data, uses_local_memory, validator_allocs, reader_allocs) = process_operators_and_validate(
192192
validator,
193193
func,
194194
(local_types, local_addr_map),
@@ -197,34 +197,38 @@ pub(crate) fn convert_module_code(
197197
reader_allocs,
198198
options.deduplicate_operands(),
199199
)?;
200-
(body, data, Some(validator_allocs), reader_allocs)
200+
(body, data, uses_local_memory, Some(validator_allocs), reader_allocs)
201201
}
202202
None => {
203-
let (body, data, reader_allocs) = process_operators(
203+
let (body, data, uses_local_memory, reader_allocs) = process_operators(
204204
func,
205205
(local_types, local_addr_map),
206206
metadata,
207207
ty_idx,
208208
reader_allocs,
209209
options.deduplicate_operands(),
210210
)?;
211-
(body, data, None, reader_allocs)
211+
(body, data, uses_local_memory, None, reader_allocs)
212212
}
213213
};
214214
#[cfg(not(feature = "validate"))]
215-
let (body, data, validator_allocs, reader_allocs) = {
215+
let (body, data, uses_local_memory, validator_allocs, reader_allocs) = {
216216
let _ = validator;
217-
let (body, data, reader_allocs) = process_operators(
217+
let (body, data, uses_local_memory, reader_allocs) = process_operators(
218218
func,
219219
(local_types, local_addr_map),
220220
metadata,
221221
ty_idx,
222222
reader_allocs,
223223
options.deduplicate_operands(),
224224
)?;
225-
(body, data, None, reader_allocs)
225+
(body, data, uses_local_memory, None, reader_allocs)
226226
};
227-
Ok((FunctionCode { instructions: body, data, locals: local_counts }, validator_allocs, reader_allocs))
227+
Ok((
228+
FunctionCode { instructions: body, data, locals: local_counts, uses_local_memory },
229+
validator_allocs,
230+
reader_allocs,
231+
))
228232
}
229233

230234
pub(crate) fn convert_rec_group(ty: wasmparser::RecGroup, group_start: u32, types: &mut Vec<SubType>) -> Result<u32> {

‎crates/parser/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ pub struct ParserOptions {
6161
/// Disable this only for trusted input. Parsing without validation may produce
6262
/// a module that violates runtime assumptions.
6363
pub validation: bool,
64+
6465
/// Whether to run the peephole rewrite optimizer.
6566
pub optimize_rewrite: bool,
67+
6668
/// Whether to deduplicate immutable function operands while parsing.
67-
///
68-
/// This uses more parse CPU to reduce precompiled module and archive size.
6969
pub deduplicate_operands: bool,
7070

7171
#[cfg(parallel_parser)]

‎crates/parser/src/macros.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ pub(crate) mod visit {
7272
) => {
7373
fn $visit(&mut self, memarg: wasmparser::MemArg $(, $lane: $ty)?) -> Self::Output {
7474
let address = self.metadata.memory_size(memarg.memory)?;
75-
let memory_arg_idx = self.push_operand(MemoryArg::new(memarg.offset, memarg.memory))?;
75+
self.mark_memory(memarg.memory);
76+
let memory_arg_idx = self.push_operand128(tinywasm_types::Operand128::<
77+
tinywasm_types::MemoryOperand,
78+
>::new(memarg.offset, memarg.memory))?;
7679
lowering_ops!(@emit self address(address) [$($input),*] => [$($output),*]
7780
lowering_ops!(@memory_instruction $instr memory_arg_idx $(, $lane)?))
7881
}
@@ -87,7 +90,17 @@ pub(crate) mod visit {
8790
lowering_ops!(@resolved global_size $inputs => $outputs $($operator)*);
8891
};
8992
(@memory_index $inputs:tt => $outputs:tt $($operator:tt)*) => {
90-
lowering_ops!(@resolved memory_size $inputs => $outputs $($operator)*);
93+
lowering_ops!(@memory_index_impl $inputs => $outputs $($operator)*);
94+
};
95+
(@memory_index_impl [$($input:ident),*] => [$($output:ident),*]
96+
$visit:ident($index:ident: $ty:ty) => $instr:ident
97+
) => {
98+
fn $visit(&mut self, $index: $ty) -> Self::Output {
99+
let address = self.metadata.memory_size($index)?;
100+
self.mark_memory($index);
101+
lowering_ops!(@emit self address(address) [$($input),*] => [$($output),*]
102+
Instruction::$instr($index).into())
103+
}
91104
};
92105
(@table $inputs:tt => $outputs:tt $($operator:tt)*) => {
93106
lowering_ops!(@resolved table_size $inputs => $outputs $($operator)*);

‎crates/parser/src/module.rs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ pub(crate) struct FunctionCode {
1313
pub instructions: Vec<Instruction>,
1414
pub data: crate::visit::FunctionDataBuilder,
1515
pub locals: ValueCounts,
16+
pub uses_local_memory: bool,
1617
}
1718

1819
pub(crate) struct OptimizedFunctionCode {
1920
pub instructions: Vec<Instruction>,
2021
pub data: WasmFunctionData,
2122
pub locals: ValueCounts,
23+
pub uses_local_memory: bool,
2224
}
2325

2426
pub(crate) fn optimize_function_code(
@@ -30,7 +32,12 @@ pub(crate) fn optimize_function_code(
3032
let optimized =
3133
optimize::optimize_instructions(code.instructions, &mut code.data, options, function_results, self_func_addr)?;
3234
let data = code.data.finish();
33-
Ok(OptimizedFunctionCode { instructions: optimized.instructions, data, locals: code.locals })
35+
Ok(OptimizedFunctionCode {
36+
instructions: optimized.instructions,
37+
data,
38+
locals: code.locals,
39+
uses_local_memory: code.uses_local_memory,
40+
})
3441
}
3542

3643
#[derive(Default)]
@@ -535,6 +542,20 @@ impl<'a> ModuleReader<'a> {
535542
.chain(self.code_type_addrs.iter().copied())
536543
.collect();
537544

545+
let imported_memory_count =
546+
self.imports.iter().filter(|import| matches!(import.kind, ImportKind::Memory(_))).count();
547+
let uses_local_memory = self.code.iter().any(|code| code.uses_local_memory);
548+
let exports_local_memory = self
549+
.exports
550+
.iter()
551+
.any(|export| export.kind == ExternalKind::Memory && export.index as usize >= imported_memory_count);
552+
let initializes_local_memory = self
553+
.data
554+
.iter()
555+
.any(|data| matches!(data.kind, DataKind::Active { mem, .. } if mem as usize >= imported_memory_count));
556+
let skip_local_memory_allocation =
557+
!self.memory_types.is_empty() && !uses_local_memory && !exports_local_memory && !initializes_local_memory;
558+
538559
let funcs = self
539560
.code
540561
.into_iter()
@@ -567,6 +588,7 @@ impl<'a> ModuleReader<'a> {
567588
exports: self.exports,
568589
elements: self.elements,
569590
memory_types: self.memory_types,
591+
skip_local_memory_allocation,
570592
tags: self.tags,
571593
}
572594
.into())

0 commit comments

Comments
 (0)