Skip to content

Commit

Permalink
Switch winch over to using cranelift for all trampolines
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed Mar 13, 2024
1 parent 18a481c commit c2d6f3e
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 732 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
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
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 c2d6f3e

Please sign in to comment.