Skip to content

Commit 786152d

Browse files
chore: refactor optimizer/add more superinstructions
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent e8f3d33 commit 786152d

File tree

9 files changed

+801
-549
lines changed

9 files changed

+801
-549
lines changed

crates/parser/src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub(crate) mod log {
3232
mod conversion;
3333
mod error;
3434
mod module;
35+
mod optimize;
3536
mod visit;
3637
pub use error::*;
3738
use module::ModuleReader;
@@ -41,14 +42,18 @@ pub use tinywasm_types::TinyWasmModule;
4142

4243
/// Parser optimization and lowering options.
4344
#[non_exhaustive]
44-
#[derive(Debug, Clone, Default)]
45+
#[derive(Debug, Clone)]
4546
pub struct ParserOptions {
46-
// /// Enable control-flow graph cleanup rewrites.
47-
// pub cfg_cleanup: bool,
48-
// /// Enable dead-code pruning.
49-
// pub dce: bool,
50-
// /// Enable return-call rewrites when safe.
51-
// pub tailcall_rewrite: bool,
47+
/// Enable post-lowering DCE pass.
48+
/// Should be enabled by default, since the parser performs some optimizations that can result in dead code.
49+
/// Disabling this may result in larger modules, but faster parsing time.
50+
pub dce: bool,
51+
}
52+
53+
impl Default for ParserOptions {
54+
fn default() -> Self {
55+
Self { dce: true }
56+
}
5257
}
5358

5459
/// A WebAssembly parser
@@ -133,7 +138,7 @@ impl Parser {
133138
return Err(ParseError::EndNotReached);
134139
}
135140

136-
reader.into_module()
141+
reader.into_module(&self.options)
137142
}
138143

139144
#[cfg(feature = "std")]
@@ -173,7 +178,7 @@ impl Parser {
173178
reader.process_payload(payload, &mut validator)?;
174179
buffer.drain(..consumed);
175180
if eof || reader.end_reached {
176-
return reader.into_module();
181+
return reader.into_module(&self.options);
177182
}
178183
}
179184
};
@@ -185,6 +190,6 @@ impl TryFrom<ModuleReader> for TinyWasmModule {
185190
type Error = ParseError;
186191

187192
fn try_from(reader: ModuleReader) -> Result<Self> {
188-
reader.into_module()
193+
reader.into_module(&ParserOptions::default())
189194
}
190195
}

crates/parser/src/module.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use crate::log::debug;
2-
use crate::{ParseError, Result, conversion};
2+
use crate::{ParseError, ParserOptions, Result, conversion, optimize};
33
use alloc::string::ToString;
4-
use alloc::sync::Arc;
54
use alloc::{format, vec::Vec};
65
use tinywasm_types::{
76
ArcSlice, Data, Element, Export, FuncType, Global, Import, ImportKind, Instruction, MemoryType, TableType,
87
TinyWasmModule, ValueCounts, ValueCountsSmall, WasmFunction, WasmFunctionData,
98
};
109
use wasmparser::{FuncValidatorAllocations, Payload, Validator};
1110

12-
pub(crate) type Code = (Arc<[Instruction]>, WasmFunctionData, ValueCounts);
11+
pub(crate) type Code = (Vec<Instruction>, WasmFunctionData, ValueCounts);
1312

1413
#[derive(Default)]
1514
pub(crate) struct ModuleReader {
@@ -31,16 +30,6 @@ pub(crate) struct ModuleReader {
3130
}
3231

3332
impl ModuleReader {
34-
fn apply_instruction_rewrites(instructions: &mut [Instruction], self_func_addr: u32) {
35-
for instr in instructions.iter_mut() {
36-
if matches!(instr, Instruction::Call(addr) if *addr == self_func_addr) {
37-
*instr = Instruction::CallSelf;
38-
} else if matches!(instr, Instruction::ReturnCall(addr) if *addr == self_func_addr) {
39-
*instr = Instruction::ReturnCallSelf;
40-
}
41-
}
42-
}
43-
4433
pub(crate) fn new() -> Self {
4534
Self::default()
4635
}
@@ -190,7 +179,7 @@ impl ModuleReader {
190179
Ok(())
191180
}
192181

193-
pub(crate) fn into_module(self) -> Result<TinyWasmModule> {
182+
pub(crate) fn into_module(self, options: &ParserOptions) -> Result<TinyWasmModule> {
194183
if !self.end_reached {
195184
return Err(ParseError::EndNotReached);
196185
}
@@ -217,8 +206,7 @@ impl ModuleReader {
217206
cref: u16::try_from(locals.cref).unwrap_or_else(|_| unreachable!("local count exceeds u16")),
218207
};
219208
let self_func_addr = imported_func_count + func_idx as u32;
220-
let mut instructions = instructions.to_vec();
221-
Self::apply_instruction_rewrites(&mut instructions, self_func_addr);
209+
let instructions = optimize::optimize_instructions(instructions, self_func_addr, options);
222210

223211
WasmFunction { instructions: ArcSlice::from(instructions), data, locals, params, ty }
224212
})

0 commit comments

Comments
 (0)