Skip to content

Commit

Permalink
Merge pull request bytecodealliance#83 from dhil/wasmfx-merge
Browse files Browse the repository at this point in the history
This patch merges in the changes to the type structure in upstream. The changes are mostly alpha conversions, though, now there is a separate type for a module interned type index.
  • Loading branch information
dhil committed Jan 29, 2024
2 parents 058476b + 74390a4 commit 1ef4c1a
Show file tree
Hide file tree
Showing 41 changed files with 1,291 additions and 511 deletions.
7 changes: 4 additions & 3 deletions cranelift/wasm/src/environ/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use crate::state::FuncTranslationState;
use crate::{
DataIndex, ElemIndex, FuncIndex, Global, GlobalIndex, GlobalInit, Heap, HeapData, Memory,
MemoryIndex, SignatureIndex, Table, TableIndex, Tag, TagIndex, TypeConvert, TypeIndex,
WasmContType, WasmError, WasmFuncType, WasmHeapType, WasmResult,
MemoryIndex, Table, TableIndex, Tag, TagIndex, TypeConvert, TypeIndex, WasmContType, WasmError,
WasmFuncType, WasmHeapType, WasmResult,
};
use core::convert::From;
use cranelift_codegen::cursor::FuncCursor;
Expand All @@ -23,6 +23,7 @@ use std::boxed::Box;
use std::string::ToString;
use std::vec::Vec;
use wasmparser::{FuncValidator, FunctionBody, Operator, ValidatorResources, WasmFeatures};
use wasmtime_types::ModuleInternedTypeIndex;

/// The value of a WebAssembly global variable.
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -848,7 +849,7 @@ pub trait ModuleEnvironment<'data>: TypeConvert {

/// Translates a type index to its signature index, only called for type
/// indices which point to functions.
fn type_to_signature(&self, index: TypeIndex) -> WasmResult<SignatureIndex> {
fn type_to_signature(&self, index: TypeIndex) -> WasmResult<ModuleInternedTypeIndex> {
let _ = index;
Err(WasmError::Unsupported("module linking".to_string()))
}
Expand Down
5 changes: 3 additions & 2 deletions crates/cranelift/src/compiler/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use anyhow::Result;
use cranelift_codegen::ir::{self, InstBuilder, MemFlags};
use cranelift_codegen::isa::{CallConv, TargetIsa};
use cranelift_frontend::FunctionBuilder;
use cranelift_wasm::ModuleInternedTypeIndex;
use std::any::Any;
use wasmtime_cranelift_shared::{ALWAYS_TRAP_CODE, CANNOT_ENTER_CODE};
use wasmtime_environ::component::*;
use wasmtime_environ::{PtrSize, SignatureIndex, WasmType};
use wasmtime_environ::{PtrSize, WasmType};

struct TrampolineCompiler<'a> {
compiler: &'a Compiler,
Expand All @@ -19,7 +20,7 @@ struct TrampolineCompiler<'a> {
offsets: VMComponentOffsets<u8>,
abi: Abi,
block0: ir::Block,
signature: SignatureIndex,
signature: ModuleInternedTypeIndex,
}

