winch: Refactoring wasmtime compiler integration pieces to share more between Cranelift and Winch (#5944)

* 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>
This commit is contained in:
Kevin Rizzo
2023-03-08 10:07:13 -05:00
committed by GitHub
parent 7d482345fb
commit 013b35ff32
35 changed files with 503 additions and 289 deletions

View File

@@ -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 }

View File

@@ -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())
}
}

View File

@@ -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!(),
}
}

View File

@@ -13,14 +13,11 @@
//! function body, the imported wasm function do not. The trampolines symbol
//! names have format "_trampoline_N", where N is `SignatureIndex`.
use crate::{CompiledFunction, RelocationTarget};
use crate::{Relocation, RelocationTarget};
use anyhow::Result;
use cranelift_codegen::binemit::Reloc;
use cranelift_codegen::ir::LibCall;
use cranelift_codegen::isa::{
unwind::{systemv, UnwindInfo},
TargetIsa,
};
use cranelift_codegen::isa::unwind::{systemv, UnwindInfo};
use cranelift_codegen::TextSectionBuilder;
use gimli::write::{Address, EhFrame, EndianVec, FrameTable, Writer};
use gimli::RunTimeEndian;
@@ -29,7 +26,7 @@ use object::{Architecture, SectionKind, SymbolFlags, SymbolKind, SymbolScope};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::ops::Range;
use wasmtime_environ::FuncIndex;
use wasmtime_environ::{Compiler, FuncIndex};
const TEXT_SECTION_NAME: &[u8] = b".text";
@@ -42,7 +39,7 @@ const TEXT_SECTION_NAME: &[u8] = b".text";
pub struct ModuleTextBuilder<'a> {
/// The target that we're compiling for, used to query target-specific
/// information as necessary.
isa: &'a dyn TargetIsa,
compiler: &'a dyn Compiler,
/// The object file that we're generating code into.
obj: &'a mut Object<'static>,
@@ -71,7 +68,11 @@ impl<'a> ModuleTextBuilder<'a> {
/// any unwinding or such information as necessary. The `num_funcs`
/// parameter indicates the number of times the `append_func` function will
/// be called. The `finish` function will panic if this contract is not met.
pub fn new(obj: &'a mut Object<'static>, isa: &'a dyn TargetIsa, num_funcs: usize) -> Self {
pub fn new(
obj: &'a mut Object<'static>,
compiler: &'a dyn Compiler,
text: Box<dyn TextSectionBuilder>,
) -> Self {
// Entire code (functions and trampolines) will be placed
// in the ".text" section.
let text_section = obj.add_section(
@@ -81,11 +82,11 @@ impl<'a> ModuleTextBuilder<'a> {
);
Self {
isa,
compiler,
obj,
text_section,
unwind_info: Default::default(),
text: isa.text_section_builder(num_funcs),
text,
libcall_symbols: HashMap::default(),
}
}
@@ -103,14 +104,17 @@ impl<'a> ModuleTextBuilder<'a> {
pub fn append_func(
&mut self,
name: &str,
func: &'a CompiledFunction,
body: &[u8],
alignment: u32,
unwind_info: Option<&'a UnwindInfo>,
relocations: &[Relocation],
resolve_reloc_target: impl Fn(FuncIndex) -> usize,
) -> (SymbolId, Range<u64>) {
let body_len = func.body.len() as u64;
let body_len = body.len() as u64;
let off = self.text.append(
true,
&func.body,
self.isa.function_alignment().max(func.alignment),
&body,
self.compiler.function_alignment().max(alignment),
);
let symbol_id = self.obj.add_symbol(Symbol {
@@ -124,11 +128,11 @@ impl<'a> ModuleTextBuilder<'a> {
flags: SymbolFlags::None,
});
if let Some(info) = &func.unwind_info {
if let Some(info) = unwind_info {
self.unwind_info.push(off, body_len, info);
}
for r in func.relocations.iter() {
for r in relocations {
match r.reloc_target {
// Relocations against user-defined functions means that this is
// a relocation against a module-local function, typically a
@@ -236,11 +240,11 @@ impl<'a> ModuleTextBuilder<'a> {
let text = self.text.finish();
self.obj
.section_mut(self.text_section)
.set_data(text, self.isa.code_section_alignment());
.set_data(text, self.compiler.page_size_align());
// Append the unwind information for all our functions, if necessary.
self.unwind_info
.append_section(self.isa, self.obj, self.text_section);
.append_section(self.compiler, self.obj, self.text_section);
}
}
@@ -333,12 +337,17 @@ impl<'a> UnwindInfoBuilder<'a> {
/// section immediately.
///
/// The `text_section`'s section identifier is passed into this function.
fn append_section(&self, isa: &dyn TargetIsa, obj: &mut Object<'_>, text_section: SectionId) {
fn append_section(
&self,
compiler: &dyn Compiler,
obj: &mut Object<'_>,
text_section: SectionId,
) {
// This write will align the text section to a page boundary and then
// return the offset at that point. This gives us the full size of the
// text section at that point, after alignment.
let text_section_size =
obj.append_section_data(text_section, &[], isa.code_section_alignment());
obj.append_section_data(text_section, &[], compiler.page_size_align());
if self.windows_xdata.len() > 0 {
assert!(self.systemv_unwind_info.len() == 0);
@@ -356,7 +365,7 @@ impl<'a> UnwindInfoBuilder<'a> {
let segment = obj.segment_name(StandardSegment::Data).to_vec();
let section_id =
obj.add_section(segment, b".eh_frame".to_vec(), SectionKind::ReadOnlyData);
self.write_systemv_unwind_info(isa, obj, section_id, text_section_size)
self.write_systemv_unwind_info(compiler, obj, section_id, text_section_size)
}
}
@@ -451,12 +460,12 @@ impl<'a> UnwindInfoBuilder<'a> {
/// bits.
fn write_systemv_unwind_info(
&self,
isa: &dyn TargetIsa,
compiler: &dyn Compiler,
obj: &mut Object<'_>,
section_id: SectionId,
text_section_size: u64,
) {
let mut cie = isa
let mut cie = compiler
.create_systemv_cie()
.expect("must be able to create a CIE for system-v unwind info");
let mut table = FrameTable::default();
@@ -473,7 +482,7 @@ impl<'a> UnwindInfoBuilder<'a> {
let fde = unwind_info.to_fde(Address::Constant(actual_offset as u64));
table.add_fde(cie_id, fde);
}
let endian = match isa.triple().endianness().unwrap() {
let endian = match compiler.triple().endianness().unwrap() {
target_lexicon::Endianness::Little => RunTimeEndian::Little,
target_lexicon::Endianness::Big => RunTimeEndian::Big,
};

View File

@@ -19,6 +19,7 @@ cranelift-codegen = { workspace = true }
cranelift-frontend = { workspace = true }
cranelift-entity = { workspace = true }
cranelift-native = { workspace = true }
wasmtime-cranelift-shared = { workspace = true }
wasmparser = { workspace = true }
target-lexicon = { workspace = true }
gimli = { workspace = true }

View File

@@ -4,15 +4,17 @@
//! 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 cranelift_codegen::{
isa::{self, OwnedTargetIsa},
CodegenResult,
};
use std::fmt;
use std::sync::Arc;
use wasmtime_environ::{CacheStore, CompilerBuilder, Setting, SettingKind};
use wasmtime_cranelift_shared::isa_builder::IsaBuilder;
use wasmtime_environ::{CacheStore, CompilerBuilder, Setting};
struct Builder {
flags: settings::Builder,
isa_flags: isa::Builder,
inner: IsaBuilder<CodegenResult<OwnedTargetIsa>>,
linkopts: LinkOptions,
cache_store: Option<Arc<dyn CacheStore>>,
}
@@ -31,22 +33,8 @@ pub struct LinkOptions {
}
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"),
inner: IsaBuilder::new(|triple| isa::lookup(triple).map_err(|e| e.into())),
linkopts: LinkOptions::default(),
cache_store: None,
})
@@ -54,11 +42,11 @@ pub fn builder() -> Box<dyn CompilerBuilder> {
impl CompilerBuilder for Builder {
fn triple(&self) -> &target_lexicon::Triple {
self.isa_flags.triple()
self.inner.triple()
}
fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {
self.isa_flags = isa::lookup(target)?;
self.inner.target(target)?;
Ok(())
}
@@ -73,37 +61,15 @@ impl CompilerBuilder for Builder {
return Ok(());
}
// ... then forward this to Cranelift
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(())
self.inner.set(name, value)
}
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(())
self.inner.enable(name)
}
fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {
let isa = self
.isa_flags
.clone()
.finish(settings::Flags::new(self.flags.clone()))?;
let isa = self.inner.build()?;
Ok(Box::new(crate::compiler::Compiler::new(
isa,
self.cache_store.clone(),
@@ -112,37 +78,22 @@ impl CompilerBuilder for Builder {
}
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()
self.inner.settings()
}
fn enable_incremental_compilation(
&mut self,
cache_store: Arc<dyn wasmtime_environ::CacheStore>,
) {
) -> Result<()> {
self.cache_store = Some(cache_store);
Ok(())
}
}
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(),
)
.field("shared_flags", &self.inner.shared_flags().to_string())
.finish()
}
}

View File

@@ -1,10 +1,8 @@
use crate::builder::LinkOptions;
use crate::debug::{DwarfSectionRelocTarget, ModuleMemoryOffset};
use crate::func_environ::FuncEnvironment;
use crate::obj::ModuleTextBuilder;
use crate::{
blank_sig, func_signature, indirect_signature, value_type, wasmtime_call_conv,
CompiledFunction, FunctionAddressMap, Relocation, RelocationTarget,
blank_sig, builder::LinkOptions, func_signature, indirect_signature, value_type,
wasmtime_call_conv, CompiledFunction, FunctionAddressMap,
};
use anyhow::{Context as _, Result};
use cranelift_codegen::ir::{
@@ -13,8 +11,8 @@ use cranelift_codegen::ir::{
use cranelift_codegen::isa::{OwnedTargetIsa, TargetIsa};
use cranelift_codegen::print_errors::pretty_error;
use cranelift_codegen::Context;
use cranelift_codegen::{settings, MachReloc, MachTrap};
use cranelift_codegen::{CompiledCode, MachSrcLoc, MachStackMap};
use cranelift_codegen::{MachReloc, MachTrap};
use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_frontend::FunctionBuilder;
use cranelift_wasm::{
@@ -30,6 +28,8 @@ use std::convert::TryFrom;
use std::mem;
use std::sync::{Arc, Mutex};
use wasmparser::{FuncValidatorAllocations, FunctionBody};
use wasmtime_cranelift_shared::obj::ModuleTextBuilder;
use wasmtime_cranelift_shared::{Relocation, RelocationTarget};
use wasmtime_environ::{
AddressMapSection, CacheStore, CompileError, FilePos, FlagValue, FunctionBodyData, FunctionLoc,
InstructionAddressMap, ModuleTranslation, ModuleTypes, PtrSize, StackMapInformation, Trap,
@@ -360,7 +360,8 @@ impl wasmtime_environ::Compiler for Compiler {
tunables: &Tunables,
resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize,
) -> Result<Vec<(SymbolId, FunctionLoc)>> {
let mut builder = ModuleTextBuilder::new(obj, &*self.isa, funcs.len());
let mut builder =
ModuleTextBuilder::new(obj, self, self.isa.text_section_builder(funcs.len()));
if self.linkopts.force_jump_veneers {
builder.force_veneers();
}
@@ -369,8 +370,15 @@ impl wasmtime_environ::Compiler for Compiler {
let mut ret = Vec::with_capacity(funcs.len());
for (i, (sym, func)) in funcs.iter().enumerate() {
let func = func.downcast_ref().unwrap();
let (sym, range) = builder.append_func(&sym, func, |idx| resolve_reloc(i, idx));
let func = func.downcast_ref::<CompiledFunction>().unwrap();
let (sym, range) = builder.append_func(
&sym,
&func.body,
func.alignment,
func.unwind_info.as_ref(),
&func.relocations,
|idx| resolve_reloc(i, idx),
);
if tunables.generate_address_map {
addrs.push(range.clone(), &func.address_map.instructions);
}
@@ -401,9 +409,23 @@ impl wasmtime_environ::Compiler for Compiler {
) -> Result<(FunctionLoc, FunctionLoc)> {
let host_to_wasm = self.host_to_wasm_trampoline(ty)?;
let wasm_to_host = self.wasm_to_host_trampoline(ty, host_fn)?;
let mut builder = ModuleTextBuilder::new(obj, &*self.isa, 2);
let (_, a) = builder.append_func("host_to_wasm", &host_to_wasm, |_| unreachable!());
let (_, b) = builder.append_func("wasm_to_host", &wasm_to_host, |_| unreachable!());
let mut builder = ModuleTextBuilder::new(obj, self, self.isa.text_section_builder(2));
let (_, a) = builder.append_func(
"host_to_wasm",
&host_to_wasm.body,
host_to_wasm.alignment,
host_to_wasm.unwind_info.as_ref(),
&host_to_wasm.relocations,
|_| unreachable!(),
);
let (_, b) = builder.append_func(
"wasm_to_host",
&wasm_to_host.body,
wasm_to_host.alignment,
wasm_to_host.unwind_info.as_ref(),
&wasm_to_host.relocations,
|_| unreachable!(),
);
let a = FunctionLoc {
start: u32::try_from(a.start).unwrap(),
length: u32::try_from(a.end - a.start).unwrap(),
@@ -420,24 +442,12 @@ impl wasmtime_environ::Compiler for Compiler {
self.isa.triple()
}
fn page_size_align(&self) -> u64 {
self.isa.code_section_alignment()
}
fn flags(&self) -> BTreeMap<String, FlagValue> {
self.isa
.flags()
.iter()
.map(|val| (val.name.to_string(), to_flag_value(&val)))
.collect()
wasmtime_cranelift_shared::clif_flags_to_wasmtime(self.isa.flags().iter())
}
fn isa_flags(&self) -> BTreeMap<String, FlagValue> {
self.isa
.isa_flags()
.iter()
.map(|val| (val.name.to_string(), to_flag_value(val)))
.collect()
wasmtime_cranelift_shared::clif_flags_to_wasmtime(self.isa.isa_flags())
}
fn is_branch_protection_enabled(&self) -> bool {
@@ -534,6 +544,14 @@ impl wasmtime_environ::Compiler for Compiler {
Ok(())
}
fn function_alignment(&self) -> u32 {
self.isa.function_alignment()
}
fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
self.isa.create_systemv_cie()
}
}
#[cfg(feature = "incremental-cache")]
@@ -599,15 +617,6 @@ fn compile_uncached<'a>(
Ok((compiled_code, code_buf))
}
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!(),
}
}
impl Compiler {
fn host_to_wasm_trampoline(&self, ty: &WasmFuncType) -> Result<CompiledFunction, CompileError> {
let isa = &*self.isa;

View File

@@ -3,12 +3,12 @@
//! This crate provides an implementation of the `wasmtime_environ::Compiler`
//! and `wasmtime_environ::CompilerBuilder` traits.
use cranelift_codegen::binemit;
use cranelift_codegen::ir;
use cranelift_codegen::isa::{unwind::UnwindInfo, CallConv, TargetIsa};
use cranelift_entity::PrimaryMap;
use cranelift_wasm::{DefinedFuncIndex, FuncIndex, WasmFuncType, WasmType};
use target_lexicon::{Architecture, CallingConvention};
use wasmtime_cranelift_shared::Relocation;
use wasmtime_environ::{
FilePos, InstructionAddressMap, ModuleTranslation, ModuleTypes, TrapInformation,
};
@@ -19,7 +19,6 @@ mod builder;
mod compiler;
mod debug;
mod func_environ;
mod obj;
type CompiledFunctions<'a> = PrimaryMap<DefinedFuncIndex, &'a CompiledFunction>;
@@ -72,28 +71,6 @@ struct FunctionAddressMap {
body_len: u32,
}
/// A record of a relocation to perform.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Relocation {
/// The relocation code.
reloc: binemit::Reloc,
/// Relocation target.
reloc_target: RelocationTarget,
/// The offset where to apply the relocation.
offset: binemit::CodeOffset,
/// The addend to add to the relocation value.
addend: binemit::Addend,
}
/// Destination function. Can be either user function or some special one, like `memory.grow`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum RelocationTarget {
/// The user function index.
UserFunc(FuncIndex),
/// A compiler-generated libcall.
LibCall(ir::LibCall),
}
/// Creates a new cranelift `Signature` with no wasm params/results for the
/// given calling convention.
///

View File

@@ -19,7 +19,7 @@ indexmap = { version = "1.0.2", features = ["serde-1"] }
thiserror = { workspace = true }
serde = { version = "1.0.94", features = ["derive"] }
log = { workspace = true }
gimli = { workspace = true }
gimli = { workspace = true, features = ["write"] }
object = { workspace = true, features = ['write_core'] }
target-lexicon = { workspace = true }
wasm-encoder = { workspace = true, optional = true }

View File

@@ -112,7 +112,9 @@ pub trait CompilerBuilder: Send + Sync + fmt::Debug {
/// Enables Cranelift's incremental compilation cache, using the given `CacheStore`
/// implementation.
fn enable_incremental_compilation(&mut self, cache_store: Arc<dyn CacheStore>);
///
/// 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<()>;
/// Builds a new [`Compiler`] object from this configuration.
fn build(&self) -> Result<Box<dyn Compiler>>;
@@ -267,7 +269,22 @@ pub trait Compiler: Send + Sync {
/// compilation target. Note that this may be an upper-bound where the
/// alignment is larger than necessary for some platforms since it may
/// depend on the platform's runtime configuration.
fn page_size_align(&self) -> u64;
fn page_size_align(&self) -> u64 {
use target_lexicon::*;
match (self.triple().operating_system, self.triple().architecture) {
(
OperatingSystem::MacOSX { .. }
| OperatingSystem::Darwin
| OperatingSystem::Ios
| OperatingSystem::Tvos,
Architecture::Aarch64(..),
) => 0x4000,
// 64 KB is the maximal page size (i.e. memory translation granule size)
// supported by the architecture and is used on some platforms.
(_, Architecture::Aarch64(..)) => 0x10000,
_ => 0x1000,
}
}
/// Returns a list of configured settings for this compiler.
fn flags(&self) -> BTreeMap<String, FlagValue>;
@@ -293,6 +310,17 @@ pub trait Compiler: Send + Sync {
translation: &ModuleTranslation<'_>,
funcs: &PrimaryMap<DefinedFuncIndex, (SymbolId, &(dyn Any + Send))>,
) -> Result<()>;
/// The function alignment required by this ISA.
fn function_alignment(&self) -> u32;
/// Creates a new System V Common Information Entry for the ISA.
///
/// Returns `None` if the ISA does not support System V unwind information.
fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
// By default, an ISA cannot create a System V CIE.
None
}
}
/// Value of a configured setting for a [`Compiler`]

View File

@@ -1623,7 +1623,7 @@ impl Config {
}
if let Some(cache_store) = &self.compiler_config.cache_store {
compiler.enable_incremental_compilation(cache_store.clone());
compiler.enable_incremental_compilation(cache_store.clone())?;
}
compiler.build()

View File

@@ -14,8 +14,11 @@ wasmtime-environ = { workspace = true }
anyhow = { workspace = true }
object = { workspace = true }
cranelift-codegen = { workspace = true }
wasmtime-cranelift-shared = { workspace = true }
wasmparser = { workspace = true }
gimli = { workspace = true }
[features]
default = ["all-arch", "component-model"]
default = ["component-model"]
component-model = ["wasmtime-environ/component-model"]
all-arch = ["winch-codegen/all-arch"]

View File

@@ -1,67 +1,54 @@
use crate::compiler::Compiler;
use anyhow::Result;
use cranelift_codegen::settings;
use anyhow::{bail, Result};
use std::sync::Arc;
use target_lexicon::Triple;
use wasmtime_cranelift_shared::isa_builder::IsaBuilder;
use wasmtime_environ::{CompilerBuilder, Setting};
use winch_codegen::isa;
use winch_codegen::{isa, TargetIsa};
/// Compiler builder.
struct Builder {
/// Target triple.
triple: Triple,
/// Shared flags builder.
shared_flags: settings::Builder,
/// ISA builder.
isa_builder: isa::Builder,
inner: IsaBuilder<Result<Box<dyn TargetIsa>>>,
}
pub fn builder() -> Box<dyn CompilerBuilder> {
let triple = Triple::host();
Box::new(Builder {
triple: triple.clone(),
shared_flags: settings::builder(),
// TODO:
// Either refactor and re-use `cranelift-native::builder()` or come up with a similar
// mechanism to lookup the host's architecture ISA and infer native flags.
isa_builder: isa::lookup(triple).expect("host architecture is not supported"),
inner: IsaBuilder::new(|triple| isa::lookup(triple).map_err(|e| e.into())),
})
}
impl CompilerBuilder for Builder {
fn triple(&self) -> &target_lexicon::Triple {
&self.triple
self.inner.triple()
}
fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {
self.triple = target;
self.inner.target(target)?;
Ok(())
}
fn set(&mut self, _name: &str, _val: &str) -> Result<()> {
Ok(())
fn set(&mut self, name: &str, value: &str) -> Result<()> {
self.inner.set(name, value)
}
fn enable(&mut self, _name: &str) -> Result<()> {
Ok(())
fn enable(&mut self, name: &str) -> Result<()> {
self.inner.enable(name)
}
fn settings(&self) -> Vec<Setting> {
vec![]
self.inner.settings()
}
fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {
let flags = settings::Flags::new(self.shared_flags.clone());
Ok(Box::new(Compiler::new(
self.isa_builder.clone().build(flags)?,
)))
let isa = self.inner.build()?;
Ok(Box::new(Compiler::new(isa)))
}
fn enable_incremental_compilation(
&mut self,
_cache_store: Arc<dyn wasmtime_environ::CacheStore>,
) {
todo!()
) -> Result<()> {
bail!("incremental compilation is not supported on this platform");
}
}

View File

@@ -43,7 +43,8 @@ impl wasmtime_environ::Compiler for Compiler {
_tunables: &Tunables,
_resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize,
) -> Result<Vec<(SymbolId, FunctionLoc)>> {
todo!()
assert!(_funcs.is_empty());
Ok(Vec::new())
}
fn emit_trampoline_obj(
@@ -59,20 +60,16 @@ impl wasmtime_environ::Compiler for Compiler {
self.isa.triple()
}
fn page_size_align(&self) -> u64 {
todo!()
}
fn flags(&self) -> std::collections::BTreeMap<String, wasmtime_environ::FlagValue> {
todo!()
wasmtime_cranelift_shared::clif_flags_to_wasmtime(self.isa.flags().iter())
}
fn isa_flags(&self) -> std::collections::BTreeMap<String, wasmtime_environ::FlagValue> {
todo!()
wasmtime_cranelift_shared::clif_flags_to_wasmtime(self.isa.isa_flags())
}
fn is_branch_protection_enabled(&self) -> bool {
todo!()
self.isa.is_branch_protection_enabled()
}
#[cfg(feature = "component-model")]
@@ -88,4 +85,12 @@ impl wasmtime_environ::Compiler for Compiler {
) -> Result<()> {
todo!()
}
fn function_alignment(&self) -> u32 {
self.isa.function_alignment()
}
fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
self.isa.create_systemv_cie()
}
}

View File

@@ -1,3 +1,4 @@
pub use builder::builder;
mod builder;
mod compiler;
pub use builder::builder;