Refactor the TypeTables type (#3971)

* Remove duplicate `TypeTables` type

This was once needed historically but it is no longer needed.

* Make the internals of `TypeTables` private

Instead of reaching internally for the `wasm_signatures` map an `Index`
implementation now exists to indirect accesses through the type of the
index being accessed. For the component model this table of types will
grow a number of other tables and this'll assist in consuming sites not
having to worry so much about which map they're reaching into.
This commit is contained in:
Alex Crichton
2022-03-30 13:51:25 -05:00
committed by GitHub
parent 5d8dd648d7
commit d1d10dc8da
12 changed files with 43 additions and 42 deletions

View File

@@ -13,9 +13,9 @@ use std::sync::Arc;
use wasmparser::{Parser, ValidPayload, Validator};
use wasmtime_environ::{
DefinedFuncIndex, DefinedMemoryIndex, FunctionInfo, ModuleEnvironment, PrimaryMap,
SignatureIndex,
SignatureIndex, TypeTables,
};
use wasmtime_jit::{CompiledModule, CompiledModuleInfo, TypeTables};
use wasmtime_jit::{CompiledModule, CompiledModuleInfo};
use wasmtime_runtime::{
CompiledModuleId, MemoryImage, MmapVec, ModuleMemoryImages, VMSharedSignatureIndex,
};
@@ -418,13 +418,7 @@ impl Module {
let (mmap, info) =
wasmtime_jit::finish_compile(translation, obj, funcs, trampolines, tunables)?;
Ok((
mmap,
Some(info),
TypeTables {
wasm_signatures: types.wasm_signatures,
},
))
Ok((mmap, Some(info), types))
}
/// Deserializes an in-memory compiled module previously created with
@@ -512,7 +506,7 @@ impl Module {
let signatures = Arc::new(SignatureCollection::new_for_module(
engine.signatures(),
&types.wasm_signatures,
&types,
module.trampolines().map(|(idx, f, _)| (idx, f)),
));

View File

@@ -48,8 +48,8 @@ use std::collections::BTreeMap;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
use wasmtime_environ::{FlagValue, Tunables};
use wasmtime_jit::{subslice_range, CompiledModule, CompiledModuleInfo, TypeTables};
use wasmtime_environ::{FlagValue, Tunables, TypeTables};
use wasmtime_jit::{subslice_range, CompiledModule, CompiledModuleInfo};
use wasmtime_runtime::MmapVec;
const HEADER: &[u8] = b"\0wasmtime-aot";

View File

@@ -6,7 +6,7 @@ use std::{
sync::RwLock,
};
use std::{convert::TryFrom, sync::Arc};
use wasmtime_environ::{PrimaryMap, SignatureIndex, WasmFuncType};
use wasmtime_environ::{PrimaryMap, SignatureIndex, TypeTables, WasmFuncType};
use wasmtime_runtime::{VMSharedSignatureIndex, VMTrampoline};
/// Represents a collection of shared signatures.
@@ -27,14 +27,14 @@ impl SignatureCollection {
/// and trampolines.
pub fn new_for_module(
registry: &SignatureRegistry,
signatures: &PrimaryMap<SignatureIndex, WasmFuncType>,
types: &TypeTables,
trampolines: impl Iterator<Item = (SignatureIndex, VMTrampoline)>,
) -> Self {
let (signatures, trampolines) = registry
.0
.write()
.unwrap()
.register_for_module(signatures, trampolines);
.register_for_module(types, trampolines);
Self {
registry: registry.0.clone(),
@@ -89,7 +89,7 @@ struct SignatureRegistryInner {
impl SignatureRegistryInner {
fn register_for_module(
&mut self,
signatures: &PrimaryMap<SignatureIndex, WasmFuncType>,
types: &TypeTables,
trampolines: impl Iterator<Item = (SignatureIndex, VMTrampoline)>,
) -> (
PrimaryMap<SignatureIndex, VMSharedSignatureIndex>,
@@ -98,8 +98,9 @@ impl SignatureRegistryInner {
let mut sigs = PrimaryMap::default();
let mut map = HashMap::default();
for (_, ty) in signatures.iter() {
sigs.push(self.register(ty));
for (idx, ty) in types.wasm_signatures() {
let b = sigs.push(self.register(ty));
assert_eq!(idx, b);
}
for (index, trampoline) in trampolines {

View File

@@ -1,6 +1,5 @@
use std::fmt;
use wasmtime_environ::{EntityType, Global, Memory, Table, WasmFuncType, WasmType};
use wasmtime_jit::TypeTables;
use wasmtime_environ::{EntityType, Global, Memory, Table, TypeTables, WasmFuncType, WasmType};
pub(crate) mod matching;
@@ -151,9 +150,7 @@ impl ExternType {
pub(crate) fn from_wasmtime(types: &TypeTables, ty: &EntityType) -> ExternType {
match ty {
EntityType::Function(idx) => {
FuncType::from_wasm_func_type(types.wasm_signatures[*idx].clone()).into()
}
EntityType::Function(idx) => FuncType::from_wasm_func_type(types[*idx].clone()).into(),
EntityType::Global(ty) => GlobalType::from_wasmtime_global(ty).into(),
EntityType::Memory(ty) => MemoryType::from_wasmtime_memory(ty).into(),
EntityType::Table(ty) => TableType::from_wasmtime_table(ty).into(),

View File

@@ -2,8 +2,9 @@ use crate::linker::Definition;
use crate::store::StoreOpaque;
use crate::{signatures::SignatureCollection, Engine, Extern};
use anyhow::{bail, Result};
use wasmtime_environ::{EntityType, Global, Memory, SignatureIndex, Table, WasmFuncType, WasmType};
use wasmtime_jit::TypeTables;
use wasmtime_environ::{
EntityType, Global, Memory, SignatureIndex, Table, TypeTables, WasmFuncType, WasmType,
};
use wasmtime_runtime::VMSharedSignatureIndex;
pub struct MatchCx<'a> {
@@ -120,7 +121,7 @@ impl MatchCx<'_> {
return Ok(());
}
let msg = "function types incompatible";
let expected = &self.types.wasm_signatures[expected];
let expected = &self.types[expected];
let actual = match self.engine.signatures().lookup_type(actual) {
Some(ty) => ty,
None => {