Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
652f30f
Enable the native target by default in winch
alexcrichton Feb 14, 2023
941c6d9
Refactor Cranelift's ISA builder to share more with Winch
alexcrichton Feb 14, 2023
da00f8b
Moving compiler shared components to a separate crate
KevinRizzoTO Mar 1, 2023
b62b77d
Restore native flag inference in compiler building
alexcrichton Feb 14, 2023
178e641
Move `Compiler::page_size_align` into wasmtime-environ
alexcrichton Feb 14, 2023
baba1a9
Fill out `Compiler::{flags, isa_flags}` for Winch
alexcrichton Feb 14, 2023
a67925c
Plumb the `is_branch_protection_enabled` flag for Winch
alexcrichton Feb 14, 2023
08011af
Moving executable creation to shared compiler crate
KevinRizzoTO Mar 1, 2023
1740110
Adding builder back in and removing from shared crate
KevinRizzoTO Mar 2, 2023
747a48f
Refactoring the shared pieces for the `CompilerBuilder`
KevinRizzoTO Mar 2, 2023
1bcae3d
Documentation updates, and renaming the finish method
KevinRizzoTO Mar 6, 2023
f411070
Adding back in a default temporarily to pass tests, and removing some…
KevinRizzoTO Mar 6, 2023
cae50f8
Fixing winch tests with wrong method name
KevinRizzoTO Mar 6, 2023
fe20812
Removing unused imports from codegen shared crate
KevinRizzoTO Mar 7, 2023
07ff9b6
Apply documentation formatting updates
KevinRizzoTO Mar 7, 2023
c53501c
Adding back in cranelift_native flag inferring
KevinRizzoTO Mar 7, 2023
5d16640
Adding new shared crate to publish list
KevinRizzoTO Mar 7, 2023
4306f49
Adding write feature to pass cargo check
KevinRizzoTO Mar 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/meta/src/gen_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
48 changes: 24 additions & 24 deletions cranelift/codegen/src/isa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Comment thread
KevinRizzoTO marked this conversation as resolved.
(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)
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/riscv64/inst/emit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/s390x/inst/emit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/x64/inst/emit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/x64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion cranelift/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions cranelift/codegen/src/machinst/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions cranelift/codegen/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 15 additions & 30 deletions cranelift/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
Expand All @@ -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 {
Expand All @@ -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');
Expand Down Expand Up @@ -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)]
Expand Down
18 changes: 18 additions & 0 deletions crates/cranelift-shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 }
Loading