Browse Source
* Enable the native target by default in winch Match cranelift-codegen's build script where if no architecture is explicitly enabled then the host architecture is implicitly enabled. * Refactor Cranelift's ISA builder to share more with Winch This commit refactors the `Builder` type to have a type parameter representing the finished ISA with Cranelift and Winch having their own typedefs for `Builder` to represent their own builders. The intention is to use this shared functionality to produce more shared code between the two codegen backends. * Moving compiler shared components to a separate crate * Restore native flag inference in compiler building This fixes an oversight from the previous commits to use `cranelift-native` to infer flags for the native host when using default settings with Wasmtime. * Move `Compiler::page_size_align` into wasmtime-environ The `cranelift-codegen` crate doesn't need this and winch wants the same implementation, so shuffle it around so everyone has access to it. * Fill out `Compiler::{flags, isa_flags}` for Winch These are easy enough to plumb through with some shared code for Wasmtime. * Plumb the `is_branch_protection_enabled` flag for Winch Just forwarding an isa-specific setting accessor. * Moving executable creation to shared compiler crate * Adding builder back in and removing from shared crate * Refactoring the shared pieces for the `CompilerBuilder` I decided to move a couple things around from Alex's initial changes. Instead of having the shared builder do everything, I went back to having each compiler have a distinct builder implementation. I refactored most of the flag setting logic into a single shared location, so we can still reduce the amount of code duplication. With them being separate, we don't need to maintain things like `LinkOpts` which Winch doesn't currently use. We also have an avenue to error when certain flags are sent to Winch if we don't support them. I'm hoping this will make things more maintainable as we build out Winch. I'm still unsure about keeping everything shared in a single crate (`cranelift_shared`). It's starting to feel like this crate is doing too much, which makes it difficult to name. There does seem to be a need for two distinct abstraction: creating the final executable and the handling of shared/ISA flags when building the compiler. I could make them into two separate crates, but there doesn't seem to be enough there yet to justify it. * Documentation updates, and renaming the finish method * Adding back in a default temporarily to pass tests, and removing some unused imports * Fixing winch tests with wrong method name * Removing unused imports from codegen shared crate * Apply documentation formatting updates Co-authored-by: Saúl Cabrera <saulecabrera@gmail.com> * Adding back in cranelift_native flag inferring * Adding new shared crate to publish list * Adding write feature to pass cargo check --------- Co-authored-by: Alex Crichton <alex@alexcrichton.com> Co-authored-by: Saúl Cabrera <saulecabrera@gmail.com>pull/5973/head
Kevin Rizzo
2 years ago
committed by
GitHub
35 changed files with 503 additions and 289 deletions
@ -0,0 +1,18 @@ |
|||||
|
[package] |
||||
|
name = "wasmtime-cranelift-shared" |
||||
|
version.workspace = true |
||||
|
authors.workspace = true |
||||
|
description = "Base-level integration with Wasmtime and Cranelift" |
||||
|
license = "Apache-2.0 WITH LLVM-exception" |
||||
|
repository = "https://github.com/bytecodealliance/wasmtime" |
||||
|
documentation = "https://docs.rs/wasmtime-cranelift-shared/" |
||||
|
edition.workspace = true |
||||
|
|
||||
|
[dependencies] |
||||
|
anyhow = { workspace = true } |
||||
|
wasmtime-environ = { workspace = true } |
||||
|
cranelift-codegen = { workspace = true } |
||||
|
cranelift-native = { workspace = true } |
||||
|
target-lexicon = { workspace = true } |
||||
|
gimli = { workspace = true } |
||||
|
object = { workspace = true } |
@ -0,0 +1,106 @@ |
|||||
|
use anyhow::Result; |
||||
|
use cranelift_codegen::isa::IsaBuilder as Builder; |
||||
|
use cranelift_codegen::settings::{self, Configurable, Flags, SetError}; |
||||
|
use target_lexicon::Triple; |
||||
|
use wasmtime_environ::{Setting, SettingKind}; |
||||
|
|
||||
|
/// A helper to build an Isa for a compiler implementation.
|
||||
|
/// Compiler builders can wrap this to provide better flexibility when setting flags.
|
||||
|
///
|
||||
|
/// Most methods are mirrored from the `wasmtime_environ::CompilerBuilder` trait, so look there for more
|
||||
|
/// information.
|
||||
|
pub struct IsaBuilder<T> { |
||||
|
/// The shared flags that all targets share.
|
||||
|
shared_flags: settings::Builder, |
||||
|
/// The internal ISA builder for the current target.
|
||||
|
inner: Builder<T>, |
||||
|
/// A callback to lookup a new ISA builder for a target.
|
||||
|
pub lookup: fn(Triple) -> Result<Builder<T>>, |
||||
|
} |
||||
|
|
||||
|
impl<T> IsaBuilder<T> { |
||||
|
/// Create a new ISA builder with the given lookup function.
|
||||
|
pub fn new(lookup: fn(Triple) -> Result<Builder<T>>) -> Self { |
||||
|
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"); |
||||
|
|
||||
|
let mut isa_flags = lookup(Triple::host()).expect("host machine is not a supported target"); |
||||
|
cranelift_native::infer_native_flags(&mut isa_flags).unwrap(); |
||||
|
|
||||
|
Self { |
||||
|
shared_flags: flags, |
||||
|
inner: isa_flags, |
||||
|
lookup, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
pub fn triple(&self) -> &target_lexicon::Triple { |
||||
|
self.inner.triple() |
||||
|
} |
||||
|
|
||||
|
pub fn target(&mut self, target: target_lexicon::Triple) -> Result<()> { |
||||
|
self.inner = (self.lookup)(target)?; |
||||
|
Ok(()) |
||||
|
} |
||||
|
|
||||
|
pub fn settings(&self) -> Vec<Setting> { |
||||
|
self.inner |
||||
|
.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() |
||||
|
} |
||||
|
|
||||
|
pub fn set(&mut self, name: &str, value: &str) -> Result<()> { |
||||
|
if let Err(err) = self.shared_flags.set(name, value) { |
||||
|
match err { |
||||
|
SetError::BadName(_) => { |
||||
|
self.inner.set(name, value)?; |
||||
|
} |
||||
|
_ => return Err(err.into()), |
||||
|
} |
||||
|
} |
||||
|
Ok(()) |
||||
|
} |
||||
|
|
||||
|
pub fn enable(&mut self, name: &str) -> Result<()> { |
||||
|
if let Err(err) = self.shared_flags.enable(name) { |
||||
|
match err { |
||||
|
SetError::BadName(_) => { |
||||
|
// Try the target-specific flags.
|
||||
|
self.inner.enable(name)?; |
||||
|
} |
||||
|
_ => return Err(err.into()), |
||||
|
} |
||||
|
} |
||||
|
Ok(()) |
||||
|
} |
||||
|
|
||||
|
pub fn build(&self) -> T { |
||||
|
self.inner |
||||
|
.finish(settings::Flags::new(self.shared_flags.clone())) |
||||
|
} |
||||
|
|
||||
|
pub fn shared_flags(&self) -> Flags { |
||||
|
settings::Flags::new(self.shared_flags.clone()) |
||||
|
} |
||||
|
} |
@ -0,0 +1,49 @@ |
|||||
|
use cranelift_codegen::binemit; |
||||
|
use cranelift_codegen::ir; |
||||
|
use cranelift_codegen::settings; |
||||
|
use std::collections::BTreeMap; |
||||
|
use wasmtime_environ::{FlagValue, FuncIndex}; |
||||
|
|
||||
|
pub mod isa_builder; |
||||
|
pub mod obj; |
||||
|
|
||||
|
/// A record of a relocation to perform.
|
||||
|
#[derive(Debug, Clone, PartialEq, Eq)] |
||||
|
pub struct Relocation { |
||||
|
/// The relocation code.
|
||||
|
pub reloc: binemit::Reloc, |
||||
|
/// Relocation target.
|
||||
|
pub reloc_target: RelocationTarget, |
||||
|
/// The offset where to apply the relocation.
|
||||
|
pub offset: binemit::CodeOffset, |
||||
|
/// The addend to add to the relocation value.
|
||||
|
pub addend: binemit::Addend, |
||||
|
} |
||||
|
|
||||
|
/// Destination function. Can be either user function or some special one, like `memory.grow`.
|
||||
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
||||
|
pub enum RelocationTarget { |
||||
|
/// The user function index.
|
||||
|
UserFunc(FuncIndex), |
||||
|
/// A compiler-generated libcall.
|
||||
|
LibCall(ir::LibCall), |
||||
|
} |
||||
|
|
||||
|
/// Converts cranelift_codegen settings to the wasmtime_environ equivalent.
|
||||
|
pub fn clif_flags_to_wasmtime( |
||||
|
flags: impl IntoIterator<Item = settings::Value>, |
||||
|
) -> BTreeMap<String, FlagValue> { |
||||
|
flags |
||||
|
.into_iter() |
||||
|
.map(|val| (val.name.to_string(), to_flag_value(&val))) |
||||
|
.collect() |
||||
|
} |
||||
|
|
||||
|
fn to_flag_value(v: &settings::Value) -> FlagValue { |
||||
|
match v.kind() { |
||||
|
settings::SettingKind::Enum => FlagValue::Enum(v.as_enum().unwrap().into()), |
||||
|
settings::SettingKind::Num => FlagValue::Num(v.as_num().unwrap()), |
||||
|
settings::SettingKind::Bool => FlagValue::Bool(v.as_bool().unwrap()), |
||||
|
settings::SettingKind::Preset => unreachable!(), |
||||
|
} |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
fn main() { |
||||
|
if cfg!(feature = "x64") || cfg!(feature = "arm64") || cfg!(feature = "all-arch") { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if cfg!(target_arch = "x86_64") { |
||||
|
println!("cargo:rustc-cfg=feature=\"x64\""); |
||||
|
} else if cfg!(target_arch = "aarch64") { |
||||
|
println!("cargo:rustc-cfg=feature=\"arm64\""); |
||||
|
} |
||||
|
println!("cargo:rerun-if-changed=build.rs"); |
||||
|
} |
Loading…
Reference in new issue