Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

winch: Implement new trampolines #6358

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

63 changes: 52 additions & 11 deletions crates/winch/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use wasmparser::FuncValidatorAllocations;
use wasmtime_cranelift_shared::{CompiledFunction, ModuleTextBuilder};
use wasmtime_environ::{
CompileError, DefinedFuncIndex, FilePos, FuncIndex, FunctionBodyData, FunctionLoc,
ModuleTranslation, ModuleTypes, PrimaryMap, Tunables, WasmFunctionInfo,
ModuleTranslation, ModuleTypes, PrimaryMap, Tunables, VMOffsets, WasmFunctionInfo,
};
use winch_codegen::TargetIsa;
use winch_codegen::{TargetIsa, TrampolineKind};
use winch_environ::FuncEnv;

pub(crate) struct Compiler {
Expand Down Expand Up @@ -67,10 +67,11 @@ impl wasmtime_environ::Compiler for Compiler {
.unwrap(),
);
let mut validator = validator.into_validator(self.take_allocations());
let env = FuncEnv::new(&translation.module, translation.get_types(), &self.isa);
let vmoffsets = VMOffsets::new(self.isa.pointer_bytes(), &translation.module);
let env = FuncEnv::new(&translation.module, translation.get_types());
let buffer = self
.isa
.compile_function(&sig, &body, &env, &mut validator)
.compile_function(&sig, &body, &vmoffsets, &env, &mut validator)
.map_err(|e| CompileError::Codegen(format!("{e:?}")));
self.save_allocations(validator.into_allocations());
let buffer = buffer?;
Expand All @@ -92,8 +93,21 @@ impl wasmtime_environ::Compiler for Compiler {
types: &ModuleTypes,
index: DefinedFuncIndex,
) -> Result<Box<dyn Any + Send>, CompileError> {
let _ = (translation, types, index);
todo!()
let func_index = translation.module.func_index(index);
let sig = translation.module.functions[func_index].signature;
let ty = &types[sig];
let wasm_ty = wasmparser::FuncType::new(
ty.params().iter().copied().map(Into::into),
ty.returns().iter().copied().map(Into::into),
);
let buffer = self
.isa
.compile_trampoline(&wasm_ty, TrampolineKind::ArrayToWasm(func_index))
.map_err(|e| CompileError::Codegen(format!("{:?}", e)))?;
let compiled_function =
CompiledFunction::new(buffer, CompiledFuncEnv {}, self.isa.function_alignment());

Ok(Box::new(compiled_function))
}

fn compile_native_to_wasm_trampoline(
Expand All @@ -102,17 +116,44 @@ impl wasmtime_environ::Compiler for Compiler {
types: &ModuleTypes,
index: DefinedFuncIndex,
) -> Result<Box<dyn Any + Send>, CompileError> {
let _ = (translation, types, index);
todo!()
let func_index = translation.module.func_index(index);
let sig = translation.module.functions[func_index].signature;
let ty = &types[sig];
let wasm_ty = wasmparser::FuncType::new(
ty.params().iter().copied().map(Into::into),
ty.returns().iter().copied().map(Into::into),
);

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

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

Ok(Box::new(compiled_function))
}

fn compile_wasm_to_native_trampoline(
&self,
translation: &ModuleTranslation<'_>,
_translation: &ModuleTranslation<'_>,
wasm_func_ty: &wasmtime_environ::WasmFuncType,
) -> Result<Box<dyn Any + Send>, CompileError> {
let _ = (translation, wasm_func_ty);
todo!()
let wasm_ty = wasmparser::FuncType::new(
wasm_func_ty.params().iter().copied().map(Into::into),
wasm_func_ty.returns().iter().copied().map(Into::into),
);

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

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

Ok(Box::new(compiled_function))
}

fn append_code(
Expand Down
2 changes: 1 addition & 1 deletion scripts/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const CRATES_TO_PUBLISH: &[&str] = &[
"wiggle-generate",
"wiggle-macro",
// winch
"winch-codegen",
"winch",
// wasmtime
"wasmtime-asm-macros",
Expand All @@ -57,6 +56,7 @@ const CRATES_TO_PUBLISH: &[&str] = &[
"wasmtime-cranelift",
"wasmtime-jit",
"wasmtime-cache",
"winch-codegen",
"winch-environ",
"wasmtime-winch",
"wasmtime",
Expand Down
140 changes: 81 additions & 59 deletions tests/all/winch.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,12 @@
use anyhow::Result;
use wasmtime::*;

#[test]
#[ignore]
fn compiles_with_winch() -> Result<()> {
let mut c = Config::new();

c.strategy(Strategy::Winch);

let engine = Engine::new(&c)?;

// Winch only supports a very basic function signature for now while it's being developed.
let test_mod = r#"
const MODULE: &'static str = r#"
(module
(import "" "" (func $add (param i32 i32) (result i32)))
(func $test (result i32)
(i32.const 42)
)
(export "test" (func $test))
)
"#;

let mut store = Store::new(&engine, ());

let module = Module::new(&engine, test_mod)?;

let instance = Instance::new(&mut store, &module, &[])?;

let f = instance
.get_func(&mut store, "test")
.ok_or(anyhow::anyhow!("test function not found"))?;

let mut returns = vec![Val::null(); 1];

// Winch doesn't support calling typed functions at the moment.
f.call(&mut store, &[], &mut returns)?;

assert_eq!(returns.len(), 1);
assert_eq!(returns[0].unwrap_i32(), 42);

Ok(())
}

#[test]
#[ignore]
fn compiles_with_winch_stack_arguments() -> Result<()> {
let mut c = Config::new();

c.strategy(Strategy::Winch);

let engine = Engine::new(&c)?;

// Winch only supports a very basic function signature for now while it's being developed.
let test_mod = r#"
(module
(func $sum10 (param $arg_1 i32) (param $arg_2 i32) (param $arg_3 i32) (param $arg_4 i32) (param $arg_5 i32) (param $arg_6 i32) (param $arg_7 i32) (param $arg_8 i32) (param $arg_9 i32) (param $arg_10 i32) (result i32)
local.get $arg_1
local.get $arg_2
Expand All @@ -73,30 +27,98 @@ fn compiles_with_winch_stack_arguments() -> Result<()> {
i32.add
local.get $arg_10
i32.add)

(func $call_add (param i32 i32) (result i32)
(local.get 0)
(local.get 1)
(call $add))

(export "42" (func $test))
(export "sum10" (func $sum10))
(export "call_add" (func $call_add))
)
"#;

fn add_fn(store: impl AsContextMut) -> Func {
Func::wrap(store, |a: i32, b: i32| a + b)
}

#[test]
#[cfg_attr(miri, ignore)]
fn array_to_wasm() {
let mut c = Config::new();
c.strategy(Strategy::Winch);
let engine = Engine::new(&c).unwrap();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, MODULE).unwrap();

let module = Module::new(&engine, test_mod)?;
let add_fn = add_fn(store.as_context_mut());
let instance = Instance::new(&mut store, &module, &[add_fn.into()]).unwrap();

let instance = Instance::new(&mut store, &module, &[])?;
let constant = instance
.get_func(&mut store, "42")
.ok_or(anyhow::anyhow!("test function not found"))
.unwrap();
let mut returns = vec![Val::null(); 1];
constant.call(&mut store, &[], &mut returns).unwrap();

let f = instance
.get_func(&mut store, "sum10")
.ok_or(anyhow::anyhow!("sum10 function not found"))?;
assert_eq!(returns.len(), 1);
assert_eq!(returns[0].unwrap_i32(), 42);

let sum = instance
.get_func(&mut store, "sum10")
.ok_or(anyhow::anyhow!("sum10 function not found"))
.unwrap();
let mut returns = vec![Val::null(); 1];

// create a new Val array with ten 1s
let args = vec![Val::I32(1); 10];

// Winch doesn't support calling typed functions at the moment.
f.call(&mut store, &args, &mut returns)?;
sum.call(&mut store, &args, &mut returns).unwrap();

assert_eq!(returns.len(), 1);
assert_eq!(returns[0].unwrap_i32(), 10);
}

#[test]
#[cfg_attr(miri, ignore)]
fn native_to_wasm() {
let mut c = Config::new();
c.strategy(Strategy::Winch);
let engine = Engine::new(&c).unwrap();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, MODULE).unwrap();

let add_fn = add_fn(store.as_context_mut());
let instance = Instance::new(&mut store, &module, &[add_fn.into()]).unwrap();

let f = instance
.get_typed_func::<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>(
&mut store, "sum10",
)
.unwrap();

let args = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
let result = f.call(&mut store, args).unwrap();

assert_eq!(result, 10);
}

#[test]
#[cfg_attr(miri, ignore)]
fn wasm_to_native() {
let mut c = Config::new();
c.strategy(Strategy::Winch);
let engine = Engine::new(&c).unwrap();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, MODULE).unwrap();

let add_fn = add_fn(store.as_context_mut());
let instance = Instance::new(&mut store, &module, &[add_fn.into()]).unwrap();

let f = instance
.get_typed_func::<(i32, i32), i32>(&mut store, "call_add")
.unwrap();

let args = (41, 1);
let result = f.call(&mut store, args).unwrap();

Ok(())
assert_eq!(result, 42);
}
1 change: 1 addition & 0 deletions winch/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target-lexicon = { workspace = true, features = ["std"] }
cranelift-codegen = { workspace = true }
regalloc2 = { workspace = true }
gimli = { workspace = true }
wasmtime-environ = { workspace = true }

[features]
x64 = ["cranelift-codegen/x86"]
Expand Down
8 changes: 8 additions & 0 deletions winch/codegen/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
//! | |
//! | |
//! | Stack slots |
//! | + `VMContext` slot |
//! | + dynamic space |
//! | |
//! | |
Expand Down Expand Up @@ -78,6 +79,13 @@ pub(crate) trait ABI {
/// Returns the designated scratch register.
fn scratch_reg() -> Reg;

/// Returns the frame pointer register.
fn fp_reg() -> Reg;

/// Returns the pinned register used to hold
/// the `VMContext`.
fn vmctx_reg() -> Reg;

/// Returns the callee-saved registers for the given
/// calling convention.
fn callee_saved_regs(call_conv: &CallingConvention) -> SmallVec<[Reg; 9]>;
Expand Down
Loading