#[derive(Copy, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions crates/cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ impl<'a, 'func, 'module_env> Call<'a, 'func, 'module_env> {
// If necessary, check the signature.
match self.env.module.table_plans[table_index].style {
TableStyle::CallerChecksSignature => {
let sig_id_size = self.env.offsets.size_of_vmshared_signature_index();
let sig_id_size = self.env.offsets.size_of_vmshared_type_index();
let sig_id_type = Type::int(u16::from(sig_id_size) * 8).unwrap();
let vmctx = self.env.vmctx(self.builder.func);
let base = self.builder.ins().global_value(pointer_type, vmctx);
Expand All @@ -1873,7 +1873,7 @@ impl<'a, 'func, 'module_env> Call<'a, 'func, 'module_env> {
pointer_type,
mem_flags,
base,
i32::try_from(self.env.offsets.vmctx_signature_ids_array()).unwrap(),
i32::try_from(self.env.offsets.vmctx_type_ids_array()).unwrap(),
);
let sig_index = self.env.module.types[ty_index].unwrap_function();
let offset =
Expand Down
30 changes: 21 additions & 9 deletions crates/environ/src/component/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
//! fused adapters, what arguments make their way to core wasm modules, etc.

use crate::component::*;
use crate::{EntityIndex, EntityRef, PrimaryMap, SignatureIndex, WasmType};
use crate::{EntityIndex, EntityRef, PrimaryMap, WasmType};
use indexmap::IndexMap;
use std::collections::HashMap;
use std::hash::Hash;
use std::ops::Index;
use wasmtime_types::ModuleInternedTypeIndex;

#[derive(Default)]
#[allow(missing_docs)]
Expand All @@ -48,7 +49,7 @@ pub struct ComponentDfg {

/// All trampolines and their type signature which will need to get
/// compiled by Cranelift.
pub trampolines: Intern<TrampolineIndex, (SignatureIndex, Trampoline)>,
pub trampolines: Intern<TrampolineIndex, (ModuleInternedTypeIndex, Trampoline)>,

/// Know reallocation functions which are used by `lowerings` (e.g. will be
/// used by the host)
Expand Down Expand Up @@ -184,8 +185,14 @@ pub enum Export {
options: CanonicalOptions,
},
ModuleStatic(StaticModuleIndex),
ModuleImport(RuntimeImportIndex),
Instance(IndexMap<String, Export>),
ModuleImport {
ty: TypeModuleIndex,
import: RuntimeImportIndex,
},
Instance {
ty: Option<TypeComponentInstanceIndex>,
exports: IndexMap<String, Export>,
},
Type(TypeDef),
}

Expand Down Expand Up @@ -407,7 +414,7 @@ impl ComponentDfg {
struct LinearizeDfg<'a> {
dfg: &'a ComponentDfg,
initializers: Vec<GlobalInitializer>,
trampolines: PrimaryMap<TrampolineIndex, SignatureIndex>,
trampolines: PrimaryMap<TrampolineIndex, ModuleInternedTypeIndex>,
trampoline_defs: PrimaryMap<TrampolineIndex, info::Trampoline>,
trampoline_map: HashMap<TrampolineIndex, TrampolineIndex>,
runtime_memories: HashMap<MemoryId, RuntimeMemoryIndex>,
Expand Down Expand Up @@ -487,12 +494,17 @@ impl LinearizeDfg<'_> {
}
}
Export::ModuleStatic(i) => info::Export::ModuleStatic(*i),
Export::ModuleImport(i) => info::Export::ModuleImport(*i),
Export::Instance(map) => info::Export::Instance(
map.iter()
Export::ModuleImport { ty, import } => info::Export::ModuleImport {
ty: *ty,
import: *import,
},
Export::Instance { ty, exports } => info::Export::Instance {
ty: *ty,
exports: exports
.iter()
.map(|(name, export)| (name.clone(), self.export(export)))
.collect(),
),
},
Export::Type(def) => info::Export::Type(*def),
}
}
Expand Down
19 changes: 15 additions & 4 deletions crates/environ/src/component/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
// requirements of embeddings change over time.

use crate::component::*;
use crate::{EntityIndex, PrimaryMap, SignatureIndex, WasmType};
use crate::{EntityIndex, PrimaryMap, WasmType};
use indexmap::IndexMap;
use serde_derive::{Deserialize, Serialize};
use wasmtime_types::ModuleInternedTypeIndex;

