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 (see3b7cb6ee64/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:
@@ -2,9 +2,9 @@
|
||||
|
||||
use crate::utils::read_to_string;
|
||||
use cranelift_codegen::isa::{CallConv, TargetIsa};
|
||||
use cranelift_filetests::FunctionRunner;
|
||||
use cranelift_filetests::SingleFunctionCompiler;
|
||||
use cranelift_native::builder as host_isa_builder;
|
||||
use cranelift_reader::{parse_test, Details, IsaSpec, ParseOptions};
|
||||
use cranelift_reader::{parse_run_command, parse_test, Details, IsaSpec, ParseOptions};
|
||||
use std::path::PathBuf;
|
||||
use target_lexicon::Triple;
|
||||
use walkdir::WalkDir;
|
||||
@@ -90,12 +90,16 @@ fn run_file_contents(file_contents: String) -> Result<(), String> {
|
||||
..ParseOptions::default()
|
||||
};
|
||||
let test_file = parse_test(&file_contents, options).map_err(|e| e.to_string())?;
|
||||
let isa = create_target_isa(&test_file.isa_spec)?;
|
||||
let mut compiler = SingleFunctionCompiler::new(isa);
|
||||
for (func, Details { comments, .. }) in test_file.functions {
|
||||
if comments.iter().any(|c| c.text.contains("run")) {
|
||||
// TODO in following changes we will parse this comment to alter the FunctionRunner's behavior.
|
||||
let isa = create_target_isa(&test_file.isa_spec)?;
|
||||
// TODO the following no longer makes sense; use FunctionRunner::with_host_isa(...) instead
|
||||
FunctionRunner::new(func, isa).run()?
|
||||
for comment in comments {
|
||||
if let Some(command) =
|
||||
parse_run_command(comment.text, &func.signature).map_err(|e| e.to_string())?
|
||||
{
|
||||
let compiled_fn = compiler.compile(func.clone()).map_err(|e| e.to_string())?;
|
||||
command.run(|args| compiled_fn.call(args))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user