From eb3889d3fafe3ce1fbcaad30b3119a0ef36f9f0b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 14 Mar 2024 09:13:47 -0500 Subject: [PATCH] Fix `wasmtime settings` command. (#8060) (#8127) * 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 --- crates/environ/src/tunables.rs | 14 ++++++++++++++ crates/wasmtime/src/config.rs | 8 ++------ src/commands/settings.rs | 12 +++++++++--- tests/all/cli_tests.rs | 7 +++++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/crates/environ/src/tunables.rs b/crates/environ/src/tunables.rs index 4ff360fcb523..7328345ee765 100644 --- a/crates/environ/src/tunables.rs +++ b/crates/environ/src/tunables.rs @@ -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)] @@ -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 { + 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 { diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs index b9a22efdfbac..35e9fe4ba787 100644 --- a/crates/wasmtime/src/config.rs +++ b/crates/wasmtime/src/config.rs @@ -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; @@ -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(), }; diff --git a/src/commands/settings.rs b/src/commands/settings.rs index 1a4a967f02dc..7055f9203a70 100644 --- a/src/commands/settings.rs +++ b/src/commands/settings.rs @@ -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)] @@ -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 diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs index 175d3de1a709..8fab74ac6484 100644 --- a/tests/all/cli_tests.rs +++ b/tests/all/cli_tests.rs @@ -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(()) +}