Switch over to raw Vec<u8> for compiled funcs.

This commit is contained in:
Chris Fallin 2024-06-21 21:39:06 -07:00 committed by Graham Kelly
parent efa07f16cf
commit eab940024b
2 changed files with 5 additions and 19 deletions

View file

@ -1303,11 +1303,6 @@ pub fn compile(module: &Module<'_>) -> anyhow::Result<wasm_encoder::Module> {
let mut code = wasm_encoder::CodeSection::new(); let mut code = wasm_encoder::CodeSection::new();
enum FuncOrRawBytes<'a> {
Raw(&'a [u8]),
Func(Cow<'a, wasm_encoder::Function>),
}
let bodies = module let bodies = module
.funcs .funcs
.entries() .entries()
@ -1318,16 +1313,14 @@ pub fn compile(module: &Module<'_>) -> anyhow::Result<wasm_encoder::Module> {
match func_decl { match func_decl {
FuncDecl::Lazy(_, _name, reader) => { FuncDecl::Lazy(_, _name, reader) => {
let data = &module.orig_bytes[reader.range()]; let data = &module.orig_bytes[reader.range()];
Ok(FuncOrRawBytes::Raw(data)) Ok(Cow::Borrowed(data))
}
FuncDecl::Compiled(_, _name, encoder) => {
Ok(FuncOrRawBytes::Func(Cow::Borrowed(encoder)))
} }
FuncDecl::Compiled(_, _name, bytes) => Ok(Cow::Borrowed(&bytes[..])),
FuncDecl::Body(_, name, body) => { FuncDecl::Body(_, name, body) => {
log::debug!("Compiling {} \"{}\"", func, name); log::debug!("Compiling {} \"{}\"", func, name);
WasmFuncBackend::new(body)? WasmFuncBackend::new(body)?
.compile() .compile()
.map(|func| FuncOrRawBytes::Func(Cow::Owned(func))) .map(|func| Cow::Owned(func.into_raw_body()))
} }
FuncDecl::Import(_, _) => unreachable!("Should have skipped imports"), FuncDecl::Import(_, _) => unreachable!("Should have skipped imports"),
FuncDecl::None => panic!("FuncDecl::None at compilation time"), FuncDecl::None => panic!("FuncDecl::None at compilation time"),
@ -1336,14 +1329,7 @@ pub fn compile(module: &Module<'_>) -> anyhow::Result<wasm_encoder::Module> {
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
for body in bodies { for body in bodies {
match body { code.raw(&body);
FuncOrRawBytes::Raw(bytes) => {
code.raw(bytes);
}
FuncOrRawBytes::Func(func) => {
code.function(&*func);
}
}
} }
into_mod.section(&code); into_mod.section(&code);

View file

@ -22,7 +22,7 @@ pub enum FuncDecl<'a> {
/// A modified or new function body that requires compilation. /// A modified or new function body that requires compilation.
Body(Signature, String, FunctionBody), Body(Signature, String, FunctionBody),
/// A compiled function body (was IR, has been collapsed back to bytecode). /// A compiled function body (was IR, has been collapsed back to bytecode).
Compiled(Signature, String, wasm_encoder::Function), Compiled(Signature, String, Vec<u8>),
/// A placeholder. /// A placeholder.
#[default] #[default]
None, None,