Skip to content

Commit

Permalink
Enable the tail calling convention by default
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
  • Loading branch information
elliottt and fitzgen committed May 3, 2024
1 parent b704160 commit 68bc800
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
27 changes: 25 additions & 2 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,30 @@ impl Config {
self
}

pub(crate) fn validate(&self) -> Result<Tunables> {
pub(crate) fn validate(mut self) -> Result<(Self, Tunables)> {
// Enable tail calls by default when cranelift is going to be used as the compilation
// strategy, and we're not targeting s390x, which currently lacks tail-call support.
match self.tunables.tail_callable {
None => {
#[cfg(feature = "cranelift")]
let default_tail_calls = self.compiler_config.strategy == Strategy::Cranelift
&& self.compiler_config.target.as_ref().map_or_else(
|| target_lexicon::Triple::host().architecture,
|triple| triple.architecture,
) != Architecture::S390x;
#[cfg(not(feature = "cranelift"))]
let default_tail_calls = false;

self.features
.set(WasmFeatures::TAIL_CALL, default_tail_calls);
self.tunables.tail_callable = Some(default_tail_calls);
}

Some(val) => {
self.features.set(WasmFeatures::TAIL_CALL, val);
}
}

if self.features.contains(WasmFeatures::REFERENCE_TYPES)
&& !self.features.contains(WasmFeatures::BULK_MEMORY)
{
Expand Down Expand Up @@ -1820,7 +1843,7 @@ impl Config {
bail!("static memory guard size cannot be smaller than dynamic memory guard size");
}

Ok(tunables)
Ok((self, tunables))
}

#[cfg(feature = "runtime")]
Expand Down
3 changes: 1 addition & 2 deletions crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ impl Engine {
crate::runtime::vm::debug_builtins::ensure_exported();
}

let config = config.clone();
let tunables = config.validate()?;
let (config, tunables) = config.clone().validate()?;

#[cfg(any(feature = "cranelift", feature = "winch"))]
let (config, compiler) = config.build_compiler(&tunables)?;
Expand Down
62 changes: 62 additions & 0 deletions tests/all/defaults.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use wasmtime::*;

#[test]
fn test_tail_call_default() -> Result<()> {
for (expected, cfg) in [
(
true,
Config::new()
.strategy(Strategy::Cranelift)
.target("x86_64")?,
),
(
true,
Config::new()
.strategy(Strategy::Cranelift)
.target("aarch64")?,
),
(
true,
Config::new()
.strategy(Strategy::Cranelift)
.target("riscv64")?,
),
(
false,
Config::new()
.strategy(Strategy::Cranelift)
.target("s390x")?,
),
(
false,
Config::new().strategy(Strategy::Winch).target("x86_64")?,
),
(
false,
Config::new().strategy(Strategy::Winch).target("aarch64")?,
),
(
false,
Config::new()
.strategy(Strategy::Cranelift)
.wasm_tail_call(false)
.target("x86_64")?,
),
] {
let engine = Engine::new(cfg)?;

let wat = r#"
(module $from_name_section
(func (export "run") (return_call 0))
)
"#;

let result = engine.precompile_module(wat.as_bytes()).map(|_| ());

eprintln!("for config {cfg:?}, got: {result:?}");

assert_eq!(expected, result.is_ok());
}

Ok(())
}
1 change: 1 addition & 0 deletions tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod code_too_large;
mod component_model;
mod coredump;
mod debug;
mod defaults;
mod epoch_interruption;
mod externals;
mod fuel;
Expand Down

0 comments on commit 68bc800

Please sign in to comment.