Browse Source

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>
pull/5973/head
Kevin Rizzo 2 years ago
committed by GitHub
parent
commit
013b35ff32
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      Cargo.lock
  2. 1
      Cargo.toml
  3. 2
      cranelift/codegen/meta/src/gen_settings.rs
  4. 48
      cranelift/codegen/src/isa/mod.rs
  5. 2
      cranelift/codegen/src/isa/riscv64/inst/emit_tests.rs
  6. 2
      cranelift/codegen/src/isa/s390x/inst/emit_tests.rs
  7. 2
      cranelift/codegen/src/isa/x64/inst/emit_tests.rs
  8. 2
      cranelift/codegen/src/isa/x64/mod.rs
  9. 4
      cranelift/codegen/src/lib.rs
  10. 2
      cranelift/codegen/src/machinst/buffer.rs
  11. 4
      cranelift/codegen/src/settings.rs
  12. 45
      cranelift/native/src/lib.rs
  13. 18
      crates/cranelift-shared/Cargo.toml
  14. 106
      crates/cranelift-shared/src/isa_builder.rs
  15. 49
      crates/cranelift-shared/src/lib.rs
  16. 57
      crates/cranelift-shared/src/obj.rs
  17. 1
      crates/cranelift/Cargo.toml
  18. 83
      crates/cranelift/src/builder.rs
  19. 77
      crates/cranelift/src/compiler.rs
  20. 25
      crates/cranelift/src/lib.rs
  21. 2
      crates/environ/Cargo.toml
  22. 32
      crates/environ/src/compilation.rs
  23. 2
      crates/wasmtime/src/config.rs
  24. 5
      crates/winch/Cargo.toml
  25. 47
      crates/winch/src/builder.rs
  26. 21
      crates/winch/src/compiler.rs
  27. 3
      crates/winch/src/lib.rs
  28. 1
      scripts/publish.rs
  29. 1
      winch/codegen/Cargo.toml
  30. 12
      winch/codegen/build.rs
  31. 37
      winch/codegen/src/isa/aarch64/mod.rs
  32. 46
      winch/codegen/src/isa/mod.rs
  33. 31
      winch/codegen/src/isa/x64/mod.rs
  34. 2
      winch/filetests/src/lib.rs
  35. 2
      winch/src/compile.rs

18
Cargo.lock

