Skip to content

Commit

Permalink
Enable copy-on-write memories for components
Browse files Browse the repository at this point in the history
This commit fixes a mistake in component compilation where copy-on-write
and lazy initialization of function tables was accidentally not
performed. I believe this is an accidental regression from a previous
refactor, and this commit now ensures that the shared infrastructure
between components and core modules accounts for copy-on-write and lazy
table initialization.
  • Loading branch information
alexcrichton committed Nov 2, 2023
1 parent e0bfa73 commit 738fe07
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
24 changes: 20 additions & 4 deletions crates/wasmtime/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::collections::{btree_map, BTreeMap, BTreeSet};
use std::{any::Any, collections::HashMap};
use wasmtime_environ::{
Compiler, DefinedFuncIndex, FuncIndex, FunctionBodyData, ModuleTranslation, ModuleType,
ModuleTypes, PrimaryMap, SignatureIndex, StaticModuleIndex, Tunables, WasmFunctionInfo,
ModuleTypes, PrimaryMap, SignatureIndex, StaticModuleIndex, WasmFunctionInfo,
};
use wasmtime_jit::{CompiledFunctionInfo, CompiledModuleInfo};

Expand Down Expand Up @@ -447,8 +447,7 @@ impl FunctionIndices {
pub fn link_and_append_code<'a>(
mut self,
mut obj: object::write::Object<'static>,
tunables: &'a Tunables,
compiler: &dyn Compiler,
engine: &'a Engine,
compiled_funcs: Vec<(String, Box<dyn Any + Send>)>,
translations: PrimaryMap<StaticModuleIndex, ModuleTranslation<'_>>,
) -> Result<(wasmtime_jit::ObjectBuilder<'a>, Artifacts)> {
Expand All @@ -457,6 +456,8 @@ impl FunctionIndices {
// The result is a vector parallel to `compiled_funcs` where
// `symbol_ids_and_locs[i]` is the symbol ID and function location of
// `compiled_funcs[i]`.
let compiler = engine.compiler();
let tunables = &engine.config().tunables;
let symbol_ids_and_locs = compiler.append_code(
&mut obj,
&compiled_funcs,
Expand Down Expand Up @@ -559,7 +560,22 @@ impl FunctionIndices {

artifacts.modules = translations
.into_iter()
.map(|(module, translation)| {
.map(|(module, mut translation)| {
// If configured attempt to use static memory initialization which
// can either at runtime be implemented as a single memcpy to
// initialize memory or otherwise enabling virtual-memory-tricks
// such as mmap'ing from a file to get copy-on-write.
if engine.config().memory_init_cow {
let align = compiler.page_size_align();
let max_always_allowed = engine.config().memory_guaranteed_dense_image_size;
translation.try_static_init(align, max_always_allowed);
}

// Attempt to convert table initializer segments to
// FuncTable representation where possible, to enable
// table lazy init.
translation.try_func_table_init();

let funcs: PrimaryMap<DefinedFuncIndex, CompiledFunctionInfo> =
wasm_functions_for_module(&mut wasm_functions, module)
.map(|(key, wasm_func_index)| {
Expand Down
3 changes: 1 addition & 2 deletions crates/wasmtime/src/component/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ impl Component {

let (mut object, compilation_artifacts) = function_indices.link_and_append_code(
object,
&engine.config().tunables,
compiler,
engine,
compiled_funcs,
module_translations,
)?;
Expand Down
19 changes: 1 addition & 18 deletions crates/wasmtime/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ impl Module {
use crate::compiler::CompileInputs;

let tunables = &engine.config().tunables;
let compiler = engine.compiler();

// First a `ModuleEnvironment` is created which records type information
// about the wasm module. This is where the WebAssembly is parsed and
Expand Down Expand Up @@ -437,25 +436,9 @@ impl Module {
engine.append_compiler_info(&mut object);
engine.append_bti(&mut object);

// If configured attempt to use static memory initialization which
// can either at runtime be implemented as a single memcpy to
// initialize memory or otherwise enabling virtual-memory-tricks
// such as mmap'ing from a file to get copy-on-write.
if engine.config().memory_init_cow {
let align = engine.compiler().page_size_align();
let max_always_allowed = engine.config().memory_guaranteed_dense_image_size;
translation.try_static_init(align, max_always_allowed);
}

// Attempt to convert table initializer segments to
// FuncTable representation where possible, to enable
// table lazy init.
translation.try_func_table_init();

let (mut object, compilation_artifacts) = function_indices.link_and_append_code(
object,
tunables,
compiler,
engine,
compiled_funcs,
std::iter::once(translation).collect(),
)?;
Expand Down

0 comments on commit 738fe07

Please sign in to comment.