Add test interpret support to filetests

This commit is contained in:
Andrew Brown
2020-04-21 12:00:45 -07:00
parent 8b18fc5937
commit b26ca3cbdd
6 changed files with 162 additions and 5 deletions

View File

@@ -42,6 +42,7 @@ mod test_cat;
mod test_compile;
mod test_dce;
mod test_domtree;
mod test_interpret;
mod test_legalizer;
mod test_licm;
mod test_postopt;
@@ -122,23 +123,24 @@ fn new_subtest(parsed: &TestCommand) -> subtest::SubtestResult<Box<dyn subtest::
"binemit" => test_binemit::subtest(parsed),
"cat" => test_cat::subtest(parsed),
"compile" => test_compile::subtest(parsed),
"rodata" => test_rodata::subtest(parsed),
"dce" => test_dce::subtest(parsed),
"domtree" => test_domtree::subtest(parsed),
"interpret" => test_interpret::subtest(parsed),
"legalizer" => test_legalizer::subtest(parsed),
"licm" => test_licm::subtest(parsed),
"postopt" => test_postopt::subtest(parsed),
"simple_preopt" => test_simple_preopt::subtest(parsed),
"preopt" => test_preopt::subtest(parsed),
"print-cfg" => test_print_cfg::subtest(parsed),
"regalloc" => test_regalloc::subtest(parsed),
"rodata" => test_rodata::subtest(parsed),
"run" => test_run::subtest(parsed),
"safepoint" => test_safepoint::subtest(parsed),
"shrink" => test_shrink::subtest(parsed),
"simple-gvn" => test_simple_gvn::subtest(parsed),
"simple_preopt" => test_simple_preopt::subtest(parsed),
"unwind" => test_unwind::subtest(parsed),
"vcode" => test_vcode::subtest(parsed),
"verifier" => test_verifier::subtest(parsed),
"preopt" => test_preopt::subtest(parsed),
"safepoint" => test_safepoint::subtest(parsed),
"unwind" => test_unwind::subtest(parsed),
_ => Err(format!("unknown test command '{}'", parsed.command)),
}
}

View File

@@ -0,0 +1,61 @@
//! Test command for interpreting CLIF files and verifying their results
//!
//! The `interpret` test command interprets each function on the host machine using [RunCommand]s.
use crate::subtest::{Context, SubTest, SubtestResult};
use cranelift_codegen::{self, ir};
use cranelift_interpreter::environment::Environment;
use cranelift_interpreter::interpreter::{ControlFlow, Interpreter};
use cranelift_reader::{parse_run_command, TestCommand};
use log::trace;
use std::borrow::Cow;
struct TestInterpret;
pub fn subtest(parsed: &TestCommand) -> SubtestResult<Box<dyn SubTest>> {
assert_eq!(parsed.command, "interpret");
if !parsed.options.is_empty() {
Err(format!("No options allowed on {}", parsed))
} else {
Ok(Box::new(TestInterpret))
}
}
impl SubTest for TestInterpret {
fn name(&self) -> &'static str {
"interpret"
}
fn is_mutating(&self) -> bool {
false
}
fn needs_isa(&self) -> bool {
false
}
fn run(&self, func: Cow<ir::Function>, context: &Context) -> SubtestResult<()> {
for comment in context.details.comments.iter() {
if let Some(command) =
parse_run_command(comment.text, &func.signature).map_err(|e| e.to_string())?
{
trace!("Parsed run command: {}", command);
let mut env = Environment::default();
env.add(func.name.to_string(), func.clone().into_owned());
let interpreter = Interpreter::new(env);
command.run(|func_name, args| {
// Because we have stored function names with a leading %, we need to re-add it.
let func_name = &format!("%{}", func_name);
match interpreter.call_by_name(func_name, args) {
Ok(ControlFlow::Return(results)) => Ok(results),
Ok(_) => panic!("Unexpected returned control flow--this is likely a bug."),
Err(t) => Err(t.to_string()),
}
})?;
}
}
Ok(())
}
}