Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/cranelift/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cranelift_codegen::{
CodegenResult,
};
use std::fmt;
use std::path;
use std::sync::Arc;
use wasmtime_cranelift_shared::isa_builder::IsaBuilder;
use wasmtime_environ::{CacheStore, CompilerBuilder, Setting};
Expand All @@ -17,6 +18,7 @@ struct Builder {
inner: IsaBuilder<CodegenResult<OwnedTargetIsa>>,
linkopts: LinkOptions,
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<path::PathBuf>,
}

#[derive(Clone, Default)]
Expand All @@ -37,6 +39,7 @@ pub fn builder() -> Box<dyn CompilerBuilder> {
inner: IsaBuilder::new(|triple| isa::lookup(triple).map_err(|e| e.into())),
linkopts: LinkOptions::default(),
cache_store: None,
clif_dir: None,
})
}

Expand All @@ -45,6 +48,11 @@ impl CompilerBuilder for Builder {
self.inner.triple()
}

fn clif_dir(&mut self, path: &path::Path) -> Result<()> {
self.clif_dir = Some(path.to_path_buf());
Ok(())
}

fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {
self.inner.target(target)?;
Ok(())
Expand Down Expand Up @@ -74,6 +82,7 @@ impl CompilerBuilder for Builder {
isa,
self.cache_store.clone(),
self.linkopts.clone(),
self.clif_dir.clone(),
)))
}

Expand Down
15 changes: 15 additions & 0 deletions crates/cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::collections::BTreeMap;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::mem;
use std::path;
use std::sync::{Arc, Mutex};
use wasmparser::{FuncValidatorAllocations, FunctionBody};
use wasmtime_cranelift_shared::obj::ModuleTextBuilder;
Expand Down Expand Up @@ -72,6 +73,7 @@ pub(crate) struct Compiler {
isa: OwnedTargetIsa,
linkopts: LinkOptions,
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<path::PathBuf>,
}

impl Drop for Compiler {
Expand Down Expand Up @@ -107,12 +109,14 @@ impl Compiler {
isa: OwnedTargetIsa,
cache_store: Option<Arc<dyn CacheStore>>,
linkopts: LinkOptions,
clif_dir: Option<path::PathBuf>,
) -> Compiler {
Compiler {
contexts: Default::default(),
isa,
linkopts,
cache_store,
clif_dir,
}
}

Expand Down Expand Up @@ -248,6 +252,17 @@ impl wasmtime_environ::Compiler for Compiler {
&mut func_env,
)?;

if let Some(path) = &self.clif_dir {
use std::io::Write;

let mut path = path.to_path_buf();
path.push(format!("wasm_func_{}", func_index.as_u32()));
path.set_extension("clif");

let mut output = std::fs::File::create(path).unwrap();
write!(output, "{}", context.func.display()).unwrap();
}

let (info, func) = compiler.finish_with_info(Some((&body, tunables)))?;

let timing = cranelift_codegen::timing::take_current();
Expand Down
6 changes: 6 additions & 0 deletions crates/environ/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::any::Any;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt;
use std::path;
use std::sync::Arc;
use thiserror::Error;

Expand Down Expand Up @@ -89,6 +90,11 @@ pub trait CompilerBuilder: Send + Sync + fmt::Debug {
/// Sets the target of compilation to the target specified.
fn target(&mut self, target: target_lexicon::Triple) -> Result<()>;

/// Enables clif output in the directory specified.
fn clif_dir(&mut self, _path: &path::Path) -> Result<()> {
anyhow::bail!("clif output not supported");
}

/// Returns the currently configured target triple that compilation will
/// produce artifacts for.
fn triple(&self) -> &target_lexicon::Triple;
Expand Down
14 changes: 13 additions & 1 deletion crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;
#[cfg(feature = "cache")]
#[cfg(any(feature = "cache", compiler))]
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -112,6 +112,7 @@ struct CompilerConfig {
flags: HashSet<String>,
#[cfg(compiler)]
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<std::path::PathBuf>,
}

#[cfg(compiler)]
Expand All @@ -123,6 +124,7 @@ impl CompilerConfig {
settings: HashMap::new(),
flags: HashSet::new(),
cache_store: None,
clif_dir: None,
}
}

Expand Down Expand Up @@ -1539,6 +1541,10 @@ impl Config {
compiler.target(target.clone())?;
}

if let Some(path) = &self.compiler_config.clif_dir {
compiler.clif_dir(path)?;
}

// If probestack is enabled for a target, Wasmtime will always use the
// inline strategy which doesn't require us to define a `__probestack`
// function or similar.
Expand Down Expand Up @@ -1624,6 +1630,12 @@ impl Config {
self.tunables.debug_adapter_modules = debug;
self
}

/// Enables clif output when compiling a WebAssembly module.
#[cfg(compiler)]
pub fn emit_clif(&mut self, path: &Path) {
self.compiler_config.clif_dir = Some(path.to_path_buf());
}
}

fn round_up_to_pages(val: u64) -> u64 {
Expand Down
21 changes: 20 additions & 1 deletion src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub struct CompileCommand {
#[clap(short = 'o', long, value_name = "OUTPUT", parse(from_os_str))]
output: Option<PathBuf>,

/// The directory path to write clif files into, one clif file per wasm function.
#[clap(long = "emit-clif", value_name = "PATH", parse(from_os_str))]
emit_clif: Option<PathBuf>,

/// The path of the WebAssembly to compile
#[clap(index = 1, value_name = "MODULE", parse(from_os_str))]
module: PathBuf,
Expand All @@ -60,7 +64,22 @@ impl CompileCommand {
pub fn execute(mut self) -> Result<()> {
self.common.init_logging();

let config = self.common.config(self.target.as_deref())?;
let mut config = self.common.config(self.target.as_deref())?;

if let Some(path) = self.emit_clif {
if !path.exists() {
std::fs::create_dir(&path)?;
}

if !path.is_dir() {
bail!(
"the path passed for '--emit-clif' ({}) must be a directory",
path.display()
);
}

config.emit_clif(&path);
}

let engine = Engine::new(&config)?;

Expand Down