Files
wasmtime/cranelift/src/wasm.rs
Benjamin Bouvier 8a9b1a9025 Implement an incremental compilation cache for Cranelift (#4551)
This is the implementation of https://github.com/bytecodealliance/wasmtime/issues/4155, using the "inverted API" approach suggested by @cfallin (thanks!) in Cranelift, and trait object to provide a backend for an all-included experience in Wasmtime. 

After the suggestion of Chris, `Function` has been split into mostly two parts:

- on the one hand, `FunctionStencil` contains all the fields required during compilation, and that act as a compilation cache key: if two function stencils are the same, then the result of their compilation (`CompiledCodeBase<Stencil>`) will be the same. This makes caching trivial, as the only thing to cache is the `FunctionStencil`.
- on the other hand, `FunctionParameters` contain the... function parameters that are required to finalize the result of compilation into a `CompiledCode` (aka `CompiledCodeBase<Final>`) with proper final relocations etc., by applying fixups and so on.

Most changes are here to accomodate those requirements, in particular that `FunctionStencil` should be `Hash`able to be used as a key in the cache:

- most source locations are now relative to a base source location in the function, and as such they're encoded as `RelSourceLoc` in the `FunctionStencil`. This required changes so that there's no need to explicitly mark a `SourceLoc` as the base source location, it's automatically detected instead the first time a non-default `SourceLoc` is set.
- user-defined external names in the `FunctionStencil` (aka before this patch `ExternalName::User { namespace, index }`) are now references into an external table of `UserExternalNameRef -> UserExternalName`, present in the `FunctionParameters`, and must be explicitly declared using `Function::declare_imported_user_function`.
- some refactorings have been made for function names:
  - `ExternalName` was used as the type for a `Function`'s name; while it thus allowed `ExternalName::Libcall` in this place, this would have been quite confusing to use it there. Instead, a new enum `UserFuncName` is introduced for this name, that's either a user-defined function name (the above `UserExternalName`) or a test case name.
  - The future of `ExternalName` is likely to become a full reference into the `FunctionParameters`'s mapping, instead of being "either a handle for user-defined external names, or the thing itself for other variants". I'm running out of time to do this, and this is not trivial as it implies touching ISLE which I'm less familiar with.

The cache computes a sha256 hash of the `FunctionStencil`, and uses this as the cache key. No equality check (using `PartialEq`) is performed in addition to the hash being the same, as we hope that this is sufficient data to avoid collisions.

A basic fuzz target has been introduced that tries to do the bare minimum:

- check that a function successfully compiled and cached will be also successfully reloaded from the cache, and returns the exact same function.
- check that a trivial modification in the external mapping of `UserExternalNameRef -> UserExternalName` hits the cache, and that other modifications don't hit the cache.
  - This last check is less efficient and less likely to happen, so probably should be rethought a bit.

Thanks to both @alexcrichton and @cfallin for your very useful feedback on Zulip.

Some numbers show that for a large wasm module we're using internally, this is a 20% compile-time speedup, because so many `FunctionStencil`s are the same, even within a single module. For a group of modules that have a lot of code in common, we get hit rates up to 70% when they're used together. When a single function changes in a wasm module, every other function is reloaded; that's still slower than I expect (between 10% and 50% of the overall compile time), so there's likely room for improvement. 

Fixes #4155.
2022-08-12 16:47:43 +00:00

342 lines
10 KiB
Rust

//! CLI tool to use the functions provided by the [cranelift-wasm](../cranelift_wasm/index.html)
//! crate.
//!
//! Reads Wasm binary/text files, translates the functions' code to Cranelift IR.
#![cfg_attr(
feature = "cargo-clippy",
allow(clippy::too_many_arguments, clippy::cognitive_complexity)
)]
use crate::disasm::print_all;
use crate::utils::parse_sets_and_triple;
use anyhow::{Context as _, Result};
use clap::Parser;
use cranelift_codegen::ir::DisplayFunctionAnnotations;
use cranelift_codegen::print_errors::{pretty_error, pretty_verifier_error};
use cranelift_codegen::settings::FlagsOrIsa;
use cranelift_codegen::timing;
use cranelift_codegen::Context;
use cranelift_entity::EntityRef;
use cranelift_wasm::{translate_module, DummyEnvironment, FuncIndex};
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
/// For verbose printing: only print if the `$x` expression is true.
macro_rules! vprintln {
($x: expr, $($tts:tt)*) => {
if $x {
println!($($tts)*);
}
};
}
/// For verbose printing: prints in color if the `$x` expression is true.
macro_rules! vcprintln {
($x: expr, $use_color: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x {
if $use_color {
$term.set_color(ColorSpec::new().set_fg(Some($color)))?;
}
println!($($tts)*);
if $use_color {
$term.reset()?;
}
}
};
}
/// For verbose printing: prints in color (without an appended newline) if the `$x` expression is true.
macro_rules! vcprint {
($x: expr, $use_color: expr, $term: ident, $color: expr, $($tts:tt)*) => {
if $x {
if $use_color {
$term.set_color(ColorSpec::new().set_fg(Some($color)))?;
}
print!($($tts)*);
if $use_color {
$term.reset()?;
}
}
};
}
/// Compiles Wasm binary/text into Cranelift IR and then into target language
#[derive(Parser)]
pub struct Options {
/// Be more verbose
#[clap(short, long)]
verbose: bool,
/// Print the resulting Cranelift IR
#[clap(short)]
print: bool,
/// Print pass timing report
#[clap(short = 'T')]
report_times: bool,
/// Print machine code disassembly
#[clap(short = 'D', long)]
disasm: bool,
/// Configure Cranelift settings
#[clap(long = "set")]
settings: Vec<String>,
/// Specify the Cranelift target
#[clap(long = "target")]
target: String,
/// Specify an input file to be used. Use '-' for stdin.
files: Vec<PathBuf>,
/// Print bytecode size
#[clap(short = 'X')]
print_size: bool,
/// Just decode Wasm into Cranelift IR, don't compile it to native code
#[clap(short = 't')]
just_decode: bool,
/// Just checks the correctness of Cranelift IR translated from Wasm
#[clap(short = 'c')]
check_translation: bool,
/// Display values' ranges and their locations
#[clap(long = "value-ranges")]
value_ranges: bool,
/// Use colors in output? [options: auto/never/always; default: auto]
#[clap(long = "color", default_value("auto"))]
color: ColorOpt,
}
#[derive(PartialEq, Eq)]
enum ColorOpt {
Auto,
Never,
Always,
}
impl std::str::FromStr for ColorOpt {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_lowercase();
match s.as_str() {
"auto" => Ok(ColorOpt::Auto),
"never" => Ok(ColorOpt::Never),
"always" => Ok(ColorOpt::Always),
_ => Err(format!("expected auto/never/always, found: {}", s)),
}
}
}
pub fn run(options: &Options) -> Result<()> {
let parsed = parse_sets_and_triple(&options.settings, &options.target)?;
for path in &options.files {
let name = String::from(path.as_os_str().to_string_lossy());
handle_module(options, path, &name, parsed.as_fisa())?;
}
Ok(())
}
fn handle_module(options: &Options, path: &Path, name: &str, fisa: FlagsOrIsa) -> Result<()> {
let color_choice = match options.color {
ColorOpt::Auto => ColorChoice::Auto,
ColorOpt::Always => ColorChoice::Always,
ColorOpt::Never => ColorChoice::Never,
};
let mut terminal = StandardStream::stdout(color_choice);
let use_color = terminal.supports_color() && options.color == ColorOpt::Auto
|| options.color == ColorOpt::Always;
vcprint!(
options.verbose,
use_color,
terminal,
Color::Yellow,
"Handling: "
);
vprintln!(options.verbose, "\"{}\"", name);
vcprint!(
options.verbose,
use_color,
terminal,
Color::Magenta,
"Translating... "
);
let module_binary = if path.to_str() == Some("-") {
let stdin = std::io::stdin();
let mut buf = Vec::new();
stdin
.lock()
.read_to_end(&mut buf)
.context("failed to read stdin")?;
wat::parse_bytes(&buf)?.into()
} else {
wat::parse_file(path)?
};
let isa = match fisa.isa {
Some(isa) => isa,
None => {
anyhow::bail!("Error: the wasm command requires an explicit isa.");
}
};
let debug_info = options.value_ranges;
let mut dummy_environ = DummyEnvironment::new(isa.frontend_config(), debug_info);
translate_module(&module_binary, &mut dummy_environ)?;
vcprintln!(options.verbose, use_color, terminal, Color::Green, "ok");
if options.just_decode {
if !options.print {
return Ok(());
}
let num_func_imports = dummy_environ.get_num_func_imports();
for (def_index, func) in dummy_environ.info.function_bodies.iter() {
let func_index = num_func_imports + def_index.index();
let mut context = Context::new();
context.func = func.clone();
if let Some(start_func) = dummy_environ.info.start_func {
if func_index == start_func.index() {
println!("; Selected as wasm start function");
}
}
vprintln!(options.verbose, "");
for export_name in
&dummy_environ.info.functions[FuncIndex::new(func_index)].export_names
{
println!("; Exported as \"{}\"", export_name);
}
println!("{}", context.func.display());
vprintln!(options.verbose, "");
}
let _ = terminal.reset();
return Ok(());
}
if options.check_translation {
vcprint!(
options.verbose,
use_color,
terminal,
Color::Magenta,
"Checking... "
);
} else {
vcprint!(
options.verbose,
use_color,
terminal,
Color::Magenta,
"Compiling... "
);
}
if options.print_size {
vprintln!(options.verbose, "");
}
let num_func_imports = dummy_environ.get_num_func_imports();
let mut total_module_code_size = 0;
let mut context = Context::new();
for (def_index, func) in dummy_environ.info.function_bodies.iter() {
context.func = func.clone();
let mut saved_size = None;
let func_index = num_func_imports + def_index.index();
let mut mem = vec![];
let (relocs, traps, stack_maps) = if options.check_translation {
if let Err(errors) = context.verify(fisa) {
anyhow::bail!("{}", pretty_verifier_error(&context.func, None, errors));
}
(vec![], vec![], vec![])
} else {
let compiled_code = context
.compile_and_emit(isa, &mut mem)
.map_err(|err| anyhow::anyhow!("{}", pretty_error(&err.func, err.inner)))?;
let code_info = compiled_code.code_info();
if options.print_size {
println!(
"Function #{} code size: {} bytes",
func_index, code_info.total_size,
);
total_module_code_size += code_info.total_size;
println!(
"Function #{} bytecode size: {} bytes",
func_index,
dummy_environ.func_bytecode_sizes[def_index.index()]
);
}
if options.disasm {
saved_size = Some(code_info.total_size);
}
(
compiled_code.buffer.relocs().to_vec(),
compiled_code.buffer.traps().to_vec(),
compiled_code.buffer.stack_maps().to_vec(),
)
};
if options.print {
vprintln!(options.verbose, "");
if let Some(start_func) = dummy_environ.info.start_func {
if func_index == start_func.index() {
println!("; Selected as wasm start function");
}
}
for export_name in
&dummy_environ.info.functions[FuncIndex::new(func_index)].export_names
{
println!("; Exported as \"{}\"", export_name);
}
let value_ranges = if options.value_ranges {
Some(context.compiled_code().unwrap().value_labels_ranges.clone())
} else {
None
};
println!(
"{}",
context.func.display_with(DisplayFunctionAnnotations {
value_ranges: value_ranges.as_ref(),
})
);
vprintln!(options.verbose, "");
}
if let Some(total_size) = saved_size {
print_all(
isa,
&context.func.params,
&mem,
total_size,
options.print,
&relocs,
&traps,
&stack_maps,
)?;
}
context.clear();
}
if !options.check_translation && options.print_size {
println!("Total module code size: {} bytes", total_module_code_size);
let total_bytecode_size: usize = dummy_environ.func_bytecode_sizes.iter().sum();
println!("Total module bytecode size: {} bytes", total_bytecode_size);
}
if options.report_times {
println!("{}", timing::take_current());
}
vcprintln!(options.verbose, use_color, terminal, Color::Green, "ok");
Ok(())
}