/// Metadata as a result of compiling a component.
pub struct ComponentTranslation {
Expand Down Expand Up @@ -146,7 +147,7 @@ pub struct Component {
pub num_runtime_post_returns: u32,

/// WebAssembly type signature of all trampolines.
pub trampolines: PrimaryMap<TrampolineIndex, SignatureIndex>,
pub trampolines: PrimaryMap<TrampolineIndex, ModuleInternedTypeIndex>,

/// The number of lowered host functions (maximum `LoweredIndex`) needed to
/// instantiate this component.
Expand Down Expand Up @@ -402,10 +403,20 @@ pub enum Export {
/// A module defined within this component is exported.
ModuleStatic(StaticModuleIndex),
/// A module imported into this component is exported.
ModuleImport(RuntimeImportIndex),
ModuleImport {
/// Module type index
ty: TypeModuleIndex,
/// Module runtime import index
import: RuntimeImportIndex,
},
/// A nested instance is being exported which has recursively defined
/// `Export` items.
Instance(IndexMap<String, Export>),
Instance {
/// Instance type index, if such is assigned
ty: Option<TypeComponentInstanceIndex>,
/// Instance export map
exports: IndexMap<String, Export>,
},
/// An exported type from a component or instance, currently only
/// informational.
Type(TypeDef),
Expand Down
26 changes: 14 additions & 12 deletions crates/environ/src/component/translate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::component::*;
use crate::ScopeVec;
use crate::{
EntityIndex, ModuleEnvironment, ModuleTranslation, ModuleTypesBuilder, PrimaryMap,
SignatureIndex, Tunables, TypeConvert, WasmHeapType, WasmType,
EntityIndex, ModuleEnvironment, ModuleTranslation, ModuleTypesBuilder, PrimaryMap, Tunables,
TypeConvert, WasmHeapType, WasmType,
};
use anyhow::{bail, Result};
use indexmap::IndexMap;
Expand All @@ -12,6 +12,7 @@ use wasmparser::types::{
AliasableResourceId, ComponentEntityType, ComponentFuncTypeId, ComponentInstanceTypeId, Types,
};
use wasmparser::{Chunk, ComponentImportName, Encoding, Parser, Payload, Validator};
use wasmtime_types::ModuleInternedTypeIndex;

mod adapt;
pub use self::adapt::*;
Expand Down Expand Up @@ -173,16 +174,16 @@ enum LocalInitializer<'data> {
Lower {
func: ComponentFuncIndex,
lower_ty: ComponentFuncTypeId,
canonical_abi: SignatureIndex,
canonical_abi: ModuleInternedTypeIndex,
options: LocalCanonicalOptions,
},
Lift(ComponentFuncTypeId, FuncIndex, LocalCanonicalOptions),

// resources
Resource(AliasableResourceId, WasmType, Option<FuncIndex>),
ResourceNew(AliasableResourceId, SignatureIndex),
ResourceRep(AliasableResourceId, SignatureIndex),
ResourceDrop(AliasableResourceId, SignatureIndex),
ResourceNew(AliasableResourceId, ModuleInternedTypeIndex),
ResourceRep(AliasableResourceId, ModuleInternedTypeIndex),
ResourceDrop(AliasableResourceId, ModuleInternedTypeIndex),

// core wasm modules
ModuleStatic(StaticModuleIndex),
Expand All @@ -200,7 +201,7 @@ enum LocalInitializer<'data> {
HashMap<&'data str, ComponentItem>,
ComponentInstanceTypeId,
),
ComponentSynthetic(HashMap<&'data str, ComponentItem>),
ComponentSynthetic(HashMap<&'data str, ComponentItem>, ComponentInstanceTypeId),

// alias section
AliasExportFunc(ModuleInstanceIndex, &'data str),
Expand Down Expand Up @@ -581,18 +582,18 @@ impl<'a, 'data> Translator<'a, 'data> {
let mut index = self.validator.types(0).unwrap().component_instance_count();
self.validator.component_instance_section(&s)?;
for instance in s {
let types = self.validator.types(0).unwrap();
let ty = types.component_instance_at(index);
let init = match instance? {
wasmparser::ComponentInstance::Instantiate {
component_index,
args,
} => {
let types = self.validator.types(0).unwrap();
let ty = types.component_instance_at(index);
let index = ComponentIndex::from_u32(component_index);
self.instantiate_component(index, &args, ty)?
}
wasmparser::ComponentInstance::FromExports(exports) => {
self.instantiate_component_from_exports(&exports)?
self.instantiate_component_from_exports(&exports, ty)?
}
};
self.result.initializers.push(init);
Expand Down Expand Up @@ -746,14 +747,15 @@ impl<'a, 'data> Translator<'a, 'data> {
fn instantiate_component_from_exports(
&mut self,
exports: &[wasmparser::ComponentExport<'data>],
ty: ComponentInstanceTypeId,
) -> Result<LocalInitializer<'data>> {
let mut map = HashMap::with_capacity(exports.len());
for export in exports {
let idx = self.kind_to_item(export.kind, export.index)?;
map.insert(export.name.0, idx);
}

Ok(LocalInitializer::ComponentSynthetic(map))
Ok(LocalInitializer::ComponentSynthetic(map, ty))
}

fn kind_to_item(
Expand Down Expand Up @@ -889,7 +891,7 @@ impl<'a, 'data> Translator<'a, 'data> {
return ret;
}

fn core_func_signature(&mut self, idx: u32) -> SignatureIndex {
fn core_func_signature(&mut self, idx: u32) -> ModuleInternedTypeIndex {
let types = self.validator.types(0).unwrap();
let id = types.core_function_at(idx);
let ty = types[id].unwrap_func();
Expand Down
38 changes: 25 additions & 13 deletions crates/environ/src/component/translate/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ enum ComponentInstanceDef<'a> {
//
// FIXME: same as the issue on `ComponentClosure` where this is cloned a lot
// and may need `Rc`.
Items(IndexMap<&'a str, ComponentItemDef<'a>>),
Items(
IndexMap<&'a str, ComponentItemDef<'a>>,
Option<TypeComponentInstanceIndex>,
),
}

#[derive(Clone)]
Expand Down Expand Up @@ -397,7 +400,7 @@ impl<'a> Inliner<'a> {
match frames.last_mut() {
Some((parent, _)) => {
parent.finish_instantiate(
ComponentInstanceDef::Items(exports),
ComponentInstanceDef::Items(exports, None),
instance_ty.unwrap(),
types,
);
Expand Down Expand Up @@ -783,14 +786,16 @@ impl<'a> Inliner<'a> {
return Ok(Some(frame));
}

ComponentSynthetic(map) => {
ComponentSynthetic(map, ty) => {
let items = map
.iter()
.map(|(name, index)| Ok((*name, frame.item(*index, types)?)))
.collect::<Result<_>>()?;
let types_ref = frame.translation.types_ref();
let ty = types.convert_instance(types_ref, *ty)?;
frame
.component_instances
.push(ComponentInstanceDef::Items(items));
.push(ComponentInstanceDef::Items(items, Some(ty)));
}

// Core wasm aliases, this and the cases below, are creating
Expand Down Expand Up @@ -846,7 +851,7 @@ impl<'a> Inliner<'a> {
// through instantiation of a component or through a
// synthetic renaming of items we just schlep around the
// definitions of various items here.
ComponentInstanceDef::Items(map) => frame.push_item(map[*name].clone()),
ComponentInstanceDef::Items(map, _) => frame.push_item(map[*name].clone()),
}
}

Expand Down Expand Up @@ -1029,7 +1034,10 @@ impl<'a> Inliner<'a> {
// from.
ComponentItemDef::Module(module) => match module {
ModuleDef::Static(idx) => dfg::Export::ModuleStatic(idx),
ModuleDef::Import(path, _) => dfg::Export::ModuleImport(self.runtime_import(&path)),
ModuleDef::Import(path, ty) => dfg::Export::ModuleImport {
ty,
import: self.runtime_import(&path),
},
},

ComponentItemDef::Func(func) => match func {
Expand All @@ -1051,7 +1059,7 @@ impl<'a> Inliner<'a> {
},

ComponentItemDef::Instance(instance) => {
let mut result = IndexMap::new();
let mut exports = IndexMap::new();
match instance {
// If this instance is one that was originally imported by
// the component itself then the imports are translated here
Expand All @@ -1064,20 +1072,24 @@ impl<'a> Inliner<'a> {
for (name, ty) in types[ty].exports.iter() {
let path = path.push(name);
let def = ComponentItemDef::from_import(path, *ty)?;
self.record_export(name, def, types, &mut result)?;
self.record_export(name, def, types, &mut exports)?;
}
dfg::Export::Instance {
ty: Some(ty),
exports,
}
}

// An exported instance which is itself a bag of items is
// translated recursively here to our `result` map which is
// translated recursively here to our `exports` map which is
// the bag of items we're exporting.
ComponentInstanceDef::Items(map) => {
ComponentInstanceDef::Items(map, ty) => {
for (name, def) in map {
self.record_export(name, def, types, &mut result)?;
self.record_export(name, def, types, &mut exports)?;
}
dfg::Export::Instance { ty, exports }
}
}
dfg::Export::Instance(result)
}

// FIXME(#4283) should make an official decision on whether this is
Expand Down Expand Up @@ -1292,7 +1304,7 @@ impl<'a> ComponentItemDef<'a> {
cur = match instance {
// If this instance is a "bag of things" then this is as easy as
// looking up the name in the bag of names.
ComponentInstanceDef::Items(names) => names[element].clone(),
ComponentInstanceDef::Items(names, _) => names[element].clone(),

// If, however, this instance is an imported instance then this
// is a further projection within the import with one more path
Expand Down
Loading

0 comments on commit 1ef4c1a

Please sign in to comment.