Files
wasmtime/crates/cranelift/src/builder.rs
Alex Crichton 0313e30d76 Remove dependency on TargetIsa from Wasmtime crates (#3178)
This commit started off by deleting the `cranelift_codegen::settings`
reexport in the `wasmtime-environ` crate and then basically played
whack-a-mole until everything compiled again. The main result of this is
that the `wasmtime-*` family of crates have generally less of a
dependency on the `TargetIsa` trait and type from Cranelift. While the
dependency isn't entirely severed yet this is at least a significant
start.

This commit is intended to be largely refactorings, no functional
changes are intended here. The refactorings are:

* A `CompilerBuilder` trait has been added to `wasmtime_environ` which
  server as an abstraction used to create compilers and configure them
  in a uniform fashion. The `wasmtime::Config` type now uses this
  instead of cranelift-specific settings. The `wasmtime-jit` crate
  exports the ability to create a compiler builder from a
  `CompilationStrategy`, which only works for Cranelift right now. In a
  cranelift-less build of Wasmtime this is expected to return a trait
  object that fails all requests to compile.

* The `Compiler` trait in the `wasmtime_environ` crate has been souped
  up with a number of methods that Wasmtime and other crates needed.

* The `wasmtime-debug` crate is now moved entirely behind the
  `wasmtime-cranelift` crate.

* The `wasmtime-cranelift` crate is now only depended on by the
  `wasmtime-jit` crate.

* Wasm types in `cranelift-wasm` no longer contain their IR type,
  instead they only contain the `WasmType`. This is required to get
  everything to align correctly but will also be required in a future
  refactoring where the types used by `cranelift-wasm` will be extracted
  to a separate crate.

* I moved around a fair bit of code in `wasmtime-cranelift`.

* Some gdb-specific jit-specific code has moved from `wasmtime-debug` to
  `wasmtime-jit`.
2021-08-16 09:55:39 -05:00

114 lines
3.3 KiB
Rust

//! Implementation of a "compiler builder" for cranelift
//!
//! This module contains the implementation of how Cranelift is configured, as
//! well as providing a function to return the default configuration to build.
use anyhow::Result;
use cranelift_codegen::isa;
use cranelift_codegen::settings::{self, Configurable, SetError};
use std::fmt;
use wasmtime_environ::{CompilerBuilder, Setting, SettingKind};
#[derive(Clone)]
struct Builder {
flags: settings::Builder,
isa_flags: isa::Builder,
}
pub fn builder() -> Box<dyn CompilerBuilder> {
let mut flags = settings::builder();
// There are two possible traps for division, and this way
// we get the proper one if code traps.
flags
.enable("avoid_div_traps")
.expect("should be valid flag");
// We don't use probestack as a stack limit mechanism
flags
.set("enable_probestack", "false")
.expect("should be valid flag");
Box::new(Builder {
flags,
isa_flags: cranelift_native::builder().expect("host machine is not a supported target"),
})
}
impl CompilerBuilder for Builder {
fn triple(&self) -> &target_lexicon::Triple {
self.isa_flags.triple()
}
fn clone(&self) -> Box<dyn CompilerBuilder> {
Box::new(Clone::clone(self))
}
fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {
self.isa_flags = isa::lookup(target)?;
Ok(())
}
fn set(&mut self, name: &str, value: &str) -> Result<()> {
if let Err(err) = self.flags.set(name, value) {
match err {
SetError::BadName(_) => {
// Try the target-specific flags.
self.isa_flags.set(name, value)?;
}
_ => return Err(err.into()),
}
}
Ok(())
}
fn enable(&mut self, name: &str) -> Result<()> {
if let Err(err) = self.flags.enable(name) {
match err {
SetError::BadName(_) => {
// Try the target-specific flags.
self.isa_flags.enable(name)?;
}
_ => return Err(err.into()),
}
}
Ok(())
}
fn build(&self) -> Box<dyn wasmtime_environ::Compiler> {
let isa = self
.isa_flags
.clone()
.finish(settings::Flags::new(self.flags.clone()));
Box::new(crate::compiler::Compiler::new(isa))
}
fn settings(&self) -> Vec<Setting> {
self.isa_flags
.iter()
.map(|s| Setting {
description: s.description,
name: s.name,
values: s.values,
kind: match s.kind {
settings::SettingKind::Preset => SettingKind::Preset,
settings::SettingKind::Enum => SettingKind::Enum,
settings::SettingKind::Num => SettingKind::Num,
settings::SettingKind::Bool => SettingKind::Bool,
},
})
.collect()
}
}
impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Builder")
.field(
"flags",
&settings::Flags::new(self.flags.clone()).to_string(),
)
.finish()
}
}