@ -3592,6 +3592,20 @@ dependencies = [
"target-lexicon",
"thiserror",
"wasmparser",
"wasmtime-cranelift-shared",
"wasmtime-environ",
]
[[package]]
name = "wasmtime-cranelift-shared"
version = "8.0.0"
dependencies = [
"anyhow",
"cranelift-codegen",
"cranelift-native",
"gimli",
"object",
"target-lexicon",
"wasmtime-environ",
]
@ -3836,8 +3850,11 @@ version = "8.0.0"
dependencies = [
"anyhow",
"cranelift-codegen",
"gimli",
"object",
"target-lexicon",
"wasmparser",
"wasmtime-cranelift-shared",
"wasmtime-environ",
"winch-codegen",
]
@ -3993,6 +4010,7 @@ version = "0.6.0"
dependencies = [
"anyhow",
"cranelift-codegen",
"gimli",
"regalloc2",
"smallvec",
"target-lexicon",

1
Cargo.toml

@ -118,6 +118,7 @@ wasmtime = { path = "crates/wasmtime", version = "8.0.0", default-features = fal
wasmtime-cache = { path = "crates/cache", version = "=8.0.0" }
wasmtime-cli-flags = { path = "crates/cli-flags", version = "=8.0.0" }
wasmtime-cranelift = { path = "crates/cranelift", version = "=8.0.0" }
wasmtime-cranelift-shared = { path = "crates/cranelift-shared", version = "=8.0.0" }
wasmtime-environ = { path = "crates/environ", version = "=8.0.0" }
wasmtime-fiber = { path = "crates/fiber", version = "=8.0.0" }
wasmtime-types = { path = "crates/types", version = "8.0.0" }

2
cranelift/codegen/meta/src/gen_settings.rs

@ -21,7 +21,7 @@ pub(crate) enum ParentGroup {
fn gen_constructor(group: &SettingGroup, parent: ParentGroup, fmt: &mut Formatter) {
let args = match parent {
ParentGroup::None => "builder: Builder",
ParentGroup::Shared => "shared: &settings::Flags, builder: Builder",
ParentGroup::Shared => "shared: &settings::Flags, builder: &Builder",
};
fmtln!(fmt, "impl Flags {");
fmt.indent(|fmt| {

48
cranelift/codegen/src/isa/mod.rs

@ -146,16 +146,34 @@ impl fmt::Display for LookupError {
/// The type of a polymorphic TargetISA object which is 'static.
pub type OwnedTargetIsa = Arc<dyn TargetIsa>;
/// Type alias of `IsaBuilder` used for building Cranelift's ISAs.
pub type Builder = IsaBuilder<CodegenResult<OwnedTargetIsa>>;
/// Builder for a `TargetIsa`.
/// Modify the ISA-specific settings before creating the `TargetIsa` trait object with `finish`.
#[derive(Clone)]
pub struct Builder {
pub struct IsaBuilder<T> {
triple: Triple,
setup: settings::Builder,
constructor: fn(Triple, settings::Flags, settings::Builder) -> CodegenResult<OwnedTargetIsa>,
constructor: fn(Triple, settings::Flags, &settings::Builder) -> T,
}
impl Builder {
impl<T> IsaBuilder<T> {
/// Creates a new ISA-builder from its components, namely the `triple` for
/// the ISA, the ISA-specific settings builder, and a final constructor
/// function to generate the ISA from its components.
pub fn new(
triple: Triple,
setup: settings::Builder,
constructor: fn(Triple, settings::Flags, &settings::Builder) -> T,
) -> Self {
IsaBuilder {
triple,
setup,
constructor,
}
}
/// Gets the triple for the builder.
pub fn triple(&self) -> &Triple {
&self.triple
@ -172,12 +190,12 @@ impl Builder {
/// flags are inconsistent or incompatible: for example, some
/// platform-independent features, like general SIMD support, may
/// need certain ISA extensions to be enabled.
pub fn finish(self, shared_flags: settings::Flags) -> CodegenResult<OwnedTargetIsa> {
(self.constructor)(self.triple, shared_flags, self.setup)
pub fn finish(&self, shared_flags: settings::Flags) -> T {
(self.constructor)(self.triple.clone(), shared_flags, &self.setup)
}
}
impl settings::Configurable for Builder {
impl<T> settings::Configurable for IsaBuilder<T> {
fn set(&mut self, name: &str, value: &str) -> SetResult<()> {
self.setup.set(name, value)
}
@ -339,24 +357,6 @@ impl<'a> dyn TargetIsa + 'a {
}
}
/// Returns the code (text) section alignment for this ISA.
pub fn code_section_alignment(&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 the minimum symbol alignment for this ISA.
pub fn symbol_alignment(&self) -> u64 {
match self.triple().architecture {

2
cranelift/codegen/src/isa/riscv64/inst/emit_tests.rs

@ -2095,7 +2095,7 @@ fn make_test_flags() -> (settings::Flags, super::super::riscv_settings::Flags) {
let b = settings::builder();
let flags = settings::Flags::new(b.clone());
let b2 = super::super::riscv_settings::builder();
let isa_flags = super::super::riscv_settings::Flags::new(&flags, b2);
let isa_flags = super::super::riscv_settings::Flags::new(&flags, &b2);
(flags, isa_flags)
}

2
cranelift/codegen/src/isa/s390x/inst/emit_tests.rs

@ -13362,7 +13362,7 @@ fn test_s390x_binemit() {
use crate::settings::Configurable;
let mut isa_flag_builder = s390x_settings::builder();
isa_flag_builder.enable("arch13").unwrap();
let isa_flags = s390x_settings::Flags::new(&flags, isa_flag_builder);
let isa_flags = s390x_settings::Flags::new(&flags, &isa_flag_builder);
let emit_info = EmitInfo::new(isa_flags);
for (insn, expected_encoding, expected_printing) in insns {

2
cranelift/codegen/src/isa/x64/inst/emit_tests.rs

@ -5155,7 +5155,7 @@ fn test_x64_emit() {
isa_flag_builder.enable("has_avx512f").unwrap();
isa_flag_builder.enable("has_avx512vbmi").unwrap();
isa_flag_builder.enable("has_avx512vl").unwrap();
let isa_flags = x64::settings::Flags::new(&flags, isa_flag_builder);
let isa_flags = x64::settings::Flags::new(&flags, &isa_flag_builder);
let emit_info = EmitInfo::new(flags, isa_flags);
for (insn, expected_encoding, expected_printing) in insns {

2
cranelift/codegen/src/isa/x64/mod.rs

@ -212,7 +212,7 @@ pub(crate) fn isa_builder(triple: Triple) -> IsaBuilder {
fn isa_constructor(
triple: Triple,
shared_flags: Flags,
builder: shared_settings::Builder,
builder: &shared_settings::Builder,
) -> CodegenResult<OwnedTargetIsa> {
let isa_flags = x64_settings::Flags::new(&shared_flags, builder);

4
cranelift/codegen/src/lib.rs

@ -88,7 +88,9 @@ pub mod verifier;
pub mod write;
pub use crate::entity::packed_option;
pub use crate::machinst::buffer::{MachCallSite, MachReloc, MachSrcLoc, MachStackMap, MachTrap};
pub use crate::machinst::buffer::{
MachCallSite, MachReloc, MachSrcLoc, MachStackMap, MachTextSectionBuilder, MachTrap,
};
pub use crate::machinst::{
CompiledCode, Final, MachBuffer, MachBufferFinalized, MachInst, MachInstEmit, Reg,
TextSectionBuilder, Writable,

2
cranelift/codegen/src/machinst/buffer.rs

@ -1610,6 +1610,8 @@ pub struct MachTextSectionBuilder<I: VCodeInst> {
}
impl<I: VCodeInst> MachTextSectionBuilder<I> {
/// Creates a new text section builder which will have `num_funcs` functions
/// pushed into it.
pub fn new(num_funcs: usize) -> MachTextSectionBuilder<I> {
let mut buf = MachBuffer::new();
buf.reserve_labels_for_blocks(num_funcs);

4
cranelift/codegen/src/settings.rs

@ -161,9 +161,9 @@ impl Builder {
}
/// Extract contents of builder once everything is configured.
pub fn state_for(self, name: &str) -> Box<[u8]> {
pub fn state_for(&self, name: &str) -> &[u8] {
assert_eq!(name, self.template.name);
self.bytes
&self.bytes
}
/// Iterates the available settings in the builder.

45
cranelift/native/src/lib.rs

@ -24,6 +24,7 @@
)]
use cranelift_codegen::isa;
use cranelift_codegen::settings::Configurable;
use target_lexicon::Triple;
/// Return an `isa` builder configured for the current host
@ -45,19 +46,26 @@ pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'
isa::LookupError::SupportDisabled => "support for architecture disabled at compile time",
isa::LookupError::Unsupported => "unsupported architecture",
})?;
if infer_native_flags {
self::infer_native_flags(&mut isa_builder)?;
}
Ok(isa_builder)
}
/// Return an `isa` builder configured for the current host
/// machine, or `Err(())` if the host machine is not supported
/// in the current configuration.
///
/// Selects the given backend variant specifically; this is
/// useful when more than oen backend exists for a given target
/// (e.g., on x86-64).
pub fn infer_native_flags(isa_builder: &mut dyn Configurable) -> Result<(), &'static str> {
#[cfg(target_arch = "x86_64")]
{
use cranelift_codegen::settings::Configurable;
if !std::is_x86_feature_detected!("sse2") {
return Err("x86 support requires SSE2");
}
if !infer_native_flags {
return Ok(isa_builder);
}
// These are temporarily enabled by default (see #3810 for
// more) so that a default-constructed `Flags` can work with
// default Wasmtime features. Otherwise, the user must
@ -123,12 +131,6 @@ pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'
#[cfg(target_arch = "aarch64")]
{
use cranelift_codegen::settings::Configurable;
if !infer_native_flags {
return Ok(isa_builder);
}
if std::arch::is_aarch64_feature_detected!("lse") {
isa_builder.enable("has_lse").unwrap();
}
@ -149,12 +151,6 @@ pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'
// we use getauxval from the libc crate directly.
#[cfg(all(target_arch = "s390x", target_os = "linux"))]
{
use cranelift_codegen::settings::Configurable;
if !infer_native_flags {
return Ok(isa_builder);
}
let v = unsafe { libc::getauxval(libc::AT_HWCAP) };
const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768;
if (v & HWCAP_S390X_VXRS_EXT2) != 0 {
@ -169,12 +165,6 @@ pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'
// getauxval from the libc crate directly as a temporary measure.
#[cfg(all(target_arch = "riscv64", target_os = "linux"))]
{
use cranelift_codegen::settings::Configurable;
if !infer_native_flags {
return Ok(isa_builder);
}
let v = unsafe { libc::getauxval(libc::AT_HWCAP) };
const HWCAP_RISCV_EXT_A: libc::c_ulong = 1 << (b'a' - b'a');
@ -217,12 +207,7 @@ pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'
// won't have a bit associated with them. The Linux kernel
// is currently working on a new way to query the extensions.
}
// squelch warnings about unused mut/variables on some platforms.
drop(&mut isa_builder);
drop(infer_native_flags);
Ok(isa_builder)
Ok(())
}
#[cfg(test)]

18
crates/cranelift-shared/Cargo.toml

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

106
crates/cranelift-shared/src/isa_builder.rs

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

49
crates/cranelift-shared/src/lib.rs

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

57
crates/cranelift/src/obj.rs → crates/cranelift-shared/src/obj.rs

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

1
crates/cranelift/Cargo.toml

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

83
crates/cranelift/src/builder.rs

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

77
crates/cranelift/src/compiler.rs

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

25
crates/cranelift/src/lib.rs

@ -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.
///

2
crates/environ/Cargo.toml

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

32
crates/environ/src/compilation.rs

@ -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`]

2
crates/wasmtime/src/config.rs

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

5
crates/winch/Cargo.toml

@ -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"]

47
crates/winch/src/builder.rs

@ -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");
}
}

21
crates/winch/src/compiler.rs

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

3
crates/winch/src/lib.rs

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

1
scripts/publish.rs

@ -52,6 +52,7 @@ const CRATES_TO_PUBLISH: &[&str] = &[
"wasmtime-fiber",
"wasmtime-environ",
"wasmtime-runtime",
"wasmtime-cranelift-shared",
"wasmtime-cranelift",
"wasmtime-jit",
"wasmtime-cache",

1
winch/codegen/Cargo.toml

@ -18,6 +18,7 @@ target-lexicon = { workspace = true, features = ["std"] }
# by Cranelift and Winch.
cranelift-codegen = { workspace = true }
regalloc2 = "0.6.0"
gimli = { workspace = true }
[features]
x64 = ["cranelift-codegen/x86"]

12
winch/codegen/build.rs

@ -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");
}

37
winch/codegen/src/isa/aarch64/mod.rs

@ -10,9 +10,9 @@ use crate::{
stack::Stack,
};
use anyhow::Result;
use cranelift_codegen::{
isa::aarch64::settings as aarch64_settings, settings::Flags, Final, MachBufferFinalized,
};
use cranelift_codegen::settings::{self, Flags};
use cranelift_codegen::{isa::aarch64::settings as aarch64_settings, Final, MachBufferFinalized};
use cranelift_codegen::{MachTextSectionBuilder, TextSectionBuilder};
use masm::MacroAssembler as Aarch64Masm;
use target_lexicon::Triple;
use wasmparser::{FuncType, FuncValidator, FunctionBody, ValidatorResources};
@ -25,15 +25,15 @@ mod regs;
/// Create an ISA from the given triple.
pub(crate) fn isa_builder(triple: Triple) -> Builder {
Builder {
Builder::new(
triple,
settings: aarch64_settings::builder(),
constructor: |triple, shared_flags, settings| {
aarch64_settings::builder(),
|triple, shared_flags, settings| {
let isa_flags = aarch64_settings::Flags::new(&shared_flags, settings);
let isa = Aarch64::new(triple, shared_flags, isa_flags);
Ok(Box::new(isa))
},
}
)
}
/// Aarch64 ISA.
@ -68,6 +68,18 @@ impl TargetIsa for Aarch64 {
&self.triple
}
fn flags(&self) -> &settings::Flags {
&self.shared_flags
}
fn isa_flags(&self) -> Vec<settings::Value> {
self.isa_flags.iter().collect()
}
fn is_branch_protection_enabled(&self) -> bool {
self.isa_flags.use_bti()
}
fn compile_function(
&self,
sig: &FuncType,
@ -88,4 +100,15 @@ impl TargetIsa for Aarch64 {
codegen.emit(&mut body, validator)?;
Ok(masm.finalize())
}
fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {
Box::new(MachTextSectionBuilder::<
cranelift_codegen::isa::aarch64::inst::Inst,
>::new(num_funcs))
}
fn function_alignment(&self) -> u32 {
// See `cranelift_codegen::isa::TargetIsa::function_alignment`.
32
}
}

46
winch/codegen/src/isa/mod.rs

@ -1,6 +1,8 @@
use anyhow::{anyhow, Result};
use core::fmt::Formatter;
use cranelift_codegen::{isa::CallConv, settings, Final, MachBufferFinalized};
use cranelift_codegen::isa::{CallConv, IsaBuilder};
use cranelift_codegen::settings;
use cranelift_codegen::{Final, MachBufferFinalized, TextSectionBuilder};
use std::{
error,
fmt::{self, Debug, Display},
@ -29,24 +31,7 @@ macro_rules! isa_builder {
}};
}
/// The target ISA builder.
#[derive(Clone)]
pub struct Builder {
/// The target triple.
triple: Triple,
/// The ISA settings builder.
settings: settings::Builder,
/// The Target ISA constructor.
constructor: fn(Triple, settings::Flags, settings::Builder) -> Result<Box<dyn TargetIsa>>,
}
impl Builder {
/// Create a TargetIsa by combining ISA-specific settings with the provided
/// shared flags.
pub fn build(self, shared_flags: settings::Flags) -> Result<Box<dyn TargetIsa>> {
(self.constructor)(self.triple, shared_flags, self.settings)
}
}
pub type Builder = IsaBuilder<Result<Box<dyn TargetIsa>>>;
/// Look for an ISA builder for the given target triple.
pub fn lookup(triple: Triple) -> Result<Builder> {
@ -92,6 +77,17 @@ pub trait TargetIsa: Send + Sync {
/// Get the target triple of the ISA.
fn triple(&self) -> &Triple;
/// Get the ISA-independent flags that were used to make this trait object.
fn flags(&self) -> &settings::Flags;
/// Get the ISA-dependent flag values that were used to make this trait object.
fn isa_flags(&self) -> Vec<settings::Value>;
/// Get a flag indicating whether branch protection is enabled.
fn is_branch_protection_enabled(&self) -> bool {
false
}
fn compile_function(
&self,
sig: &FuncType,
@ -108,6 +104,18 @@ pub trait TargetIsa: Send + Sync {
fn endianness(&self) -> target_lexicon::Endianness {
self.triple().endianness().unwrap()
}
/// See `cranelift_codegen::isa::TargetIsa::create_systemv_cie`.
fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
// By default, an ISA cannot create a System V CIE.
None
}
/// See `cranelift_codegen::isa::TargetIsa::text_section_builder`.
fn text_section_builder(&self, num_labeled_funcs: usize) -> Box<dyn TextSectionBuilder>;
/// See `cranelift_codegen::isa::TargetIsa::function_alignment`.
fn function_alignment(&self) -> u32;
}
impl Debug for &dyn TargetIsa {

31
winch/codegen/src/isa/x64/mod.rs

@ -10,9 +10,9 @@ use crate::{
regset::RegSet,
};
use anyhow::Result;
use cranelift_codegen::{
isa::x64::settings as x64_settings, settings::Flags, Final, MachBufferFinalized,
};
use cranelift_codegen::settings::{self, Flags};
use cranelift_codegen::{isa::x64::settings as x64_settings, Final, MachBufferFinalized};
use cranelift_codegen::{MachTextSectionBuilder, TextSectionBuilder};
use target_lexicon::Triple;
use wasmparser::{FuncType, FuncValidator, FunctionBody, ValidatorResources};
@ -30,17 +30,17 @@ mod regs;
/// Create an ISA builder.
pub(crate) fn isa_builder(triple: Triple) -> Builder {
Builder {
Builder::new(
triple,
settings: x64_settings::builder(),
constructor: |triple, shared_flags, settings| {
x64_settings::builder(),
|triple, shared_flags, settings| {
// TODO: Once enabling/disabling flags is allowed, and once features like SIMD are supported
// ensure compatibility between shared flags and ISA flags.
let isa_flags = x64_settings::Flags::new(&shared_flags, settings);
let isa = X64::new(triple, shared_flags, isa_flags);
Ok(Box::new(isa))
},
}
)
}
/// x64 ISA.
@ -73,6 +73,14 @@ impl TargetIsa for X64 {
&self.triple
}
fn flags(&self) -> &settings::Flags {
&self.shared_flags
}
fn isa_flags(&self) -> Vec<settings::Value> {
self.isa_flags.iter().collect()
}
fn compile_function(
&self,
sig: &FuncType,
@ -94,4 +102,13 @@ impl TargetIsa for X64 {
Ok(masm.finalize())
}
fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {
Box::new(MachTextSectionBuilder::<cranelift_codegen::isa::x64::Inst>::new(num_funcs))
}
fn function_alignment(&self) -> u32 {
// See `cranelift_codegen`'s value of this for more information.
16
}
}

2
winch/filetests/src/lib.rs

@ -94,7 +94,7 @@ mod test {
let shared_flags = settings::Flags::new(settings::builder());
let isa_builder = lookup(triple).unwrap();
let isa = isa_builder.build(shared_flags).unwrap();
let isa = isa_builder.finish(shared_flags).unwrap();
let mut validator = Validator::new();
let parser = WasmParser::new(0);

2
winch/src/compile.rs

@ -27,7 +27,7 @@ pub fn run(opt: &Options) -> Result<()> {
let triple = Triple::from_str(&opt.target)?;
let shared_flags = settings::Flags::new(settings::builder());
let isa_builder = lookup(triple)?;
let isa = isa_builder.build(shared_flags)?;
let isa = isa_builder.finish(shared_flags)?;
let mut validator = Validator::new();
let parser = WasmParser::new(0);
let mut types = Default::default();

Loading…
Cancel
Save