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.
242 lines
7.8 KiB
Rust
242 lines
7.8 KiB
Rust
use cranelift_codegen::ir::*;
|
|
use cranelift_codegen::isa::CallConv;
|
|
use cranelift_codegen::settings;
|
|
use cranelift_codegen::{ir::types::I16, Context};
|
|
use cranelift_entity::EntityRef;
|
|
use cranelift_frontend::*;
|
|
use cranelift_module::*;
|
|
use cranelift_object::*;
|
|
|
|
#[test]
|
|
fn error_on_incompatible_sig_in_declare_function() {
|
|
let flag_builder = settings::builder();
|
|
let isa_builder = cranelift_codegen::isa::lookup_by_name("x86_64-unknown-linux-gnu").unwrap();
|
|
let isa = isa_builder
|
|
.finish(settings::Flags::new(flag_builder))
|
|
.unwrap();
|
|
let mut module =
|
|
ObjectModule::new(ObjectBuilder::new(isa, "foo", default_libcall_names()).unwrap());
|
|
let mut sig = Signature {
|
|
params: vec![AbiParam::new(types::I64)],
|
|
returns: vec![],
|
|
call_conv: CallConv::SystemV,
|
|
};
|
|
module
|
|
.declare_function("abc", Linkage::Local, &sig)
|
|
.unwrap();
|
|
sig.params[0] = AbiParam::new(types::I32);
|
|
module
|
|
.declare_function("abc", Linkage::Local, &sig)
|
|
.err()
|
|
.unwrap(); // Make sure this is an error
|
|
}
|
|
|
|
fn define_simple_function(module: &mut ObjectModule) -> FuncId {
|
|
let sig = Signature {
|
|
params: vec![],
|
|
returns: vec![],
|
|
call_conv: CallConv::SystemV,
|
|
};
|
|
|
|
let func_id = module
|
|
.declare_function("abc", Linkage::Local, &sig)
|
|
.unwrap();
|
|
|
|
let mut ctx = Context::new();
|
|
ctx.func = Function::with_name_signature(UserFuncName::user(0, func_id.as_u32()), sig);
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
{
|
|
let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
|
let block = bcx.create_block();
|
|
bcx.switch_to_block(block);
|
|
bcx.ins().return_(&[]);
|
|
}
|
|
|
|
module.define_function(func_id, &mut ctx).unwrap();
|
|
|
|
func_id
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic(expected = "Result::unwrap()` on an `Err` value: DuplicateDefinition(\"abc\")")]
|
|
fn panic_on_define_after_finalize() {
|
|
let flag_builder = settings::builder();
|
|
let isa_builder = cranelift_codegen::isa::lookup_by_name("x86_64-unknown-linux-gnu").unwrap();
|
|
let isa = isa_builder
|
|
.finish(settings::Flags::new(flag_builder))
|
|
.unwrap();
|
|
let mut module =
|
|
ObjectModule::new(ObjectBuilder::new(isa, "foo", default_libcall_names()).unwrap());
|
|
|
|
define_simple_function(&mut module);
|
|
define_simple_function(&mut module);
|
|
}
|
|
|
|
#[test]
|
|
fn switch_error() {
|
|
use cranelift_codegen::settings;
|
|
|
|
let sig = Signature {
|
|
params: vec![AbiParam::new(types::I32)],
|
|
returns: vec![AbiParam::new(types::I32)],
|
|
call_conv: CallConv::SystemV,
|
|
};
|
|
|
|
let mut func = Function::with_name_signature(UserFuncName::default(), sig);
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
{
|
|
let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut func, &mut func_ctx);
|
|
let start = bcx.create_block();
|
|
let bb0 = bcx.create_block();
|
|
let bb1 = bcx.create_block();
|
|
let bb2 = bcx.create_block();
|
|
let bb3 = bcx.create_block();
|
|
println!("{} {} {} {} {}", start, bb0, bb1, bb2, bb3);
|
|
|
|
bcx.declare_var(Variable::new(0), types::I32);
|
|
bcx.declare_var(Variable::new(1), types::I32);
|
|
let in_val = bcx.append_block_param(start, types::I32);
|
|
bcx.switch_to_block(start);
|
|
bcx.def_var(Variable::new(0), in_val);
|
|
bcx.ins().jump(bb0, &[]);
|
|
|
|
bcx.switch_to_block(bb0);
|
|
let discr = bcx.use_var(Variable::new(0));
|
|
let mut switch = cranelift_frontend::Switch::new();
|
|
for &(index, bb) in &[
|
|
(9, bb1),
|
|
(13, bb1),
|
|
(10, bb1),
|
|
(92, bb1),
|
|
(39, bb1),
|
|
(34, bb1),
|
|
] {
|
|
switch.set_entry(index, bb);
|
|
}
|
|
switch.emit(&mut bcx, discr, bb2);
|
|
|
|
bcx.switch_to_block(bb1);
|
|
let v = bcx.use_var(Variable::new(0));
|
|
bcx.def_var(Variable::new(1), v);
|
|
bcx.ins().jump(bb3, &[]);
|
|
|
|
bcx.switch_to_block(bb2);
|
|
let v = bcx.use_var(Variable::new(0));
|
|
bcx.def_var(Variable::new(1), v);
|
|
bcx.ins().jump(bb3, &[]);
|
|
|
|
bcx.switch_to_block(bb3);
|
|
let r = bcx.use_var(Variable::new(1));
|
|
bcx.ins().return_(&[r]);
|
|
|
|
bcx.seal_all_blocks();
|
|
bcx.finalize();
|
|
}
|
|
|
|
let flags = settings::Flags::new(settings::builder());
|
|
match cranelift_codegen::verify_function(&func, &flags) {
|
|
Ok(_) => {}
|
|
Err(err) => {
|
|
let pretty_error =
|
|
cranelift_codegen::print_errors::pretty_verifier_error(&func, None, err);
|
|
panic!("pretty_error:\n{}", pretty_error);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn libcall_function() {
|
|
let flag_builder = settings::builder();
|
|
let isa_builder = cranelift_codegen::isa::lookup_by_name("x86_64-unknown-linux-gnu").unwrap();
|
|
let isa = isa_builder
|
|
.finish(settings::Flags::new(flag_builder))
|
|
.unwrap();
|
|
let mut module =
|
|
ObjectModule::new(ObjectBuilder::new(isa, "foo", default_libcall_names()).unwrap());
|
|
|
|
let sig = Signature {
|
|
params: vec![],
|
|
returns: vec![],
|
|
call_conv: CallConv::SystemV,
|
|
};
|
|
|
|
let func_id = module
|
|
.declare_function("function", Linkage::Local, &sig)
|
|
.unwrap();
|
|
|
|
let mut ctx = Context::new();
|
|
ctx.func = Function::with_name_signature(UserFuncName::user(0, func_id.as_u32()), sig);
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
{
|
|
let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
|
let block = bcx.create_block();
|
|
bcx.switch_to_block(block);
|
|
|
|
let int = module.target_config().pointer_type();
|
|
let zero = bcx.ins().iconst(I16, 0);
|
|
let size = bcx.ins().iconst(int, 10);
|
|
|
|
let mut signature = module.make_signature();
|
|
signature.params.push(AbiParam::new(int));
|
|
signature.returns.push(AbiParam::new(int));
|
|
let callee = module
|
|
.declare_function("malloc", Linkage::Import, &signature)
|
|
.expect("declare malloc function");
|
|
let local_callee = module.declare_func_in_func(callee, &mut bcx.func);
|
|
let argument_exprs = vec![size];
|
|
let call = bcx.ins().call(local_callee, &argument_exprs);
|
|
let buffer = bcx.inst_results(call)[0];
|
|
|
|
bcx.call_memset(module.target_config(), buffer, zero, size);
|
|
|
|
bcx.ins().return_(&[]);
|
|
}
|
|
|
|
module.define_function(func_id, &mut ctx).unwrap();
|
|
|
|
module.finish();
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic(expected = "has a null byte, which is disallowed")]
|
|
fn reject_nul_byte_symbol_for_func() {
|
|
let flag_builder = settings::builder();
|
|
let isa_builder = cranelift_codegen::isa::lookup_by_name("x86_64-unknown-linux-gnu").unwrap();
|
|
let isa = isa_builder
|
|
.finish(settings::Flags::new(flag_builder))
|
|
.unwrap();
|
|
let mut module =
|
|
ObjectModule::new(ObjectBuilder::new(isa, "foo", default_libcall_names()).unwrap());
|
|
|
|
let sig = Signature {
|
|
params: vec![],
|
|
returns: vec![],
|
|
call_conv: CallConv::SystemV,
|
|
};
|
|
|
|
let _ = module
|
|
.declare_function("function\u{0}with\u{0}nul\u{0}bytes", Linkage::Local, &sig)
|
|
.unwrap();
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic(expected = "has a null byte, which is disallowed")]
|
|
fn reject_nul_byte_symbol_for_data() {
|
|
let flag_builder = settings::builder();
|
|
let isa_builder = cranelift_codegen::isa::lookup_by_name("x86_64-unknown-linux-gnu").unwrap();
|
|
let isa = isa_builder
|
|
.finish(settings::Flags::new(flag_builder))
|
|
.unwrap();
|
|
let mut module =
|
|
ObjectModule::new(ObjectBuilder::new(isa, "foo", default_libcall_names()).unwrap());
|
|
|
|
let _ = module
|
|
.declare_data(
|
|
"data\u{0}with\u{0}nul\u{0}bytes",
|
|
Linkage::Local,
|
|
/* writable = */ true,
|
|
/* tls = */ false,
|
|
)
|
|
.unwrap();
|
|
}
|