diff --git a/crates/cranelift/src/builder.rs b/crates/cranelift/src/builder.rs index 5fcf8e05fbc3..cd255eccd999 100644 --- a/crates/cranelift/src/builder.rs +++ b/crates/cranelift/src/builder.rs @@ -12,9 +12,10 @@ use std::fmt; use std::path; use std::sync::Arc; use wasmtime_cranelift_shared::isa_builder::IsaBuilder; -use wasmtime_environ::{CacheStore, CompilerBuilder, Setting}; +use wasmtime_environ::{CacheStore, CompilerBuilder, Setting, Tunables}; struct Builder { + tunables: Tunables, inner: IsaBuilder>, linkopts: LinkOptions, cache_store: Option>, @@ -36,6 +37,7 @@ pub struct LinkOptions { pub fn builder() -> Box { Box::new(Builder { + tunables: Tunables::default(), inner: IsaBuilder::new(|triple| isa::lookup(triple).map_err(|e| e.into())), linkopts: LinkOptions::default(), cache_store: None, @@ -76,9 +78,15 @@ impl CompilerBuilder for Builder { self.inner.enable(name) } + fn set_tunables(&mut self, tunables: Tunables) -> Result<()> { + self.tunables = tunables; + Ok(()) + } + fn build(&self) -> Result> { let isa = self.inner.build()?; Ok(Box::new(crate::compiler::Compiler::new( + self.tunables.clone(), isa, self.cache_store.clone(), self.linkopts.clone(), diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs index fe5c6a4bc488..3578cad1374e 100644 --- a/crates/cranelift/src/compiler.rs +++ b/crates/cranelift/src/compiler.rs @@ -65,6 +65,7 @@ impl Default for CompilerContext { /// A compiler that compiles a WebAssembly module with Compiler, translating /// the Wasm to Compiler IR, optimizing it and then translating to assembly. pub(crate) struct Compiler { + tunables: Tunables, contexts: Mutex>, isa: OwnedTargetIsa, linkopts: LinkOptions, @@ -102,6 +103,7 @@ impl Drop for Compiler { impl Compiler { pub(crate) fn new( + tunables: Tunables, isa: OwnedTargetIsa, cache_store: Option>, linkopts: LinkOptions, @@ -109,6 +111,7 @@ impl Compiler { ) -> Compiler { Compiler { contexts: Default::default(), + tunables, isa, linkopts, cache_store, @@ -123,7 +126,6 @@ impl wasmtime_environ::Compiler for Compiler { translation: &ModuleTranslation<'_>, func_index: DefinedFuncIndex, input: FunctionBodyData<'_>, - tunables: &Tunables, types: &ModuleTypes, ) -> Result<(WasmFunctionInfo, Box), CompileError> { let isa = &*self.isa; @@ -135,17 +137,17 @@ impl wasmtime_environ::Compiler for Compiler { let mut compiler = self.function_compiler(); let context = &mut compiler.cx.codegen_context; - context.func.signature = wasm_call_signature(isa, wasm_func_ty, tunables); + context.func.signature = wasm_call_signature(isa, wasm_func_ty, &self.tunables); context.func.name = UserFuncName::User(UserExternalName { namespace: 0, index: func_index.as_u32(), }); - if tunables.generate_native_debuginfo { + if self.tunables.generate_native_debuginfo { context.func.collect_debug_info(); } - let mut func_env = FuncEnvironment::new(isa, translation, types, tunables); + let mut func_env = FuncEnvironment::new(isa, translation, types, &self.tunables); // The `stack_limit` global value below is the implementation of stack // overflow checks in Wasmtime. @@ -220,7 +222,7 @@ impl wasmtime_environ::Compiler for Compiler { write!(output, "{}", context.func.display()).unwrap(); } - let (info, func) = compiler.finish_with_info(Some((&body, tunables)))?; + let (info, func) = compiler.finish_with_info(Some((&body, &self.tunables)))?; let timing = cranelift_codegen::timing::take_current(); log::debug!("{:?} translated in {:?}", func_index, timing.total()); @@ -232,7 +234,6 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_array_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - tunables: &Tunables, types: &ModuleTypes, def_func_index: DefinedFuncIndex, ) -> Result, CompileError> { @@ -242,7 +243,7 @@ impl wasmtime_environ::Compiler for Compiler { let isa = &*self.isa; let pointer_type = isa.pointer_type(); - let wasm_call_sig = wasm_call_signature(isa, wasm_func_ty, tunables); + let wasm_call_sig = wasm_call_signature(isa, wasm_func_ty, &self.tunables); let array_call_sig = array_call_signature(isa); let mut compiler = self.function_compiler(); @@ -301,7 +302,6 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_native_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - tunables: &Tunables, types: &ModuleTypes, def_func_index: DefinedFuncIndex, ) -> Result, CompileError> { @@ -312,7 +312,7 @@ impl wasmtime_environ::Compiler for Compiler { let isa = &*self.isa; let pointer_type = isa.pointer_type(); let func_index = translation.module.func_index(def_func_index); - let wasm_call_sig = wasm_call_signature(isa, wasm_func_ty, tunables); + let wasm_call_sig = wasm_call_signature(isa, wasm_func_ty, &self.tunables); let native_call_sig = native_call_signature(isa, wasm_func_ty); let mut compiler = self.function_compiler(); @@ -353,12 +353,11 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_wasm_to_native_trampoline( &self, - tunables: &Tunables, wasm_func_ty: &WasmFuncType, ) -> Result, CompileError> { let isa = &*self.isa; let pointer_type = isa.pointer_type(); - let wasm_call_sig = wasm_call_signature(isa, wasm_func_ty, tunables); + let wasm_call_sig = wasm_call_signature(isa, wasm_func_ty, &self.tunables); let native_call_sig = native_call_signature(isa, wasm_func_ty); let mut compiler = self.function_compiler(); @@ -444,7 +443,6 @@ impl wasmtime_environ::Compiler for Compiler { &self, obj: &mut Object<'static>, funcs: &[(String, Box)], - tunables: &Tunables, resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize, ) -> Result> { let mut builder = @@ -461,7 +459,7 @@ impl wasmtime_environ::Compiler for Compiler { .downcast_ref::>() .unwrap(); let (sym, range) = builder.append_func(&sym, func, |idx| resolve_reloc(i, idx)); - if tunables.generate_address_map { + if self.tunables.generate_address_map { let addr = func.address_map(); addrs.push(range.clone(), &addr.instructions); } @@ -476,7 +474,7 @@ impl wasmtime_environ::Compiler for Compiler { builder.finish(); - if tunables.generate_address_map { + if self.tunables.generate_address_map { addrs.append_to(obj); } traps.append_to(obj); @@ -486,12 +484,11 @@ impl wasmtime_environ::Compiler for Compiler { fn emit_trampolines_for_array_call_host_func( &self, - tunables: &Tunables, ty: &WasmFuncType, host_fn: usize, obj: &mut Object<'static>, ) -> Result<(FunctionLoc, FunctionLoc)> { - let mut wasm_to_array = self.wasm_to_array_trampoline(ty, host_fn, tunables)?; + let mut wasm_to_array = self.wasm_to_array_trampoline(ty, host_fn)?; let mut native_to_array = self.native_to_array_trampoline(ty, host_fn)?; let mut builder = ModuleTextBuilder::new(obj, self, self.isa.text_section_builder(2)); @@ -784,11 +781,10 @@ impl Compiler { &self, ty: &WasmFuncType, host_fn: usize, - tunables: &Tunables, ) -> Result, CompileError> { let isa = &*self.isa; let pointer_type = isa.pointer_type(); - let wasm_call_sig = wasm_call_signature(isa, ty, tunables); + let wasm_call_sig = wasm_call_signature(isa, ty, &self.tunables); let array_call_sig = array_call_signature(isa); let mut compiler = self.function_compiler(); diff --git a/crates/cranelift/src/compiler/component.rs b/crates/cranelift/src/compiler/component.rs index f3457f980b1b..e914d8a26805 100644 --- a/crates/cranelift/src/compiler/component.rs +++ b/crates/cranelift/src/compiler/component.rs @@ -8,10 +8,9 @@ use cranelift_frontend::FunctionBuilder; use std::any::Any; use wasmtime_cranelift_shared::{ALWAYS_TRAP_CODE, CANNOT_ENTER_CODE}; use wasmtime_environ::component::*; -use wasmtime_environ::{PtrSize, SignatureIndex, Tunables, WasmType}; +use wasmtime_environ::{PtrSize, SignatureIndex, WasmType}; struct TrampolineCompiler<'a> { - tunables: &'a Tunables, compiler: &'a Compiler, isa: &'a (dyn TargetIsa + 'static), builder: FunctionBuilder<'a>, @@ -32,7 +31,6 @@ enum Abi { impl<'a> TrampolineCompiler<'a> { fn new( - tunables: &'a Tunables, compiler: &'a Compiler, func_compiler: &'a mut super::FunctionCompiler<'_>, component: &'a Component, @@ -46,14 +44,13 @@ impl<'a> TrampolineCompiler<'a> { let func = ir::Function::with_name_signature( ir::UserFuncName::user(0, 0), match abi { - Abi::Wasm => crate::wasm_call_signature(isa, ty, tunables), + Abi::Wasm => crate::wasm_call_signature(isa, ty, &compiler.tunables), Abi::Native => crate::native_call_signature(isa, ty), Abi::Array => crate::array_call_signature(isa), }, ); let (builder, block0) = func_compiler.builder(func); TrampolineCompiler { - tunables, compiler, isa, builder, @@ -491,8 +488,11 @@ impl<'a> TrampolineCompiler<'a> { i32::from(self.offsets.ptr.vm_func_ref_vmctx()), ); - let sig = - crate::wasm_call_signature(self.isa, &self.types[self.signature], self.tunables); + let sig = crate::wasm_call_signature( + self.isa, + &self.types[self.signature], + &self.compiler.tunables, + ); let sig_ref = self.builder.import_signature(sig); // NB: note that the "caller" vmctx here is the caller of this @@ -624,7 +624,6 @@ impl<'a> TrampolineCompiler<'a> { impl ComponentCompiler for Compiler { fn compile_trampoline( &self, - tunables: &Tunables, component: &ComponentTranslation, types: &ComponentTypes, index: TrampolineIndex, @@ -632,7 +631,6 @@ impl ComponentCompiler for Compiler { let compile = |abi: Abi| -> Result<_> { let mut compiler = self.function_compiler(); let mut c = TrampolineCompiler::new( - tunables, self, &mut compiler, &component.component, diff --git a/crates/environ/src/compilation.rs b/crates/environ/src/compilation.rs index 5b463960660f..9b7964327889 100644 --- a/crates/environ/src/compilation.rs +++ b/crates/environ/src/compilation.rs @@ -1,10 +1,10 @@ //! A `Compilation` contains the compiled function bodies for a WebAssembly //! module. -use crate::obj; +use crate::{obj, Tunables}; use crate::{ DefinedFuncIndex, FilePos, FuncIndex, FunctionBodyData, ModuleTranslation, ModuleTypes, - PrimaryMap, StackMap, Tunables, WasmError, WasmFuncType, + PrimaryMap, StackMap, WasmError, WasmFuncType, }; use anyhow::Result; use object::write::{Object, SymbolId}; @@ -122,6 +122,9 @@ pub trait CompilerBuilder: Send + Sync + fmt::Debug { /// This will return an error if the compiler does not support incremental compilation. fn enable_incremental_compilation(&mut self, cache_store: Arc) -> Result<()>; + /// Set the tunables for this compiler. + fn set_tunables(&mut self, tunables: Tunables) -> Result<()>; + /// Builds a new [`Compiler`] object from this configuration. fn build(&self) -> Result>; } @@ -174,7 +177,6 @@ pub trait Compiler: Send + Sync { translation: &ModuleTranslation<'_>, index: DefinedFuncIndex, data: FunctionBodyData<'_>, - tunables: &Tunables, types: &ModuleTypes, ) -> Result<(WasmFunctionInfo, Box), CompileError>; @@ -186,7 +188,6 @@ pub trait Compiler: Send + Sync { fn compile_array_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - tunables: &Tunables, types: &ModuleTypes, index: DefinedFuncIndex, ) -> Result, CompileError>; @@ -199,7 +200,6 @@ pub trait Compiler: Send + Sync { fn compile_native_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - tunables: &Tunables, types: &ModuleTypes, index: DefinedFuncIndex, ) -> Result, CompileError>; @@ -211,7 +211,6 @@ pub trait Compiler: Send + Sync { /// Wasm-to-host transition (e.g. registers used for fast stack walking). fn compile_wasm_to_native_trampoline( &self, - tunables: &Tunables, wasm_func_ty: &WasmFuncType, ) -> Result, CompileError>; @@ -246,7 +245,6 @@ pub trait Compiler: Send + Sync { &self, obj: &mut Object<'static>, funcs: &[(String, Box)], - tunables: &Tunables, resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize, ) -> Result>; @@ -276,7 +274,6 @@ pub trait Compiler: Send + Sync { /// the function pointer to the host code. fn emit_trampolines_for_array_call_host_func( &self, - tunables: &Tunables, ty: &WasmFuncType, // Actually `host_fn: VMArrayCallFunction` but that type is not // available in `wasmtime-environ`. diff --git a/crates/environ/src/component/compiler.rs b/crates/environ/src/component/compiler.rs index 01bc31f558a0..ed192e1df6df 100644 --- a/crates/environ/src/component/compiler.rs +++ b/crates/environ/src/component/compiler.rs @@ -1,5 +1,4 @@ use crate::component::{ComponentTranslation, ComponentTypes, TrampolineIndex}; -use crate::Tunables; use anyhow::Result; use serde::{Deserialize, Serialize}; use std::any::Any; @@ -41,7 +40,6 @@ pub trait ComponentCompiler: Send + Sync { /// this trait for Cranelift for more information. fn compile_trampoline( &self, - tunables: &Tunables, component: &ComponentTranslation, types: &ComponentTypes, trampoline: TrampolineIndex, diff --git a/crates/wasmtime/src/compiler.rs b/crates/wasmtime/src/compiler.rs index af4b25d8bcea..33856f4b2198 100644 --- a/crates/wasmtime/src/compiler.rs +++ b/crates/wasmtime/src/compiler.rs @@ -32,8 +32,7 @@ use wasmtime_environ::{ }; use wasmtime_jit::{CompiledFunctionInfo, CompiledModuleInfo}; -type CompileInput<'a> = - Box Result + Send + 'a>; +type CompileInput<'a> = Box Result + Send + 'a>; /// A sortable, comparable key for a compilation output. /// @@ -168,10 +167,7 @@ pub struct CompileInputs<'a> { } impl<'a> CompileInputs<'a> { - fn push_input( - &mut self, - f: impl FnOnce(&Tunables, &dyn Compiler) -> Result + Send + 'a, - ) { + fn push_input(&mut self, f: impl FnOnce(&dyn Compiler) -> Result + Send + 'a) { self.inputs.push(Box::new(f)); } @@ -207,13 +203,13 @@ impl<'a> CompileInputs<'a> { ret.collect_inputs_in_translations(types.module_types(), module_translations); for (idx, trampoline) in component.trampolines.iter() { - ret.push_input(move |tunables, compiler| { + ret.push_input(move |compiler| { Ok(CompileOutput { key: CompileKey::trampoline(idx), symbol: trampoline.symbol_name(), function: compiler .component_compiler() - .compile_trampoline(tunables, component, types, idx)? + .compile_trampoline(component, types, idx)? .into(), info: None, }) @@ -228,9 +224,8 @@ impl<'a> CompileInputs<'a> { // requested through initializers above or such. if component.component.num_resources > 0 { if let Some(sig) = types.find_resource_drop_signature() { - ret.push_input(move |tunables, compiler| { - let trampoline = - compiler.compile_wasm_to_native_trampoline(tunables, &types[sig])?; + ret.push_input(move |compiler| { + let trampoline = compiler.compile_wasm_to_native_trampoline(&types[sig])?; Ok(CompileOutput { key: CompileKey::resource_drop_wasm_to_native_trampoline(), symbol: "resource_drop_trampoline".to_string(), @@ -259,15 +254,10 @@ impl<'a> CompileInputs<'a> { for (module, translation, functions) in translations { for (def_func_index, func_body) in functions { - self.push_input(move |tunables, compiler| { + self.push_input(move |compiler| { let func_index = translation.module.func_index(def_func_index); - let (info, function) = compiler.compile_function( - translation, - def_func_index, - func_body, - tunables, - types, - )?; + let (info, function) = + compiler.compile_function(translation, def_func_index, func_body, types)?; Ok(CompileOutput { key: CompileKey::wasm_function(module, def_func_index), symbol: format!( @@ -282,11 +272,10 @@ impl<'a> CompileInputs<'a> { let func_index = translation.module.func_index(def_func_index); if translation.module.functions[func_index].is_escaping() { - self.push_input(move |tunables, compiler| { + self.push_input(move |compiler| { let func_index = translation.module.func_index(def_func_index); let trampoline = compiler.compile_array_to_wasm_trampoline( translation, - tunables, types, def_func_index, )?; @@ -302,11 +291,10 @@ impl<'a> CompileInputs<'a> { }) }); - self.push_input(move |tunables, compiler| { + self.push_input(move |compiler| { let func_index = translation.module.func_index(def_func_index); let trampoline = compiler.compile_native_to_wasm_trampoline( translation, - tunables, types, def_func_index, )?; @@ -330,10 +318,9 @@ impl<'a> CompileInputs<'a> { } for signature in sigs { - self.push_input(move |tunables, compiler| { + self.push_input(move |compiler| { let wasm_func_ty = &types[signature]; - let trampoline = - compiler.compile_wasm_to_native_trampoline(tunables, wasm_func_ty)?; + let trampoline = compiler.compile_wasm_to_native_trampoline(wasm_func_ty)?; Ok(CompileOutput { key: CompileKey::wasm_to_native_trampoline(signature), symbol: format!( @@ -350,11 +337,10 @@ impl<'a> CompileInputs<'a> { /// Compile these `CompileInput`s (maybe in parallel) and return the /// resulting `UnlinkedCompileOutput`s. pub fn compile(self, engine: &Engine) -> Result { - let tunables = &engine.config().tunables; let compiler = engine.compiler(); // Compile each individual input in parallel. - let raw_outputs = engine.run_maybe_parallel(self.inputs, |f| f(tunables, compiler))?; + let raw_outputs = engine.run_maybe_parallel(self.inputs, |f| f(compiler))?; // Bucket the outputs by kind. let mut outputs: BTreeMap> = BTreeMap::new(); @@ -474,7 +460,6 @@ impl FunctionIndices { let symbol_ids_and_locs = compiler.append_code( &mut obj, &compiled_funcs, - tunables, &|caller_index: usize, callee_index: FuncIndex| { let module = self .compiled_func_index_to_module diff --git a/crates/wasmtime/src/component/component.rs b/crates/wasmtime/src/component/component.rs index 81548bcfcc53..0d3af1916c75 100644 --- a/crates/wasmtime/src/component/component.rs +++ b/crates/wasmtime/src/component/component.rs @@ -205,7 +205,7 @@ impl Component { let (mut object, compilation_artifacts) = function_indices.link_and_append_code( object, - tunables, + &engine.config().tunables, compiler, compiled_funcs, module_translations, diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs index c13644c84d9a..de6514e300e5 100644 --- a/crates/wasmtime/src/config.rs +++ b/crates/wasmtime/src/config.rs @@ -1662,6 +1662,8 @@ impl Config { compiler.enable_incremental_compilation(cache_store.clone())?; } + compiler.set_tunables(self.tunables.clone())?; + Ok((self, compiler.build()?)) } diff --git a/crates/wasmtime/src/trampoline/func.rs b/crates/wasmtime/src/trampoline/func.rs index 1e09b5e2f84c..716e709a140e 100644 --- a/crates/wasmtime/src/trampoline/func.rs +++ b/crates/wasmtime/src/trampoline/func.rs @@ -89,7 +89,6 @@ where let (wasm_call_range, native_call_range) = engine .compiler() .emit_trampolines_for_array_call_host_func( - &engine.config().tunables, ft.as_wasm_func_type(), array_call_shim:: as usize, &mut obj, diff --git a/crates/winch/src/builder.rs b/crates/winch/src/builder.rs index d8f17d5bae6a..d9d534cc7a74 100644 --- a/crates/winch/src/builder.rs +++ b/crates/winch/src/builder.rs @@ -38,6 +38,11 @@ impl CompilerBuilder for Builder { self.inner.settings() } + fn set_tunables(&mut self, tunables: wasmtime_environ::Tunables) -> Result<()> { + let _ = tunables; + Ok(()) + } + fn build(&self) -> Result> { let isa = self.inner.build()?; diff --git a/crates/winch/src/compiler.rs b/crates/winch/src/compiler.rs index 94ca27561b45..73605c2c3761 100644 --- a/crates/winch/src/compiler.rs +++ b/crates/winch/src/compiler.rs @@ -6,7 +6,7 @@ use wasmparser::FuncValidatorAllocations; use wasmtime_cranelift_shared::{CompiledFunction, ModuleTextBuilder}; use wasmtime_environ::{ CompileError, DefinedFuncIndex, FilePos, FuncIndex, FunctionBodyData, FunctionLoc, - ModuleTranslation, ModuleTypes, PrimaryMap, TrapEncodingBuilder, Tunables, WasmFunctionInfo, + ModuleTranslation, ModuleTypes, PrimaryMap, TrapEncodingBuilder, WasmFunctionInfo, }; use winch_codegen::{TargetIsa, TrampolineKind}; @@ -53,7 +53,6 @@ impl wasmtime_environ::Compiler for Compiler { translation: &ModuleTranslation<'_>, index: DefinedFuncIndex, data: FunctionBodyData<'_>, - _tunables: &Tunables, types: &ModuleTypes, ) -> Result<(WasmFunctionInfo, Box), CompileError> { let index = translation.module.func_index(index); @@ -88,7 +87,6 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_array_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - _tunables: &Tunables, types: &ModuleTypes, index: DefinedFuncIndex, ) -> Result, CompileError> { @@ -108,7 +106,6 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_native_to_wasm_trampoline( &self, translation: &ModuleTranslation<'_>, - _tunables: &Tunables, types: &ModuleTypes, index: DefinedFuncIndex, ) -> Result, CompileError> { @@ -129,7 +126,6 @@ impl wasmtime_environ::Compiler for Compiler { fn compile_wasm_to_native_trampoline( &self, - _tunables: &Tunables, wasm_func_ty: &wasmtime_environ::WasmFuncType, ) -> Result, CompileError> { let buffer = self @@ -147,7 +143,6 @@ impl wasmtime_environ::Compiler for Compiler { &self, obj: &mut Object<'static>, funcs: &[(String, Box)], - _tunables: &Tunables, resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize, ) -> Result> { let mut builder = @@ -176,7 +171,6 @@ impl wasmtime_environ::Compiler for Compiler { fn emit_trampolines_for_array_call_host_func( &self, - _tunables: &Tunables, ty: &wasmtime_environ::WasmFuncType, // Actually `host_fn: VMArrayCallFunction` but that type is not // available in `wasmtime-environ`.