Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Emit post-optimization CLIF with --emit-clif #10011

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 27 additions & 17 deletions crates/cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,10 @@ 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, &self.tunables)))?;
let (info, func) = compiler.finish_with_info(
Some((&body, &self.tunables)),
&format!("wasm_func_{}", func_index.as_u32()),
)?;

let timing = cranelift_codegen::timing::take_current();
log::debug!("{:?} translated in {:?}", func_index, timing.total());
Expand Down Expand Up @@ -363,7 +355,10 @@ impl wasmtime_environ::Compiler for Compiler {
builder.ins().return_(&[true_return]);
builder.finalize();

Ok(Box::new(compiler.finish()?))
Ok(Box::new(compiler.finish(&format!(
"array_to_wasm_{}",
func_index.as_u32(),
))?))
}

fn compile_wasm_to_array_trampoline(
Expand Down Expand Up @@ -433,7 +428,9 @@ impl wasmtime_environ::Compiler for Compiler {
builder.ins().return_(&results);
builder.finalize();

Ok(Box::new(compiler.finish()?))
Ok(Box::new(compiler.finish(&format!(
"wasm_to_array_trampoline_{wasm_func_ty}"
))?))
}

fn append_code(
Expand Down Expand Up @@ -643,7 +640,9 @@ impl wasmtime_environ::Compiler for Compiler {
builder.ins().return_(&results);
builder.finalize();

Ok(Box::new(compiler.finish()?))
Ok(Box::new(
compiler.finish(&format!("wasm_to_builtin_{}", index.name()))?,
))
}

fn compiled_function_relocation_targets<'a>(
Expand Down Expand Up @@ -959,21 +958,32 @@ impl FunctionCompiler<'_> {
(builder, block0)
}

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

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

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

let mut path = path.join(clif_filename);
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
7 changes: 5 additions & 2 deletions crates/cranelift/src/compiler/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct TrampolineCompiler<'a> {
tunables: &'a Tunables,
}

#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
enum Abi {
Wasm,
Array,
Expand Down Expand Up @@ -746,7 +746,10 @@ impl ComponentCompiler for Compiler {
c.translate(&component.trampolines[index]);
c.builder.finalize();

Ok(Box::new(compiler.finish()?))
Ok(Box::new(compiler.finish(&format!(
"component_trampoline_{}_{abi:?}",
index.as_u32(),
))?))
};
Ok(AllCallFunc {
wasm_call: compile(Abi::Wasm)?,
Expand Down
Loading