Skip to content

Commit cb51a4f

Browse files
authored
Enable introspecting pulley at runtime (#9886)
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 #1980
1 parent d78544e commit cb51a4f

File tree

7 files changed

+34
-23
lines changed

7 files changed

+34
-23
lines changed

crates/c-api/include/wasmtime/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,6 @@ wasmtime_config_cache_config_load(wasm_config_t *, const char *);
382382

383383
#endif // WASMTIME_FEATURE_CACHE
384384

385-
#ifdef WASMTIME_FEATURE_COMPILER
386-
387385
/**
388386
* \brief Configures the target triple that this configuration will produce
389387
* machine code for.
@@ -398,6 +396,8 @@ wasmtime_config_cache_config_load(wasm_config_t *, const char *);
398396
*/
399397
WASMTIME_CONFIG_PROP(wasmtime_error_t *, target, const char *)
400398

399+
#ifdef WASMTIME_FEATURE_COMPILER
400+
401401
/**
402402
* \brief Enables a target-specific flag in Cranelift.
403403
*

crates/c-api/include/wasmtime/engine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ WASM_API_EXTERN wasm_engine_t *wasmtime_engine_clone(wasm_engine_t *engine);
3535
*/
3636
WASM_API_EXTERN void wasmtime_engine_increment_epoch(wasm_engine_t *engine);
3737

38+
/**
39+
* \brief Returns whether this engine is using the Pulley interpreter to execute
40+
* WebAssembly code.
41+
*/
42+
WASM_API_EXTERN bool wasmtime_engine_is_pulley(wasm_engine_t *engine);
43+
3844
#ifdef __cplusplus
3945
} // extern "C"
4046
#endif

crates/c-api/src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ pub extern "C" fn wasmtime_config_native_unwind_info_set(c: &mut wasm_config_t,
249249
}
250250

251251
#[no_mangle]
252-
#[cfg(any(feature = "cranelift", feature = "winch"))]
253252
pub unsafe extern "C" fn wasmtime_config_target_set(
254253
c: &mut wasm_config_t,
255254
target: *const c_char,

crates/c-api/src/engine.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ pub extern "C" fn wasmtime_engine_clone(engine: &wasm_engine_t) -> Box<wasm_engi
4747
pub extern "C" fn wasmtime_engine_increment_epoch(engine: &wasm_engine_t) {
4848
engine.engine.increment_epoch();
4949
}
50+
51+
#[no_mangle]
52+
pub extern "C" fn wasmtime_engine_is_pulley(engine: &wasm_engine_t) -> bool {
53+
engine.engine.is_pulley()
54+
}

crates/cli-flags/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,8 @@ impl CommonOptions {
576576
collector => config.collector(collector),
577577
_ => err,
578578
}
579-
match_feature! {
580-
["cranelift" : &self.target]
581-
target => config.target(target)?,
582-
_ => err,
579+
if let Some(target) = &self.target {
580+
config.target(target)?;
583581
}
584582
match_feature! {
585583
["cranelift" : self.codegen.cranelift_debug_verifier]

crates/wasmtime/src/config.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl core::hash::Hash for ModuleVersionStrategy {
126126
pub struct Config {
127127
#[cfg(any(feature = "cranelift", feature = "winch"))]
128128
compiler_config: CompilerConfig,
129+
target: Option<target_lexicon::Triple>,
129130
#[cfg(feature = "gc")]
130131
collector: Collector,
131132
profiling_strategy: ProfilingStrategy,
@@ -171,7 +172,6 @@ pub struct Config {
171172
#[derive(Debug, Clone)]
172173
struct CompilerConfig {
173174
strategy: Option<Strategy>,
174-
target: Option<target_lexicon::Triple>,
175175
settings: HashMap<String, String>,
176176
flags: HashSet<String>,
177177
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
@@ -185,7 +185,6 @@ impl CompilerConfig {
185185
fn new() -> Self {
186186
Self {
187187
strategy: Strategy::Auto.not_auto(),
188-
target: None,
189188
settings: HashMap::new(),
190189
flags: HashSet::new(),
191190
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
@@ -230,6 +229,7 @@ impl Config {
230229
tunables: ConfigTunables::default(),
231230
#[cfg(any(feature = "cranelift", feature = "winch"))]
232231
compiler_config: CompilerConfig::default(),
232+
target: None,
233233
#[cfg(feature = "gc")]
234234
collector: Collector::default(),
235235
#[cfg(feature = "cache")]
@@ -305,9 +305,8 @@ impl Config {
305305
/// # Errors
306306
///
307307
/// This method will error if the given target triple is not supported.
308-
#[cfg(any(feature = "cranelift", feature = "winch"))]
309308
pub fn target(&mut self, target: &str) -> Result<&mut Self> {
310-
self.compiler_config.target =
309+
self.target =
311310
Some(target_lexicon::Triple::from_str(target).map_err(|e| anyhow::anyhow!(e))?);
312311

313312
Ok(self)
@@ -2053,10 +2052,10 @@ impl Config {
20532052
}
20542053

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

@@ -2256,7 +2255,7 @@ impl Config {
22562255
// specified (which indicates no feature inference) and the target
22572256
// matches the host.
22582257
let target_for_builder =
2259-
if self.compiler_config.target.is_none() && target == target_lexicon::Triple::host() {
2258+
if self.target.is_none() && target == target_lexicon::Triple::host() {
22602259
None
22612260
} else {
22622261
Some(target.clone())

crates/wasmtime/src/engine.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use object::SectionKind;
1616
use std::{fs::File, path::Path};
1717
use wasmparser::WasmFeatures;
1818
use wasmtime_environ::obj;
19-
use wasmtime_environ::{FlagValue, ObjectKind, Tunables};
19+
use wasmtime_environ::{FlagValue, ObjectKind, TripleExt, Tunables};
2020

2121
mod serialization;
2222

@@ -223,13 +223,7 @@ impl Engine {
223223
/// Returns the target triple which this engine is compiling code for
224224
/// and/or running code for.
225225
pub(crate) fn target(&self) -> target_lexicon::Triple {
226-
// If a compiler is configured, use that target.
227-
#[cfg(any(feature = "cranelift", feature = "winch"))]
228-
return self.compiler().triple().clone();
229-
230-
// ... otherwise it's the native target
231-
#[cfg(not(any(feature = "cranelift", feature = "winch")))]
232-
return target_lexicon::Triple::host();
226+
return self.config().compiler_target();
233227
}
234228

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

263256
let compiler = self.compiler();
264257

@@ -529,6 +522,17 @@ impl Engine {
529522
)),
530523
}
531524
}
525+
526+
/// Returns whether this [`Engine`] is configured to execute with Pulley,
527+
/// Wasmtime's interpreter.
528+
///
529+
/// Note that Pulley is the default for host platforms that do not have a
530+
/// Cranelift backend to support them. For example at the time of this
531+
/// writing 32-bit x86 is not supported in Cranelift so the
532+
/// `i686-unknown-linux-gnu` target would by default return `true` here.
533+
pub fn is_pulley(&self) -> bool {
534+
self.target().is_pulley()
535+
}
532536
}
533537

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

0 commit comments

Comments
 (0)