Add ability to call CLIF functions with arbitrary arguments in filetests

This resolves the work started in https://github.com/bytecodealliance/cranelift/pull/1231 and https://github.com/bytecodealliance/wasmtime/pull/1436. Cranelift filetests currently have the ability to run CLIF functions with a signature like `() -> b*` and check that the result is true under the `test run` directive. This PR adds the ability to call functions with arbitrary arguments and non-boolean returns and either print the result or check against a list of expected results:
 - `run` commands look like `; run: %add(2, 2) == 4` or `; run: %add(2, 2) != 5` and verify that the executed CLIF function returns the expected value
 - `print` commands look like `; print: %add(2, 2)` and print the result of the function to stdout

To make this work, this PR compiles a single Cranelift `Function` into a `CompiledFunction` using a `SingleFunctionCompiler`. Because we will not know the signature of the function until runtime, we use a `Trampoline` to place the values in the appropriate location for the calling convention; this should look a lot like what @alexcrichton is doing with `VMTrampoline` in wasmtime (see 3b7cb6ee64/crates/api/src/func.rs (L510-L526), 3b7cb6ee64/crates/jit/src/compiler.rs (L260)). To avoid re-compiling `Trampoline`s for the same function signatures, `Trampoline`s are cached in the `SingleFunctionCompiler`.
This commit is contained in:
Andrew Brown
2020-04-15 13:50:51 -07:00
parent 2048d3d30c
commit 38dff29179
8 changed files with 510 additions and 93 deletions

View File

@@ -2,7 +2,7 @@
//!
//! The `run` test command compiles each function on the host machine and executes it
use crate::function_runner::FunctionRunner;
use crate::function_runner::SingleFunctionCompiler;
use crate::subtest::{Context, SubTest, SubtestResult};
use cranelift_codegen::ir;
use cranelift_reader::parse_run_command;
@@ -36,11 +36,11 @@ impl SubTest for TestRun {
}
fn run(&self, func: Cow<ir::Function>, context: &Context) -> SubtestResult<()> {
let mut compiler = SingleFunctionCompiler::with_host_isa(context.flags.clone());
for comment in context.details.comments.iter() {
if comment.text.contains("run") {
let trimmed_comment = comment.text.trim_start_matches(|c| c == ' ' || c == ';');
let command = parse_run_command(trimmed_comment, &func.signature)
.map_err(|e| format!("{}", e))?;
if let Some(command) =
parse_run_command(comment.text, &func.signature).map_err(|e| e.to_string())?
{
trace!("Parsed run command: {}", command);
// If this test requests to run on a completely different
@@ -51,16 +51,15 @@ impl SubTest for TestRun {
return Ok(());
}
// TODO in following changes we will use the parsed command to alter FunctionRunner's behavior.
//
// Note that here we're also explicitly ignoring `context.isa`,
// regardless of what's requested. We want to use the native
// host ISA no matter what here, so the ISA listed in the file
// is only used as a filter to not run into situations like
// running x86_64 code on aarch64 platforms.
let runner =
FunctionRunner::with_host_isa(func.clone().into_owned(), context.flags.clone());
runner.run()?
let compiled_fn = compiler
.compile(func.clone().into_owned())
.map_err(|e| e.to_string())?;
command.run(|args| compiled_fn.call(args))?;
}
}
Ok(())