Skip to content

Commit

Permalink
Enable introspecting pulley at runtime (bytecodealliance#9886)
Browse files Browse the repository at this point in the history
This commit includes a few assorted changes to improve detection of
whether Wasmtime is using Pulley at runtime:

* A new `Engine::is_pulley` API is added.
* A new `wasmtime_engine_is_pulley` C API is added.
* The `Config::target` method is now available regardless of compilation
  features.
* The `Engine::target` method now consults the `Config` to have the same
  fallback logic for when a target is not explicitly configured.

cc bytecodealliance#1980
  • Loading branch information
alexcrichton authored Dec 30, 2024
1 parent d78544e commit cb51a4f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/c-api/include/wasmtime/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,6 @@ wasmtime_config_cache_config_load(wasm_config_t *, const char *);

#endif // WASMTIME_FEATURE_CACHE

#ifdef WASMTIME_FEATURE_COMPILER

/**
* \brief Configures the target triple that this configuration will produce
* machine code for.
Expand All @@ -398,6 +396,8 @@ wasmtime_config_cache_config_load(wasm_config_t *, const char *);
*/
WASMTIME_CONFIG_PROP(wasmtime_error_t *, target, const char *)

#ifdef WASMTIME_FEATURE_COMPILER

/**
* \brief Enables a target-specific flag in Cranelift.
*
Expand Down
6 changes: 6 additions & 0 deletions crates/c-api/include/wasmtime/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ WASM_API_EXTERN wasm_engine_t *wasmtime_engine_clone(wasm_engine_t *engine);
*/
WASM_API_EXTERN void wasmtime_engine_increment_epoch(wasm_engine_t *engine);

/**
* \brief Returns whether this engine is using the Pulley interpreter to execute
* WebAssembly code.
*/
WASM_API_EXTERN bool wasmtime_engine_is_pulley(wasm_engine_t *engine);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
1 change: 0 additions & 1 deletion crates/c-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ pub extern "C" fn wasmtime_config_native_unwind_info_set(c: &mut wasm_config_t,
}

#[no_mangle]
#[cfg(any(feature = "cranelift", feature = "winch"))]
pub unsafe extern "C" fn wasmtime_config_target_set(
c: &mut wasm_config_t,
target: *const c_char,
Expand Down
5 changes: 5 additions & 0 deletions crates/c-api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ pub extern "C" fn wasmtime_engine_clone(engine: &wasm_engine_t) -> Box<wasm_engi
pub extern "C" fn wasmtime_engine_increment_epoch(engine: &wasm_engine_t) {
engine.engine.increment_epoch();
}

#[no_mangle]
pub extern "C" fn wasmtime_engine_is_pulley(engine: &wasm_engine_t) -> bool {
engine.engine.is_pulley()
}
6 changes: 2 additions & 4 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,8 @@ impl CommonOptions {
collector => config.collector(collector),
_ => err,
}
match_feature! {
["cranelift" : &self.target]
target => config.target(target)?,
_ => err,
if let Some(target) = &self.target {
config.target(target)?;
}
match_feature! {
["cranelift" : self.codegen.cranelift_debug_verifier]
Expand Down
13 changes: 6 additions & 7 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl core::hash::Hash for ModuleVersionStrategy {
pub struct Config {
#[cfg(any(feature = "cranelift", feature = "winch"))]
compiler_config: CompilerConfig,
target: Option<target_lexicon::Triple>,
#[cfg(feature = "gc")]
collector: Collector,
profiling_strategy: ProfilingStrategy,
Expand Down Expand Up @@ -171,7 +172,6 @@ pub struct Config {
#[derive(Debug, Clone)]
struct CompilerConfig {
strategy: Option<Strategy>,
target: Option<target_lexicon::Triple>,
settings: HashMap<String, String>,
flags: HashSet<String>,
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
Expand All @@ -185,7 +185,6 @@ impl CompilerConfig {
fn new() -> Self {
Self {
strategy: Strategy::Auto.not_auto(),
target: None,
settings: HashMap::new(),
flags: HashSet::new(),
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
Expand Down Expand Up @@ -230,6 +229,7 @@ impl Config {
tunables: ConfigTunables::default(),
#[cfg(any(feature = "cranelift", feature = "winch"))]
compiler_config: CompilerConfig::default(),
target: None,
#[cfg(feature = "gc")]
collector: Collector::default(),
#[cfg(feature = "cache")]
Expand Down Expand Up @@ -305,9 +305,8 @@ impl Config {
/// # Errors
///
/// This method will error if the given target triple is not supported.
#[cfg(any(feature = "cranelift", feature = "winch"))]
pub fn target(&mut self, target: &str) -> Result<&mut Self> {
self.compiler_config.target =
self.target =
Some(target_lexicon::Triple::from_str(target).map_err(|e| anyhow::anyhow!(e))?);

Ok(self)
Expand Down Expand Up @@ -2053,10 +2052,10 @@ impl Config {
}

/// Returns the configured compiler target for this `Config`.
fn compiler_target(&self) -> target_lexicon::Triple {
pub(crate) fn compiler_target(&self) -> target_lexicon::Triple {
// If a target is explicitly configured, always use that.
#[cfg(any(feature = "cranelift", feature = "winch"))]
if let Some(target) = self.compiler_config.target.clone() {
if let Some(target) = self.target.clone() {
return target;
}

Expand Down Expand Up @@ -2256,7 +2255,7 @@ impl Config {
// specified (which indicates no feature inference) and the target
// matches the host.
let target_for_builder =
if self.compiler_config.target.is_none() && target == target_lexicon::Triple::host() {
if self.target.is_none() && target == target_lexicon::Triple::host() {
None
} else {
Some(target.clone())
Expand Down
22 changes: 13 additions & 9 deletions crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use object::SectionKind;
use std::{fs::File, path::Path};
use wasmparser::WasmFeatures;
use wasmtime_environ::obj;
use wasmtime_environ::{FlagValue, ObjectKind, Tunables};
use wasmtime_environ::{FlagValue, ObjectKind, TripleExt, Tunables};

mod serialization;

Expand Down Expand Up @@ -223,13 +223,7 @@ impl Engine {
/// Returns the target triple which this engine is compiling code for
/// and/or running code for.
pub(crate) fn target(&self) -> target_lexicon::Triple {
// If a compiler is configured, use that target.
#[cfg(any(feature = "cranelift", feature = "winch"))]
return self.compiler().triple().clone();

// ... otherwise it's the native target
#[cfg(not(any(feature = "cranelift", feature = "winch")))]
return target_lexicon::Triple::host();
return self.config().compiler_target();
}

/// Verify that this engine's configuration is compatible with loading
Expand Down Expand Up @@ -258,7 +252,6 @@ impl Engine {
#[cfg(any(feature = "cranelift", feature = "winch"))]
{
use target_lexicon::Triple;
use wasmtime_environ::TripleExt;

let compiler = self.compiler();

Expand Down Expand Up @@ -529,6 +522,17 @@ impl Engine {
)),
}
}

/// Returns whether this [`Engine`] is configured to execute with Pulley,
/// Wasmtime's interpreter.
///
/// Note that Pulley is the default for host platforms that do not have a
/// Cranelift backend to support them. For example at the time of this
/// writing 32-bit x86 is not supported in Cranelift so the
/// `i686-unknown-linux-gnu` target would by default return `true` here.
pub fn is_pulley(&self) -> bool {
self.target().is_pulley()
}
}

#[cfg(any(feature = "cranelift", feature = "winch"))]
Expand Down

0 comments on commit cb51a4f

Please sign in to comment.