diff --git a/Cargo.lock b/Cargo.lock index 89e19639e7a3..e2b45e4be12e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2604,7 +2604,7 @@ version = "0.0.0" dependencies = [ "cargo_metadata", "heck", - "wit-component 0.18.0", + "wit-component", ] [[package]] @@ -3067,18 +3067,18 @@ checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" [[package]] name = "wasm-encoder" -version = "0.36.2" +version = "0.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "822b645bf4f2446b949776ffca47e2af60b167209ffb70814ef8779d299cd421" +checksum = "7b09bc5df933a3dabbdb72ae4b6b71be8ae07f58774d5aa41bd20adcd41a235a" dependencies = [ "leb128", ] [[package]] name = "wasm-metadata" -version = "0.10.11" +version = "0.10.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2167ce53b2faa16a92c6cafd4942cff16c9a4fa0c5a5a0a41131ee4e49fc055f" +checksum = "3b4a14bbedb07737809c00843d1f2f88ba0b8950c114283e0387e30b1b6ee558" dependencies = [ "anyhow", "indexmap 2.0.0", @@ -3092,9 +3092,9 @@ dependencies = [ [[package]] name = "wasm-mutate" -version = "0.2.40" +version = "0.2.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49afec9e74f8dd90367c8f9b60440e6b84bdfd46e8ca133dd6a040f296bbc328" +checksum = "140f2555b7c16b89592a1cd2f2626612c8c3cba9ff3224f87b00e063264b4b38" dependencies = [ "egg", "log", @@ -3106,16 +3106,15 @@ dependencies = [ [[package]] name = "wasm-smith" -version = "0.12.23" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "426255ec37d1127b020492d957b1a48c0828e667ad2693d98af942e276cfbd86" +checksum = "8ee085568165cd2886137be8ece587fe2f5cabbc0d63e35d8c8afc7daef2e8c2" dependencies = [ "arbitrary", "flagset", "indexmap 2.0.0", "leb128", "wasm-encoder", - "wasmparser", ] [[package]] @@ -3158,9 +3157,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.116.1" +version = "0.118.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a58e28b80dd8340cb07b8242ae654756161f6fc8d0038123d679b7b99964fa50" +checksum = "ebbb91574de0011ded32b14db12777e7dd5e9ea2f9d7317a1ab51a9495c75924" dependencies = [ "indexmap 2.0.0", "semver", @@ -3177,9 +3176,9 @@ dependencies = [ [[package]] name = "wasmprinter" -version = "0.2.72" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aff4df0cdf1906ec040e97d78c3fc8fd26d3f8d70adaac81f07f80957b63b54" +checksum = "61a7a046e6636d25c06a5df00bdc34e02f9e6e0e8a356d738299b961a6126114" dependencies = [ "anyhow", "wasmparser", @@ -3355,7 +3354,7 @@ dependencies = [ "wasmtime-wasi-nn", "wasmtime-wasi-threads", "wasmtime-wast", - "wast 67.0.1", + "wast 69.0.0", "wat", "windows-sys", ] @@ -3386,7 +3385,7 @@ dependencies = [ "wasmtime", "wasmtime-component-util", "wasmtime-wit-bindgen", - "wit-parser 0.13.0", + "wit-parser", ] [[package]] @@ -3741,7 +3740,7 @@ dependencies = [ "anyhow", "log", "wasmtime", - "wast 67.0.1", + "wast 69.0.0", ] [[package]] @@ -3766,7 +3765,7 @@ dependencies = [ "anyhow", "heck", "indexmap 2.0.0", - "wit-parser 0.13.0", + "wit-parser", ] [[package]] @@ -3784,9 +3783,9 @@ dependencies = [ [[package]] name = "wast" -version = "67.0.1" +version = "69.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a974d82fac092b5227c1663e16514e7a85f32014e22e6fdcb08b71aec9d3fb1e" +checksum = "efa51b5ad1391943d1bfad537e50f28fe938199ee76b115be6bae83802cd5185" dependencies = [ "leb128", "memchr", @@ -3796,11 +3795,11 @@ dependencies = [ [[package]] name = "wat" -version = "1.0.79" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb220934f92f8551144c0003d1bc57a060674c99139f45ed623fbbf6d9262e7" +checksum = "74a4c2488d058326466e086a43f5d4ea448241a8d0975e3eb0642c0828be1eb3" dependencies = [ - "wast 67.0.1", + "wast 69.0.0", ] [[package]] @@ -3941,6 +3940,7 @@ dependencies = [ "similar", "target-lexicon", "toml", + "wasmparser", "wasmtime-environ", "wat", "winch-codegen", @@ -4056,9 +4056,9 @@ dependencies = [ [[package]] name = "wit-bindgen" -version = "0.13.1" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38726c54a5d7c03cac28a2a8de1006cfe40397ddf6def3f836189033a413bc08" +checksum = "d0e367a0bdb6b9f6db939a21749dfcd398b62a23f58afa5c44e7de16289dbeba" dependencies = [ "bitflags 2.4.1", "wit-bindgen-rust-macro", @@ -4066,33 +4066,33 @@ dependencies = [ [[package]] name = "wit-bindgen-core" -version = "0.13.1" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8bf1fddccaff31a1ad57432d8bfb7027a7e552969b6c68d6d8820dcf5c2371f" +checksum = "147bee3fde39f80da448dc971d3357c34f5810605e0ba59345ebe576002371c8" dependencies = [ "anyhow", - "wit-component 0.17.0", - "wit-parser 0.12.2", + "wit-component", + "wit-parser", ] [[package]] name = "wit-bindgen-rust" -version = "0.13.2" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e7200e565124801e01b7b5ddafc559e1da1b2e1bed5364d669cd1d96fb88722" +checksum = "3033046c0f9112a67dc6c1e4281347c8d5f1e01d4cafc01321c259d45e3b8e4f" dependencies = [ "anyhow", "heck", "wasm-metadata", "wit-bindgen-core", - "wit-component 0.17.0", + "wit-component", ] [[package]] name = "wit-bindgen-rust-macro" -version = "0.13.1" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ae33920ad8119fe72cf59eb00f127c0b256a236b9de029a1a10397b1f38bdbd" +checksum = "f191047320b2c8bd05a5d367a28c7fe0ad409d43153efc8881aa775a652ca112" dependencies = [ "anyhow", "proc-macro2", @@ -4100,33 +4100,14 @@ dependencies = [ "syn 2.0.29", "wit-bindgen-core", "wit-bindgen-rust", - "wit-component 0.17.0", -] - -[[package]] -name = "wit-component" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "480cc1a078b305c1b8510f7c455c76cbd008ee49935f3a6c5fd5e937d8d95b1e" -dependencies = [ - "anyhow", - "bitflags 2.4.1", - "indexmap 2.0.0", - "log", - "serde", - "serde_derive", - "serde_json", - "wasm-encoder", - "wasm-metadata", - "wasmparser", - "wit-parser 0.12.2", + "wit-component", ] [[package]] name = "wit-component" -version = "0.18.0" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d0371ac5e57e42991aa53f0d79e53e53484afbf54777a5347605b0b229b9d" +checksum = "5b8a35a2a9992898c9d27f1664001860595a4bc99d32dd3599d547412e17d7e2" dependencies = [ "anyhow", "bitflags 2.4.1", @@ -4138,24 +4119,7 @@ dependencies = [ "wasm-encoder", "wasm-metadata", "wasmparser", - "wit-parser 0.13.0", -] - -[[package]] -name = "wit-parser" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43771ee863a16ec4ecf9da0fc65c3bbd4a1235c8e3da5f094b562894843dfa76" -dependencies = [ - "anyhow", - "id-arena", - "indexmap 2.0.0", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", + "wit-parser", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 3b530c3eec4b..df97dfaec83d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -227,18 +227,18 @@ io-extras = "0.18.0" rustix = "0.38.21" is-terminal = "0.4.0" # wit-bindgen: -wit-bindgen = { version = "0.13.1", default-features = false } +wit-bindgen = { version = "0.15.0", default-features = false } # wasm-tools family: -wasmparser = "0.116.0" -wat = "1.0.79" -wast = "67.0.1" -wasmprinter = "0.2.72" -wasm-encoder = "0.36.2" -wasm-smith = "0.12.23" -wasm-mutate = "0.2.40" +wasmparser = "0.118.0" +wat = "1.0.81" +wast = "69.0.0" +wasmprinter = "0.2.74" +wasm-encoder = "0.38.0" +wasm-smith = "0.13.0" +wasm-mutate = "0.2.42" wit-parser = "0.13.0" -wit-component = "0.18.0" +wit-component = "0.18.2" # Non-Bytecode Alliance maintained dependencies: # -------------------------- diff --git a/cranelift/filetests/src/test_wasm/env.rs b/cranelift/filetests/src/test_wasm/env.rs index efd21a5b897d..fb76f3560654 100644 --- a/cranelift/filetests/src/test_wasm/env.rs +++ b/cranelift/filetests/src/test_wasm/env.rs @@ -236,7 +236,7 @@ impl<'data> ModuleEnvironment<'data> for ModuleEnv { } impl TypeConvert for ModuleEnv { - fn lookup_heap_type(&self, _index: TypeIndex) -> WasmHeapType { + fn lookup_heap_type(&self, _index: wasmparser::UnpackedIndex) -> WasmHeapType { todo!() } } @@ -271,7 +271,7 @@ impl<'a> FuncEnv<'a> { } impl TypeConvert for FuncEnv<'_> { - fn lookup_heap_type(&self, _index: TypeIndex) -> WasmHeapType { + fn lookup_heap_type(&self, _index: wasmparser::UnpackedIndex) -> WasmHeapType { todo!() } } diff --git a/cranelift/wasm/src/environ/dummy.rs b/cranelift/wasm/src/environ/dummy.rs index f27ffc7025e0..a4d46f983d6f 100644 --- a/cranelift/wasm/src/environ/dummy.rs +++ b/cranelift/wasm/src/environ/dummy.rs @@ -248,7 +248,7 @@ impl<'dummy_environment> DummyFuncEnvironment<'dummy_environment> { } impl<'dummy_environment> TypeConvert for DummyFuncEnvironment<'dummy_environment> { - fn lookup_heap_type(&self, _index: TypeIndex) -> WasmHeapType { + fn lookup_heap_type(&self, _index: wasmparser::UnpackedIndex) -> WasmHeapType { unimplemented!() } } @@ -704,7 +704,7 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ } impl TypeConvert for DummyEnvironment { - fn lookup_heap_type(&self, _index: TypeIndex) -> WasmHeapType { + fn lookup_heap_type(&self, _index: wasmparser::UnpackedIndex) -> WasmHeapType { unimplemented!() } } diff --git a/cranelift/wasm/src/translation_utils.rs b/cranelift/wasm/src/translation_utils.rs index 8a6ceb2c73fa..9e8ea744914a 100644 --- a/cranelift/wasm/src/translation_utils.rs +++ b/cranelift/wasm/src/translation_utils.rs @@ -18,22 +18,14 @@ where T: WasmModuleResources, { return Ok(match ty { - wasmparser::BlockType::Empty => { - let params: &'static [wasmparser::ValType] = &[]; - let results: std::vec::Vec = vec![]; - ( - itertools::Either::Left(params.iter().copied()), - itertools::Either::Left(results.into_iter()), - ) - } - wasmparser::BlockType::Type(ty) => { - let params: &'static [wasmparser::ValType] = &[]; - let results: std::vec::Vec = vec![ty.clone()]; - ( - itertools::Either::Left(params.iter().copied()), - itertools::Either::Left(results.into_iter()), - ) - } + wasmparser::BlockType::Empty => ( + itertools::Either::Left(std::iter::empty()), + itertools::Either::Left(None.into_iter()), + ), + wasmparser::BlockType::Type(ty) => ( + itertools::Either::Left(std::iter::empty()), + itertools::Either::Left(Some(ty).into_iter()), + ), wasmparser::BlockType::FuncType(ty_index) => { let ty = validator .resources() diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs index 7a0f038daddf..f594744de775 100644 --- a/crates/cranelift/src/compiler.rs +++ b/crates/cranelift/src/compiler.rs @@ -32,8 +32,8 @@ use wasmparser::{FuncValidatorAllocations, FunctionBody}; use wasmtime_cranelift_shared::{CompiledFunction, ModuleTextBuilder}; use wasmtime_environ::{ AddressMapSection, CacheStore, CompileError, FlagValue, FunctionBodyData, FunctionLoc, - ModuleTranslation, ModuleTypes, PtrSize, StackMapInformation, TrapEncodingBuilder, Tunables, - VMOffsets, WasmFunctionInfo, + ModuleTranslation, ModuleTypesBuilder, PtrSize, StackMapInformation, TrapEncodingBuilder, + Tunables, VMOffsets, WasmFunctionInfo, }; #[cfg(feature = "component-model")] @@ -131,7 +131,7 @@ impl wasmtime_environ::Compiler for Compiler { translation: &ModuleTranslation<'_>, func_index: DefinedFuncIndex, input: FunctionBodyData<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, ) -> Result<(WasmFunctionInfo, Box), CompileError> { let isa = &*self.isa; let module = &translation.module; @@ -240,7 +240,7 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_array_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, def_func_index: DefinedFuncIndex, ) -> Result, CompileError> { let func_index = translation.module.func_index(def_func_index); @@ -308,7 +308,7 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_native_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, def_func_index: DefinedFuncIndex, ) -> Result, CompileError> { let func_index = translation.module.func_index(def_func_index); diff --git a/crates/cranelift/src/compiler/component.rs b/crates/cranelift/src/compiler/component.rs index e914d8a26805..c20582fc0f55 100644 --- a/crates/cranelift/src/compiler/component.rs +++ b/crates/cranelift/src/compiler/component.rs @@ -15,7 +15,7 @@ struct TrampolineCompiler<'a> { isa: &'a (dyn TargetIsa + 'static), builder: FunctionBuilder<'a>, component: &'a Component, - types: &'a ComponentTypes, + types: &'a ComponentTypesBuilder, offsets: VMComponentOffsets, abi: Abi, block0: ir::Block, @@ -34,7 +34,7 @@ impl<'a> TrampolineCompiler<'a> { compiler: &'a Compiler, func_compiler: &'a mut super::FunctionCompiler<'_>, component: &'a Component, - types: &'a ComponentTypes, + types: &'a ComponentTypesBuilder, index: TrampolineIndex, abi: Abi, ) -> TrampolineCompiler<'a> { @@ -625,7 +625,7 @@ impl ComponentCompiler for Compiler { fn compile_trampoline( &self, component: &ComponentTranslation, - types: &ComponentTypes, + types: &ComponentTypesBuilder, index: TrampolineIndex, ) -> Result>> { let compile = |abi: Abi| -> Result<_> { diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs index 0a52276ebee5..7db5c6255f3a 100644 --- a/crates/cranelift/src/func_environ.rs +++ b/crates/cranelift/src/func_environ.rs @@ -21,8 +21,8 @@ use std::convert::TryFrom; use std::mem; use wasmparser::Operator; use wasmtime_environ::{ - BuiltinFunctionIndex, MemoryPlan, MemoryStyle, Module, ModuleTranslation, ModuleTypes, PtrSize, - TableStyle, Tunables, TypeConvert, VMOffsets, WASM_PAGE_SIZE, + BuiltinFunctionIndex, MemoryPlan, MemoryStyle, Module, ModuleTranslation, ModuleTypesBuilder, + PtrSize, TableStyle, Tunables, TypeConvert, VMOffsets, WASM_PAGE_SIZE, }; use wasmtime_environ::{FUNCREF_INIT_BIT, FUNCREF_MASK}; @@ -112,7 +112,7 @@ wasmtime_environ::foreach_builtin_function!(declare_function_signatures); pub struct FuncEnvironment<'module_environment> { isa: &'module_environment (dyn TargetIsa + 'module_environment), module: &'module_environment Module, - types: &'module_environment ModuleTypes, + types: &'module_environment ModuleTypesBuilder, translation: &'module_environment ModuleTranslation<'module_environment>, @@ -169,7 +169,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> { pub fn new( isa: &'module_environment (dyn TargetIsa + 'module_environment), translation: &'module_environment ModuleTranslation<'module_environment>, - types: &'module_environment ModuleTypes, + types: &'module_environment ModuleTypesBuilder, tunables: &'module_environment Tunables, wmemcheck: bool, ) -> Self { @@ -1204,8 +1204,12 @@ impl<'a, 'func, 'module_env> Call<'a, 'func, 'module_env> { } impl TypeConvert for FuncEnvironment<'_> { - fn lookup_heap_type(&self, ty: TypeIndex) -> WasmHeapType { - self.module.lookup_heap_type(ty) + fn lookup_heap_type(&self, ty: wasmparser::UnpackedIndex) -> WasmHeapType { + wasmtime_environ::WasmparserTypeConverter { + module: self.module, + types: self.types, + } + .lookup_heap_type(ty) } } diff --git a/crates/environ/src/compilation.rs b/crates/environ/src/compilation.rs index 3cee62b856ad..a6475f63c90c 100644 --- a/crates/environ/src/compilation.rs +++ b/crates/environ/src/compilation.rs @@ -3,7 +3,7 @@ use crate::{obj, Tunables}; use crate::{ - DefinedFuncIndex, FilePos, FuncIndex, FunctionBodyData, ModuleTranslation, ModuleTypes, + DefinedFuncIndex, FilePos, FuncIndex, FunctionBodyData, ModuleTranslation, ModuleTypesBuilder, PrimaryMap, StackMap, WasmError, WasmFuncType, }; use anyhow::Result; @@ -179,7 +179,7 @@ pub trait Compiler: Send + Sync { translation: &ModuleTranslation<'_>, index: DefinedFuncIndex, data: FunctionBodyData<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, ) -> Result<(WasmFunctionInfo, Box), CompileError>; /// Compile a trampoline for an array-call host function caller calling the @@ -190,7 +190,7 @@ pub trait Compiler: Send + Sync { fn compile_array_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, index: DefinedFuncIndex, ) -> Result, CompileError>; @@ -202,7 +202,7 @@ pub trait Compiler: Send + Sync { fn compile_native_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, index: DefinedFuncIndex, ) -> Result, CompileError>; diff --git a/crates/environ/src/component/compiler.rs b/crates/environ/src/component/compiler.rs index 13b4a3a4a4c7..b89f67a77fc3 100644 --- a/crates/environ/src/component/compiler.rs +++ b/crates/environ/src/component/compiler.rs @@ -1,4 +1,4 @@ -use crate::component::{ComponentTranslation, ComponentTypes, TrampolineIndex}; +use crate::component::{ComponentTranslation, ComponentTypesBuilder, TrampolineIndex}; use anyhow::Result; use serde_derive::{Deserialize, Serialize}; use std::any::Any; @@ -41,7 +41,7 @@ pub trait ComponentCompiler: Send + Sync { fn compile_trampoline( &self, component: &ComponentTranslation, - types: &ComponentTypes, + types: &ComponentTypesBuilder, trampoline: TrampolineIndex, ) -> Result>>; } diff --git a/crates/environ/src/component/translate.rs b/crates/environ/src/component/translate.rs index df404bd5afa1..3eb37bfcc2f6 100644 --- a/crates/environ/src/component/translate.rs +++ b/crates/environ/src/component/translate.rs @@ -894,7 +894,7 @@ impl<'a, 'data> Translator<'a, 'data> { let id = types.core_function_at(idx); let ty = types[id].unwrap_func(); let ty = self.types.convert_func_type(ty); - self.types.module_types_builder().wasm_func_type(ty) + self.types.module_types_builder().wasm_func_type(id, ty) } } @@ -928,7 +928,7 @@ mod pre_inlining { } pub fn module_types_builder(&mut self) -> &mut ModuleTypesBuilder { - self.types.module_types_builder() + self.types.module_types_builder_mut() } pub fn types(&self) -> &ComponentTypesBuilder { @@ -943,7 +943,7 @@ mod pre_inlining { } impl TypeConvert for PreInliningComponentTypes<'_> { - fn lookup_heap_type(&self, index: TypeIndex) -> WasmHeapType { + fn lookup_heap_type(&self, index: wasmparser::UnpackedIndex) -> WasmHeapType { self.types.lookup_heap_type(index) } } diff --git a/crates/environ/src/component/types.rs b/crates/environ/src/component/types.rs index ecf38d4b135b..7c2f2ebe2a70 100644 --- a/crates/environ/src/component/types.rs +++ b/crates/environ/src/component/types.rs @@ -294,25 +294,6 @@ impl ComponentTypes { InterfaceType::Result(i) => &self[*i].abi, } } - - /// Smaller helper method to find a `SignatureIndex` which corresponds to - /// the `resource.drop` intrinsic in components, namely a core wasm function - /// type which takes one `i32` argument and has no results. - /// - /// This is a bit of a hack right now as ideally this find operation - /// wouldn't be needed and instead the `SignatureIndex` itself would be - /// threaded through appropriately, but that's left for a future - /// refactoring. Try not to lean too hard on this method though. - pub fn find_resource_drop_signature(&self) -> Option { - self.module_types - .wasm_signatures() - .find(|(_, sig)| { - sig.params().len() == 1 - && sig.returns().len() == 0 - && sig.params()[0] == WasmType::I32 - }) - .map(|(i, _)| i) - } } macro_rules! impl_index { @@ -324,6 +305,14 @@ macro_rules! impl_index { &self.$field[idx] } } + + impl std::ops::Index<$ty> for ComponentTypesBuilder { + type Output = $output; + #[inline] + fn index(&self, idx: $ty) -> &$output { + &self.component_types[idx] + } + } )*) } @@ -355,6 +344,16 @@ where } } +impl Index for ComponentTypesBuilder +where + ModuleTypes: Index, +{ + type Output = >::Output; + fn index(&self, idx: T) -> &Self::Output { + self.module_types.index(idx) + } +} + /// Structured used to build a [`ComponentTypes`] during translation. /// /// This contains tables to intern any component types found as well as @@ -405,16 +404,35 @@ impl ComponentTypesBuilder { self.component_types } - /// Returns the `ComponentTypes`-in-progress. - pub fn component_types(&self) -> &ComponentTypes { - &self.component_types + /// Smaller helper method to find a `SignatureIndex` which corresponds to + /// the `resource.drop` intrinsic in components, namely a core wasm function + /// type which takes one `i32` argument and has no results. + /// + /// This is a bit of a hack right now as ideally this find operation + /// wouldn't be needed and instead the `SignatureIndex` itself would be + /// threaded through appropriately, but that's left for a future + /// refactoring. Try not to lean too hard on this method though. + pub fn find_resource_drop_signature(&self) -> Option { + self.module_types + .wasm_signatures() + .find(|(_, sig)| { + sig.params().len() == 1 + && sig.returns().len() == 0 + && sig.params()[0] == WasmType::I32 + }) + .map(|(i, _)| i) } /// Returns the underlying builder used to build up core wasm module types. /// /// Note that this is shared across all modules found within a component to /// improve the wins from deduplicating function signatures. - pub fn module_types_builder(&mut self) -> &mut ModuleTypesBuilder { + pub fn module_types_builder(&self) -> &ModuleTypesBuilder { + &self.module_types + } + + /// Same as `module_types_builder`, but `mut`. + pub fn module_types_builder_mut(&mut self) -> &mut ModuleTypesBuilder { &mut self.module_types } @@ -586,7 +604,7 @@ impl ComponentTypesBuilder { types::EntityType::Func(idx) => { let ty = types[*idx].unwrap_func(); let ty = self.convert_func_type(ty); - EntityType::Function(self.module_types_builder().wasm_func_type(ty)) + EntityType::Function(self.module_types_builder_mut().wasm_func_type(*idx, ty)) } types::EntityType::Table(ty) => EntityType::Table(self.convert_table_type(ty)), types::EntityType::Memory(ty) => EntityType::Memory(ty.clone().into()), @@ -897,23 +915,11 @@ impl ComponentTypesBuilder { } impl TypeConvert for ComponentTypesBuilder { - fn lookup_heap_type(&self, _index: TypeIndex) -> WasmHeapType { + fn lookup_heap_type(&self, _index: wasmparser::UnpackedIndex) -> WasmHeapType { panic!("heap types are not supported yet") } } -// Forward the indexing impl to the internal `TypeTables` -impl Index for ComponentTypesBuilder -where - ComponentTypes: Index, -{ - type Output = >::Output; - - fn index(&self, sig: T) -> &Self::Output { - &self.component_types[sig] - } -} - fn intern(map: &mut HashMap, list: &mut PrimaryMap, item: T) -> U where T: Hash + Clone + Eq, diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs index 5bce7ae16134..5e1b2281adbb 100644 --- a/crates/environ/src/module.rs +++ b/crates/environ/src/module.rs @@ -1049,14 +1049,6 @@ impl Module { } } -impl TypeConvert for Module { - fn lookup_heap_type(&self, index: TypeIndex) -> WasmHeapType { - match self.types[index] { - ModuleType::Function(i) => WasmHeapType::TypedFunc(i), - } - } -} - /// Type information about functions in a wasm module. #[derive(Debug, Serialize, Deserialize)] pub struct FunctionType { diff --git a/crates/environ/src/module_environ.rs b/crates/environ/src/module_environ.rs index f39a9b608db7..f2f463eaa799 100644 --- a/crates/environ/src/module_environ.rs +++ b/crates/environ/src/module_environ.rs @@ -5,8 +5,8 @@ use crate::module::{ use crate::{ DataIndex, DefinedFuncIndex, ElemIndex, EntityIndex, EntityType, FuncIndex, GlobalIndex, GlobalInit, MemoryIndex, ModuleTypesBuilder, PrimaryMap, SignatureIndex, TableIndex, - TableInitialValue, Tunables, TypeConvert, TypeIndex, Unsigned, WasmError, WasmFuncType, - WasmHeapType, WasmResult, WasmType, + TableInitialValue, Tunables, TypeConvert, TypeIndex, Unsigned, WasmError, WasmHeapType, + WasmResult, WasmType, WasmparserTypeConverter, }; use cranelift_entity::packed_option::ReservedValue; use std::borrow::Cow; @@ -14,10 +14,11 @@ use std::collections::HashMap; use std::convert::TryFrom; use std::path::PathBuf; use std::sync::Arc; +use wasmparser::types::{CoreTypeId, Types}; use wasmparser::{ - types::Types, CustomSectionReader, DataKind, ElementItems, ElementKind, Encoding, ExternalKind, - FuncToValidate, FunctionBody, NameSectionReader, Naming, Operator, Parser, Payload, TypeRef, - Validator, ValidatorResources, + CompositeType, CustomSectionReader, DataKind, ElementItems, ElementKind, Encoding, + ExternalKind, FuncToValidate, FunctionBody, NameSectionReader, Naming, Operator, Parser, + Payload, TypeRef, Validator, ValidatorResources, }; /// Object containing the standalone environment information. @@ -237,9 +238,10 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> { self.result.module.types.reserve(num); self.types.reserve_wasm_signatures(num); - for ty in types.into_iter_err_on_gc_types() { - let ty = self.convert_func_type(&ty?); - self.declare_type_func(ty)?; + for i in 0..types.count() { + let types = self.validator.types(0).unwrap(); + let ty = types.core_type_at(i); + self.declare_type(ty.unwrap_sub())?; } } @@ -791,12 +793,22 @@ and for re-adding support for interface types you can see this issue: self.result.module.num_escaped_funcs += 1; } - fn declare_type_func(&mut self, wasm: WasmFuncType) -> WasmResult<()> { - let sig_index = self.types.wasm_func_type(wasm); - self.result - .module - .types - .push(ModuleType::Function(sig_index)); + fn declare_type(&mut self, id: CoreTypeId) -> WasmResult<()> { + let types = self.validator.types(0).unwrap(); + let ty = &types[id]; + assert!(ty.is_final); + assert!(ty.supertype_idx.is_none()); + match &ty.composite_type { + CompositeType::Func(ty) => { + let wasm = self.convert_func_type(ty); + let sig_index = self.types.wasm_func_type(id, wasm); + self.result + .module + .types + .push(ModuleType::Function(sig_index)); + } + CompositeType::Array(_) | CompositeType::Struct(_) => unimplemented!(), + } Ok(()) } @@ -870,7 +882,11 @@ and for re-adding support for interface types you can see this issue: } impl TypeConvert for ModuleEnvironment<'_, '_> { - fn lookup_heap_type(&self, index: TypeIndex) -> WasmHeapType { - self.result.module.lookup_heap_type(index) + fn lookup_heap_type(&self, index: wasmparser::UnpackedIndex) -> WasmHeapType { + WasmparserTypeConverter { + types: &self.types, + module: &self.result.module, + } + .lookup_heap_type(index) } } diff --git a/crates/environ/src/module_types.rs b/crates/environ/src/module_types.rs index 287cbb667b63..3a6cfc324622 100644 --- a/crates/environ/src/module_types.rs +++ b/crates/environ/src/module_types.rs @@ -1,7 +1,12 @@ -use crate::{PrimaryMap, SignatureIndex, WasmFuncType}; +use crate::{ + Module, ModuleType, PrimaryMap, SignatureIndex, TypeConvert, TypeIndex, WasmFuncType, + WasmHeapType, +}; use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap; use std::ops::Index; +use wasmparser::types::CoreTypeId; +use wasmparser::UnpackedIndex; /// All types used in a core wasm module. /// @@ -38,6 +43,7 @@ impl Index for ModuleTypes { pub struct ModuleTypesBuilder { types: ModuleTypes, interned_func_types: HashMap, + wasmparser_to_wasmtime: HashMap, } impl ModuleTypesBuilder { @@ -49,7 +55,13 @@ impl ModuleTypesBuilder { /// Interns the `sig` specified and returns a unique `SignatureIndex` that /// can be looked up within [`ModuleTypes`] to recover the [`WasmFuncType`] /// at runtime. - pub fn wasm_func_type(&mut self, sig: WasmFuncType) -> SignatureIndex { + pub fn wasm_func_type(&mut self, id: CoreTypeId, sig: WasmFuncType) -> SignatureIndex { + let sig = self.intern_func_type(sig); + self.wasmparser_to_wasmtime.insert(id, sig); + sig + } + + fn intern_func_type(&mut self, sig: WasmFuncType) -> SignatureIndex { if let Some(idx) = self.interned_func_types.get(&sig) { return *idx; } @@ -63,6 +75,12 @@ impl ModuleTypesBuilder { pub fn finish(self) -> ModuleTypes { self.types } + + /// Returns an iterator over all the wasm function signatures found within + /// this module. + pub fn wasm_signatures(&self) -> impl Iterator { + self.types.wasm_signatures() + } } // Forward the indexing impl to the internal `ModuleTypes` @@ -76,3 +94,27 @@ where &self.types[sig] } } + +#[allow(missing_docs)] +pub struct WasmparserTypeConverter<'a> { + pub types: &'a ModuleTypesBuilder, + pub module: &'a Module, +} + +impl TypeConvert for WasmparserTypeConverter<'_> { + fn lookup_heap_type(&self, index: UnpackedIndex) -> WasmHeapType { + match index { + UnpackedIndex::Id(id) => { + let signature = self.types.wasmparser_to_wasmtime[&id]; + WasmHeapType::TypedFunc(signature) + } + UnpackedIndex::RecGroup(_) => unreachable!(), + UnpackedIndex::Module(i) => { + let i = TypeIndex::from_u32(i); + match self.module.types[i] { + ModuleType::Function(sig) => WasmHeapType::TypedFunc(sig), + } + } + } + } +} diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 2719e6df9337..06796ae500ec 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -483,7 +483,7 @@ pub trait TypeConvert { match ty { wasmparser::HeapType::Func => WasmHeapType::Func, wasmparser::HeapType::Extern => WasmHeapType::Extern, - wasmparser::HeapType::Concrete(i) => self.lookup_heap_type(TypeIndex::from_u32(i)), + wasmparser::HeapType::Concrete(i) => self.lookup_heap_type(i), wasmparser::HeapType::Any | wasmparser::HeapType::None @@ -500,5 +500,5 @@ pub trait TypeConvert { /// Converts the specified type index from a heap type into a canonicalized /// heap type. - fn lookup_heap_type(&self, index: TypeIndex) -> WasmHeapType; + fn lookup_heap_type(&self, index: wasmparser::UnpackedIndex) -> WasmHeapType; } diff --git a/crates/wasmtime/src/compiler.rs b/crates/wasmtime/src/compiler.rs index 5022ef9dac0c..8027d91bf18b 100644 --- a/crates/wasmtime/src/compiler.rs +++ b/crates/wasmtime/src/compiler.rs @@ -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, WasmFunctionInfo, + ModuleTypesBuilder, PrimaryMap, SignatureIndex, StaticModuleIndex, WasmFunctionInfo, }; use wasmtime_jit::{CompiledFunctionInfo, CompiledModuleInfo}; @@ -173,7 +173,7 @@ impl<'a> CompileInputs<'a> { /// Create the `CompileInputs` for a core Wasm module. pub fn for_module( - types: &'a ModuleTypes, + types: &'a ModuleTypesBuilder, translation: &'a ModuleTranslation<'a>, functions: PrimaryMap>, ) -> Self { @@ -188,7 +188,7 @@ impl<'a> CompileInputs<'a> { /// Create a `CompileInputs` for a component. #[cfg(feature = "component-model")] pub fn for_component( - types: &'a wasmtime_environ::component::ComponentTypes, + types: &'a wasmtime_environ::component::ComponentTypesBuilder, component: &'a wasmtime_environ::component::ComponentTranslation, module_translations: impl IntoIterator< Item = ( @@ -200,7 +200,7 @@ impl<'a> CompileInputs<'a> { ) -> Self { let mut ret = CompileInputs::default(); - ret.collect_inputs_in_translations(types.module_types(), module_translations); + ret.collect_inputs_in_translations(types.module_types_builder(), module_translations); for (idx, trampoline) in component.trampolines.iter() { ret.push_input(move |compiler| { @@ -241,7 +241,7 @@ impl<'a> CompileInputs<'a> { fn collect_inputs_in_translations( &mut self, - types: &'a ModuleTypes, + types: &'a ModuleTypesBuilder, translations: impl IntoIterator< Item = ( StaticModuleIndex, diff --git a/crates/wasmtime/src/component/component.rs b/crates/wasmtime/src/component/component.rs index a57165ed441d..280b3fdef60e 100644 --- a/crates/wasmtime/src/component/component.rs +++ b/crates/wasmtime/src/component/component.rs @@ -187,7 +187,6 @@ impl Component { Translator::new(tunables, &mut validator, &mut types, &scope) .translate(binary) .context("failed to parse WebAssembly module")?; - let types = types.finish(); let compile_inputs = CompileInputs::for_component( &types, @@ -198,6 +197,7 @@ impl Component { }), ); let unlinked_compile_outputs = compile_inputs.compile(&engine)?; + let types = types.finish(); let (compiled_funcs, function_indices) = unlinked_compile_outputs.pre_link(); let mut object = compiler.object(ObjectKind::Component)?; diff --git a/crates/wasmtime/src/module.rs b/crates/wasmtime/src/module.rs index a5553ff94f9c..e36517307bf0 100644 --- a/crates/wasmtime/src/module.rs +++ b/crates/wasmtime/src/module.rs @@ -415,10 +415,10 @@ impl Module { .translate(parser, wasm) .context("failed to parse WebAssembly module")?; let functions = mem::take(&mut translation.function_body_inputs); - let types = types.finish(); let compile_inputs = CompileInputs::for_module(&types, &translation, functions); let unlinked_compile_outputs = compile_inputs.compile(engine)?; + let types = types.finish(); let (compiled_funcs, function_indices) = unlinked_compile_outputs.pre_link(); // Emplace all compiled functions into the object file with any other diff --git a/crates/winch/src/compiler.rs b/crates/winch/src/compiler.rs index 89e4a775d3c3..bc1191887674 100644 --- a/crates/winch/src/compiler.rs +++ b/crates/winch/src/compiler.rs @@ -7,7 +7,8 @@ use wasmparser::FuncValidatorAllocations; use wasmtime_cranelift_shared::{CompiledFunction, ModuleTextBuilder}; use wasmtime_environ::{ CompileError, DefinedFuncIndex, FilePos, FuncIndex, FunctionBodyData, FunctionLoc, - ModuleTranslation, ModuleTypes, PrimaryMap, TrapEncodingBuilder, VMOffsets, WasmFunctionInfo, + ModuleTranslation, ModuleTypesBuilder, PrimaryMap, TrapEncodingBuilder, VMOffsets, + WasmFunctionInfo, }; use winch_codegen::{BuiltinFunctions, TargetIsa, TrampolineKind}; @@ -70,7 +71,7 @@ impl wasmtime_environ::Compiler for Compiler { translation: &ModuleTranslation<'_>, index: DefinedFuncIndex, data: FunctionBodyData<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, ) -> Result<(WasmFunctionInfo, Box), CompileError> { let index = translation.module.func_index(index); let sig = translation.module.functions[index].signature; @@ -112,7 +113,7 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_array_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, index: DefinedFuncIndex, ) -> Result, CompileError> { let func_index = translation.module.func_index(index); @@ -131,7 +132,7 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_native_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - types: &ModuleTypes, + types: &ModuleTypesBuilder, index: DefinedFuncIndex, ) -> Result, CompileError> { let func_index = translation.module.func_index(index); diff --git a/supply-chain/imports.lock b/supply-chain/imports.lock index 1761b70bec2b..5ce3ee20ebb4 100644 --- a/supply-chain/imports.lock +++ b/supply-chain/imports.lock @@ -1182,6 +1182,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wasm-encoder]] +version = "0.38.0" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wasm-metadata]] version = "0.10.9" when = "2023-10-14" @@ -1203,6 +1210,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wasm-metadata]] +version = "0.10.13" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wasm-mutate]] version = "0.2.38" when = "2023-10-14" @@ -1224,6 +1238,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wasm-mutate]] +version = "0.2.42" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wasm-smith]] version = "0.12.21" when = "2023-10-14" @@ -1245,6 +1266,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wasm-smith]] +version = "0.13.0" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wasmparser]] version = "0.115.0" when = "2023-10-14" @@ -1266,6 +1294,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wasmparser]] +version = "0.118.0" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wasmprinter]] version = "0.2.70" when = "2023-10-14" @@ -1287,6 +1322,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wasmprinter]] +version = "0.2.74" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wasmtime]] version = "14.0.2" when = "2023-10-26" @@ -1608,6 +1650,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wast]] +version = "69.0.0" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wat]] version = "1.0.77" when = "2023-10-14" @@ -1629,6 +1678,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wat]] +version = "1.0.81" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wiggle]] version = "14.0.2" when = "2023-10-26" @@ -1775,6 +1831,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wit-bindgen]] +version = "0.15.0" +when = "2023-11-27" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wit-bindgen-core]] version = "0.13.0" when = "2023-10-18" @@ -1789,6 +1852,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wit-bindgen-core]] +version = "0.15.0" +when = "2023-11-27" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wit-bindgen-rust]] version = "0.13.0" when = "2023-10-18" @@ -1803,6 +1873,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wit-bindgen-rust]] +version = "0.15.0" +when = "2023-11-27" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wit-bindgen-rust-macro]] version = "0.13.0" when = "2023-10-18" @@ -1817,6 +1894,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wit-bindgen-rust-macro]] +version = "0.15.0" +when = "2023-11-27" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wit-component]] version = "0.16.0" when = "2023-10-18" @@ -1838,6 +1922,13 @@ user-id = 1 user-login = "alexcrichton" user-name = "Alex Crichton" +[[publisher.wit-component]] +version = "0.18.2" +when = "2023-11-20" +user-id = 1 +user-login = "alexcrichton" +user-name = "Alex Crichton" + [[publisher.wit-parser]] version = "0.12.1" when = "2023-10-18" diff --git a/winch/codegen/src/codegen/env.rs b/winch/codegen/src/codegen/env.rs index 77be7b924ba2..09293f228cd5 100644 --- a/winch/codegen/src/codegen/env.rs +++ b/winch/codegen/src/codegen/env.rs @@ -9,8 +9,8 @@ use std::collections::{ }; use wasmparser::BlockType; use wasmtime_environ::{ - FuncIndex, GlobalIndex, ModuleTranslation, ModuleTypes, PtrSize, TableIndex, TablePlan, - TypeConvert, TypeIndex, VMOffsets, WasmFuncType, WasmType, + FuncIndex, GlobalIndex, ModuleTranslation, ModuleTypesBuilder, PtrSize, TableIndex, TablePlan, + TypeConvert, TypeIndex, VMOffsets, WasmFuncType, WasmHeapType, WasmType, }; /// Table metadata. @@ -92,7 +92,7 @@ pub struct FuncEnv<'a, 'translation: 'a, 'data: 'translation, P: PtrSize> { /// Metadata about the translation process of a WebAssembly module. pub translation: &'translation ModuleTranslation<'data>, /// The module's function types. - pub types: &'translation ModuleTypes, + pub types: &'translation ModuleTypesBuilder, /// Track resolved table information. resolved_tables: HashMap, } @@ -108,7 +108,7 @@ impl<'a, 'translation, 'data, P: PtrSize> FuncEnv<'a, 'translation, 'data, P> { pub fn new( vmoffsets: &'a VMOffsets

, translation: &'translation ModuleTranslation<'data>, - types: &'translation ModuleTypes, + types: &'translation ModuleTypesBuilder, ) -> Self { Self { vmoffsets, @@ -139,7 +139,7 @@ impl<'a, 'translation, 'data, P: PtrSize> FuncEnv<'a, 'translation, 'data, P> { pub fn callee_from_index(&self, idx: FuncIndex) -> Callee { let types = &self.translation.get_types(); let ty = types[types.core_function_at(idx.as_u32())].unwrap_func(); - let ty = self.translation.module.convert_func_type(ty); + let ty = self.convert_func_type(ty); let import = self.translation.module.is_imported_function(idx); let info = CalleeInfo { ty, index: idx }; @@ -174,7 +174,7 @@ impl<'a, 'translation, 'data, P: PtrSize> FuncEnv<'a, 'translation, 'data, P> { match blockty { Empty => ABIResultsData::wrap(ABIResults::default()), Type(ty) => { - let ty = self.translation.module.convert_valtype(ty); + let ty = self.convert_valtype(ty); let results = ::abi_results(&[ty], &CallingConvention::Default); ABIResultsData::wrap(results) } @@ -239,3 +239,13 @@ impl<'a, 'translation, 'data, P: PtrSize> FuncEnv<'a, 'translation, 'data, P> { &self.translation.module.table_plans[index] } } + +impl TypeConvert for FuncEnv<'_, '_, '_, P> { + fn lookup_heap_type(&self, idx: wasmparser::UnpackedIndex) -> WasmHeapType { + wasmtime_environ::WasmparserTypeConverter { + module: &self.translation.module, + types: self.types, + } + .lookup_heap_type(idx) + } +} diff --git a/winch/codegen/src/frame/mod.rs b/winch/codegen/src/frame/mod.rs index e05a71fe397f..3bd3df0c17b8 100644 --- a/winch/codegen/src/frame/mod.rs +++ b/winch/codegen/src/frame/mod.rs @@ -6,7 +6,7 @@ use anyhow::Result; use smallvec::SmallVec; use std::ops::Range; use wasmparser::{BinaryReader, FuncValidator, ValidatorResources}; -use wasmtime_environ::{ModuleTranslation, TypeConvert, WasmType}; +use wasmtime_environ::{TypeConvert, WasmType}; // TODO: // SpiderMonkey's implementation uses 16; @@ -36,7 +36,7 @@ pub(crate) struct DefinedLocals { impl DefinedLocals { /// Compute the local slots for a Wasm function. pub fn new( - translation: &ModuleTranslation<'_>, + types: &impl TypeConvert, reader: &mut BinaryReader<'_>, validator: &mut FuncValidator, ) -> Result { @@ -51,7 +51,7 @@ impl DefinedLocals { let ty = reader.read()?; validator.define_locals(position, count, ty)?; - let ty = translation.module.convert_valtype(ty); + let ty = types.convert_valtype(ty); for _ in 0..count { let ty_size = ::sizeof(&ty); next_stack = align_to(next_stack, ty_size) + ty_size; diff --git a/winch/codegen/src/isa/aarch64/mod.rs b/winch/codegen/src/isa/aarch64/mod.rs index 34459b7c8bd7..15d6b21884bb 100644 --- a/winch/codegen/src/isa/aarch64/mod.rs +++ b/winch/codegen/src/isa/aarch64/mod.rs @@ -17,7 +17,7 @@ use cranelift_codegen::{MachTextSectionBuilder, TextSectionBuilder}; use masm::MacroAssembler as Aarch64Masm; use target_lexicon::Triple; use wasmparser::{FuncValidator, FunctionBody, ValidatorResources}; -use wasmtime_environ::{ModuleTranslation, ModuleTypes, VMOffsets, WasmFuncType}; +use wasmtime_environ::{ModuleTranslation, ModuleTypesBuilder, VMOffsets, WasmFuncType}; mod abi; mod address; @@ -87,7 +87,7 @@ impl TargetIsa for Aarch64 { sig: &WasmFuncType, body: &FunctionBody, translation: &ModuleTranslation, - types: &ModuleTypes, + types: &ModuleTypesBuilder, builtins: &mut BuiltinFunctions, validator: &mut FuncValidator, ) -> Result> { @@ -98,8 +98,8 @@ impl TargetIsa for Aarch64 { let stack = Stack::new(); let abi_sig = abi::Aarch64ABI::sig(sig, &CallingConvention::Default); - let defined_locals = - DefinedLocals::new::(translation, &mut body, validator)?; + let env = FuncEnv::new(&vmoffsets, translation, types); + let defined_locals = DefinedLocals::new::(&env, &mut body, validator)?; let frame = Frame::new::(&abi_sig, &defined_locals)?; let gpr = RegBitSet::int( ALL_GPR.into(), @@ -110,7 +110,6 @@ impl TargetIsa for Aarch64 { let fpr = RegBitSet::float(0, 0, usize::try_from(MAX_FPR).unwrap()); let regalloc = RegAlloc::from(gpr, fpr); let codegen_context = CodeGenContext::new(regalloc, stack, frame, builtins, &vmoffsets); - let env = FuncEnv::new(&vmoffsets, translation, types); let mut codegen = CodeGen::new(&mut masm, codegen_context, env, abi_sig); codegen.emit(&mut body, validator)?; diff --git a/winch/codegen/src/isa/mod.rs b/winch/codegen/src/isa/mod.rs index 480a5608478d..7ccf621f5f8c 100644 --- a/winch/codegen/src/isa/mod.rs +++ b/winch/codegen/src/isa/mod.rs @@ -10,7 +10,7 @@ use std::{ }; use target_lexicon::{Architecture, Triple}; use wasmparser::{FuncValidator, FunctionBody, ValidatorResources}; -use wasmtime_environ::{ModuleTranslation, ModuleTypes, WasmFuncType}; +use wasmtime_environ::{ModuleTranslation, ModuleTypesBuilder, WasmFuncType}; #[cfg(feature = "x64")] pub(crate) mod x64; @@ -158,7 +158,7 @@ pub trait TargetIsa: Send + Sync { sig: &WasmFuncType, body: &FunctionBody, translation: &ModuleTranslation, - types: &ModuleTypes, + types: &ModuleTypesBuilder, builtins: &mut BuiltinFunctions, validator: &mut FuncValidator, ) -> Result>; diff --git a/winch/codegen/src/isa/x64/mod.rs b/winch/codegen/src/isa/x64/mod.rs index b6dd7ec6436a..7e56d933fc65 100644 --- a/winch/codegen/src/isa/x64/mod.rs +++ b/winch/codegen/src/isa/x64/mod.rs @@ -19,7 +19,7 @@ use cranelift_codegen::{isa::x64::settings as x64_settings, Final, MachBufferFin use cranelift_codegen::{MachTextSectionBuilder, TextSectionBuilder}; use target_lexicon::Triple; use wasmparser::{FuncValidator, FunctionBody, ValidatorResources}; -use wasmtime_environ::{ModuleTranslation, ModuleTypes, VMOffsets, WasmFuncType}; +use wasmtime_environ::{ModuleTranslation, ModuleTypesBuilder, VMOffsets, WasmFuncType}; use self::regs::{ALL_FPR, ALL_GPR, MAX_FPR, MAX_GPR, NON_ALLOCATABLE_FPR, NON_ALLOCATABLE_GPR}; @@ -91,7 +91,7 @@ impl TargetIsa for X64 { sig: &WasmFuncType, body: &FunctionBody, translation: &ModuleTranslation, - types: &ModuleTypes, + types: &ModuleTypesBuilder, builtins: &mut BuiltinFunctions, validator: &mut FuncValidator, ) -> Result> { @@ -107,7 +107,8 @@ impl TargetIsa for X64 { let stack = Stack::new(); let abi_sig = abi::X64ABI::sig(sig, &CallingConvention::Default); - let defined_locals = DefinedLocals::new::(translation, &mut body, validator)?; + let env = FuncEnv::new(&vmoffsets, translation, types); + let defined_locals = DefinedLocals::new::(&env, &mut body, validator)?; let frame = Frame::new::(&abi_sig, &defined_locals)?; let gpr = RegBitSet::int( ALL_GPR.into(), @@ -121,7 +122,6 @@ impl TargetIsa for X64 { ); let regalloc = RegAlloc::from(gpr, fpr); - let env = FuncEnv::new(&vmoffsets, translation, types); let codegen_context = CodeGenContext::new(regalloc, stack, frame, builtins, &vmoffsets); let mut codegen = CodeGen::new(&mut masm, codegen_context, env, abi_sig); diff --git a/winch/filetests/Cargo.toml b/winch/filetests/Cargo.toml index e311cfcd8c2d..1de596fea757 100644 --- a/winch/filetests/Cargo.toml +++ b/winch/filetests/Cargo.toml @@ -24,3 +24,4 @@ serde = { workspace = true } serde_derive = { workspace = true } cranelift-codegen = { workspace = true } capstone = { workspace = true } +wasmparser = { workspace = true } diff --git a/winch/filetests/src/lib.rs b/winch/filetests/src/lib.rs index e9593cff3c8b..237ecbb8286b 100644 --- a/winch/filetests/src/lib.rs +++ b/winch/filetests/src/lib.rs @@ -12,8 +12,8 @@ mod test { use wasmtime_environ::ModuleTranslation; use wasmtime_environ::{ wasmparser::{Parser as WasmParser, Validator}, - DefinedFuncIndex, FunctionBodyData, ModuleEnvironment, ModuleTypes, Tunables, TypeConvert, - VMOffsets, + DefinedFuncIndex, FunctionBodyData, ModuleEnvironment, ModuleTypesBuilder, Tunables, + TypeConvert, VMOffsets, }; use winch_codegen::{lookup, BuiltinFunctions, TargetIsa}; use winch_test_macros::generate_file_tests; @@ -109,7 +109,6 @@ mod test { .translate(parser, &wasm) .context("Failed to translate WebAssembly module") .unwrap(); - let types = types.finish(); let body_inputs = std::mem::take(&mut translation.function_body_inputs); @@ -148,7 +147,7 @@ mod test { fn compile( isa: &Box, - module_types: &ModuleTypes, + module_types: &ModuleTypesBuilder, translation: &ModuleTranslation, f: (DefinedFuncIndex, FunctionBodyData<'_>), ) -> Vec { @@ -157,7 +156,7 @@ mod test { let index = module.func_index(f.0); let sig = types[types.core_function_at(index.as_u32())].unwrap_func(); - let sig = translation.module.convert_func_type(&sig); + let sig = DummyConvert.convert_func_type(&sig); let vmoffsets = VMOffsets::new(isa.pointer_bytes(), &translation.module); let mut builtins = BuiltinFunctions::new(&vmoffsets, isa.wasmtime_call_conv()); @@ -176,4 +175,12 @@ mod test { disasm(buffer.data(), isa).unwrap() } + + struct DummyConvert; + + impl TypeConvert for DummyConvert { + fn lookup_heap_type(&self, _: wasmparser::UnpackedIndex) -> wasmtime_environ::WasmHeapType { + todo!() + } + } } diff --git a/winch/src/compile.rs b/winch/src/compile.rs index 3890371ba6f1..e6c3040e32c0 100644 --- a/winch/src/compile.rs +++ b/winch/src/compile.rs @@ -5,7 +5,7 @@ use std::{fs, path::PathBuf, str::FromStr}; use target_lexicon::Triple; use wasmtime_environ::{ wasmparser::{Parser as WasmParser, Validator}, - DefinedFuncIndex, FunctionBodyData, ModuleEnvironment, ModuleTranslation, ModuleTypes, + DefinedFuncIndex, FunctionBodyData, ModuleEnvironment, ModuleTranslation, ModuleTypesBuilder, Tunables, TypeConvert, VMOffsets, }; use winch_codegen::{lookup, BuiltinFunctions, TargetIsa}; @@ -36,7 +36,6 @@ pub fn run(opt: &Options) -> Result<()> { let mut translation = ModuleEnvironment::new(&tunables, &mut validator, &mut types) .translate(parser, &bytes) .context("Failed to translate WebAssembly module")?; - let types = types.finish(); let body_inputs = std::mem::take(&mut translation.function_body_inputs); body_inputs @@ -49,13 +48,13 @@ pub fn run(opt: &Options) -> Result<()> { fn compile( isa: &Box, translation: &ModuleTranslation, - module_types: &ModuleTypes, + module_types: &ModuleTypesBuilder, f: (DefinedFuncIndex, FunctionBodyData<'_>), ) -> Result<()> { let index = translation.module.func_index(f.0); let types = &translation.get_types(); let sig = types[types.core_function_at(index.as_u32())].unwrap_func(); - let sig = translation.module.convert_func_type(sig); + let sig = DummyConvert.convert_func_type(sig); let FunctionBodyData { body, validator } = f.1; let vmoffsets = VMOffsets::new(isa.pointer_bytes(), &translation.module); let mut builtins = BuiltinFunctions::new(&vmoffsets, isa.wasmtime_call_conv()); @@ -78,3 +77,11 @@ fn compile( Ok(()) } + +struct DummyConvert; + +impl TypeConvert for DummyConvert { + fn lookup_heap_type(&self, _: wasmparser::UnpackedIndex) -> wasmtime_environ::WasmHeapType { + todo!() + } +}