Skip to content

Commit

Permalink
Fix wasmtime settings command. (#8060) (#8127)
Browse files Browse the repository at this point in the history
* Fix `wasmtime settings` command.

Currently the `settings` command panics because the tunables are not set in the
compiler builder.

This commit creates a default tunables based on either the target triple passed
on the command line or uses the default for the host.

Fixes #8058.

* Additional clean up.

Co-authored-by: Peter Huene <[email protected]>
  • Loading branch information
alexcrichton and peterhuene authored Mar 14, 2024
1 parent cfbfcd3 commit eb3889d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
14 changes: 14 additions & 0 deletions crates/environ/src/tunables.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::{anyhow, bail, Result};
use serde_derive::{Deserialize, Serialize};
use target_lexicon::{PointerWidth, Triple};

/// Tunable parameters for WebAssembly compilation.
#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -70,6 +72,18 @@ impl Tunables {
}
}

/// Returns the default set of tunables for the given target triple.
pub fn default_for_target(target: &Triple) -> Result<Self> {
match target
.pointer_width()
.map_err(|_| anyhow!("failed to retrieve target pointer width"))?
{
PointerWidth::U32 => Ok(Tunables::default_u32()),
PointerWidth::U64 => Ok(Tunables::default_u64()),
_ => bail!("unsupported target pointer width"),
}
}

/// Returns the default set of tunables for running under MIRI.
pub fn default_miri() -> Tunables {
Tunables {
Expand Down
8 changes: 2 additions & 6 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
use target_lexicon::{Architecture, PointerWidth};
use target_lexicon::Architecture;
use wasmparser::WasmFeatures;
#[cfg(feature = "cache")]
use wasmtime_cache::CacheConfig;
Expand Down Expand Up @@ -1684,11 +1684,7 @@ impl Config {
let mut tunables = Tunables::default_host();
#[cfg(any(feature = "cranelift", feature = "winch"))]
let mut tunables = match &self.compiler_config.target.as_ref() {
Some(target) => match target.pointer_width() {
Ok(PointerWidth::U32) => Tunables::default_u32(),
Ok(PointerWidth::U64) => Tunables::default_u64(),
_ => bail!("unknown pointer width"),
},
Some(target) => Tunables::default_for_target(target)?,
None => Tunables::default_host(),
};

Expand Down
12 changes: 9 additions & 3 deletions src/commands/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Parser;
use serde::{ser::SerializeMap, Serialize};
use std::collections::BTreeMap;
use std::str::FromStr;
use wasmtime_environ::{CompilerBuilder, FlagValue, Setting, SettingKind};
use wasmtime_environ::{CompilerBuilder, FlagValue, Setting, SettingKind, Tunables};

/// Displays available Cranelift settings for a target.
#[derive(Parser, PartialEq)]
Expand Down Expand Up @@ -108,10 +108,16 @@ impl SettingsCommand {
pub fn execute(self) -> Result<()> {
// Gather settings from the cranelift compiler builder
let mut builder = wasmtime_cranelift::builder(None)?;
if let Some(target) = &self.target {
let tunables = if let Some(target) = &self.target {
let target = target_lexicon::Triple::from_str(target).map_err(|e| anyhow!(e))?;
let tunables = Tunables::default_for_target(&target)?;
builder.target(target)?;
}
tunables
} else {
Tunables::default_host()
};

builder.set_tunables(tunables)?;
let mut settings = Settings::from_builder(&builder);

// Add inferred settings if no target specified
Expand Down
7 changes: 7 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,3 +1588,10 @@ mod test_programs {
Ok(())
}
}

#[test]
fn settings_command() -> Result<()> {
let output = run_wasmtime(&["settings"])?;
assert!(output.contains("Cranelift settings for target"));
Ok(())
}

0 comments on commit eb3889d

Please sign in to comment.