Skip to content

Commit eb3889d

Browse files
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 <[email protected]>
1 parent cfbfcd3 commit eb3889d

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

crates/environ/src/tunables.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use anyhow::{anyhow, bail, Result};
12
use serde_derive::{Deserialize, Serialize};
3+
use target_lexicon::{PointerWidth, Triple};
24

35
/// Tunable parameters for WebAssembly compilation.
46
#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
@@ -70,6 +72,18 @@ impl Tunables {
7072
}
7173
}
7274

75+
/// Returns the default set of tunables for the given target triple.
76+
pub fn default_for_target(target: &Triple) -> Result<Self> {
77+
match target
78+
.pointer_width()
79+
.map_err(|_| anyhow!("failed to retrieve target pointer width"))?
80+
{
81+
PointerWidth::U32 => Ok(Tunables::default_u32()),
82+
PointerWidth::U64 => Ok(Tunables::default_u64()),
83+
_ => bail!("unsupported target pointer width"),
84+
}
85+
}
86+
7387
/// Returns the default set of tunables for running under MIRI.
7488
pub fn default_miri() -> Tunables {
7589
Tunables {

crates/wasmtime/src/config.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fmt;
66
use std::path::Path;
77
use std::str::FromStr;
88
use std::sync::Arc;
9-
use target_lexicon::{Architecture, PointerWidth};
9+
use target_lexicon::Architecture;
1010
use wasmparser::WasmFeatures;
1111
#[cfg(feature = "cache")]
1212
use wasmtime_cache::CacheConfig;
@@ -1684,11 +1684,7 @@ impl Config {
16841684
let mut tunables = Tunables::default_host();
16851685
#[cfg(any(feature = "cranelift", feature = "winch"))]
16861686
let mut tunables = match &self.compiler_config.target.as_ref() {
1687-
Some(target) => match target.pointer_width() {
1688-
Ok(PointerWidth::U32) => Tunables::default_u32(),
1689-
Ok(PointerWidth::U64) => Tunables::default_u64(),
1690-
_ => bail!("unknown pointer width"),
1691-
},
1687+
Some(target) => Tunables::default_for_target(target)?,
16921688
None => Tunables::default_host(),
16931689
};
16941690

src/commands/settings.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clap::Parser;
55
use serde::{ser::SerializeMap, Serialize};
66
use std::collections::BTreeMap;
77
use std::str::FromStr;
8-
use wasmtime_environ::{CompilerBuilder, FlagValue, Setting, SettingKind};
8+
use wasmtime_environ::{CompilerBuilder, FlagValue, Setting, SettingKind, Tunables};
99

1010
/// Displays available Cranelift settings for a target.
1111
#[derive(Parser, PartialEq)]
@@ -108,10 +108,16 @@ impl SettingsCommand {
108108
pub fn execute(self) -> Result<()> {
109109
// Gather settings from the cranelift compiler builder
110110
let mut builder = wasmtime_cranelift::builder(None)?;
111-
if let Some(target) = &self.target {
111+
let tunables = if let Some(target) = &self.target {
112112
let target = target_lexicon::Triple::from_str(target).map_err(|e| anyhow!(e))?;
113+
let tunables = Tunables::default_for_target(&target)?;
113114
builder.target(target)?;
114-
}
115+
tunables
116+
} else {
117+
Tunables::default_host()
118+
};
119+
120+
builder.set_tunables(tunables)?;
115121
let mut settings = Settings::from_builder(&builder);
116122

117123
// Add inferred settings if no target specified

tests/all/cli_tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,3 +1588,10 @@ mod test_programs {
15881588
Ok(())
15891589
}
15901590
}
1591+
1592+
#[test]
1593+
fn settings_command() -> Result<()> {
1594+
let output = run_wasmtime(&["settings"])?;
1595+
assert!(output.contains("Cranelift settings for target"));
1596+
Ok(())
1597+
}

0 commit comments

Comments
 (0)