Skip to content

Commit

Permalink
Store Tunables inside the Compiler
Browse files Browse the repository at this point in the history
Instead of passing as an argument to every `Compiler` method.
  • Loading branch information
fitzgen committed Jul 27, 2023
1 parent 84a4158 commit 82b7774
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 76 deletions.
10 changes: 9 additions & 1 deletion crates/cranelift/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodegenResult<OwnedTargetIsa>>,
linkopts: LinkOptions,
cache_store: Option<Arc<dyn CacheStore>>,
Expand All @@ -36,6 +37,7 @@ pub struct LinkOptions {

pub fn builder() -> Box<dyn CompilerBuilder> {
Box::new(Builder {
tunables: Tunables::default(),
inner: IsaBuilder::new(|triple| isa::lookup(triple).map_err(|e| e.into())),
linkopts: LinkOptions::default(),
cache_store: None,
Expand Down Expand Up @@ -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<Box<dyn wasmtime_environ::Compiler>> {
let isa = self.inner.build()?;
Ok(Box::new(crate::compiler::Compiler::new(
self.tunables.clone(),
isa,
self.cache_store.clone(),
self.linkopts.clone(),
Expand Down
32 changes: 14 additions & 18 deletions crates/cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<CompilerContext>>,
isa: OwnedTargetIsa,
linkopts: LinkOptions,
Expand Down Expand Up @@ -102,13 +103,15 @@ impl Drop for Compiler {

impl Compiler {
pub(crate) fn new(
tunables: Tunables,
isa: OwnedTargetIsa,
cache_store: Option<Arc<dyn CacheStore>>,
linkopts: LinkOptions,
clif_dir: Option<path::PathBuf>,
) -> Compiler {
Compiler {
contexts: Default::default(),
tunables,
isa,
linkopts,
cache_store,
Expand All @@ -123,7 +126,6 @@ impl wasmtime_environ::Compiler for Compiler {
translation: &ModuleTranslation<'_>,
func_index: DefinedFuncIndex,
input: FunctionBodyData<'_>,
tunables: &Tunables,
types: &ModuleTypes,
) -> Result<(WasmFunctionInfo, Box<dyn Any + Send>), CompileError> {
let isa = &*self.isa;
Expand All @@ -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.
Expand Down Expand Up @@ -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());
Expand All @@ -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<Box<dyn Any + Send>, CompileError> {
Expand All @@ -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();
Expand Down Expand Up @@ -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<Box<dyn Any + Send>, CompileError> {
Expand All @@ -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();
Expand Down Expand Up @@ -353,12 +353,11 @@ impl wasmtime_environ::Compiler for Compiler {

fn compile_wasm_to_native_trampoline(
&self,
tunables: &Tunables,
wasm_func_ty: &WasmFuncType,
) -> Result<Box<dyn Any + Send>, 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();
Expand Down Expand Up @@ -444,7 +443,6 @@ impl wasmtime_environ::Compiler for Compiler {
&self,
obj: &mut Object<'static>,
funcs: &[(String, Box<dyn Any + Send>)],
tunables: &Tunables,
resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize,
) -> Result<Vec<(SymbolId, FunctionLoc)>> {
let mut builder =
Expand All @@ -461,7 +459,7 @@ impl wasmtime_environ::Compiler for Compiler {
.downcast_ref::<CompiledFunction<CompiledFuncEnv>>()
.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);
}
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -784,11 +781,10 @@ impl Compiler {
&self,
ty: &WasmFuncType,
host_fn: usize,
tunables: &Tunables,
) -> Result<CompiledFunction<CompiledFuncEnv>, 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();
Expand Down
16 changes: 7 additions & 9 deletions crates/cranelift/src/compiler/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -624,15 +624,13 @@ impl<'a> TrampolineCompiler<'a> {
impl ComponentCompiler for Compiler {
fn compile_trampoline(
&self,
tunables: &Tunables,
component: &ComponentTranslation,
types: &ComponentTypes,
index: TrampolineIndex,
) -> Result<AllCallFunc<Box<dyn Any + Send>>> {
let compile = |abi: Abi| -> Result<_> {
let mut compiler = self.function_compiler();
let mut c = TrampolineCompiler::new(
tunables,
self,
&mut compiler,
&component.component,
Expand Down
13 changes: 5 additions & 8 deletions crates/environ/src/compilation.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<dyn CacheStore>) -> 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<Box<dyn Compiler>>;
}
Expand Down Expand Up @@ -174,7 +177,6 @@ pub trait Compiler: Send + Sync {
translation: &ModuleTranslation<'_>,
index: DefinedFuncIndex,
data: FunctionBodyData<'_>,
tunables: &Tunables,
types: &ModuleTypes,
) -> Result<(WasmFunctionInfo, Box<dyn Any + Send>), CompileError>;

Expand All @@ -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<Box<dyn Any + Send>, CompileError>;
Expand All @@ -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<Box<dyn Any + Send>, CompileError>;
Expand All @@ -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<Box<dyn Any + Send>, CompileError>;

Expand Down Expand Up @@ -246,7 +245,6 @@ pub trait Compiler: Send + Sync {
&self,
obj: &mut Object<'static>,
funcs: &[(String, Box<dyn Any + Send>)],
tunables: &Tunables,
resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize,
) -> Result<Vec<(SymbolId, FunctionLoc)>>;

Expand Down Expand Up @@ -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`.
Expand Down
2 changes: 0 additions & 2 deletions crates/environ/src/component/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::component::{ComponentTranslation, ComponentTypes, TrampolineIndex};
use crate::Tunables;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::any::Any;
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 82b7774

Please sign in to comment.