Skip to content

Commit

Permalink
Added an --emit-opt-clif CLI option
Browse files Browse the repository at this point in the history
The option --emit-opt-clif follows the --emit-clif implementation, but
the output comes with the egraph optimizations applied.

* This commit does not include support for the wasmtime explorer.
  • Loading branch information
dimitris-aspetakis committed Aug 20, 2024
1 parent a2f0f2f commit efb423e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
8 changes: 8 additions & 0 deletions crates/cranelift/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct Builder {
linkopts: LinkOptions,
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<path::PathBuf>,
opt_clif_dir: Option<path::PathBuf>,
wmemcheck: bool,
}

Expand All @@ -44,6 +45,7 @@ pub fn builder(triple: Option<Triple>) -> Result<Box<dyn CompilerBuilder>> {
linkopts: LinkOptions::default(),
cache_store: None,
clif_dir: None,
opt_clif_dir: None,
wmemcheck: false,
}))
}
Expand All @@ -58,6 +60,11 @@ impl CompilerBuilder for Builder {
Ok(())
}

fn opt_clif_dir(&mut self, path: &path::Path) -> Result<()> {
self.opt_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 @@ -97,6 +104,7 @@ impl CompilerBuilder for Builder {
self.cache_store.clone(),
self.linkopts.clone(),
self.clif_dir.clone(),
self.opt_clif_dir.clone(),
self.wmemcheck,
)))
}
Expand Down
25 changes: 23 additions & 2 deletions crates/cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::compiler::path::PathBuf;
use crate::debug::DwarfSectionRelocTarget;
use crate::func_environ::FuncEnvironment;
use crate::DEBUG_ASSERT_TRAP_CODE;
Expand Down Expand Up @@ -69,6 +70,7 @@ pub struct Compiler {
linkopts: LinkOptions,
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<path::PathBuf>,
opt_clif_dir: Option<path::PathBuf>,
wmemcheck: bool,
}

Expand Down Expand Up @@ -107,6 +109,7 @@ impl Compiler {
cache_store: Option<Arc<dyn CacheStore>>,
linkopts: LinkOptions,
clif_dir: Option<path::PathBuf>,
opt_clif_dir: Option<path::PathBuf>,
wmemcheck: bool,
) -> Compiler {
Compiler {
Expand All @@ -116,6 +119,7 @@ impl Compiler {
linkopts,
cache_store,
clif_dir,
opt_clif_dir,
wmemcheck,
}
}
Expand Down Expand Up @@ -228,7 +232,11 @@ impl wasmtime_environ::Compiler for Compiler {
write!(output, "{}", context.func.display()).unwrap();
}

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

let timing = cranelift_codegen::timing::take_current();
log::debug!("{:?} translated in {:?}", func_index, timing.total());
Expand Down Expand Up @@ -799,21 +807,34 @@ impl FunctionCompiler<'_> {
}

fn finish(self) -> Result<CompiledFunction, CompileError> {
let (info, func) = self.finish_with_info(None)?;
let (info, func) = self.finish_with_info(None, &None, 0)?;
assert!(info.stack_maps.is_empty());
Ok(func)
}

fn finish_with_info(
mut self,
body_and_tunables: Option<(&FunctionBody<'_>, &Tunables)>,
opt_clif_dir: &Option<PathBuf>,
func_index: u32,
) -> Result<(WasmFunctionInfo, CompiledFunction), CompileError> {
let context = &mut self.cx.codegen_context;
let isa = &*self.compiler.isa;
let (_, _code_buf) =
compile_maybe_cached(context, isa, self.cx.incremental_cache_ctx.as_mut())?;
let mut compiled_code = context.take_compiled_code().unwrap();

if let Some(path) = opt_clif_dir {
use std::io::Write;

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

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

// Give wasm functions, user defined code, a "preferred" alignment
// instead of the minimum alignment as this can help perf in niche
// situations.
Expand Down
5 changes: 5 additions & 0 deletions crates/environ/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ pub trait CompilerBuilder: Send + Sync + fmt::Debug {
anyhow::bail!("clif output not supported");
}

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

/// Returns the currently configured target triple that compilation will
/// produce artifacts for.
fn triple(&self) -> &target_lexicon::Triple;
Expand Down
13 changes: 13 additions & 0 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ struct CompilerConfig {
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<std::path::PathBuf>,
opt_clif_dir: Option<std::path::PathBuf>,
wmemcheck: bool,
}

Expand All @@ -177,6 +178,7 @@ impl CompilerConfig {
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
cache_store: None,
clif_dir: None,
opt_clif_dir: None,
wmemcheck: false,
}
}
Expand Down Expand Up @@ -1897,6 +1899,10 @@ impl Config {
compiler.clif_dir(path)?;
}

if let Some(path) = &self.compiler_config.opt_clif_dir {
compiler.opt_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 @@ -1999,6 +2005,13 @@ impl Config {
self
}

/// Enables optimized clif output when compiling a WebAssembly module.
#[cfg(any(feature = "cranelift", feature = "winch"))]
pub fn emit_opt_clif(&mut self, path: &Path) -> &mut Self {
self.compiler_config.opt_clif_dir = Some(path.to_path_buf());
self
}

/// Configures whether, when on macOS, Mach ports are used for exception
/// handling instead of traditional Unix-based signal handling.
///
Expand Down
19 changes: 19 additions & 0 deletions src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub struct CompileCommand {
#[arg(long = "emit-clif", value_name = "PATH")]
pub emit_clif: Option<PathBuf>,

/// The directory path to write optimized clif files into, one clif file per wasm function.
#[arg(long = "emit-opt-clif", value_name = "PATH")]
pub emit_opt_clif: Option<PathBuf>,

/// The path of the WebAssembly to compile
#[arg(index = 1, value_name = "MODULE")]
pub module: PathBuf,
Expand Down Expand Up @@ -78,6 +82,21 @@ impl CompileCommand {
config.emit_clif(&path);
}

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

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

config.emit_opt_clif(&path);
}

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

if self.module.file_name().is_none() {
Expand Down

0 comments on commit efb423e

Please sign in to comment.