Skip to content

Commit

Permalink
winch: Switch to using cranelift for all trampolines (#8109)
Browse files Browse the repository at this point in the history
* Switch winch over to using cranelift for all trampolines

* Fix unused code warnings

* Fix unused code warnings

prtest:full
  • Loading branch information
elliottt committed Mar 21, 2024
1 parent a1b54d9 commit d69ba34
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 786 deletions.
59 changes: 7 additions & 52 deletions crates/winch/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use wasmtime_environ::{
ModuleTranslation, ModuleTypesBuilder, PrimaryMap, TrapEncodingBuilder, VMOffsets,
WasmFunctionInfo,
};
use winch_codegen::{BuiltinFunctions, TargetIsa, TrampolineKind};
use winch_codegen::{BuiltinFunctions, TargetIsa};

/// Function compilation context.
/// This struct holds information that can be shared globally across
Expand All @@ -25,11 +25,6 @@ struct CompilationContext {

pub(crate) struct Compiler {
isa: Box<dyn TargetIsa>,

/// The trampoline compiler is only used for the component model currently, but will soon be
/// used for all winch trampolines. For now, mark it as unused to handle the situation where
/// the component-model feature is disabled.
#[allow(unused)]
trampolines: Box<dyn wasmtime_environ::Compiler>,
contexts: Mutex<Vec<CompilationContext>>,
}
Expand Down Expand Up @@ -150,22 +145,8 @@ impl wasmtime_environ::Compiler for Compiler {
types: &ModuleTypesBuilder,
index: DefinedFuncIndex,
) -> Result<Box<dyn Any + Send>, CompileError> {
let func_index = translation.module.func_index(index);
let sig = translation.module.functions[func_index].signature;
let ty = &types[sig];
let buffer = self
.isa
.compile_trampoline(&ty, TrampolineKind::ArrayToWasm(func_index))
.map_err(|e| CompileError::Codegen(format!("{:?}", e)))?;

let mut compiled_function =
CompiledFunction::new(buffer, CompiledFuncEnv {}, self.isa.function_alignment());

if self.isa.flags().unwind_info() {
self.emit_unwind_info(&mut compiled_function)?;
}

Ok(Box::new(compiled_function))
self.trampolines
.compile_array_to_wasm_trampoline(translation, types, index)
}

fn compile_native_to_wasm_trampoline(
Expand All @@ -174,42 +155,16 @@ impl wasmtime_environ::Compiler for Compiler {
types: &ModuleTypesBuilder,
index: DefinedFuncIndex,
) -> Result<Box<dyn Any + Send>, CompileError> {
let func_index = translation.module.func_index(index);
let sig = translation.module.functions[func_index].signature;
let ty = &types[sig];

let buffer = self
.isa
.compile_trampoline(ty, TrampolineKind::NativeToWasm(func_index))
.map_err(|e| CompileError::Codegen(format!("{:?}", e)))?;

let mut compiled_function =
CompiledFunction::new(buffer, CompiledFuncEnv {}, self.isa.function_alignment());

if self.isa.flags().unwind_info() {
self.emit_unwind_info(&mut compiled_function)?;
}

Ok(Box::new(compiled_function))
self.trampolines
.compile_native_to_wasm_trampoline(translation, types, index)
}

fn compile_wasm_to_native_trampoline(
&self,
wasm_func_ty: &wasmtime_environ::WasmFuncType,
) -> Result<Box<dyn Any + Send>, CompileError> {
let buffer = self
.isa
.compile_trampoline(wasm_func_ty, TrampolineKind::WasmToNative)
.map_err(|e| CompileError::Codegen(format!("{:?}", e)))?;

let mut compiled_function =
CompiledFunction::new(buffer, CompiledFuncEnv {}, self.isa.function_alignment());

if self.isa.flags().unwind_info() {
self.emit_unwind_info(&mut compiled_function)?;
}

Ok(Box::new(compiled_function))
self.trampolines
.compile_wasm_to_native_trampoline(wasm_func_ty)
}

fn append_code(
Expand Down
59 changes: 5 additions & 54 deletions winch/codegen/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,60 +88,6 @@ pub(crate) fn vmctx_types<A: ABI>() -> [WasmValType; 2] {
[A::ptr_type(), A::ptr_type()]
}

/// Returns an [ABISig] for the array calling convention.
/// The signature looks like:
/// ```ignore
/// unsafe extern "C" fn(
/// callee_vmctx: *mut VMOpaqueContext,
/// caller_vmctx: *mut VMOpaqueContext,
/// values_ptr: *mut ValRaw,
/// values_len: usize,
/// )
/// ```
pub(crate) fn array_sig<A: ABI>(call_conv: &CallingConvention) -> ABISig {
let params = [A::ptr_type(), A::ptr_type(), A::ptr_type(), A::ptr_type()];
A::sig_from(&params, &[], call_conv)
}

/// Returns an [ABISig] that follows a variation of the system's
/// calling convention.
/// The main difference between the flavor of the returned signature
/// and the vanilla signature is how multiple values are returned.
/// Multiple returns are handled following Wasmtime's expectations:
/// * A single value is returned via a register according to the calling
/// convention.
/// * More than one values are returned via a return pointer.
/// These variations look like:
///
/// Single return value.
///
/// ```ignore
/// unsafe extern "C" fn(
/// callee_vmctx: *mut VMOpaqueContext,
/// caller_vmctx: *mut VMOpaqueContext,
/// // rest of parameters
/// ) -> // single result
/// ```
///
/// Multiple return values.
///
/// ```ignore
/// unsafe extern "C" fn(
/// callee_vmctx: *mut VMOpaqueContext,
/// caller_vmctx: *mut VMOpaqueContext,
/// // rest of parameters
/// retptr: *mut (), // 2+ results
/// ) -> // first result
/// ```
pub(crate) fn native_sig<A: ABI>(ty: &WasmFuncType, call_conv: &CallingConvention) -> ABISig {
// 6 is used semi-arbitrarily here, we can modify as we see fit.
let mut params: SmallVec<[WasmValType; 6]> = SmallVec::new();
params.extend_from_slice(&vmctx_types::<A>());
params.extend_from_slice(ty.params());

A::sig_from(&params, ty.returns(), call_conv)
}

/// Trait implemented by a specific ISA and used to provide
/// information about alignment, parameter passing, usage of
/// specific registers, etc.
Expand All @@ -156,6 +102,7 @@ pub(crate) trait ABI {
fn arg_base_offset() -> u8;

/// The offset to the return address, relative to the frame pointer.
#[allow(unused)]
fn ret_addr_offset() -> u8;

/// Construct the ABI-specific signature from a WebAssembly
Expand Down Expand Up @@ -204,9 +151,11 @@ pub(crate) trait ABI {
}

/// Returns the frame pointer register.
#[allow(unused)]
fn fp_reg() -> Reg;

/// Returns the stack pointer register.
#[allow(unused)]
fn sp_reg() -> Reg;

/// Returns the pinned register used to hold
Expand All @@ -215,6 +164,7 @@ pub(crate) trait ABI {

/// Returns the callee-saved registers for the given
/// calling convention.
#[allow(unused)]
fn callee_saved_regs(call_conv: &CallingConvention) -> SmallVec<[(Reg, OperandSize); 18]>;

/// The size, in bytes, of each stack slot used for stack parameter passing.
Expand Down Expand Up @@ -609,6 +559,7 @@ impl ABIParams {
}

/// Get the [`ABIOperand`] param in the nth position.
#[allow(unused)]
pub fn get(&self, n: usize) -> Option<&ABIOperand> {
self.operands.inner.get(n)
}
Expand Down
10 changes: 1 addition & 9 deletions winch/codegen/src/isa/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
regalloc::RegAlloc,
regset::RegBitSet,
stack::Stack,
BuiltinFunctions, TrampolineKind,
BuiltinFunctions,
};
use anyhow::Result;
use cranelift_codegen::settings::{self, Flags};
Expand Down Expand Up @@ -136,14 +136,6 @@ impl TargetIsa for Aarch64 {
32
}

fn compile_trampoline(
&self,
_ty: &WasmFuncType,
_kind: TrampolineKind,
) -> Result<MachBufferFinalized<Final>> {
todo!()
}

fn emit_unwind_info(
&self,
_result: &MachBufferFinalized<Final>,
Expand Down
1 change: 1 addition & 0 deletions winch/codegen/src/isa/aarch64/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub(crate) const ALL_GPR: u32 = u32::MAX & !NON_ALLOCATABLE_GPR;
/// This function will return the set of registers that need to be saved
/// according to the system ABI and that are known not to be saved during the
/// prologue emission.
#[allow(unused)]
pub(crate) fn callee_saved() -> SmallVec<[(Reg, OperandSize); 18]> {
use OperandSize::*;
let regs: SmallVec<[_; 18]> = smallvec![
Expand Down
12 changes: 1 addition & 11 deletions winch/codegen/src/isa/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BuiltinFunctions, TrampolineKind};
use crate::BuiltinFunctions;
use anyhow::{anyhow, Result};
use core::fmt::Formatter;
use cranelift_codegen::isa::unwind::{UnwindInfo, UnwindInfoKind};
Expand Down Expand Up @@ -203,16 +203,6 @@ pub trait TargetIsa: Send + Sync {
/// See `cranelift_codegen::isa::TargetIsa::function_alignment`.
fn function_alignment(&self) -> u32;

/// Compile a trampoline kind.
///
/// This function, internally dispatches to the right trampoline to emit
/// depending on the `kind` paramter.
fn compile_trampoline(
&self,
ty: &WasmFuncType,
kind: TrampolineKind,
) -> Result<MachBufferFinalized<Final>>;

/// Returns the pointer width of the ISA in bytes.
fn pointer_bytes(&self) -> u8 {
let width = self.triple().pointer_width().unwrap();
Expand Down
32 changes: 0 additions & 32 deletions winch/codegen/src/isa/x64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::isa::x64::masm::MacroAssembler as X64Masm;
use crate::masm::MacroAssembler;
use crate::regalloc::RegAlloc;
use crate::stack::Stack;
use crate::trampoline::{Trampoline, TrampolineKind};
use crate::{
isa::{Builder, TargetIsa},
regset::RegBitSet,
Expand Down Expand Up @@ -149,37 +148,6 @@ impl TargetIsa for X64 {
16
}

fn compile_trampoline(
&self,
ty: &WasmFuncType,
kind: TrampolineKind,
) -> Result<MachBufferFinalized<Final>> {
use TrampolineKind::*;

let mut masm = X64Masm::new(
self.pointer_bytes(),
self.shared_flags.clone(),
self.isa_flags.clone(),
);
let call_conv = self.wasmtime_call_conv();

let trampoline = Trampoline::new(
&mut masm,
regs::scratch(),
regs::argv(),
&call_conv,
self.pointer_bytes(),
);

match kind {
ArrayToWasm(idx) => trampoline.emit_array_to_wasm(ty, idx)?,
NativeToWasm(idx) => trampoline.emit_native_to_wasm(ty, idx)?,
WasmToNative => trampoline.emit_wasm_to_native(ty)?,
}

Ok(masm.finalize())
}

fn emit_unwind_info(
&self,
buffer: &MachBufferFinalized<Final>,
Expand Down
2 changes: 0 additions & 2 deletions winch/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ mod masm;
mod regalloc;
mod regset;
mod stack;
mod trampoline;
pub use trampoline::TrampolineKind;
mod visitor;

0 comments on commit d69ba34

Please sign in to comment.