Skip to content

Commit

Permalink
Shuffle some items around in wasmtime (#7839)
Browse files Browse the repository at this point in the history
* Shuffle some items around in `wasmtime`

This is a follow-up to #7766 with some more changes and reorganization.
These are some small items here and there which shouldn't have any
actual impact on functionality but are intended to reorganize a bit.
Changes here include:

* Move component artifact definitions to `wasmtime-environ` as core
  module ones already live there.
* Rename the module with module artifacts from `instantiate` to
  `module_artifacts`.
* Make `wasmtime-jit-icache-coherence` an optional dependency as only
  the `runtime` feature requires it.
* Reorganize serialized metadata for wasmtime ELF files to consolidate
  everything back into `wasmtime::engine::serialization`. This is to
  prevent the definition of the serialization format being spread across
  a few files.
* Touching up the `serialization` module to compile in all builds of the
  `wasmtime` crate.

* fix docs typo

---------

Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
  • Loading branch information
alexcrichton and fitzgen authored Jan 29, 2024
1 parent b5057d8 commit f0c9e9b
Show file tree
Hide file tree
Showing 13 changed files with 736 additions and 757 deletions.
2 changes: 2 additions & 0 deletions crates/environ/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ pub const MAX_FLAT_PARAMS: usize = 16;
/// are transferred through memory.
pub const MAX_FLAT_RESULTS: usize = 1;

mod artifacts;
mod compiler;
pub mod dfg;
mod info;
mod translate;
mod types;
mod vmcomponent_offsets;
pub use self::artifacts::*;
pub use self::compiler::*;
pub use self::info::*;
pub use self::translate::*;
Expand Down
45 changes: 45 additions & 0 deletions crates/environ/src/component/artifacts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! Definitions of compilation artifacts of the component compilation process
//! which are serialized with `bincode` into output ELF files.

use crate::{
component::{AllCallFunc, Component, ComponentTypes, TrampolineIndex, TypeComponentIndex},
CompiledModuleInfo, FunctionLoc, PrimaryMap, StaticModuleIndex,
};
use serde_derive::{Deserialize, Serialize};

/// Serializable state that's stored in a compilation artifact.
#[derive(Serialize, Deserialize)]
pub struct ComponentArtifacts {
/// The type of this component.
pub ty: TypeComponentIndex,
/// Information all kept available at runtime as-is.
pub info: CompiledComponentInfo,
/// Type information for this component and all contained modules.
pub types: ComponentTypes,
/// Serialized metadata about all included core wasm modules.
pub static_modules: PrimaryMap<StaticModuleIndex, CompiledModuleInfo>,
}

/// Runtime state that a component retains to support its operation.
#[derive(Serialize, Deserialize)]
pub struct CompiledComponentInfo {
/// Type information calculated during translation about this component.
pub component: Component,

/// Where lowered function trampolines are located within the `text`
/// section of `code_memory`.
///
/// These are the
///
/// 1. Wasm-call,
/// 2. array-call, and
/// 3. native-call
///
/// function pointers that end up in a `VMFuncRef` for each
/// lowering.
pub trampolines: PrimaryMap<TrampolineIndex, AllCallFunc<FunctionLoc>>,

/// The location of the wasm-to-native trampoline for the `resource.drop`
/// intrinsic.
pub resource_drop_wasm_to_native_trampoline: Option<FunctionLoc>,
}
4 changes: 2 additions & 2 deletions crates/environ/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod address_map;
mod builtin;
mod compilation;
mod demangling;
mod instantiate;
mod module;
mod module_artifacts;
mod module_environ;
mod module_types;
pub mod obj;
Expand All @@ -37,7 +37,7 @@ pub use crate::tunables::Tunables;
pub use crate::vmoffsets::*;
pub use object;

pub use crate::instantiate::{
pub use crate::module_artifacts::{
CompiledFunctionInfo, CompiledModuleInfo, FinishedObject, FunctionName, Metadata, ObjectBuilder,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Define the `instantiate` function, which takes a byte array containing an
//! encoded wasm module and returns a live wasm instance. Also, define
//! `CompiledModule` to allow compiling and instantiating to be done as separate
//! steps.
//! Definitions of runtime structures and metadata which are serialized into ELF
//! with `bincode` as part of a module's compilation process.

use crate::{
obj, DefinedFuncIndex, FuncIndex, FunctionLoc, MemoryInitialization, Module, ModuleTranslation,
Expand Down
5 changes: 3 additions & 2 deletions crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ wasmtime-environ = { workspace = true }
wasmtime-jit-debug = { workspace = true, features = [
"perf_jitdump",
], optional = true }
wasmtime-jit-icache-coherence = { workspace = true }
wasmtime-jit-icache-coherence = { workspace = true, optional = true }
wasmtime-cache = { workspace = true, optional = true }
wasmtime-fiber = { workspace = true, optional = true }
wasmtime-cranelift = { workspace = true, optional = true }
Expand Down Expand Up @@ -97,6 +97,7 @@ default = [
'coredump',
'debug-builtins',
'runtime',
'component-model',
]

# An on-by-default feature enabling runtime compilation of WebAssembly modules
Expand Down Expand Up @@ -171,4 +172,4 @@ coredump = ["dep:wasm-encoder", "runtime"]
debug-builtins = ["wasmtime-runtime?/debug-builtins"]

# Enable support for executing compiled Wasm modules.
runtime = ["dep:wasmtime-runtime"]
runtime = ["dep:wasmtime-runtime", "dep:wasmtime-jit-icache-coherence"]
47 changes: 5 additions & 42 deletions crates/wasmtime/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,22 @@
//! functions. It is up to the caller to serialize the relevant parts of the
//! `Artifacts` into the ELF file.

use crate::Engine;
use anyhow::{Context, Result};
use std::{
any::Any,
collections::{btree_map, BTreeMap, BTreeSet, HashMap},
mem,
};

use anyhow::{Context, Result};
use object::{
write::{Object, StandardSegment},
SectionKind,
};
#[cfg(feature = "component-model")]
use wasmtime_environ::component::Translator;
use wasmtime_environ::{
obj, CompiledFunctionInfo, CompiledModuleInfo, Compiler, DefinedFuncIndex, FinishedObject,
CompiledFunctionInfo, CompiledModuleInfo, Compiler, DefinedFuncIndex, FinishedObject,
FuncIndex, FunctionBodyData, ModuleEnvironment, ModuleInternedTypeIndex, ModuleTranslation,
ModuleType, ModuleTypes, ModuleTypesBuilder, ObjectKind, PrimaryMap, StaticModuleIndex,
WasmFunctionInfo,
};

#[cfg(feature = "component-model")]
use crate::component_artifacts::{CompiledComponentInfo, ComponentArtifacts};
use crate::{Engine, Metadata, ModuleVersionStrategy, VERSION};

/// Converts an input binary-encoded WebAssembly module to compilation
/// artifacts and type information.
///
Expand Down Expand Up @@ -121,7 +113,8 @@ pub(crate) fn build_artifacts<T: FinishedObject>(
pub(crate) fn build_component_artifacts<T: FinishedObject>(
engine: &Engine,
binary: &[u8],
) -> Result<(T, ComponentArtifacts)> {
) -> Result<(T, wasmtime_environ::component::ComponentArtifacts)> {
use wasmtime_environ::component::{CompiledComponentInfo, ComponentArtifacts};
use wasmtime_environ::ScopeVec;

let tunables = &engine.config().tunables;
Expand Down Expand Up @@ -189,36 +182,6 @@ pub(crate) fn build_component_artifacts<T: FinishedObject>(
Ok((result, artifacts))
}

/// Produces a blob of bytes by serializing the `engine`'s configuration data to
/// be checked, perhaps in a different process, with the `check_compatible`
/// method below.
///
/// The blob of bytes is inserted into the object file specified to become part
/// of the final compiled artifact.
pub(crate) fn append_compiler_info(engine: &Engine, obj: &mut Object<'_>) {
let section = obj.add_section(
obj.segment_name(StandardSegment::Data).to_vec(),
obj::ELF_WASM_ENGINE.as_bytes().to_vec(),
SectionKind::ReadOnlyData,
);
let mut data = Vec::new();
data.push(VERSION);
let version = match &engine.config().module_version {
ModuleVersionStrategy::WasmtimeVersion => env!("CARGO_PKG_VERSION"),
ModuleVersionStrategy::Custom(c) => c,
ModuleVersionStrategy::None => "",
};
// This precondition is checked in Config::module_version:
assert!(
version.len() < 256,
"package version must be less than 256 bytes"
);
data.push(version.len() as u8);
data.extend_from_slice(version.as_bytes());
bincode::serialize_into(&mut data, &Metadata::new(engine)).unwrap();
obj.set_section_data(section, data, 1);
}

type CompileInput<'a> = Box<dyn FnOnce(&dyn Compiler) -> Result<CompileOutput> + Send + 'a>;

/// A sortable, comparable key for a compilation output.
Expand Down
38 changes: 0 additions & 38 deletions crates/wasmtime/src/component_artifacts.rs

This file was deleted.

Loading

0 comments on commit f0c9e9b

Please sign in to comment.